var obj;
var field_0;
var field_1;
var field_2;
var field_loading;

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

	this.obj = obj;
	this.form = form;

	resetSelect(obj);
	
	

	// check if object exisits.
	if(typeof obj != 'object') return false;

	if (fk_value > 0) {
		this.obj.options[0] = new Option("Loading...", "");
		form.country_id.disabled=true;
		form.state_id.disabled=true;
		form.region_id.disabled=true;
	} else {
		return false;
	}

	if(obj.name == "state_id"){
		resetSelect(form.region_id);
		this.obj.oldText = "Select a Province/State";
		url  = host+'/location.php?country_id=' + fk_value;
		if(fk_value > 0) loadResult(url,'');
	}

	if(obj.name == "region_id"){
		resetSelect(form.region_id);
		this.obj.oldText = "Select a City";
		url  = host+'/location.php?state_id=' + fk_value;
		if(fk_value > 0) loadResult(url,'');
	}

	if(obj.name == "federal_id"){
		url  = host+'/location.php?country_idf=' + fk_value;
		if(fk_value > 0) loadResult(url,'');
	}

	if(obj.name == "provincial_id"){
		url  = host+'/location.php?country_idp=' + fk_value;
		if(fk_value > 0) loadResult(url,'');
	}

	if(obj.name == "municipal_id"){
		url  = host+'/location.php?country_idm=' + fk_value;
		if(fk_value > 0) loadResult(url,'');
	}
	
}

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

	this.obj = obj;

	resetSelect(obj);

	// check if object exisits.
	if(typeof obj != 'object') return false;

	if (obj.name == "search_state") {
		resetSelect(form.search_region);
		url = host + '/location.php?country_id=' + fk_value;
		if (fk_value > 0) loadResult(url, '');
	}

	if (obj.name == "search_region") {
		url = host + '/location.php?state_id=' + fk_value;
		if (fk_value > 0) loadResult(url, '');
	}

}

function resetSelect(obj){
	while (obj.options.length>1) {
		deleteIndex=obj.options.length-1;
		obj.options[deleteIndex]=null;
	}
}

function loadXMLDoc(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			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;
			if(response) {
				var result = new Array();
				for(i=0; i < response.getElementsByTagName('id').length; i++){
					result[i] = {'id': response.getElementsByTagName('id')[i].firstChild.data, 'name': response.getElementsByTagName('name')[i].firstChild.data};
				}
				loadResult('',result);
				unlockLocation();
			}
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
			unlockLocation();
		}
		unlockLocation();
	}
}

function loadResult(url, result){
	if (result != ''){
		// Response mode
		this.obj.options[0]  = new Option(this.obj.oldText, "");
		for(i=0; i < result.length; i++){
			this.obj.options[this.obj.options.length]  = new Option(result[i].name,result[i].id);
		}
	} else if(url != '') {
		// Input mode
		return (loadXMLDoc(url));
	}
}

function unlockLocation() {
	this.form.country_id.disabled="";
	this.form.state_id.disabled="";
	this.form.region_id.disabled="";
}