/*
custommap.js
Scripts to control OpenLayers map functionality for MI custom map tool
Sept 2009
*/

//----------------------Globals
var map;
var vlayer;					//Layer for map extents
var rlayer;					//Layer for radials
var baselayer;					//Layer for basemapping
var polygonExtentFeature;
var polygonRadialFeature = new Array();
var drawFeatureControl;		//Control that handels drawing features
var modifyControl;			//Control that handels modifying features
var zoomScales = new Array(6200000,1100000,610000,279000,113000,30000,11450,2850);

OpenLayers.Feature.Vector.style['default']['strokeWidth'] = '3';		//Define vector feature styles
OpenLayers.Feature.Vector.style['default']['fillColor'] = 'red';
OpenLayers.Feature.Vector.style['default']['strokeColor'] = 'red';
OpenLayers.Feature.Vector.style['default']['fillOpacity'] = '0.3';
OpenLayers.Feature.Vector.style['default']['graphicOpacity'] = '1';
OpenLayers.Feature.Vector.style['select']['strokeWidth'] = '3';
OpenLayers.Feature.Vector.style['select']['fillColor'] = 'red';
OpenLayers.Feature.Vector.style['select']['strokeColor'] = 'red';
OpenLayers.Feature.Vector.style['select']['fillOpacity'] = '0.3';
OpenLayers.Feature.Vector.style['select']['graphicOpacity'] = '1';

