
//Global XMLHTTP Request object
var XmlHttpCountry;


function Load()
{    
    var ddlCountry = parentId + "CountryState1_ddlCountry";
   
    var ddlState = parentId + "CountryState1_ddlState";
    var txtOther = parentId + "CountryState1_txtOthers";
    var trState = "trState";

    var countryList = document.getElementById(ddlCountry);
    //Getting the selected country from country combo box.
    var selectedCountry = countryList.options[countryList.selectedIndex].value;
    // URL to get states for a given country
    if (selectedCountry == "US")
    {
        if (document.getElementById(trState) == null)
        {
            document.getElementById(ddlState).style.display = "block";
	        document.getElementById(txtOther).style.display = "none";
	    }
	    else
	       document.getElementById(trState).style.display = "block"; 
    }
    else
    {
        if (document.getElementById(trState) == null)
        {
            document.getElementById(ddlState).style.display = "none";
	        document.getElementById(txtOther).style.display = "block";
	    }
	    else
	       document.getElementById(trState).style.display = "none"; 
    }
}









//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttpCountry()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttpCountry = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpCountry = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpCountry = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttpCountry && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpCountry = new XMLHttpRequest();
	}
}

//Gets called when country combo box selection changes
function CountryListOnChange() 
{   
    var ddlCountry = parentId + "CountryState1_ddlCountry";    
    var ddlState = parentId + "CountryState1_ddlState";
    var txtOther = parentId + "CountryState1_txtOthers";
    var trState = "trState";
    
	var countryList = document.getElementById(ddlCountry);
	//Getting the selected country from country combo box.
	var selectedCountry = countryList.options[countryList.selectedIndex].value;
	// URL to get states for a given country
	if (selectedCountry == "US")
	{
	    if (document.getElementById(trState) == null)
	    {
	        document.getElementById(ddlState).style.display = "block";
		    document.getElementById(txtOther).style.display = "none";
		}
		else
		   document.getElementById(trState).style.display = "block"; 
	    var requestUrl = AjaxServerPageName + "?lookupType=State&SelectedCountry=" + encodeURIComponent(selectedCountry);
	    CreateXmlHttpCountry();
    		
	    // If browser supports XMLHTTPRequest object
	    if(XmlHttpCountry)
	    {
		    //Setting the event handler for the response
		    XmlHttpCountry.onreadystatechange = HandleResponseCountry;
    		
		    //Initializes the request object with GET (METHOD of posting), 
		    //Request URL and sets the request as asynchronous.
		    XmlHttpCountry.open("GET", requestUrl,  true);
    		
		    //Sends the request to server
		    XmlHttpCountry.send(null);		
	    }
	}
	else
	{
	    if (document.getElementById(trState) == null)
	    {
	        document.getElementById(ddlState).style.display = "none";
		    document.getElementById(txtOther).style.display = "block";
		}
		else
		   document.getElementById(trState).style.display = "none"; 
	}
}

//Called when response comes back from server for Country
function HandleResponseCountry()
{
	// To make sure receiving response data from server is completed
	if(XmlHttpCountry.readyState == 4)
	{
	    // To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpCountry.status == 200)
		{	
		    ClearAndSetStateListItems(XmlHttpCountry.responseXML);
		}
		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 ClearAndSetStateListItems(responseXML)
{
    var ddlState = parentId + "CountryState1_ddlState";
    
	//Clears the state combo box contents.
    var stateList = document.getElementById(ddlState);
	for (var count = stateList.options.length-1; count >-1; count--)
	{
		stateList.options[count] = null;
	}
	//End Clear Boxes
	
	var statePkID = responseXML.getElementsByTagName('StateId');
	var stateNodes = responseXML.getElementsByTagName('StateName');
	var textValue; 
	var PkID;
	var optionItem;
	//Add new states list to the state combo box.
	
	for (var count = 0; count < stateNodes.length; count++)
	{
	    PkID=GetInnerTextCountry(statePkID[count]);
	    textValue = GetInnerTextCountry(stateNodes[count]);
   		if(textValue==BlankSelection)
   		{
		optionItem = new Option(textValue, PkID,  true, true);
		}
		else
		{
		optionItem = new Option(textValue, PkID,  false, false);
		}
		stateList.options[stateList.length] = optionItem;
	}
}

//Returns the node text value 
function GetInnerTextCountry (node)
{
	 return (node.textContent || node.innerText || node.text || node.value || node.innerHTML || node.firstChild.nodeValue) ;
}

