/**
 * inventory search form
 * 
 * Javascript to handle inventory search component
 * 
 * @version		1.0rc1
 * 
 * @author		liquid motors inc.
 * @copyright		Copyright 2010 liquid motors inc.
 */

function addOption(selectbox,text,value )
{
//alert(selectbox);

  var optn = document.createElement("OPTION");
  optn.text = text;
  optn.value = value;
  selectbox.options.add(optn);
}

function refineInv(type) {
  if(type == 'NEW') {
      for (var a=document.inventorysearchform.makeListBox.length; a > 0 ; a-- ){
        document.inventorysearchform.makeListBox.options.remove(a);
      }
      addOption(document.inventorysearchform.makeListBox, "FORD", "FORD");
      
      for (var a=document.inventorysearchform.yearFromBox.length; a > 0 ; a-- ){
        document.inventorysearchform.yearFromBox.options.remove(a);
      }
      addOption(document.inventorysearchform.yearFromBox, "2010", "2010");
      addOption(document.inventorysearchform.yearFromBox, "2011", "2011");
      
       for (var a=document.inventorysearchform.yearToBox.length; a > 0 ; a-- ){
        document.inventorysearchform.yearToBox.options.remove(a);
      }
      addOption(document.inventorysearchform.yearToBox, "2010", "2010");
      addOption(document.inventorysearchform.yearToBox, "2011", "2011");
      
      
  } else {
    for (var a=document.inventorysearchform.makeListBox.length; a > 0 ; a-- ){
          document.inventorysearchform.makeListBox.options.remove(a);
        }
        for (var a=document.inventorysearchform.yearToBox.length; a > 0 ; a-- ){
        document.inventorysearchform.yearToBox.options.remove(a);
      }
      for (var a=document.inventorysearchform.yearFromBox.length; a > 0 ; a-- ){
        document.inventorysearchform.yearFromBox.options.remove(a);
      }
    for (var i=0; i < makes.length;++i){
      addOption(document.inventorysearchform.makeListBox, makes[i], makes[i]);
    }
    for (var i=0; i < years.length;++i){
  addOption(document.inventorysearchform.yearFromBox, years[i], years[i]);
}
for (var i=0; i < years.length;++i){
  addOption(document.inventorysearchform.yearToBox, years[i], years[i]);
}
  }

}

for (var i=0; i < makes.length;++i){
  addOption(document.inventorysearchform.makeListBox, makes[i], makes[i]);
}
for (var i=0; i < years.length;++i){
  addOption(document.inventorysearchform.yearFromBox, years[i], years[i]);
}
for (var i=0; i < years.length;++i){
  addOption(document.inventorysearchform.yearToBox, years[i], years[i]);
}
/*for (var i=0; i < vehicletype.length;++i){
  addOption(document.inventorysearchform.autotypeListBox, vehicletype[i], vehicletype[i]);
}*/

