
	// Update the main map
	function ShowMap() {

		if ( sLayerName != "" || sFieldName != "" || sFieldValue != "" ) {
				
			// Find out the new Easting and Northing of the selected feature
			if ( FindCoordinatesFromLayerSearch() == true ) {
		
				// Peform Layer Search
				UpdateMapLayerSearch();
			
				// Update the Geoset
				UpdateOverviewMap();
			
				document.getElementById("MapWidthTag").innerHTML = "<p align='center'><font color='#336699'>MapWidth: " + Zoom + " meters</font></p>";
				
			} else {
			
				// As there was a problem with the layer search, switch to normall search
				DefaultMap();
				
			}
			
		} else {
			// Load the default map as there is no layer search
			DefaultMap();
		}
	}
	
	// Default start map
	function DefaultMap() {
		
		// Update the Geoset
		UpdateOverviewMap();

		// Select the correct zoom level
		SelectZoomLevelIndicator();

		document.getElementById("MapWidthTag").innerHTML = "<p align='center'><font color='#336699'>MapWidth: " + Zoom + " meters</font></p>";

		document.getElementById("map").src = 'http://' + Domain + '/' + Virtual + '/getmapimage.asp?theme=' + theme + '&easting=' + Easting + '&northing=' + Northing + '&zoom=' + Zoom + '&mapwidth=' + MapWidth + '&mapheight=' + MapHeight + '&maxzoomlevel=' + Zoom + "&MinZoomLevel=" + MinZoomLevel;
			
	}
	
	function FindCoordinatesFromLayerSearch() {
	
		var xmlDoc;
		var bReturn = false;
	
		var sURL = "http://" + Domain  + "/" + Virtual;
		sURL += "/dialogbox_MapInformation.asp?theme=" + theme + "&layername=" + sLayerName + "&fieldname=" + sFieldName + "&fieldvalue=" + sFieldValue + "&mapmode=GetBounds";

		xmlDoc = createDomObject(sURL);		
		if ( xmlDoc == null ) { return; }				

		rootNode = xmlDoc.documentElement;
		
		if ( rootNode.childNodes.length > 0 ) {
		
			if ( rootNode.nodeName.toLowerCase() != "planaccesserror" ) {
				
				if (BrowserName == "Netscape") {				
					Easting = parseInt(rootNode.childNodes.item(4).firstChild.nodeValue);
				} else {
					Easting = parseInt(rootNode.childNodes.item(4).text);
				}
				
				if (BrowserName == "Netscape") {
					Northing = parseInt(rootNode.childNodes.item(5).firstChild.nodeValue);
				} else {
					Northing = parseInt(rootNode.childNodes.item(5).text);
				}
				
				if (BrowserName == "Netscape") {
					Zoom = parseInt(rootNode.childNodes.item(6).firstChild.nodeValue);
				} else {
					Zoom = parseInt(rootNode.childNodes.item(6).text);
				}
				
				bReturn = true;
				
			}
			
		}
		
		xmlDoc = null;
		return bReturn;
	}
	
	function UpdateMapLayerSearch() {
		
		sURL = "http://" + Domain + "/" + Virtual + "/GetMapImage.asp?theme=" + theme;
		sURL += "&mapheight=" + MapHeight + "&mapwidth=" + MapWidth;
		sURL += "&layername=" + sLayerName + "&FieldName=" + sFieldName + "&fieldvalue=" + sFieldValue;
		
		// Fetch the new image
		document.getElementById("map").src = sURL;
		
	}
	
	// Update the map with the new layer selection
	function UpdateMap() {

		var x = 0;
		var sURL = "";

		sURL = "http://" + Domain + "/" + Virtual + "/GetMapImage.asp?theme=" + theme;
		sURL += "&layers=" + ReturnVisibleLayers();

		// Add the hidden layercontrol layers to the GetMapImage call
		for ( x = 0; x < HiddenLayers.length; x++ ) {

			sURL += HiddenLayers[x] + ",";
		}

		sURL += "&mapheight=" + MapHeight + "&mapwidth=" + MapWidth + "&easting=" + Easting + "&northing=" + Northing + "&zoom=" + Zoom;
		sURL += "&maxzoomlevel=" + Zoom + "&MinZoomLevel=" + MinZoomLevel;

		if ( bMapPointer == true ) {
			sURL += "&arrowpointer=true";
			bMapPointer = false;
		}

		// Fetch the new image
		document.getElementById("map").src = sURL;

		// Update the Geoset
		UpdateOverviewMap();

		document.getElementById("MapWidthTag").innerHTML = "<p align=\"center\"><font color=\"#336699\">MapWidth: " + Zoom + " meters</font></p>";

	}
	
	function UpdateOverviewMap() {

		var sOverURL = '';
		OverHeight = 240;
		OverWidth = 240;

		sOverURL = "http://" + Domain + "/" + Virtual + "/GetMapImage.asp?theme=" + OverviewMap + "&arrowpointer=true"
		sOverURL += "&mapheight=" + OverHeight + "&mapwidth=" + OverWidth + "&easting=" + Easting + "&northing=" + Northing + "&zoom=" + Zoom*4;
		sOverURL += "&maxzoomlevel=" + Zoom*4;

		document.getElementById("theMap").src = sOverURL;

	}
	
	function MoveMap( iEasting, iNorthing, iZoom ) {

		Easting = iEasting;
		Northing = iNorthing;
		Zoom = iZoom;
		bMapPointer = true;
		
		// Select the correct zoom level
		SelectZoomLevelIndicator();

		UpdateMap();

	}
	
	/* Nav */
	
	function ZoomMapIn( mouseX, mouseY ) {
		
		// Calculate the bounds
		var mapX1 = Easting - ( Zoom / 2 )
		var mapX2 = Easting + ( Zoom / 2 )
		var mapY1 = Northing - Math.round(((Zoom / 2) * (MapHeight / MapWidth)));
		var mapY2 = Northing + Math.round(((Zoom / 2) * (MapHeight / MapWidth)));
		if ( parseInt(Zoom / 2) < MinZoomLevel ) {
			Zoom = MinZoomLevel;
		} else {
			Zoom = Zoom / 2;
		}
		
		Easting = Math.round((mouseX / MapWidth * (mapX2 - mapX1) + mapX1));
		Northing = Math.round(mapY2 - mouseY / MapHeight * (mapY2 - mapY1));
		
		// Select the correct zoom level
		SelectZoomLevelIndicator();
		
		UpdateMap();
	}
	
	function ZoomMapOut( mouseX, mouseY ) {
		
		// Calculate the bounds
		var mapX1 = Easting - ( Zoom / 2 )
		var mapX2 = Easting + ( Zoom / 2 )
		var mapY1 = Northing - Math.round(((Zoom / 2) * (MapHeight / MapWidth)));
		var mapY2 = Northing + Math.round(((Zoom / 2) * (MapHeight / MapWidth)));
		// Only zoom out if under the max zoom level
		if ( (Zoom * 2) < MaxZoomLevel ) {
			Zoom = Zoom * 2;
		}
			
		Easting = Math.round((mouseX / MapWidth * (mapX2 - mapX1) + mapX1));
		Northing = Math.round(mapY2 - mouseY / MapHeight * (mapY2 - mapY1));
		
		// Select the correct zoom level
		SelectZoomLevelIndicator();
		
		UpdateMap();
	}
	
	function PanCoordinates(x1, y1, x2, y2) {
		
		// Calculate the new bounds
		var mapX1 = Easting - ( Zoom / 2 )
		var mapX2 = Easting + ( Zoom / 2 )
		var mapY1 = Northing - Math.round(((Zoom / 2) * (MapHeight / MapWidth)));
		var mapY2 = Northing + Math.round(((Zoom / 2) * (MapHeight / MapWidth)));
		
		// Work out the new center X & Y
		var mapX = Math.round((x2 / MapWidth * (mapX2 - mapX1) + mapX1));
		var mapY = Math.round(mapY2 - y2 / MapHeight * (mapY2 - mapY1));
	
		var oldCentreX = (mapX2-mapX1)/2+mapX1
		var oldCentreY = (mapY1-mapY2)/2+mapY2
	
		// Calculate the new X and Y
		Easting = oldCentreX - mapX + mapX1
		Northing = oldCentreY - mapY + mapY2	
		
		UpdateMap();
	}
	
	// Calculations which move the map to the new position by generating the new BNG coordinates
	function PanMap( mouseX, mouseY ) {
		
		// Calculate the new bounds
		var mapX1 = Easting - ( Zoom / 2 )
		var mapX2 = Easting + ( Zoom / 2 )
		var mapY1 = Northing - Math.round(((Zoom / 2) * (MapHeight / MapWidth)));
		var mapY2 = Northing + Math.round(((Zoom / 2) * (MapHeight / MapWidth)));
			
		// Work out the new center X & Y
		Easting = Math.round((mouseX / MapWidth * (mapX2 - mapX1) + mapX1));
		Northing = Math.round(mapY2 - mouseY / MapHeight * (mapY2 - mapY1));
			
		UpdateMap();
	}
	
	// Pan Navigation
	function PanBut(sDirection) {

		var mouseX = 0;
		var mouseY = 0;

		switch (sDirection) {
			case 'NW':
				mouseX = Math.round(MapWidth / 4);
				mouseY = Math.round(MapHeight / 4);
				break;
			case 'NN':
				mouseX = Math.round(MapWidth / 2);
				mouseY = Math.round(MapHeight / 4);
				break;
			case 'NE':
				mouseX = Math.round(MapWidth / 4) * 3;
				mouseY = Math.round(MapHeight / 4);
				break;
			case 'WW':
				mouseX = Math.round(MapWidth / 4);
				mouseY = Math.round(MapHeight / 2);
				break;
			case 'EE':
				mouseX = Math.round(MapWidth / 4) * 3;
				mouseY = Math.round(MapHeight / 2);
				break;
			case 'SW':
				mouseX = Math.round(MapWidth / 4);
				mouseY = Math.round(MapHeight / 4) * 3;
				break;
			case 'SS':
				mouseX = Math.round(MapWidth / 2);
				mouseY = Math.round(MapHeight / 4) * 3;
				break;
			case 'SE':
				mouseX = Math.round(MapWidth / 4) * 3;
				mouseY = Math.round(MapHeight / 4) * 3;
				break;
			}

		PanMap(mouseX, mouseY);
	}
	
	function ZoomExtents() {

		Easting = dEastingExtents;
		Northing = dNorthingExtents;
		Zoom = dZoomExtents;

		UpdateMap();
	}
	
	