//----------------------------------------------------------------MAP SCRIPTS
//----------------------Map initialization
function init(passedCoordinates,passedRadials){
		 	 
	//Define graphic map styles
	var layer_style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
	layer_style.fillOpacity = 0.2;
	layer_style.graphicOpacity = 1;
	//Map extent
	mapextenstyle = OpenLayers.Util.extend({}, layer_style);
	mapextenstyle.strokeColor = "#CC0000";
	mapextenstyle.fillColor = "#666666";
	mapextenstyle.strokeWidth = 4;
			
	//Radial Style
	radialStyle = OpenLayers.Util.extend({}, layer_style);
	radialStyle.strokeColor = "#000000";
	radialStyle.fillColor = "none";
	radialStyle.strokeWidth = 2;

	//Define map object
	map = new OpenLayers.Map( $('map'), { controls: [], projection: 'EPSG:27700',
			units:'m',
			maxExtent: new OpenLayers.Bounds(-372577,-18568,883687,1237696),
					
			scales: zoomScales});
			//resolutions: [2184, 576, 216, 110, 75, 40, 15, 7, 3, 1]} );
			
	//Define the base layer
	baselayer = new OpenLayers.Layer.MapServer( "Base",
			"http://www.planvu.co.uk/cgi-bin/mapserv",
			{ map: '/var/www/html/mi/mi2.map',
			layers: 'uk regional mv1m mv625 mv200 mv100 mv40 mv10'}, { singleTile: true, isBaseLayer: true, transitionEffect: 'null'});
	 
		
	//Create layer for Extents to be drawn on
	vlayer = new OpenLayers.Layer.Vector( "Vector Layer" );
		
	//Create layer for radials to be drawn on
	rlayer = new OpenLayers.Layer.Vector( "Vector Layer" );	
	
	//Add layers to the ap	
	map.addLayers([baselayer, vlayer, rlayer]);
	map.fractionalZoom = false;

	//Define initial map extent
	var bounds = new OpenLayers.Bounds(-372577,-18568,883687,1237696);
	if (!map.getCenter()) map.zoomToExtent(bounds);
	 
	//Add the necessary controls
	//map.addControl(new OpenLayers.Control.LayerSwitcher());
	map.addControl(new OpenLayers.Control.PanZoomBar());
	//map.addControl(new OpenLayers.Control.MouseToolbar());
	//map.addControl(new OpenLayers.Control.MousePosition());
	//map.addControl(new OpenLayers.Control.NavToolbar());
	//map.addControl(new OpenLayers.Control.EditingToolbar(vlayer));
	//map.addControl(new OpenLayers.Control.Scale());
	map.addControl(new OpenLayers.Control.ScaleLine({'div':OpenLayers.Util.getElement('scalediv')}))
	map.addControl(new OpenLayers.Control.Navigation());
	 
	drawFeatureControl = new OpenLayers.Control.DrawFeature(vlayer,
				OpenLayers.Handler.RegularPolygon,
				{handlerOptions: {sides: 4}})
	  
	 map.addControl(drawFeatureControl);
	 
	 modifyControl =  new OpenLayers.Control.ModifyFeature(vlayer);	
	 map.addControl(modifyControl);

	  	  
	 //Define evets 
	//Re-Centre the map if the extent is moved
 	vlayer.events.register('featureadded', vlayer, function(evt) { 
	  if(vlayer.features.length > 0){
		  //Zoom to shape
		  var theGeometryasBounds = vlayer.features[0].geometry.getBounds();
		  var theExtentWidth = theGeometryasBounds.getWidth();
		  var theExtentHeight = theGeometryasBounds.getHeight();
		  var centreLatLon = theGeometryasBounds.getCenterLonLat();
		  var theX = centreLatLon.lon;
		  var theY = centreLatLon.lat; 
		  var blx = (theX - (theExtentWidth/2)) - (theExtentWidth/4);
		  var bly = (theY - (theExtentHeight/2)) - (theExtentHeight/4);
		  var urx = (theX + (theExtentWidth/2)) + (theExtentWidth/4);
		  var ury = (theY + (theExtentHeight/2)) + (theExtentWidth/4);
		  var bounds = new OpenLayers.Bounds(blx,bly,urx,ury);	  
		  map.zoomToExtent(bounds);
		 // document.getElementById('txtRadials').value=  '';
		 //Delete move any existing radials ???

		 //Turn pan tool back on and deactive the drawing - else will try to draw again when clicking on the tool menu	
		 document.getElementById("drawTool").src = "../images/btnDraw.png";
		 document.getElementById("moveTool").src = "../images/btnModify.png";
		 document.getElementById("panTool").src = "../images/btnPanON.png";
		 drawFeatureControl.deactivate();	 				
	  }
	}); 

	vlayer.events.register('featuremodified', vlayer, function(evt) { 
		if(vlayer.features.length > 0){
		 	thegeometryasBounds = vlayer.features[0].geometry.getBounds();
		 	var centreLatLon = thegeometryasBounds.getCenterLonLat();
			map.setCenter(centreLatLon,map.getZoom(),false,false);    
		}
	}); 
	
	vlayer.events.register('beforefeaturesadded', vlayer, function(evt) { 
		vlayer.removeFeatures(vlayer.features[0]);		
	}); 
		
	//Redraw any passed extent or radials
	if(passedCoordinates != ""){
		recreateExtent(passedCoordinates);		
		if(passedRadials != ""){
			addExtent(passedRadials, 'circle');
		}
	}
	
	map.events.registerPriority("click", map, function() {
		baselayer.transitionEffect = "null";
	})	
	map.events.registerPriority("mousedown", map, function() {
		baselayer.transitionEffect = "resize";
	})
				
}
//----------------------Draw the map extent from the passed coordinate string  
function recreateExtent(passedCoordinates){	
	vlayer.destroyFeatures()
	var pointList = [];			
	var theCoordArray = passedCoordinates.split(",");		
	for (c=0; c< theCoordArray.length ;c++){
		var theXY =  theCoordArray[c].split(" ");
		var newPoint = new OpenLayers.Geometry.Point(theXY[0],theXY[1]);
		pointList.push(newPoint);
	}					
	var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
	polygonExtentFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon([linearRing]),null,mapextenstyle);
	vlayer.addFeatures(polygonExtentFeature);
	
	//Zoom to shape
	var theGeometryasBounds = vlayer.features[0].geometry.getBounds();
	var theExtentWidth = theGeometryasBounds.getWidth();
	var theExtentHeight = theGeometryasBounds.getHeight();
	var centreLatLon = theGeometryasBounds.getCenterLonLat();
	var theX = centreLatLon.lon;
	var theY = centreLatLon.lat;	
	var blx = (theX - (theExtentWidth/2)) - (theExtentWidth/4);
	var bly = (theY - (theExtentHeight/2)) - (theExtentHeight/4);
	var urx = (theX + (theExtentWidth/2)) + (theExtentWidth/4);
	var ury = (theY + (theExtentHeight/2)) + (theExtentWidth/4);
	var bounds = new OpenLayers.Bounds(blx,bly,urx,ury);
	map.zoomToExtent(bounds);
}

