
function formatWarrantyPrice(theVal){

	return ((Math.round((theVal)*100)/100)+0.000001).toFixed(2);
}

/* NEW EXPERIMENT WITH TABS */
function showVidBroc(sTab)
		{
			var arTabs = new Array("photoTab","videoTab","brochureTab");
			var arSections = new Array("photosection","videosection","brochuresection");
			var sTemp = '';
			for(i = 0; i < arTabs.length; i++)
			{
				sTemp = document.getElementById(arTabs[i]);
				if(sTemp)
					sTemp.className = 'notselected';
			}
			document.getElementById(sTab+'Tab').className = 'selected';
			for(i = 0; i < arSections.length; i++)
			{
				sTemp = document.getElementById(arSections[i]);
				if(sTemp)
					sTemp.style.display = 'none';
			}
			document.getElementById(sTab+'section').style.display = 'block';
		}

function showHideContent(a)
		{
			 document.getElementById(a).style.display = 'none';
			 if(a=='rptgAdTeaser')
				document.getElementById('rptgAdFull').style.display = 'block';
			else
				document.getElementById('rptgAdTeaser').style.display = 'block';
		}

function initShowHideDivs()
		{
			document.getElementById('rptgAdFull').style.display = 'none';
		}

/* NEW EXPERIMENT WITH TABS */

function displayFrmTbl(id,sel){

	if (document.getElementById("frmPartExchange").checked==true)
	{
		document.getElementById("frmTable").style.display='';
		document.getElementById("frmPartExchangeField").style.display='';
		
		//document.getElementById("WSP").value ="";
	}
	else 
	{
		document.getElementById("frmTable").style.display='none';
		document.getElementById("frmPartExchangeField").style.display='none';
		//document.getElementById("WSP").value ="";
	}
}

function displaySubsDiv(id,sel){
		document.getElementById("SubsHiddenDiv").style.display='';
		document.getElementById("SubsButton").style.display='none';
}
function closeSubsDiv(id,sel){
document.getElementById('SubsName').value="";
		document.getElementById('SubsEmail').value="";
		document.getElementById("SubsHiddenDiv").style.display='none';
		document.getElementById("SubsButton").style.display='';
}


function submitSort(selectedOption, queryString){
	url=document.location.href;
	indSort=url.indexOf('&Sort=');
	if(indSort>0)
	{
		urlEnd='';
		urlStart = url.substring(0,indSort);
		if(url.indexOf('&',indSort+1) >0){
			urlEnd = url.substr( url.indexOf('&',indSort+1));
		}
		
		url = urlStart + "&Sort="+selectedOption+ urlEnd;
		
		document.location.href = url;
		
	}else{
		document.location.href = "/bikeListings/?"+queryString+"&Sort="+selectedOption;
	}
}

function submitSortUrl(selectedOption, queryString){
	
    url = queryString+"/st_"+selectedOption;
	document.location.href = url;
}

var xmlHttp


function LoadModels(selectedMake, directory, use_dhtml){
  
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null){
	  alert ("Browser does not support HTTP Request")
	  return
	}
	if (selectedMake != "-1"){
		var url="/model.php" //directory+
		url=url+"?MakeId="+selectedMake;
		url=url+"&sid="+Math.random()
		
		xmlHttp.onreadystatechange=stateChangedModel
		/*if (directory == ""){
			xmlHttp.onreadystatechange=stateChangedModelCount
		}else{
			xmlHttp.onreadystatechange=stateChangedModel
		}*/
	 if (use_dhtml){
                xmlHttp.onreadystatechange=stateChangedModelDHTML
		}
     else{
                xmlHttp.onreadystatechange=stateChangedModel
            }
		
	document.getElementById("LoadingModels").innerHTML = "Loading Models ...";

		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}else{
		var theModel = document.getElementById("Model");
		theModel.length = 0;
		theModel.options[0] = new Option("Any Model", -1);
	}
}


