$(document).ready(function() {
		
	// get a reference to all required location fields
	country_field	= document.getElementById("country_name");
	region_field	= document.getElementById("region_name");
	suburb_field	= document.getElementById("suburbInput");
	search_btn		= "N";
	if (document.getElementById("searchBtn")!=undefined) {
		search_btn = "Y";
	}

	if ( country_field!==null ) {
		// get initial values of each field 
		country_val	= country_field.value;
		region_val	= document.getElementById("region_val").value;
		suburb_val	= document.getElementById("suburb_val").value;


		// make sure the region field is populated when a new country is selected
		country_field.onchange = function() {
			country_val = country_field.value;
			load_region_select();
		};

		if (country_field.selectedIndex>0) {
			// populate region select box
			load_region_select();
		}
	}

});


function update_select(select, options) {
  $(select).empty().append(options);
}


function set_select_value(select, value) {
  for (var i = 0; i < select.options.length; i++) {
    if (select.options[i].value == value) {
      select.selectedIndex = i;
      select.options[i].selected = true;
      break;
    }    
  }
}


function lookupSuburb(inputString) {
	var vInputString = '';

	if ( inputString.length == 0 ) {
		// Hide the suggestion box.
		$('#suggestions').hide();
		// if input string cleared, clear suburb_val
		$('#suburb_val').val('');

	} else if ( country_field.value!='' && region_field.value!='' )	{
		sub_val	= document.getElementById("suburb_val").value;
		vInputString = inputString;
		if ( sub_val!='' ) {
			len = sub_val.length;
			vInputString = inputString.substring(len);
		}
		if (vInputString!='') {
			$.get("/modules/_locations/locations_ajax.php", {country:country_field.value, region:region_field.value, suburbInput: ""+vInputString+"", searchBtn:search_btn}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
				setTimeout("$('#suggestions').hide();", 5000);
			});
		}
	}
} // lookupSuburb


// On search forms, postcode and suburb entered in the same field
function fill(thisValue) {
	if (thisValue!=undefined) {
		sub_val	= document.getElementById("suburb_val").value;
		$('#suburbInput').val(sub_val + thisValue + ', ');
		$('#suburb_val').val(sub_val + thisValue + ', ');
		setTimeout("$('#suggestions').hide();", 5000);
	}
}


// On edit forms, postcode and suburb to be entered in separate fields
function fillSuburb(thisValue) {
	if (thisValue!=undefined) {
		$('#suburbInput').val(thisValue);
		setTimeout("$('#suggestions').hide();", 5000);
	}
}


// On edit forms, postcode and suburb to be entered in separate fields
function fillPostcode(thisValue) {
	if ( document.getElementById("postcodeInput")!=undefined ) {
		if (thisValue!=undefined) {
			$('#postcodeInput').val(thisValue);
			setTimeout("$('#suggestions').hide();", 5000);
		}
	}
}



function load_region_select() {
	update_select(region_field, '<option value="-">[loading...]</option>');

	if (country_field.selectedIndex==0) {
		update_select(region_field, '<option value="-">[Please select a country first]</option>');
	
	} else {
		iso2 = country_field.value;
		$.get("/modules/_locations/locations_ajax.php", {country:iso2, region:region_val}, function(x) {
			update_select(region_field, x);
		
			// if country matches initial value, set to initial region value
			if (country_field.value == country_val) {
				set_select_value(region_field, region_val);

			} else {
				// otherwise set to first option
				region_field.options[0].selected = true;
			}

		});
	}
}
