var params="";
 
function loadModels(i,m){
	var varModelsArray  = makemodel[0][i];
	var intModelsCount  = varModelsArray.length;
	var blnIsNS        = navigator.appName=='Netscape';
	var k=0;
	var o;

	for(jk=0;jk<=1;jk++){
		if(jk==0){
			o=document.getElementById("quicksearch");
			if(!o){o=document.quicksearch;}
			if(!o){o=document.forms["quicksearch"];}
		}else{
			o=document.getElementById("quicksearch2");
			if(!o){o=document.quicksearch2;}
			if(!o){o=document.forms["quicksearch2"];}
		}
		if(o){
			o.make.selectedIndex=i;
			o.model.length = 1;
			o.model.length = intModelsCount;
			for (j=1; j<intModelsCount; j++) {
				o.model[j] = new Option(varModelsArray[j]);
				if(varModelsArray[j].toLowerCase()==m){k=j;}
			}
			if (k>0){o.model.selectedIndex=k;}
			//Campo asked for this to be removed Dec 2011
			//if(o.category){o.category.selectedIndex=0;}
		}	
	}
}
 
function clearMakeModel(i){
	//alert(i);
}

function notClearMakeModel(i){
	var o;
	for(jk=0;jk<=1;jk++){
		if(jk==0){
			o=document.getElementById("quicksearch");
			if(!o){o=document.quicksearch;}
			if(!o){o=document.forms["quicksearch"];}
		}else{
			o=document.getElementById("quicksearch2");
			if(!o){o=document.quicksearch2;}
			if(!o){o=document.forms["quicksearch2"];}
		}
		if(o){
			//Campo asked for this to be removed Dec 2011
			//if(o.category){o.category.selectedIndex=i;}
			//if(o.make){o.make.selectedIndex=0;}
			//if(o.model){o.model.selectedIndex=0;}
		}
	}
}

function modelSelected(i){
	var o;
	o=document.getElementById("quicksearch");
	if(!o){o=document.quicksearch;}
	if(!o){o=document.forms["quicksearch"];}

	if(o){o.model.selectedIndex=i;}
	o=document.getElementById("quicksearch2");
	if(!o){o=document.quicksearch2;}
	if(!o){o=document.forms["quicksearch2"];}

	if(o){o.model.selectedIndex=i;}
}

function priceSelected(i){
	var o;
	o=document.getElementById("quicksearch");
	if(!o){o=document.quicksearch;}
	if(!o){o=document.forms["quicksearch"];}
	if(o){o.pricebracket.selectedIndex=i;}

	o=document.getElementById("quicksearch2");
	if(!o){o=document.quicksearch2;}
	if(!o){o=document.forms["quicksearch2"];}

	if(o){
		o.pricebracket.selectedIndex=i;
		o.selPriceSearch_range.selectedIndex=i;
	}
	
}

function mileageSelected(i){
	var o;
	o=document.getElementById("quicksearch");
	if(!o){o=document.quicksearch;}
	if(!o){o=document.forms["quicksearch"];}
	if(o){o.mileage.selectedIndex=i;}

	o=document.getElementById("quicksearch2");
	if(!o){o=document.quicksearch2;}
	if(!o){o=document.forms["quicksearch2"];}
	if(o){o.mileage.selectedIndex=i;}
}

function fuelSelected(i){
	var o;
	o=document.getElementById("quicksearch");
	if(!o){o=document.quicksearch;}
	if(!o){o=document.forms["quicksearch"];}
	if(o){o.fuel.selectedIndex=i;}

	o=document.getElementById("quicksearch2");
	if(!o){o=document.quicksearch2;}
	if(!o){o=document.forms["quicksearch2"];}
	if(o){o.fuel.selectedIndex=i;}
}

function transmissionSelected(i){
	var o;
	o=document.getElementById("quicksearch");
	if(!o){o=document.quicksearch;}
	if(!o){o=document.forms["quicksearch"];}
	if(o){o.transmission.selectedIndex=i;}

	o=document.getElementById("quicksearch2");
	if(!o){o=document.quicksearch2;}
	if(!o){o=document.forms["quicksearch2"];}
	if(o){o.transmission.selectedIndex=i;}
}

function colourSelected(i){
	var o;
	o=document.getElementById("quicksearch");
	if(!o){o=document.quicksearch;}
	if(!o){o=document.forms["quicksearch"];}
	if(o){o.colour.selectedIndex=i;}

	o=document.getElementById("quicksearch2");
	if(!o){o=document.quicksearch2;}
	if(!o){o=document.forms["quicksearch2"];}
	if(o){o.colour.selectedIndex=i;}
}

function ageSelected(i){
	var o;
	o=document.getElementById("quicksearch");
	if(!o){o=document.quicksearch;}
	if(!o){o=document.forms["quicksearch"];}
	if(o){o.age.selectedIndex=i;}

	o=document.getElementById("quicksearch2");
	if(!o){o=document.quicksearch2;}
	if(!o){o=document.forms["quicksearch2"];}
	if(o){o.age.selectedIndex=i;}
}

function showPanel(s){
	var o;
	var p=new Array("price","finance","costs");
	for(var i=0;i<=2;i++){
		o=document.getElementById("search_"+p[i]);
		if(o){o.style.display="none";}
	}
	o=document.getElementById("search_"+s);
	if(o){o.style.display="block";}
}

