var xmlhttp=null;
var countrySelect = null;
var divisionSelect = null;
var locationSelect = null;
var radiusSelect = null;
var formDisabling = false;
var disabledBlock = null;
var activeFormIsSearch = null;
var invisibleLocationInput = false;

// note this uses jQuery
// initialise hide of country/div submit buttons when using AJAX
$(document).ready(function(){
	// hide "Change" buttons for location if Javascript active
	$("#submitcountry").css("display","none");
	$("#submitdivision").css("display","none");
});

function locSelectCountry(isSearch, isLeftNav)
{
	// isSearch == boolean; if true, allow "All" values on selects
	//	 if false (e.g. registration), must enter loc info to max available
	//	 level of geodata detail
	
	if (isLeftNav)
	{
		// left-nav search should always use these ids
		countrySelect = document.getElementById("gninputcountry");
		divisionSelect = document.getElementById("gninputdivision");
		locationSelect = document.getElementById("gninputcity");
	}
	else
	{
		// all loc selects except left nav search should use these ids
		countrySelect = document.getElementById("inputcountry");
		divisionSelect = document.getElementById("inputdivision");
		locationSelect = document.getElementById("inputcity");
		if (isSearch && document.getElementById("locSelectRadius") != null)
		{
			radiusSelect = document.getElementById("locSelectRadius");
		}
	}
	activeFormIsSearch = isSearch;
	if (locationSelect !=null)
	{
		if (locationSelect.type == "hidden") invisibleLocationInput = true;
	}
	if (!isSearch)
	{
		formDisabling = true;
		disabledBlock = document.getElementById("formDisabledDuringLocSelect");
	}
	else formDisabling = false;
	
	// show please-wait message in division select
	clearLocList(divisionSelect);
	appendToSelect(divisionSelect, 0, document.createTextNode(pleaseWaitMesg));
	divisionSelect.disabled = true;
	
	if (window.XMLHttpRequest)
	{// code for IE7, Firefox, Mozilla, etc.
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE5, IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (xmlhttp!=null)
	{
		var url = "/xml/divisions.xml?country=" + countrySelect.options[countrySelect.selectedIndex].value;
		if (isSearch) url = url + "&isSearch=1";
		xmlhttp.onreadystatechange=onCountryResponse;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else
	{
		// revert form back to non-JS version
	}
}
function onCountryResponse()
{
	// only if req shows "loaded"
	if (xmlhttp.readyState == 4)
	{
		// only if "OK"
		if (xmlhttp.status == 200)
		{
			clearLocList(divisionSelect);
			buildLocList(divisionSelect, true);
		}
		else
		{
			// error retrieving XML data
			// ... revert form back to non-JS version
		}
	}
}

// empty relevant loc select list content
function clearLocList(thisSelect)
{
	while (thisSelect.length > 0)
	{
		thisSelect.remove(0);
	}
}

// add item to select element the less
// elegant, but compatible way.
function appendToSelect(select, value, content)
{
	var opt;
	opt = document.createElement("option");
	opt.value = value;
	opt.appendChild(content);
	select.appendChild(opt);
}

// fill relevant loc select list with items from
// the current XML document
function buildLocList(thisSelect, wasCountry)
{
	clearLocList(thisSelect);
	
	// if country or division changed, radius field is disabled
	// until a location is selected
	if (radiusSelect != null) radiusSelect.className = "form-disabled";
	
	// loop through <location> elements, and add each
	// to the next loc select list
	var items = xmlhttp.responseXML.getElementsByTagName("location");
	if (items.length > 0)
	{
		if (activeFormIsSearch)
		{
			// "all" option at top of list
			if (wasCountry)
			{
				appendToSelect(divisionSelect, 0, document.createTextNode(allDivisionsMesg));
			}
			else if (!invisibleLocationInput)
			{
				appendToSelect(locationSelect, 0, document.createTextNode(allLocationsMesg));
			}
		}
		else
		{
			// "please select location" option at top of list
			if (wasCountry)
			{
				appendToSelect(divisionSelect, 0, document.createTextNode(pleaseSelectLocationMesg));
			}
		}
		for (var i = 0; i < items.length; i++)
		{
			appendToSelect(thisSelect, items[i].getElementsByTagName("value")[0].firstChild.nodeValue, document.createTextNode(items[i].getElementsByTagName("name")[0].firstChild.nodeValue));
		}
		thisSelect.disabled = false;
		
		if (wasCountry && !invisibleLocationInput)
		{
			// set location select message
			clearLocList(locationSelect);
			if (activeFormIsSearch)
			{
				appendToSelect(locationSelect, 0, document.createTextNode(allLocationsMesg));
			}
			else
			{
				appendToSelect(locationSelect, 0, document.createTextNode(pleaseSelectDivisionMesg));
			}
			locationSelect.disabled = true;
			// disable rest of form
			if (radiusSelect != null) radiusSelect.className = "form-disabled";
			if (formDisabling) disableRestOfForm(true);
		}
		else
		{
			// enable rest of form
			if (formDisabling) disableRestOfForm(false);
		}
	}
	else
	{
		var disableRestOfFormNow = false;
		if (activeFormIsSearch)
		{
			if (!invisibleLocationInput)
			{
				clearLocList(locationSelect);
				appendToSelect(locationSelect, 0, document.createTextNode(allLocationsMesg));
			}
			if (wasCountry)
			{
				appendToSelect(divisionSelect, 0, document.createTextNode(allDivisionsMesg));
			}
		}
		else
		{
			if (!invisibleLocationInput)
			{
				clearLocList(locationSelect);
				appendToSelect(locationSelect, 0, document.createTextNode(noLocInfoAvailMesg));
			}
			if (wasCountry)
			{
				appendToSelect(divisionSelect, 0, document.createTextNode(noLocInfoAvailMesg));
				locationSelect.disabled = true;
				if (countrySelect.options[countrySelect.selectedIndex].value == 0)
				{
					// "Please select a location" option was selected
					disableRestOfFormNow = true;
				}
			}
			else if (divisionSelect.options[divisionSelect.selectedIndex].value == 0)
			{
				// "Please select a location" option was selected
				disableRestOfFormNow = true;
			}
		}
		thisSelect.disabled = true;

		// enable rest of form (unless "Please select a location" option was selected)
		if (formDisabling) disableRestOfForm(disableRestOfFormNow);
	}
}
function locSelectDivision(isSearch, isLeftNav)
{
	if (isLeftNav)
	{
		// left-nav search should always use these ids
		countrySelect = document.getElementById("gninputcountry");
		divisionSelect = document.getElementById("gninputdivision");
		locationSelect = document.getElementById("gninputcity");
	}
	else
	{
		// non-left-nav search should always use these ids
		countrySelect = document.getElementById("inputcountry");
		divisionSelect = document.getElementById("inputdivision");
		locationSelect = document.getElementById("inputcity");
		if (isSearch && document.getElementById("locSelectRadius") != null)
		{
			radiusSelect = document.getElementById("locSelectRadius");
		}
	}
	activeFormIsSearch = isSearch;
	if (locationSelect !=null)
	{
		if (locationSelect.type == "hidden") invisibleLocationInput = true;
	}
	if (!isSearch)
	{
		formDisabling = true;
		disabledBlock = document.getElementById("formDisabledDuringLocSelect");
	}
	else formDisabling = false;
	
	if (!invisibleLocationInput)
	{
		// show please-wait message in location select
		locationSelect.disabled = true;
		clearLocList(locationSelect);
		appendToSelect(locationSelect, 0, document.createTextNode(pleaseWaitMesg));
	}
	
	if (window.XMLHttpRequest)
	{// code for IE7, Firefox, Mozilla, etc.
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE5, IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (xmlhttp!=null)
	{
		var url = "/xml/locations.xml?division=" + divisionSelect.options[divisionSelect.selectedIndex].value;
		if (isSearch) url = url + "&isSearch=1";
		xmlhttp.onreadystatechange=onDivisionResponse;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else
	{
		// revert form back to non-JS version
	}
}
function onDivisionResponse()
{
	// only if req shows "loaded"
	if (xmlhttp.readyState == 4)
	{
		// only if "OK"
		if (xmlhttp.status == 200 && !invisibleLocationInput)
		{
			clearLocList(locationSelect);
			buildLocList(locationSelect, false);
		}
		else
		{
			// error retrieving XML data
			// ... revert form back to non-JS version
		}
	}
}
function disableRestOfForm(doDisable)
{
	// enable or disable rest of form
	var blockClass = "form-disabled";
	if (!doDisable) blockClass = "form-enabled";
	disabledBlock.className = blockClass;
	for (var i = 0; i < disabledBlock.getElementsByTagName("input").length; i++)
	{
		disabledBlock.getElementsByTagName("input")[i].disabled = doDisable;
	}
	for (var i = 0; i < disabledBlock.getElementsByTagName("textarea").length; i++)
	{
		disabledBlock.getElementsByTagName("textarea")[i].disabled = doDisable;
	}
	for (var i = 0; i < disabledBlock.getElementsByTagName("select").length; i++)
	{
		disabledBlock.getElementsByTagName("select")[i].disabled = doDisable;
	}
}
function locSelectLocation(isSearch, isLeftNav)
{
	if (!isLeftNav && isSearch && document.getElementById("locSelectRadius") != null)
	{
		locationSelect = document.getElementById("inputcity");
		radiusSelect = document.getElementById("locSelectRadius");
		if (locationSelect.selectedIndex == 0)
		{
			// "All cities/towns" selected
			radiusSelect.className = "form-disabled";
		}
		else
		{
			// a specific location selected
			radiusSelect.className = "form-enabled";
		}
	}
}
