var url = "/engine/text_browse/getData.cfm"; // The server-side script

function clearState()
	{
	// clear out the state select
	for (i = document.myDestinationSelectSearch.state.length; i >= 0; i--)
		{
		document.myDestinationSelectSearch.state.options[i] = null;
		}
	var NewOption		= new Option("Loading...","", false, false );
	document.myDestinationSelectSearch.state.options[0] = NewOption;
	};

function clearCity()
	{
	// clear out the state select
	for (i = document.myDestinationSelectSearch.city.length; i >= 0; i--)
		{
		document.myDestinationSelectSearch.city.options[i] = null;
		}
	var NewOption		= new Option("Loading...","", false, false );
	document.myDestinationSelectSearch.city.options[0] = NewOption;
	};

function handleStateHttpResponse()
	{
	if (http1.readyState == 4)
		{
		var xmlDocument = http1.responseXML;
		var myDataLength = xmlDocument.getElementsByTagName('sc').length - 1;
		for (i = 0; i <= myDataLength; i++)
			{
			var statecode	= xmlDocument.getElementsByTagName('sc').item(i).firstChild.data;
	      	var state		= xmlDocument.getElementsByTagName('st').item(i).firstChild.data;
			var NewOption		= new Option( state,statecode, false, false );
			document.myDestinationSelectSearch.state.options[i] = NewOption;
			}
		// Change the selection of the state drop down
		if(xmlDocument.getElementsByTagName('statecode').length == 2)
			{
			document.myDestinationSelectSearch.state.selectedIndex = 1;
			}
		else
			{
			document.myDestinationSelectSearch.state.selectedIndex = 0;
			}
		}		
	};
	
function handleCityHttpResponse()
	{
	if (http2.readyState == 4)
		{
		var xmlDocument = http2.responseXML;
		var myDataLength = xmlDocument.getElementsByTagName('cc').length - 1;
		
		for (i = 0; i <= myDataLength; i++)
			{
			var citycode	= xmlDocument.getElementsByTagName('cc').item(i).firstChild.data;
	      	var city		= xmlDocument.getElementsByTagName('ci').item(i).firstChild.data;
			var NewOption	= new Option( city,citycode, false, false );
			document.myDestinationSelectSearch.city.options[i] = NewOption;
			}
		// Change the selection of the city drop down
		if(xmlDocument.getElementsByTagName('cc').length == 2)
			{
			document.myDestinationSelectSearch.city.selectedIndex = 1;
			}
		else
			{
			document.myDestinationSelectSearch.city.selectedIndex = 0;
			}
		
		}
	};
	
function updateCityAndState()
	{
	//alert("yo");
	var CountryID = document.myDestinationSelectSearch.country.options[document.myDestinationSelectSearch.country.selectedIndex].value;
	// clear out the state select
	clearState();
	// clear out the city select
	clearCity();
	// Deal with the states
	http1.open("GET", url + "?task=getstate&countryid=" + escape(CountryID), true);
	http1.onreadystatechange = handleStateHttpResponse;
	http1.send(null);
	// Deal with the cities
	http2.open("GET", url + "?task=getcity&countryid=" + escape(CountryID), true);
	http2.onreadystatechange = handleCityHttpResponse;
	http2.send(null);
	};
	
function updateCityByState()
	{
	var CountryID = document.myDestinationSelectSearch.country.options[document.myDestinationSelectSearch.country.selectedIndex].value;
	var StateID = document.myDestinationSelectSearch.state.options[document.myDestinationSelectSearch.state.selectedIndex].value;
	// clear out the city select
	clearCity();
	// Deal with the states
	http2.open("GET", url + "?task=getCityByState&countryid=" + escape(CountryID) + "&stateid=" + escape(StateID), true);
	http2.onreadystatechange = handleCityHttpResponse;
	http2.send(null);
	};
	
function getHTTPObject()
	{
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
		xmlhttp = false;
		}
		}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
		{
		try
			{
			xmlhttp = new XMLHttpRequest();
			}
		catch (e)
			{
			xmlhttp = false;
			}
		}
	return xmlhttp;
	};
	
var http1 = getHTTPObject(); // We create the HTTP Object
var http2 = getHTTPObject(); // We create the HTTP Object