/*
 * $Archive:  $
 * $Date:  $
 * $Revision:  $
 * $Author:  $
 * $History:  $
 * $NoKeywords:  $
 * 
 * Copyright Notice "Copyright (c) 2002 GeoWise Limited"
 */
// The width of the map at the maximum zoom level (in metres)
var MAX_ZOOM_WIDTH = 1200;
// The multiplier used to scale up zoom levels (see function zoomToLevel)
// Increasing this will make bigger jumps between zoom levels and make the map at zoom level 2
// cover a much bigger area. Decreasing it will have the reverse effect. A multiplier of 1 will 
// make everything the same, obviously!
var ZOOM_MULTIPLIER = 2;
// Tool tip stuff
var oldToolTip = '';
var currZoomLevel = 1;
// Map sizes
var bigMapWidth = 510;
var bigMapHeight = 370;
var smallMapWidth = 390;
var smallMapHeight = 270;
// How many times bigger is the printable map?
var printSizeMultiple = 2;
// Zoom Scales
var zoomScales = new Array(12);
// For each of these - 1:2000 is 2000, 1:50,000 is 50000 etc.
zoomScales[11] = 2000;
zoomScales[10] = 5000;
zoomScales[9] = 10000;
zoomScales[8] = 25000;
zoomScales[7] = 50000;
zoomScales[6] = 100000;
zoomScales[5] = 200000;
zoomScales[4] = 500000;
zoomScales[3] = 1000000;
zoomScales[2] = 2000000;
zoomScales[1] = 5000000;
zoomScales[0] = 9293637;
// The buttons used for each zoom level
var zoomButtons = new Array(12);
zoomButtons[11] = 'iZoom12';
zoomButtons[10] = 'iZoom11';
zoomButtons[9] = 'iZoom10';
zoomButtons[8] = 'iZoom9';
zoomButtons[7] = 'iZoom8';
zoomButtons[6] = 'iZoom7';
zoomButtons[5] = 'iZoom6';
zoomButtons[4] = 'iZoom5';
zoomButtons[3] = 'iZoom4';
zoomButtons[2] = 'iZoom3';
zoomButtons[1] = 'iZoom2';
zoomButtons[0] = 'iZoom1';
var outsideMinX = -99682.0298223816;
var outsideMinY = 5182.21991219348;
var outsideMaxX = 799599.523122382;
var outsideMaxY = 657602.170087806;
	
