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

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

	this.obj = obj;	
    
    var is_extra_location = (obj.name.indexOf("_extra") != -1);
    
    if (is_extra_location) {
	    this.field_0 = form.country_id_extra;
	    this.field_1 = form.state_id_extra;
	    this.field_2 = form.region_id_extra;
    } else {
        this.field_0 = form.country_id;
        this.field_1 = form.state_id;
        this.field_2 = form.region_id;
    }
		
	resetSelectLoc(obj);
	
	if (fk_value > 0) {
        
        if (is_extra_location) {
            form.country_id_extra.disabled=true;
            form.state_id_extra.disabled=true;
            form.region_id_extra.disabled=true;
        } else {
            form.country_id.disabled=true;
            form.state_id.disabled=true;
            form.region_id.disabled=true;
        }
        
	}

	// check if object exisits.
	if(typeof obj != 'object') return false;
	resetSelectLoc(obj);
	
	if(obj.name == "state_id"){	
		if (fk_value > 0) {
			obj.options[1] = new Option("Loading...", -1);
			obj.options[1].selected=true;
			this.field_loading = obj;
		}
		url  = host+'/location.php?country_id=' + fk_value;
		if(fk_value > 0) loadResultLoc(url,'');
	}
	
	if(obj.name == "region_id"){
		if (fk_value > 0) {
			obj.options[1] = new Option("Loading...", -1);
			obj.options[1].selected=true;
			this.field_loading = obj;
		}
		url  = host+'/location.php?state_id=' + fk_value+'&from='+from;
		if(fk_value > 0) loadResultLoc(url,'');
	}
	
	if(obj.name == "state_id_extra"){		
        if (fk_value > 0) {
            obj.options[1] = new Option("Loading...", -1);
            obj.options[1].selected=true;
            this.field_loading = obj;
        }        
		url  = host+'/location.php?country_id_extra=' + fk_value;
		if(fk_value > 0) loadResultLoc(url,'');
	}
	
	if(obj.name == "region_id_extra"){
        if (fk_value > 0) {
            obj.options[1] = new Option("Loading...", -1);
            obj.options[1].selected=true;
            this.field_loading = obj;
        }        
		url  = host+'/location.php?state_id_extra=' + fk_value;
		if(fk_value > 0) loadResultLoc(url,'');
	}
	
}

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

function loadXMLDocLoc(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChangeLoc;
		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 = processReqChangeLoc;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processReqChangeLoc() {
	// 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};
				}
				loadResultLoc('',result);
				unlockLocationLoc();
			}
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function loadResultLoc(url, result){
	if (result != ''){
		// Response mode
		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 (loadXMLDocLoc(url));
	}
}

function unlockLocationLoc() {
	if (this.field_loading) {
		this.field_loading.options[1]=null;
		this.field_loading.options[0].selected=true;
	}
	this.field_0.disabled=false;
	this.field_1.disabled=false;
	this.field_2.disabled=false;
}