$(document).ready(function() {
	//check for blur on the postcode field
	$("input#post_code").blur(function() {
		if($("input#post_code").val() != '')
		{
			$.post(deriveRoot()+"check-postcode/", {
				ajax		: 'true',
				postcode	: $("input#post_code").val()
			}, function(data) {
				
				var values = data.split(':');
				
				if(values[0] == "true") // No postcode found so insert
				{
					usePointFromPostcode($("input#address_1").val(), values[1], store);
				}
				else
				{
					if(values[0] != 'false')
					{
						$("input[name='post_code']").val(values[0]);
					}
					$("input[name='lat']").val(values[1]);
					$("input[name='lng']").val(values[2]);
				}
			});
		}
	});
});

/*----------------------------------
	store - makes an ajax call to a page which
	stores the lat, lng into a db ready for next time ;)
----------------------------------*/
function store(lat, lng, postcode) {
	$.post(deriveRoot()+"insert-postcode/", {
		ajax 	: 'true',
		postcode: postcode,
		lat 	: lat,
		lng 	: lng
	}, function(data) {
		//insert an if statement to doing testing if required
		//we dont need to check whether it was a success or not
		//if it isnt googlemaps will still make a map and the user
		//can still roll to the next stage
		var values = data.split(':');
		
		$("input[name='lat']").val(values[1]);
		$("input[name='lng']").val(values[2]);
		if(values[0] != 'false')
		{
			$("input[name='post_code']").val(postcode);
		}
	});
}

/*----------------------------------
TESTING FUNCTION MIMICS PHP PRINT_R
-----------------------------------*/
function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}

/*----------------------------------
	usePointFromPostcode - Uses GoogleSearch API
	to locate the requested post code
----------------------------------*/
function usePointFromPostcode(address1, postcode, callbackFunction) {
	var localSearch = new GlocalSearch();
	var i = 0;

	localSearch.setSearchCompleteCallback(null,
	
	function() {
		if (localSearch.results[0]) {
			//print_r(localSearch.results);   
			var resultLat = localSearch.results[0].lat;
			var resultLng = localSearch.results[0].lng;
			$("input[name='lat']").val(resultLat);
			$("input[name='lng']").val(resultLng);
			callbackFunction(resultLat, resultLng, postcode);
		}else{
			if(i==0) {
				localSearch.execute(postcode);
				i++;
			} else {
				alert("Postcode not found!");
			}
		}
	});  
	localSearch.execute(address1+", "+postcode);
}

function deriveRoot()
{
  var root = window.location.protocol+'//'+window.location.hostname+'/';
  
  if ( ( pos = window.location.pathname.search('public_html') ) != -1 )
  {
    root += window.location.pathname.substring(1,pos+12);
  }
  
  if ( window.location.pathname.search('demo') != -1 )
  {
    root += 'demo/';
  }
  
  return root;
}