
// Must match the order of items in CurrencyCriteria. used to translate
// indexes to currency codes - so that changes to currency criteria can 
// repopulate prices in the max rate filter
var currencyCodes = new Array ("USD", "EUR", "GBP", "CAD", "AUD", "CHF", "JPY", "RMB", "INR");

function currencyIndex (currency)
{
   for (var i = 0; i < currencyCodes.length; i++)
   {
      if (currency == currencyCodes[i]) {return "" + i;}
   }
   return "0";
}

function setCurrency (currency, bCriteria)
{
   var input;
   if (bCriteria)
   {
      document.getElementById("zfk").value = currency == null ? 0 : currency;
   }
   else
   {
      document.getElementById("currency").
               value= currency == null ? 'USD' : currency;
   }

   if (currency == null) return 'USD';
   return bCriteria ? currencyCodes [parseInt(currency)] : currency;
}

// HAC-Related stuff that Used to live in tripadvisor.js
function fillRates(currency, bCriteria)
{
   currency = setCurrency (currency, bCriteria);
   var rateSelect = document.
                       getElementById(bCriteria ? "zfp" : "minmaxrate").options;
   rateSelect.length = 0;
   var levels = currencyLevel[currency];
   for (var i = 0 ; i < levels.length ; i += 2)
   {
       var label = levels[i];
       var value = bCriteria ? i/2 : levels[i+1];
       rateSelect[i/2] = new Option(label, value);

       // if the current max rate is in the minmax value string, select it
       if ( levels[i+1].indexOf("," + maxRate) > 0)
       {
	   rateSelect.selectedIndex = i/2;
       }
   }
}

function fillRatesAndClear(currency, bCriteria)
{
	maxRate = '9999999';
	fillRates(currency, bCriteria);
}

function setLCurrency (currency, bCriteria)
{
   var input;
   if (bCriteria)
   {
      $("l1currency").value = currency == null ? 0 : currency;
   }
   else
   {
      $("currency").value= currency == null ? 'USD' : currency;
   }

   if (currency == null) return 'USD';
   return bCriteria ? currencyCodes [parseInt($("l1currency").value)] : currency;
}

function fillRatesLightning(priceSelectVal)
{
   $("l1currency").value = (priceSelectVal == null) ? 0 : priceSelectVal;
   currency = currencyCodes [parseInt($("l1currency").value)];
   var currencyVals = currencyHash [parseInt($("l1currency").value)];
   
   if($('priceSelect'))
   {
     $('priceSelect').setProperty('class', ''); 
     $('priceSelect').addClass('s' + currencyVals.steps);
     $('priceSelect').addClass('mn' + currencyVals.min);
     $('priceSelect').addClass('mx' + currencyVals.max);
     $('priceSelect').addClass('smin' + currencyVals.selMin);
     $('priceSelect').addClass('smax' + currencyVals.selMax);
     $('priceSelect').addClass('dualSliderTest');
     $('priceSelect').addClass('round');
     $('priceSelect').addClass('namepslider');
     $('priceSelect').addClass('cur' + parseInt($("l1currency").value));
     dualSliderRule($('priceSelect'));
   }
}

var homeHacGeoChanged = function(elmt, response) {
  $('HAC_FORM').geo.value = response.value;
}

/* If hidden geo field has been set, clear it when we type in the text input
 * field. It will be set again if the user chooses from typeahead.
 */
rules['#hacGeo'] = function(elmt) {
    var ignoreKeys = ['enter', 'up', 'down', 'left', 'right', 'tab']; //mootools constants
    elmt.addEvent('keypress', function(e) {
        var evt = new Event(e);
        if(ignoreKeys.indexOf(evt.key) == -1)
        {
            $('HAC_FORM').geo.value = '';
        }
    });
}

/*
 * Update the rate drop down for the accommodation overview
 */
function updateOverviewRate()
{
  //  get the selected periodicity
  var pricePeriodWeekly = $('pricePeriod_w');
  var periodicity = pricePeriodWeekly && pricePeriodWeekly.checked ? 'w' : 'd';

  //  get the selected currency
  var l1currency = $("l1currency");
  var currency = l1currency ? l1currency.value : 0; // default to USD

  //  grab the price select and empty all children except the 'Any' option
  var select = $('l1price');
  var anyOptionLabel = select.getElement('option').innerHTML;
  select.empty();
  var anyOption = new Element('option', {'value':0});
  anyOption.innerHTML = anyOptionLabel;
  select.appendChild(anyOption);

  //  construct the map key and get the new set of options
  var key = periodicity + '_' + currency;
  var options = periodicPriceMap[key];
  for (var optionLabel in options)
  {
      var newOption = new Element('option', {'value':options[optionLabel]});
      newOption.innerHTML = optionLabel;
      select.appendChild(newOption);
  }
}