var xmlHttp;
var regionId;

function createXMLHttpRequest () {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest ();
// alert("!");
	}
	else {
		xmlHttp = false;
	}
}

function processRegion (region_id) {
	createXMLHttpRequest ();

	
	regionId = region_id;
	
// 	alert (regionId);
	
	xmlHttp.onreadystatechange = readyStateHandler;
	xmlHttp.open ("GET", "cities.xml?a=1", true);
	xmlHttp.send (null);
}

function readyStateHandler () {
// 	alert ("1");
// 	alert (xmlHttp.readyState);
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
// 			alert (xmlHttp.responseText);
			populateCities ();
		}
	}
}

function populateCities () {
	var xmlDoc = xmlHttp.responseXML;
	
	var citiesElement = document.getElementById ("cities");
	if (citiesElement.hasChildNodes ()) {
		while (citiesElement.childNodes.length > 0) {
			citiesElement.removeChild (citiesElement.childNodes[0]);
		}
	}
	var cities = xmlDoc.getElementsByTagName ("enum");
	var optionElement = document.createElement("option");
	var optionValue = document.createTextNode ("[Choose]");
	optionElement.appendChild(optionValue);
	citiesElement.appendChild(optionElement);
// 	alert (cities.length);
	var count = 0;
	for (var i=0; i<cities.length; i++) {
		city = cities[i];
// 		alert (city.getAttribute("region"));
		if (city.getAttribute("region") == regionId) {
// 			alert (city.childNodes[0].childNodes[0].nodeValue);
			var optionElement = document.createElement("option");
			var optionValue = document.createTextNode (city.childNodes[0].childNodes[0].nodeValue);
			if (count % 2 == 0) {
				optionElement.className = "colored";
			}
			optionElement.setAttribute("value", city.childNodes[0].childNodes[0].nodeValue);
			optionElement.appendChild(optionValue);
			citiesElement.appendChild(optionElement);
			count = count + 1;
// 			alert (citiesElement.options[count].value);
		}
	}
// 	alert (regionId);
/*	var enumIterator = xmlDoc.evaluate("//enum[contains(@region, '"+regionId+"')]/string");
	alert ("1");
	var thisNode = enumIterator.iterateNext();
	while (thisNode) {
	    for_alert = thisNode.textContent;
		thisNode = enumIterator.iterateNext();
	}
	alert (for_alert);*/
}