

function checktowncache() {
	// quit if this function has already been called
	if (arguments.callee.done) return;
	
	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	if (document.getElementById("towncache").value > 0) {
		update_search("townforce", document.getElementById("towncache").value);
	} else {
		if (document.getElementById("countycache").value > 0) {
			update_search("county", document.getElementById("countycache").value);
		} else {
			if (document.getElementById("regioncache").value > 0) {
				update_search("region", document.getElementById("regioncache").value);
			}
		}
	}
}



var request = new getXMLHTTPRequest();
function update_search(area, area_id) {
	rando = new Date().getTime();
	switch(area) {
	case "region":
		search_updating();
		request.open("GET", "/ajax/towns/r/?region="+area_id+"&p="+rando, true);
		request.onreadystatechange = update_region_response;
		request.send(null);
		break;
	case "county":
		if (area_id == "") {
			update_search("region", document.getElementById("sregion").options[document.getElementById("sregion").selectedIndex].value);
		} else {
			search_updating();
			request.open("GET", "/ajax/towns/c/?county="+area_id+"&p="+rando, true);
			request.onreadystatechange = update_county_response;
			request.send(null);
		}
		break;
	case "town":
	case "townforce":
		if ((area == "townforce" || document.getElementById("scounty").selectedIndex == 0) && document.getElementById("stown").value != "" && document.getElementById("stown").value != "new") {
			search_updating();
			request.open("GET", "/ajax/towns/c/?town="+area_id+"&p="+rando, true);
			request.onreadystatechange = update_county_response;
			request.send(null);
		}
		if (document.getElementById("forcelocation")) {
			if (document.getElementById("stown").value == "new") {
				document.getElementById("addtown").style.display = "inline";
			} else {
				document.getElementById("addtown").style.display = "none";
			}
		}
		break;
	}
	
}

function update_region_response() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			update_counties(request.responseXML.getElementsByTagName("county"));
			update_towns(request.responseXML.getElementsByTagName("town"));
			search_ready();
		}
	}
	
}

function update_county_response() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			update_counties(request.responseXML.getElementsByTagName("county"));
			update_towns(request.responseXML.getElementsByTagName("town"));
			
			setregion(request.responseXML.getElementsByTagName("results")[0].getAttribute("regionid"));
			setcounty(request.responseXML.getElementsByTagName("results")[0].getAttribute("countyid"));
			if (request.responseXML.getElementsByTagName("results")[0].getAttribute("townid")) {
				settown(request.responseXML.getElementsByTagName("results")[0].getAttribute("townid"));
			}
			search_ready();
		}
	}
	
}

function setregion(regionid) {
	for (i = 0; i < document.getElementById("sregion").options.length; i++) {
		if (document.getElementById("sregion").options[i].value == regionid) {
			document.getElementById("sregion").selectedIndex = i;
		}
	}
}

function setcounty(countyid) {
	for (i = 0; i < document.getElementById("scounty").options.length; i++) {
		if (document.getElementById("scounty").options[i].value == countyid) {
			document.getElementById("scounty").selectedIndex = i;
		}
	}
}

function settown(townid) {
	for (i = 0; i < document.getElementById("stown").options.length; i++) {
		if (document.getElementById("stown").options[i].value == townid) {
			document.getElementById("stown").selectedIndex = i;
		}
	}
}

function update_counties(counties) {
	for (i = document.getElementById("scounty").options.length-1; i >= 0; i--) {
		document.getElementById("scounty").options[i] = null;
	}
	
	if (!document.getElementById("forcelocation") || document.getElementById("forcelocation").value == 0) {
		document.getElementById("scounty").options[0] = new Option("No Preference", "");
	} else {
		document.getElementById("scounty").options[0] = new Option("Select Below", "");
		document.getElementById("scounty").options[0].disabled = true;
		document.getElementById("scounty").options[1] = new Option("------------", "");
		document.getElementById("scounty").options[1].disabled = true;
	}

	existing_options_count = document.getElementById("scounty").options.length;
	for (i = 0; i < counties.length; i++) {
		//alert(counties[i].getAttribute("countyid")+", "+counties[i].childNodes[0].nodeValue);
		document.getElementById("scounty").options[existing_options_count+i] = new Option(counties[i].childNodes[0].nodeValue, counties[i].getAttribute("countyid"));
		
	}
}

