var obj;



function fillSelect(host, obj, fk_value, form){



	this.obj = obj;



	resetSelect(obj);



	// check if object exisits.

	if(typeof obj != 'object') return false;



	if(obj.name == "state"){

		resetSelect(form.region);

		url  = host + '/location.php?country=' + fk_value;

		if(fk_value != '') loadResult(url,'');

	} else if (obj.name == "region"){

		url  = host + '/location.php?state=' + fk_value;

		if(fk_value != '') loadResult(url,'');

	}

}



function resetSelect(obj){

	while (obj.options.length>1) {

		deleteIndex=obj.options.length-1;

		obj.options[deleteIndex]=null;

	}

}



function loadXMLDoc(url) {

	req=null;

	if (typeof XMLHttpRequest!="undefined") {

		req=new XMLHttpRequest();

		if (req) {

			req.onreadystatechange = processReqChange;

			req.open("GET", url, true);

			req.send(null);

		}

	}

	if (!req&&typeof ActiveXObject!="undefined") {

		try {

			req=new ActiveXObject("Msxml2.XMLHTTP");

		}

		catch(e) {

			try {

				req=new ActiveXObject("Microsoft.XMLHTTP");

			}

			catch(e2) {

				try {

					req=new ActiveXObject("Msxml2.XMLHTTP.4.0");

				}

				catch(e3) {

					req=null;

				}

			}

		}

		if (req) {

			req.onreadystatechange = processReqChange;

			req.open("GET", url, true);

			req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

			req.send();

		}

	}

}



function processReqChange() {

	// only if req shows "complete"

	if (req.readyState == 4) {

		// only if "OK"

		if (req.status == 200) {

			// ...processing statements go here



			//response  = req.responseXML.documentElement;

			response=req.responseText;



			if(response) {

				loadResult('', response.split("~"));

			}

		} else {

			alert("There was a problem retrieving the XML data:\n" + req.statusText);

		}

	}

}



function loadResult(url, result){

	if (result != ''){

		// Response mode

		for(i=0; i < result.length; i++){

			this.obj.options[this.obj.options.length]  = new Option(result[i],result[i]);

		}

	} else if(url != '') {



		// Input mode

		return (loadXMLDoc(url));

	}

}

