function newCookie(name,value,days) {
 var days = 0;   

 if (days) {
   var date = new Date();
   date.setTime(date.getTime()+(days*24*60*60*1000));
   var expires = "; expires="+date.toGMTString(); }
   else var expires = "";
   document.cookie = name+"="+value+expires+"; path=/"; }

function readCookie(name) {
   var nameSG = name + "=";
   var nuller = '';
  if (document.cookie.indexOf(nameSG) == -1)
    return nuller;

   var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
  if (c.indexOf(nameSG) == 0) return c.substring(nameSG.length,c.length); }
    return null; }

function eraseCookie(name) {
  newCookie(name,"",1); }

function toMem(a) {

    newCookie('vehicle', strModel);
    newCookie('trim', trimBox.value);
    newCookie('zip', zipBox.value);
    newCookie('year', strYear);
	newCookie('ZipCode',zipCodeValue.value);
}

function delMem(a) {

  eraseCookie('vehicle');
  eraseCookie('trim');
  eraseCookie('zip');
  eraseCookie('year');
  eraseCookie('lease');
  eraseCookie('ZipCode');
   }


function remCookie() {
 document.paymentEstimatorForm.vehicle.value = readCookie("vehicle");
 document.paymentEstimatorForm.drpTrim.value = readCookie("trim");
 document.paymentEstimatorForm.txtZipcode.value = readCookie("zip");
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();	
    }
  }
}

//acc=[]

addLoadEvent(function() {
			    
			   
  
  var rC = readCookie("zip");
  
  //var tB = readCookie("trim");
  var tB = "";
  
  //var vM = readCookie("vehicle");
  
  var vM = "";
 
 
  var cYear = readCookie("year");
  


   var vMindex = 0;
  var vTindex = 0;


  if (rC != ""){

	//alert(vMindex);
	//alert(vTindex);
	//alert(cYear);
	


	//document.paymentEstimatorForm.drpTrim.value = tB;
	//document.paymentEstimatorForm.vehicle.value = vM;
	//setTimeout(function(){
		//document.paymentEstimatorForm.txtZipcode.value = rC;
		//}, 250);
//	document.paymentEstimatorForm.vehicle.selectedIndex = vMindex;

	for (i=0;i<document.paymentEstimatorForm.vehicle.length;i++){

		//tempV = document.paymentEstimatorForm.vehicle[i].value;


/*		var selectValue = document.paymentEstimatorForm.vehicle[i].value;
		var theArray = selectValue.split(',')
		var firstValue = theArray[0]
		var secondValue = theArray[1]
		
		alert(firstValue);
		alert(secondValue);*/

	
		tempV = document.forms['paymentEstimatorForm'].vehicle.options[i].text;
		
		//acc.push(tempV);

		//console.info(tempV);
		
		//document.forms['testform'].testselect.options[i]

		
		if (tempV == vM){
			indexI = i;
			//alert(indexI);
			document.paymentEstimatorForm.vehicle.selectedIndex = indexI;
			
			
			setTimeout(function(){
						 
						for (i=0;i<document.paymentEstimatorForm.drpTrim.length;i++){
						
						tempM = document.paymentEstimatorForm.drpTrim[i].value;
						//alert(tempM);
						
							if (tB == tempM){
								//alert(tB);
								//alert(tempM);
								if (document.paymentEstimatorForm.vehicle.selectedIndex > 0){
									
										//alert(i);
										//document.paymentEstimatorForm.drpTrim.selectedIndex = i;
										if (document.paymentEstimatorForm.drpTrim.selectedIndex != i){
											document.paymentEstimatorForm.drpTrim.selectedIndex = i;
											 submitBtnClicked(false);
											
										}
										 submitBtnClicked(false);
												 
								 }
								//},200);
								//alert("hi");
								//alert(document.paymentEstimatorForm.drpTrim);
							}
						
						}
			},750);
			
			
			setTimeout(function(){
			document.paymentEstimatorForm.txtZipcode.value = rC;
			}, 250);
			
		}
	
		/*alert(document.paymentEstimatorForm.Vehicle.vehicleName);
		//alert(i);
		//alert(vM);
		if (vM == document.paymentEstimatorForm.vehicle[i].value){
			alert(document.paymentEstimatorForm.vehicle[i].value);
		}
		/*if (vM == document.paymentEstimatorForm.vehicle[i]){
			document.paymentEstimatorForm.vehicle.selectedIndex = i;
		}*/
		
	} 
	


	modelTrigger(DWRUtil.getText('vehicle'), $('drpTrim'), $('txtZipcode'), DWRUtil.getValue('vehicle'));
	
	if (document.paymentEstimatorForm.vehicle.selectedIndex > 0){
		setTimeout(function(){
			document.paymentEstimatorForm.drpTrim.selectedIndex = vTindex;	
			if(document.paymentEstimatorForm.vehicle.selectedIndex > 0 &&
	    document.paymentEstimatorForm.drpTrim.selectedIndex > 0 &&
	    document.paymentEstimatorForm.txtZipcode.value != '') {
	    submitBtnClicked(false);
	 }		
			
		},500);
	}

  }
  
  
  	//alert(acc.join("\n"))
	
	
});
