arrmodels=new Array();
arrmakes=new Array();
//window.onload = searchInventory_default();
//
// QueryString
//

function QueryString(key)
{
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++)
	{
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;		
		}
	}

}

QueryString_Parse();
	function initMake(){
		var oList2 = document.theform_1.ddMake;
		clearDynaList(oList2);
		oList2[oList2.length]= new Option("All Makes");
	}
	function initModel(){
		var oList3 = document.theform_1.ddModel;
		clearDynaList(oList3);
		oList3[oList3.length]= new Option("All Models");
	}
	function initYear(){
		var oList4 = document.theform_1.ddYear;
		clearDynaList(oList4);
		oList4[oList4.length]= new Option("All Years");
	}
	//set default value for the dropdowns of the listused page
	function  searchInventory_default(){
		var oList1 = document.theform_1.ddNewUsed;
		var oList2 = document.theform_1.ddMake;
		var oList3 = document.theform_1.ddModel;
		var oList4 = document.theform_1.ddYear;	
		var choosestatus=QueryString("s");
		var choosemake=QueryString("d");
		var choosemodel=QueryString("m");
		var chooseyear=QueryString("y");
		
		var arr_cars=new Array();	
		if (oList1.selectedIndex == -1){
  		oList1.selectedIndex = 0;
		 }
		initMake();
		initModel();
		initYear();
		//set default make selections
		populateDynaList(oList2, arr_cars); 	
		get_index(oList1,choosestatus);
	  changenewused();   
		if (choosemake==""){
			document.theform_1.ddMake.selectedIndex=0;
		}else{
			get_index(oList2,choosemake);
			changemakes();			
			if(choosemodel==""){
				document.theform_1.ddModel.selectedIndex=0;
			}else{
			get_index(oList3,choosemodel);
			changemodels();			
				if(chooseyear==""){
					document.theform_1.ddYear.selectedIndex=0;
				}else{
					get_index(oList4,chooseyear);
				}	
			}			
		}
	}	
	function changenewused(){
		var oList1 = document.theform_1.ddNewUsed;
		var oList2 = document.theform_1.ddMake;
		var status=""; 
		if (oList1.selectedIndex == -1){
  		oList1.selectedIndex = 0;
		 }
	initMake();
	initModel();
	initYear();
	//get the new/used cars array
	//--if(QueryString('s')=='n'||QueryString('s')=='u'){
	//--	status=QueryString('s');
	//--}else{
	   	status=document.theform_1.ddNewUsed.options[document.theform_1.ddNewUsed.selectedIndex].value
  	if (document.theform_1.ddNewUsed.selectedIndex==1){
  	      if(window.location.href.indexOf("melclaytonfordsantabarbara")!=-1){            
              window.location.href="/inventory/sbInventory.aspx";
          }
  	}
	//--}
	arr_cars=return_array(cars,status);
	populateDynaList(oList2, arr_cars);
 	return true;
}
	function changemakes(){
		var oList1=document.theform_1.ddNewUsed;
		var oList2=document.theform_1.ddMake;
		var oList3=document.theform_1.ddModel;
		var arrList="";
		var status="";
		var make="";
		if (oList2.selectedIndex==-1){
  		oList2.selectedIndex = 0;
		 }
	initModel();
	initYear();
	//get the models for the selected make car;
	if (oList2.selectedIndex!=0){
	status=document.theform_1.ddNewUsed.options[document.theform_1.ddNewUsed.selectedIndex].value
	make=document.theform_1.ddMake.options[document.theform_1.ddMake.selectedIndex].value	
	arr_models=return_array(arrmakes[make],status);
	populateDynaList(oList3, arr_models);
	return true;
	}
}
	function changemodels(){
		var oList1=document.theform_1.ddNewUsed;
		var oList4=document.theform_1.ddYear;
		var oList3=document.theform_1.ddModel;
		var arrList="";
		var status="";
		var make="";
		if (oList3.selectedIndex==-1){
  		oList3.selectedIndex = 0;
		 }
	initYear();
	//get the years for the selected make model;
	if (oList3.selectedIndex!=0){
	status=document.theform_1.ddNewUsed.options[document.theform_1.ddNewUsed.selectedIndex].value
	model=document.theform_1.ddModel.options[document.theform_1.ddModel.selectedIndex].value	
	arr_years=return_array(arrmodels[model],status);
	populateDynaList(oList4, arr_years);
	return true;
	}
}
	//get the arrays from the string
	function return_array(cars,status){
	if (status=='n'){
	var j=0;
	arr_cars=new Array();
	apairs=cars.split(',');
	for(var i=0;i<apairs.length;i++){
		singleapair=apairs[i].split('#');
		   if(singleapair[1]=='1'||singleapair[1]=='3'){
			arr_cars[j]=singleapair[0];
			j++;		  
		   }
	}
	return arr_cars
        }//end of new car array
        if(status=='u') {
	var j=0;
	arr_cars=new Array();
	apairs=cars.split(',');
	for(var i=0;i<apairs.length;i++){
		singleapair=apairs[i].split('#');
		   if(singleapair[1]=='2'||singleapair[1]=='3'){
			arr_cars[j]=singleapair[0];
			j++;		  
		   }
	}
	return arr_cars
	}//end of used car array
}
	//clear the list
	function clearDynaList(oList){
 		for (var i = oList.options.length; i >= 0; i--){
 		oList.options[i] = null;
 		} 
		oList.selectedIndex = -1;
}
	//added options to the list
	function populateDynaList(oList, aArray){
 		for (var i = 0; i < aArray.length; i= i + 1){
   			oList.options[oList.options.length] = new Option(aArray[i], aArray[i]);
  		}
 		oList.selectedIndex = 0; 		
}
	//get the index for the slected value
	function get_index(oList, value_in){
		for(var i=0;i<oList.length;i=i+1){
			if(oList.options[i].value==value_in){						
			oList.selectedIndex=i;	
			}
		}
	}
	

	function SubmitSearch(){

	//min price
		pricebottom=document.getElementById("MyPriceBottom").value;

	//max price
		pricetop=document.getElementById("MyPriceTop").value;

	var strQString = "?s=" + "&pb=" +pricebottom + "&pt=" +pricetop
			window.location.href="/inventory/inventory.aspx"+strQString;  
	}