modelQuery = null;

function repopModelSelect(theSelect) {
	var theField = document.searchForm.modID;
	theField.options.length = 0;
	theField.options[0] = new Option('-Select-', '', true, true);
	
	theSelect = theSelect.substring(theSelect.indexOf('<select><option>') + 16, theSelect.indexOf('</option></select>'));
	theOptions = theSelect.split('</option><option>');
	for(i=0; i < theOptions.length; i++){
		var thisOptionValue = theOptions[i].substring(theOptions[i].indexOf('<value>') + 7, theOptions[i].indexOf('</value>'));
		var thisOptionText = theOptions[i].substring(theOptions[i].indexOf('<text>') + 6, theOptions[i].indexOf('</text>'));
		theField.options[i+1] = new Option(thisOptionText, thisOptionValue, false, (thisOptionValue == document.searchForm.modID_selected.value)? true:false);
	}
}

var theModelChecker = { 
	success: function(o) {
		repopModelSelect(o.responseText);
	}, 
	failure: function(o) {
		// do nothing
	}, 
	argument: null
}

function getModelSelect(theMakID){
	var theField = document.searchForm.modID;
	if (theMakID.length){
		theField.options.length = 0;
		theField.options[0] = new Option(' - Updating Choices - ', '', true, true);
		
		var theCheckURL = '/scripts/getVehicleModelsForMake.cfm?makID=' + theMakID;
		modelQuery = YAHOO.util.Connect.asyncRequest('GET', theCheckURL, theModelChecker, null);
	} else {
		theField.options.length = 0;
		theField.options[0] = new Option('-Select a Make-', '', true, true);
	}
}

function gotoPage(thePage){
	document.searchForm.pageNum.value = thePage;
	document.searchForm.submit();
}

function setOrder(theOrder){
	if (document.searchForm.orderBy.value == theOrder){
		document.searchForm.orderBy.value = theOrder + '-DESC';
	} else {
		document.searchForm.orderBy.value = theOrder;
	}
	document.searchForm.submit();
}

function setCompareID(theVehicle){
	if (document.searchForm.compareIDs.value.indexOf(',' + theVehicle) == -1 && document.searchForm.compareIDs.value.indexOf(theVehicle + ',') == -1){
		if(document.searchForm.compareIDs.value.length > 0){
			if(document.searchForm.compareIDs.value.split(',').length == 3){
				document.searchForm.compareIDs.value = document.searchForm.compareIDs.value.substring(0, document.searchForm.compareIDs.value.indexOf(',', document.searchForm.compareIDs.value.indexOf(',') + 1)) + ',' + theVehicle;
			} else {
				document.searchForm.compareIDs.value = document.searchForm.compareIDs.value + ',' + theVehicle;
			}
		} else {
			document.searchForm.compareIDs.value = theVehicle;
		}
		document.searchForm.submit();
	} else {
		alert('You have already added this vehicle to the compare list.');
		return false;
	}
}
