//###############################################################################################################
//###############################################################################################################
//###############################################################################################################
var XmlHttpObj;
//###############################################################################################################
//###############################################################################################################
//###############################################################################################################
function CreateXmlHttpObj()
{
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}
//###############################################################################################################
//###############################################################################################################
//###############################################################################################################
var StateFieldName;
var SelectedID;

function CountryIDOnChange(cName,sName,sID) 
{

    //var CountryID = document.getElementById("CountryID");
    var CountryID = document.getElementById(cName);
    StateFieldName = sName;
    SelectedID = sID;

    var selectedContinent = CountryID.options[CountryID.selectedIndex].value;
    
    var requestUrl;
    requestUrl = "contact/contact_ajax_state.php" + "?CountryID=" + encodeURIComponent(selectedContinent);
    
	CreateXmlHttpObj();
	
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandler;
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);		
	}
}
//###############################################################################################################
//###############################################################################################################
//###############################################################################################################
function StateChangeHandler()
{
	if(XmlHttpObj.readyState == 4)
	{
		if(XmlHttpObj.status == 200)
		{			
			PopulateStateID(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}
//###############################################################################################################
//###############################################################################################################
//###############################################################################################################
function PopulateStateID(countryNode)
{
    //var StateID = document.getElementById("StateID");
	var StateID = document.getElementById(StateFieldName);

	for (var count = StateID.options.length-1; count >-1; count--)
	{
		StateID.options[count] = null;
	}

	var countryNodes = countryNode.getElementsByTagName('country');
	var idValue;
	var textValue; 
	var optionItem;


	for (var count = 0; count < countryNodes.length; count++)
	{
   		textValue = GetInnerText(countryNodes[count]);
		idValue = countryNodes[count].getAttribute("id");
		optionItem = new Option( unescape(textValue), idValue,  false, false);
		StateID.options[StateID.length] = optionItem;
		if(idValue == SelectedID)
		StateID.selectedIndex = (StateID.length-1);
	}
	//alert(SelectedID);

}
//###############################################################################################################
//###############################################################################################################
//###############################################################################################################
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}