/*===========================================================================*/
// 20090316 AK city id for 'selected'
// 20090316 AK new code from ajax_test.js, reformatted and modified for generic, added semicolons
/*===========================================================================*/

/*===========================================================================*/
var xmlHttp

//The purpose of the function is to solve the problem of creating different XMLHTTP objects for different browsers.
function GetXmlHttpObject() {
	try { // Opera 8.0+, Firefox, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) { // Internet Explorer Browsers
		try { //IE 6 +
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) { //IE 5.5
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	};
	return xmlHttp;
};
/*===========================================================================*/
var objDestinationControl = null;
var selectedCityID = null;

function regionCities(){

	if (xmlHttp.readyState==4) {

		//lets get a grip on the PHP-generated XML document using some DOM
		var xmlDoc = xmlHttp.responseXML.documentElement;
	
		//check if any elements exist..ie if the XML document returned an <empty> element then we assume none exist
		var empty = xmlDoc.getElementsByTagName('empty');
		if (empty.length) {
	
			//remove all options by setting the options array's length to 0 
			objDestinationControl.options.length = 0;
			objDestinationControl.options[0] = new Option('select a region first', '', false, true);
		} else {
	
			//remove all options by setting the options array's length to 0 
			objDestinationControl.options.length = 0
			
			//declare some variables
			var cityId = null;
			var cityName = null;
	
			//how many rows (i.e. XML elements) did our XML document return?
			var length = xmlDoc.getElementsByTagName('city').length;

			if (length > 0) {
				objDestinationControl.options[0] = new Option('select a city', '', false, false);
			} else {
				objDestinationControl.options[0] = new Option('select a region first', '', false, true);
			};
			// populate the select menu with new options, using the Option() constructor on each option. Loop through based on how many rows we have 
			for (var i=0; i < length; i++) {
				cityId = xmlDoc.getElementsByTagName("id")[i].childNodes[0].nodeValue;
				cityName = xmlDoc.getElementsByTagName("name")[i].childNodes[0].nodeValue;

				if (cityId==selectedCityID) {
					optionSelected = true;
				} else {
					optionSelected = false;
				};
				// note + 1 because of unselected option prompt above
				objDestinationControl.options[i + 1] = new Option(cityName, cityId, false, optionSelected);
			};
		}; //else 
	}; //if (xmlHttp.readyState==4) 
};
/*===========================================================================*/
function ajaxRegionCities(objControl, regionID, cityID) {
	// stuff these 2 into globals so the handler can get at them (see note for handler call below)
	objDestinationControl = objControl;
	if (cityID.length > 0) {
		selectedCityID = cityID;
	};

	//create object via function call
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	};
	// create querystring to our PHP page - pass the 'id' of the selected region
	// selectedCityID not currently used in this call, but if we want to add the selected to the xml...

// NOTE: we are doing URLEncode for the moment (and the 'reciprocal' in ajax_get_region_cities.php) because we don't have a numeric
// cityID right now, and the city names being used for 'selected' may have spaces and other junk in them
// ALSO NOTE: this is NOT the native escape
	var url="/ajax_get_region_cities.php?regionid=" + regionID + "&cityid=" + URLEncode(cityID); 

	//make request, specify response function and send request
	xmlHttp.open("GET", url, true); 
	xmlHttp.onreadystatechange = regionCities;  // NOTE!!! using parenthesis or parameters will break this as 'not implemented'
	xmlHttp.send(null);
};
/*===========================================================================*/

