// Open AJAX connection
function getXMLHTTP() { //fuction to return the xml http object
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	}
	catch(e)	{		
		try{			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1){
				xmlhttp=false;
			}
		}
	}
		
	return xmlhttp;
}

function popMakes(vYear) {
		
	var strURL = "popMakes.php?year=" + vYear;
	var req = getXMLHTTP();
	
	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status == 200) {						
					document.getElementById('iVehicleMakeHolder').innerHTML=req.responseText;
					empty_list(document.getElementById("iVehicleModel"));
					add_list_item(document.getElementById("iVehicleModel"),"Any Model","Select Model");
					empty_list(document.getElementById("iVehicleTrim"));
					add_list_item(document.getElementById("iVehicleTrim"),"Any Trim","Select Trim");
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			
		req.open("GET", strURL, true);
		req.send(null);
	}	
	
}

function popModels(vYear, vMake) {
		
	var strURL = "popModels.php?year=" + vYear + "&make=" + vMake;
	var req = getXMLHTTP();
	
	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status == 200) {						
					document.getElementById('iVehicleModelHolder').innerHTML=req.responseText;
					empty_list(document.getElementById("iVehicleTrim"));
					add_list_item(document.getElementById("iVehicleTrim"),"Any Trim","Select Trim");					
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			
		req.open("GET", strURL, true);
		req.send(null);
	}	
	
}

function popTrims(vYear, vMake, vModel) {
		
	var strURL = "popTrims.php?year=" + vYear + "&make=" + vMake + "&model=" + vModel;
	var req = getXMLHTTP();
	
	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status == 200) {						
					document.getElementById('iVehicleTrimHolder').innerHTML=req.responseText;						
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			
		req.open("GET", strURL, true);
		req.send(null);
	}	
	
}

