//<![CDATA[
    var map; //Create new Google Map
	var localSearch = new GlocalSearch(); //Ajax local search for post code
    var gdir; //Store GDirections
    var geocoder = null; 
	var coord = "50.781955916823705, -2.9942089319229126"; //Co-ordinates for Strictly Woodturning Parking Facilties
	var html = '<span class="bodytext" url="../strictlywoodturning.css" style="text-align:left;"><br />Directions will appear here when you have submitted a valid postcode</span>';
	var checkPost = "false";//innerHTML for directions DIV

	//mapLoad() - creates new map on page load with markers and associated content. Also set's event listeners so that it knows when to 
	//execute a certain accessor or mutator method.
	function mapLoad() {
	
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));
			gdir = new GDirections(map, document.getElementById("directions"));
			GEvent.addListener(gdir, "load", onGDirectionsLoad);
        	GEvent.addListener(gdir, "error", handleErrors);
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.setMapType(G_NORMAL_MAP);
			map.setCenter(new GLatLng(52.62306005822959, -1.834716796875),6);
			setPanel();
			
			//Create markers - Points of Interest (POI)
			var point = new GLatLng(52.49875913933867, -1.4756441116333008);
			var marker = createMarker(point, '<div id="html_page" class="bodytext" style="width:375px; height: 110px;"><img src="http://www.axminster.co.uk/images/articles/tradeoutlets_nuneaton.jpg" alt="Axminster Tool Centre, Nuneaton" style="float:left; padding:10px;" /><a href="http://www.axminster.co.uk" target="_blank"class="programmelink">Axminster Tool Centre, Nuneaton</a><br /><br />Our biggest store yet, covering 12,000 square feet of retail space. Not only does this store stock the complete range of products, it plays host to Festool World - our shop-in-shop. </div>', 'Tool Centre');
		    map.addOverlay(marker);	

			var point = new GLatLng(50.780339706477335, -3.00351619720459);
			var marker = createMarker(point, '<div id="html_page" class="bodytext" style="width:375px; height: 110px;"><img src="http://www.axminster.co.uk/images/articles/tradeoutlets_axminster.jpg" alt="Axminster Tool Centre, Axminster" style="float:left; padding:10px;" /><a href="http://www.axminster.co.uk" target="_blank"class="programmelink">Axminster Tool Centre, Axminster</a><br /><br />Our purpose built store in Axminster, Devon stocks the full range. Also incorporating the Axminster Skill Centre where you can discover turning, carving and routing.</div>', 'Tool Centre');
		    map.addOverlay(marker);	
			
			var point = new GLatLng(50.790426, -2.980915); // new skill centre location  -  Unit 10 
			//var point = new GLatLng(50.78013619229547, -3.003588616847992);  //old location in Axminster store
			var marker = createMarker(point, '<div id="html_page" class="bodytext" style="width:375px; height: 110px;"><img src="images/axskillcentre_logo.jpg" alt="Axminster Skill Centre, Axminster" style="float:left; padding:10px;" /><a href="http://www.axminsterskillcentre.co.uk" target="_blank" class="programmelink">Axminster Skill Centre, Axminster</a><br /><br />The Axminster Skill centre has been established to offer our customers the opportunity to develop their skills within the diverse range of woodworking and associated crafts.</div>', 'Skill Centre');
		    map.addOverlay(marker);	
			
			var point = new GLatLng(51.361129904392854, 0.7204198837280273);
			var marker = createMarker(point, '<div id="html_page" class="bodytext" style="width:375px; height: 110px;"><img src="http://www.axminster.co.uk/images/articles/tradeoutlets_sittingbourne.jpg" alt="Axminster Tool Centre, Sittingbourne" style="float:left; padding:10px;" /><a href="http://www.axminster.co.uk" target="_blank"class="programmelink">Axminster Tool Centre, Sittingbourne</a><br /><br />View our extensive range of machinery, power tools and accessories in 16,000 square feet of space. Industrial machinery from quality brands - including Rojek and Martin - is also on display.</div>', 'Tool Centre');
		    map.addOverlay(marker);	
			
			var point = new GLatLng(51.62123396262196, -0.7727980613708496);
			var marker = createMarker(point, '<div id="html_page" class="bodytext" style="width:375px; height: 110px;"><img src="http://www.axminster.co.uk/images/articles/tradeoutlets_highwycombe.jpg" alt="Axminster Tool Centre, High Wycombe" style="float:left; padding:10px;" /><a href="http://www.axminster.co.uk" target="_blank"class="programmelink">Axminster Tool Centre, High Wycombe</a><br /><br />Our Axminster Tool Centre High Wycombe outlet follows the format of our existing stores: a vast range of tools and machinery with customer service and technical knowledge second to none.<br /></div>', 'Tool Centre');
		    map.addOverlay(marker);
			
			var point = new GLatLng(50.781955916823705, -2.9942089319229126);
			var marker = createMarker(point, '<div id="html_page" class="bodytext" style="height:25px;">Strictly Woodturning Parking Facilties</div>', 'Parking Facilities');
		    map.addOverlay(marker);	
			
		
		} else {
		  alert("Sorry, the use of Google Maps on this page is not compatible with this browser.");
		}
	
	}

	//Create new markers on the map. With the point(Lat and lng), html and labels.
	function createMarker(point, html1, label1) {
			var marker = new GMarker(point);
			GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowTabsHtml([new GInfoWindowTab(label1,html1)]);
	});
		GEvent.addListener(marker, "infowindowclose", function() {
	});
		return marker;
	}

	 //handles any errors that may arise upon set directions method and alerts the user if an error has occured
 	function doClear(theText) {
     		if (theText.value == theText.defaultValue) {
         		theText.value = ""
     		}
 	}

	//accessor method that returns the coordinates of the destination address
    function setDirections(fromAddress, toAddress, locale) {
	  document.getElementById('directions').innerHTML = "";
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    }
	
	//Sets the directions panel to whatever may be within the html variable. Accessed at the beginning on mapLoad()
    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\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 that did not work, please enter a valid post code. For example, EX13 5PH.");
	   
	}
	
	function onGDirectionsLoad(){ 
	  // Use this function to access information about the latest load()
      // results.
	}

	//accessor method for the retrieval of the destination coordinates
	function getAddr() {
		return coord;
	}
	
	//set's the content of directions to fill with what ever is needed within the variable "html"
	function setPanel() {
		document.getElementById('directions').innerHTML = html;
	}
	
	function addLoadEvent(func) {
	  var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
		window.onload = func;
	  } else {
		window.onload = function() {
		  oldonload();
		  func();
		}
	  }	
	}
	
	function addUnLoadEvent(func) {
		var oldonunload = window.onunload;
		if (typeof window.onunload != 'function') {
		  window.onunload = func;
		} else {
		  window.onunload = function() {
			oldonunload();
			func();
		  }
		}
	}
	
	function doSearch(value, locale) {
		postcodeCheck(value);
		if(checkPost == "true") {		
			var destination = placePoint(value, dealer_search, locale);
			return;
		}
		else { 
			alert("I'm sorry your postcode is in a invalid format. EX13 5PH is a good postcode example.");
		}
	}
	
	/***********************************************
	* Check to see if the postcode is in the correct format. Post code can be typed with and without space. Code was changed in areas that suited our needs.
	* Copyright 2009 WebMediaBrands Inc. All Rights Reserved.
	* http://javascript.internet.com/forms/uk-postcode-validation.html
	***********************************************/
	
	
	function postcodeCheck(value) {
		var value2 = value;
		size = value2.length;
		value2 = value.toUpperCase();
		
		while (value2.slice(0,1) == " ")
			{ value2 = value2.substr(1, size-1); size = value2.length;
			}
		 while(value2.slice(size-1,size)== " ") //Strip trailing spaces
  			{ value2 = value2.substr(0,size-1); size = value2.length;
 		    }	
		
		if (size < 6 || size > 8) {
			checkPost = 'false';
			return false;
		}
		
 		if (!(isNaN(value2.charAt(0)))){ //leftmost character must be alpha character rule
			checkPost = 'false';
			return false;
  		}
		
 		if (isNaN(value2.charAt(size-3))){ //first character of inward code must be numeric rule
			checkPost = 'false';
			return false;
  		}
		
 		if (!(isNaN(value2.charAt(size-2)))){ //second character of inward code must be alpha rule
			checkPost = 'false';
			return false;
  		}
		
 		if (!(isNaN(value2.charAt(size-1)))){ //third character of inward code must be alpha rule
			checkPost = 'false';
			return false;
  		}
		
		count1 = value2.indexOf(" ");
		count2 = value2.lastIndexOf(" ");
		
		if (count1 != count2) {
			checkPost = 'false';
			return false;
	 	}
	 	else {
			checkPost = "true";
			return true;
		}
	 }
	
	
		
	/*  locate the starting address using the Ajax API, which finds the postcode from Google and places the point
	
	/*	Geocoding UK Postcodes with Google Map API <http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/>
		Copyright (c) 2007 Tom Anthony
	*/
	
	/*Some elements of code have been modified by Axminster Tool Centre Ltd	*/

	function placePoint(postcode, callbackFunction, locale) {
	
	localSearch.setSearchCompleteCallback(null, 
		function() {
			
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point, locale);
			} else{
				document.getElementById('directions').innerHTML = "<p>Post code not found.</p>";
			}
		});	
		
	localSearch.execute(postcode + ", UK");
	}
	
	//complete the search for the directions using the start, getAddr method and the prefix (en)
	function dealer_search (point, locale) {
				
		var start = (point.lat() + ', ' + point.lng());
		setDirections(start, getAddr(), locale);
	
	}

	addLoadEvent(mapLoad);
	addUnLoadEvent(GUnload);
//]]>