var xmlHttp;
var xmlHttp2;

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	 {
	 // Firefox, Opera 8.0+, Safari
	 xmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {
	 //Internet Explorer
	 try
	  {
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	 catch (e)
	  {
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	 }
	return xmlHttp;
}

function Populate_States(country, state)
{ 
	state_selected = state

	//alert(country);
	xmlHttp2=GetXmlHttpObject()
	if (xmlHttp2==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	
	var url="/countries_state/get_states/"
	url=url+country

	xmlHttp2.onreadystatechange=stateChanged_Populate_States
	xmlHttp2.open("GET", url, true)		
	xmlHttp2.send(null)
}

function stateChanged_Populate_States() 
{ 
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
	 { 
		var temp = new Array();
		
		var str = xmlHttp2.responseText;
		
		//alert(state_selected);
		//alert(str);
		if(str)
		 {
			temp = str.split("|");
			if(document.getElementById('state'))
				document.getElementById('state').value = ''; // Resetting the value bcoz it retains the value if you choose us/ca

			document.getElementById('state_us_ca').options.length = 0;

			document.getElementById('state_us_ca').options[0] = new Option('Select a State',"");
			
			for(var i=1;i<=temp.length;i++)
			 {		
				//alert(temp);
				var value = new Array();
				value = temp[i-1].split("-");

				if(value[0] == state_selected)
					document.getElementById('state_us_ca').options[i] = new Option(value[1],value[0],true,1);
				else
					document.getElementById('state_us_ca').options[i] = new Option(value[1],value[0]);
			 }
		 }
	 } 
}
