var setMPG; 
var C21array = new Array();
var cselect = getCookieData('selectedcriteria0');




function  presetparams(){
var cntC21 = 0;
var parscriteria = cselect.split("|");
	for (var j = 0; j < parscriteria.length; j++){
	var p = parscriteria[j];
	//alert('This is the value of p: ' + p);
		
		if (p.indexOf('C21') > -1){
		//setMPG = p.indexOf('C21');
		setMPG  = p.indexOf('C21');
		C21array[cntC21] = p;
		cntC21++;
		}//ends C21 if
	}//ends for loop
	setC21(); // populates the miles per Gallon radio button
}


function submitform(){
document.smallfamilyform.submit();
}

function resetmilsPerGal(){
//alert("I am in the resetmils");
var mpgIds =  new Array("oInputMPG21To25", "oInputMPG26To30", "oInputMPG31To35", "oInputMPG36To40", "oInputMPG41orMore");
for(var i = 0; i < mpgIds.length; i++){
var x = mpgIds[i];
document.getElementById(x).value = '';
}
}

function milesPerGal(mileRange){
var x = mileRange;
	if (x == ''){
	resetmilsPerGal();
	}else{
	var mpgIds =  new Array("MPG21To25", "MPG26To30", "MPG31To35", "MPG36To40", "MPG41orMore");
	var hiddenmpgIDs = new Array("oInputMPG21To25", "oInputMPG26To30", "oInputMPG31To35", "oInputMPG36To40", "oInputMPG41orMore");
	
		for(var i = 0; i < mpgIds.length; i++){
			if (x == mpgIds[i]){
			var pos = i + 1;
				for (t = pos; t < mpgIds.length; t++){
				var element = document.getElementById(hiddenmpgIDs[t]);
				element.value=mpgIds[t];
				//alert(element.value);
				}
			}
		}
	}	
}

function setSeat(x){
document.getElementById('Seats5').value = x;
}

function forsunlovers(){
//alert("I am in the function");
//alert(document.smallfamilyform.C2.options[document.smallfamilyform.C2.selectedIndex].text);
//alert(document.getElementById('C2').options[document.getElementById('C2').selectedIndex.text]);
var letsee = document.smallfamilyform.C2.options[document.smallfamilyform.C2.selectedIndex].text;
	if (letsee == 'Sunroof'){
	document.getElementById('C2').name = 'erase';
	document.getElementById('Sunroof').value = 'Sunroof';
	}
}

function submitForm(){

if (document.getElementById('C2')){
	if (document.getElementById('C2').value == 'Convertible'){
		if(document.getElementById('Sunroof')){
		document.getElementById('Sunroof').value = '';
		}
	}
	
	if(document.getElementById('Sunroof')){
		if (document.getElementById('Sunroof').value == ''){
		document.getElementById('Sunroof').name = 'erase';
		}
	}
	
	if (document.getElementById('C2').value == 'sunroofnotifier'){
	document.getElementById('C2').name = 'erase';
	}
}

if (document.getElementById('C21')){
//alert('I have a C21 element');
	erasehidden();
}

erasefirstoption();
document.smallfamilyform.submit();
}




function erasehidden(){
//alert('I am in erasehidden');
var hiddenvars = new Array("oInputMPG21To25", "oInputMPG26To30", "oInputMPG31To35", "oInputMPG36To40", "oInputMPG41orMore");

	for (i=0; i < hiddenvars.length; i++){
	//alert('entering loop');
	var x = hiddenvars[i];
	var j = document.getElementById(x).name;
	//alert("This is the value of j: " + j);
	var y = document.getElementById(x).value;
	//alert("This is the value of y: " + y);
		if (y == ""){
		//alert('changing name');
		document.getElementById(x).name = 'erase';
		//alert(document.getElementById(x).name);
		}
	}
}


function erasefirstoption(){
var listofpossibleemptyval = new Array("C4", "C2", "C6", "C9");
	for (i=0; i < listofpossibleemptyval.length; i++){
		if(document.getElementById(listofpossibleemptyval[i])){
		var x = listofpossibleemptyval[i];
		document.getElementById(x).options[0].selected = false;
		}
	}
}



