
//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();
	}
}

//Gets called when Company combo box selection changes
function CompanyListOnChange() {
    
var CompanyList = document.getElementById("ctl00_MainPlaceHolder_ddlPriceBrand"); 


    //Getting the selected Company from Company combo box.
    var selectedCompany = CompanyList.options[CompanyList.selectedIndex].value;
	
	// URL to get Models for a given Company
	var requestUrl = "Ajax_GetModelValues.aspx?selectedCompany=" + encodeURIComponent(selectedCompany);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if (XmlHttp) {
	    //Initializes the request object with GET (METHOD of posting),
	    //Request URL and sets the request as asynchronous.
	    
	    XmlHttp.open("GET", requestUrl, true);

	    //Setting the event handler for the response
	    XmlHttp.onreadystatechange = HandleResponse;

	    //Sends the request to server
	    XmlHttp.send(null);
	}
}


//Called when response comes back from server
function HandleResponse()
{
	// 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.responseXML.documentElement);
		}
		else
		{
			//alert("There was a problem retrieving data from the server." );
		}
	}
}

//Clears the contents of state combo box and adds the states of currently selected country
function ClearAndSetModelListItems(CompanyNode)
{
    var ModelList = document.getElementById("ctl00_MainPlaceHolder_ddlPriceModel");
  
    //Clears the state Model box contents.
	for (var count = ModelList.options.length-1; count >-1; count--)
	{
		ModelList.options[count] = null;
    }
    
    var optionItem;

    if (CompanyNode != null) {
        
        var ModelNodes = CompanyNode.getElementsByTagName('ModelName');
        var PidNodes = CompanyNode.getElementsByTagName('PID');
        var textValue;
        var pidValue;

        //Add new Model list to the Model combo box.
        for (var count = 0; count < ModelNodes.length; count++) {
            textValue = GetInnerText(ModelNodes[count]);
            pidValue = GetInnerText(PidNodes[count]);
            optionItem = new Option(textValue, pidValue, false, false);
            ModelList.options[ModelList.length] = optionItem;
        }
    }
    else {
        optionItem = new Option('----', '', false, false);
        ModelList.options[ModelList.length] = optionItem;
    }
}

//Returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}










