var countries = ['US'];

function stateDefined(countryCode)
{
	for (i = 0; i < countries.length; i++)
		if (countries[i] == countryCode)
			return true;
	
	return false;
}

function getStates(countryCode)
{
	if (stateDefined(countryCode))
	{
		// use ajax here to fetch country code from server
		MakeAjaxRequest('../inc/getStates.php?countryCode='+countryCode, populateStates);
	}
	else
		showTextBox();
}

function showTextBox()
{
	$('stateSpan').innerHTML = "<input type='text' name='state' id='state' value='' />";
}

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 MakeAjaxRequest(url, callbackFunction)
{
	//alert(url);
	
	xmlHttpObj = GetXmlHttpObject();
	
	if (!xmlHttpObj)
	{
		alert('hi');
		return false;
	}
	
	xmlHttpObj.onreadystatechange = function() 
	{
		 if (xmlHttpObj.readyState  == 4)
         {
         	if (xmlHttpObj.status  == 200)
         	{
            	callbackFunction(xmlHttpObj.responseText);
			}
			//else
			//	showTextBox();			
         }
	};
	
	xmlHttpObj.open('GET',url,true);
	xmlHttpObj.send(null);
}

function populateStates(text)
{
	$('stateSpan').innerHTML = text;
}

function $(element)
{
	return document.getElementById(element);
}