var http = false;
var SelectToFill = false;



function CreateRequestObject () {
	http = false;
	if (window.XMLHttpRequest) {
		http = new XMLHttpRequest();
		if (http.overrideMimeType) http.overrideMimeType ('text/xml');
	} else if (window.ActiveXObject) {
		try {
			http = new ActiveXObject ("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http = new ActiveXObject ("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http) { alert ("Browser is AJAX incompatible.\nWill function in limited mode"); return false; }
	return true;
}


function SendRequest (url, handleFunction) {
	http.onreadystatechange = handleFunction;
	http.open ('GET', url, true);
	http.send (null);
}



function ClearSelectOptions (forElement) {
	for (var count = forElement.options.length-1; count >-1; count--)
		forElement.options[count] = null;
	forElement.length = 0;
	try {
		forElement.add (new Option (lang_option_not_specified, 0), null);
	} catch (e) {
		forElement.add (new Option (lang_option_not_specified, 0));
	}
	
}


function FillRegions (oElem, oTarget, root, ClearCities) { 
	ClearSelectOptions (ClearCities);
	ClearSelectOptions (oTarget);
	var CountryValue = oElem.options[oElem.selectedIndex].value;
	if (CountryValue == '0') return;
	var url = root+"jx/regions.php?country=" + CountryValue;
	//alert (url);
	SelectToFill = oTarget;
	CreateRequestObject();
	SendRequest (url, FillOptionsHandle);
		
}

function FillCities(oElem, oTarget, root) { 
	ClearSelectOptions (oTarget);
	var RegionValue = oElem.options[oElem.selectedIndex].value;
	if (RegionValue == '0') return;
	var url = root+"jx/cities.php?region=" + RegionValue;
	SelectToFill = oTarget;
	CreateRequestObject();
	SendRequest (url, FillOptionsHandle);
		
}

function FillOptionsHandle () {
	if (http.readyState == 4) {

		root_node = http.responseXML.documentElement;
		var options = root_node.getElementsByTagName("en");
		
		//alert (options.length);
		var textValue, optionValue; 
		var optionItem;
		for (var count=0; count<options.length; count++) {
			optionValue = options[count].childNodes[0].firstChild.nodeValue;
			textValue =   options[count].childNodes[1].firstChild.nodeValue;
			optionItem = new Option (textValue, optionValue);
			try {SelectToFill.add (optionItem, null);}
			catch (e) { SelectToFill.add (optionItem);}
		}
		
	}
}