function LoadTypes(selectedModel, directory, use_dhtml){
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null){
	  alert ("Browser does not support HTTP Request")
	  return
	} 
	var url=directory+"type.php"
	url=url+"?ModelMID="+selectedModel;
	url=url+"&sid="+Math.random()
	if (directory != ""){
        if(use_dhtml){
            xmlHttp.onreadystatechange=stateChangedTypeDHTML
            }
        else{
            xmlHttp.onreadystatechange=stateChangedType
        }
	}else{
		xmlHttp.onreadystatechange=stateChangedTypeCount
	}	
	
	document.getElementById("LoadingModels").innerHTML = "Loading Types ...";

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}
function LoadEngineSize(selectedModel){
  
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null){
	  alert ("Browser does not support HTTP Request")
	  return
	} 
	var url="/enginesize.php"
	url=url+"?ModelId="+selectedModel;
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateChangedEngineSize
	
	document.getElementById("LoadingModels").innerHTML = "Loading Engine Size ...";

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

// HIGH CATEGORY CLAIMS LIMIT
function LoadClaimsLimit(selectedDuration, FromTo){
if (selectedDuration != "-1")
  {
		document.getElementById("img1").style.display = '';
		document.getElementById("img2").style.display = '';
		document.getElementById("img4").style.display = '';
		document.getElementById("img5").style.display = '';
		//document.getElementById("ClaimsLimit").value = "";
		//document.getElementById("CPrice").value = "0";
  }
  else
  {
  
		//document.getElementById("ClaimsLimit").value = "";
		//document.getElementById("CPrice").value = "0";

		document.getElementById("img1").style.display = 'none';
		document.getElementById("img2").style.display = 'none';
		document.getElementById("img4").style.display = 'none';
		document.getElementById("img5").style.display = 'none';

		// CALCULATING TOTAL
			var MotoAssistPrice = document.getElementById("MotoAssistPrice").value*1;
			var CCPrice = document.getElementById("CPrice").value *1;
			var preTotal = (MotoAssistPrice+CCPrice)*100/100;
			var VatCalculate = Math.round(preTotal*0.2*100)/100;
			document.getElementById("VAT").value =formatWarrantyPrice(VatCalculate);
			document.getElementById("GrandTotal").value = formatWarrantyPrice(Math.round((preTotal+VatCalculate)*100)/100);
  }
  
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null){
	  alert ("Browser does not support HTTP Request")
	  return
	}
	if (selectedDuration != "-1"){
		var url="claimslimit.php"
		url=url+"?HBID="+selectedDuration;
		url=url+"&sid="+Math.random()

			xmlHttp.onreadystatechange=stateChangedClaimsLimit
			
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}

function stateChangedClaimsLimit() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
		var theObject = document.getElementById("ClaimsLimit");
		var theObject2 = document.getElementById("CPrice");

		theObject.value='';
		theObject2.value='';
		var theDetails = theResponse.split(";;;;");
				
			var eachElement = theDetails[0].split(",,;,");
						
				theObject.value =formatWarrantyPrice( theObject.value +eachElement[0] );
				theObject2.value = formatWarrantyPrice(theObject2.value +eachElement[1]);
			
		document.getElementById("img1").style.display = 'none';
		document.getElementById("img2").style.display = 'none';
		document.getElementById("img4").style.display = 'none';
		document.getElementById("img5").style.display = 'none';
			
				// CALCULATING TOTAL
			var MotoAssistPrice = document.getElementById("MotoAssistPrice").value*1;
			var CCPrice = document.getElementById("CPrice").value *1;
			var preTotal = (MotoAssistPrice+CCPrice)*100/100;
			var VatCalculate = Math.round(preTotal*0.2*100)/100;
			document.getElementById("VAT").value =formatWarrantyPrice(VatCalculate);
			document.getElementById("GrandTotal").value = formatWarrantyPrice(Math.round((preTotal+VatCalculate)*100)/100);
			
	} 
}