//----------Activates the selected map tool	 
function activateTool(toolType){
	//Deactivate any existing controls
	drawFeatureControl.deactivate();
	modifyControl.deactivate();

	if(toolType=='draw'){
		if(polygonExtentFeature != null){		//Map extent already exists - destroy it
			vlayer.destroyFeatures();
		}
		drawFeatureControl.handler.irregular = true;
		drawFeatureControl.activate();												//Activate drawing control and update images on toolbar
		document.getElementById("drawTool").src = "../images/btnDrawON.png";
		document.getElementById("moveTool").src = "../images/btnModify.png";
		document.getElementById("panTool").src = "../images/btnPan.png";
		
	}else if (toolType=='move'){												   //Activate modify control and update images on toolbar
		modifyControl.mode = OpenLayers.Control.ModifyFeature.DRAG;
		modifyControl.mode |= OpenLayers.Control.ModifyFeature.RESIZE;
		modifyControl.activate();
		document.getElementById("drawTool").src = "../images/btnDraw.png";
		document.getElementById("moveTool").src = "../images/btnModifyON.png";
		document.getElementById("panTool").src = "../images/btnPan.png";
	}else if (toolType=='pan'){
		document.getElementById("drawTool").src = "../images/btnDraw.png";
		document.getElementById("moveTool").src = "../images/btnModify.png";
		document.getElementById("panTool").src = "../images/btnPanON.png";
	}

}
//----------Zoom map to point at specified zoom width	 
function zoomToPoint(theX,theY,zoomWidth){
	var blx = theX - (zoomWidth/2);
	var bly = theY - (zoomWidth/2);
	var urx = theX + (zoomWidth/2);
	var ury = theY + (zoomWidth/2);
	var bounds = new OpenLayers.Bounds(blx,bly,urx,ury);
	map.zoomToExtent(bounds); 
}

//----------Checks the custom extent and adds to the map
function addCustomMapSize(){
	if((document.frmSearch1.userx.value == "") || (document.frmSearch1.userx.value == "")){
		alert("Please define a custom map width and height");
	}else{
		var theSize = document.frmSearch1.userx.value + "," + document.frmSearch1.usery.value;
		addExtent(theSize, "custom");
	}
}

