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

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


	this.obj = obj;
	this.field_0 = form.country_id;
	this.field_1 = form.state_id;
	this.field_2 = form.region_id;
	
	this.rhost = host;
	this.rname = obj.name;
	this.rval = fk_value;
	this.rform = form;

	resetSelect(obj);

	// check if object exisits.
	if (typeof obj != 'object') return false;
	
	if(autos > 0){
		window.autosel = autos;
	} else {
		window.autosel = 0;
	}
	
	if(autosb > 0){
		window.autoselb = autosb;
	} else {
		window.autoselb = 0;
	}

	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(showText(LANG_JS_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(showText(LANG_JS_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 fillSelectC(host, obj, fk_value, form, sect) {
	resetSelect(obj);

	//resetSelect(form.region_id);
	if (fk_value > 0) {
		form.search_sub_category.options[1] = new Option(showText(LANG_JS_LOADING), -1);
		form.search_sub_category.options[1].selected=true;
		this.field_loading = form.search_sub_category;
	}
	url = host + '/'+sect+'/cats.php?cat_id=' + fk_value;
	if (fk_value > 0){
		loadResult(url, '');
	}
}

function setSearchCat(val){
	document.getElementById('hidden_category_id').value = val;
}

function fillPcode(host, fk_value, field) {
	if(fk_value == "Select a Suburb") {
		field.value = "";
	} else {
	 	put = field;
		try {
		  xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
		  field.value = "loading...";
		} catch (e) { 
		  // No Ajax support
		} 
		 
		// the xmlhttp object triggers an event everytime the status changes 
		// triggered() function handles the events 
		xmlhttp.onreadystatechange = triggered; 
		 
		// open takes in the HTTP method and url.
		url = host + '/get_postcode.php?state=vic&suburb=' + fk_value;
		req = "state=vic&suburb=" + fk_value;
		xmlhttp.open("GET", url); 
		 
		// send the request. if this is a POST request we would have 
		// sent post variables: send("name=aleem&gender=male) 
		// Moz is fine with just send(); but 
		// IE expects a value here, hence we do send(null); 
		xmlhttp.send(null);
	}
} 
	 
function triggered() {
	
	// if the readyState code is 4 (Completed) 
	// and http status is 200 (OK) we go ahead and get the responseText 
	// other readyState codes: 
	// 0=Uninitialised 1=Loading 2=Loaded 3=Interactive 
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
			// alert("triggered! "+xmlhttp.responseText);
			put.value = xmlhttp.responseText;
			//document.getElementById("zip_code").innerHTML = xmlhttp.responseText;
	}
}

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(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") {
		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, '');
	}

}

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;
		//alert(url);
		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);
			//trace("auto select = "+window.autosel+" result id = "+result[i].id+" Loop Count = "+i);
			if(window.autosel > 0){			
				if(window.autosel == result[i].id){
					auto_id = (i+1);
				}
			} else {
				auto_id = 0;
			}
		}
	} else if (url != '') {
		// Input mode
		
		return (loadXMLDoc(url));
	}
}
function selectOptionByValue(selObj, val){
    var A= selObj.options, L= A.length;
    while(L){
        if (A[--L].value== val){
            selObj.selectedIndex= L;
            L= 0;
        }
    }
}
function clear_asearch(){
	document.getElementById('wheres').value = "";
}
function clear_pcode(){
	document.getElementById('zip_code').value = "";
}
function unlockLocation() {
	if (this.field_loading) {
		this.field_loading.options[1]=null;
		
		// this.field_loading.options[window.autosel].selected=true;
		
		//trace("auto selecting item id "+auto_id);
		this.field_loading.options[auto_id].selected=true;
	}
	this.field_0.disabled=false;
	this.field_1.disabled=false;
	this.field_2.disabled=false;
	
	if(this.obj.name == "state_id" && window.autosel > 0){
		fillSelect(this.rhost, this.rform.region_id, this.rform.state_id.value, this.rform, window.autoselb);
	} else if("region_id" && window.autosel > 0){
		fillPcode(this.rhost, this.rform.region_id.options[this.rform.region_id.selectedIndex].innerHTML, this.rform.zip_code);
	}
	
}