
// declare a global  XMLHTTP Request object
var XmlHttpObj; var XmlHttpObj1;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}
function CreateXmlHttpObj1()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj1 = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj1 = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj1 = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj1 && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj1 = new XMLHttpRequest();
	}
}
function ClearLists()
{

	    var countryList = document.getElementById('cars_model');
		// clear the list 
		for (var count = countryList.options.length-1; count >0; count--)
		{
			countryList.options[count] = null;
		}
	    var countryList = document.getElementById('cars_make');
		// clear the list 
		for (var count = countryList.options.length-1; count >0; count--)
		{
			countryList.options[count] = null;
		}
	
}

// called from onChange or onClick event of the dropdown list
function startListOnChange() 
{
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    requestUrl = "xml_data_provider.php?actionn=get_table_data&field="+startListOnChange.arguments[0];
    filtername = "";
    filter = "";
    for (var i = 1; i < startListOnChange.arguments.length; i++)
    {
	    startList = startListOnChange.arguments[i];
	    selectedObj = startList.options[startList.selectedIndex].value;
	    filtername = filtername + startList.name + ",";
		filter = filter + selectedObj + ",";
    }
    filtername = filtername.substring(0,filtername.length-1);
    filter = filter.substring(0,filter.length-1);
    requestUrl1 = "&filtername=" + filtername  + "&filter=" + filter; 
    requestUrl = requestUrl + requestUrl1;
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = StateChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);	
			
	}
	return 1;
}


// called from onChange or onClick event of the dropdown list
function calc_price() 
{
    
	var make = document.getElementById('cars_make').value;
	var model = document.getElementById('cars_model').value;
	var year = document.getElementById('cars_year').value;
	var mileage = document.getElementById('mileage').value;
    // url of page that will send xml data back to client browser
    var requestUrl;
    requestUrl = "xml_data_provider.php?actionn=get_price";
    requestUrl1 = "&make="+make+"&model="+model+"&year="+year+"&mileage="+mileage;
    requestUrl = requestUrl + requestUrl1;
	CreateXmlHttpObj1();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj1)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj1.onreadystatechange = StateChangeHandler1;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj1.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj1.send(null);		
	}
}

// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function StateChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	//alert('readystate: ' + XmlHttpObj.readyState);
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{	
			var response = XmlHttpObj.responseXML.documentElement;
			var actionn = response.getAttribute("act");
			if (actionn=='get_table_data')
				PopulateList(response);
			else if (actionn=='get_price')
				PopulatePrice(response);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}
function StateChangeHandler1()
{
	// state ==4 indicates receiving response data from server is completed
	//alert('readystate: ' + XmlHttpObj.readyState);
	if(XmlHttpObj1.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj1.status == 200)
		{	
			var response = XmlHttpObj1.responseXML.documentElement;
			var actionn = response.getAttribute("act");
			if (actionn=='get_table_data')
				PopulateList(response);
			else if (actionn=='get_price')
				PopulatePrice(response);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj1.status);
		}
	}
}
// populate the contents of the dropdown list
function PopulateList(Node)
{
	var endfield = Node.getElementsByTagName('field')[0].getAttribute("name");
    var List = document.getElementById(endfield);
	// clear the list 
	for (var count = List.options.length-1; count >0; count--)
	{
		List.options[count] = null;
	}

	var Nodes = Node.getElementsByTagName('selection');
	var textValue; 
	var optionItem;
	
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < Nodes.length; count++)
	{
   		textValue = GetInnerText(Nodes[count]);
   		valValue=Nodes[count].getAttribute("make"); 
   		if (valValue=='') valValue=textValue;
		optionItem = new Option( valValue, textValue, false, false);
		List.options[List.length] = optionItem;
	}
}

// populate the contents of the price divs
function PopulatePrice(Node)
{
	var Nodes = Node.getElementsByTagName('price');
	// populate the divs with data from the xml doc
	for (var count = 0; count < Nodes.length; count++)
	{
		textValue = GetInnerText(Nodes[count]);
		divname='price'+ (count+1);
		pricediv = document.getElementById(divname);
   		pricediv.innerHTML = textValue;
   		divname='nameprice'+ (count+1);
   		pricedivname = document.getElementById(divname);
   		var is_hidden=textValue.indexOf('your car is not covered');
		if (is_hidden!=-1)
		{
			pricediv.className='nocoverage';
			pricedivname.style.display='none';
		}
		else 
		{
			pricediv.className='quotered';
			pricedivname.style.display='';
		}
	}
}
// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}