var selectedMinPriceValue = null;
var selectedMaxPriceValue = null;
var selectedMinMileageValue = null;
var selectedMaxMileageValue = null;
var selectedMinYearValue = null;
var selectedMaxYearValue = null;
var urlSplit = window.location.href.split("/");
for (var u=0; u<urlSplit.length; ++u) {

    if (urlSplit[u] == "Certified"){
        if (document.getElementById("cbCertified")!=null)
            document.getElementById("cbCertified").checked = true;
    }else if (urlSplit[u] == "CARFAX-OneOwner") {
        if (document.getElementById("cbOneOwner")!=null)
            document.getElementById("cbOneOwner").checked = true;
    }else if (urlSplit[u] == "Bargains"){
        if (document.getElementById("cbBargains")!=null)
            document.getElementById("cbBargains").checked = true;
    }else if (urlSplit[u] == "Specials"){
        if (document.getElementById("cbSpecials")!=null)
            document.getElementById("cbSpecials").checked = true;
    }
    
  var strItem= urlSplit[u].split("-");
  var strCriteria = strItem[0];
  var strCriteriaLength = strCriteria.length + 1;
  var strCriteriaValue = urlSplit[u].substr(strCriteriaLength, urlSplit[u].length);
    
  if (strCriteria == "Price") {
    selectedMinPriceValue = strCriteriaValue.split("-")[0];
    selectedMaxPriceValue = strCriteriaValue.split("-")[1];
    document.getElementById("priceRange").value = "$" + selectedMinPriceValue + " - $" + selectedMaxPriceValue;

  } else if (strCriteria == "Miles") {
    selectedMinMileageValue = strCriteriaValue.split("-")[0];
    selectedMaxMileageValue = strCriteriaValue.split("-")[1];
    document.getElementById("mileageRange").value = selectedMinMileageValue + " - " + selectedMaxMileageValue;
  
  } else if (strCriteria == "Year") {
    selectedMinYearValue = strCriteriaValue.split("-")[0];
    selectedMaxYearValue = strCriteriaValue.split("-")[1];
    document.getElementById("yearRange").value = selectedMinYearValue + " - " + selectedMaxYearValue;
    
  } else if (strCriteria == "PerPage") {
    for (var p=0; p < document.inventorysearchform.itemsPerPage.length; p++) {
      if (document.inventorysearchform.itemsPerPage.options[p].innerHTML == strCriteriaValue){
        document.inventorysearchform.itemsPerPage.options[p].selected = true;
      }
    }    
  } else if (strCriteria == "Style") {
    for (var t=0; t < document.inventorysearchform.autotypeListBox.length; t++) {
      if (document.inventorysearchform.autotypeListBox.options[t].value == strCriteriaValue){
        document.inventorysearchform.autotypeListBox.options[t].selected = true;
      }
    }    
  } else if (strCriteria == "Sort") {
  
    for (var s=0; s < document.inventorysearchform.sortByColumn.length; s++) {
      if (escape(document.inventorysearchform.sortByColumn.options[s].innerHTML.toUpperCase()) == strCriteriaValue){
        document.inventorysearchform.sortByColumn.options[s].selected = true;
      }
    }    
  }  else if (strCriteria == "Make") {
    
    var strMake = strCriteriaValue.split(".")[0];
    var strModel = strCriteriaValue.split(".")[1];

    for (var m=0; m < document.inventorysearchform.makeListBox.length; m++) {
      if (document.inventorysearchform.makeListBox.options[m].value == strMake){
        document.inventorysearchform.makeListBox.options[m].selected = true;
        
        if (strModel != null) {
        strModel = strModel.replace("%20", " ");
        strModel = strModel.replace("%20AND%20 ", " & ");
        
            if (document.inventorysearchform.makeListBox.selectedIndex >0){
            var strMake = document.inventorysearchform.makeListBox.options[document.inventorysearchform.makeListBox.selectedIndex].value;
            var strMake = strMake.replace("-", "_");
            var strMake = strMake.replace(" ", "_");
              var initModelArray = eval(strMake + "models");
              for (var i=0; i < initModelArray.length;++i){
                addOption(document.inventorysearchform.modelListBox, initModelArray[i], initModelArray[i]);
              }
            }
          for (var model=0; model < document.inventorysearchform.modelListBox.length; model++) {
            if (document.inventorysearchform.modelListBox.options[model].value == strModel){
              document.inventorysearchform.modelListBox.options[model].selected = true;
            }
          }
        }
      }
    }
  }  

}

if(document.inventorysearchform.modelListBox.selectedIndex == 0) {
  if (document.inventorysearchform.makeListBox.selectedIndex >0){
  var strMake = document.inventorysearchform.makeListBox.options[document.inventorysearchform.makeListBox.selectedIndex].value;
  var strMake = strMake.replace("-", "_");
  var strMake = strMake.replace(" ", "_");
    var initModelArray = eval(strMake + "models");
    for (var i=0; i < initModelArray.length;++i){
      addOption(document.inventorysearchform.modelListBox, initModelArray[i], initModelArray[i]);
    }
  }
}