// LOW CATEGORY CLAIMS LIMIT
function LoadLowCategoryClaimsLimit(selectedDuration, FromTo){
  
  if (selectedDuration != "-1")
  {
		document.getElementById("img1").style.display = '';
		document.getElementById("img2").style.display = '';
		document.getElementById("img4").style.display = '';
		document.getElementById("img5").style.display = '';
		//document.getElementById("ClaimsLimit").value = "";
		//document.getElementById("CPrice").value = "0";
  }
  else
  {
  
		//document.getElementById("ClaimsLimit").value = "";
		//document.getElementById("CPrice").value = "0";

		document.getElementById("img1").style.display = 'none';
		document.getElementById("img2").style.display = 'none';
		document.getElementById("img4").style.display = 'none';
		document.getElementById("img5").style.display = 'none';

		// CALCULATING TOTAL
			var MotoAssistPrice = document.getElementById("MotoAssistPrice").value*1;
			var CCPrice = document.getElementById("CPrice").value *1;
			var preTotal = (MotoAssistPrice+CCPrice)*100/100;
			var VatCalculate = Math.round(preTotal*0.2*100)/100;
			document.getElementById("VAT").value = formatWarrantyPrice(VatCalculate);
			document.getElementById("GrandTotal").value = formatWarrantyPrice(Math.round((preTotal+VatCalculate)*100)/100);
  }
  
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null){
	  alert ("Browser does not support HTTP Request")
	  return
	}
	if (selectedDuration != "-1"){
		var url="lowcategoryclaimslimit.php"
		url=url+"?BID="+selectedDuration;
		url=url+"&sid="+Math.random()

			xmlHttp.onreadystatechange=stateChangedLowCategoryClaimsLimit
			
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}

function stateChangedLowCategoryClaimsLimit() 
{ 

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
		var theObject = document.getElementById("ClaimsLimit");
		var theObject2 = document.getElementById("CPrice");

		theObject.value='';
		theObject2.value='';
		var theDetails = theResponse.split(";;;;");
				
			var eachElement = theDetails[0].split(",,;,");
						
				theObject.value = formatWarrantyPrice(theObject.value +eachElement[0]);
				theObject2.value = formatWarrantyPrice(theObject2.value +eachElement[1]);
			document.getElementById("img1").style.display = 'none';
		document.getElementById("img2").style.display = 'none';
		document.getElementById("img4").style.display = 'none';
		document.getElementById("img5").style.display = 'none';
			
				// CALCULATING TOTAL
			var MotoAssistPrice = document.getElementById("MotoAssistPrice").value*1;
			var CCPrice = document.getElementById("CPrice").value *1;
			var preTotal = (MotoAssistPrice+CCPrice)*100/100;
			var VatCalculate = Math.round(preTotal*0.2*100)/100;
			document.getElementById("VAT").value =formatWarrantyPrice(VatCalculate);
			document.getElementById("GrandTotal").value = formatWarrantyPrice(Math.round((preTotal+VatCalculate)*100)/100);
			
	} 
}

// ADMIN CLAIMS LIMIT
function LoadAdminClaimsLimit(selectedDuration, FromTo){
  
  if (selectedDuration != "-1")
  {
  document.getElementById("img1").style.display = '';
  document.getElementById("img2").style.display = '';
  document.getElementById("img4").style.display = '';
    document.getElementById("img5").style.display = '';
  }
  else
  {
		//document.getElementById("ClaimsLimit").value = "";
		//document.getElementById("CPrice").value = "0";
		//document.getElementById("MotoAssistPrice").value = "0";
		//document.getElementById("GrandTotal").value = "0";
		document.getElementById("img1").style.display = 'none';
		document.getElementById("img2").style.display = 'none';
		document.getElementById("img4").style.display = 'none';
		  document.getElementById("img5").style.display = 'none';
  }
  
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null){
	  alert ("Browser does not support HTTP Request")
	  return
	}
	if (selectedDuration != "-1"){
		var url="adminclaimslimit.php"
		url=url+"?WAID="+selectedDuration;
		url=url+"&sid="+Math.random()

			xmlHttp.onreadystatechange=stateChangedAdminClaimsLimit
			
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}

