// fm.rate.js // ver. 1.31  // 2011 Apr 04 Mo //

fm.deb = "";
fm.c_price_addons = "<span class='price_star'>&nbsp;*</span>";

fm.FSRate = function() {
  this.curNames   = null; // [id, symbol, name, ...]
  this.rates      = null; // [idFrom, idTo, exchRate, ... ]
};

fm.FSRate.prototype.convert = function( argPrice, argCurIdOld, argCurIdNew, argFormat ) {
  //fm.deb += "convert( " + argPrice + ", " + argCurIdOld + ", " + argCurIdNew + ", " + argFormat + ")\n" 
  var result = null;

  if (argCurIdNew != 0) {
    var curName = this.getCurName( argCurIdNew, argFormat );
    //
    if (argCurIdOld != argCurIdNew) {
      //
      for (var n=0; n<this.rates.length; n+=3) {
        var curIdFor  = this.rates[n];
        var curIdWhat = this.rates[n+1];
        //
        if (curIdFor == argCurIdOld && curIdWhat == argCurIdNew) {
          //fm.deb += "convert - 2. n: " + n + " curIdFor: " + curIdFor + " curIdWhat: " + curIdWhat + "\n";
          var rate = this.rates[n+2];
          var newPrice = Math.round( argPrice * rate );
          //fm.deb += "convert - 3. rate: " + rate + " newPrice: " + newPrice + "\n";
          result = "" + newPrice + (argFormat > 0 ? " " : "") + curName;
          //fm.deb += "convert - 4. result: " + result + "\n";
          break;
        }
      }
    }
    else {
      // curIdOld == curIdNew
      result = "" + argPrice + (argFormat > 0 ? " " : "") + curName;
    }
  }
  else {
    // curIdNew == 0
    var curNameOrig = this.getCurName( argCurIdOld, argFormat );
    result = "" + argPrice + (argFormat > 0 ? " " : "") + curNameOrig;
  }
  if (argFormat == 3) {
    result += fm.c_price_addons;
  }
  return result;
};

fm.FSRate.prototype.getCurName = function( argCurId, argFormat ) {
  var result = "?";
  //
  for (var n=0; n<this.curNames.length; n+=3) {
    var curId = this.curNames[n];
    //
    if (curId == argCurId) {
      if (argFormat == 0 || argFormat == 2) {
        result = this.curNames[n+1];
      }
      else {
        result = this.curNames[n+2];
      }
      break;
    }
  }
  return result;
};

fm.FSRate.prototype.onChange = function( argSelectId ) {
  var curIdNew = fm.Select_getSelValue( argSelectId );
  //
  if (curIdNew == null) return;
  //
  //alert( "onChange - curIdNew: " + curIdNew + "\n");
  var expireDays = 365*2;
  fm.Cookie_set( "nt.curIdMain", curIdNew, expireDays );
  fm.rate.setPrices( curIdNew );
    //alert( "onChange - f. " + fm.deb );
};

fm.FSRate.prototype.setCurNames = function( argCurNames ) {
  this.curNames = argCurNames;
};

fm.FSRate.prototype.setPrices = function( argCurIdNew ) {
  //fm.deb += ".setPrices - argCurIdNew: " + argCurIdNew + "\n";
  var elems = document.getElementsByTagName("span");
  //
  if (elems == null) return;
  //
  for (var n=0; n<elems.length; n++) {
    var elem = elems[n];
    var elemName = fm.Elem_getAttr( elem, "name");
    //
    if (elemName != null && elemName == "price") {
      //fm.deb += " elemName: " + elemName;
      var elemValue = fm.Elem_getAttr( elem, "value");
      //
      if (elemValue != null) {
        //fm.deb += " elemValue: " + elemValue + "\n";
        var priceTriple = eval( elemValue );
        //
        if (priceTriple != null && priceTriple instanceof Array) {
          var priceValue = priceTriple[0];
          var curIdOrig  = priceTriple[1];
          var format     = priceTriple[2];
          //fm.deb += "priceValue: " + priceValue + " curIdOrig: " + curIdOrig + " format: " + format + "\n";
                    //
          var newPrice = this.convert( priceValue, curIdOrig, argCurIdNew, format );
          //fm.deb += " newPrice: " + newPrice + "\n";
          //
          if (newPrice != null) {
            elem.innerHTML = newPrice;
          }
        }
      }
    }
  }
};

fm.FSRate.prototype.setRates = function( argRates ) {
  this.rates = argRates;
};


fm.rate = new fm.FSRate();


