/*********************************************************
 *	File:		  currencyConverter.js
 *	Author:		MetaDesign
 *	Created:	July 23, 2007
 *
 *	Description:
 *	Javascript class for currency converter
 ********************************************************/

/*--------------------------------------------------------------
 * Class: CurrencyConverter
 *
 * Javascript class for currency converter module.
 * Updates currency converter display based on user input.
 *------------------------------------------------------------*/
var CurrencyConverter = Class.create();
CurrencyConverter.prototype = {

  /*--------------------------------------------------------------
   * Method: initialize
   *
   * Gets a handle to the form controls and assigns onchange handlers.
   * Gets a handle to the cost display elements.
   * Saves the base currency and exchange rates.
   *
   * Parameters:
   * pAmountBox     object    The cost input text box
   * pOtherSelect   object    The pulldown for user-selected
   *                          "other" currency
   * pButton        object    The form submit button
   * pBaseCurr      string    The base currency for conversion.
   * pExchangeRates string    Hashtable of exchange rates by
   *                          currency relative to USD
   *
   * Return:
   * None
   *------------------------------------------------------------*/
  initialize: function(pAmountBox, pOtherSelect, pButton, pBaseCurr, pExchangeRates) {
    // Get the form controls.
    this.amountBox = $(pAmountBox);
    this.otherSelect = $(pOtherSelect);
    this.button = $(pButton);
    
    // Assign onchange event handlers to form elements.
    this.amountBox.onchange = this.updateCosts.bind(this);
    this.otherSelect.onchange = this.updateCosts.bind(this);
    this.button.onchange = this.updateCosts.bind(this);
    
    // Get the common currencies display by class name.
    this.commonCurr = $$(".commonCurr");
    // Get the user currency display by id.
    this.userCurr = $("userCurr");
    
    // Save the base currency and exchange rates.
    this.baseCurr = pBaseCurr;
    this.exchangeRates = pExchangeRates;
  },
    
  /*--------------------------------------------------------------
   * Method: updateCosts
   *
   * Updates the cost displays.
   *
   * Parameters:
   * None
   *
   * Return:
   * None
   *------------------------------------------------------------*/
  updateCosts: function() {
    // Get the cost to convert from the input text box.
    var baseCost = this.amountBox.value;

    // Update the display for the common currencies.
    for (var i = 0; i < this.commonCurr.length; i++) {
      var commonDiv = this.commonCurr[i];
      // The currency code of the common currency is stored in the id of the div.
      commonDiv.innerHTML = this.formatCost(this.exchange(baseCost, commonDiv.id));
    }
    
    // Get the user-selected currency.
    var userCurrency = this.getUserCurr();
    
    // Set the user currency display if a user currency was selected.
    if (userCurrency.value != "") {
      this.userCurr.innerHTML = this.formatCost(this.exchange(baseCost, userCurrency.value)) + " " + userCurrency.text;
    } else {
      this.userCurr.innerHTML = "";
    }
  },
  
  /*--------------------------------------------------------------
   * Method: getUserCurr
   *
   * Retrieves the user-selected currency item.
   *
   * Parameters:
   * None
   *
   * Return:
   * object   The user-selected pulldown item.
   *------------------------------------------------------------*/
  getUserCurr: function() {
    return this.otherSelect[this.otherSelect.selectedIndex];
  },
    
  /*--------------------------------------------------------------
   * Method: exchange
   *
   * Calculates the exchange cost for a different currency.
   *
   * Parameters:
   * pBaseCost  float     The cost to convert
   * pCurr      string    The currency to convert to
   *
   * Return:
   * float    The converted cost or "-" if exchange data
   *          is not found.
   *------------------------------------------------------------*/
  exchange: function(pBaseCost, pCurr) {
    var baseExchange = this.exchangeRates[this.baseCurr];
    var targetExchange = this.exchangeRates[pCurr];
    if (baseExchange && targetExchange) {
      return pBaseCost/baseExchange * targetExchange;
    } else {
      return "-";
    }
  },
    
  /*--------------------------------------------------------------
   * Method: formatCost
   *
   * Formats a float into a two decimal currency cost.
   *
   * Parameters:
   * pCost    float     The cost to format
   *
   * Return:
   * string   The cost truncated to two decimal places.
   *------------------------------------------------------------*/
  formatCost: function(pCost) {
    if (pCost != "-") {
      return Math.floor(pCost*100)/100;
    } else {
      return "-";
    }
  }
}