/**	Critical method to respond to a user action on the screen.
* Handles aal user requests and updates relevant forms, before re-submitting page.
*/
function triggerUpdate() {
	// Identify request
	if (toolStatus == 'IDENTIFY') {
		// Make up a new request for the Identify page
		document.forms['fIdentify'].elements['X'].value = mouseX;
		document.forms['fIdentify'].elements['Y'].value = mouseY;
		document.forms['fIdentify'].elements['WIDTH'].value = document.forms['mapForm'].elements['WIDTH'].value;
		document.forms['fIdentify'].elements['HEIGHT'].value = document.forms['mapForm'].elements['HEIGHT'].value;
		document.forms['fIdentify'].elements['BBOX'].value = document.forms['mapForm'].elements['BBOX'].value;
		document.forms['fIdentify'].elements['QUERY_LAYERS'].value = document.forms['mapForm'].elements['QUERY_LAYERS'].value;
		document.forms['fIdentify'].elements['LAYERS'].value = document.forms['mapForm'].elements['LAYERS'].value;
		document.forms['fIdentify'].elements['USE_BG_LAYERS'].value = document.forms['mapForm'].elements['USE_BG_LAYERS'].value;
		document.forms['fIdentify'].elements['FEATURE'].value = document.forms['mapForm'].elements['FEATURE'].value;
		document.forms['fIdentify'].elements['MAP'].value = document.forms['mapForm'].elements['MAP'].value;
		document.forms['fIdentify'].elements['SLIDE'].value = document.forms['mapForm'].elements['SLIDE'].value;
		hidePage('./images/datalogo.gif');
		document.forms['fIdentify'].submit();
	}
	// Zoom or Pan request
	else {
		hidePage();
		document.forms['mapForm'].elements['OLD_BBOX'].value = document.forms['mapForm'].elements['BBOX'].value;
		// Find the resolution of the requested area
		var resx = (rx2 - rx1) / iWidth;
		var resy = (ry2 - ry1) / iHeight;
		// Don't match, so force the y resolution to the x one
		if (resx != resy) {
			resy = resx;
			var cy = ((ry2 - ry1) / 2.0) + ry1;
			ry1 = cy - (resy * (iHeight / 2.0));
			ry2 = cy + (resy * (iHeight / 2.0));
		}
		var across = Math.abs(rx2 - rx1);
 		var down = Math.abs(ry2 - ry1);
 		// Trap the user within a scale
 		var reqScale = getScale(across, iWidth);
 		if (reqScale > zoomScales[0]) {
 			if (document.forms['mapForm'].elements['MAP'].value == 'advanced') zoomHomeAdvanced();
 			else zoomHome();
 			showPage();
 			return;
 		}
 		// Trap the user inside the big map extent (overview map)
 		//if ((across > realInsetWidth) || (down > realInsetHeight)) {
		//	rx1 = realInsetX1;
		//	ry1 = realInsetY1;
		//	rx2 = realInsetX2;
		//	ry2 = realInsetX2;
		//}
		//else 
		// Trap the user within a zooming range
		if (across < MAX_ZOOM_WIDTH) {
			// Recalculate the centre point
			var xRes = (maxx - minx) / iWidth;
			var yRes = (maxy - miny) / iHeight;
			var factor = yRes / xRes;
			var factor = (iHeight * 1.0) / iWidth;
			var centreX = rx1 + (across / 2);
			var centreY = ry1 + (down / 2);
			// Make a bounding box based on the max zoomable area
			rx1 = centreX - (MAX_ZOOM_WIDTH / 2);
			ry1 = centreY - ((MAX_ZOOM_WIDTH / 2) * factor);
			rx2 = centreX + (MAX_ZOOM_WIDTH / 2);
			ry2 = centreY + ((MAX_ZOOM_WIDTH / 2) * factor);
		}
		across = Math.abs(rx2 - rx1);
		down = Math.abs(ry2 - ry1);
		// Trap them in the area (England)
	 	if (rx2 > outsideMaxX) {
	 		rx2 = outsideMaxX;
	 		rx1 = rx2 - across;
	 	}
	 	if (rx1 < outsideMinX) {
	 		rx1 = outsideMinX;
	 		rx2 = rx1 + across;
	 	}
	 	if (ry2 > outsideMaxY) {
	 		ry2 = outsideMaxY;
	 		ry1 = ry2 - down;
	 	}
	 	if (ry1 < outsideMinY) {
	 		ry1 = outsideMinY;
 			ry2 = ry1 + down;
	 	}
	 	// Update the form and submit it
 		document.forms['mapForm'].elements['BBOX'].value = rx1 + ',' + ry1 + ',' + rx2 + ',' + ry2;
 		document.forms['mapForm'].action = './map.aspx?m=' + document.forms['mapForm'].elements['MAP'].value;
 		document.forms['mapForm'].submit();
 	}
}

function toggleLayers(cb) {
	window.setTimeout('doToggleLayers(\'' + cb.id + '\')', 20);
}

var toggledOnLayers = new Array();
var toggledOffLayers = new Array();

