// This Javascript is based on code provided by the Blackpool Community Church Javascript Team http://www.commchurch.freeserve.co.uk/   

// This script is a simple modification of the example found at http://econym.googlepages.com/directions.htm
// Thank you Mike Williams & the Blackpool Community Church Javascript Team!

var config = new Array();

	config['latitude'] = 51.13308703201545;
	config['longitude'] = -0.017431676387786865;
	config['name'] = 'East Grinstead Bathrooms &amp; Kitchens';
	config['info_window'] = '';
	config['zoom'] = 15;

if (GBrowserIsCompatible()) { 
	// Create marker and info window event function
	function createMarker(point,name,info) {
    	var marker = new GMarker(point,name);

		return marker;
	}
	// Create directions request
	function getDirections() { 
   		var saddr = document.getElementById('saddr').value
   	    var daddr = document.getElementById('daddr').value
        gdir.load('from: ' + saddr + ' to: ' + daddr);
    }
	// Create Google Map and load settings
    var map = new GMap2(document.getElementById('google-map'));
    map.setCenter(new GLatLng(config['latitude'],config['longitude']), config['zoom']);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());

	// Create GDirections object
	var gdir=new GDirections(map, document.getElementById("directions"));

	// Error array
    var reasons=[];
    	reasons[G_GEO_SUCCESS]            = 'Success';
    	reasons[G_GEO_MISSING_ADDRESS]    = 'Please enter your town or village';
    	reasons[G_GEO_UNKNOWN_ADDRESS]    = 'Sorry, but we were unable to locate your address - avoid postcodes and house names, check spelling and try again';
    	reasons[G_GEO_UNAVAILABLE_ADDRESS]= 'The given address cannot be returned due to legal or contractual reasons.';
    	reasons[G_GEO_BAD_KEY]            = 'This Google API key is invalid - please notify the webmaster';
    	reasons[G_GEO_TOO_MANY_QUERIES]   = 'The daily map request quota for this site has been exceeded.';
    	reasons[G_GEO_SERVER_ERROR]       = 'Server error: The geocoding request could not be successfully processed.';
    	reasons[G_GEO_BAD_REQUEST]        = 'A directions request could not be successfully parsed.';
   		reasons[G_GEO_MISSING_QUERY]      = 'Please enter your town or village';
    	reasons[G_GEO_UNKNOWN_DIRECTIONS] = 'Sorry, but the directions could not be calculated';

	// Catch direction errors
	GEvent.addListener(gdir, 'error', function() {
   		var code = gdir.getStatus().code;
       	var reason='Code ' + code;
       	if (reasons[code]) {
   	    	reason = reasons[code]
        } 
       	alert(reason);
   	});
   	// Create the marker
   	var point = new GLatLng(config['latitude'],config['longitude']);
   	var marker = createMarker(point,config['name'],config['info_window']);
   	map.addOverlay(marker);
   	// Modify form action
   	var direction_form = document.getElementById("direction-form");
   	direction_form.action = 'javascript:getDirections()';
}