var xmlHttp;
var type;
var countryID;
var target;
var target2;

function updateCountyTown(typesource,targetsource,targetsource2) {
	createXMLHttpRequest();
	createXMLHttpRequest2();
	type = typesource;
	target = targetsource;
	target2 = targetsource2;
	getSelectedId(type);
	
	if( countryID == "all" ){	
	 	document.getElementById("county").style.display = 'none';
		document.getElementById("town").style.display = 'none';
		return;
	}

	var url = "ScriptLibrary/XMLCountyByCountry.asp?ts=" + new Date().getTime();
	var queryStr = "countryID=" + countryID;
	xmlHttp.onreadystatechange=handleStateChange;
	xmlHttp.open("POST", url);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlHttp.send(queryStr);
	
	var url2 = "ScriptLibrary/XMLTownsByCountry.asp?ts=" + new Date().getTime();
	var queryStr2 = "countryID=" + countryID;
	xmlHttp2.onreadystatechange=handleStateChange2;
	xmlHttp2.open("POST", url2);
	xmlHttp2.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlHttp2.send(queryStr2);

}

function refreshList(typesource,targetsource) {
	createXMLHttpRequest();
	type = typesource;
	target = targetsource;
	getSelectedId(type);
	
	if( countryID == "all" ){	
		updateCountyTown('country','county','town')
		return;
	}

	var url = "ScriptLibrary/ajaxFunctions2.asp?ts=" + new Date().getTime();
	var queryStr = "countryID=" + countryID;
	xmlHttp.onreadystatechange=handleStateChange3;
	xmlHttp.open("POST", url);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlHttp.send(queryStr);
}

function handleStateChange() {
	
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
				updateList();
		}
	}
}
 
function handleStateChange2() {
	
	if (xmlHttp2.readyState == 4) {
		if (xmlHttp2.status == 200) {
				updateList2();
		}
	}
}

function handleStateChange3() {
	
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
				updateList3();
		}
	}
}

function updateList() {
	 //alert("type: " + type);
	 var select = document.getElementById(target);
	 select.options.length = 0;
	 //alert(xmlHttp.responseXML.xml);
	 var options = xmlHttp.responseXML.getElementsByTagName("county");
	 for (var i = 0, n = options.length; i < n; i++) {
		 select.appendChild(createElementWithValue(options[i]));
	 }
	 select.style.display = 'inline';
	 
}

function updateList2() {
	 //alert("type: " + type);
	 var select = document.getElementById(target2);
	 select.options.length = 0;
	 //alert(xmlHttp.responseXML.xml);
	 var options = xmlHttp2.responseXML.getElementsByTagName("town");
	 for (var i = 0, n = options.length; i < n; i++) {
		 select.appendChild(createElementWithValue(options[i]));
	 }
	 select.style.display = 'inline';
	 
}

function updateList3() {
	 //alert("type: " + type);
	 var select = document.getElementById(target);
	 select.options.length = 0;
	 //alert(xmlHttp.responseXML.xml);
	 var options = xmlHttp.responseXML.getElementsByTagName("town");
	 for (var i = 0, n = options.length; i < n; i++) {
		 select.appendChild(createElementWithValue(options[i]));
	 }
	 select.style.display = 'inline';
	 
}

function createElementWithValue(text) {
	 var element = document.createElement("option");
	 element.setAttribute("value", text.getAttribute("id"));
	 var text = document.createTextNode(text.firstChild.nodeValue);
	 element.appendChild(text);
	 return element;
}

function getSelectedId(elementId) {
	 //alert("elementId: " + elementId);
	 //var selectedId = null;
	 var options = document.getElementById(elementId).childNodes;
	 var option = null;
	 for (var i = 0, n = options.length; i < n; i++) {
		 option = options[i];
		 if (option.selected) {
			 countryID = option.value;
		 }
	 }
 }

function createXMLHttpRequest() {
	if(window.XMLHttpRequest) { 
			xmlHttp = new XMLHttpRequest();
		} else if (window.ActiveXObject) { 
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
				}
			}
		}
}

function createXMLHttpRequest2() {
	if(window.XMLHttpRequest) { 
			xmlHttp2 = new XMLHttpRequest();
		} else if (window.ActiveXObject) { 
			try {
				xmlHttp2 = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlHttp2 = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
				}
			}
		}
}