function doToggleLayers(cbId) {
	var currentBox = document.getElementById(cbId);
	var toggler = currentBox.value.split(';');
	var layerControls = new Array('LAYERS', 'QUERY_LAYERS');
	for (var c = 0; c < layerControls.length; c++) {
		if (currentBox.checked) {
			var cMapLayers = ',' + document.forms['mapForm'].elements[layerControls[c]].value + ',';
			if (cMapLayers.indexOf(',' + toggler[c + 1] + ',') < 0) {
				cMapLayers = document.forms['mapForm'].elements[layerControls[c]].value + ',' + toggler[c + 1];
				document.forms['mapForm'].elements[layerControls[c]].value = cMapLayers;
				toggledOnLayers.push(cbId);
			}
		}
		else {
			toggler[c + 1] = ',' + toggler[c + 1] + ',';
			var mapLayers = document.forms['mapForm'].elements[layerControls[c]].value.split(',');
			for (var i = (mapLayers.length - 1); i >= 0; i--) {
				if (toggler[c + 1].indexOf(',' + mapLayers[i] + ',') >= 0) {
					mapLayers.splice(i, 1);
					toggledOffLayers.push(cbId);
				}
			}
			document.forms['mapForm'].elements[layerControls[c]].value = mapLayers.join(',');
		}
	}
	var ei = document.forms['mapForm'].elements['EXCLUDED_LAYERS'].value.indexOf(toggler[0]);
	if (currentBox.checked && (ei >= 0)) {
		var before = (ei > 1 ? document.forms['mapForm'].elements['EXCLUDED_LAYERS'].value.substring(0, ei - 1) : ''); // remove comma too...
		var after = document.forms['mapForm'].elements['EXCLUDED_LAYERS'].value.substring(ei);
		after = (after.indexOf(',') > 0 ? after.substring(after.indexOf(',')) : '');
		document.forms['mapForm'].elements['EXCLUDED_LAYERS'].value = before + after;
	}
	else if (ei < 0) {
		document.forms['mapForm'].elements['EXCLUDED_LAYERS'].value += ',' + toggler[0];
	}
	window.clearTimeout();
	window.setTimeout('submitAfterlayerChange();', 1400);
}

function resetToggleBoxes() {
	for (var i = 0; i < toggledOnLayers.length; i++) {
		document.getElementById(toggledOnLayers[i]).checked = false;
	}
	for (var i = 0; i < toggledOffLayers.length; i++) {
		document.getElementById(toggledOffLayers[i]).checked = true;
	}
	return null;
}

function submitAfterlayerChange() {
	// Force a change to the BBOX!
	var bbox = document.forms['mapForm'].elements['BBOX'].value.split(',');
	bbox[3] = parseFloat(bbox[3]) + 0.0001; // 1mm
	document.forms['mapForm'].elements['BBOX'].value = bbox.join(',');
	hidePage();
	//window.onbeforeunload = resetToggleBoxes();
	document.forms['mapForm'].submit();
}

/** Reset the map extent to the initial size.
*/
function zoomHome() {
	// Find out the current scale
	var cs = getScale((maxx - minx), iWidth);
	if (Math.round(cs) != zoomScales[0]) {
		document.forms['mapForm'].elements['BBOX'].value = '';
		hidePage();
		document.forms['mapForm'].submit();
	}
}

/** Reset the map extent to the initial size.
*/
function zoomHomeAdvanced() {
	document.forms['mapForm'].elements['BBOX'].value = outsideMinX + ',' + outsideMinY + ',' + outsideMaxX + ',' + outsideMaxY;
	hidePage();
	document.forms['mapForm'].submit();
}

