    var map = null;
    var geocoder = null;

    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(53.799637, -1.54911), 15);
		var mapTypeControl = new GMapTypeControl();
        var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
        var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
        map.addControl(mapTypeControl, topRight);
		map.addControl(new GSmallMapControl());
        geocoder = new GClientGeocoder();
      }
	  address = document.form.Address1.value+', '+document.form.Address2.value+', '+document.form.City.value+', '+document.form.County.value+', '+document.form.Postcode.value;
	  postcode = document.form.Postcode.value;
	  showAddress(address, postcode);
    }
	
	function setlocal(lat1, long1) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(lat1, long1), 15);
		var mapTypeControl = new GMapTypeControl();
        var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
        var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
        map.addControl(mapTypeControl, topRight);
		map.addControl(new GSmallMapControl());
        geocoder = new GClientGeocoder();
		var marker = new GMarker(new GLatLng(lat1, long1),{draggable: true});
			   GEvent.addListener(marker, "dragstart", function() {
        	  map.closeInfoWindow();
       			 });
	
       		  GEvent.addListener(marker, "dragend", function() {
          	  //marker.openInfoWindowHtml();
			  document.form.lat.value = marker.getPoint().lat();
		 	  document.form.long.value = marker.getPoint().lng();
      		  });
			  marker.trackMarkers = true;
              map.addOverlay(marker);
			  document.form.lat.value = marker.getPoint().lat();
		 	  document.form.long.value = marker.getPoint().lng();
              //marker.openInfoWindowHtml();
      }
	  
    }

	function initialize_home() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(53.799637, -1.54911), 15);
		var mapTypeControl = new GMapTypeControl();
        var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
        var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
        map.addControl(mapTypeControl, topRight);
		map.addControl(new GSmallMapControl());
        geocoder = new GClientGeocoder();
      }
	  
    }
	
    function showAddress(address, postcode) {
		
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
				if (postcode=='done'){
						   alert(address + " not found");
				} else {
					
              showAddress(postcode,'done');
				}
            } else {
              map.setCenter(point, 15);
              var marker = new GMarker(point,{draggable: true});
			   GEvent.addListener(marker, "dragstart", function() {
        	  map.closeInfoWindow();
       			 });
	
       		  GEvent.addListener(marker, "dragend", function() {
          	  marker.openInfoWindowHtml(address);
		 	  document.form.lat.value = marker.getPoint().lat();
		 	  document.form.long.value = marker.getPoint().lng();
      		  });
			  marker.trackMarkers = true;
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
			  document.form.lat.value = marker.getPoint().lat();
		 	  document.form.long.value = marker.getPoint().lng();
            }
          }
        );
      }
    }
	
	function showAddress_home() {
		document.form.SubmitBut.disabled = false;
		address = document.form.Address1.value+', '+document.form.Address2.value+', '+document.form.City.value+', '+document.form.County.value+', '+document.form.Postcode.value;
	  postcode = document.form.Postcode.value;
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
				if (postcode=='done'){
						   alert(address + " not found");
				} else {
					
              showAddress(postcode,'done');
				}
            } else {
              map.setCenter(point, 15);
              var marker = new GMarker(point,{draggable: true});
			   GEvent.addListener(marker, "dragstart", function() {
        	  map.closeInfoWindow();
       			 });
	
       		  GEvent.addListener(marker, "dragend", function() {
          	  marker.openInfoWindowHtml(address);
		 	  document.form.lat.value = marker.getPoint().lat();
		 	  document.form.long.value = marker.getPoint().lng();
      		  });
			  marker.trackMarkers = true;
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
			  document.form.lat.value = marker.getPoint().lat();
		 	  document.form.long.value = marker.getPoint().lng();
            }
          }
        );
      }
	 
    }
	
	
	function PlaceHomeMarker(lat,  lng,unbounded){
		var point = new GLatLng(lat,lng);
		var marker = new GMarker(point);
		map.addOverlay(marker);
	}
	// distance calculator
	function showAddress_dc(address, postcode) {
		
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
				if (postcode=='done'){
						   alert(address + " not found");
				} else {
					
              showAddress(postcode,'done');
				}
            } else {
              map.setCenter(point, 15);
              var marker = new GMarker(point,{draggable: true});
			   GEvent.addListener(marker, "dragstart", function() {
        	  map.closeInfoWindow();
       			 });
	
       		  GEvent.addListener(marker, "dragend", function() {
          	  marker.openInfoWindowHtml(address);

      		  });
			  marker.trackMarkers = true;
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);

            }
          }
        );
      }
    }
	
	function getdistance(id,lat1,long1, lat2,long2,distancelist){

    //  directionsPanel = document.getElementById("route");
     // directions = new GDirections(map, directionsPanel);
	  var point1 = new GLatLng(lat1,long1,true);
	   var point2 = new GLatLng(lat2,long2,true);
	  //directions.load(point2); // directs to
	  distancelist.push(new Array(point1.distanceFrom(point2),id));
	  return distancelist;
    }
	
	function getdistanceSearch(id,lat1,long1, lat2,long2,SearchResults){

    //  directionsPanel = document.getElementById("route");
     // directions = new GDirections(map, directionsPanel);
	  var point1 = new GLatLng(lat1,long1,true);
	   var point2 = new GLatLng(lat2,long2,true);
	  //directions.load(point2); // directs to
	  SearchResults.push(new Array(point1.distanceFrom(point2),id));
	  return SearchResults;
    }
	
	
function usePointFromPostcode(callbackFunction) {
  var localSearch = new GlocalSearch();
  var postcode = document.SearchForm.Postcode.value;
  if(postcode!=''){
  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);
		document.SearchForm.resultLat.value = resultLat;
		document.SearchForm.resultLng.value = resultLng;
        callbackFunction(point);
      }else{
        alert("Postcode not found!");
      }
	
    });  
  }
  localSearch.execute(postcode + ", UK");
}