//----------Function to add an extent/radials to the map
function addExtent(theSize, theType){
   
	//Get the map centre
	var theMapCentre = map.getCenter(); 
	var centreX = theMapCentre.lon; 
	var centreY = theMapCentre.lat;  
 
	if(theType == "rect"){  											//ADDING FIXED MAP EXTENT
		//Remove any existing
		if(polygonExtentFeature != null){
			vlayer.destroyFeatures()
		}
	 	//Get Scale
		 if(document.frmSearch1.selectedMapScale.value.indexOf("P") != -1){
			theMapScale = Number(document.frmSearch1.selectedMapScale.value.substring(0,document.frmSearch1.selectedMapScale.value.length-1))
		 }else{
			theMapScale = Number(document.frmSearch1.selectedMapScale.value);
		 }
	 	//Get the selected units
		if(document.frmSearch1.txtMapUnits.value == "in"){
			theSize = theSize * theMapScale;
			theSize = theSize * 0.0254;
		}else if(document.frmSearch1.txtMapUnits.value == "cm"){
			theSize = theSize * theMapScale;
			theSize = theSize * 0.01;
		}else if(document.frmSearch1.txtMapUnits.value == "mm"){
			theSize = theSize * theMapScale;
			theSize = theSize * 0.001;
		}else if(document.frmSearch1.txtMapUnits.value == "km"){
			theSize = theSize * 1000; 
		}else if(document.frmSearch1.txtMapUnits.value == "mi"){
			theSize = theSize * 1609; 		
		}
	 
		var pointList = [];
		var newPoint = new OpenLayers.Geometry.Point(centreX - (theSize/2),centreY - (theSize/2));
		pointList.push(newPoint); 
		var newPoint = new OpenLayers.Geometry.Point(centreX - (theSize/2),centreY + (theSize/2));
		pointList.push(newPoint);  
		var newPoint = new OpenLayers.Geometry.Point(centreX + (theSize/2),centreY + (theSize/2));
		pointList.push(newPoint);  
		var newPoint = new OpenLayers.Geometry.Point(centreX + (theSize/2),centreY - (theSize/2));
		pointList.push(newPoint);    
		pointList.push(pointList[0]);
	  
		var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
		polygonExtentFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon([linearRing]),null,mapextenstyle);
		vlayer.addFeatures(polygonExtentFeature);
	  
		//Zoom to shape
		var theGeometryasBounds = vlayer.features[0].geometry.getBounds();
		var theExtentWidth = theGeometryasBounds.getWidth();
		var theExtentHeight = theGeometryasBounds.getHeight();
		var centreLatLon = theGeometryasBounds.getCenterLonLat();
		var theX = centreLatLon.lon;
		var theY = centreLatLon.lat; 
		var blx = (theX - (theExtentWidth/2)) - (theExtentWidth/4);
		var bly = (theY - (theExtentHeight/2)) - (theExtentHeight/4);
		var urx = (theX + (theExtentWidth/2)) + (theExtentWidth/4);
		var ury = (theY + (theExtentHeight/2)) + (theExtentWidth/4);
		var bounds = new OpenLayers.Bounds(blx,bly,urx,ury);
		map.zoomToExtent(bounds);
	
		//Clear the other options
		document.frmSearch1.userx.value = "";
		document.frmSearch1.usery.value = "";
		document.frmSearch1.txtCounty.value = "";
		document.frmSearch1.papersize.value = "";
		
		//Store the extent type (scaled or ground)		
		if((document.frmSearch1.txtMapUnits.value == "km") || (document.frmSearch1.txtMapUnits.value == "mi")){
			document.frmSearch1.extentType.value = "ground";		
		}else{
			document.frmSearch1.extentType.value = "scaled";
		}
		
	
	}else if(theType=='paper'){														//ADDING PAPER SIZE MAP EXTENT
		//Remove any existing
		if(polygonExtentFeature != null){
			vlayer.destroyFeatures()
		}
		
		var paperId = theSize;				
		//Set paper size in metres (as portrait)
		if(paperId.toLowerCase().indexOf("a5") != -1){
			xSizeMetres = 0.148; 
			ySizeMetres = 0.210;
		}else if(paperId.toLowerCase().indexOf("a4") != -1){
			xSizeMetres = 0.210; 
			ySizeMetres = 0.297;
		}else if(paperId.toLowerCase().indexOf("a3") != -1){
			xSizeMetres = 0.297; 
			ySizeMetres = 0.420;
		}else if(paperId.toLowerCase().indexOf("a2") != -1){
			xSizeMetres = 0.420; 
			ySizeMetres = 0.594;
		}else if(paperId.toLowerCase().indexOf("a1") != -1){
			xSizeMetres = 0.594; 
			ySizeMetres = 0.841;
		}else if(paperId.toLowerCase().indexOf("a02") != -1){
			xSizeMetres = 1.189; 
			ySizeMetres = 1.682; 
		}else if(paperId.toLowerCase().indexOf("a0") != -1){
			xSizeMetres = 0.841; 
			ySizeMetres = 1.189; 
		}else if(paperId.toLowerCase().indexOf("msl") != -1){		//one metre square
			xSizeMetres = 1; 
			ySizeMetres = 1; 
		}
		//Swap sizes if landscape
		if(paperId.toLowerCase().indexOf("l") != -1){
			var tempSize =	ySizeMetres
			ySizeMetres = xSizeMetres;
			xSizeMetres = tempSize;	
		}			
	 
	 	//Define the size in ground coordinates (m)
		if(document.frmSearch1.selectedMapScale.value.indexOf("P") != -1){
			theMapScale = Number(document.frmSearch1.selectedMapScale.value.substring(0,document.frmSearch1.selectedMapScale.value.length-1))
		}else{
			theMapScale = Number(document.frmSearch1.selectedMapScale.value);
		}
	 	var theXSize = xSizeMetres * theMapScale;
	 	var theYSize = ySizeMetres * theMapScale;
	 
		//Define the polygon
		var pointList = [];
		var newPoint = new OpenLayers.Geometry.Point(centreX - (theXSize/2),centreY - (theYSize/2));
		pointList.push(newPoint); 
		var newPoint = new OpenLayers.Geometry.Point(centreX - (theXSize/2),centreY + (theYSize/2));
		pointList.push(newPoint);  
		var newPoint = new OpenLayers.Geometry.Point(centreX + (theXSize/2),centreY + (theYSize/2));
		pointList.push(newPoint);  
		var newPoint = new OpenLayers.Geometry.Point(centreX + (theXSize/2),centreY - (theYSize/2));
		pointList.push(newPoint);    
		pointList.push(pointList[0]);
	  
		var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
		polygonExtentFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon([linearRing]),null,mapextenstyle);
		vlayer.addFeatures(polygonExtentFeature);
	  
		//Zoom to shape
		var theGeometryasBounds = vlayer.features[0].geometry.getBounds();
		var theExtentWidth = theGeometryasBounds.getWidth();
		var theExtentHeight = theGeometryasBounds.getHeight();
		var centreLatLon = theGeometryasBounds.getCenterLonLat();
		var theX = centreLatLon.lon;
		var theY = centreLatLon.lat; 
		var blx = (theX - (theExtentWidth/2)) - (theExtentWidth/4);
		var bly = (theY - (theExtentHeight/2)) - (theExtentHeight/4);
		var urx = (theX + (theExtentWidth/2)) + (theExtentWidth/4);
		var ury = (theY + (theExtentHeight/2)) + (theExtentWidth/4);
		var bounds = new OpenLayers.Bounds(blx,bly,urx,ury);
		map.zoomToExtent(bounds);
		
		//Clear the other options
		document.frmSearch1.userx.value = "";
		document.frmSearch1.usery.value = "";
		document.frmSearch1.txtCounty.value = "";
		document.frmSearch1.txtRadius.value = "";
		
		//Store the extent type (scaled or ground)
		document.frmSearch1.extentType.value = "scaled";

	
	}else if(theType=='custom'){													//ADDING CUSTOM SIZE MAP EXTENT
		//Remove any existing
		if(polygonExtentFeature != null){
			vlayer.destroyFeatures()
		}
		var theXY = theSize.split(",");
		//Get scale
		if(document.frmSearch1.selectedMapScale.value.indexOf("P") != -1){
			theMapScale = Number(document.frmSearch1.selectedMapScale.value.substring(0,document.frmSearch1.selectedMapScale.value.length-1))
		}else{
			theMapScale = Number(document.frmSearch1.selectedMapScale.value);
		}
		//Get the selected units
		if(document.frmSearch1.txtMapUnits2.value == "in"){
			theXY[0] = theXY[0] * theMapScale;
			theXY[0] = theXY[0] * 0.0254;
			theXY[1] = theXY[1] * theMapScale;
			theXY[1] = theXY[1] * 0.0254;
		}else if(document.frmSearch1.txtMapUnits2.value == "cm"){
			theXY[0] = theXY[0] * theMapScale;
			theXY[0] = theXY[0] * 0.01;
			theXY[1] = theXY[1] * theMapScale;
			theXY[1] = theXY[1] * 0.01;
		}else if(document.frmSearch1.txtMapUnits2.value == "mm"){
			theXY[0] = theXY[0] * theMapScale;
			theXY[0] = theXY[0] * 0.001;
			theXY[1] = theXY[1] * theMapScale;
			theXY[1] = theXY[1] * 0.001;
		}else if(document.frmSearch1.txtMapUnits2.value == "km"){
			theXY[0] = theXY[0] * 1000; 
			theXY[1] = theXY[1] * 1000; 
		}else if(document.frmSearch1.txtMapUnits2.value == "mi"){
			theXY[0] = theXY[0] * 1609; 
			theXY[1] = theXY[1] * 1609; 		
		}
	
		//Define the polygon
		var pointList = [];
		var newPoint = new OpenLayers.Geometry.Point(centreX - (theXY[0]/2),centreY - (theXY[1]/2));
		pointList.push(newPoint); 
		var newPoint = new OpenLayers.Geometry.Point(centreX - (theXY[0]/2),centreY + (theXY[1]/2));
		pointList.push(newPoint);  
		var newPoint = new OpenLayers.Geometry.Point(centreX + (theXY[0]/2),centreY + (theXY[1]/2));
		pointList.push(newPoint);  
		var newPoint = new OpenLayers.Geometry.Point(centreX + (theXY[0]/2),centreY - (theXY[1]/2));
		pointList.push(newPoint);    
		pointList.push(pointList[0]);
	  
		var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
		polygonExtentFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon([linearRing]),null,mapextenstyle);
		vlayer.addFeatures(polygonExtentFeature);
	  
		//Zoom to shape
		var theGeometryasBounds = vlayer.features[0].geometry.getBounds();
		var theExtentWidth = theGeometryasBounds.getWidth();
		var theExtentHeight = theGeometryasBounds.getHeight();
		var centreLatLon = theGeometryasBounds.getCenterLonLat();
		var theX = centreLatLon.lon;
		var theY = centreLatLon.lat; 
		var blx = (theX - (theExtentWidth/2)) - (theExtentWidth/4);
		var bly = (theY - (theExtentHeight/2)) - (theExtentHeight/4);
		var urx = (theX + (theExtentWidth/2)) + (theExtentWidth/4);
		var ury = (theY + (theExtentHeight/2)) + (theExtentWidth/4);
		var bounds = new OpenLayers.Bounds(blx,bly,urx,ury);
		map.zoomToExtent(bounds);

		//Clear the other options
		document.frmSearch1.txtCounty.value = "";
		document.frmSearch1.papersize.value = "";
		document.frmSearch1.txtRadius.value = "";
				
		//Store the extent type (scaled or ground)		
		if((document.frmSearch1.txtMapUnits2.value == "km") || (document.frmSearch1.txtMapUnits2.value == "mi")){
			document.frmSearch1.extentType.value = "ground";		
		}else{
			document.frmSearch1.extentType.value = "scaled";
		}

	}else if(theType=='region'){													//ADDING MAP EXTENT BASED ON A REGION EXTENT
		//Remove any existing
		if(polygonExtentFeature != null){
			vlayer.destroyFeatures()
		}
		var theXY = theSize.split(",");
		
		centreX = Number(theXY[0]) + ((Number(theXY[2]) - Number(theXY[0]))/2);
		centreY = Number(theXY[1]) + ((Number(theXY[3]) - Number(theXY[1]))/2);
		theWidth = (Number(theXY[2]) - Number(theXY[0]));		//Need to add a buffer??????????
		theHeight = (Number(theXY[3]) - Number(theXY[1]));

		//Define the polygon
		var pointList = [];
		var newPoint = new OpenLayers.Geometry.Point(centreX - (theWidth/2),centreY - (theHeight/2));
		pointList.push(newPoint); 
		var newPoint = new OpenLayers.Geometry.Point(centreX - (theWidth/2),centreY + (theHeight/2));
		pointList.push(newPoint);  
		var newPoint = new OpenLayers.Geometry.Point(centreX + (theWidth/2),centreY + (theHeight/2));
		pointList.push(newPoint);  
		var newPoint = new OpenLayers.Geometry.Point(centreX + (theWidth/2),centreY - (theHeight/2));
		pointList.push(newPoint);    
		pointList.push(pointList[0]);
	  
		var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
		polygonExtentFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon([linearRing]),null,mapextenstyle);
		vlayer.addFeatures(polygonExtentFeature);
	  
		//Zoom to shape
		var theGeometryasBounds = vlayer.features[0].geometry.getBounds();
		var theExtentWidth = theGeometryasBounds.getWidth();
		var theExtentHeight = theGeometryasBounds.getHeight();
		var centreLatLon = theGeometryasBounds.getCenterLonLat();
		var theX = centreLatLon.lon;
		var theY = centreLatLon.lat; 
		var blx = (theX - (theExtentWidth/2)) - (theExtentWidth/4);
		var bly = (theY - (theExtentHeight/2)) - (theExtentHeight/4);
		var urx = (theX + (theExtentWidth/2)) + (theExtentWidth/4);
		var ury = (theY + (theExtentHeight/2)) + (theExtentWidth/4);
		var bounds = new OpenLayers.Bounds(blx,bly,urx,ury);
		map.zoomToExtent(bounds);
		
		//Clear the other options
		document.frmSearch1.userx.value = "";
		document.frmSearch1.usery.value = "";
		document.frmSearch1.papersize.value = "";
		document.frmSearch1.txtRadius.value = "";
		
		//Store the extent type (scaled or ground)
		document.frmSearch1.extentType.value = "ground";	

	}else if(theType=='circle'){													//ADDING MAP RADIALS
	 
	 	//Ensure radials are drawn over the map extent - get centre of extent	
		//if(polygonExtentFeature != null){	
		   	if(vlayer.features.length > 0){
         		thegeometryasstring = vlayer.features[0].geometry;
		 		var theGeometryCentroid = vlayer.features[0].geometry.getCentroid();
				var centreX = theGeometryCentroid.x; 
				var centreY = theGeometryCentroid.y;
				showRadials = true;
			}else{
				alert("Please ensure you have defined a map extent first.");
				showRadials = false;
			}
		//}else{
		//	alert("Please ensure you have defined a map extent first.");
		//	showRadials = false;	
		//}
	  
		//Remove any existing
		for (i=0;i<polygonRadialFeature.length;i++){
			if(polygonRadialFeature[i] != null){
				rlayer.removeFeatures(polygonRadialFeature[i]);
			}
		}
		
		if(showRadials){		
			if (theSize != 'user'){	
				//Get the selected units
				if(document.frmSearch1.txtRadialUnits.value == "km"){
					theSize = theSize * 1000; 
				}else if(document.frmSearch1.txtRadialUnits.value == "mi"){
					theSize = theSize * 1609; 		
				}
				var radialArray = new Array();
				theWidth = vlayer.features[0].geometry.getBounds().getWidth();
				theHeight = vlayer.features[0].geometry.getBounds().getHeight();
			
				if (theWidth > theHeight){
					var maxSize = theWidth/2;
				}else{
					var maxSize = theHeight/2;
				}
				var currentSize = Number(theSize);
				for(i=0;i<50;i++)	{
					if(currentSize > maxSize){
						break;
					}
					radialArray[i] = currentSize;
					currentSize += Number(theSize);
				}
				
			}else{
				if(document.getElementById('txtRadial').value == ''){
					alert('Please enter a value');
				}else{
					var radialArray = document.getElementById('txtRadial').value.split(" ");
					for (i=0; i < radialArray.length; i++){
						//Get the selected units
						if(document.frmSearch1.txtRadialUnits.value == "km"){
							radialArray[i] = radialArray[i]*1000;
						}else if(document.frmSearch1.txtRadialUnits.value == "mi"){
							radialArray[i] = radialArray[i]*1609;		
						}		
					}
				}
			}
	 
			for (i=0; i < radialArray.length; i++){
				var pointList = [];
				for (n=0;n<72;n++){
					strXtemp = (Math.round(((Math.cos((n*5)*(Math.PI/180)))*Number(radialArray[i])))) + centreX;
					strYtemp = (Math.round(((Math.sin((n*5)*(Math.PI/180)))*Number(radialArray[i])))) + centreY;
					var newPoint = new OpenLayers.Geometry.Point(strXtemp,strYtemp);
					pointList.push(newPoint);  
				}
				pointList.push(pointList[0]);   
				var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
				radialArray[i] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon([linearRing]),null,radialStyle);
				polygonRadialFeature = radialArray;
				rlayer.addFeatures(radialArray[i]);
			}
		}
 	}
}