/** Update the status of the user map tools.
*/
function setToolStatus(status) {
	toolStatus = status;
	// Reset all the map tool buttons to their 'off' state
	var s = document.images['iZoomInTool'].src;
	document.images['iZoomInTool'].src = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
	document.images['iZoomInTool'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
	s = document.images['iZoomOutTool'].src;
	document.images['iZoomOutTool'].src = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
	document.images['iZoomOutTool'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
	//s = document.images['iPanTool'].src;
	//document.images['iPanTool'].src = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
	s = document.images['iIdentifyTool'].src;
	document.images['iIdentifyTool'].src = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
	document.images['iIdentifyTool'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
	// Depending on the tool status update the screen display
	if (toolStatus == 'IDENTIFY') {
		s = document.images['iIdentifyTool'].src;
		document.images['iIdentifyTool'].src = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		document.images['iIdentifyTool'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		if (document.getElementById) {
			var im = document.images['theMap'];
			if (im.alt) im.alt = 'Click for information, click+drag to pan';
			if (im.title) im.title = 'Click for information, click+drag to pan';
			im.style.cursor = 'help';
			var tip = document.getElementById('mapTip');
			if (tip) tip.innerHTML = 'Click on the map to get information about map features, or click and drag to pan map';
			currentStatusText = 'Information tool is selected';
		}
	}
	// Pan is not currently used
	else if (toolStatus == 'PAN') {
		//s = document.images['iPanTool'].src;
		//document.images['iPanTool'].src = s.substring(0, s.lastIndexOf('-')) + '-on.gif';
		if (document.getElementById) {
			var im = document.images['theMap'];
			if (im.alt) im.alt = 'Click and drag to move the map';
			if (im.title) im.title = 'Click and drag to move the map';
			im.style.cursor = 'move';
		}
	}
	else if (toolStatus == 'ZOOM_OUT') {
		s = document.images['iZoomOutTool'].src;
		document.images['iZoomOutTool'].src = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		document.images['iZoomOutTool'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		if (document.getElementById) {
			var im = document.images['theMap'];
			if (im.alt) im.alt = 'Click / click and drag to zoom out';
			if (im.title) im.title = 'Click / click and drag to zoom out';
			im.style.cursor = 'crosshair';
			var tip = document.getElementById('mapTip');
			if (tip) tip.innerHTML = 'Click on the map (or click and drag a box) to zoom out';
			currentStatusText = 'Zoom out tool is selected';
		}
	}
	else {
		s = document.images['iZoomInTool'].src;
		document.images['iZoomInTool'].src = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		document.images['iZoomInTool'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		if (document.getElementById) {
			var im = document.images['theMap'];
			if (im.alt) im.alt = 'Click / click and drag to zoom in';
			if (im.title) im.title = 'Click / click and drag to zoom in';
			im.style.cursor = 'crosshair';
			var tip = document.getElementById('mapTip');
			if (tip) tip.innerHTML = 'Click on the map (or click and drag a box) to zoom in';
			currentStatusText = 'Zoom in tool is selected';
		}
	}
	document.forms['mapForm'].elements['TOOL'].value = toolStatus;
	currentStatusText += '. ' + currentDisplayScale
	window.status = currentStatusText;
}

/** Copies form values from the main map form into the search form.
* (Allows easier page design without having to worry about updating everything on the server).
*/
function updateSearchValues() {
	document.forms['fSearch'].elements['MAP'].value = document.forms['mapForm'].elements['MAP'].value;
	document.forms['fSearch'].elements['WIDTH'].value = document.forms['mapForm'].elements['WIDTH'].value;
	document.forms['fSearch'].elements['HEIGHT'].value = document.forms['mapForm'].elements['HEIGHT'].value;
	document.forms['fSearch'].elements['SLIDE'].value = document.forms['mapForm'].elements['SLIDE'].value;
}

/** Copies form values from the one form into another form.
* The values copied are MAP, BBOX, FEATURE, WIDTH, HEIGHT, POINT (the map basics).
* (Allows easier page design without having to worry about updating everything on the server).
* @param srcFormName The name of the form to copy values FROM.
* @param dstFormName The name of the form to copy values TO.
*/
function updateMapFormValues(srcFormName, dstFormName) {
	if (document.forms[srcFormName].elements['MAP']) document.forms[dstFormName].elements['MAP'].value = document.forms[srcFormName].elements['MAP'].value;
	if (document.forms[srcFormName].elements['MAP']) document.forms[dstFormName].elements['MAP'].value = document.forms[srcFormName].elements['MAP'].value;
	if (document.forms[srcFormName].elements['UPOINT']) {
		if (document.forms[dstFormName].elements['POINT']) document.forms[dstFormName].elements['POINT'].value = document.forms[srcFormName].elements['UPOINT'].value;
		if (document.forms[dstFormName].elements['UPOINT']) document.forms[dstFormName].elements['UPOINT'].value = document.forms[srcFormName].elements['UPOINT'].value;
	}
	if (document.forms[srcFormName].elements['POINT']) {
		if (document.forms[dstFormName].elements['POINT']) document.forms[dstFormName].elements['POINT'].value = document.forms[srcFormName].elements['POINT'].value;
		if (document.forms[dstFormName].elements['UPOINT']) document.forms[dstFormName].elements['UPOINT'].value = document.forms[srcFormName].elements['POINT'].value;
	}
	document.forms[dstFormName].elements['FEATURE'].value = document.forms[srcFormName].elements['FEATURE'].value;
	document.forms[dstFormName].elements['BBOX'].value = document.forms[srcFormName].elements['BBOX'].value;
	document.forms[dstFormName].elements['WIDTH'].value = document.forms[srcFormName].elements['WIDTH'].value;
	document.forms[dstFormName].elements['HEIGHT'].value = document.forms[srcFormName].elements['HEIGHT'].value;
}

/** Update the size of the map.
* Preserves the scale of the map by changing the bounding box.
* @param sz One of 'small' or 'big'
*/
function setMapSize(sz) {
	if (sz == 'small') {
		var cx = ((maxx - minx) / 2) + minx;
		var resx = (maxx - minx) / iWidth;
		var cy = ((maxy - miny) / 2) + miny;
		var resy = (maxy - miny) / iHeight;
		rx1 = cx - ((smallMapWidth / 2) * resx);
		ry1 = cy - ((smallMapHeight / 2) * resy);
		rx2 = cx + ((smallMapWidth / 2) * resx);
		ry2 = cy + ((smallMapHeight / 2) * resy);
		document.forms['mapForm'].elements['BBOX'].value = rx1 + ',' + ry1 + ',' + rx2 + ',' + ry2;
		document.forms['mapForm'].elements['WIDTH'].value = smallMapWidth;
		document.forms['mapForm'].elements['HEIGHT'].value = smallMapHeight;
		zoomScales[0] = 9293637;
		//var s = document.images['iSmallerMap'].src;
		//document.images['iSmallerMap'].src = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		//document.images['iSmallerMap'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		//s = document.images['iBiggerMap'].src;
		//document.images['iBiggerMap'].src = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
		//document.images['iBiggerMap'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
	}
	else {
		var cx = ((maxx - minx) / 2) + minx;
		var resx = (maxx - minx) / iWidth;
		var cy = ((maxy - miny) / 2) + miny;
		var resy = (maxy - miny) / iHeight;
		rx1 = cx - ((bigMapWidth / 2) * resx);
		ry1 = cy - ((bigMapHeight / 2) * resy);
		rx2 = cx + ((bigMapWidth / 2) * resx);
		ry2 = cy + ((bigMapHeight / 2) * resy);
		document.forms['mapForm'].elements['BBOX'].value = rx1 + ',' + ry1 + ',' + rx2 + ',' + ry2;
	 	document.forms['mapForm'].elements['WIDTH'].value = bigMapWidth;
		document.forms['mapForm'].elements['HEIGHT'].value = bigMapHeight;
		zoomScales[0] = 6781843;
		//var s = document.images['iBiggerMap'].src;
		//document.images['iBiggerMap'].src = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		//document.images['iBiggerMap'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		//s = document.images['iSmallerMap'].src;
		//document.images['iSmallerMap'].src = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
		//document.images['iSmallerMap'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
	}
	hidePage();
	document.forms['mapForm'].submit();
}

/** Toggles the background layers on and off.
*/
function toggleBackground() {
	if ((document.forms['mapForm'].elements['USE_BG_LAYERS'].value == 'TRUE') ||
		(document.forms['mapForm'].elements['USE_BG_LAYERS'].value == ''))
		document.forms['mapForm'].elements['USE_BG_LAYERS'].value = 'FALSE';
	else document.forms['mapForm'].elements['USE_BG_LAYERS'].value = 'TRUE';
	hidePage();
	document.forms['mapForm'].submit();
}

/** Hides the 'page loading' dialog and shows the map beneath.
*/
function showPage() {
	// Internet Explorer 5+ or Netscape 6
  	if (document.getElementById) {
	   //document.getElementById('mapPage').style.visibility = 'visible';
	   document.getElementById('wait').style.visibility = 'hidden';
	   if (document.getElementById('glass')) document.getElementById('glass').style.visibility = 'hidden';
	}
	// Internet Explorer 5-
	else if (document.all) {
		//document.all['mapPage'].style.visibility = 'visible';  
	    document.all['wait'].style.visibility = 'hidden';  
	    if (document.all['glass']) document.all['glass'].style.visibility = 'hidden';  
	}
	// Netscape 4.7
	else if (document.layers) {
		//if (document.layers['glass']) document.layers['glass'].visibility = 'hide';
	    document.layers['wait'].visibility = 'hide';
	}
}      
  	
/** Hides the 'page loading' dialog and hides the map beneath.
*/
function hidePage(imgSrc) {
  	// Internet Explorer 5+ or Netscape 6
	if (document.getElementById) {
		if (document.getElementById('glass')) {
			document.getElementById('glass').style.visibility = 'visible';
		}
		if (imgSrc && document.getElementById('loadingImage')) {
			document.getElementById('loadingImage').src = imgSrc;
			document.getElementById('loadingImage').setAttribute('src', imgSrc);
		}
		else if (document.getElementById('loadingImage')) {
			document.getElementById('loadingImage').src = './images/logo.gif';
		}
		document.getElementById('wait').style.visibility = 'visible';
		document.getElementById('wait').style.left = hspc + (iWidth / 2) - 67;
		document.getElementById('wait').style.top = vspc + (iHeight / 2) - 49;
	}
	// Internet Explorer 5-
	else if (document.all) {
		if (document.all['glass']) document.all['glass'].style.visibility = 'visible';
		document.all['wait'].style.visibility = 'visible';
		document.all['wait'].style.left = hspc + (iWidth / 2) - 67;
		document.all['wait'].style.top = vspc + (iHeight / 2) - 49;
	}
	// Netscape 4.7
	else if (document.layers) {
		document.layers['wait'].visibility = 'show';
		if (document.layers['glass']) document.layers['glass'].visibility = 'show';
		document.layers['wait'].moveTo(hspc + (iWidth / 2) - 67,  vspc + (iHeight / 2) - 49);
	}
}

/** Zoom in or out by a specified amount.
* @param direction One of 'ZOOM_IN' or 'ZOOM_OUT'
*/	
function clickZoom(direction) {
	if (direction == 'ZOOM_IN') {
		var nz = currZoomLevel + 1;
		zoomToLevel((nz == 2 ? 3 : nz)); // Ignore 2 - special case!
	}
	else if (direction == 'ZOOM_OUT') {
		var nz = currZoomLevel - 1;
		zoomToLevel((nz == 2 ? 1 : nz)); // Ignore 2 - special case!
	}
}
	
/** Find the correct scale display element in the page and make it obvious to 
* the user.
*/	
function findScaleButton() {
	var across = Math.abs(maxx - minx);
	var widthInInches = iWidth / 97.6925;
	var xscale = across / iWidth;
	var realWidthInInches = iWidth * xscale * 3.2808 * 12;
	// Rounded because we got silly double precision errors!
	var mapScale = Math.round(realWidthInInches / widthInInches);
	var got = false;
	for (var i = zoomScales.length - 1; i >= 0; i--) {
		if (mapScale <= zoomScales[i]) {
			currZoomLevel = i + 1;
			var imgId = zoomButtons[i];
			var img = document.images[imgId];
			if (img) {
				var s = img.src;
				img.src = s.substring(0, s.indexOf('-')) + '-on.gif';
				got = true;
				break;
			}
		}
	}
}

/** Finds the required measurment for a given scale.
*	Used to get the required width or height (in map units) at a specific scale.
*	@param requiredScale The scale that should be used e.g. 100000 for 1:100000
*	@param sizeInPixels The size of the object in pixels (e.g. the map size in pixels)
*	@return The extent in map units that corresponds to the given extent at the given scale
*/
function getScaleExtent(requiredScale, sizeInPixels) {
	// Pixels per inch = 97.6925, inches per foot = 12, feet per metre = 3.2808
	var widthInInches = sizeInPixels / 97.6925;
	var realWidthInInches = requiredScale * widthInInches;
	var requiredWidth = (realWidthInInches / 12.0) / 3.2808;
	return requiredWidth;
}
			
/**	Zooms the map to a specific scale level.
*	The zoom levels are set by global variables above - MAX_ZOOM_WIDTH and ZOOM_MULTIPLIER.
*	At zoom level 8 the map will be MAX_ZOOM_WIDTH across.
*	At zoom level 2 the map will be MAX_ZOOM_WIDTH * ZOOM_MULTIPLIER^(8 - 2).
*	i.e. if the width at zoom level 8 is 400m and the ZOOM_MULTIPLIER is 2 (the default), 
*	then the width at zoom level 3 will be 51200m - 400 * 2^5 or 400 * 2 * 2 * 2 * 2 * 2 
*	@param zoom The zoom level - between 1 (the full extent) and 8 (the maximum zoom or smallest extent)
*/
function zoomToLevel(zoom) {
	var level;
	if ((zoom < 1) || (zoom == 1)) {
		if (document.forms['mapForm'].elements['MAP'].value == 'advanced') 
			zoomHomeAdvanced();
 		else 
 			zoomHome();
		return;
	}
	else {
		if (zoom > 12) zoom = 12;
		// Move from 1 based to 0 based
		zoom = Math.max(0, zoom - 1);
		var extent = getExtent(zoomScales[zoom]);
		// Get the centre of the main map.
		var mapCenterX = minx + ((maxx - minx)/2);
		var mapCenterY = miny + ((maxy - miny)/2); 
		// Reset the bbox coords.
		// These will be rejigged by triggerUpdate to preserve screen resolution.
		rx1 = mapCenterX - (extent / 2); 
		rx2 = mapCenterX + (extent / 2); 
		ry1 = mapCenterY - (extent / 2); 
		ry2 = mapCenterY + (extent / 2); 
	}
	toolStatus = 'ZOOM_IN';
	triggerUpdate();
}

function getExtent(scale) {
	var widthInInches = iWidth / 97.6925;
	var widthInMetres = (widthInInches / 12) / 3.2808;
	var requiredWidthInMetres = scale * widthInMetres;
	return requiredWidthInMetres;
}

function getScale(mapWidthInUnits, mapWidthInPixels) {
	// Pixels per inch = 97.6925, inches per foot = 12, feet per metre = 3.2808
	var widthInInches = mapWidthInPixels / 97.6925;
	var xscale = mapWidthInUnits / mapWidthInPixels;
	var realWidthInInches = mapWidthInPixels * xscale * 3.2808 * 12;
	var mapScale = realWidthInInches / widthInInches;
	return mapScale;
}
	
/** Updates the printing form and submits it, taking the user to the print preview page.
*/
function printMap() {
	if (document.forms['fPrint'].elements['PMULTIPLIER']) document.forms['fPrint'].elements['PMULTIPLIER'].value = printSizeMultiple;
	document.forms['fPrint'].elements['WIDTH'].value = printSizeMultiple * parseInt(document.forms['mapForm'].elements['WIDTH'].value);
	document.forms['fPrint'].elements['HEIGHT'].value = printSizeMultiple * parseInt(document.forms['mapForm'].elements['HEIGHT'].value);
	document.forms['fPrint'].elements['BBOX'].value = document.forms['mapForm'].elements['BBOX'].value;
	document.forms['fPrint'].elements['MAP'].value = document.forms['mapForm'].elements['MAP'].value;
	document.forms['fPrint'].elements['FEATURE'].value = document.forms['mapForm'].elements['FEATURE'].value;
	document.forms['fPrint'].elements['UPOINT'].value = document.forms['mapForm'].elements['UPOINT'].value;
	document.forms['fPrint'].elements['USE_BG_LAYERS'].value = document.forms['mapForm'].elements['USE_BG_LAYERS'].value;
	document.forms['fPrint'].submit();
}
	
/** Updates the form values that relate to the currently selected tool
* and updates any screen decoration that depends on the current tool.
*/
function checkUserTools() {
	if (!toolStatus || (toolStatus == '')) {
		toolStatus = 'IDENTIFY';
	}
	// No longer need to do the tool status stuff for the advanced map
	/*if ((document.forms['mapForm'].elements['MAP'].value != '') && 
			(document.forms['mapForm'].elements['MAP'].value.indexOf('advanced') == 0)) {
		setToolStatus(toolStatus);
	}
	// Otherwise we are defaulting to Identify tool
	else {*/
		if (document.getElementById) {
			var im = document.images['theMap'];
			if (im.alt) im.alt = 'Click for information, click+drag to pan';
			if (im.title) im.title = 'Click for information, click+drag to pan';
			im.style.cursor = 'help';
			var tip = document.getElementById('mapTip');
			if (tip) tip.innerHTML = 'Click on the map to get information about map features, or click and drag to pan map';
			currentStatusText = 'Information tool is selected';
		}
		currentStatusText += '. ' + currentDisplayScale
		window.status = currentStatusText;
	/*}*/
	document.forms['mapForm'].elements['TOOL'].value = toolStatus;
	document.forms['mapForm'].elements['USE_BG_LAYERS'].value = backgroundOn;
	// Update the appearance of some page elements
	var x = parseInt(document.forms['mapForm'].elements['WIDTH'].value);
	if (x == bigMapWidth) {
		//var s = document.images['iBiggerMap'].src;
		//document.images['iBiggerMap'].src = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		//document.images['iBiggerMap'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		//s = document.images['iSmallerMap'].src;
		//document.images['iSmallerMap'].src = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
		//document.images['iSmallerMap'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
		// Resize the legend
		var lsize = 315 + bigMapWidth - smallMapWidth;
		if (usingThematic && (document.forms['mapForm'].elements['MAP'].value.indexOf('advanced') == 0)) {
			lsize = 100 + bigMapWidth - smallMapWidth;
		}
		if (document.getElementById) {
			if (document.getElementById('legendHolder')) {
				document.getElementById('legendHolder').style.height = lsize + 'px';
			}
		}
		else if (document.all) {
			if (document.all['legendHolder']) {
				document.all['legendHolder'].style.height = lsize + 'px';
			}
		}
	}
	else {
		//var s = document.images['iBiggerMap'].src;
		//document.images['iBiggerMap'].src = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
		//document.images['iBiggerMap'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_off.gif';
		//s = document.images['iSmallerMap'].src;
		//document.images['iSmallerMap'].src = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		//document.images['iSmallerMap'].oSrc = s.substring(0, s.lastIndexOf('_')) + '_over.gif';
		var lsize = 315;
		if (usingThematic && (document.forms['mapForm'].elements['MAP'].value.indexOf('advanced') == 0)) {
			lsize = 281;
		}
		else if (document.forms['mapForm'].elements['MAP'].value.indexOf('advanced') == 0) {
			lsize = 338;
		}
		else if (document.forms['mapForm'].elements['MAP'].value.indexOf('aes') == 0) {
			lsize = 281;
		}
		else if (document.forms['mapForm'].elements['MAP'].value.indexOf('int') == 0) {
			lsize = 293;
		}
		// Resize the legend
		if (document.getElementById) {
			if (document.getElementById('legendHolder')) {
				document.getElementById('legendHolder').style.height = lsize + 'px';
			}
		}
		else if (document.all) {
			if (document.all['legendHolder']) {
				document.all['legendHolder'].style.height = lsize + 'px';
			}
		}
	}
}
	
/** Reset the map boundary to the previous zoomed or panned	area.
*/
function zoomToPrevious() {
	var tmp = document.forms['mapForm'].elements['BBOX'].value;
	if (tmp == document.forms['mapForm'].elements['OLD_BBOX'].value) {
		alert('You cannot go back to the previous extent from here,\n' +
				'because you are either zoomed all the way out (showing\n' +
				'all of England) or you reached this map from a search\n' + 
				'or by clicking on a link in another page.\n\n' +
				'Use the browser\'s "Back" button if you want to get back\n' +
				'to the previous page.');
		return;
	}
	document.forms['mapForm'].elements['BBOX'].value = document.forms['mapForm'].elements['OLD_BBOX'].value;
	document.forms['mapForm'].elements['OLD_BBOX'].value = tmp;
	hidePage();
	document.forms['mapForm'].submit();
}
	
/** More modern overview map stuff - better for IE5+ and Firefox/NN6+
	*/
function createOverviewLocationBox(x, y, w, h, hex) {
	if ((typeof(document.getElementById) != 'undefined') && (typeof(document.createElement) != 'undefined')) {
		try {
			var holder = document.getElementById('overviewMapPanel');
			var locationBox = document.createElement('div');
			locationBox.setAttribute('id', 'placerBox');
			holder.appendChild(locationBox);
			locationBox.style.position = 'absolute';
			locationBox.style.left = x + 'px';
			locationBox.style.top = y + 'px';
			locationBox.style.width = w + 'px';
			locationBox.style.height = h + 'px';
			locationBox.style.border = 'solid 2px ' + hex;
			var i = document.createElement('img');
			locationBox.appendChild(i);
			i.setAttribute('src', './images/pixel.gif');
			i.setAttribute('alt', '*');
			i.setAttribute('width', (w - 2));
			i.setAttribute('height', (h - 2));
			return true;
		}
		catch (ex) {
			alert(ex); // debug
			return false;
		}
	}
	else
		return false;
}