
	// AJAX Funtionality
	// *******************************************
	var req;

	function loadXMLDoc(url) {
		req = false;
		if(window.XMLHttpRequest){
    		try { req = new XMLHttpRequest(); }
    		catch(e) { req = false;	}
		} 
		else if(window.ActiveXObject) {
       		try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
       		catch(e) {
       			try { req = new ActiveXObject("Microsoft.XMLHTTP");	}
       			catch(e) { req = false; }
			}
		}
		if(req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send("");
		}
	}	
	
	function processReqChange(){
		if (req.readyState == 4){
			if (req.status == 200){	
				processXMLReponse(req.responseXML);
			}else{
				alert("There was a problem processing the command:\n" + req.statusText);
			}
		}
	}
	
		
	// Core function that handles the reponses
	function processXMLReponse(xmldoc){
		//Populate Lists
		var node_list;
		var select;
			
		// Makes
		UpdateList(xmldoc, "MAKE", "SelectMake", "Any Make");
				
		// Models
		UpdateList(xmldoc, "MODEL", "SelectModel", "Any Model");
		
		// Exterior Colors
		UpdateList(xmldoc, "COLOR", "SelectColor", "Any Color");
				
		// Trim
		UpdateList(xmldoc, "TRIM", "SelectTrim", "Any Trim");
		
		// BodyStyles
		UpdateList(xmldoc, "BODYSTYLE", "SelectBody", "Any Body");
		
		UpdateMilesList(xmldoc, "SelectMileage");
		
		// Years
		UpdateYearList(xmldoc, "SelectYearMin");
		UpdateYearList(xmldoc, "SelectYearMax");
		
		// Price Range
		UpdatePriceList(xmldoc, "SelectPriceMin");
		UpdatePriceList(xmldoc, "SelectPriceMax");
		
	} 

	function UpdateList(xmldoc, tagName, selectBoxName, default_option){
		var node_list = xmldoc.getElementsByTagName(tagName);
		var select = document.getElementById(selectBoxName);
		var counter = 1;
		
		// Store Past Value (only retain the value if the item is in the stack)
		var stored_value;
		
		if(document.getElementById("stack").value.match(tagName.toLowerCase()) != null){
			//alert(tagName + " in stack, reselecting " + document.getElementById("stack").value.match(tagName.toLowerCase()));
			if(select.selectedIndex != -1){
				stored_value = select.options[select.selectedIndex].value;
			}
		}
		
		// Clear List
		while (select.length > 0){
			select.remove(0);
		}
		
		// Repopulate
		select.options[0] = new Option(default_option, "Any");
		for (var i = 0; i < node_list.length; i++){
			if(node_list.item(i).firstChild != null){
				option_value = node_list.item(i).firstChild.nodeValue
				select.options[counter] = new Option(option_value, option_value);
				counter++;
			}
		}
		
		// Reselect
		for (var i = 0; i < select.options.length; i ++){
			option_value = select.options[i].value;
			//alert(option_value + " - " + stored_value);
			if(option_value == stored_value){
				select.selectedIndex = i;
			}
		}
	}
		
	function UpdateYearList(xmldoc, selectBoxName){
		var select = document.getElementById(selectBoxName);
		var counter = 1;
		var minyear;
		var maxyear;
		
		if (xmldoc.getElementsByTagName("MINYEAR").item(0).firstChild != null && xmldoc.getElementsByTagName("MAXYEAR").item(0).firstChild.nodeValue != null){
			minyear = xmldoc.getElementsByTagName("MINYEAR").item(0).firstChild.nodeValue;
			maxyear = xmldoc.getElementsByTagName("MAXYEAR").item(0).firstChild.nodeValue;
		}
		
		// Store Past Value
		var stored_value;
		
		if(document.getElementById("stack").value.match("year") != null){
			if(select.selectedIndex != -1){
				stored_value = select.options[select.selectedIndex].value;
			}
		}
		
		// Clear List
		while (select.length > 0){
			select.remove(0);
		}
		
		// Repopulate
		select.options[0] = new Option("Any", "Any");
		var counter = 1;
		
		// Check to see if either list should be restricted.
		if (selectBoxName == "SelectYearMin"){
		    // See if there is a current value for selectmaxyear...
		    if(document.getElementById("SelectYearMax").value != "" && document.getElementById("SelectYearMax").value != "Any"){
		        maxyear = document.getElementById("SelectYearMax").value;
		    }   
		}
		
		if (selectBoxName == "SelectYearMax"){
		    // See if there is a current value for selectmaxyear...
		    if(document.getElementById("SelectYearMin").value != "" && document.getElementById("SelectYearMin").value != "Any"){
		        minyear = document.getElementById("SelectYearMin").value;
		    }   
		}
				
		for (var i = minyear; i <= maxyear; i++){
			select.options[counter] = new Option(i,i);
			counter++;
		}		
		// Reselect
		for (var i = 0; i < select.options.length; i ++){
			option_value = select.options[i].value;
			if(option_value == stored_value){
				select.selectedIndex = i;
			}
		}
	}

	function UpdateMilesList(xmldoc, selectBoxName){
		var select = document.getElementById(selectBoxName);
		var counter = 1;
		
		var minmiles;
		var maxmiles;
		
		if (xmldoc.getElementsByTagName("MINMILES").item(0).firstChild != null && xmldoc.getElementsByTagName("MAXMILES").item(0).firstChild.nodeValue != null){
			minmiles = xmldoc.getElementsByTagName("MINMILES").item(0).firstChild.nodeValue;
			maxmiles = xmldoc.getElementsByTagName("MAXMILES").item(0).firstChild.nodeValue;
		}
		
		// Store Past Value
		var stored_value;
		
		if(document.getElementById("stack").value.match("miles") != null){
			if(select.selectedIndex != -1){
				stored_value = select.options[select.selectedIndex].value;
			}
		}
		
		// Clear List
		while (select.length > 0){
			select.remove(0);
		}
		
		// Repopulate
		select.options[0] = new Option("Any", "Any");
		var counter = 1;
		
		
		if (minmiles <= 5000 && maxmiles >= 0){
			select.options[counter] = new Option("5,000",5000);
			counter++;
		}
		if (minmiles <= 10000 && maxmiles > 5000){
			select.options[counter] = new Option("10,000",10000);
			counter++;
		}
		if (minmiles <= 25000 && maxmiles > 10000){
			select.options[counter] = new Option("25,000",25000);
			counter++;
		}
		if (minmiles <= 50000 && maxmiles > 25000){
			select.options[counter] = new Option("50,000",50000);
			counter++;
		}
		if (minmiles <= 75000 && maxmiles > 50000){
			select.options[counter] = new Option("75,000",75000);
			counter++;
		}
		if (minmiles <= 100000 && maxmiles > 75000){
			select.options[counter] = new Option("100,000",100000);
			counter++;
		}
		if (minmiles <= 150000 && maxmiles > 100000){
			select.options[counter] = new Option("150,000",150000);
			counter++;
		}
				
		// Reselect
		for (var i = 0; i < select.options.length; i ++){
			option_value = select.options[i].value;
			if(option_value == stored_value){
				select.selectedIndex = i;
			}
		}
	}

	function UpdatePriceList(xmldoc, selectBoxName){
		var select = document.getElementById(selectBoxName);
		var counter = 1;
		
		var minprice;
		var maxprice;
		
		if (xmldoc.getElementsByTagName("MINPRICE").item(0).firstChild != null && xmldoc.getElementsByTagName("MAXPRICE").item(0).firstChild.nodeValue != null){
			minprice = xmldoc.getElementsByTagName("MINPRICE").item(0).firstChild.nodeValue;
			maxprice = xmldoc.getElementsByTagName("MAXPRICE").item(0).firstChild.nodeValue;
		}
		
		// Store Past Value
		var stored_value;
		
		if(document.getElementById("stack").value.match("price") != null){
			if(select.selectedIndex != -1){
				stored_value = select.options[select.selectedIndex].value;
			}
		}
		
		// Clear List
		while (select.length > 0) {
			select.remove(0);
		}
		
		// Repopulate
		select.options[0] = new Option("Any", "Any");
		var counter = 1;
		
		// Check to see if either list should be restricted.
		if (selectBoxName == "SelectPriceMin"){
		    // See if there is a current value for selectmaxyear...
		    if(document.getElementById("SelectPriceMax").value != "" && document.getElementById("SelectPriceMax").value != "Any"){
		        maxprice = document.getElementById("SelectPriceMax").value - 2;
		    }   
		}
		
		if (selectBoxName == "SelectPriceMax"){
		    // See if there is a current value for selectmaxyear...
		    if(document.getElementById("SelectPriceMin").value != "" && document.getElementById("SelectPriceMin").value != "Any"){
		        minprice = document.getElementById("SelectPriceMin").value;
		    }   
		}

		if (minprice <= 4999)
		{
			select.options[counter] = new Option("$0",4999);
			counter++;
		}
		if (minprice <= 9999 && maxprice >= 0)
		{
			select.options[counter] = new Option("$5,000",5000);
			counter++;
		}		
		if (minprice <= 14999 && maxprice >= 5000)
		{
			select.options[counter] = new Option("$10,000",10000);
			counter++;
		}
		if (minprice <= 19999 && maxprice >= 10000)
		{
			select.options[counter] = new Option("$15,000",15000);
			counter++;
		}
		if (minprice <= 24999 && maxprice >= 15000)
		{
			select.options[counter] = new Option("$20,000",20000);
			counter++;
		}
		if (minprice <= 29999 && maxprice >= 20000)
		{
			select.options[counter] = new Option("$25,000",25000);
			counter++;
		}
		if (minprice <= 34999 && maxprice >= 25000)
		{
			select.options[counter] = new Option("$30,000",30000);
			counter++;
		}
		if (minprice <= 39999 && maxprice >= 30000)
		{
			select.options[counter] = new Option("$35,000",35000);
			counter++;
		}
		if (minprice <= 44999 && maxprice >= 35000)
		{
			select.options[counter] = new Option("$40,000",40000);
			counter++;
		}
		if (minprice <= 39999 && maxprice >= 40000)
		{
			select.options[counter] = new Option("$45,000",45000);
			counter++;
		}
		if (minprice <= 44999 && maxprice >= 45000)
		{
			select.options[counter] = new Option("$50,000",50000);
			counter++;
		}		        			        		        		        
		if (minprice <= 49999 && maxprice >= 50000)
		{
			select.options[counter] = new Option("$55,000",55000);
			counter++;
		}
		if (minprice <= 54999 && maxprice >= 55000)
		{
			select.options[counter] = new Option("$60,000",60000);
			counter++;
		}
		if (minprice <= 59999 && maxprice >= 60000)
		{
			select.options[counter] = new Option("$65,000",65000);
			counter++;
		}
		if (minprice <= 64999 && maxprice >= 65000)
		{
			select.options[counter] = new Option("$70,000",70000);
			counter++;
		}
		if (minprice <= 99999 && maxprice >= 70000)
		{
			select.options[counter] = new Option("$75,000",75000);
			counter++;
		}		        		        
		if (maxprice >= 75000)
		{
			select.options[counter] = new Option("$100000+",100000);
			counter++;
		}

		// Reselect
		for (var i = 0; i < select.options.length; i ++){
			option_value = select.options[i].value;
			if(option_value == stored_value){
				select.selectedIndex = i;
			}
		}
	}

	// Page Functionality 
	// *******************************************
	function init()
	{
	    loadXMLDoc("/models.asp?type=used&stack=");
	}
	
	function ParamsChanged(type)
	{
		cur_stack = document.getElementById("stack").value;
		new_stack = "";
		
		if (cur_stack.match(type) != null){
			new_stack = cur_stack.substring(0,cur_stack.indexOf(type) + type.length);
		}else{
			if (cur_stack != ""){
				new_stack = cur_stack + ",";
			}
			new_stack = new_stack + type;
		}

		document.getElementById("stack").value = new_stack;
					
		var params = "";
		
		if (document.getElementById("type").value != ""){
		    params = "type=" + document.getElementById("type").value + "&stack=" + new_stack;
		}else{
		    params = "stack=" + new_stack;
		}
		var l_make;
		var l_model;
		var l_color;
		var l_trim;
		var l_miles;
		var l_yearmin;
		var l_yearmax;
		var l_pricemin;
		var l_pricemax;
		var l_location;
		var l_bodystyle = "";
		
		if(document.getElementById("SelectMake").selectedIndex != -1){
			l_make = document.getElementById("SelectMake").options[document.getElementById("SelectMake").selectedIndex].value;
			params += "&make=" + l_make;
		}
		
		if(document.getElementById("SelectModel").selectedIndex != -1){
			l_model = document.getElementById("SelectModel").options[document.getElementById("SelectModel").selectedIndex].value;
			params += "&model=" + escape(l_model);
		}
		
		if(document.getElementById("SelectColor").selectedIndex != -1){
			l_color = document.getElementById("SelectColor").options[document.getElementById("SelectColor").selectedIndex].value;
			params += "&color=" + l_color;
		}
		
		if(document.getElementById("SelectLocation").selectedIndex != -1)
		{
			l_location = document.getElementById("SelectLocation").options[document.getElementById("SelectLocation").selectedIndex].value;
			params += "&location=" + l_location;
		}
		
		if(document.getElementById("SelectTrim").selectedIndex != -1){
			l_trim = document.getElementById("SelectTrim").options[document.getElementById("SelectTrim").selectedIndex].value;
			params += "&trim=" + l_trim;
		}

		if(document.getElementById("SelectMileage").selectedIndex != -1){
			l_miles = document.getElementById("SelectMileage").options[document.getElementById("SelectMileage").selectedIndex].value;
			params += "&mileage=" + l_miles;
		}
		
		if(document.getElementById("SelectYearMin").selectedIndex != -1){
			l_yearmin = document.getElementById("SelectYearMin").options[document.getElementById("SelectYearMin").selectedIndex].value;
			params += "&yearmin=" + l_yearmin;
		}
		
		if(document.getElementById("SelectYearMax").selectedIndex != -1){
			l_yearmax = document.getElementById("SelectYearMax").options[document.getElementById("SelectYearMax").selectedIndex].value;
			params += "&yearmax=" + l_yearmax;
		}
		
		if(document.getElementById("SelectPriceMin").selectedIndex != -1){
			l_pricemin = document.getElementById("SelectPriceMin").options[document.getElementById("SelectPriceMin").selectedIndex].value;
			if (l_pricemin == "4999"){
			    l_pricemin = "0";
			}
			params += "&pricemin=" + l_pricemin;
		}
		
		if(document.getElementById("SelectPriceMax").selectedIndex != -1){
			l_pricemax = document.getElementById("SelectPriceMax").options[document.getElementById("SelectPriceMax").selectedIndex].value;
			params += "&pricemax=" + l_pricemax;
		}
		
		if(document.getElementById("SelectBody").selectedIndex != -1){
			l_body = document.getElementById("SelectBody").options[document.getElementById("SelectBody").selectedIndex].value;
			params += "&bodystyle=" + l_body;
		}
		
		//alert("/models.asp?" + params);
		document.getElementById("params").value = params;
		
		// Get the results for the given parameters
		loadXMLDoc("/models.asp?" + params); 
	}
	
	function PostSearch(){
		// Create the parameters
		var params = "param_new-used=" + document.getElementById("type").value;
		var l_make;
		var l_model;
		var l_color;
		var l_trim;
		var l_miles;
		var l_yearmin;
		var l_yearmax;
		var l_pricemin;
		var l_pricemax;
		var l_options = "";
		var l_bodystyle;
		var l_location;
		var price_field = "sellingprice";
		
		if(document.getElementById("SelectLocation").selectedIndex != -1)
		{
			l_location = document.getElementById("SelectLocation").options[document.getElementById("SelectLocation").selectedIndex].value;
			if (l_location == "Warehouse")
			{
				params += "&param_comment5=W&param_location=SunChevrolet";
			}
			if (l_location == "SunChev")
			{
				params += "&exclude=comment5|W&param_location=SunChevrolet";
			}	
			if (l_location == "SunAutoWarehouseofCortland")
			{
				params += "&param_location=SunAutoWarehouseofCortland";
			}
		}
		if(document.getElementById("SelectMake").selectedIndex != -1){
			l_make = document.getElementById("SelectMake").options[document.getElementById("SelectMake").selectedIndex].value;
			if (l_make != "Any"){
				params += "&param_make=" + l_make;
			}
		}
		if(document.getElementById("SelectModel").selectedIndex != -1){
			l_model = document.getElementById("SelectModel").options[document.getElementById("SelectModel").selectedIndex].value;
			if (l_model != "Any"){
				params += "&param_model=" + escape(l_model);
			}
		}
		
		if(document.getElementById("SelectTrim").selectedIndex != -1){
			l_trim = document.getElementById("SelectTrim").options[document.getElementById("SelectTrim").selectedIndex].value;
			if (l_trim != "Any"){
				params += "&param_trim=" + l_trim;
			}
		}
		
		if(document.getElementById("SelectMileage").selectedIndex != -1){
			l_miles = document.getElementById("SelectMileage").options[document.getElementById("SelectMileage").selectedIndex].value;
			if (l_miles != "Any"){
				params += "&param_miles_end=" + l_miles;
			}
		}
		
		if(document.getElementById("SelectColor").selectedIndex != -1){
			l_color = document.getElementById("SelectColor").options[document.getElementById("SelectColor").selectedIndex].value;
			if (l_color != "Any"){
				params += "&param_exteriorcolor=" + l_color;
			}
		}
		if(document.getElementById("SelectYearMin").selectedIndex != -1){
			l_yearmin = document.getElementById("SelectYearMin").options[document.getElementById("SelectYearMin").selectedIndex].value;
			if (l_yearmin != "Any"){
				params += "&param_year_begin=" + l_yearmin;
			}
		}
		if(document.getElementById("SelectYearMax").selectedIndex != -1){
			l_yearmax = document.getElementById("SelectYearMax").options[document.getElementById("SelectYearMax").selectedIndex].value;
			if (l_yearmax != "Any"){
				params += "&param_year_end=" + l_yearmax;
			}
		}
		if(document.getElementById("SelectPriceMin").selectedIndex != -1)
		{
			l_pricemin = document.getElementById("SelectPriceMin").options[document.getElementById("SelectPriceMin").selectedIndex].value;
			if (l_pricemin != "Any")
			{
				params += "&param_" + price_field + "_begin=" + l_pricemin;
			}
		}
		if(document.getElementById("SelectPriceMax").selectedIndex != -1)
		{
			l_pricemax = document.getElementById("SelectPriceMax").options[document.getElementById("SelectPriceMax").selectedIndex].value;
			if (l_pricemax != "Any")
			{
				params += "&param_" + price_field + "_end=" + l_pricemax;
			}
		}
		if(document.getElementById("SelectBody").selectedIndex != -1){
			l_bodystyle = document.getElementById("SelectBody").options[document.getElementById("SelectBody").selectedIndex].value;
			if (l_bodystyle != "Any"){
				if (l_bodystyle.toLowerCase() == "truck")
					params += "&param_standardbody=pickup"; // hack for converting pickup to truck in drop down box... reconverting here as well as in models xml file
				else
					params += "&param_standardbody=" + l_bodystyle;
			}
		}
		
		// Handle options
		// Normal options
		var cbxOptions = document.forms["Form1"].search_options;
		for (i = 0; i < cbxOptions.length; i++)
		{
			if (cbxOptions[i].checked)
			{
				var checkedOptionValue = cbxOptions[i].value;
				var checkedOptions = checkedOptionValue.split(",");
				var checkedOptionsColl = "";
				
				if (l_options != "")
					l_options = l_options + ",";
					
				for (k = 0; k < checkedOptions.length; k++) {
					if (k == checkedOptions.length-1)
						checkedOptionsColl = checkedOptionsColl + checkedOptions[k];
					else
						checkedOptionsColl = checkedOptionsColl + checkedOptions[k] + "|";
				}
				
				l_options = l_options + checkedOptionsColl;

			}
		}
		// Check for custom listed options
		var customOpts = document.getElementById('search_options_custom').value;
		if (customOpts != '') {
			if (l_options != "")
				l_options = l_options + ',' + customOpts;
			else
				l_options = customOpts;
		}
		// If options exist then add the parameter
		if (l_options != "")
		{
			params += "&search_options=" + l_options;	
		}

		
		location.href = 'inventory/browse2.asp?' + params;
	}	

