// JavaScript Document

/* function setDefault(){
	var trimDefault = "<select name='vehicleVariant' id='vehicleVariant' onchange='populatePriceDetails(this.value);'>Select a Variant</option></select>";
	document.getElementById("variantDiv").innerHTML  = trimDefault;
	return true;
} */
/* commented on 01-Apr-09
function varanitTempA(modelId,trimId){
	document.getElementById("vehicleVariant").value = trimId;
	//populatePriceDetails(trimId);
}
*/


function varanitTempA(modelId,trimId){
	document.getElementById("vehicleVariant").value = trimId;
   // fireEvent(document.getElementById("vehicleVariant"), 'change');
	try{
		document.getElementById('vehicleVariant').fireEvent('onChange');	
	} catch(e){
		var event = document.createEvent('HTMLEvents');
		event.initEvent('change',true, false);
		document.getElementById("vehicleVariant").dispatchEvent(event);
	}

	return true;
}


/* function varanitJsptoFlash(modelId,trimId){
	//document.getElementById("vehicleVariant").value = trimId;
		//populatePriceDetails(trimId);
		//alert(window.toolkit);
		window.toolkit.varanitTempJsp(modelId,trimId);
} */
function varanitJsptoFlash(){
		var tempModel = document.getElementById("vehicleModel").value ;		
		var trimId = document.getElementById("vehicleVariant").value;
		var makemodelarr = tempModel.split("~");		
		var modelId = makemodelarr[2];
		window.toolkit.varanitTempJsp(modelId,trimId);
}

function populatePriceDetails(trim){
	var trimId = trim;
	var make_model = document.getElementById('vehicleModel').value;
	var makemodelarr = make_model.split("~");
	var modelId = makemodelarr[2];
	var makeId=''; //no need to pass makeId ad modelId is unique in DB
//	var makeId = document.configDeal.makeId.value;
	fillPriceConfigDetails(makeId,modelId,trimId,locationId);
}
function fillPriceConfigDetails(makeId,modelId,trimId,locationId){
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
  		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	if(http_request){
			var url="newcars/getPriceForVariant.htm?makeId="+makeId+"&modelId="+modelId+"&trimId="+trimId+"&locationId="+locationId;
			//alert(url);
			http_request.onreadystatechange = function() { processPriceConfigDetails(http_request); } ;
			http_request.open ("POST", url, true);			
			http_request.send(null);
	}
}

 function processPriceConfigDetails(http_request){	
	 	if (http_request.readyState == 4){
		//alert(http_request.responseText);
			document.getElementById("priceConfigDetailsDiv").innerHTML = http_request.responseText;
		}
	}

function getToolkitPage(val){
	//alert('1');
	var make_model = val
	var makemodelarr = make_model.split("~");
	
	var make = makemodelarr[0];
	var model = makemodelarr[1];
	while(make.indexOf(" ")!=-1){
		make = make.replace(" ","");
	}
	while(model.indexOf(" ")!=-1){
		model = model.replace(" ","");
	}
	var url = (make.replace(" ","")+"_"+model.replace(" ","")).toLowerCase()+".htm";
	document.newcarToolkitForm.action = url;
	document.newcarToolkitForm.submit();
}
function variantChange(trim){

//	alert(document.getElementById('vehicleLocationId').length);

	if(document.getElementById('vehicleLocationId').length==1){
		var locationId = document.getElementById('vehicleLocationId').value;
		locationChange(locationId);
		return true;
	}else{
		var transObj = document.getElementById("transDiv");
		transObj.style.display = "block";
		transObj.style.filter = 'alpha(opacity=80)';
		transObj.style.opacity = 0.8;
		
		var elSel = document.getElementById('vehicleLocationId');
		var elOptNew = document.createElement('option');
		elOptNew.text = 'Select Location'  ;
		elOptNew.value = '';
		//alert(elSel.selectedIndex)
		var elOptOld = elSel.options[elSel.selectedIndex];  
		try {
		  elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
		}
		catch(ex) {
		  elSel.add(elOptNew, elSel.selectedIndex); // IE only
		}
		document.getElementById('vehicleLocationId').selectedIndex=0;
	}
	return true;
}

function locationChange(locationId){

		if(locationId==""){
			alert('Please select the location');
			var prevLocation = document.getElementById("locationId").value;
			document.getElementById("vehicleLocationId").value = prevLocation;
			return false;
		}
		document.getElementById("transDiv").style.display = "none";
		var makeId = document.getElementById("makeId").value;
		var tempModel = document.getElementById("vehicleModel").value ;		
		var trimId = document.getElementById("vehicleVariant").value;
		var makemodelarr = tempModel.split("~");		
		var modelId = makemodelarr[2];
		//alert(makeId+" "+modelId+" "+trimId+" "+locationId);
		fillPriceConfigDetails(makeId,modelId,trimId,locationId);
}
