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

function fillSelect(host, obj, fk_value, form, chkname) {
	this.obj = obj;
	this.field_0 = form.country_id;	
	this.field_1 = form.state_id;
	this.field_2 = form.region_id;
	this.field_3 = form.city_id;
	resetSelect(obj);
	
	// check if object exisits.
	if (typeof obj != 'object') return false;

	if (fk_value > 0) {
		form.country_id.disabled=true;
		form.state_id.disabled=true;
		form.region_id.disabled=true;
	}

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

	if (obj.name == "region_id") {
		if (fk_value > 0) {
			form.region_id.options[1] = new Option("Loading...", -1);
			form.region_id.options[1].selected=true;
			this.field_loading = form.region_id;
		}
		url = host + '/location.php?state_id=' + fk_value;
		if (fk_value > 0) loadResult(url, '');
	}

}
function fillSelect_Custom(host, obj, fk_value, form) {
    this.obj = obj;
    this.field_0 = form.country_id;
    this.field_1 = form.state_id;
    this.field_2 = form.region_id;
    resetSelect(obj);
	
    // check if object exisits.
    if (typeof obj != 'object') return false;

    if (fk_value > 0) {
        form.country_id.disabled=true;
        form.state_id.disabled=true;
        form.region_id.disabled=true;
    }

    if (obj.id == "state_id") {
        resetSelect(form.region_id);
        if (fk_value > 0) {
            form.state_id.options[1] = new Option("Loading...", -1);
            form.state_id.options[1].selected=true;
            this.field_loading = form.state_id;
        }
        url = host + '/location.php?country_id=' + fk_value;
        if (fk_value > 0) loadResult(url, '');
    }

    if (obj.id == "region_id") {
        if (fk_value > 0) {
            form.region_id.options[1] = new Option("Loading...", -1);
            form.region_id.options[1].selected=true;
            this.field_loading = form.region_id;
        }
        url = host + '/location.php?state_id=' + fk_value;
        if (fk_value > 0) loadResult(url, '');
    }
}

function fillSelect_front(host, obj, fk_value, form) {
	this.obj = obj;
	
	this.field_0 = form.country;
	this.field_1 = form.state;
	this.field_2 = form.region;
	this.field_3 = form.city;
	resetSelect(obj);

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

	if (fk_value > 0) {
		form.country.disabled=true;
		form.state.disabled=true;
		form.region.disabled=true;
		form.city.disabled=true;
	}
	
	if (obj.name == "state") {
		resetSelect(form.region);
		resetSelect(form.city);
		if (fk_value != "") {
			form.state.options[1] = new Option("Loading...", -1);
			form.state.options[1].selected=true;
			this.field_loading = form.state;
		}
		url = host + '/location.php?country=' + fk_value;
		if (fk_value != "") loadResult(url, '');
	}

	if (obj.name == "region") {
		resetSelect(form.city);
		if (fk_value!="") {
			form.region.options[1] = new Option("Loading...", -1);
			form.region.options[1].selected=true;
			this.field_loading = form.region;
		}
		url = host + '/location.php?state=' + fk_value;
		if (fk_value != "") loadResult(url, '');
	}
	
	if (obj.name == "city") {
		if (fk_value != "") {
			form.city.options[1] = new Option("Loading...", -1);
			form.city.options[1].selected=true;
			this.field_loading = form.city;
		}
		url = host + '/location.php?region=' + fk_value;
		if (fk_value != "") loadResult(url, '');
	}
	

}
function fillSelect_billing(host, obj, fk_value, form) {
	this.obj = obj;
	
	//form.x_country = "Australia";
	this.field_0 = form.x_country;
	this.field_1 = form.x_state;
	this.field_2 = form.x_city;
	resetSelect(obj);
	
	// check if object exisits.
	if (typeof obj != 'object') return false;
	if (fk_value > 0) {
		form.country_id.disabled=true;
		form.x_state.disabled=true;
		form.x_city.disabled=true;
	}

	if (obj.name == "x_state") {
		resetSelect(form.x_region);
		resetSelect(form.x_city);
		if (fk_value != "") {
			form.x_state.options[1] = new Option("Loading...", -1);
			form.x_state.options[1].selected=true;
			this.field_loading = form.x_state;
		}
		url = host + '/location.php?x_country=' + fk_value;
		if (fk_value!= "") loadResult(url, '');
	}
	
	if (obj.name == "x_region") {
		resetSelect(form.x_city);
		if (fk_value!= "") {
			form.x_region.options[1] = new Option("Loading...", -1);
			form.x_region.options[1].selected=true;
			this.field_loading = form.x_region;
		}
		url = host + '/location.php?x_state=' + fk_value;
		if (fk_value!= "") loadResult(url, '');
	}
	
	if (obj.name == "x_city") {
		if (fk_value != "") {
			form.x_city.options[1] = new Option("Loading...", -1);
			form.x_city.options[1].selected=true;
			this.field_loading = form.x_city;
		}
		url = host + '/location.php?x_region=' + fk_value;
		if (fk_value != "") loadResult(url, '');
	}
}

function fillSelect_seach(host, obj, fk_value, form) {
	this.obj = obj;
	
	//form.x_country = "Australia";
	this.field_0 = form.country;
	this.field_1 = form.search_state;
	this.field_2 = form.state_city_id;
	this.field_3 = form.location;
	resetSelect(obj);
	
	// check if object exisits.
	if (typeof obj != 'object') return false;
	if (fk_value > 0) {
		form.state_city_id.disabled=true;
	}

	if (obj.name == "state_city_id") {
		resetSelect(form.state_city_id);
		if (fk_value!= "") {
			form.state_city_id.options[1] = new Option("Loading...", -1);
			form.state_city_id.options[1].selected=true;
			this.field_loading = form.state_city_id;
		}
		url = host + '/location.php?search_state=' + fk_value;
		if (fk_value!= "") loadResult(url, '');
	}
}

function loadBillingResult(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 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;

	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;
	}

	if (obj.name == "search_state") {
		resetSelect(form.search_region);
		if (fk_value > 0) {
			form.search_state.options[1] = new Option("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") {
		if (fk_value > 0) {
			form.search_region.options[1] = new Option("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, '');
	}

}

function activexControl(){
	var xmlHttp;
	try{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e){
  // Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			try{
			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){
			  alert("Your browser does not support AJAX!");
			  return false;
			}
		}
  	}
  
  return xmlHttp;
}

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
	req = new activexControl();
	req.onreadystatechange = processReqChange;
	req.open("GET", url, true);
	req.send(null);
	/*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++) {
			sub_name = result[i].name;
			sub_name.replace(" ","&nbsp;");
			document.getElementById(result[i].obj_name).options[document.getElementById(result[i].obj_name).options.length] = new Option(sub_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;
}
