
//Global XMLHTTP Request object
var XmlHttp;

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------

function MakeListOnChange(VendorProductType, VendorCategory) 
{

	var MakeList = document.getElementById("ddlMake");

	var selectedMake = MakeList.options[MakeList.selectedIndex].value;
		
	var requestUrl = "ajaxGetModels.aspx" + "?VendorProductType=" + VendorProductType + "&VendorCategory=" + VendorCategory + "&Make=" + encodeURIComponent(selectedMake);	
	CreateXmlHttp();
	
		
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleMakeResponse;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

//Called when response comes back from server
function HandleMakeResponse()
{

	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			

			ClearAndSetModelListItems(XmlHttp.responseText );
			
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

function ClearAndSetModelListItems(strIn)
{
    var ModelList = document.getElementById("ddlModel");

	for (var count = ModelList.options.length-1; count >-1; count--)
	{
		ModelList.options[count] = null;
	}

	var optionItem;
		
		document.getElementById("ModelArrow").src="images/redarrow.gif";
		document.getElementById("MakeArrow").src="images/spacer.gif";
		ModelList.disabled=false;
		optionItem = new Option('Choose Vehicle Model', '',  false, false);
		ModelList.options[ModelList.length] = optionItem;

		var addModel = strIn.split('|');		
		for(i=0; i!=addModel.length; i++)
			{
				if(addModel[i] != '') {
					optionItem = new Option(addModel[i], addModel[i],  false, false);
					ModelList.options[ModelList.length] = optionItem;
					}
			}
			
			
	//}
}


// ------------------------------------------------------------------------------------------------------------------------------------------------------------------

function ModelListOnChange() 
{

	var MakeList = document.getElementById("ddlMake");
	var ModelList = document.getElementById("ddlModel");
	var VendorProductType = document.getElementById("hidVendorProductType").value;
	var VendorCategory = document.getElementById("hidVendorCategory").value;
	
	var selectedMake = MakeList.options[MakeList.selectedIndex].value;
	var selectedModel = ModelList.options[ModelList.selectedIndex].value;
		
	var requestUrl = "ajaxGetYears.aspx" + "?VendorProductType=" + VendorProductType + "&VendorCategory=" + VendorCategory + "&Make=" + encodeURIComponent(selectedMake) + "&Model=" + encodeURIComponent(selectedModel);
	CreateXmlHttp();
	
		
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleModelResponse;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

//Called when response comes back from server
function HandleModelResponse()
{

	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means  received is OK
		if(XmlHttp.status == 200)
		{			

			ClearAndSetYearListItems(XmlHttp.responseText );
			
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

function ClearAndSetYearListItems(strIn)
{
    var YearList = document.getElementById("ddlYear");
	

	for (var count = YearList.options.length-1; count >-1; count--)
	{
		YearList.options[count] = null;
	}

	var optionItem;
		
		document.getElementById("YearArrow").src="images/redarrow.gif";
		document.getElementById("ModelArrow").src="images/spacer.gif";
		document.getElementById("MakeArrow").src="images/spacer.gif";
		YearList.disabled=false;
		optionItem = new Option('Choose Vehicle Year', '',  false, false);
		YearList.options[YearList.length] = optionItem;

		var addYear = strIn.split('|');		
		for(i=0; i!=addYear.length; i++)
			{
				if(addYear[i] != '') {
					optionItem = new Option(addYear[i], addYear[i],  false, false);
					YearList.options[YearList.length] = optionItem;
					}
			}
			
			
	//}
}