function stateChangedAdminClaimsLimit() 
{ 
	
	//document.getElementById("CPrice").value= "0";
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
		var theObject = document.getElementById("ClaimsLimit");
		var theObject2 = document.getElementById("CPrice");

		theObject.value='';
		theObject2.value='';

		var theDetails = theResponse.split(";;;;");
				
			var eachElement = theDetails[0].split(",,;,");
						
				theObject.value = formatWarrantyPrice(theObject.value +eachElement[0]);
				theObject2.value = formatWarrantyPrice(theObject2.value +eachElement[1]);
				
			document.getElementById("img1").style.display = "none";
			document.getElementById("img2").style.display = 'none';
			document.getElementById("img4").style.display = 'none';
			document.getElementById("img5").style.display = 'none';
			 
				// CALCULATING TOTAL
			var MotoAssistPrice = document.getElementById("MotoAssistPrice").value*1;
			var CCPrice = document.getElementById("CPrice").value *1;
			var preTotal = (MotoAssistPrice+CCPrice)*100/100;
			var VatCalculate = Math.round(preTotal*0.2*100)/100;
			document.getElementById("VAT").value =formatWarrantyPrice(VatCalculate);
			document.getElementById("GrandTotal").value = formatWarrantyPrice(Math.round((preTotal+VatCalculate)*100)/100);
	} 
}

// MOTO ASSIST PRICE
function LoadMotoAssistPrice(selectedDuration, FromTo){
  
  if (selectedDuration != "-1")
  {
		document.getElementById("img3").style.display = '';
		document.getElementById("img5").style.display = '';
		document.getElementById("img4").style.display = '';
		//document.getElementById("MotoAssistPrice").value = "0";
  }
  else
  {
  
		//document.getElementById("MotoAssistPrice").value = "0";
		document.getElementById("img3").style.display = 'none';
		document.getElementById("img5").style.display = 'none';
		document.getElementById("img4").style.display = 'none';
		// CALCULATING TOTAL
			var MotoAssistPrice = document.getElementById("MotoAssistPrice").value*1;
			var CCPrice = document.getElementById("CPrice").value *1;
			var preTotal = (MotoAssistPrice+CCPrice)*100/100;
			var VatCalculate = Math.round(preTotal*0.2*100)/100;
			document.getElementById("VAT").value = formatWarrantyPrice(VatCalculate);
			document.getElementById("GrandTotal").value = formatWarrantyPrice(Math.round((preTotal+VatCalculate)*100)/100);
  }
  
 
	
	
xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
	  alert ("Browser does not support HTTP Request")
	  return
	}
	if (selectedDuration != "-1"){
		var url="motoassistprice.php"
		url=url+"?MAID="+selectedDuration;
		url=url+"&sid="+Math.random()

		xmlHttp.onreadystatechange=stateChangedMotoAssistPrice

		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}

function stateChangedMotoAssistPrice() 
{ 
	//document.getElementById("MotoAssistPrice").value = "0";
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
		var theObject = document.getElementById("MotoAssistPrice");

		theObject.value='';

		var theDetails = theResponse.split(";;;;");
				
			var eachElement = theDetails[0].split(",,;,");
						
				theObject.value = formatWarrantyPrice(theObject.value +eachElement[0]) ;
				document.getElementById("img3").style.display = 'none';
			document.getElementById("img5").style.display = 'none';
			document.getElementById("img4").style.display = 'none';
			
			// CALCULATING TOTAL
			var MotoAssistPrice = document.getElementById("MotoAssistPrice").value*1;
			var CCPrice = document.getElementById("CPrice").value *1;
			var preTotal = (MotoAssistPrice+CCPrice)*100/100;
			var VatCalculate = Math.round(preTotal*0.2*100)/100;
			document.getElementById("VAT").value =formatWarrantyPrice(VatCalculate);
			document.getElementById("GrandTotal").value = formatWarrantyPrice(Math.round((preTotal+VatCalculate)*100)/100);			
	} 
}



