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

function fillSelect(host, obj, fk_value, form, domain) {
	
		
	this.obj = obj;
	
	if (domain==undefined){
		domain='';
		this.field_0 = form.country_id;
		this.field_1 = form.state_id;
		this.state_name = 'state_id';
		this.field_2 = form.region_id;
		this.region_name ='region_id';
	    this.field_3 = form.city_id;
		this.city_name ='city_id';
	}else{
		if (domain=='playadelcarmen'){
			this.field_0 = form.playadelcarmencountry_id;
			
			this.field_1 = form.playadelcarmenstate_id;
			this.state_name = 'playadelcarmenstate_id';
			
			this.field_2 = form.playadelcarmenregion_id;
			this.region_name ='playadelcarmenregion_id';
			
		    this.field_3 = form.playadelcarmencity_id;
			this.city_name ='playadelcarmencity_id';
		}
		
		if (domain=='tulum'){
			this.field_0 = form.tulumcountry_id;
			
			this.field_1 = form.tulumstate_id;
			this.state_name = 'tulumstate_id';
			
			this.field_2 = form.tulumregion_id;
			this.region_name ='tulumregion_id';
			
		    this.field_3 = form.tulumcity_id;
			this.city_name ='tulumcity_id';	
		}
		
		if (domain=='discovermexico'){
			this.field_0 = form.discovermexicocountry_id;
			
			this.field_1 = form.discovermexicostate_id;
			this.state_name = 'discovermexicostate_id';
		
			this.field_2 = form.discovermexicoregion_id;
			this.region_name ='discovermexicoregion_id';
		    
			this.field_3 = form.discovermexicocity_id;
			this.city_name ='discovermexicocity_id';	
		}
		
		if (domain=='cancuntulum'){
			this.field_0 = form.cancuntulumcountry_id;
		
			this.field_1 = form.cancuntulumstate_id;
			this.state_name = 'cancuntulumstate_id';
			
			this.field_2 = form.cancuntulumregion_id;
			this.region_name ='cancuntulumregion_id';
			
		    this.field_3 = form.cancuntulumcity_id;
			this.city_name ='cancuntulumcity_id';	
		}
		
		if (domain=='mayanriviera'){
			this.field_0 = form.mayanrivieracountry_id;
			
			this.field_1 = form.mayanrivierastate_id;
			this.state_name = 'mayanrivierastate_id';
			
			this.field_2 = form.mayanrivieraregion_id;
			this.region_name ='mayanrivieraregion_id';
			
		    this.field_3 = form.mayanrivieracity_id;
			this.city_name ='mayanrivieracity_id';
		}
	}

	resetSelect(obj,1);

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

	if (fk_value > 0) {
		field_0.disabled=true;
		field_1.disabled=true;
		field_2.disabled=true;
        field_3.disabled=true;
	}

	if (obj.name == state_name) {
		resetSelect(field_2);
		resetSelect(field_3);
		if (fk_value > 0) {
			field_1.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			field_1.options[1].selected=true;
			this.field_loading = field_1;
		}
		url = host + '/location.php?country_id=' + fk_value+'&domain='+domain;
		if (fk_value > 0) loadResult(url, '');
	}

	if (obj.name == region_name) {
        resetSelect(field_3);
        
		if (fk_value > 0) {
			field_2.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			field_2.options[1].selected=true;
			this.field_loading = field_2;
		}
		url = host + '/location.php?state_id=' + fk_value+'&domain='+domain;
		if (fk_value > 0) loadResult(url, '');
	}
    
    if (obj.name == city_name) {
        if (fk_value > 0) {
            field_3.options[1] = new Option(showText(LANG_JS_LOADING), -1);
            field_3.options[1].selected=true;
            this.field_loading = field_3;
        }
        url = host + '/location.php?region_id=' + fk_value+'&domain='+domain;
        if (fk_value > 0) loadResult(url, '');
    }

}

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

	this.obj = obj;
	this.field_0 = form.search_country;
	this.field_1 = form.search_state;
	this.field_2 = form.search_region;
    this.field_3 = form.search_city;

	resetSelect(obj);

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

	if (fk_value > 0) {
		form.search_country.disabled=true;
		form.search_state.disabled=true;
		form.search_region.disabled=true;
        form.search_city.disabled=true;
	}

	if (obj.name == "search_state") {
		resetSelect(form.search_region);
		if (fk_value > 0) {
			form.search_state.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			form.search_state.options[1].selected=true;
			this.field_loading = form.search_state;
		}
		url = host + '/location.php?country_id=' + fk_value + '&searchpage=1';
		if (fk_value > 0) loadResult(url, '');
	}

	if (obj.name == "search_region") {
        resetSelect(form.search_city);
		if (fk_value > 0) {
			form.search_region.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			form.search_region.options[1].selected=true;
			this.field_loading = form.search_region;
		}
		url = host + '/location.php?state_id=' + fk_value + '&searchpage=1';
		if (fk_value > 0) loadResult(url, '');
	}
    
    if (obj.name == "search_city") {
        if (fk_value > 0) {
            form.search_city.options[1] = new Option(showText(LANG_JS_LOADING), -1);
            form.search_city.options[1].selected=true;
            this.field_loading = form.search_city;
        }
        url = host + '/location.php?region_id=' + fk_value + '&searchpage=1';
        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, 'obj_name': response.getElementsByTagName('obj_name')[i].firstChild.data};
				}
				loadResult('',result);
				
				unlockLocation();
			}
		} 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++) {
			document.getElementById(result[i].obj_name).options[document.getElementById(result[i].obj_name).options.length] = new Option(result[i].name,result[i].id);
		}
	} else if (url != '') {
		// Input mode
		return (loadXMLDoc(url));
	}
}

function unlockLocation() {

	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;
    this.field_3.disabled=false;
}