﻿	var xmlHttpForm;
	var xmlHttpResult;
	
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

	function openExplain( criterionID ) {
	    newwindow=window.open('CriteriaExplain.asp?criterionID='+criterionID, 'Explain','width=600,height=330,resizable=1,scrollbars=1');
	    if (window.focus) {newwindow.focus()}
	}
	function openDoorOpeningExplain() {
	    newwindow = window.open('DoorOpeningExplainPopUp.asp', 'Explain', 'width=600,height=330,resizable=1,scrollbars=1');
	    if (window.focus) { newwindow.focus() }
	}
	


	function UpdateRecomendedValues() {

		oManufacturer	= document.getElementById("ddlManufacturer");
		oModel			= document.getElementById("ddlModel");
		
		var ManufacturerID = oManufacturer.options[oManufacturer.selectedIndex].value;
		var CarID = oModel.options[oModel.selectedIndex].value;
		
		showForm("", "", ManufacturerID, CarID); 
	}

	function InitModelList(ListNumber, selected) {

		oModel	= document.getElementById("ddlModel");
		list	= GetModelList(ListNumber);

		for (i=oModel.options.length; i>=0; i--) {
			oModel.options[i] = null;
		}
	
		j=0;
		for (i=0; i<list.length; i++) {
			oModel.options[j] = new Option(list[i][1], list[i][0]);		
			if (list[i][0] == selected ) {
				oModel.options[j].selected = true;	
			}
			j++;
		}						
	}

	function FillModel( ListNumber ) {
		
		oModel	= document.getElementById("ddlModel");
		list	= GetModelList(ListNumber);

		// empty existing items
		for (i=oModel.options.length; i>=0; i--) {
			oModel.options[i] = null;
		}
		
		j=0;
		for (i=0; i<list.length; i++) {
			oModel.options[j] = new Option(list[i][1], list[i][0]);		
			j++;
		}		
	}
									
	function resetForm() {
		
		//oManufacturer	= document.getElementById("ddlManufacturer");
		//oModel			= document.getElementById("ddlModel");
		//oManufacturer.selectedIndex = 0;
		//oModel.selectedIndex = 0;
		
		showResult('RESET','');
		showForm("1", "", "", ""); 
		window.location = "carmeasurement.asp"
	}
	
	function changeMeasurement(value) {
		document.getElementById("selectedMeasurement").value = value;
		showForm("", value, "", ""); 
		showResult("","");
	}
	
	function sendInput(form,multiply) {
	    
	    
		var inputString = "";
		var input		= "";
		var attribute = document.getElementById("inputAttribute").value;
		
		var arAttribute	= attribute.split(";");
		for (var i=0; i<arAttribute.length-1; i++) {
			arr = arAttribute[i].split("|");
			inputString = getInputString(arr[0], arr[1], arr[2], multiply) + ";" + inputString;
			
			}

		
		showResult("VIEW",inputString);
		showForm(form, multiply, "", "");		
	}
	
	function getInputString(criterionID, id, type, multiply) {	

		var inputString = "";
		if (type=="DL") {
			var oField = document.getElementById("Field"+ id);	
			var value = oField.options[oField.selectedIndex].value;
			inputString = criterionID + "|" + value + "||1";
		} else {

			if ( (criterionID==10) || (criterionID==95) ) { multiply = 1; }

			var oMin		= document.getElementById("Field"+ id + "Min");	
			var oMax 		= document.getElementById("Field"+ id + "Max");	
			var oDefaultMin	= document.getElementById("Default"+ id + "Min");	
			var oDefaultMax = document.getElementById("Default"+ id + "Max");	
			
			var inputMin 	= oMin.value.trim();
			var inputMax	= oMax.value.trim();
			var defaultMin 	= oDefaultMin.value.trim();
			var defaultMax 	= oDefaultMax.value.trim();

			if ( !isNumeric(defaultMin) && !isNumeric(defaultMax) ) {
				inputMin = "";
				inputMax = "";
			}

			if ( isNumeric(inputMin) || isNumeric(inputMax) ) {
				if (isNumeric(inputMin)) { inputMin = inputMin * multiply; } else { inputMin = defaultMin; }
				if (isNumeric(inputMax)) { inputMax = inputMax * multiply; } else { inputMax = defaultMax; }
			} else {
				inputMin = "";
				inputMax = "";
			}

			if ((criterionID == 10)|| (criterionID == 95)) {
			    if ((inputMin != "") && (inputMax != "")) {
			       
			        openDoorOpeningExplain()
			    }
			}
            
            
			inputString = criterionID + "|" + inputMin + "|" + inputMax + "|2";
		}
		return inputString;
	}

	function isNumeric( txt ) {
		if ( txt.length != 0 ) {
			if ( isNaN(parseFloat(txt)) )
				return false;
			else
				return true;
		} else {
			return false;
		}
	}

	function removeSpaces(string) {
		var tstring = "";
		string = '' + string;
		splitstring = string.split(" ");
		for(i = 0; i < splitstring.length; i++)
		tstring += splitstring[i];
		return tstring;
	}

	function removeCriterion(criterionID,formID,value) {
		showResult("REMOVE",criterionID);
		showForm(formID,value,"",""); 
	}

	function showForm( criterionID, measurement, manufacturerID, carID ) {
		var url="CarMeasurementForm.asp?i=" + Math.random() + "&criterionID=" + criterionID + "&measurement=" + measurement + "&manufacturerID=" + manufacturerID + "&carID=" + carID
		xmlHttpForm=GetXmlHttpObject(formChanged)
		xmlHttpForm.open("GET", url , true)
		xmlHttpForm.send(null)
	}

	function showResult(mode, Query) {

	    var d = new Date();

	  //  alert( d.getTime());
	    var url = "CarMeasurementResult.asp?i=" + Math.random() + "&m=" + mode + "&q=" + Query+ "&cache=" + d.getTime();
	    xmlHttpResult = GetXmlHttpObject(resultChanged)
	    xmlHttpResult.open("GET", url, true)
	    xmlHttpResult.send(null)
	}
	function addNewPresetSearch(Name, SearchID) {

	    // alert("PresetSearchAdd.asp?Name=" + Name + "&SearchID=" + SearchID);
	    var url = "PresetSearchAdd.asp?Name=" + Name + "&SearchID=" + SearchID;
	    xmlHttpResult = GetXmlHttpObject(resultChanged);
	    xmlHttpResult.open("GET", url, true);
	    xmlHttpResult.send(null);
	    window.location = "carmeasurement.asp";

	}
	function deletePresetSearch(SearchID) {

        var d=new Date();

      //  alert("PresetSearchDelete.asp?SearchID=" + SearchID + "&cache=" + d.getTime());
	   var url = "PresetSearchDelete.asp?SearchID=" + SearchID+"&cache="+d.getTime();
	    xmlHttpResult = GetXmlHttpObject(resultChanged);
	    xmlHttpResult.open("GET", url, true);
	    xmlHttpResult.send(null);
	    window.location = "carmeasurement.asp";

	
	} 
	
	
	function showPresetSearchResult(mode, Query,SearchID) {

	    var url = "CarMeasurementResult.asp?i=" + Math.random() + "&m=" + mode + "&q=" + Query + "&PresetSearchID=" + SearchID
	    xmlHttpResult = GetXmlHttpObject(resultChanged)
	    xmlHttpResult.open("GET", url, true)
	    xmlHttpResult.send(null)
	} 
	
	function formChanged() { 
		if (xmlHttpForm.readyState==4 || xmlHttpForm.readyState=="complete") {	 
			document.getElementById("carguide_middle").innerHTML=xmlHttpForm.responseText 
		} 
	}

	function resultChanged() { 
		if (xmlHttpResult.readyState==4 || xmlHttpResult.readyState=="complete") {	 
			document.getElementById("carguide_right").innerHTML=xmlHttpResult.responseText 
		} 
	}
	
	function GetXmlHttpObject(handler) { 
	
		var objXmlHttp=null
	
		if (navigator.userAgent.indexOf("Opera")>=0) {
			alert("This example doesn't work in Opera") 
			return 
		}
		
		if (navigator.userAgent.indexOf("MSIE")>=0) { 
		
			var strName="Msxml2.XMLHTTP"
			if (navigator.appVersion.indexOf("MSIE 5.5")>=0) {
				strName="Microsoft.XMLHTTP"
			} 
			try { 
				objXmlHttp=new ActiveXObject(strName)
				objXmlHttp.onreadystatechange=handler 
				return objXmlHttp
			} 
			catch(e) { 
				alert("Error. Scripting for ActiveX might be disabled") 
				return 
			} 
		} 
		if (navigator.userAgent.indexOf("Mozilla")>=0) {
			objXmlHttp=new XMLHttpRequest()
			objXmlHttp.onload=handler
			objXmlHttp.onerror=handler 
			return objXmlHttp
		}
	}