function FinanceCalc()
{

var price = document.getElementById("finBikePrice").value*1;
var intRate = document.getElementById("finIntRate").value*1;
var deposit = document.getElementById("finDeposit").value*1;
var term = document.getElementById("finTerm").value*1;




if (document.getElementById("finIntRate").value!="0") // agar interest rate 0 nhi hay to
{

var totPrice = price - deposit;
var totIntRate = (totPrice*intRate)/100;


document.getElementById("finTotCost").value= formatWarrantyPrice(Math.round((totPrice+totIntRate)*Math.pow(10,2))/Math.pow(10,2));
document.getElementById("finEstPerMon").value= formatWarrantyPrice(Math.round(((totPrice+totIntRate)/term)*Math.pow(10,2))/Math.pow(10,2));

}
else // agar interest rate 0 hay to 
{
var totPrice = price - deposit;
document.getElementById("finTotCost").value= formatWarrantyPrice(price-deposit);
document.getElementById("finEstPerMon").value= formatWarrantyPrice(Math.round((totPrice/term)*Math.pow(10,2))/Math.pow(10,2));
}


}



function FinanceCalcBalance(){
	var price = document.getElementById("financeCashPrice").value*1;
	var deposit = document.getElementById("financeDeposit").value*1;

	var balance = price-deposit;
	document.getElementById("financeBalonFin").value = formatWarrantyPrice(Math.round(balance));
}


function LoadDealerDetail(selectedDealer, FromTo,mode){

	if(!mode){
		mode = '';
	}
  
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null){
	  alert ("Browser does not support HTTP Request")
	  return
	}
	if (selectedDealer != "-1"){
		var url="dealerContact.php"
		url=url+"?DealerId="+selectedDealer;
		url=url+"&sid="+Math.random()

		if (FromTo == "PickupFrom"){
			xmlHttp.onreadystatechange=stateChangedPickupFrom
		}else if (FromTo == "DeliverTo"){
			xmlHttp.onreadystatechange=stateChangedDeliverTo
		}else{
			if(mode=="dealer"){
				xmlHttp.onreadystatechange=stateChangedInvoiceToDealer
			}else{
				xmlHttp.onreadystatechange=stateChangedInvoiceTo
			}
		}		

		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}

function stateChangedModel() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
		document.getElementById("LoadingModels").innerHTML = "&nbsp;";
		var theModel = document.getElementById("Model");
		theModel.length = 0;
		//document.getElementById("Model").value;
		var theModels = theResponse.split(";;;;");
		theModel.options[0] = new Option("Any Model", -1);
		
		for (i=0;i<theModels.length-1;i++){
			var eachElement = theModels[i].split(",,;,");
			theModel.options[i+1] = new Option(eachElement[1], eachElement[0]);
		}
	} 
}

function stateChangedModelDHTML() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
		document.getElementById("LoadingModels").innerHTML = "&nbsp;";
        
        model_sel.clearAll();
        model_sel.setComboText("Any Model");
        type_sel.setComboText("Any Type");

		var theModels = theResponse.split(";;;;");
		model_sel.addOption([[-1, "Any Model"]]);
		selectedOpt = "";
		if (theModels.length == 2){
			selectedOpt = "yes";
		}
		for (i=0;i<theModels.length-1;i++){
			var eachElement = theModels[i].split(",,;,");
            model_sel.addOption([[eachElement[0], eachElement[1]]]);
            if (selectedOpt == "yes"){
				model_sel.setComboValue(eachElement[0]);
			}
		}
	} 
}

// bulkUpload
function LoadModelsBulk(selectedMake, directory,modelBox){
  
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null){
	  alert ("Browser does not support HTTP Request")
	  return
	}
	
	if (selectedMake != "-1"){
		var url=directory+"modelBulk.php"
		url=url+"?MakeId="+ selectedMake +"&ModelBox="+ modelBox;
		url=url+"&sid="+Math.random()
		
		xmlHttp.onreadystatechange=stateChangedModelBulk
		
		//document.getElementById("LoadingModels").innerHTML = "Loading Models ...";

		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}else{
		var theModel = document.getElementById("Model"+modelBox);
		theModel.length = 0;
		theModel.options[0] = new Option("Any Model", -1);
	}
}


