
// declare a global  XMLHTTP Request object
var XmlHttpObj;

// 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();
	}
}


// called from onChange or onClick event of the continent dropdown list
function ManufacturerListOnChange() 
{
    var manufacturerList = document.getElementById("manufacturerList");
    
    // get selected continent from dropdown list
    var selectedManufacturer = manufacturerList.options[manufacturerList.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    
    // use the following line if using php
     requestUrl = "http://www.p11dorganiser.co.uk/co2/xml_data_provider.php" + "?filter=" + encodeURIComponent(selectedManufacturer);
    
	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 = CarStateChangeHandler;
		
		// 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);		
	}
}

function ModelListOnChange() 
{   
    var modelList = document.getElementById("modelList");
    var selectedModel = modelList.options[modelList.selectedIndex].value;
	var manufacturerList = document.getElementById("manufacturerList");
	var selectedManufacturer = manufacturerList.options[manufacturerList.selectedIndex].value;
    var requestUrl;
	
    requestUrl = "http://www.p11dorganiser.co.uk/co2/xml_data_provider.php" + "?model=" + encodeURIComponent(selectedModel) + "&filter=" + encodeURIComponent(selectedManufacturer);
	CreateXmlHttpObj();
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = VariationStateChangeHandler;
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);		
	}
}

function VariationListOnChange() 
{   
    var variationList = document.getElementById("variationList");
    var selectedVariation = variationList.options[variationList.selectedIndex].value;
	
	var modelList = document.getElementById("modelList");
    var selectedModel = modelList.options[modelList.selectedIndex].value;
	
   var requestUrl;
    requestUrl = "http://www.p11dorganiser.co.uk/co2/xml_data_provider.php" + "?model=" + encodeURIComponent(selectedModel) + "&description=" + encodeURIComponent(selectedVariation);
	CreateXmlHttpObj();
	
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = TransmissionStateChangeHandler;
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);		
	} 
} 

function TransmissionListOnChange() 
{   
    var variationList = document.getElementById("variationList");
    var selectedVariation = variationList.options[variationList.selectedIndex].value;
	
	var modelList = document.getElementById("modelList");
    var selectedModel = modelList.options[modelList.selectedIndex].value;
	
	var transmissionList = document.getElementById("transmissionList");
	var selectedTransmission = transmissionList.options[transmissionList.selectedIndex].value; 
	
    var requestUrl;
    requestUrl = "http://www.p11dorganiser.co.uk/co2/xml_data_provider.php" + "?model=" + encodeURIComponent(selectedModel) + "&description=" + encodeURIComponent(selectedVariation) + "&transmission=" 
										 + encodeURIComponent(selectedTransmission);
	CreateXmlHttpObj();
  	
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = CapacityStateChangeHandler;
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);		
	} 
} 


function CapacityListOnChange() 
{   
    var variationList = document.getElementById("variationList");
    var selectedVariation = variationList.options[variationList.selectedIndex].value;
	
	var modelList = document.getElementById("modelList");
    var selectedModel = modelList.options[modelList.selectedIndex].value;
	
	var transmissionList = document.getElementById("transmissionList");
	var selectedTransmission = transmissionList.options[transmissionList.selectedIndex].value; 
	
	var capacityList = document.getElementById("capacityList");
	var selectedCapacity = capacityList.options[capacityList.selectedIndex].value; 
	
    var requestUrl;
    requestUrl = "http://www.p11dorganiser.co.uk/co2/xml_data_provider.php" + "?model=" + encodeURIComponent(selectedModel) + "&description=" + encodeURIComponent(selectedVariation) + "&transmission=" 
										 + encodeURIComponent(selectedTransmission) + "&engCap=" + encodeURIComponent(selectedCapacity);
	CreateXmlHttpObj();
  	
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = FuelStateChangeHandler;
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);		
	} 
} 


function FuelListOnChange() 
{   
    var variationList = document.getElementById("variationList");
    var selectedVariation = variationList.options[variationList.selectedIndex].value;
	
	var modelList = document.getElementById("modelList");
    var selectedModel = modelList.options[modelList.selectedIndex].value;
	
	var transmissionList = document.getElementById("transmissionList");
	var selectedTransmission = transmissionList.options[transmissionList.selectedIndex].value; 
	
	var capacityList = document.getElementById("capacityList");
	var selectedCapacity = capacityList.options[capacityList.selectedIndex].value; 
	
	var fuelList = document.getElementById("fuelList");
	var selectedFuel = fuelList.options[fuelList.selectedIndex].value;
	
    var requestUrl;
    requestUrl = "http://www.p11dorganiser.co.uk/co2/xml_data_provider.php" + "?model=" + encodeURIComponent(selectedModel) + "&description=" + encodeURIComponent(selectedVariation) + "&transmission=" 
										 + encodeURIComponent(selectedTransmission) + "&engCap=" + encodeURIComponent(selectedCapacity) + "&fuel=" + encodeURIComponent(selectedFuel);
										 
	//alert(requestUrl);
	CreateXmlHttpObj();
  	
	if(XmlHttpObj)
	{   
		XmlHttpObj.onreadystatechange = CO2StateChangeHandler;
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);		
	} 
}