function changeModelList(item) {

  //var modelArray = eval(item.options[item.selectedIndex].value + "models")
  var strMake = item.options[item.selectedIndex].value;
 
  var strMake = strMake.replace("-", "_");
  var strMake = strMake.replace(" ", "_");
  var modelArray = eval(strMake + "models");
  for (var a=document.inventorysearchform.modelListBox.length; a > 0 ; a-- ){
    document.inventorysearchform.modelListBox.options.remove(a);
  }

  for (var i=0; i < modelArray.length;++i){
    addOption(document.inventorysearchform.modelListBox, modelArray[i], modelArray[i]);
  }

}
function get_radio_value()
{
for (var i=0; i < document.inventorysearchform.search_type.length; i++)
   {
   if (document.inventorysearchform.search_type[i].checked)
      {
      var rad_val = document.inventorysearchform.search_type[i].value;
      return rad_val;
      }
   }
}
function searchInventory() {
    var strURL = "http://" + window.location.host;
    var searchType = "";

   /* for (var u=0; u<urlSplit.length; ++u) {
         if (urlSplit[u] == "New")
            strURL = strURL + "/New";
         else if (urlSplit[u] == "Used")
            strURL = strURL + "/Used";
    }

    if (document.getElementById("cbCertified")!=null){
      if (document.getElementById("cbCertified").checked == true) {
        strURL = "/Certified";       
        }
        else {
          for (var u=0; u<urlSplit.length; ++u) {
           if (urlSplit[u] == "Certified")
              strURL = strURL + "/Used";
           
          } 
        }*/
      
     
       
      if (get_radio_value() == "CERTIFIED") {
      
        strURL = "/Certified";       
        }
        else if(get_radio_value() == "USED") {
        strURL = "/Used";       
        }
        else if(get_radio_value() == "NEW") {
        strURL = "/New";       
        }
        
    /*
    if (document.getElementById("cbOneOwner")!=null){
      if (document.getElementById("cbOneOwner").checked == true)
        strURL = strURL + "/CARFAX-OneOwner";
    }
    if (document.getElementById("cbBargains")!=null){
      if (document.getElementById("cbBargains").checked == true)
        strURL = strURL + "/Bargains";
    }
    if (document.getElementById("cbSpecials")!=null){
      if (document.getElementById("cbSpecials").checked == true)
        strURL = strURL + "/Specials";
    }
*/

    if (document.inventorysearchform.makeListBox.selectedIndex > 0)
    {
    strURL = strURL + "/Make-" + document.inventorysearchform.makeListBox[document.inventorysearchform.makeListBox.selectedIndex].value;

    if (document.inventorysearchform.modelListBox.selectedIndex > 0)
      strURL = strURL + "." + document.inventorysearchform.modelListBox[document.inventorysearchform.modelListBox.selectedIndex].value;

    }
    if (document.inventorysearchform.yearFromBox.selectedIndex > 0)
    {
    strURL = strURL + "/Year-" + document.inventorysearchform.yearFromBox[document.inventorysearchform.yearFromBox.selectedIndex].value;
      if (document.inventorysearchform.yearToBox.selectedIndex > 0) {
        strURL = strURL + "-" + document.inventorysearchform.yearToBox[document.inventorysearchform.yearToBox.selectedIndex].value;
      }
      else {
      
      strURL = strURL + "-2010";
      }
    }
    if (document.inventorysearchform.priceFromBox.selectedIndex > 0)
    {
    strURL = strURL + "/Price-" + document.inventorysearchform.priceFromBox[document.inventorysearchform.priceFromBox.selectedIndex].value;

      if (document.inventorysearchform.priceToBox.selectedIndex > 0) {
        strURL = strURL + "-" + document.inventorysearchform.priceToBox[document.inventorysearchform.priceToBox.selectedIndex].value;
        }
    else {
          strURL = strURL + "-1000000";
    }
    }

/*    var selectedPriceRange = document.getElementById("priceRange").value;
    selectedPriceRange = selectedPriceRange.replace(/ /g, "").replace(/[$]/g, "");
    strURL = strURL + "/Price-" + selectedPriceRange;

    var selectedMileageRange = document.getElementById("mileageRange").value;
    selectedMileageRange = selectedMileageRange.replace(/ /g, "");
    strURL = strURL + "/Miles-" + selectedMileageRange

    var selectedYearRange = document.getElementById("yearRange").value;
    selectedYearRange = selectedYearRange.replace(/ /g, "");
    strURL = strURL + "/Year-" + selectedYearRange

    if (document.inventorysearchform.sortByColumn.selectedIndex != 0 )
    strURL = strURL + "/Sort-" + document.inventorysearchform.sortByColumn[document.inventorysearchform.sortByColumn.selectedIndex].innerHTML.toUpperCase();

    if (document.inventorysearchform.autotypeListBox.selectedIndex != 0 )
    strURL = strURL + "/Style-" + document.inventorysearchform.autotypeListBox[document.inventorysearchform.autotypeListBox.selectedIndex].value;

    strURL = strURL + "/PerPage-" + document.inventorysearchform.itemsPerPage[document.inventorysearchform.itemsPerPage.selectedIndex].innerHTML;
*/
    window.location.href= strURL + "/Results.aspx";
}

var minYearValue = getMin(years);
var maxYearValue = getMax(years);
var minMileageValue = getMin(mileage);
var maxMileageValue = getMax(mileage);
var minPriceValue = getMin(price);
var maxPriceValue = getMax(price);

function getMin(array) {
  var minValue = 0;
  for (var i=0; i < array.length;++i){
    array[i] = array[i].replace("$","").replace(",","");
    if (!(isNaN(array[i]))){
      if (minValue == 0) {
        minValue = parseFloat(array[i]);
      }
      if (array[i] <minValue){
        minValue = parseFloat(array[i]);
      }
    }
  }              
  return minValue;
}

function getMax(array) {
  var maxValue = 0;
  for (var i=0; i < array.length;++i){
    array[i] = array[i].replace("$","").replace(",","");
    if (!(isNaN(array[i]))){
      if (array[i] >maxValue){
        maxValue = parseFloat(array[i]);
      }
    }
  }    
  return maxValue;
}

if (maxPriceValue % 5000 > 0){
  var roundUp = parseFloat(maxPriceValue / 5000);
  roundUp = Math.ceil(roundUp);
  maxPriceValue = roundUp * 5000;
}

if (maxMileageValue % 25000 > 0){
  var roundUp = parseFloat(maxMileageValue / 25000);
  roundUp = Math.ceil(roundUp);
  maxMileageValue = roundUp * 25000;
}


if(selectedMinPriceValue == null)
  selectedMinPriceValue = minPriceValue
if(selectedMaxPriceValue == null)
  selectedMaxPriceValue = maxPriceValue
if(selectedMinYearValue == null)
  selectedMinYearValue = minYearValue
if(selectedMaxYearValue == null)
  selectedMaxYearValue = maxYearValue
if(selectedMinMileageValue == null)
  selectedMinMileageValue = minMileageValue
if(selectedMaxMileageValue == null)
  selectedMaxMileageValue = maxMileageValue