// bulkUpload
function stateChangedModelBulk() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
		
		//document.getElementById("LoadingModels").innerHTML = "&nbsp;";
		//document.getElementById("Model").value;
		var theModels = theResponse.split(";;;;");
		
		var theModel = document.getElementById("Model"+theModels[0]);
		theModel.length = 0;
		theModel.options[0] = new Option("Any Model", -1);
		
		for (i=1;i<theModels.length-1;i++){
			var eachElement = theModels[i].split(",,;,");
			theModel.options[i+1] = new Option(eachElement[1], eachElement[0]);
		}
	} 
}


function stateChangedModelCount() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
		document.getElementById("LoadingModels").innerHTML = "&nbsp;";
		var theModel = document.getElementById("Model");
		theModel.length = 0;
		//document.getElementById("Model").value;
		var theModels = theResponse.split(";;;;");
		theModel.options[0] = new Option("Any Model", -1);
		
		for (i=0;i<theModels.length-1;i++){
			var eachElement = theModels[i].split(",,;,");
			theModel.options[i+1] = new Option(eachElement[1], eachElement[0]);  //+ " ("+eachElement[2]+")"
		}
	} 
}
function stateChangedEngineSize() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
		document.getElementById("LoadingModels").innerHTML = "&nbsp;";
		var theEngineSize = document.getElementById("EngineSize");
		theEngineSize.length = 0;
		var theEngineSizes = theResponse.split(";;;;");
		theEngineSize.options[0] = new Option("Any Engine Size", -1);
		
		for (i=0;i<theEngineSizes.length-1;i++){
			var eachElement = theEngineSizes[i].split(",,;,");
			theEngineSize.options[i+1] = new Option(eachElement[1]+ " ("+eachElement[2]+")", eachElement[0]);
		}
	} 
}

function stateChangedType() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
	
		var theResponse = xmlHttp.responseText;
		
		document.getElementById("LoadingModels").innerHTML = "&nbsp;";
		var theType = document.getElementById("Type");
		theType.length = 0;
		var theTypes = theResponse.split(";;;;");
		theType.options[0] = new Option("Any Type", -1);
		selectedOpt = "";
		if (theTypes.length == 2){
			selectedOpt = "yes";
		}
		for (i=0;i<theTypes.length-1;i++){
			var eachElement = theTypes[i].split(",,;,");
			theType.options[i+1] = new Option(eachElement[1], eachElement[0]);
			if (selectedOpt == "yes"){
				theType.options[i+1].selected = true;
			}
		}
	} 
}

function stateChangedTypeDHTML() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
	
		var theResponse = xmlHttp.responseText;
		
		document.getElementById("LoadingModels").innerHTML = "&nbsp;";
        type_sel.clearAll();
        type_sel.setComboText("Any Type");
		var theTypes = theResponse.split(";;;;");
        type_sel.addOption([[-1, "Any Type"]]);
		selectedOpt = "";
		if (theTypes.length == 2){
			selectedOpt = "yes";
		}
		for (i=0;i<theTypes.length-1;i++){
			var eachElement = theTypes[i].split(",,;,");
            type_sel.addOption([[eachElement[0], eachElement[1]]]);
            if(selectedOpt == "yes"){
                type_sel.setComboValue(eachElement[0]);}
		}
	} 
}


function stateChangedTypeCount() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
	
		var theResponse = xmlHttp.responseText;
		
		document.getElementById("LoadingModels").innerHTML = "&nbsp;";
		var theType = document.getElementById("Type");
		theType.length = 0;
		var theTypes = theResponse.split(";;;;");
		theType.options[0] = new Option("Any Type", -1);
		selectedOpt = "";
		if (theTypes.length == 2){
			selectedOpt = "yes";
		}
		for (i=0;i<theTypes.length-1;i++){
			var eachElement = theTypes[i].split(",,;,");
			theType.options[i+1] = new Option(eachElement[1]+ " ("+eachElement[2]+")", eachElement[0]);
			if (selectedOpt == "yes"){
				theType.options[i+1].selected = true;
			}
		}
	} 
}



