var geocoder = new GClientGeocoder();
var localSearch = new GlocalSearch();
	
function getLatLng(_form) {
	
	var address = "";	
	if (_form.address1 != null){
		var address1 = trim(_form.address1.value);
		if (address1.length > 0){
			address = address1;
		}
	}
	if (_form.address2 != null) {
		var address2 = trim(_form.address2.value);
		if (address2.length > 0){
			if (address.length > 0){
				address = address + ", " + address2;
			}
			else {
				address = address2;	
			}
		}
	}
	if (_form.address3 != null) {
		var address3 = trim(_form.address3.value);
		if (address3.length > 0){
			if (address.length > 0){
				address = address + ", " + address3;
			}
			else {
				address = address3;	
			}
		}
	}
	if (_form.county != null) {
		var county = trim(_form.county.value);
		if (county.length > 0){
			if (address.length > 0){
				address = address + ", " + county;
			}
			else {
				address = county;	
			}
		}
	}
	if (_form.postcode != null) {
		var postcode = trim(_form.postcode.value);
		if (postcode.length > 0){
			// Check for a space in the postcode because geocoder doesn't seem to work without
			var spaceIndex = postcode.indexOf(" ");
			if (postcode.length >= 5 && spaceIndex == -1){
				// If there is no space stick one in
				postcode = postcode.slice(0, postcode.length-3) + " " + postcode.slice(postcode.length-3, postcode.length);
			}
		
			if (address.length > 0){
				address = address + ", " + postcode;
			}
			else {
				address = postcode;	
			}
		}
	}

	var doSubmit = false;
	
	if (postcode.length == 0){
		alert("Please provide a postcode");
	}
	else {
		doSubmit = true;
	}
		
	/* if (_form.supplyAddress != null){
		if (address1.length == 0) {
			alert("Please provide a street name");
			doSubmit = false;
		}
	} */
		
	if (doSubmit) {
		address = address + ", UK";
				
		localSearch.setSearchCompleteCallback(null,function(){
			if (localSearch.results[0]) {	
				_form.lng.value = localSearch.results[0].lng;
				_form.lat.value = localSearch.results[0].lat;
				_form.submit();			
			} else {
				alert(address + " could not be found by Google Maps");
			}
		});
		localSearch.execute(address);
	}
	
}	

function checkEnter(e){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable
	
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		_form = document.getElementById("postcodeSearch");
		getLatLng(_form);
		return false; 
	}
	else{
		return true ;
	}
}

function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 