function InitManufacturerList()
{
	var requestUrl;
	requestUrl = "http://www.p11dorganiser.co.uk/co2/xml_data_provider.php";
	CreateXmlHttpObj();
	if(XmlHttpObj)
	{
       	XmlHttpObj.onreadystatechange = ManStateChangeHandler;
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.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 CarStateChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateModelList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

function ManStateChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateManList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}	
}

function VariationStateChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateVarList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}	
}


function TransmissionStateChangeHandler()
{
 
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateTranList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}	
} 

function CapacityStateChangeHandler()
{
	if(XmlHttpObj.readyState == 4)
	{
		if(XmlHttpObj.status == 200)
		{			
			PopulateCapList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}	
} 

function FuelStateChangeHandler()
{
	if(XmlHttpObj.readyState == 4)
	{
		if(XmlHttpObj.status == 200)
		{			
			PopulateFuelList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}	
} 

function CO2StateChangeHandler()
{
	if(XmlHttpObj.readyState == 4)
	{
		if(XmlHttpObj.status == 200)
		{			
			PopulateCO2(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}	
} 



// populate the contents of the country dropdown list
function PopulateModelList(modelNode)
{
    var modelList = document.getElementById("modelList");
	// clear the country list 
	for (var count = modelList.options.length-1; count >-1; count--)
	{
		modelList.options[count] = null;
	}

	var modelNodes = modelNode.getElementsByTagName('model');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < modelNodes.length; count++)
	{
   		textValue = GetInnerText(modelNodes[count]);
		idValue = modelNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		modelList.options[modelList.length] = optionItem;
	}
	ModelListOnChange();
}


function PopulateManList(manNode)
{
	
    var manList = document.getElementById("manufacturerList");
	// clear the country list 
	for (var count = manList.options.length-1; count >-1; count--)
	{
		manList.options[count] = null;
	}
	
	var manNodes = manNode.getElementsByTagName('manufacturer');
	
	var idValue;
	var textValue; 
	var optionItem;
	
	optionItem = new Option( "--Select A Manufacturer--", "Default",  false, true);
	manList.options[manList.length] = optionItem;
	
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < manNodes.length; count++)
	{
   		textValue = GetInnerText(manNodes[count]);
		idValue = manNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		manList.options[manList.length] = optionItem;
	}
	
}

function PopulateVarList(varNode)
{
	
    var varList = document.getElementById("variationList");
	
	for (var count = varList.options.length-1; count >-1; count--)
	{
		varList.options[count] = null;
	}
	
	var varNodes = varNode.getElementsByTagName('variation');
	
	var idValue;
	var textValue; 
	var optionItem;
	
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < varNodes.length; count++)
	{
   		textValue = GetInnerText(varNodes[count]);
		idValue = varNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		varList.options[varList.length] = optionItem;
	}
	
	VariationListOnChange();
}


function PopulateTranList(tranNode)
{
    var tranList = document.getElementById("transmissionList");
	
	for (var count = tranList.options.length-1; count >-1; count--)
	{
		tranList.options[count] = null;
	}
	
	var tranNodes = tranNode.getElementsByTagName('transmission');
	
	var idValue;
	var textValue; 
	var optionItem;
	
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < tranNodes.length; count++)
	{
   		textValue = GetInnerText(tranNodes[count]);
		idValue = tranNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		tranList.options[tranList.length] = optionItem;
	}
	
	TransmissionListOnChange();
}


function PopulateCapList(capNode)
{
	var capList = document.getElementById("capacityList");
	for (var count = capList.options.length-1; count >-1; count--)
	{
		capList.options[count] = null;
	}
	var capNodes = capNode.getElementsByTagName('engine_capacity');
	var idValue;
	var textValue; 
	var optionItem;
	
	for (var count = 0; count < capNodes.length; count++)
	{
   		textValue = GetInnerText(capNodes[count]);
		idValue = capNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		capList.options[capList.length] = optionItem;
	}
	
	CapacityListOnChange();
}



function PopulateFuelList(fuelNode)
{   
	var fuelList = document.getElementById("fuelList");
	for (var count = fuelList.options.length-1; count >-1; count--)
	{
		fuelList.options[count] = null;
	}
	var fuelNodes = fuelNode.getElementsByTagName('fuel');
	var idValue;
	var textValue; 
	var optionItem;
	
	for (var count = 0; count < fuelNodes.length; count++)
	{
   		textValue = GetInnerText(fuelNodes[count]);
		idValue = fuelNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		fuelList.options[fuelList.length] = optionItem;
	}
	
	FuelListOnChange() 
	
}

function PopulateCO2(CO2Node)
{   
	var co2List = document.getElementById("CO2");
	var co2Nodes = CO2Node.getElementsByTagName('CO2');
	var textValue; 

	for (var count = 0; count < co2Nodes.length; count++)
	{    
   		textValue = GetInnerText(co2Nodes[count]);
		co2List.innerHTML = textValue;
	} 
}


// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}









