var AjaxServerPageName = "AjaxServer.aspx";
var strSelectName = "";
var strSelectValue = "";

//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 country combo box selection changes
function fnSelectOnChange(obj, objform) 
{
	strSelectName = obj.name;
	strSelectValue = obj.options[obj.selectedIndex].value;
	
	if(strSelectName != "PartType_Generation") return;
	
	// URL to get states for a given country
	var requestUrl = AjaxServerPageName + "?SelectName=" + encodeURIComponent(strSelectName) + "&SelectValue=" + encodeURIComponent(strSelectValue);
	//alert(requestUrl);
	
	CreateXmlHttp();

	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,  true);
		XmlHttp.send(null);		
	}
}

function HandleResponse()
{
	if(XmlHttp.readyState == 4)
	{
	
	    //alert(XmlHttp.responseText);
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			fnClearAndSetListItems(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 fnClearAndSetListItems(objselectNodes)
{
	var selectNodes = objselectNodes.getElementsByTagName('setSelect');
	//alert(selectNodes.length);
	
	var textValue; 
	var optionItem;
	var objSelect;
	var intI;
	var intJ;
	var intK;
	var listItemsNodes;
	
	for (intI = 0; intI < selectNodes.length; intI++)
	{
   		textValue = GetInnerText(selectNodes[intI]);
		//alert(textValue);

		listItemsNodes = selectNodes[intI].getElementsByTagName('listItems');
		//alert(listItemsNodes.length);
				
		//Clears the combo box contents.
		objSelect = document.getElementById(textValue);
	    for (intJ = objSelect.options.length-1; intJ >-1; intJ--)
	    {
		    objSelect.options[intJ] = null;
	    }

        optionItem = new Option("Select", "",  false, false);
		objSelect.options[objSelect.length] = optionItem;

		
		for (intK = 0; intK < listItemsNodes.length; intK++)
	    {   
		    optionItem = new Option( listItemsNodes[intK].getAttribute("Name"), listItemsNodes[intK].getAttribute("PartTypeID"),  false, false);
		    objSelect.options[objSelect.length] = optionItem;
	    }
	}
}

//Returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}