function stateChangedPickupFrom() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
	
	//	document.getElementById("LoadingModels").innerHTML = "&nbsp;";
		var theObject = document.getElementById("PickupFrom");
		theObject.value='';
		var theDetails = theResponse.split(";;;;");
				
			var eachElement = theDetails[0].split(",,;,");
			for(i=0;i<eachElement.length-1;i++)
			{
				theObject.value = theObject.value +eachElement[i] +"\n";
			}
	} 
}

function stateChangedDeliverTo() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
	
	//	document.getElementById("LoadingModels").innerHTML = "&nbsp;";
		var theObject = document.getElementById("DeliverTo");
		theObject.value='';
		var theDetails = theResponse.split(";;;;");
				
			var eachElement = theDetails[0].split(",,;,");
			for(i=0;i<eachElement.length-1;i++)
			{
				theObject.value = theObject.value +eachElement[i] +"\n";
			}
	} 
}

function stateChangedInvoiceTo() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
	//	document.getElementById("LoadingModels").innerHTML = "&nbsp;";
		var theObject = document.getElementById("InvoiceTo");
		theObject.value='';
		var theDetails = theResponse.split(";;;;");
				
			var eachElement = theDetails[0].split(",,;,");
			for(i=0;i<eachElement.length-1;i++)
			{
				theObject.value = theObject.value +eachElement[i] +"\n";
			}
		if (theDetails.length > 1){
			eachElement = theDetails[1].split(",,;,");
			if(document.getElementById("AccountsContact")){
				document.getElementById("AccountsContact").value = eachElement[0];
			}
			if(document.getElementById("AccountsEmail")){
				document.getElementById("AccountsEmail").value = eachElement[1];
			}
			if(document.getElementById("ContactPhone")){
				document.getElementById("ContactPhone").value = eachElement[2];
			}
			if(document.getElementById("MSMMember")){
				document.getElementById("MSMMember").value = eachElement[3];
			}
		}
		if (theDetails.length > 2){
			if (theDetails[2] != ""){
				document.getElementById("amoutOutStanding").innerHTML = "<label>Total OutStanding: <br class='clear'/><br class='clear'/><a href='/Dealer/accountStatement.php?dealerID="+theDetails[3]+"' target='_blank'>&pound; "+theDetails[2]+"</a></label>";
				document.getElementById("amoutOutStanding").style.display = "";
			}else{
				document.getElementById("amoutOutStanding").innerHTML = "";
			}
		}
	}
	
	calculateInvoiceTotal();
}
function stateChangedInvoiceToDealer()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var theResponse = xmlHttp.responseText;
	//	document.getElementById("LoadingModels").innerHTML = "&nbsp;";
		var theObject = document.getElementById("InvoiceTo");
		theObject.value='';
		var theDetails = theResponse.split(";;;;");
				
			var eachElement = theDetails[0].split(",,;,");
			for(i=0;i<eachElement.length-1;i++)
			{
				theObject.value = theObject.value +eachElement[i] +"\n";
			}
		if (theDetails.length == 2){
			eachElement = theDetails[1].split(",,;,");
			if(document.getElementById("AccountsContact")){
				document.getElementById("AccountsContact").value = eachElement[0];
			}
			if(document.getElementById("AccountsEmail")){
				document.getElementById("AccountsEmail").value = eachElement[1];
			}
			if(document.getElementById("ContactPhone")){
				document.getElementById("ContactPhone").value = eachElement[2];
			}
			if(document.getElementById("MSMMember")){
				document.getElementById("MSMMember").value = eachElement[3];
			}
		}
	}
	calculateInvoiceTotal2();}
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest){
	  objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject){
	   objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}



function dosearch(){
	var zipCode = document.getElementById("PostCode").value;
	var zipRadius = document.getElementById("PostCodeRadius").value;
	
	if (zipCode == ""){
		alert("Enter a postcode to continue.");
		return false;
	}else{
		document.forms["findRV"].submit();
	}
}


function formatPostCode(){
	myPostCode = document.getElementById("PostCode").value;
	if (checkPostCode (myPostCode)) {
		document.getElementById("PostCode").value = checkPostCode (myPostCode);
	}
}

function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}


