
var ajax = new Array();

function getModelList(sel,ord,modl)
{
	var carCode = sel.options[sel.selectedIndex].value;
	var modlx = modl;
	var shortie = document.getElementById("modelSelect" + ord);
	shortie.options.length = 0;	// Empty model select box
	if(carCode.length>0){
		
		var index = ajax.length;
		ajax[index] = new sack();
		ajax[index].requestFile = 'carcompareget.php?carsCode='+carCode+'&modl='+modlx;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createModels(index,ord) };	// function that will be executed after file found
		ajax[index].runAJAX();		// Execute AJAX function
		shortie.disabled = false;
		shortie.options[shortie.options.length] = new Option('Choose the model','0');
	}
}

function createModels(index,ord)
{
	var obj = document.getElementById('modelSelect' + ord);
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
function getTrimList(sel,ord,triml)
{
	var trimCode = sel.options[sel.selectedIndex].text;
	
	var trimlx = triml;
	var shortie = document.getElementById("trimSelect" + ord);
	shortie.options.length = 0;	// Empty trim select box
	if(trimCode.length>0){
				
		var index = ajax.length;
		ajax[index] = new sack();
		ajax[index].requestFile = 'carcompareget2.php?trimsCode='+trimCode+'&triml='+trimlx;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createTrims(index,ord) };	// function that will be executed after file found
		ajax[index].runAJAX();		// Execute AJAX function
		shortie.disabled = false;
		shortie.options[shortie.options.length] = new Option('Choose the trim level','0');
	}
}

function createTrims(index,ord)
{
	var obj = document.getElementById('trimSelect' + ord);
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////


function sendDetail(sel)
{
	var carID1 = document.specific.txtCar1.value;
	var carID2 = document.specific.txtCar2.value;
	var carID3 = document.specific.txtCar3.value;
	
	if(carID1.length==0&&carID2.length==0&&carID3.length==0){alert ("You did not choose any car models!");return false;}
	if(carID1.length>0&&carID2.length==0&&carID3.length==0){alert ("You must choose at least 2 models to compare!");return false;}
	if(carID1.length==0&&carID2.length>0&&carID3.length==0){alert ("You must choose at least 2 models to compare!");return false;}
	if(carID1.length==0&&carID2.length==0&&carID3.length>0){alert ("You must choose at least 2 models to compare!");return false;}
	
	if(carID1==carID2){alert ("You must choose different models!");return false;}
	if(carID1==carID3){alert ("You must choose different models!");return false;}
	if(carID2==carID3){alert ("You must choose different models!");return false;}

	 window.location = 'carcompresult.php?id1='+carID1+"&id2="+carID2+"&id3="+carID3;
}
////////////////////////////////////////////////////////////////
function showimage(ord){
	if (!document.images)
	return
	shortie = document.specific["modelSelect"+ord];
	origText = shortie.options[shortie.selectedIndex].text; 
	origVal = shortie.options[shortie.selectedIndex].value;

	document.images["pictures"+ord].src = 'carpics/' + origVal + '.jpg'
	document.specific["txtModel"+ord].value = origText;
}

function showid(ord){
	shortie = document.specific["trimSelect"+ord];
	document.specific["txtCar"+ord].value = shortie.options[shortie.selectedIndex].value;
	document.specific["txtTrim"+ord].value = shortie.options[shortie.selectedIndex].text;
}


function returner(){
var comboValue 
var selIndex = document.specific.makeSelect1.selectedIndex; 
comboValue = document.specific.makeSelect1.options[selIndex].value;

	if(comboValue.length>0){
		getModelList(document.specific.makeSelect1,1,document.specific.txtModel1.value);
		getModelList(document.specific.makeSelect2,2,document.specific.txtModel2.value);
		getModelList(document.specific.makeSelect3,3,document.specific.txtModel3.value);
		alert ("Your previous selection has been saved");
		
		getTrimList(document.specific.modelSelect1,1,document.specific.txtTrim1.value);
		showimage(1);
		getTrimList(document.specific.modelSelect2,2,document.specific.txtTrim2.value);
		showimage(2);
		getTrimList(document.specific.modelSelect3,3,document.specific.txtTrim3.value);
		showimage(3);
			
		
	}
}