//----------Resizes a scaled extent if the map base has been changed
function resizeExtent(theNewScale){

	if(vlayer.features.length > 0){
         thegeometryasstring = vlayer.features[0].geometry;
		 var theGeometryasBounds = vlayer.features[0].geometry.getBounds();
		 var theExtentWidth = theGeometryasBounds.getWidth();
		 var theExtentHeight = theGeometryasBounds.getHeight();
		 var theCentrePoint = vlayer.features[0].geometry.getBounds().getCenterLonLat();		 
		 var centreX = theCentrePoint.lon;
		 var centreY = theCentrePoint.lat;
		 //Get Scale
		 if(document.frmSearch1.selectedMapScale.value.indexOf("P") != -1){
			theMapScale = Number(document.frmSearch1.selectedMapScale.value.substring(0,document.frmSearch1.selectedMapScale.value.length-1))
		 }else{
			theMapScale = Number(document.frmSearch1.selectedMapScale.value);
		 }
		 var theExtentWidthAtScaleM = theExtentWidth / theMapScale;
		 var theExtentHeightAtScaleM = theExtentHeight / theMapScale;
		 
		 var newWidth =  theExtentWidthAtScaleM * theNewScale;
		 var newHeight = theExtentHeightAtScaleM * theNewScale;
		 	 
		//Remove any existing
		if(polygonExtentFeature != null){
			vlayer.destroyFeatures()
		}

		//Define the new polygon
		var pointList = [];
		var newPoint = new OpenLayers.Geometry.Point(centreX - (newWidth/2),centreY - (newHeight/2));
		pointList.push(newPoint); 
		var newPoint = new OpenLayers.Geometry.Point(centreX - (newWidth/2),centreY + (newHeight/2));
		pointList.push(newPoint);  
		var newPoint = new OpenLayers.Geometry.Point(centreX + (newWidth/2),centreY + (newHeight/2));
		pointList.push(newPoint);  
		var newPoint = new OpenLayers.Geometry.Point(centreX + (newWidth/2),centreY - (newHeight/2));
		pointList.push(newPoint);    
		pointList.push(pointList[0]);
	  
		var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
		polygonExtentFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon([linearRing]),null,mapextenstyle);
		vlayer.addFeatures(polygonExtentFeature);

		//Zoom to shape
		var blx = (centreX - (newWidth/2)) - (newWidth/4);
		var bly = (centreY - (newHeight/2)) - (newHeight/4);
		var urx = (centreX + (newWidth/2)) + (newWidth/4);
		var ury = (centreY + (newHeight/2)) + (newHeight/4);
		var bounds = new OpenLayers.Bounds(blx,bly,urx,ury);
		map.zoomToExtent(bounds);
	}
	
}


