﻿

var http_request_cities = false;

function RequestCities(url, id) {

	http_request_cities = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request_cities = new XMLHttpRequest();
		if (http_request_cities.overrideMimeType) {
			http_request_cities.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request_cities = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request_cities = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { }
		}
	}

	if (!http_request_cities) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request_cities.onreadystatechange = alertCities;
	http_request_cities.open('GET', url + '?state=' + id, true);
	http_request_cities.send(null);
}

function alertCities() {
	if (http_request_cities.readyState == 4) {
		if (http_request_cities.status == 200) {
			ClearAndSetCityListItems(http_request_cities.responseXML.documentElement);
		}
		else {
			alert('There was a problem with the request.  Unable to load cities.');
		}
	}
}

//Clears the contents of state combo box and adds the states of currently selected country
function ClearAndSetCityListItems(nodeState) {
	var listCity = document.getElementById("gd_city");

	//Clears the city combo box contents.
	for (var count = listCity.options.length - 1; count > -1; count--) {
		listCity.options[count] = null;
	}

	var nodeCities = nodeState.getElementsByTagName('city');
	var textValue;
	var optionItem;
	var textID;

	//Add new city list to the city combo box.
	for (var count = 0; count < nodeCities.length; count++) {
		textID = GetInnerText(nodeCities[count].attributes.getNamedItem("id"));
		textValue = GetInnerText(nodeCities[count]);
		optionItem = new Option(textValue, textID, false, false);
		listCity.options[listCity.length] = optionItem;
	}
}

//Returns the node text value 
function GetInnerText(node) {
	return (node.textContent || node.innerText || node.text);
}



function GDState_OnChange(url) {
	RequestCities(url, document.getElementById("gd_state").value);
}



function GDGetLocation() {
	var divLocation = document.getElementById("spanGDLocation");
	var inputCity = document.getElementById("gd_browse_city");
	var inputState = document.getElementById("gd_browse_state");

	if (divLocation != null) {
		if (google.loader.ClientLocation && google.loader.ClientLocation.address.country_code == "US" && google.loader.ClientLocation.address.region) {
			divLocation.innerHTML = google.loader.ClientLocation.address.city + ', ' + google.loader.ClientLocation.address.region;
			if (inputCity != null) inputCity.value = google.loader.ClientLocation.address.city;
			if (inputState != null) inputState.value = google.loader.ClientLocation.address.region;
		}
		else {
			divLocation.innerHTML = "Unknown Location";
		}
	}
}

function GDSubmitCategorySearch(catid) {
	var inputCat = document.getElementById("gd_browse_category");
	if (inputCat != null) inputCat.value = catid;
	document.formMain.submit();
}

function GDSubmitForm() {
	var inputCat = document.getElementById("gd_browse_category");
	if (inputCat != null) inputCat.value = '';
	document.formMain.submit();
}
