var XmlHttp;
function CreateXmlHttp()
{
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
		    //alert(oc.value);
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}

function getRecord(selectedList,obj,descdentObj,cityobj)
{
	//alert(encodeURIComponent(obj.value));
    var url;
        
    if (selectedList == 'Country')
    {
        for (var count = cityobj.options.length-1; count >-1; count--)
	    {
		    cityobj.options[count] = null;
	    }
	    optionItem = new Option('---select---', '---select---',  false, false);
	    cityobj.options[0] = optionItem;
        url = "ASearchFetchRecord.aspx?id=1&val="+encodeURIComponent(obj.value);
    }
    else if (selectedList == 'State')
    {
        url = "ASearchFetchRecord.aspx?id=2&val="+encodeURIComponent(obj.value);
    }
    
    CreateXmlHttp();
    
    if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = function()
		{
    		HandleResponse(selectedList,descdentObj);
		}
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", url,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
	
    return false;
}

function HandleResponse(selectedList,descdentObj)
{
	// 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)
		{			
		    ClearAndSetStateListItems(XmlHttp.responseXML.documentElement,selectedList,descdentObj);
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}


function ClearAndSetStateListItems(Node,selectedList,descdentObj)
{
    
    var List = descdentObj;
	//Clears the state combo box contents.
	for (var count = List.options.length-1; count >-1; count--)
	{
		List.options[count] = null;
	}
   
    if(selectedList == 'Country')
    {
	    var Nodes = Node.getElementsByTagName('state');
	}
	else if(selectedList == 'State')
	{
	   var Nodes = Node.getElementsByTagName('city');
	}
	var textValue; 
	var optionItem;
	
	for (var count = 0; count < Nodes.length; count++)
	{
   		textValue = GetInnerText(Nodes[count]);
		optionItem = new Option( textValue, textValue,  false, false);
		List.options[List.length] = optionItem;
	}
	optionItem = new Option('---select---', '---select---',  false, false);
	List.options[0] = optionItem;
	List.selectedIndex = 0;
}


function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}