function update_towns(towns) {
	for (i = document.getElementById("stown").options.length-1; i >= 0; i--) {
		document.getElementById("stown").options[i] = null;
	}
	
	if (!document.getElementById("forcelocation") || document.getElementById("forcelocation").value == 0) {
		document.getElementById("stown").options[0] = new Option("No Preference", "");
	} else {
		if (towns.length > 0) {
			document.getElementById("stown").options[0] = new Option("Select Below", "");
			document.getElementById("stown").options[0].disabled = true;
			document.getElementById("stown").options[1] = new Option("------------", "");
			document.getElementById("stown").options[1].disabled = true;
		} else {
			document.getElementById("stown").options[0] = new Option("Specify Region/County", "");
			document.getElementById("stown").options[0].disabled = true;
		}
		document.getElementById("addtown").style.display = "none";
	}
	
	existing_options_count = document.getElementById("stown").options.length;
	for (i = 0; i < towns.length; i++) {
		document.getElementById("stown").options[existing_options_count+i] = new Option(towns[i].childNodes[0].nodeValue, towns[i].getAttribute("townid"));
	}
	
	if (document.getElementById("forcelocation") && document.getElementById("forcelocation").value == 1 && towns.length > 0) {
		document.getElementById("stown").options[document.getElementById("stown").length] = new Option("------------", "");
		document.getElementById("stown").options[document.getElementById("stown").length -1].disabled = true;
		document.getElementById("stown").options[document.getElementById("stown").length] = new Option("Specify New Town", "new");
	}
}

function search_updating() {
	document.getElementById("sregion").disabled = true;
	document.getElementById("scounty").disabled = true;
	document.getElementById("stown").disabled = true;
}

function search_ready() {
	document.getElementById("sregion").disabled = false;
	document.getElementById("scounty").disabled = false;
	document.getElementById("stown").disabled = false;

}

function search_check_load() {
	if (document.getElementById("scounty").selectedIndex > 0) {
		update_search('county', document.getElementById("scounty").options[document.getElementById("scounty").selectedIndex].value);
	}
}

function update_region_from_flash(regionID) {
	//alert(regionID); return;
	
	for (i = 0; i < document.getElementById("sregion").options.length; i++) {
		if (document.getElementById("sregion").options[i].value == regionID) {
			document.getElementById("sregion").selectedIndex = i;
			update_search('region', regionID);
		}
	}
	
}

function cacheloc() {
	document.getElementById("regioncache").value = document.getElementById("sregion").options[document.getElementById("sregion").selectedIndex].value;
	document.getElementById("countycache").value = document.getElementById("scounty").options[document.getElementById("scounty").selectedIndex].value;
	document.getElementById("towncache").value = document.getElementById("stown").options[document.getElementById("stown").selectedIndex].value;
}
	

var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
function flashmap_DoFSCommand(command, args) {
  //var myFlashObj = InternetExplorer ? getElementById("flashmap") : document.flashmap;
  if (command == "map_update") {
  	update_region_from_flash(args);
  }
}

if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
  document.write("<script language=\'VBScript\'>\n");
  document.write("on error resume next \n");
  document.write("Sub flashmap_FSCommand(ByVal command, ByVal args)\n");
  document.write(" call flashmap_DoFSCommand(command, args)\n");
  document.write("end sub\n");
  document.write("</scr"+"ipt>\n");
}


/* for Mozilla */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", checktowncache, false);
}
/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script defer src=\'/alt/ie_towns_onload.js\'><"+"/script>");
/*@end @*/

addLoadEvent(checktowncache);