//----------Checks if map extent chosen and open the confirm page
function getfeatures(){
   	if(vlayer.features.length > 0){
         thegeometryasstring = vlayer.features[0].geometry;
		 var theGeometryasBounds = vlayer.features[0].geometry.getBounds();
		 var theExtentWidth = theGeometryasBounds.getWidth();
		 var theExtentHeight = theGeometryasBounds.getHeight();
		 if(theExtentWidth < theExtentHeight){
			 var theSmallestDimension = theExtentWidth;
		 }else{
			 var theSmallestDimension = theExtentHeight; 
		 }
		 if(theSmallestDimension > 3000){	 
			 document.frmSearch1.extentInfo.value = thegeometryasstring;
			 document.frmSearch1.action = "custom-wall-maps-confirm.asp";
			 document.frmSearch1.submit();
		 }else{
		 		alert('The map coverage is too small to supply as a wall map. Please choose a bigger area.');
		 }
     }else{
		alert('Please ensure that you select an area to be covered before proceeding');
	 }
}

//----------Reset the map deletes vector features and zoom to max extent
function destroyMap(){
	vlayer.destroyFeatures();
	rlayer.destroyFeatures();
	map.zoomToMaxExtent();
	document.getElementById('txtRadial').value = "";
	document.getElementById('txtRadials').value = "";
}
//----------Reset the drawing deletes vector features
function destroyExtent(){
	vlayer.destroyFeatures();
	rlayer.destroyFeatures();
	document.getElementById('txtRadial').value = "";
	document.getElementById('txtRadials').value = "";
}
//----------Deletes any radials
function destroyRadials(){
	rlayer.destroyFeatures();
	document.getElementById('txtRadial').value = "";
	document.getElementById('txtRadials').value = "";
}


function updateMapScale(){
	var theScale = Number(document.mapform.theMapScale.value);
	if(theScale != 0){
		//alert(theScale)
		map.zoomToScale(theScale,false);
	}
}



//--------------------------End of Script