/*
 *	These scripts should be included on the home-page for enabling inventory search.
 *  Expects the following objects in the including file
 *  models : The new models applicable for this dealer
 *  makeList: List of makes(brands applicable to this dealer)
 *  usedTypeList  : The vehicle types to be used
 *  usedYears	 : List to be populated for year selection
 *  localizedAll : the localized value for All option. Used inside JS
 *  This file should be included only after populating those variables
 */
function addAllOptions(formElement, optionList, scope)
{
  for (i in optionList){
    var option = document.createElement('option');
    option.text = optionList[i];
    scope.addOption(formElement[scope.name], option);
  }
}

var invSearch;
var newInvSearch;
var usedInvSearch;

var yearSelect = new SelectBox('Year', 'year', 'year', true);
var typeSelect = new SelectBox('VehicleType', 'type', 'type', false, localizedAll);
var modelSelect = new SelectBox('modelId', 'name', 'id', true);

var usedYearSelect = new SelectBox('Year', 'year', 'year', false, localizedAll);
var usedTypeSelect = new SelectBox('VehicleType', 'type', 'type', false, localizedAll);
var usedMakeSelect = new SelectBox('make', 'make', 'make', false, localizedAll);

usedYearSelect.populate = function(objectSet, formElement)
{
  this.clear(formElement);
  addAllOptions(formElement, usedYearList, this);
};

/**
 * populate the vehicle-type select box in sorting order
 */
usedTypeSelect.populate = function(objectSet, formElement)
{
  this.clear(formElement);
  for(var i = 0; i < usedTypeList.length - 1; i++){
    for(var j = i + 1; j < usedTypeList.length; j++){
      if(usedTypeList[i] > usedTypeList[j]){
        var tmp = usedTypeList[i];
        usedTypeList[i] = usedTypeList[j];
        usedTypeList[j] = tmp;
      }
    }
  }
  addAllOptions(formElement, usedTypeList, this);
};

usedMakeSelect.populate = function(objectSet, formElement)
{
  this.clear(formElement);
  addAllOptions(formElement, makeList, this);
};

YAHOO.util.Event.addListener(window, "load", function()
{
  initializeInventorySearch();
});

function initializeInventorySearch()
{
  newInvSearch = new ModelSearch('inv-Search', [yearSelect, typeSelect, modelSelect], models, true);
  usedInvSearch = new ModelSearch('inv-Search', [usedYearSelect, usedTypeSelect, usedMakeSelect], models, false);
  
  if(newEnabled && usedEnabled){
    document.forms['inv-Search']['inv-radio'][0].checked = "checked";
    setInvSearch('new');
    return;
  }
  document.forms['inv-Search']['inv-radio'].checked = "checked";
  if(newEnabled){
    setInvSearch('new');
  }
  else if (usedEnabled){
    setInvSearch('old');
  }
}
/**
 * Set the inventory search
 * @param mode the inventory search mode
 */
function setInvSearch(mode)
{
  if (mode == 'new'){
    invSearch = newInvSearch;
    document.forms['inv-Search']['modelId'].style.display = "";
    document.forms['inv-Search']['modelId'].disabled = "";
    document.forms['inv-Search']['make'].style.display = "none";
    document.forms['inv-Search']['make'].disabled = "disabled";
    document.forms['inv-Search'].action = newInvURL;
  }
  else{
    invSearch = usedInvSearch;
    document.forms['inv-Search']['modelId'].style.display = "none";
    document.forms['inv-Search']['modelId'].disabled = "disabled";
    document.forms['inv-Search']['make'].style.display = "";
    document.forms['inv-Search']['make'].disabled = "";
    document.forms['inv-Search'].action = usedInvURL;
  }
  invSearch.init();
  
  if (mode == 'new'){
    selectValue('inv-Search', 'Year', defaultYear, 'text');
    newInvSearch.selectionChanged(0);
    selectValue('inv-Search', 'modelId', defaultVehicle, 'text');
  }
}

/**
 *  submit InvForm 
 */
function submitInvForm()
{
  document.forms['inv-Search'].submit();
}

/**
 * Selects a value from selectName selection box in invForm. The value to set is passed in setValue.
 * optionProperty is typically 'text' or 'value' and indicates whether the text or the value should
 * be matched
 * @param invForm the inventory form
 * @param selectName the select box name
 * @param setValue the value to set
 * @param optionProperty the option property
 * 
 */
function selectValue(invForm, selectName, setValue, optionProperty)
{
  var options = document.forms[invForm][selectName].options;
  for (var i = 0; i < options.length; i++) 
  {
    var optionValue = options[i][optionProperty];
    if (optionValue == setValue){
      document.forms[invForm][selectName].selectedIndex = i;
      return true;
    }
  }
  return false;
}
