/**
 * Useful functions for implementing the model select drop-downs for new & pre-owned inventory.
 * Look into the homepagecoupon offerdetails_contents.ftl & basicContactForm.ftl & basicFOCContactForm.ftl for example usage.
 * @author : sa
 */

/**
 * Comparator for reverse sorting.
 */
function reverseSort(a, b)
{
    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}

/*
 * Return a sorted array from of a set.
 */
function sortContents(objectSet, comp)
{
  var sortableArray = new Array();
  for (var obj in objectSet)
  {
    sortableArray[sortableArray.length] = obj;
  }
  if (comp == null) 
  {
    return sortableArray.sort();
  }
  else
  {
    return sortableArray.sort(comp);
  }
}

/**
 * Populate the dropdown.
 */
function populateDropDown(select, values, index)
{
  select.length = index;
  for (var v = 0; v < values.length; v++) {
    var value = values[v];
    select.options[select.options.length] = new Option(value, value);
  }
  //Select the first element by default.
  select.selectedIndex = 0;
}

/**
 * Populates a dropdown based on a property.
 */
function populateSelectProperty(selectId, startIndex, vehicleModels, property, sorter)
{
  if (vehicleModels == null)
  {
    return;
  }
  var elements = new Array();
  for (var v = 0; v < vehicleModels.length; v++)
  {
    var model = vehicleModels[v];
    var value = model[property];
    elements[value]  = value;
  }
  var sortedElements  =  sortContents (elements, sorter);
  var select = document.getElementById(selectId);
  if (select != null)
  {
    populateDropDown(select, sortedElements, startIndex);
  }
}

/**
 * Populate the year, make, model dropdowns.
 */
function populateMakeYearModels(yearSelectId, makeSelectId, modelSelectId, vehicleModels, yearIndex, makeIndex, modelIndex)
{
  populateSelectProperty(yearSelectId, yearIndex, vehicleModels, 'year', reverseSort);
  populateSelectProperty(makeSelectId, makeIndex, vehicleModels, 'make', null);
  populateSelectProperty(modelSelectId, modelIndex, vehicleModels, 'name', null);
}

/**
 * Select a value from a select control.
 */
function selectValue(selectControl, value)
{
  var options = selectControl.options;
  for (var i = 0; i < options.length; i++)
  {
    if (options[i].value == value)
    {
      selectControl.selectedIndex = i;
    }
  }
}

/**
 * Filters vehicles based on equality of a property value to the provided value.
 */
function filterVehiclesByPropertyValue(vehicles, property, value)
{
  var filter = new Array();
  for (var v = 0; v < vehicles.length; v++)
  {
    var model = vehicles[v];
    var pvalue = model[property];
    if (pvalue == value)
    {
      filter[filter.length] = model;
    }
  }
  return filter;
}

/**
 * Filters vehicles  based on a property in a specified range. Note that the valueLow &
 * valueUp are compared using the regular comparision operators.
 */
function filterVehiclesByPropertyRange(vehicles, property, valueLow, valueUp)
{
  var filter = new Array();
  for (var v = 0; v < vehicles.length; v++)
  {
    var model = vehicles[v];
    var pvalue = model[property];
    if ((valueLow == null || pvalue >= valueLow) && (valueUp == null || pvalue <= valueUp))
    {
      filter[filter.length] = model;
    }
  }
  return filter;
}
/**
 * A function to build an associative array of request parameters.
 */
function getRequestParams()
{
  var params = new Array();
  if (window.location.search.length < 2)
  {
    return params;
  }
  // Split the string by & and then =
  var pairs = window.location.search.substring(1).split("&");
  for (var i = 0; i < pairs.length; i++)
  {
    var arrPair = pairs[i].split("=");
    if (arrPair.length == 1)
    {
      params[arrPair[0]] = null;
    }
    else
    {
      params[arrPair[0]] = arrPair[1];
    }
  }
  return params;
}
