var obj;
var field_0;
var field_1;
var field_2;
var field_loading;
p = 0; // See fillAdvancedSearch (global)
function fillSelect(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.name == "state_id") {
		resetSelect(form.state_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;
		}
		
		// Gets All the STATES [ Brisbane, Sunshine Coast Etc ] and places it into the reults object name in this case that is
		// state_id [so places all in state_id drop menu];
		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, '');
	}
	
	// --- ADDED BY ANDRE //
	// This is for members -> listing admin
	if (obj.name == "city_id") {
		//resetSelect(form.city_id);
		if (fk_value > 0) {
			//form.city_id.options.length=0;
			//form.city_id.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			//form.city_id.options[1].selected=true;
			this.field_loading = form.city_id;
		}
		url = host + '/location.php?region_id=' + fk_value;
		if (fk_value > 0) loadResult(url, '');
	}
	
	// -- End Andre //
}


function fill(host,fk_value,form) {
		if (fk_value > 0) {
				//form.city_id.options[1] = new Option(showText(LANG_JS_LOADING), -1);
				//form.city_id.options[1].selected=true;
				//this.field_loading = form.city_id;
				//form.city.style.display = '';// Chrome does not like
				document.getElementById('city_id').style.display = '';
			}
			url = host + '/location.php?advanced=' + fk_value;
			if (fk_value > 0) loadResult(url, '');
}

// Added By Andre // 
// fk_value  = foreign key value
function fillAdvancedSearch(host, obj, fk_value, form) {
		this.obj = obj;
		
			
		// Set a global variable letting me know thsi is called from advanced search
		// this is so that the 'pretty' box stuff is triggered
		p = 1;
		
		if (obj.name == "state_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;
				//form.state.style.display = '';// Chrome does not like
				document.getElementById('state_id').style.display = ''; 
				document.getElementById("search").style.backgroundImage = "url(/custom/theme_files/handylocal/images/bg_search_adv.png)";
				document.getElementById("search").style.height ="262px";
				
			}
			url = host + '/location.php?country_id=' + fk_value;
			if (fk_value > 0) loadResult(url, '');
		}
		
		if (obj.name == "city_id") {
			if (fk_value > 0) {
				//form.city_id.options[1] = new Option(showText(LANG_JS_LOADING), -1);
				//form.city_id.options[1].selected=true;
				//this.field_loading = form.city_id;
				//form.city.style.display = '';// Chrome does not like
				document.getElementById('city_id').style.display = '';
			}
			url = host + '/location.php?advanced=' + fk_value;
			if (fk_value > 0) loadResult(url, '');
		}
	
}
// -- End Andre //

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

}

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);
		}
		// Front end search ' pretty box' trigger
		if(p == 1){
			if(result[0].obj_name == 'state_id'){$('#state_id').selectbox();}
			if(result[0].obj_name == 'city_id'){$('#city_id').selectbox();}
		}
		
	
	} 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;
}






// -----------------------
// ------ NEW STUFF ------
// Andre - 16 -10 -2009
// -----------------------

// Get all Zones and display them in the listing form.

function processXML(xml){
	var res = {}
	// create JSOn object and return it.
	jQuery(xml).find('id').each(function(i){
		res[i] = [{'id' : jQuery(this).text(), 'name' : jQuery(this).next().text()}];
	});
	return res; //[ is returning undefined -- not sure why -- :(
}

function getXml(host,fk_value){
		url = host + '/location.php?state_id=' + fk_value;
		jQuery.ajax({
				type: "GET",
				url: url,
				dataType: "xml",
				success: function(xml){	return processXML(xml); }
		});
}


function getZones(host, obj, fk_value,selected) {
		this.obj = obj;
		var dataL ='';
		var dataR ='';
		var x = 0;
				
	
	//console.log( selected );
	//console.dir( selected );
	//alert(jQuery(selected).size);
	jQuery("#cities").html('');
	
		url = host + '/location.php?state_id=' + fk_value;
		jQuery.ajax({
				type: "GET",
				url: url,
				dataType: "xml",
				success: function(xml) {
					// Clear the zones first then reload.
					// Make sure to show previously selected zones
					//var splitData = (jQuery('id',xml).length) /2;
					jQuery(xml).find('id').each(function(){
						var id 		= jQuery(this).text();
						var name 	= jQuery(this).next().text();
						var chck 	= false;

						// Check the Selected -- if its more than one it will be an array of objects else its a sible object
						if (typeof selected === 'object'){
							jQuery.each(selected.Zones, function(i, zone) {	
									if(zone.region_id !== undefined && zone.region_id !== ""){
									if(zone.region_id === id){chck = 'checked="checked"';}
								}
							});
						}else{
							
						}
						
						// Split the selct boxes up into 2 columns
						if (x % 2 == 0) { 
							dataL 	= 	dataL + '<label">'+name+'</label><input name="zone['+id+']" displayName="'+name+'" type="checkbox" onclick="getCities(\'http://handylocals.com.au\', this.value, \''+name+'\', this.checked);" '+chck+' value="'+id+'" /><br>';
						}else{
							dataR 	= dataR + '<label">'+name+'</label><input name="zone['+id+']" displayName="'+name+'" type="checkbox" onclick="getCities(\'http://handylocals.com.au\', this.value, \''+name+'\', this.checked);" '+chck+' value="'+id+'" /><br><script>getCities(\'http://handylocals.com.au\', \''+id+'\', \''+name+'\', \'true\');</script>';
						}
						x++;
					});
					
					jQuery("#z_left").html(dataL);
					jQuery("#z_right").html(dataR);
					showCities(host);
				}
			});
}

// Once a Zone is clicked in listing this will get the cities for that Zone
// it will then place those sities in a div

function getCities(host,fk_value,zone,checkbox){
	
	if(checkbox === true){
	
	this.obj = obj;
	var data ='<div id="zone_'+fk_value+'" style="float:left; width:120px; height:200px; overflow:auto; padding:3px; border:solid 1px #ddd; background-color:#eee; margin:3px;"><h3 style="display:block; border-bottom: dashed 1px #ddd;">'+zone+'</h3><br>';;
				
	//console.log( getXml(host,fk_value) );
	//console.dir( getXml(host,fk_value) );

		url = host + '/location.php?region_id=' + fk_value;
		jQuery.ajax({
				type: "GET",
				url: url,
				dataType: "xml",
				success: function(xml) {
					jQuery(xml).find('id').each(function(){
						var id 		= jQuery(this).text();
						var name 	= jQuery(this).next().text();
						data 		= data + name+'<br>';
					});
					data = data + '</div>';
					jQuery("#cities").append(data);
				}
			});
	}else{
		jQuery("#zone_"+fk_value).remove();
	}
}

// used to show all cities for check boxes 
// called when page first loads so it can load all cities from the databse.
function showCities(host){
	//alert(this);
		jQuery("#zones").find(':checkbox').each(function(){
			if(this.checked === true){getCities(host,this.value,jQuery(this).attr('displayName'),true)}												   
		});
}

