// Confirmation window before proceding to a page
function confirmAction(warning,link) {
	var answer = confirm (warning)
	if (answer)
		window.location = link;
	else
		return false;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function openRow(rowid) {
	if (document.getElementById(rowid + '-inner').style.display == 'block') {
		document.getElementById(rowid).style.border = '';
		document.getElementById(rowid + '-inner').style.display = 'none';
		document.getElementById(rowid).style.marginTop = '0px';
		document.getElementById(rowid).style.marginBottom = '2px';
		document.getElementById(rowid).style.backgroundColor = '';
	}
	else {
		document.getElementById(rowid).style.border = '1px solid #0F1B4B';
		document.getElementById(rowid + '-inner').style.display = 'block';
		document.getElementById(rowid).style.marginTop = '5px';
		document.getElementById(rowid).style.marginBottom = '5px';
		document.getElementById(rowid).style.backgroundColor = '#C7D0E3';
	}
}

function swapParam(key,value){
	var current_url = unescape(location.href);
	var q_pos = current_url.indexOf('?');
	var querystring = current_url.substring(q_pos + 1);
	var arguments = querystring.split('&');
	var new_querystring = '';
	var newLocationString = '';
	var writeArgument = true;
	// remove any trace of parameter attached on the current link
	for (i = 0; i < arguments.length ; i++){
		writeArgument = true;
		
		if (key != 'filter') { // filter and unfilter params can stick so this removes them but not if theyre whats being set
			if (arguments[i].indexOf('filter=') >= 0) // 
				writeArgument = false;
		}
		
		if (key != 'unfilter') {
			if (arguments[i].indexOf('unfilter=') >= 0)
				writeArgument = false;
		}
		
		if (arguments[i].indexOf('clear=') >= 0) // will never stick around
			writeArgument = false;
		
		if (arguments[i].indexOf('page=') >= 0) // will never stick around
			writeArgument = false;
		
		if (arguments[i].indexOf(key + '=') >= 0)
			writeArgument = false;
			
		if (writeArgument == true)
			new_querystring += arguments[i] + '&';
	}
	//eliminate trailing ampersand
	new_querystring = new_querystring.substring(0, new_querystring.length - 1);
	// add the appropriate flags for what carfax options they need - for the none case do nothing since we removed those flags above already
	new_querystring = new_querystring + '&' + key + '=' + value;
	newLocationString = '?' + new_querystring;
	location.href = newLocationString;
}

function filterSearch() {
	var field = document.getElementById("vlSearchField").options[document.getElementById("vlSearchField").selectedIndex].value;
	var value= document.getElementById('vlSearchValue').value;
	if (value.length > 0 || value != '') {
		var sFilter = field + "|" + value;
		swapParam('filter', sFilter);
	}
}

////////////////////////////////////////////////////////////////
// Basic list box functions
////////////////////////////////////////////////////////////////
function add_list_item(ob, value, text){
	var option = document.createElement("OPTION");
	IE = ((document.all)&&(navigator.appVersion.indexOf("MSIE")!=-1)) ? true : false;

	option.value = value;
	option.text = text;

	if (IE)
		ob.add(option);
	else
		ob.add(option, null);
}
		
function empty_list(ob){
	while(ob.length){
		ob.remove(0);
	}
}

function populate_wanted_makes() {
	// Using the large make/model array, update the make choices.
	var make;
	add_list_item(document.getElementById("iVehicleMake"),"Any Make","Select Make");
	for (i=0; i<VehicleWantedList.length; i++){
		//Assign the value of the first element in each array (make name) to a temp variable.
		make = VehicleWantedList[i][0];
		add_list_item(document.getElementById("iVehicleMake"),make,make);
	}
	document.getElementById("iVehicleMake").selectedIndex = 0;
	add_list_item(document.getElementById("iVehicleModel"),"Any Model","Select Model");
	document.getElementById("iVehicleModel").disabled = true;
}
        
function populate_trade_makes() {
	// Using the large make/model array, update the make choices.
	var make;
	add_list_item(document.getElementById("iTradeMake"),"Any Make","Select Make");
	for (i=0; i<VehicleTradeList.length; i++){
		//Assign the value of the first element in each array (make name) to a temp variable.
		make = VehicleTradeList[i][0];
		add_list_item(document.getElementById("iTradeMake"),make,make);
	}
	document.getElementById("iTradeMake").selectedIndex = 0;
	add_list_item(document.getElementById("iTradeModel"),"Any Model","Select Model");
	document.getElementById("iTradeModel").disabled = true;
}
		
function wanted_changed_search() {
	// Get the new value in the make drop.
	var make = document.getElementById("iVehicleMake").value;

	if (make != "Any Make") {
		// Clear the model list.
		empty_list(document.getElementById("iVehicleModel"));

		// Add the first item back to the model list.
		add_list_item(document.getElementById("iVehicleModel"),"Any Model","Select Model");
		
		// Get the additional values of this particular make array and add them to the model list.
		var veh_index = document.getElementById("iVehicleMake").selectedIndex - 1;
		var upper_bound = VehicleWantedList[veh_index].length;
		
		for (i=1; i<upper_bound; i++)
			add_list_item(document.getElementById("iVehicleModel"),VehicleWantedList[veh_index][i],VehicleWantedList[veh_index][i]);

		document.getElementById("iVehicleModel").selectedIndex = 0;              
		
		// Enabled the model drop box.
		document.getElementById("iVehicleModel").disabled = false;
		
	} else {
		document.getElementById("iVehicleModel").selectedIndex = 0;
		document.getElementById("iVehicleModel").disabled = true;
	}
}
			
function trade_changed_search() {
	// Get the new value in the make drop.
	var make = document.getElementById("iTradeMake").value;

	if (make != "Any Make") {
		// Clear the model list.
		empty_list(document.getElementById("iTradeModel"));

		// Add the first item back to the model list.
		add_list_item(document.getElementById("iTradeModel"),"Any Model","Select Model");
		
		// Get the additional values of this particular make array and add them to the model list.
		var veh_index = document.getElementById("iTradeMake").selectedIndex - 1;
		var upper_bound = VehicleTradeList[veh_index].length;
		
		for (i=1; i<upper_bound; i++)
			add_list_item(document.getElementById("iTradeModel"),VehicleTradeList[veh_index][i],VehicleTradeList[veh_index][i]);

		document.getElementById("iTradeModel").selectedIndex = 0;
			
		// Enabled the model drop box.
		document.getElementById("iTradeModel").disabled = false;
		
	} else {
		document.getElementById("iTradeModel").selectedIndex = 0;
		document.getElementById("iTradeModel").disabled = true;
	}
}
