// JavaScript Document
var map;
var gdir;
var geocoder = null;
var addressMarker;

function initialize() {
  if (GBrowserIsCompatible()) {      
	map = new GMap2(document.getElementById("map_canvas"));
	gdir = new GDirections(map, document.getElementById("directions"));
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	setDirections(fromfield, tofield, "en");
  }
}

function setDirections(fromAddress, toAddress, locale)
	{
	if (postcodeCheck(fromAddress))
		{
		while(fromAddress.slice(size-1,size)== " ") //Strip trailing spaces
			{
			fromAddress = fromAddress.substr(0,size-1);
			size = fromAddress.length;
			}
		fromAddress = fromAddress.substr(0,size-2); // strip last character from valid postcode to get around Royal Mail's vice-like grip on what should be freely and publically available information, so that the Google geocoder can do its thing with at least some accuracy
		}
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
	}

function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	 alert("Sorry, we could not find your starting point. Please try your postcode, or the first line of your address and town (eg '34 Street Road, Townsville').\nYou can also use the full map service at http://maps.google.co.uk/");
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	 alert("Your request could not be successfully processed, yet the exact reason for the failure is not known, sorry.\nYou may wish to use the full map service at http://maps.google.co.uk/");
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("You need to enter a starting address to generate route directions.");

//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	 
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	 alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	
   else alert("Sorry, we can't find your starting point, please try adding county and/or country information after your town name (eg 'Boston, UK').\nYou may wish to use the full map service at http://maps.google.co.uk/");
   
}

function onGDirectionsLoad(){ 
  // Use this function to access information about the latest load()
  // results.

  // e.g.
  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
  // and yada yada yada...
}

function postcodeCheck(test)
	{ //check postcode format is valid
	size = test.length;
	test = test.toUpperCase(); //Change to uppercase
	while (test.slice(0,1) == " ") //Strip leading spaces
		{
		test = test.substr(1,size-1);
		size = test.length;
		}
	while(test.slice(size-1,size)== " ") //Strip trailing spaces
		{
		test = test.substr(0,size-1);
		size = test.length;
		}

	if (size < 6 || size > 8)
		{ //Code length rule
		return false;
		}
	if (!(isNaN(test.charAt(0))))
		{ //leftmost character must be alpha character rule
		return false;
		}
	if (isNaN(test.charAt(size-3)))
		{ //first character of inward code must be numeric rule
		return false;
		}
	if (!(isNaN(test.charAt(size-2))))
		{ //second character of inward code must be alpha rule
		return false;
		}
	if (!(isNaN(test.charAt(size-1))))
		{ //third character of inward code must be alpha rule
		return false;
		}
	 if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
	   return false;
	   }
	 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
	 if (count1 != count2){//only one space rule
	   return false;
	  }
	return true;
	}
