
//Global XMLHTTP Request object
var XmlHttp;
var currentCntrl = 0
var firstgo = 0
//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{	firstgo = 0
	//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 CountryListOnChange() 
{   
	
	currentCntrl = 0
	
	var countryList = document.getElementById("ctl00_ContentPlaceHolder1_ddlBillCountry");
	////Getting the selected country from country combo box.
	var selectedCountry = countryList.options[countryList.selectedIndex].value;
	
	//// URL to get states for a given country
	//alert(selectedCountry);
	var requestUrl = AjaxServerPageName + "?SelectedCountry=" + encodeURIComponent(selectedCountry) ;
	
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;
		//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);		
	}
	
	//document.getElementById("LeftControls1_tdSate").innerHTML= document.getElementById("LeftControls1_tdSate").innerHTML;
	
}



//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)
		{			
			ClearAndSetStateListItems(XmlHttp.responseXML.documentElement);
			//ClearAndSetStateListItems1(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 ClearAndSetStateListItems(countryNode)
{
	
	if(currentCntrl < 1)
	{
	    stateList = document.getElementById("ctl00_ContentPlaceHolder1_ddlBillState");
	}
			
	//Clears the state combo box contents.
	
	if(stateList.options.length)
	{	
		stateList.options.length = 0;
	}

	var stateNodes = countryNode.getElementsByTagName('item');
	// alert(countryNode.getElementsByValue('item'));
	
	var textValue; 
	var optionItem;
	var val;
	
	//var option2 = new Option('All State' ,0 ,  false, false);
	//document.getElementById("_ctl0_ContentPlaceHolder1_ddlBillState").options[0] = option2;
	//Add new states list to the state combo box.
	for (var count = 0; count < stateNodes.length; count++)
	{
   		textValue = GetInnerText(stateNodes[count]);
   		var value1 = stateNodes[count].getAttribute('value');
   		optionItem = new Option(textValue ,value1 ,  false, false);
		stateList.options[stateList.length] = optionItem;
	}
	
 document.getElementById("ctl00_ContentPlaceHolder1_billstateid").value =  document.getElementById("ctl00_ContentPlaceHolder1_ddlBillState").options[document.getElementById("ctl00_ContentPlaceHolder1_ddlBillState").selectedIndex].value;
  document.getElementById("ctl00_ContentPlaceHolder1_billstate").value =  document.getElementById("ctl00_ContentPlaceHolder1_ddlBillState").options[document.getElementById("ctl00_ContentPlaceHolder1_ddlBillState").selectedIndex].text;
}

//Returns the node text value 
function GetInnerText (node)
{	
	 return (node.textContent || node.innerText || node.text) ;
	
}


//Gets called when country combo box selection changes
function CountryListOnChange1() 
{   
	
	currentCntrl = 0
	
	var countryList = document.getElementById("ctl00_ContentPlaceHolder1_ddlShipCountry");
	////Getting the selected country from country combo box.
	var selectedCountry = countryList.options[countryList.selectedIndex].value;
	
	//// URL to get states for a given country
	
	var requestUrl = AjaxServerPageName + "?SelectedCountry=" + encodeURIComponent(selectedCountry) ;
	
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse1;
		//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 HandleResponse1()
{	
	// 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)
		{			
			ClearAndSetStateListItems1(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 ClearAndSetStateListItems1(countryNode)
{
	
	if(currentCntrl < 1)
	{
	    stateList = document.getElementById("ctl00_ContentPlaceHolder1_ddlShipState");
	}
			
	//Clears the state combo box contents.
	
	if(stateList.options.length)
	{	
		stateList.options.length = 0;
	}

	var stateNodes = countryNode.getElementsByTagName('item');
	// alert(countryNode.getElementsByValue('item'));
	
	var textValue; 
	var optionItem;
	var val;
	
	//var option2 = new Option('Select State' ,0 ,  false, false);
	//document.getElementById("_ctl0_ContentPlaceHolder1_ddlShipState").options[0] = option2;
	//Add new states list to the state combo box.
	for (var count = 0; count < stateNodes.length; count++)
	{
   		textValue = GetInnerText1(stateNodes[count]);
   		var value1 = stateNodes[count].getAttribute('value');
   		optionItem = new Option(textValue ,value1 ,  false, false);
		stateList.options[stateList.length] = optionItem;
	}
	
  document.getElementById("ctl00_ContentPlaceHolder1_shipstateid").value =  document.getElementById("ctl00_ContentPlaceHolder1_ddlShipState").options[document.getElementById("ctl00_ContentPlaceHolder1_ddlShipState").selectedIndex].value;
  document.getElementById("ctl00_ContentPlaceHolder1_shipstate").value =  document.getElementById("ctl00_ContentPlaceHolder1_ddlShipState").options[document.getElementById("ctl00_ContentPlaceHolder1_ddlShipState").selectedIndex].text;
}

//Returns the node text value 
function GetInnerText1 (node)
{	
	 return (node.textContent || node.innerText || node.text) ;
	
}


function StateListOnChange ()
{
  document.getElementById("ctl00_ContentPlaceHolder1_billstateid").value =  document.getElementById("ctl00_ContentPlaceHolder1_ddlBillState").options[document.getElementById("ctl00_ContentPlaceHolder1_ddlBillState").selectedIndex].value;
  document.getElementById("ctl00_ContentPlaceHolder1_billstate").value =  document.getElementById("ctl00_ContentPlaceHolder1_ddlBillState").options[document.getElementById("ctl00_ContentPlaceHolder1_ddlBillState").selectedIndex].text;
  
  //document.getElementById("_ctl0_ContentPlaceHolder1_shipstateid").value =  document.getElementById("_ctl0_ContentPlaceHolder1_ddlShipState").options[document.getElementById("_ctl0_ContentPlaceHolder1_ddlBillState").selectedIndex].value;
  //document.getElementById("_ctl0_ContentPlaceHolder1_shipstate").value =  document.getElementById("_ctl0_ContentPlaceHolder1_ddlBillState").options[document.getElementById("_ctl0_ContentPlaceHolder1_ddlBillState").selectedIndex].text;
}
   
function StateListOnChange1 ()
{
  document.getElementById("ctl00_ContentPlaceHolder1_shipstateid").value =  document.getElementById("ctl00_ContentPlaceHolder1_ddlShipState").options[document.getElementById("ctl00_ContentPlaceHolder1_ddlShipState").selectedIndex].value;
  document.getElementById("ctl00_ContentPlaceHolder1_shipstate").value =  document.getElementById("ctl00_ContentPlaceHolder1_ddlShipState").options[document.getElementById("ctl00_ContentPlaceHolder1_ddlShipState").selectedIndex].text;
}
 