function sortBy(s){
	showPanel("none");
	findCars(s);
}

function runSearch(s){
	if(!s){s="";}

	var category="";
	var make="";
	var model="";
	var keywords="";
	var colour="";
	var price=0;
	var mileage=0;

	var transmission="";
	var fuel="";
	var age=0;

	var sptype=0;
	var costmpg=0;
	var costinsurance=0;
	var costtax="";

	var pxvalue=0;
	var financetype="hp";
	var deposit=0;
	var monthlyamount=0;
	var enginecc=0;

	// get the parameters from the search panel
	var o;
	o=document.getElementById("quicksearch");
	if(!o){o=document.quicksearch;}
	if(!o){o=document.forms["quicksearch"];}

	if(o.category.selectedIndex>0){category=o.category.value;}
	if(o.make.selectedIndex>0){make=o.make.value;}
	if(o.model.selectedIndex>0){model=makemodel[0][o.make.selectedIndex][o.model.selectedIndex];}
	if(o.pricebracket.selectedIndex>0){price=o.pricebracket.selectedIndex;}
	if(o.enginecc.selectedIndex>0){enginecc=o.enginecc.value;}

	if(o.colour){
		if(o.colour.selectedIndex>0){colour=o.colour.value;}
		if(o.mileage.selectedIndex>0){mileage=o.mileage.value;}
	}

	if(o.keyword){keywords=o.keyword.value;}

	// if additional info is shown, include this
	if(o.transmission){
		if(o.transmission.selectedIndex>0){transmission=o.transmission.value;}
	}
	if(o.fuel){
		if(o.fuel.selectedIndex>0){fuel=o.fuel.value;}
	}
	if(o.age){
		if(o.age.selectedIndex>0){age=o.age.value;}
	}
	// if we've been sent a parameter (a specific type of search) then use this info too
	
	var params="";

	if(category!=""){params=params+"category="+category+"&";}
	if(make!=""){params=params+"make="+make+"&";}
	if(model!=""){params=params+"model="+model+"&";}
	if(colour!=""){params=params+"colour="+colour+"&";}
	if(price>0){params=params+"price="+price+"&";}
	if(mileage>0){params=params+"mileage="+mileage+"&";}
	if(transmission!=""){params=params+"transmission="+transmission+"&";}
	if(fuel!=""){params=params+"fuel="+fuel+"&";}
	if(age>0){params=params+"age="+age+"&";}
	if(enginecc>0){params=params+"enginecc="+enginecc+"&";}
	if(keywords!=""){params=params+"keywords="+keywords+"&";}
	
	if (s=="low"){params=params+"orderby=pricelow";}
	if (s=="high"){params=params+"orderby=pricehigh";}
	if (s=="colour"){params=params+"orderby=colour";}
	if (s=="newest"){params=params+"orderby=newest";}

	if(s=="savings"){
		var o=document.getElementById("quicksearch2");
		if(!o){o=document.quicksearch2;}
		if(!o){o=document.forms["quicksearch2"];}
		if(o){sptype=o.selPriceSearch_type.selectedIndex;}
	}
	if(sptype>0){params=params+"searchtype="+sptype;}

	if(s=="economy"){
		var o=document.getElementById("quicksearch2");
		if(!o){o=document.quicksearch2;}
		if(!o){o=document.forms["quicksearch2"];}
		if(o){costmpg=o.selCostSearch_mpg.value;}
		if(o){costinsurance=o.selCostSearch_insurance.value;}
		if(o){costtax=o.selCostSearch_tax.value;}
	}
	if(costmpg>0){params=params+"mpg="+costmpg+"&";}
	if(costinsurance>0){params=params+"insurance="+costinsurance+"&";}
	if(costtax!=""){params=params+"tax="+costtax+"&";}

	var depositValue=new Array(0,250,1000,1500,2000,2500,3000,3500,4000,4500,5000,5500,6000,6500,7000,7500,8000,8500,9000,9500,10000);
	var monthlyValue=new Array(100,150,200,250,300,350,400,450,500,501);

	if(s=="finance"){
		var o=document.getElementById("quicksearch2");
		if(!o){o=document.quicksearch2;}
		if(!o){o=document.forms["quicksearch2"];}
		if(o){pxvalue=o.selFinanceSearch_px.selectedIndex*500;}
		if(o){financetype=o.selFinanceSearch_type.value;}
		if(o){deposit=depositValue[o.selFinanceSearch_deposit.selectedIndex];}
		if(o){monthlyamount=monthlyValue[o.selFinanceSearch_monthly.selectedIndex];}
	}
	if (pxvalue>0){params=params+"pxvalue="+pxvalue+"&";}
	if (deposit>0){params=params+"deposit="+deposit+"&";}
	if (monthlyamount>0){
		params=params+"monthlyamount="+monthlyamount+"&";
		params=params+"financetype="+financetype+"&";
	}
	if(params!=""){params="?"+params;}
	document.location="search_results.asp"+params;
}

function checkForEnter(event){
	var keyCode = event.which;
    if (keyCode == undefined) {keyCode = event.keyCode;}
	if (keyCode==10 || keyCode==13){runSearch();}
}

function disableEnterKey(e){
     var key;
     if(window.event){
          key = window.event.keyCode;     //IE
     }else{
          key = e.which;     //firefox
	 }
     if(key == 13){
		  runSearch();
          return false;
     }else{
          return true;
	 }
}