function setTowing_High(theValue){
//alert('I am in the settow high function');
	if (theValue == ''){
	resetTow_High();
	}else{
	var towing = document.getElementsByName('C22');
	var lastitem = towing.length - 1;
	var HightowC22 = towing[lastitem]; 
		for(var i = 0; i < towing.length; i++){
			var n = towing[i].value;
			if (n == theValue) {
				switch (n){
				case "0" :
				HightowC22.value = 1000;
				break;
				case "1000" :
				HightowC22.value = 2000;
				break;
				case "2000" :
				HightowC22.value = 3000;
				break;
				case "3000" :
				HightowC22.value = 4000;
				break;
				case "4000" :
				HightowC22.value = 5000;
				break;
				case "5000" :
				HightowC22.value = 30000;
				break;
				}
			}
		}
	}
}

function resetTow_High(){
//alert("I am in reset high");
document.getElementById('TowingHigh').value = '';
}

function setPayload_High(theValue){
//alert("I am in payhigh");
	if (theValue == ''){
	resetPay_High();
	}else{
	var towing = document.getElementsByName('C23');
	var lastitem = towing.length - 1;
	var HightowC23 = towing[lastitem];  
		for(var i = 0; i < towing.length; i++){
			var n = towing[i].value;
			if (n == theValue) {
				switch (n){
				case "0" :
				HightowC23.value = 1000;
				break;
				case "1000" :
				HightowC23.value = 2000;
				break;
				case "2000" :
				HightowC23.value = 3000;
				break;
				case "3000" :
				HightowC23.value = 4000;
				break;
				case "4000" :
				HightowC23.value = 5000;
				break;
				case "5000" :
				HightowC23.value = 30000;
				break;
				}
			}
		}
	}
}

function resetPay_High(){
//alert("I am in reset high");
document.getElementById('TowingHigh').value = '';
}

function getRidofCookie(){
delete_cookie('selectedcriteria0');
}

function formClear() {
document.criteriaForm.reset();
document.getElementById('manufactures').style.display = "none";
}



function setC21(){
if (setMPG > -1){
var theValueC21 = C21array[0];
var valInput = theValueC21.split(":")
var theValue = valInput[1];
			switch (theValue){
			case "MPG21To25" :
			document.getElementById('MPG21To25').selected = true;
			milesPerGal(theValue);
			break;
			case "MPG26To30" :
			document.getElementById('MPG26To30').selected = true;
			milesPerGal(theValue);
			break;
			case "MPG31To35" :
			document.getElementById('MPG31To35').selected = true;
			milesPerGal(theValue);
			break;
			case "MPG36To40" :
			document.getElementById('MPG36To40').selected = true;
			milesPerGal(theValue);
			break;
			case "MPG41orMore" :
			document.getElementById('MPG41orMore').selected = true;
			milesPerGal(theValue);
			break;
			}
} 
}




function loadpage(){
//fillForm();
delete_cookie('selectedcriteria0');
} 

function getCookieData(labelName){
	var labelLen = labelName.length;
	var cookieData = document.cookie;
	var cLen = cookieData.length;
	var i = 0;
	var cEnd;
	while(i < cLen){
		var j = i + labelLen;
		if (cookieData.substring(i,j) == labelName){
		cEnd = cookieData.indexOf(";",j);
			if (cEnd == -1){
				cEnd = cookieData.length;
			}
			return unescape(cookieData.substring(j+1, cEnd));
		}
		i++
	}
	return "";
}	


// Go Back and fillin empty picture code
var allChange = new Array();

function addImg(index){
//alert("Error: " + index);
allChange[index].error = true;
}

function changeAll(){
	for(i=0;i<allChange.length;i++){
		if(allChange[i].error){
		document.getElementById(allChange[i].id).src = allChange[i].src;
		}
	}
}

function runerrorset(){
seterror = true;
}

function delete_cookie(cookie_name){ 
var cookie_date = new Date ( );  // current date & time
cookie_date.setTime ( cookie_date.getTime() - 1 );
document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
