	var map;
    var geocoder;
	
	// initial start point for the map
	//var startLat = 50.725233;
	//var startLong = -3.532565;
	//var startZoom = 15;
	// bounding box for preferred area when geocoding
	//var geocodeBoundsSWLat = 50.670789;
	//var geocodeBoundsSWLong = -3.629608;
	//var geocodeBoundsNELat = 50.764693;
	//var geocodeBoundsNELong = -3.401641;
	
    function loadMapForEditing() {
      	if (document.getElementById("mapediting")) {
		  	// create the map and set it up with start position and controls
		  	map = new GMap2(document.getElementById("mapediting"));
      		map.setCenter(new GLatLng(startLat, startLong), currentZoom);
	  		map.addControl(new GSmallMapControl());
      		map.addControl(new GMapTypeControl());
	  
	  		// create the geocoder and set it up with a viewport
      		geocoder = new GClientGeocoder();
	  		geocoder.setBaseCountryCode(geocodeBaseCountryCode);
	  		geocoder.setCache(null);
	  		//geocodeBoundsSW = new GLatLng(geocodeBoundsSWLat, geocodeBoundsSWLong);
	  		//geocodeBoundsNE = new GLatLng(geocodeBoundsNELat, geocodeBoundsNELong);
	  		//geocodeBounds = new GLatLngBounds(geocodeBoundsSW, geocodeBoundsNE);
	  		//geocoder.setViewport(geocodeBounds);
			
			// if there's already a marker, recreate it
			/*
			if ((currentLat != 0) && (currentLong != 0)) {
    	    	point = new GLatLng(currentLat, currentLong);
    			map.clearOverlays();
    			marker = new GMarker(point);
				map.addOverlay(marker);
			}
			*/			
			
			// if there's already a marker, recreate it
			if ((marker_type != 'n') && (markerLat[0] != 0) && (markerLong[0] != 0)) {
    			map.clearOverlays();
    	    	
    	    	var i=0;
    	    	while ((markerLat[i] != null) && (markerLong[i] != null)) {
    	    	    point = new GLatLng(markerLat[i], markerLong[i]);
        			marker = new GMarker(point);
    	    		marker.click = null;
	    			map.addOverlay(marker);
		    		if (markerCaption[i] != "") {
	    	    		//marker.openInfoWindowHtml(markerCaption[i]);
	    	    		//marker.bindInfoWindowHtml(markerCaption[i]);
		  		    }
		  		    i++;
		  		}
			}			

			// sort out the type of map to show
			if (currentType == 0) {
				map.setMapType(G_NORMAL_MAP);
			} else if (currentType == 1) {
				map.setMapType(G_SATELLITE_MAP);
			} else if (currentType == 2) {
				map.setMapType(G_HYBRID_MAP);
			}

	  		// what happens when the user clicks on the map (when it's set to allow a single marker)...
			if (marker_type == 's')  {
    	  		GEvent.addListener(map, "click", function(marker, point) {
	    			map.clearOverlays();
    	    		marker = new GMarker(point);
			    	map.addOverlay(marker);
				    //if ((marker_caption) && (marker_caption != "")) {
    	    		//	marker.openInfoWindowHtml("<div class=\"googlemapinfowindow\">" + marker_caption + "</div>");
	    	  		//}
		    		
			    	// ikg - store the values
				    markerLat[0] = marker.getPoint().lat();
    				markerLong[0] = marker.getPoint().lng();
	    			//alert(currentLat);
		    		//alert(currentLong);
			    	
				    saveMapSettings();
	  		    });
	  		}

	  		// what happens when the user drags the map...
	  		GEvent.addListener(map, "dragend", function(marker, point) {
				saveMapSettings();
	  		});

	  		// what happens when the user zooms the map...
	  		GEvent.addListener(map, "zoomend", function(marker, point) {
				saveMapSettings();
	  		});

	  		// what happens when the user changes the map type...
	  		GEvent.addListener(map, "maptypechanged", function(marker, point) {
				saveMapSettings();
	  		});
		}
	}

    function loadMapForDisplay() {
      	if (document.getElementById("mapdisplay")) {
		  	// create the map and set it up with start position and controls
		  	map = new GMap2(document.getElementById("mapdisplay"));

		  	var ct = new GLatLng(startLat, startLong);
      		map.setCenter(ct, currentZoom);
	  		
			// add the controls, depending on what's been requested
			if (controls == "all") {
				map.addControl(new GSmallMapControl());
	      		map.addControl(new GMapTypeControl());
			} else if (controls == "zoom") {
				map.addControl(new GSmallMapControl());
			} else if (controls == "view") {
	      		map.addControl(new GMapTypeControl());
			}
	  
			// if there's already a marker, recreate it
			if ((marker_type != 'n') && (markerLat[0] != 0) && (markerLong[0] != 0)) {
    			map.clearOverlays();
    	    
    	    	var i=0;
    	    	while ((markerLat[i] != null) && (markerLong[i] != null)) {
    	    	    point = new GLatLng(markerLat[i], markerLong[i]);
        			marker = new GMarker(point);
	    			map.addOverlay(marker);
		    		if (markerCaption[i] != "") {
	    	    		//marker.openInfoWindowHtml(markerCaption[i]);
	    	    		marker.bindInfoWindowHtml(markerCaption[i]);
		  		    }

		  		    i++;
		  		}
			}			
			
			// sort out the type of map to show
			if (currentType == 0) {
				map.setMapType(G_NORMAL_MAP);
			} else if (currentType == 1) {
				map.setMapType(G_SATELLITE_MAP);
			} else if (currentType == 2) {
				map.setMapType(G_HYBRID_MAP);
			}
			
			// sort out dragging (or not)
			if (draggable == 1) {
				map.enableDragging();
			} else {
				map.disableDragging();
			}
			
			document.getElementById("mapdisplay").style.display = "block";
		}
	}

	
    // addAddressToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
    function addAddressToMap(response) {
      	map.clearOverlays();
      	if (!response || response.Status.code != 200) {
       	 	alert(lang_mapnoaddress);
      	} else {
        	place = response.Placemark[0];
        	point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        	marker = new GMarker(point);
        	map.setCenter(point, currentZoom);
        	map.addOverlay(marker);
        	//marker.openInfoWindowHtml('<b>Latitude:</b> ' + place.Point.coordinates[1] + '<br>' + 
			//  '<b>Longitude:</b> ' + place.Point.coordinates[0]);

			// ikg - store the values
			markerLat[0] = place.Point.coordinates[1];
			markerLong[0] = place.Point.coordinates[0];

			saveMapSettings();
      	}
    }

    // addLatLngToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
    function addLatLngToMap(response) {
      	map.clearOverlays();
      	if (!response) {
      	    alert(lang_mapnoaddress);
      	} else {
        	point = new GLatLng(response.lat(),
                            response.lng());
        	marker = new GMarker(point);
        	map.setCenter(point, currentZoom);
        	map.addOverlay(marker);
        	//marker.openInfoWindowHtml('<b>Latitude:</b> ' + place.Point.coordinates[1] + '<br>' + 
			//  '<b>Longitude:</b> ' + place.Point.coordinates[0]);

			// ikg - store the values
			markerLat[0] = response.lat();
			markerLong[0] = response.lng();
			//alert(currentLat);
			//alert(currentLong);

			saveMapSettings();
      	}
    }

	// code adapted from http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/
	// gets around problems with geocoding UK postcodes
	function usePointFromPostcode(postcode, callbackFunction) {
	    var localSearch = new GlocalSearch();
	  	localSearch.setSearchCompleteCallback(null, function() {
	      	map.clearOverlays();
	     	if (localSearch.results[0]) {    
	        	point = new GLatLng(localSearch.results[0].lat,
    	                        localSearch.results[0].lng);
        		marker = new GMarker(point);
        		map.setCenter(point, currentZoom);
	        	map.addOverlay(marker);
    	    	//marker.openInfoWindowHtml('<b>Latitude:</b> ' + place.Point.coordinates[1] + '<br>' + 
				//  '<b>Longitude:</b> ' + place.Point.coordinates[0]);

				// ikg - store the values
				markerLat[0] = localSearch.results[0].lat;
				markerLong[0] = localSearch.results[0].lng;
				//alert(currentLat);
				//alert(currentLong);

				saveMapSettings();
	      	} else {
                alert(lang_mapnoaddress);
      		}
    	}
		);  
    
	  	localSearch.execute(postcode + ", " + geocodePostcodeCountry);
	}

    // showLocation() is called when you click on the Search button
    // in the form.  It geocodes the address entered into the form
    // and adds a marker to the map at that location.
    function showLocation() {
      	if (document.getElementById("txtMapSearch")) {
			if (document.getElementById("txtMapSearch").value != "") {
				var address = document.getElementById("txtMapSearch").value;
    		  	//geocoder.getLocations(address, addAddressToMap);
    		  	//geocoder.getLatLng(address, addLatLngToMap);
				 usePointFromPostcode(address, null);
			}
		}
    }
	
	function saveMapSettings() {
		//if ((currentLat != 0) && (currentLong != 0)) {
			// sort out the type of map to show
			var type = map.getCurrentMapType().getName();
			// and the current zoom 
			var zoom = map.getZoom();
		    // and the current centre position
			var centre = new GLatLng();
			centre = map.getCenter();
			// show the output (rather than writing to the db)
			//alert("Map output:\nMap latitude: " + centre.lat() + "\nMap longitude: " + centre.lng() + "\nMarker latitude: " + currentLat + "\nMarker longitude: " + currentLong + "\nType: " + type + "\nZoom: " + zoom + "\nShow controls: " + document.forms[1].opt_controls.value + "\nDraggable: " + document.forms[1].opt_draggable.value + "\nShow marker: " + document.forms[1].opt_marker.value);
			// add into the hidden fields in the page, in order to write it back to the database
			if (document.getElementById("txtMapLong")) {
				document.getElementById("txtMapLong").value = centre.lng();
			}
			if (document.getElementById("txtMapLat")) {
				document.getElementById("txtMapLat").value = centre.lat();
			}
			if ((document.getElementById("txtMapMarkerLong")) && (markerLong[0] != undefined)) {
				document.getElementById("txtMapMarkerLong").value = markerLong[0];
			}
			if ((document.getElementById("txtMapMarkerLat")) && (markerLat[0] != undefined)) {
				document.getElementById("txtMapMarkerLat").value = markerLat[0];
			}
			if (document.getElementById("txtMapType")) {
				// sort out the type of map to show
				if (type == 'Map') {
					type = 0;
				} else if (type == 'Satellite') {
					type = 1;
				} else if (type == 'Hybrid') {
					type = 2;
				}
				document.getElementById("txtMapType").value = type;
			}
			if (document.getElementById("txtMapZoom")) {
				document.getElementById("txtMapZoom").value = zoom;
			}

			// set the save alert
			setChanged(false);
			
			return true;
		//} else {
		//	alert("No place selected.");
		//	return false;
		//}
	}
	
	function clearMapSettings() {
	
	}
	

