var global_town = "";

function getHTTPObject(exec)
{
	var xmlhttp = false;

	/*@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;
		}
	}
	if (xmlhttp)
	{
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					//req_json=eval('(' + xmlhttp.responseText + ')');
					eval(exec + "('" + xmlhttp.responseText + "')");
				}
			}
		}
	}
	return xmlhttp;
}
function parseAnswer(data)
{
	req_json=eval('(' + data + ')');
	
	objSel=document.getElementById("towns_list");
	objSel.remove(0);
	objSel.style.color="#4F4F4F";
	addOption(objSel,"All","All");
	for (var item in req_json){
		addOption(objSel,req_json[item][0],req_json[item][0]);
	}
}
function addOption(objSel,text,value)
{
	var optNew = document.createElement('option');
	optNew.text = text;
	optNew.value = value;
	if ( global_town == text )
		optNew.selected=true;
	try {
		objSel.add(optNew,null);
	}
	catch(ex) {
		objSel.add(optNew);
	}
}
function getTowns(state)
{
	if ( state )
	{
		state=state.replace(" ","_");
		
		objSel=document.getElementById("towns_list");
		while (objSel.length) objSel.remove(0);
		objSel.style.color="#999";
		addOption(objSel,"Loading...",null);
		
		if ( document.getElementById("state_blank") )
			document.getElementById("state_list").remove(0);
		
		var xmlhttp = null;
		xmlhttp = getHTTPObject("parseAnswer"); 
		xmlhttp.open("GET", "/"+state+"/towns/",true); 
		xmlhttp.send(null);
	}
	else
		document.getElementById("towns_list").innerHTML="";
}
function subMethode()
{
	if ( document.getElementById("state_blank") )
		alert("Select a state.");
	else
	{
		state=document.getElementById("state_list");
		towns=document.getElementById("towns_list");
		state=state.options[state.selectedIndex].value.replace(" ","_");  
		towns=towns.options[towns.selectedIndex].value.replace(" ","_");
		
		if ( towns == "All" )
			document.location.href="/" + state;
		else
			document.location.href="/" + state + "/" + towns;
	}
	return false;
}