// JavaScript Document
var toolmode = 0;			//sets variable for selected tool button
var imageDownID = "";
var cursor_val = "";			// variable to hold cursor image value

var serverpath = "http://derbyshiremaps.derbyshire.gov.uk/planaccess/";
var thetheme = "Portal3_layers";
var thepartner = "DCC";

var colour_background_layers = "";		//text list of colour background map layers
var mono_background_layers = "";		//text list of mono background map layers
var simple_background_layers = "";		//text list of simple / stylised background map layers
var current_background_layers = "";		//sets to either colour_background_layers or mono_background_layers, or simple_background_layers
var current_layers = "";			//text list of current active layers, divided by commas for calling getmapimage
var historical_layers = "";			//text list of historical layers, divided by commas for calling getmapimage
var not_current_INFO_layers = "";		//text list of layers not to include in info search, divided by commas
var layers_list_array = new Array();		//array of allowed layers from PA Layer admin db
var layers_list_display_array = new Array();	//xxx	//array of layer display names from PA Layer admin db
var layers_list_active_array = new Array();	//mirror array of active (yes / no) for allowed layers array
var section_list_array = new Array();		//mirror array of data sections for allowed layers array (from PA Layer admin db)
var disclaimer_list_array = new Array();	//mirror array of disclaimer yes / no for allowed layers array (from PA Layer admin db)
var layerinfo_list_array = new Array();		//mirror array of layer info for allowed layers array (from PA Layer admin db)
var layerzoom_list_array = new Array();
var sections_active_array = new Array();
var sections_ticked_array = new Array();

var layer_legend_array = new Array();
var legend_layers_array = new Array();
var legend_items_array = new Array();
var legend_filenames_array = new Array();

var historic_check = 0;				//to aid in setting up the historic layers checkbox (0 on opening (unchecked), 1 when checked
var historical_sel = 0;

var GSTLayerList = new Array();			//array of all selectable layers in geoset
var GSTnum_layers = 0;

var fmn_layer = "";
var fmn_live_list_array = new Array();
var fmn_db_list_array = new Array();

var max_zoom = 155000;
var min_zoom = 100;
var zoom = "155000";
var easting = "426000";
var northing = "359000";
var mapwidth = "615";
var mapheight = "380";
var gridref = "";

var last_zoom = "155000";
var last_easting = "426000";
var last_northing = "359000";

var menu_add_layers_txt_on = "";
var menu_current_layers_txt_on = "";
var menu_search_results_txt_on = "";
var menu_find_places_txt_on = "";
var menu_add_layers_txt_off = "";
var menu_current_layers_txt_off = "";
var menu_search_results_txt_off = "";
var menu_find_places_txt_off = "";
var menu_active = "";
var menu_current_txt = "";

var Loc_name_array = new Array();
var Loc_east_array = new Array();
var Loc_north_array = new Array();

var AddName = "";
var AddNum = "";
var AddRoad = "";
var AddPC = "";
var FMNPC = "";
var FMNloc_index = 0;
var FMNloc_type = "fmn_optionpc";
var FMNrad = "10";
var FMNmeasure = "km";

var post_thepartner = "";
var thepc = "";
var thecoordsstr = "";

var add_chosen = "no";
var addeasting = "";
var addnorthing = "";

var seeded_search_layer = "";
var seeded_search_layer_display = "";	//xxx
var seeded_search_field = "";
var seeded_search_value = "";
var seeded_panel = "";

var OS_lets_array = new Array("SV","SW","SX","SY","SZ","TV","SR","SS","ST","SU","TQ","TR","SM","SN","SO","SP","TL","TM","SH","SJ","SK","TF","TG","SC","SD","SE","TA","NW","NX","NY","NZ","NR","NS","NT","NU","NL","NM","NN","NO","NF","NG","NH","NJ","NK","NA","NB","NC","ND","HW","HX","HY","HZ","HT","HU","HP");
OS_lets_array = OS_lets_array.sort();


function right(e) {
	//var msg = "Sorry, you don't have permission to right-click.";
	if (navigator.appName == 'Netscape' && e.which == 3) {
		//alert(msg);
		return false;
		}
	if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2) {
		//alert(msg);
		return false;
	} 
	else return true;
}


function trap() {
	if(document.images) {
    		for(i=0;i<document.images.length;i++) {
			document.images[i].onmousedown = right;
			document.images[i].onmouseup = right;
		}
	}
}





// *****************************************************************************************************
// ******************************* Set up of initial variable values ***********************************
// *****************************************************************************************************





function post_partner(theinput) {
	post_thepartner = theinput;
}



function setup_portal(layerurl) {

	FetchLayerList();
	//colour_background_layers = "DCCDistricts,Carto_Text,Topo_Line,Carto_Area,Topo_Area,10Kcolour,50Kcolour,Road Atlas,Miniscale_Colour";
	//mono_background_layers = "DCCDistricts,Carto_Text,Topo_Line_Mono,10Kmono,50Kmono,Road_Atlas_Mono,Miniscale_Mono";
	//simple_background_layers = "Derbyshire_Locations,Road_Labels,Mway,P_S,P_L,A_L,A_S,B,Mn,St,rl,Dist,W_L,W_S,Urb,Peak,County";
	//current_background_layers = colour_background_layers;
	//setup_initial_fmn_layers();

	if (document.getElementById("link_returncall").value == "link_return") {		// If opening portal from one of the POST links

		document.getElementById("link_returncall").value = "not_linked";
		thepartner = post_thepartner.toUpperCase();

		colour_background_layers = "DCCDistricts,Carto_Text,Topo_Line,Carto_Area,Topo_Area,10Kcolour,50Kcolour,Road Atlas,Miniscale_Colour";
		mono_background_layers = "DCCDistricts,Carto_Text,Topo_Line_Mono,10Kmono,50Kmono,Road_Atlas_Mono,Miniscale_Mono";
		simple_background_layers = "Derbyshire_Locations,Road_Labels,Mway,P_S,P_L,A_L,A_S,B,Mn,St,rl,Dist,W_L,W_S,Urb,Peak,County";
		current_background_layers = colour_background_layers;
		setup_initial_fmn_layers();


		zoom = document.getElementById("link_zoom").value;
		easting = document.getElementById("link_easting").value;
		northing = document.getElementById("link_northing").value;
		last_zoom = zoom;
		last_easting = easting;
		last_northing = northing;

		menu_active = document.getElementById("link_menu_active").value;
		if (menu_active == "search_results") {
			menu_active = "find_places";
		}

		var form_layers_array = new Array();
		var URLlayer = "";
		current_layers = document.getElementById('link_current_layers').value;
		if (current_layers != "") {
			var URLlayers_array = current_layers.split(",");
			var URLlayer = "";

			for (y=0; y<URLlayers_array.length; y++) {
				URLlayer = URLlayers_array[y];
				URLlayer = URLlayer.replace(/%20/g," ");
				URLlayer = display_to_dataset(URLlayer);	//xxx

				for (x in layers_list_array) {
					if (URLlayer.toUpperCase() == layers_list_array[x].toUpperCase()) {		//for each layer in URL call, set active to Yes to display it.
						layers_list_active_array[x] = "Yes";
						current_layers = URLlayer + "," + current_layers;
						Layer_check(x);
						add_layer_to_fmn_array(layers_list_display_array[x]);	//xxx
					}
				}
			}
		}

		current_background_layers = document.getElementById('link_current_background_layers').value;
		if ((current_background_layers.match("mono") == "mono") && (current_background_layers.match("PRIM4") != "PRIM4")) {
			document.getElementById('mono_map').checked="checked";
		} else if (current_background_layers.match("PRIM4") == "PRIM4") {
			document.getElementById('simple_map').checked="checked";
		} else {
			document.getElementById('colour_map').checked="checked";
		}

		AddName = document.getElementById('link_AddName').value;
		AddNum = document.getElementById('link_AddNum').value;
		AddRoad = document.getElementById('link_AddRoad').value;
		AddPC = document.getElementById('link_AddPC').value;
		FMNPC = document.getElementById('link_FMNPC').value;
		FMNrad = document.getElementById('link_FMNrad').value;
		FMNloc_index = document.getElementById('link_FMNloc_index').value;
		FMNloc_type = document.getElementById('link_FMNloc_type').value;
		if (FMNrad == "") {
			FMNrad = "10";
		}
		FMNmeasure = document.getElementById('link_FMNmeasure').value;


 		setup_inital_menu_text();
		setup_menu_panel();      

		historical_layers = document.getElementById('link_historical_layers').value;
       		historic_check = document.getElementById('link_historic_check').value;
		if (historic_check == 1) {
       			historical_sel = document.getElementById('link_historical_sel').value;
			if (menu_active == "add_layers") {
				historicmap(historical_sel);
			}
		}

		update_map_location();
		update_zoom_bar();
		setmap_labels();
		getMap();

	} else {									//Opening portal from launch page

		document.getElementById("link_url_str").value = layerurl;

		if (document.getElementById('fm_style').value != "") {
			thepartner = document.getElementById('fm_style').value;
			thepartner = thepartner.toUpperCase();
		}

		set_partner_locations();						//set standard partner opening zooms and locations

		colour_background_layers = "DCCDistricts,Carto_Text,Topo_Line,Carto_Area,Topo_Area,10Kcolour,50Kcolour,Road Atlas,Miniscale_Colour";
		mono_background_layers = "DCCDistricts,Carto_Text,Topo_Line_Mono,10Kmono,50Kmono,Road_Atlas_Mono,Miniscale_Mono";
		simple_background_layers = "Derbyshire_Locations,Road_Labels,Mway,P_S,P_L,A_L,A_S,B,Mn,St,rl,Dist,W_L,W_S,Urb,Peak,County";
		current_background_layers = colour_background_layers;
		setup_initial_fmn_layers();


		if ((document.getElementById('fm_easting').value != "") && (document.getElementById('fm_northing').value)) {
			easting = document.getElementById('fm_easting').value;
			northing = document.getElementById('fm_northing').value;
			last_easting = easting;
			last_northing = northing;
		}

		if (document.getElementById('fm_marker').value == "yes") {
			add_chosen = "yes";
			addeasting = easting;
			addnorthing = northing;
		}

		var theback = document.getElementById('fm_back').value;
		if (theback.match("mono") == "mono") {
			current_background_layers = mono_background_layers;
			document.getElementById('mono_map').checked="checked";
		} else if (theback.match("simple") == "simple") {
			current_background_layers = simple_background_layers;
			document.getElementById('simple_map').checked="checked";
		}

		if (document.getElementById('fm_layers').value != "") {

			var thelayers = document.getElementById('fm_layers').value;
			thelayers = unescape(thelayers);
			thelayers = thelayers.replace(/%2C/g,",");
			thelayers = thelayers.replace(/-/g,",");
			var URLlayers_array = thelayers.split(",");
			var URLlayer = "";

			for (y=0; y<URLlayers_array.length; y++) {
				URLlayer = URLlayers_array[y];
				URLlayer = URLlayer.replace(/%20/g," ");
				URLlayer = display_to_dataset(URLlayer);	//xxx

				for (x in layers_list_array) {
					if (URLlayer.toUpperCase() == layers_list_array[x].toUpperCase()) {		//for each layer in URL call, set active to Yes to display it.
						layers_list_active_array[x] = "Yes";
						current_layers = URLlayer + "," + current_layers;
						Layer_check(x);
						add_layer_to_fmn_array(layers_list_display_array[x]);	//xxx
					}
				}
			}
		}

		if ((document.getElementById('fm_search_layer').value != "") &&
		    (document.getElementById('fm_search_field').value != "") &&
		    (document.getElementById('fm_search_value').value != "")) {
			seeded_search_field = document.getElementById('fm_search_field').value;
			seeded_search_value = document.getElementById('fm_search_value').value;
			seeded_search_layer_display = document.getElementById('fm_search_layer').value;	//xxx 
			seeded_search_layer = display_to_dataset(seeded_search_layer_display);	//xxx 
		}


		if (thecoordsstr.match(",") == ",") {
			var coords_array = thecoordsstr.split(",");
			easting = coords_array[0];
			northing = coords_array[1];
			last_easting = easting;
			last_northing = northing;
			if (zoom == "") {
				zoom = String(max_zoom);
				last_zoom = zoom;
			}
			if (add_chosen == "yes") {
				addeasting = easting;
				addnorthing = northing;
			}
		}

		if (current_layers == "") {			//If any layers specified in opening url, open menus with 'current layers' menu open, not 'find places'
			menu_active = "find_places";
		} else {
			menu_active = "current_layers";
		}

		if (document.getElementById('fm_panel').value != "") {
			seeded_panel = document.getElementById('fm_panel').value.toUpperCase();
			if (seeded_panel == "KEY") {	
				menu_active = "current_layers";
			} else if (seeded_panel == "SEARCH") {
				menu_active = "search_results";
			} else if (seeded_panel == "LAYER") {
				menu_active = "add_layers";
			} else if (seeded_panel == "FIND") {
				menu_active = "find_places";
			} else {
				menu_active = "find_places";
			}
		}

		setup_inital_menu_text();
		setup_menu_panel();

		if (document.getElementById('fm_zoom').value != "") {
			zoom = document.getElementById('fm_zoom').value;
			last_zoom = zoom;
			if (menu_active == "current_layers") {
				create_current_layers_menu();
				setup_menu_panel();
			}
		}

		if ((seeded_search_layer != "") && (seeded_search_field != "") && (seeded_search_value != "")) {
			seeded_layer_search();
		} else {
			update_map_location();
			update_zoom_bar();
			setmap_labels();
			getMap();
		}
	}
}




function PC_coords(coordsstr) {

	if (coordsstr.match(",") == ",") {
		thecoordsstr = coordsstr;
	}
}




function set_partner_locations() {							//set opening partner zooms and locations

	switch (thepartner)
	{
	case "AVC":
  		easting = "437000";
  		northing = "347800";
  		zoom = "40000";
  		break
	case "BOL":
  		easting = "448800";
  		northing = "367100";
  		zoom = "40000";
  		break
	case "CBC":
  		easting = "440500";
  		northing = "373300";
  		zoom = "16000";
  		break
	case "DCC":
  		easting = "426000";
  		northing = "359000";
  		zoom = "155000";
  		break
	case "DDC":
  		easting = "420000";
  		northing = "358400";
  		zoom = "90000";
  		break
	case "DER":
  		easting = "435700";
  		northing = "335600";
  		zoom = "20000";
  		break
	case "ERE":
  		easting = "442700";
  		northing = "338200";
  		zoom = "25000";
  		break
	case "HPC":
  		easting = "410300";
  		northing = "386700";
  		zoom = "65000";
  		break
	case "NED":
  		easting = "437200";
  		northing = "369300";
  		zoom = "40000";
  		break
	case "PDP":
  		easting = "412500";
  		northing = "380500";
  		zoom = "100000";
  		break
	case "POL":
  		easting = "426000";
  		northing = "359000";
  		zoom = "155000";
  		break
	case "SDC":
  		easting = "431500";
  		northing = "325300";
  		zoom = "45000";
  		break
	case "EPL":
  		easting = "426000";
  		northing = "359000";
  		zoom = "155000";
  		break
  	case "DDC":
  		easting = "420000";
  		northing = "358400";
  		zoom = "90000";
  		break
	}
	last_zoom = zoom;
	last_easting = easting;
	last_northing = northing;
}




function homepage_link() {

	switch (thepartner)
	{
	case "AVC":
  		window.open("http://www.ambervalley.gov.uk", "_blank");
  		break
	case "BOL":
  		window.open("http://www.bolsover.gov.uk", "_blank");
  		break
	case "CBC":
  		window.open("http://www.chesterfield.gov.uk", "_blank");
  		break
	case "DCC":
  		window.open("http://www.derbyshire.gov.uk", "_blank");
  		break
	case "DDC":
  		window.open("http://www.derbyshiredales.gov.uk", "_blank");
  		break
	case "DER":
  		window.open("http://www.derby.gov.uk", "_blank");
  		break
	case "ERE":
  		window.open("http://www.erewash.gov.uk", "_blank");
  		break
	case "HPC":
  		window.open("http://www.highpeak.gov.uk", "_blank");
  		break
	case "NED":
  		window.open("http://www.ne-derbyshire.gov.uk", "_blank");
  		break
	case "PDP":
  		window.open("http://www.peakdistrict-npa.gov.uk", "_blank");
  		break
	case "POL":
  		window.open("http://www.derbyshire.police.uk/", "_blank");
  		break
	case "SDC":
  		window.open("http://www.south-derbys.gov.uk", "_blank");
  		break
	case "EPL":
  		window.open("http://www.derbyshire.gov.uk", "_blank");
  		break
	case "TST":
  		window.open("http://www.derbyshire.gov.uk", "_blank");
  		break
	default:
  		//do nothing
  		break
	} 
}




function update_linkform() {

	document.getElementById('link_current_background_layers').value = current_background_layers;
	document.getElementById('link_current_layers').value = current_layers;
	document.getElementById('link_historical_layers').value = historical_layers;
	document.getElementById('link_historic_check').value = historic_check;
	document.getElementById('link_historical_sel').value = historical_sel;
	document.getElementById('link_zoom').value = zoom;
	document.getElementById('link_easting').value = easting;
	document.getElementById('link_northing').value = northing;
	document.getElementById('link_menu_active').value = menu_active;

	document.getElementById('link_AddName').value = AddName;
	document.getElementById('link_AddNum').value = AddNum;
	document.getElementById('link_AddRoad').value = AddRoad;
	document.getElementById('link_AddPC').value = AddPC;
	document.getElementById('link_FMNPC').value = FMNPC;
	document.getElementById('link_FMNrad').value = FMNrad;
	document.getElementById('link_FMNloc_index').value = FMNloc_index;
	document.getElementById('link_FMNloc_type').value = FMNloc_type;
	document.getElementById('link_FMNmeasure').value = FMNmeasure;
}



function seeded_layer_search() {							//NEW 3.2

	var sURL = serverpath + "dialogbox_MapInformation.asp?theme=" + thetheme + "&mapwidth=" + mapwidth + "&mapheight=" + mapheight + "&mapmode=GetBounds";
	sURL += "&infomode=xml&layername=" + seeded_search_layer + "&fieldname=" + seeded_search_field + "&fieldvalue=" + seeded_search_value;

	xmlinfoRequestObj = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")
	xmlinfoRequestObj.onreadystatechange = proccess_seededsearch_XML;
	xmlinfoRequestObj.open("GET", sURL, true);
	xmlinfoRequestObj.send(null);
}


function proccess_seededsearch_XML() {								//NEW 3.2
	if (xmlinfoRequestObj.readyState == 4 && (xmlinfoRequestObj.status == 200 || xmlinfoRequestObj.status == 304)) {
		xmlDoc = xmlinfoRequestObj.responseXML;
		check_search_results();
	}
}


function check_search_results() {								//NEW 3.2

	var x = 0;
	var map_search_x = "";
	var map_search_y = "";
	var map_search_width = "";

	LayerIndex = 0;
	var SEARCHnodeName = "";
	var SEARCHnodeText = "";

	rootNode = xmlDoc.documentElement;

	for ( x = 0; x < rootNode.childNodes.length; x++ ) {

		if (BrowserName == "Netscape") {
			SEARCHnodeText = rootNode.childNodes.item(x).firstChild.nodeValue;
			SEARCHnodeName = rootNode.childNodes.item(x).nodeName;
		} else {
			SEARCHnodeText = rootNode.childNodes.item(x).text;
			SEARCHnodeName = rootNode.childNodes.item(x).nodeName;
		}

		if ( SEARCHnodeName.toUpperCase() == "CENTERX" ) {
			map_search_x = SEARCHnodeText;
				 
		} else if ( SEARCHnodeName.toUpperCase() == "CENTERY" ) {
			map_search_y = SEARCHnodeText;

		} else if ( SEARCHnodeName.toUpperCase() == "MAPWIDTH" ) {
			map_search_width = SEARCHnodeText;
		}
	}

	xmlDoc = null;

	if (map_search_x != "" && map_search_y != "" && map_search_width != "") {

		last_zoom = zoom;
		if (map_search_width > max_zoom) {
			zoom = max_zoom;
		} else if (map_search_width < min_zoom) {
			zoom = min_zoom;
		} else {
			zoom = map_search_width;
		}
		if (document.getElementById('fm_zoom').value != "") {
			zoom = document.getElementById('fm_zoom').value;
			last_zoom = zoom;
		}

		last_easting = easting;
		last_northing = northing;
		easting = map_search_x;
		northing = map_search_y;
	
		if (menu_active == "search_results") {

			sHTML = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" alt=\"\" title=\"\"></td>";
			sHTML += "<td width=\"250\" id=\"menu_search_results\" class=\"sel_menu_bar\">Search results</td></tr>";
			sHTML += "<tr><td colspan=\"2\" class=\"menu_panel\" valign=\"top\">";
			// PANEL TABLE
			sHTML += "<div id=\"Infores\" STYLE=\"overflow:auto; height=390; width=270; text-align:left\" NOWRAP>";
			sHTML += "<table width=\"100%\" class=\"menu_panel_table\">";
			sHTML += "<tr><td width=\"100%\" class=\"panel_title\" colspan=\"3\" valign=\"top\">Currently searching for:<br>" + seeded_search_layer_display + "</td></tr>";	//xxx
			sHTML += "<tr><td class=\"panel_text\" height=\"5\" width=\"60\" colspan=\"3\"></td></tr>";
			menu_search_results_txt_on = sHTML;
			setup_menu_panel(); 
			search_panel_info();

		} else if (menu_active == "current_layers") { 
			update_map_location();
			update_zoom_bar();
			setmap_labels();
			AddFMNlayer(seeded_search_layer_display);	//xxx
			create_current_layers_menu();	//xxx
			setup_menu_panel();	//xxx

		} else {
			update_map_location();
			update_zoom_bar();
			setmap_labels();
			AddFMNlayer(seeded_search_layer_display); //xxx
		}
		
	} else {
		set_partner_locations();
		update_map_location();
		update_zoom_bar();
		setmap_labels();
		getMap();
	}
}



function search_panel_info() {								//NEW 3.2

	var sURL = serverpath + "dialogbox_InfoPointViewer.asp?";
	sURL += "infomode=xml&theme=" + thetheme + "&layername=" + seeded_search_layer + "&fieldname=" + seeded_search_field + "&fieldvalue=" + seeded_search_value;
	xmlinfoRequestObj = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")
	xmlinfoRequestObj.onreadystatechange = proccess_seededsearchpanel_XML;
	xmlinfoRequestObj.open("GET", sURL, true);
	xmlinfoRequestObj.send(null);
}


function proccess_seededsearchpanel_XML() {						//NEW 3.2

	if (xmlinfoRequestObj.readyState == 4 && (xmlinfoRequestObj.status == 200 || xmlinfoRequestObj.status == 304)) {
		xmlDoc = xmlinfoRequestObj.responseXML;
		create_search_panel();
	}
}



function create_search_panel() {							//NEW 3.2


	rootNode = xmlDoc.documentElement;

	if ( rootNode.childNodes.length > 0 ) {

	sHTML = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" alt=\"\" title=\"\"></td>";
	sHTML += "<td width=\"250\" id=\"menu_search_results\" class=\"sel_menu_bar\">Search results</td></tr>";
	sHTML += "<tr><td colspan=\"2\" class=\"menu_panel\" valign=\"top\">";
	// PANEL TABLE
	sHTML += "<div id=\"Searchres\" STYLE=\"overflow:auto; height:390; width:270; text-align:left\" NOWRAP>";
	sHTML += "<table width=\"100%\" class=\"menu_panel_table\">";

	sHTML += "<tr><td width=\"100%\" class=\"panel_title\" colspan=\"3\" valign=\"top\">Your map is centred on:</td></tr>";
	sHTML += "<tr><td class=\"panel_text\" height=\"5\" width=\"60\" colspan=\"3\"></td></tr>";


	var INFO_Elements = rootNode.childNodes.item(0)
	var ResultNo = "";

	// Populate results table
	for (x = 0; x < rootNode.childNodes.length; x++) {

		// Get the number of columns in the XML document
		var featureRows = rootNode.childNodes.item(x).childNodes.length;
		sHTML += "<tr><td class=\"panel_text\" height=\"5\" width=\"90\"><img border=\"0\" src=\"images/fmnheader.gif\" alt=\"\" title=\"\"></td><td class=\"panel_text\" width=\"30\"></td><td class=\"panel_text\" width=\"140\"></td></tr>";


		for ( ll = 0; ll < featureRows; ll++ ) {


			// Loop around all the fields in the feature
			var dataRows = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.length;
			for ( l = 0; l < dataRows; l++ ) {

				var Search_Name = "";
				var Search_Text = "";

				if (BrowserName == "Netscape") {
					if(rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).hasChildNodes()){
						Search_Text = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).firstChild.nodeValue;
						Search_Name = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).nodeName;
					}
				} else {
					Search_Text = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).text;
					Search_Name = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).nodeName;
				}


				if (Search_Name.toUpperCase() == "EASTING") {
					var Search_Easting = Search_Text;
				} else if (Search_Name.toUpperCase() == "NORTHING") {
					var Search_Northing = Search_Text;
				}
			}

			var layer_title_display = dataset_to_display(rootNode.childNodes.item(x).getAttribute("Name"));	//xxx

			sHTML += "<tr><td class=\"panel_text_highlight_title\" colspan=\"2\">" + layer_title_display + "</td>";	//xxx
			if (Search_Easting == undefined || Search_Easting == undefined){
				sHTML += "<td></td></tr>";
			} else {
				sHTML += "<td class=\"panel_text\" align=\"right\"><input type=\"button\" name=\"findonmapb\" id=\"findonmapb\" onclick=\"FMNMoveMap(" + Search_Easting + "," + Search_Northing +")\" align=\"right\" valign=\"middle\" value=\"Find on Map\" style=\"cursor:pointer\"></td></tr>";
			}

			// Loop around all the fields in the feature
			for ( l = 0; l < dataRows; l++ ) {

				var Search_Name = "";
				var Search_Text = "";


				if (BrowserName == "Netscape") {
					if(rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).hasChildNodes()){
						Search_Text = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).firstChild.nodeValue;
						Search_Name = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).nodeName;
					}
				} else {
					Search_Text = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).text;
					Search_Name = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).nodeName;
				}

				if (Search_Text != "" && Search_Text.toUpperCase() != "N/A" && Search_Name.toUpperCase() != "EASTING" && Search_Name.toUpperCase() != "NORTHING") {

					Search_Name = Search_Name.replace(/_/g," ");
					Search_Name = (Search_Name.toUpperCase()).substring(0,1) + (Search_Name.toLowerCase()).substring(1,Search_Name.length);
					sHTML += "<tr><td class=\"panel_text\" valign=\"top\">" + Search_Name + ":</td>";
					sHTML += "<td class=\"panel_text\" valign=\"top\" colspan=\"2\">";

					if ( Search_Text.substring(0,5) == 'http:' ) {
						sHTML += "<a href='" + Search_Text + "' target='_blank' title='Open webpage in new window'>Open webpage in new window</a></td>";
					} else if ( Search_Text.substring(0,6) == 'https:' ) {
						sHTML += "<a href='" + Search_Text + "' target='_blank' title='Open webpage in new window'>Open webpage in new window</a></td>";
					} else if ( Search_Text.substring(0,7) == 'mailto:' ) {
						sHTML += "<a href=\"" + Search_Text + "\" title='Send email'>Send email</a></td>";
					} else {
						Search_Text = Search_Text.replace(/,,/g,",");
						Search_Text = Search_Text.replace(/,/g,", ");
						Search_Text = Search_Text.replace(/, , /g,", ");
						sHTML += Search_Text + "</td></tr>";
					}
				}
			}	

			if (Search_Easting != "" && Search_Northing != "") {
				var transportdirectlink = "http://www.transportdirect.info/transportdirect/en/journeyplanning/jplandingpage.aspx?id=BusinessLinks&oo=&on=&o=&do=en&dn=Selected " + seeded_search_layer + "&d=" + Search_Easting + "," + Search_Northing + "&p=0";
				sHTML += "<tr><td class=\"panel_text\" valign=\"top\">DIRECTIONS:</td>";
				sHTML += "<td class=\"panel_text\" colspan=\"2\"><a href=\"" + transportdirectlink + "\" target='_blank' title='Open Travel Planner website in new window'><b>Travel Planner</b> (opens in a new window)</a></td></tr>";
			}
		}
		sHTML += "<tr><td class=\"panel_text\" height=\"5\" width=\"60\" colspan=\"3\"></td></tr>";
	}

	sHTML += "</table></div>";
	// END OF PANEL TABLE
	sHTML += "</td></tr>";
	sHTML += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";

	menu_search_results_txt_on = sHTML;
	setup_menu_panel(); 
	xmlDoc = null;
	}

	update_map_location();
	update_zoom_bar();
	setmap_labels();
	getMap();	//xxx
	AddFMNlayer(seeded_search_layer_display);	//xxx

}


//***********************************************************************************
// Do Client Side Browser Checking
//***********************************************************************************		
	
// Sniff the browser and it's version
	
var BrowserName = navigator.appName;
var BrowserVersion;
	
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var agt = navigator.userAgent.toLowerCase();
    
// If the browser is IE then do futher checks on the version
if ( BrowserName == "Microsoft Internet Explorer" ) {
       	
	var is_ie3    = (is_major < 4);
	var is_ie4    = (is_major == 4) && (agt.indexOf("msie 4")!=-1);
	var is_ie5    = (is_major == 4) && (agt.indexOf("msie 5.0")!=-1);
	var is_ie5_5  = (is_major == 4) && (agt.indexOf("msie 5.5") !=-1);
	var is_ie6    = (is_major == 4) && (agt.indexOf("msie 6.")!=-1);
		
	if ( is_ie3 ) {
		BrowserVersion = "3";
	}
	else if ( is_ie4 ) {
		BrowserVersion = "4";
	}
	else if ( is_ie5 ) {
		BrowserVersion = "5";
	}
	else if ( is_ie5_5 ) {
		BrowserVersion = "5.5";
	}
	else if ( is_ie6 ) {
		BrowserVersion = "6";
	}
} else {
	BrowserVersion = parseInt(navigator.appVersion);
} 

var os = navigator.platform;
os = os.toLowerCase();



// *****************************************************************************************************
// ****************************************** Set up layers ********************************************
// *****************************************************************************************************


//list of allowed layers for portal (from layer admin database query)
function layer_array(layer_list) {

	layers_list_array = layer_list.split(",");
	var x = 0;
	for (x in layers_list_array) {
		layers_list_active_array[x] = "No";
	}
}


//list of layer display names for portal (from layer admin database query)	//xxx
function layerdisplay_array(displayname_list) {

	layers_list_display_array = displayname_list.split(",");
}


//list of sections for allowed layers for portal (from layer admin database query)
function section_array(section_list) {
	section_list_array = section_list.split(",");
	var x = 0;
	var thesection = "";
	var thesection_no = 0;
	for (x in section_list_array) {
		if (thesection != section_list_array[x]) {
			sections_active_array[thesection_no] = "No";
			thesection_no = thesection_no + 1;
			thesection = section_list_array[x];
		}
	}
}


//list of layers - disclaimer Yes/No for portal (from layer admin database query)
function disclaimer_array(disclaimer_list) {

	disclaimer_list_array = disclaimer_list.split(",");
}


//list of allowed layers info for portal (from layer admin database query)
function layerinfo_array(layerinfo_list) {

	layerinfo_list_array = layerinfo_list.split("#");
}


//list of layers zoom info for portal (from layer admin database query)
function layerzoom_array(layerzoom_list) {

	layerzoom_list_array = layerzoom_list.split(",");
}


function dataset_to_display(dataset_title) {		//xxx		//converts layer name code to display name

	var x = 0;
	var dataset_name, display_name;
	for (x in layers_list_array) {
		dataset_name = layers_list_array[x];
		if (dataset_name.toUpperCase() == dataset_title.toUpperCase()) {
			display_name = layers_list_display_array[x];
		}
	}
	return display_name;
}


function display_to_dataset(display_title) {		//xxx		//converts display name to layer name code

	var x = 0;
	var dataset_name, display_name;

	for (x in layers_list_display_array) {
		display_name = layers_list_display_array[x];
		if (display_name.toUpperCase() == display_title.toUpperCase()) {
			dataset_name = layers_list_array[x];
		}
	}
	return dataset_name;
}



var displayinfo_selectbox_id = -1;



function display_info(theinfotext,e,thisObject) {				// displays layer info on mouse over the 'i' icon


	if (BrowserName == "Netscape" ) {
		mouseX = e.pageX;
		mouseY = e.pageY;
	} else {
		mouseX = event.clientX - 2;
		mouseY = event.clientY - 2;
		if (BrowserName == "Microsoft Internet Explorer") {		//IE correction
			mouseX += document.body.scrollLeft;
			mouseY += document.body.scrollTop;
		}
	}

	infobox_div.style.left = mouseX + 20;
	infobox_div.style.top = mouseY - 5;
	infobox_div.innerHTML = theinfotext;

	var winheight = 0;							// this next bit needed to re-position the div if
	if (BrowserName == "Netscape" ) {					// it is going to go below the bottom of the page
		winheight = window.innerHeight;
	} else {	
		winheight = document.body.offsetHeight;
	}

	var vertical_check = mouseY + document.getElementById('infobox_div').offsetHeight;
	if (vertical_check >= winheight) {
		infobox_div.style.top = mouseY - document.getElementById('infobox_div').offsetHeight;
	}

	infobox_div.style.visibility = "visible";

	// the adding of this iframe is a hack to fix an IE bug that otherwise will display the select dropdowns on top...
	thewidth = infobox_div.offsetWidth;
	var theheight = infobox_div.offsetHeight;
	var thetop = mouseY - 5;
	var theleft = mouseX + 20;
	var iframe_txt = "<iframe id=\"iFrameID\" src=\"\" scrolling=\"no\" frameborder=\"0\"";
	iframe_txt += "style=\"position:absolute;width:" + thewidth + "px;top:" + thetop + "px;left:" + theleft + "px;height:" + theheight + "px;border:none;display:block;z-index:0\">";
	iFrame_div.innerHTML = iframe_txt;
}


function display_off() {					// switches off layer info display on mouse out the 'i' icon

	infobox_div.innerHTML = "";
	infobox_div.style.visibility = "hidden";
	infobox_div.style.left = 0;
	infobox_div.style.top = 0;

	// the adding of this iframe is a hack to fix an IE bug that otherwise will display the select dropdowns on top...
	var iframe_txt = "<iframe id=\"iFrameID\" src=\"\" scrolling=\"no\" frameborder=\"0\"";
	iframe_txt += "style=\"position:absolute;width:0px;top:0px;left:0px;height:0px;border:none;display:block;z-index:0\">";
	iFrame_div.innerHTML = iframe_txt;
}


function Layer_check(layer_id) {

	if (disclaimer_list_array[layer_id] == "True" ) {     		//if layer has associated disclaimer, display it
		alert(layerinfo_list_array[layer_id]);
	}

									//other layer specific instructions
	if (layers_list_display_array[layer_id] == "Adopted_Highways") {	//xxx
		document.getElementById('mono_map').checked="checked";
		changebackmap("mono");
	}
	if (layers_list_display_array[layer_id] == "Landscape character") {	//xxx
		document.getElementById('mono_map').checked="checked";
		changebackmap("mono");
	}
	if (layers_list_display_array[layer_id] == "Thermal Readings") {	//xxx
		document.getElementById('mono_map').checked="checked";
		changebackmap("mono");
	}
}


//list of layers legend info for portal (from layer admin database query) - to be used against other layers arrays
function layerlegend_array(layerlegend_list) {								//NEW 3.2

	layer_legend_array = layerlegend_list.split("#");
}



//list of legend layer info for portal (from layer admin database query) - to be used against other LEGEND arrays
function legend_layers_array_fn(legendlayers_list) {							//NEW 3.2

	legend_layers_array = legendlayers_list.split("#");
}



//list of legend items for portal (from layer admin database query)
function legend_items_array_fn(legenditems_list) {							//NEW 3.2

	legend_items_array = legenditems_list.split("#");
}



//list of legend item filenames for portal (from layer admin database query)
function legend_filenames_array_fn(legendfilename_list) {						//NEW 3.2

	legend_filenames_array = legendfilename_list.split("#");
}




function changebackmap(type) {

	dis_anim();
	if (type == "colour") {
		current_background_layers = colour_background_layers;
		document.getElementById('mono_map').checked = false;		// unchecking required to force the issue in Safari for Macs
		document.getElementById('simple_map').checked = false;
	} else if (type == "mono") {
		current_background_layers = mono_background_layers;
		document.getElementById('colour_map').checked = false;
		document.getElementById('simple_map').checked = false;
	} else if (type == "simple") {
		current_background_layers = simple_background_layers;
		document.getElementById('mono_map').checked = false;
		document.getElementById('colour_map').checked = false;
	}
	getMap();
}



//	These two functions set up the variables that hold the geoset selectable layers list (array), and 
//	list of layers NOT currently shown on the map.

// Fetch the list of layers for the geoset
function FetchLayerList() {

	xmlRequestObj = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")
	xmlRequestObj.onreadystatechange = proccessXML;
	var xml_url = serverpath + "dialogbox_LayerInformation.asp?theme=" + thetheme;
	xmlRequestObj.open("GET", xml_url, true);
	xmlRequestObj.send(null);
}


function proccessXML() {
	if (xmlRequestObj.readyState == 4 && (xmlRequestObj.status == 200 || xmlRequestObj.status == 304)) {
		xmlDoc = xmlRequestObj.responseXML;
		createLayersList();
	}
}


// Create an array of all selectable layers, and a string of layers NOT on map (separated by commas)
function createLayersList()
{
	var x = 0;
	LayerIndex = 0;
	rootNode = xmlDoc.documentElement;
	for ( x = 0; x < rootNode.childNodes.length; x++ ) {
	   	var LayerNode = rootNode.childNodes.item(x);
	   	if ( LayerNode.getAttribute("Selectable") == "True" ) {
	      		// Add Layer to Layers Array
	     		GSTLayerList[LayerIndex] = LayerNode.getAttribute("Name");
			LayerIndex += 1;
		}
	}
	GSTnum_layers = LayerIndex;
	setinfolayers();
}


//	This function edits the not_current_INFO_layers variable to reflect changes in 
//	the active layers - used for the info tool - layers to remove.
function setinfolayers() {

	not_current_INFO_layers = GSTLayerList.join(",");
	not_current_INFO_layers = not_current_INFO_layers + ",";
	var x = 0;
	var layer_remove = "";
	for (x in layers_list_array) {
	   if (layers_list_active_array[x] == "Yes") {
		var layervisible = zoom_checklayervisible(x);
		if (layervisible == "yes") {	
			layer_remove = layers_list_array[x] + ",";
			not_current_INFO_layers = not_current_INFO_layers.replace(layer_remove,"");
		} else {										//if outside zoom range
			//do not remove - either not active or not in current zoom
		}
	   }
	}
	not_current_INFO_layers = not_current_INFO_layers.replace(/,,/g,",");
	not_current_INFO_layers = not_current_INFO_layers.substr(0,(not_current_INFO_layers.length - 1))
}


function zoom_checklayervisible(x) {					//checks if layer is visible - ie has no zoom settings, or has zoom
									//settings, but is currently visible - based on admin db entries			
	var thelayerzoomsettings = layerzoom_list_array[x];
	var layerzoom_array = thelayerzoomsettings.split("#");
	var low_zoom = Number(layerzoom_array[0]);
	var top_zoom = Number(layerzoom_array[1]);
	if (thelayerzoomsettings == "") {
		return "yes";
	} else if ((Number(zoom) >= low_zoom) && (Number(zoom) <= top_zoom)) {
		return "yes";
	} else if (Number(zoom) < low_zoom){
		return "no_too_far_in";
	} else if (Number(zoom) > top_zoom){
		return "no_too_far_out";
	}
}



function SectionLayerChange(thesection_no,thesection,checked_val) {

	dis_anim();
	var section_test = "";

	if (checked_val == "true") {					// if have clicked to show all section layers
		sections_ticked_array[thesection_no] = "Yes";
		var thelayer_section = "";
		for (x in layers_list_array) {				// switch on each layer with that section title
			thelayer_section = section_list_array[x];
			if (thelayer_section == thesection) {
				layers_list_active_array[x] = "Yes";
				add_layer_to_fmn_array(layers_list_display_array[x]);	//xxx
			}
		}
	} else {							// if have clicked to switch off whole section
		sections_ticked_array[thesection_no] = "No";
		var thelayer_section = "";
		for (x in layers_list_array) {				// switch on each layer with that section title
			thelayer_section = section_list_array[x];
			if (thelayer_section == thesection) {
				layers_list_active_array[x] = "No"
			}
		}
	}

	current_layers = "";
	for (x in layers_list_array) {
		if (layers_list_active_array[x] == "Yes") {
			current_layers += layers_list_array[x] + ",";
		}
	}

	create_add_layers_menu();
	setup_menu_panel();
	var theelementid = "sectick_" + thesection_no;
	document.getElementById(theelementid).focus();

	getMap();
}



function LayerChange(chkbox_id,checked_val) {

	dis_anim();

	if (checked_val == "true") {
		layers_list_active_array[chkbox_id] = "Yes";
		Layer_check(chkbox_id);
		add_layer_to_fmn_array(layers_list_display_array[chkbox_id]);	//xxx
	} else { 
		layers_list_active_array[chkbox_id] = "No"; 
	}

	current_layers = "";
	for (x in layers_list_array) {
		if (layers_list_active_array[x] == "Yes") {
			current_layers += layers_list_array[x] + ",";
		}
	}
	create_add_layers_menu();
	setup_menu_panel();
	document.getElementById(chkbox_id).focus();

	getMap();
}



function Layers_Clear() {

	dis_anim();
	current_layers = "";
	for (x in layers_list_array) {
		layers_list_active_array[x] = "No";
	}
	for (y in sections_ticked_array) {
		sections_ticked_array[y] = "No";
	}
	create_add_layers_menu();
	create_current_layers_menu();
	setup_menu_panel();
	getMap();
}



function Remove_layer(remove_layer) {

	dis_anim();
	current_layers = "";
	for (x in layers_list_array) {
		if (layers_list_array[x] == remove_layer) {
			layers_list_active_array[x] = "No"
		} else if (layers_list_active_array[x] == "Yes") {
			current_layers += layers_list_array[x] + ",";
		}
	}	
	create_add_layers_menu();
	create_current_layers_menu();
	setup_menu_panel();
	getMap();
}



//called on clicking historical map check box
function Historical_click(checked_val) {

	if ((checked_val == "true") && (Historical_drop.Historic_option.selectedIndex != 0)) {
		historicmap(Historical_drop.Historic_option.selectedIndex);
	} else if (checked_val == "true") {
		historic_check = 1;
		//do nothing
	} else {
		dis_anim();
		Historical_drop.Historic_option.selectedIndex = 0;
		historical_sel = 0;
		historical_layers = "";
		historic_check = 0;
		getMap();		
	}

	create_add_layers_menu();
	setup_menu_panel();

	if (checked_val == "true") {
		document.getElementById("Historic_option").focus();
	} else { 
		document.getElementById("historic").focus();
	}
}


	
//changes historic map - called from dropdown change or historical_click function
function historicmap(map_option) {

	dis_anim();
	historic_check = 1;

	if ((map_option == "1843_1893") || (map_option == 1)) {
		historical_layers = "Urban_Areas,S10k_1843_1893,No_historic_maps,";
		historical_sel = 1;
	} else if ((map_option == "1891_1912") || (map_option == 2)) {
		historical_layers = "Urban_Areas,S10k_1891_1912,No_historic_maps,";
		historical_sel = 2;
	} else if ((map_option == "1904_1939") || (map_option == 3)) {
		historical_layers = "Urban_Areas,S10k_1904_1939,No_historic_maps,";
		historical_sel = 3;
	} else if ((map_option == "1919_1939") || (map_option == 4)) {
		historical_layers = "Urban_Areas,S10k_1919_1939,No_historic_maps,";
		historical_sel = 4;
	} else if ((map_option == "None (Remove)") || (map_option == 5)) {
		historical_layers = "";
		historical_sel = 0;
		historic_check = 0;
		//Historical_drop.Historic_option.selectedIndex = 0;
	} 
	create_add_layers_menu();
	setup_menu_panel();
	document.getElementById("Historic_option").focus();
        Historical_drop.Historic_option.selectedIndex = historical_sel;
	getMap();
}



function switchFMNradio(loc_type) {		// Called on click in pc box or drop down list to auto-switch radio buttons

	if (loc_type == "place") {
		FMNloc_type = "fmn_optionloc";
		document.getElementById('fmnlocpl').checked = "checked";
		rbswitch("fmnloc");
	} else {
		FMNloc_type = "fmn_optionpc";
		document.getElementById('fmnlocpc').checked = "checked";
		document.getElementById('fmnlocpc').checked = "checked";
		rbswitch("fmnpc");
	}
}


//switch radio buttons off - needed to force the issue for Safari on Macs
function rbswitch(button_on) {

	if (button_on == "fmnpc") {
		document.getElementById('fmnlocpl').checked = false;
	} else if (button_on == "fmnloc") {
		document.getElementById('fmnlocpc').checked = false;
	} else if (button_on == "fmn_km") {
		document.getElementById('fmn_miles').checked = false;
	} else if (button_on == "fmn_miles") {
		document.getElementById('fmn_km').checked = false;
	} 
}





// *****************************************************************************************************
// ****************************************** Menu operation *******************************************
// *****************************************************************************************************

function setup_inital_menu_text() {

	menu_add_layers_txt_off = "<tr><td width=\"22\" height=\"25\" class=\"menu_bar\"><img border=\"0\" src=\"images/menu_arrow.gif\" onclick=\"change_menu(\'add_layers\')\" width=\"14\" height=\"14\" style=\"cursor:pointer\" alt=\"Change Map Information menu\" title=\"click here to open Change Map Information menu\"></td>";
	menu_add_layers_txt_off += "<td id=\"menu_add_layers\" class=\"menu_bar\" onclick=\"change_menu(\'add_layers\')\" style=\"cursor:pointer\" tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || change_menu(\'add_layers\')\">Change map information</td></tr>";
	menu_add_layers_txt_off += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";

	menu_current_layers_txt_off = "<tr><td width=\"22\" height=\"25\" class=\"menu_bar\"><img border=\"0\" src=\"images/menu_arrow.gif\" onclick=\"change_menu(\'current_layers\')\" width=\"14\" height=\"14\" style=\"cursor:pointer\" alt=\"Map Legend menu\" title=\"click here to open Map Legend / Key menu\"></td>";
	menu_current_layers_txt_off += "<td id=\"menu_current_layers\" class=\"menu_bar\" onclick=\"change_menu(\'current_layers\')\" style=\"cursor:pointer\" tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || change_menu(\'current_layers\')\">Map Legend / Key</td></tr>";
	menu_current_layers_txt_off += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";

	menu_search_results_txt_off = "<tr><td width=\"22\" height=\"25\" class=\"menu_bar\"><img border=\"0\" src=\"images/menu_arrow.gif\" onclick=\"change_menu(\'search_results\')\" width=\"14\" height=\"14\" style=\"cursor:pointer\" alt=\"Search Results menu\" title=\"click here to open Search Results menu\"></td>";
	menu_search_results_txt_off += "<td id=\"menu_search_results\" class=\"menu_bar\" onclick=\"change_menu(\'search_results\')\" style=\"cursor:pointer\" tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || change_menu(\'search_results\')\">Search results</td></tr>";
	menu_search_results_txt_off += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";

	menu_find_places_txt_off = "<tr><td width=\"22\" height=\"25\" class=\"menu_bar\"><img border=\"0\" src=\"images/menu_arrow.gif\" onclick=\"change_menu(\'find_places\')\" width=\"14\" height=\"14\" style=\"cursor:pointer\" alt=\"Find Places menu\" title=\"click here to open Find Places menu\"></td>";
	menu_find_places_txt_off += "<td id=\"menu_find_places\" class=\"menu_bar\" onclick=\"change_menu(\'find_places\')\" style=\"cursor:pointer\" tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || change_menu(\'find_places\')\">Find places on the map</td></tr>";
	menu_find_places_txt_off += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";

	menu_search_results_txt_on = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" style=\"cursor:pointer\" alt=\"\" title=\"\"></td>";
	menu_search_results_txt_on += "<td width=\"250\" id=\"menu_search_results\" class=\"sel_menu_bar\" style=\"cursor:pointer\">Search results</td></tr>";
	menu_search_results_txt_on += "<tr><td colspan=\"2\" class=\"menu_panel\">";
	// PANEL TABLE
	menu_search_results_txt_on += "<table width=\"100%\" height=\"100%\" class=\"menu_panel_table\">";
	menu_search_results_txt_on += "<tr><td width=\"100%\" class=\"menu_subpanel\" valign=\"top\">";
	menu_search_results_txt_on += "There are currently no search results to display";
	menu_search_results_txt_on += "</td></tr></table>";
	// END OF PANEL TABLE
	menu_search_results_txt_on += "</td></tr>";
	menu_search_results_txt_on += "<tr><td class=\"menu_bar_spacer\"></td><td height=\"2\" class=\"menu_bar_spacer\"></td></tr>";

	create_findplaces_menu();
	create_add_layers_menu();
	create_current_layers_menu();
}



function display_info_test(theinfotext,e,thisObject) {				// NEW 3.2 To try & stop firefox problem

	var new_displayinfo_selectbox_id = thisObject.id;
	if (new_displayinfo_selectbox_id != displayinfo_selectbox_id) {
		display_info(theinfotext,e,thisObject);
		displayinfo_selectbox_id = new_displayinfo_selectbox_id;
	} else { 
		display_off();
	}
}


function create_findplaces_menu() {

	menu_find_places_txt_on = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" style=\"cursor:pointer\" alt=\"\" title=\"\"></td>";
	menu_find_places_txt_on += "<td id=\"menu_find_places\" class=\"sel_menu_bar\" style=\"cursor:pointer\">Find places on the map</td></tr>";
	menu_find_places_txt_on += "<tr><td colspan=\"2\" class=\"menu_panel\">";
	// PANEL TABLE
	menu_find_places_txt_on += "<table width=\"100%\" height=\"100%\" class=\"menu_panel_table\"><tr><td colspan=\"3\">";
	menu_find_places_txt_on += "<table width=\"100%\"><tr><td width=\"100%\" height=\"3\"></td></tr><tr><td class=\"panel_title\">Find a place:</td></tr>";
	menu_find_places_txt_on += "<tr><td width=\"100%\" height=\"3\"></td></tr>";
	menu_find_places_txt_on += "<tr><td align=\"left\">";
	menu_find_places_txt_on += "&nbsp;<select class=\"combobox\" id=\"location_option\" name=\"location_option\" style=\"width: 190\" onmouseover=\"display_info_test(\'Choose a placename from the list, click Find, and the map will move to it.\',event,this)\" onmouseout=\"display_off();\" onclick=\"display_info_test(\'Choose a placename from the list, click Find, and the map will move to it.\',event,this)\">";
	menu_find_places_txt_on += "<option selected  onmouseover=\"display_off()\">Select location... </option>";
	for ( x = 1; x < Loc_name_array.length; x++ ) {
		menu_find_places_txt_on += "<option value=\"" + x + "\" id=\"" + Loc_name_array[x] + "\"  onmouseover=\"display_off()\">" + Loc_name_array[x] + "</option>";
	}
	menu_find_places_txt_on += "</select>&nbsp;<input type=\"button\" name=\"locsearchb\" id=\"locsearchb\" onclick=\"loc_find(\'loc\');display_off();\" align=\"left\" value=\"Find\" style=\"cursor:pointer\" onmouseover=\"display_info(\'Click here to find the place.\',event,this)\" onmouseout=\"display_off()\">";
	menu_find_places_txt_on += "</td></tr></table></td></tr>";

	menu_find_places_txt_on += "<tr><td height=\"17\" colspan=\"3\" align=\"center\" valign=\"middle\"><img border=\"0\" src=\"images/HR.gif\" height=\"2\" alt=\"\" title=\"\"></td></tr>";

	menu_find_places_txt_on += "<tr><td colspan=\"3\"><table><tr><td colspan=\"2\" class=\"panel_title\"><u>OR</u>&nbsp;&nbsp;Find a full / partial address:</td></tr>";
	menu_find_places_txt_on += "<tr><td height=\"3\" colspan=\"2\"></td></tr>";
	menu_find_places_txt_on += "<tr><td class=\"panel_text\" width=\"35\">Name</td><td width=\"220\"><input type=\"text\" name=\"addName\" id=\"addName\" size=\"25\" class=\"panel_box\" value=\"" + AddName + "\" onKeyPress=\"checkEnter(event)\" onmouseover=\"display_info(\'Enter the name of the organisation or building.\',event,this)\" onmouseout=\"display_off()\"></td></tr>";
	menu_find_places_txt_on += "<tr><td class=\"panel_text\">Number</td><td ><input type=\"text\" name=\"addNumber\" id=\"addNumber\" size=\"10\" class=\"panel_box\" value=\"" + AddNum + "\" onKeyPress=\"checkEnter(event)\" onmouseover=\"display_info(\'Enter the building number.\',event,this)\" onmouseout=\"display_off()\"></td></tr>";
	menu_find_places_txt_on += "<tr><td class=\"panel_text\">Road</td><td ><input type=\"text\" name=\"addRoad\" id=\"addRoad\" size=\"25\" class=\"panel_box\" value=\"" + AddRoad + "\" onKeyPress=\"checkEnter(event)\" onmouseover=\"display_info(\'Enter the road name.\',event,this)\" onmouseout=\"display_off()\"></td></tr>";
	menu_find_places_txt_on += "<tr><td class=\"panel_text\">Postcode</td><td ><input type=\"text\" name=\"addPostcode\" id=\"addPostcode\" size=\"10\" class=\"panel_box\" onBlur=\"this.value=ignoreSpaces(this.value);\" value=\"" + AddPC + "\" onKeyPress=\"this.value=ignoreSpaces(this.value);javascript:checkEnter(event)\" onmouseover=\"display_info(\'Enter the full postcode, e.g. DE4 3AG.\',event,this)\" onmouseout=\"display_off()\"></td></tr>";
	menu_find_places_txt_on += "<tr height=\"30\"><td>&nbsp;</td><td valign=\"bottom\"><input type=\"button\" name=\"addsearchb\" id=\"addsearchb\" onclick=\"Add_Search()\" align=\"left\" value=\"Search\" style=\"cursor:pointer\" onmouseover=\"display_info(\'Click here to start the Address Search.\',event,this)\" onmouseout=\"display_off()\">";
	menu_find_places_txt_on += "&nbsp;<input type=\"button\" name=\"addclearb\" id=\"addclearb\" onclick=\"Add_Clear()\" align=\"left\" value=\" Clear \" style=\"cursor:pointer\" onmouseover=\"display_info(\'Click here to clear all the address search fields.\',event,this)\" onmouseout=\"display_off()\"></td></tr></table></td></tr>";

	menu_find_places_txt_on += "<tr><td height=\"17\" colspan=\"3\" align=\"center\" valign=\"middle\"><img border=\"0\" src=\"images/HR.gif\" height=\"2\" alt=\"\" title=\"\"></td></tr>";

	menu_find_places_txt_on += "<tr><td colspan=\"3\"><table>";
	menu_find_places_txt_on += "<tr><td class=\"panel_title\" colspan=\"3\"><u>OR</u>&nbsp;&nbsp;Find your nearest:</td></tr>";
	menu_find_places_txt_on += "<tr><td height=\"3\" colspan=\"3\"></td></tr>";
	if (FMNloc_type == "fmn_optionpc") {
		menu_find_places_txt_on += "<tr><td class=\"panel_text\"><input name=\"fmnloc\" id=\"fmnlocpc\" type=\"radio\" value=\"fmn_optionpc\" onClick=\"rbswitch(\'fmnpc\')\" checked style=\"cursor:pointer\"  onmouseover=\"display_info(\'Search by postcode.\',event,this)\" onmouseout=\"display_off()\">&nbsp;Postcode</td>";
		menu_find_places_txt_on += "<td><input type=\"text\" name=\"fmnPostcode\" id=\"fmnPostcode\" size=\"10\" class=\"panel_box\" onBlur=\"this.value=ignoreSpaces(this.value);\" onFocus=\"switchFMNradio(\'pc\');\" value=\"" + FMNPC + "\" onmouseover=\"display_info(\'Enter the postcode.\',event,this)\" onmouseout=\"display_off()\"></td><td class=\"panel_text\">(e.g. DE4 3AG)</td></tr>";
		menu_find_places_txt_on += "<tr><td class=\"panel_text\"><input name=\"fmnloc\" id=\"fmnlocpl\" type=\"radio\" value=\"fmn_optionloc\" onClick=\"rbswitch(\'fmnloc\')\" style=\"cursor:pointer\" onmouseover=\"display_info(\'Search by placename.\',event,this)\" onmouseout=\"display_off()\">&nbsp;Place</td>";
	} else {
		menu_find_places_txt_on += "<tr><td class=\"panel_text\"><input name=\"fmnloc\" id=\"fmnlocpc\" type=\"radio\" value=\"fmn_optionpc\" onClick=\"rbswitch(\'fmnpc\')\" style=\"cursor:pointer\" onmouseover=\"display_info(\'Search by postcode.\',event,this)\" onmouseout=\"display_off()\">&nbsp;Postcode</td>";
		menu_find_places_txt_on += "<td><input type=\"text\" name=\"fmnPostcode\" id=\"fmnPostcode\" size=\"10\" class=\"panel_box\" onBlur=\"this.value=ignoreSpaces(this.value);\" onFocus=\"switchFMNradio(\'pc\');\" value=\"" + FMNPC + "\" onmouseover=\"display_info(\'Enter the postcode.\',event,this)\" onmouseout=\"display_off()\"></td><td class=\"panel_text\">(e.g. DE4 3AG)</td></tr>";
		menu_find_places_txt_on += "<tr><td class=\"panel_text\"><input name=\"fmnloc\" id=\"fmnlocpl\" type=\"radio\" value=\"fmn_optionloc\" onClick=\"rbswitch(\'fmnloc\')\" checked style=\"cursor:pointer\" onmouseover=\"display_info(\'Search by placename.\',event,this)\" onmouseout=\"display_off()\">&nbsp;Place</td>";
	}

	menu_find_places_txt_on += "<td colspan=\"2\"><select class=\"combobox\" id=\"fmnlocation_option\" name=\"fmnlocation_option\" onChange=\"switchFMNradio(\'place\');loc_find(\'fmn\');\" style=\"width: 175\"  onmouseover=\"display_info_test(\'Choose a placename from the list.\',event,this)\" onmouseout=\"display_off()\">";
	menu_find_places_txt_on += "<option selected>Select location... </option>";
	for ( x = 1; x < Loc_name_array.length; x++ ) {
		if (FMNloc_index == x) {
			menu_find_places_txt_on += "<option value=\"" + x + "\" id=\"" + Loc_name_array[x] + "\" selected=\"selected\">" + Loc_name_array[x] + "</option>";
		} else {
			menu_find_places_txt_on += "<option value=\"" + x + "\" id=\"" + Loc_name_array[x] + "\">" + Loc_name_array[x] + "</option>";
		}
	}
	menu_find_places_txt_on += "</select></td></tr>";

	menu_find_places_txt_on += "<tr><td class=\"panel_text\">&nbsp;Distance<br>&nbsp;(to search)</td>";
	menu_find_places_txt_on += "<td><input type=\"text\" name=\"fmnradius\" id=\"fmnradius\" size=\"10\" value=\"" + FMNrad + "\" class=\"panel_box\" onmouseover=\"display_info(\'Enter the distance (as a number).\',event,this)\" onmouseout=\"display_off()\"></td>";
	menu_find_places_txt_on += "<td class=\"panel_text\">";
	if (FMNmeasure == "km") {
		menu_find_places_txt_on += "<input name=\"fmnmeasure\" id=\"fmn_km\" type=\"radio\" value=\"fmn_km\" onClick=\"rbswitch(\'fmn_km\')\" checked style=\"cursor:pointer\"  onmouseover=\"display_info(\'Set distance units to kilometres.\',event,this)\" onmouseout=\"display_off()\">km";
		menu_find_places_txt_on += "<input name=\"fmnmeasure\" id=\"fmn_miles\" type=\"radio\" value=\"fmn_miles\" onClick=\"rbswitch(\'fmn_miles\')\" style=\"cursor:pointer\" onmouseover=\"display_info(\'Set distance units to miles.\',event,this)\" onmouseout=\"display_off()\">miles";
	} else {
		menu_find_places_txt_on += "<input name=\"fmnmeasure\" id=\"fmn_km\" type=\"radio\" value=\"fmn_km\" onClick=\"rbswitch(\'fmn_km\')\" style=\"cursor:pointer\" onmouseover=\"display_info(\'Set distance units to kilometres.\',event,this)\" onmouseout=\"display_off()\">km";
		menu_find_places_txt_on += "<input name=\"fmnmeasure\" id=\"fmn_miles\" type=\"radio\" value=\"fmn_miles\" onClick=\"rbswitch(\'fmn_miles\')\" checked style=\"cursor:pointer\" onmouseover=\"display_info(\'Set distance units to miles.\',event,this)\" onmouseout=\"display_off()\">miles";
	}
	menu_find_places_txt_on += "</td></tr>";

	menu_find_places_txt_on += "<tr><td colspan=\"3\">&nbsp;";
	menu_find_places_txt_on += "<select class=\"combobox\" name=\"fmnLayerName\" id=\"fmnLayerName\" style=\"width: 170\" onmouseover=\"display_info_test(\'Choose information to search for from the list.\',event,this)\" onmouseout=\"display_off()\">";
	menu_find_places_txt_on += "<option value=\"\" selected> I want to find..</option>";
	for ( x = 0; x < fmn_live_list_array.length; x++ ) {
		menu_find_places_txt_on += "<option value=\"" + fmn_live_list_array[x] + "\" id=\"" + fmn_live_list_array[x] + "\">" + fmn_live_list_array[x] + "</option>";
	}	
	menu_find_places_txt_on += "</select>&nbsp;&nbsp;<input type=\"button\" name=\"fmnsearchb\" id=\"fmnsearchb\" onclick=\"FMN_Search();display_off();\" align=\"left\" value=\"Search\" style=\"cursor:pointer\" onmouseover=\"display_info(\'Click here to start the Find Your Nearest Search.\',event,this)\" onmouseout=\"display_off()\">";
	menu_find_places_txt_on += "</td></tr><tr><td height=\"10\" colspan=\"3\"></td></tr></table></td></tr>";

	menu_find_places_txt_on += "</table>";
	// END OF PANEL TABLE
	menu_find_places_txt_on += "</td></tr>";
}




function create_add_layers_menu() {

	menu_add_layers_txt_on = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" style=\"cursor:pointer\" alt=\"\" title=\"\"></td>";
	menu_add_layers_txt_on += "<td id=\"menu_add_layers\" class=\"sel_menu_bar\" style=\"cursor:pointer\">Change map information</td></tr>";
	menu_add_layers_txt_on += "<tr><td colspan=\"2\" class=\"menu_panel\">";
	// PANEL TABLE
	menu_add_layers_txt_on += "<div id=\"layercontrol\" STYLE=\"overflow:auto; height:390; width:270; text-align:left\" NOWRAP>";
	menu_add_layers_txt_on += "<table width=\"100%\" class=\"menu_panel_table\">";
	menu_add_layers_txt_on += "<tr><td class=\"panel_title\" colspan=\"4\" valign=\"top\">Information available</td></tr>";
	menu_add_layers_txt_on += "<tr><td align=\"left\" class=\"panel_text\" colspan=\"4\"> Click on the subject headings below to open or close their options.</td></tr>";
	menu_add_layers_txt_on += "<tr><td align=\"left\" class=\"panel_text\" colspan=\"4\"> Select the check box next to the information you wish to add or remove from the map.</td></tr>";
	menu_add_layers_txt_on += "<tr><td class=\"panel_text\" height=\"5\" colspan=\"4\"></td></tr>";

	var thesection = "";
	var thesection_no = -1;
	for ( x = 0; x < layers_list_array.length; x++ ) {

		if (thesection != section_list_array[x]) {
			thesection = section_list_array[x];
			thesection_no = thesection_no + 1;
			menu_add_layers_txt_on += "<tr><td class=\"panel_text\" height=\"3\" colspan=\"4\"></td></tr>";
			if (sections_active_array[thesection_no] == "No") {
				menu_add_layers_txt_on += "<tr><td align=\"center\" class=\"layers_title\" width=\"20\"><img src=\"images/dot.gif\" align=\"center\" alt=\"\" title=\"\"></td>";
				menu_add_layers_txt_on += "<td id=\"anc_" + thesection_no + "\" class=\"layers_title\" height=\"18\" width=\"240\" colspan=\"3\" onclick=\"change_menu_layers(\'" + thesection_no + "\')\" tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || change_menu_layers(\'" + thesection_no + "\')\" style=\"cursor:pointer\">&nbsp;" + section_list_array[x] + "</td></tr>";
			} else if (sections_active_array[thesection_no] == "Yes") {
				if (sections_ticked_array[thesection_no] == "Yes") {
					menu_add_layers_txt_on += "<tr><td align=\"left\" class=\"layers_title\" width=\"20\"><input type=\"checkbox\" checked name=\"sectick_" + thesection_no + "\" id=\"sectick_" + thesection_no + "\" onClick=\"SectionLayerChange(" + thesection_no + ", \'" + section_list_array[x] + "\',\'false\')\" tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || SectionLayerChange(" + thesection_no + ", \'" + section_list_array[x] + "\')\"></td>";
				} else {
					menu_add_layers_txt_on += "<tr><td align=\"left\" class=\"layers_title\" width=\"20\"><input type=\"checkbox\" name=\"sectick_" + thesection_no + "\" id=\"sectick_" + thesection_no + "\" onClick=\"SectionLayerChange(" + thesection_no + ", \'" + section_list_array[x] + "\',\'true\')\" tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || SectionLayerChange(" + thesection_no + ", \'" + section_list_array[x] + "\',\'true\')\"></td>";
				}
				menu_add_layers_txt_on += "<td id=\"anc_" + thesection_no + "\" class=\"layers_title\" height=\"18\" width=\"240\" colspan=\"3\" onclick=\"change_menu_layers(\'" + thesection_no + "\')\"  tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || change_menu_layers(\'" + thesection_no + "\')\" style=\"cursor:pointer\">&nbsp;" + section_list_array[x] + "</td></tr>";
			}
		}

		if (sections_active_array[thesection_no] == "No") {
			//don't list layers
		} else if (sections_active_array[thesection_no] == "Yes") {
			//list layers
			if (layers_list_active_array[x] == "Yes") {
				menu_add_layers_txt_on += "<tr><td align=\"left\" width=\"20\"><input type=\"checkbox\" checked name=\"" + x + "\" id=\"" + x + "\" onClick=\"LayerChange(" + x + ",\'false\')\" tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || LayerChange(" + x + ",\'false\')\"></td>";
			} else {
				menu_add_layers_txt_on += "<tr><td align=\"left\" width=\"20\"><input type=\"checkbox\" name=\"" + x + "\" id=\"" + x + "\"  onClick=\"LayerChange(" + x + ",\'true\')\" tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || LayerChange(" + x + ",\'true\')\"></td>";
			}		

			//if (layer_legend_array[x] == "True") {
			//	var img_text = "";
			//} else {
				var img_src = "http://derbyshiremaps.derbyshire.gov.uk/portal_32/images/MAPKEY/" + layers_list_display_array[x] + ".gif";	//xxx
				var img_text = "<img border=\"0\" src=\"" + img_src + "\" alt=\"Map feature symbol\" title=\"symbol showing how this information will appear on the map\">";
			//}

			var layervisible = zoom_checklayervisible(x);
			var theinfo = layerinfo_list_array[x];
			if (layervisible == "yes") {
				menu_add_layers_txt_on += "<td align=\"left\" width=\"224\" class=\"panel_text\"  colspan=\"2\" valign=\"bottom\">" + img_text + layers_list_display_array[x] + "</td>";	//xxx
				menu_add_layers_txt_on += "<td align=\"right\" width=\"16\" class=\"panel_text\" valign=\"top\">";	
				menu_add_layers_txt_on += "<img border=\"0\" src=\"images/buttons/layer_info.gif\" id=\"layerinfob\" onmouseover=\"display_info(\'<b>" + layers_list_display_array[x] + "</b><br>" + theinfo + "\',event,this)\" onmouseout=\"display_off()\" width=\"16\" height=\"19\" align=\"right\" valign=\"middle\" alt=\"\" title=\"\"></td></tr>";	//xxx
			} else if (layervisible == "no_too_far_in") {
				menu_add_layers_txt_on += "<td align=\"left\" width=\"224\" class=\"panel_text\"  colspan=\"2\" valign=\"bottom\" style=\"cursor:pointer\" onmouseover=\"display_info(\'<i>You need to zoom the map out a bit to see this layer.</i>\',event,this)\" onmouseout=\"display_off()\">" + img_text + "<font color=\"red\"> <i><u>" + layers_list_display_array[x] + "</u></i></font></td>";	//xxx
				menu_add_layers_txt_on += "<td align=\"right\" width=\"16\" class=\"panel_text\" valign=\"top\">";	
				menu_add_layers_txt_on += "<img border=\"0\" src=\"images/buttons/layer_info.gif\" id=\"layerinfob\" onmouseover=\"display_info(\'<b>" + layers_list_display_array[x] + "</b><i> - You need to zoom the map out a bit to see this layer.</i><br>" + theinfo + "\',event,this)\" onmouseout=\"display_off()\" width=\"16\" height=\"19\" align=\"right\" valign=\"middle\" alt=\"\" title=\"\"></td></tr>";	//xxx
			} else if (layervisible == "no_too_far_out") {
				menu_add_layers_txt_on += "<td align=\"left\" width=\"224\" class=\"panel_text\"  colspan=\"2\" valign=\"bottom\" style=\"cursor:pointer\" onmouseover=\"display_info(\'<i>You need to zoom the map in a bit to see this layer.</i>\',event,this)\" onmouseout=\"display_off()\">" + img_text + "<font color=\"red\"> <i><u>" + layers_list_display_array[x] + "</u></i></font></td>";	//xxx
				menu_add_layers_txt_on += "<td align=\"right\" width=\"16\" class=\"panel_text\" valign=\"top\">";	
				menu_add_layers_txt_on += "<img border=\"0\" src=\"images/buttons/layer_info.gif\" id=\"layerinfob\" onmouseover=\"display_info(\'<b>" + layers_list_display_array[x] + "</b><i> - You need to zoom the map in a bit to see this layer.</i><br>" + theinfo + "\',event,this)\" onmouseout=\"display_off()\" width=\"16\" height=\"19\" align=\"right\" valign=\"middle\" alt=\"\" title=\"\"></td></tr>";	//xxx
			}


			//if (layer_legend_array[x] == "True") {

			//	for ( y = 0; y < legend_layers_array.length; y++ ) {

			//		if (layers_list_array[x] == legend_layers_array[y]) {

			//			var img_src = "http://derbyshiremaps.derbyshire.gov.uk/portal_32/images/MAPKEY/" + legend_filenames_array[y] + ".gif";
			//			menu_add_layers_txt_on += "<tr><td></td><td align=\"left\" class=\"panel_text\" colspan=\"2\" valign=\"middle\"><img border=\"0\" src=\"" + img_src + "\" alt=\"Map feature symbol\" title=\"symbol showing how this information will appear on the map\">";
			//			menu_add_layers_txt_on += "&nbsp;&nbsp;&nbsp;" + legend_items_array[y] + "</td></tr>";
			//		}
			//	}
			//} 
		}	
	}

	menu_add_layers_txt_on += "<tr><td class=\"panel_text\" height=\"3\" colspan=\"4\"></td></tr>"
	menu_add_layers_txt_on += "<tr><td align=\"left\" class=\"panel_text\" colspan=\"4\"><input type=\"button\" id=\"remlayersb\" name=\"remlayersb\" align=\"left\" value=\"Remove all info from map\" onclick=\"Layers_Clear()\" style=\"cursor:pointer\"></td></tr>"

	//Historical maps
	menu_add_layers_txt_on += "<tr><td class=\"panel_text\" height=\"20\" colspan=\"4\"></td></tr>";
	menu_add_layers_txt_on += "<tr><td align=\"center\" class=\"layers_title\" width=\"20\"><img src=\"images/dot.gif\" align=\"center\" alt=\"\" title=\"\"></td><td id=\"anc_historical\" class=\"layers_title\" height=\"18\" width=\"240\" colspan=\"3\">&nbsp;Historical Maps</td></tr>";
	if (historic_check == 1) {
		menu_add_layers_txt_on += "<tr><td align=\"left\" width=\"20\"><input type=\"checkbox\" name=\"historic\" id=\"historic\" onClick=\"Historical_click(\'false\')\" checked tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || Historical_click(\'false\')\"></td>";
	} else {
		menu_add_layers_txt_on += "<tr><td align=\"left\" width=\"20\"><input type=\"checkbox\" name=\"historic\" id=\"historic\" onClick=\"Historical_click(\'true\')\" tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || Historical_click(\'true\')\"></td>";
	}
	menu_add_layers_txt_on += "<td align=\"left\" width=\"200\" class=\"panel_text\">Historical Maps</td>";
	menu_add_layers_txt_on += "<td align=\"right\" width=\"20\" class=\"panel_text\" colspan=\"2\"><a class=\"panel_text\" href=\"javascript:links_post('historical_help')\" title=\"Information on using historical mapping\">[help]</a></td></tr>";

	menu_add_layers_txt_on += "<tr><td></td><td><form name=\"Historical_drop\" id=\"Historical_drop\" colspan=\"3\"><select class=\"combobox\" id=\"Historic_option\" name=\"Historic_option\" onChange=\"historicmap(options[selectedIndex].value)\" size=\"1\" style=\"font-size: 8pt\">";
	menu_add_layers_txt_on += "<option>Select period...</option>";
	menu_add_layers_txt_on += "<option value=\"1843_1893\" id=\"1843_1893\">1843-1893</option>";
	menu_add_layers_txt_on += "<option value=\"1891_1912\" id=\"1891_1912\">1891-1912</option>";
	menu_add_layers_txt_on += "<option value=\"1904_1939\" id=\"1904_1939\">1904-1939</option>";
	menu_add_layers_txt_on += "<option value=\"1919_1939\" id=\"1919_1939\">1919-1939</option>";
	if (historic_check == 1) {
		menu_add_layers_txt_on += "<option value=\"None (Remove)\" id=\"None\">None (Remove)</option>";
	}	
	menu_add_layers_txt_on += "</select></form></td></tr>";

	menu_add_layers_txt_on += "<tr><td class=\"panel_text\" height=\"6\" colspan=\"4\"></td></tr>";
	menu_add_layers_txt_on += "</table></div>";

	// END OF PANEL TABLE
	menu_add_layers_txt_on += "</td></tr>";
	menu_add_layers_txt_on += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";
}



function change_menu_layers(sec_val) {

	oldsecval = sections_active_array[sec_val];
	if (oldsecval == "No") {
		sections_active_array[sec_val] = "Yes";
	} else if (oldsecval == "Yes") {
		sections_active_array[sec_val] = "No";
	}

	create_add_layers_menu();
	setup_menu_panel();
	go_anchor("anc_" + sec_val);

	if (sections_active_array[sec_val] == "Yes") {
		document.getElementById("sectick_" + sec_val).focus();
	} else {
		document.getElementById("anc_" + sec_val).focus();
	}	
}


function go_anchor(n){
	document.getElementById("layercontrol").scrollTop = document.getElementById(n).offsetTop
}




function create_current_layers_menu() {

	menu_current_layers_txt_on = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" style=\"cursor:pointer\" alt=\"\" title=\"\"></td>";
	menu_current_layers_txt_on += "<td id=\"menu_current_layers\" class=\"sel_menu_bar\" style=\"cursor:pointer\">Map Legend / Key</td></tr>";
	menu_current_layers_txt_on += "<tr><td colspan=\"2\" class=\"menu_panel\" valign=\"top\">";
	// PANEL TABLE
	menu_current_layers_txt_on += "<div id=\"legend\" STYLE=\"overflow:auto; height:390; width:270; text-align:left\" NOWRAP>";
	menu_current_layers_txt_on += "<table width=\"100%\" class=\"menu_panel_table\" valign=\"top\">";
	menu_current_layers_txt_on += "<tr><td class=\"panel_title\" valign=\"top\" colspan=\"4\">Map Legend</td></tr>";
	menu_current_layers_txt_on += "<tr><td class=\"panel_text\" height=\"5\" colspan=\"4\"></td></tr>";

	var layer_check = 0;
	for ( x = 0; x < layers_list_array.length; x++ ) {
		if (layers_list_active_array[x] == "Yes") {
			layer_check = layer_check + 1;
		}		
	}
	if (layer_check != 0) {
		menu_current_layers_txt_on += "<tr><td align=\"right\" class=\"panel_text\" colspan=\"4\"><input type=\"button\" id=\"remlayersb\" name=\"remlayersb\" align=\"left\" value=\"Remove all from map\" onclick=\"Layers_Clear()\" style=\"cursor:pointer\"></td></tr>"
		menu_current_layers_txt_on += "<tr><td class=\"panel_text\" height=\"3\" colspan=\"4\"></td></tr>"
	}

	for ( x = 0; x < layers_list_array.length; x++ ) {
		if (layers_list_active_array[x] == "Yes") {

			if (layer_legend_array[x] == "True") {
				menu_current_layers_txt_on += "<tr><td align=\"left\" width=\"20\" valign=\"top\"><img border=\"0\" src=\"http://derbyshiremaps.derbyshire.gov.uk/portal_32/images/MAPKEY/blank_line.gif\" alt=\"\" title=\"\"></td>";
			} else {
				var img_src = "http://derbyshiremaps.derbyshire.gov.uk/portal_32/images/MAPKEY/" + layers_list_display_array[x] + ".gif";	//xxx
				menu_current_layers_txt_on += "<tr><td align=\"left\" width=\"20\" valign=\"top\"><img border=\"0\" src=\"" + img_src + "\" alt=\"Map feature symbol\" title=\"symbol showing how this information will appear on the map\"></td>";
			}

			var layervisible = zoom_checklayervisible(x);
			var theinfo = layerinfo_list_array[x];
			if (layervisible == "yes") {
				menu_current_layers_txt_on += "<td align=\"left\" width=\"175\" class=\"panel_text\" valign=\"middle\">" + layers_list_display_array[x] + "</td>";	//xxx
				menu_current_layers_txt_on += "<td align=\"center\" width=\"35\" class=\"panel_text\">";	
				menu_current_layers_txt_on += "<img border=\"0\" src=\"images/buttons/layer_info.gif\" id=\"layerinfob2\" onmouseover=\"display_info(\'<b>" + layers_list_display_array[x] + "</b><br>" + theinfo + "\',event,this)\" onmouseout=\"display_off()\" width=\"16\" height=\"19\" align=\"right\" valign=\"middle\" alt=\"\" title=\"\"></td>";	//xxx
			} else if (layervisible == "no_too_far_in") {
				menu_current_layers_txt_on += "<td align=\"left\" width=\"175\" class=\"panel_text\" valign=\"middle\" style=\"cursor:pointer\" onmouseover=\"display_info(\'<i>You need to zoom the map out a bit to see this layer.</i>\',event,this)\" onmouseout=\"display_off()\"><font color=\"red\"><i><u>" + layers_list_display_array[x] + "</u></i></font></td>";	//xxx
				menu_current_layers_txt_on += "<td align=\"center\" width=\"35\" class=\"panel_text\">";	
				menu_current_layers_txt_on += "<img border=\"0\" src=\"images/buttons/layer_info.gif\" id=\"layerinfob2\" onmouseover=\"display_info(\'<b>" + layers_list_display_array[x] + "</b><i> - You need to zoom the map out a bit to see this layer.</i><br>" + theinfo + "\',event,this)\" onmouseout=\"display_off()\" width=\"16\" height=\"19\" align=\"right\" valign=\"middle\" alt=\"\" title=\"\"></td>";	//xxx
			} else if (layervisible == "no_too_far_out") {
				menu_current_layers_txt_on += "<td align=\"left\" width=\"175\" class=\"panel_text\" valign=\"middle\" style=\"cursor:pointer\" onmouseover=\"display_info(\'<i>You need to zoom the map in a bit to see this layer.</i>\',event,this)\" onmouseout=\"display_off()\"><font color=\"red\"><i><u>" + layers_list_display_array[x] + "</u></i></font></td>";	 //xxx
				menu_current_layers_txt_on += "<td align=\"center\" width=\"35\" class=\"panel_text\">";	
				menu_current_layers_txt_on += "<img border=\"0\" src=\"images/buttons/layer_info.gif\" id=\"layerinfob2\" onmouseover=\"display_info(\'<b>" + layers_list_display_array[x] + "</b><i> - You need to zoom the map in a bit to see this layer.</i><br>" + theinfo + "\',event,this)\" onmouseout=\"display_off()\" width=\"16\" height=\"19\" align=\"right\" valign=\"middle\" alt=\"\" title=\"\"></td>";	//xxx
			}
			menu_current_layers_txt_on += "<td align=\"center\" width=\"25\" class=\"panel_text\"><img border=\"0\" src=\"images/buttons/Remove.gif\" name=\"removeb\" id=\"removeb\" onclick=\"Remove_layer(\'" + layers_list_array[x] +"\')\"  tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || Remove_layer(\'" + layers_list_array[x] +"\')\" style=\"cursor:pointer\" width=\"23\" height=\"20\" align=\"center\" valign=\"middle\" alt=\"Remove information from the map\" title=\"Click here to remove this information from the map\"></td></tr>";

			if (layer_legend_array[x] == "True") {

				for ( y = 0; y < legend_layers_array.length; y++ ) {

					if (layers_list_display_array[x] == legend_layers_array[y]) {

						menu_current_layers_txt_on += "<tr><td align=\"left\" width=\"20\" valign=\"top\"><img border=\"0\" src=\"http://derbyshiremaps.derbyshire.gov.uk/portal_32/images/MAPKEY/blank.gif\" alt=\"\" title=\"\"></td>";
						var img_src = "http://derbyshiremaps.derbyshire.gov.uk/portal_32/images/MAPKEY/" + legend_filenames_array[y] + ".gif";
						menu_current_layers_txt_on += "<td align=\"left\" class=\"panel_text\" colspan=\"3\" valign=\"middle\"><img border=\"0\" src=\"" + img_src + "\" alt=\"Map feature symbol\" title=\"symbol showing how this information will appear on the map\">";
						menu_current_layers_txt_on += "&nbsp;&nbsp;&nbsp;" + legend_items_array[y] + "</td></tr>";
					}
				}
			}
		}	
	}

	if (layer_check == 0) {
		menu_current_layers_txt_on += "<tr><td class=\"menu_subpanel\" valign=\"top\" colspan=\"4\">You currently have no<br>information added to the map.</td></tr>";
		menu_current_layers_txt_on += "<tr><td class=\"panel_text\" height=\"310\" colspan=\"4\"></td></tr>";
	} else { 
		menu_current_layers_txt_on += "<tr><td class=\"panel_text\" height=\"3\" colspan=\"4\"></td></tr>";
		menu_current_layers_txt_on += "<tr><td height=\"2\" colspan=\"4\" class=\"menu_bar_spacer\"></td></tr>";
		menu_current_layers_txt_on += "<tr><td class=\"panel_text\" height=\"15\" colspan=\"4\"></td></tr>";
		menu_current_layers_txt_on += "<tr><td align=\"left\" valign=\"top\" class=\"panel_text\"><img border=\"0\" src=\"images/NoData.gif\" alt=\"Map feature symbol for no data areas\" title=\"style showing how areas without data will be displayed on the map\"></td>"
		menu_current_layers_txt_on += "<td class=\"panel_text\" align=\"left\" valign=\"top\" colspan=\"3\">Not all map information is complete. Where information is missing, that area is shown with red hatching.</td>";
	}
	menu_current_layers_txt_on += "</table></div>";
	// END OF PANEL TABLE
	menu_current_layers_txt_on += "</td></tr>";
	menu_current_layers_txt_on += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";

}


function setup_menu_panel() {

 	menu_current_txt = "";
 	menu_current_txt = "<table cellpadding=\"0\" cellspacing=\"0\" width=\"272\" height=\"510\" border=\"0\">";
        
	switch (menu_active)
	{
	case "find_places":
  		menu_current_txt += menu_add_layers_txt_off;
  		menu_current_txt += menu_current_layers_txt_off;
  		menu_current_txt += menu_search_results_txt_off;
  		menu_current_txt += menu_find_places_txt_on;
  		break
	case "search_results":
  		menu_current_txt += menu_find_places_txt_off;
  		menu_current_txt += menu_add_layers_txt_off;
  		menu_current_txt += menu_current_layers_txt_off;
  		menu_current_txt += menu_search_results_txt_on;
  		break
	case "current_layers":
  		menu_current_txt += menu_find_places_txt_off;
  		menu_current_txt += menu_add_layers_txt_off;
  		menu_current_txt += menu_search_results_txt_off;
  		menu_current_txt += menu_current_layers_txt_on;
  		break
	case "add_layers":
  		menu_current_txt += menu_find_places_txt_off;
  		menu_current_txt += menu_current_layers_txt_off;
  		menu_current_txt += menu_search_results_txt_off;
  		menu_current_txt += menu_add_layers_txt_on;
  		break
	default:
  		menu_current_txt += menu_add_layers_txt_off;
  		menu_current_txt += menu_current_layers_txt_off;
  		menu_current_txt += menu_search_results_txt_off;
  		menu_current_txt += menu_find_places_txt_on;
	}

	menu_current_txt += "</table>";
	menus.innerHTML = menu_current_txt;     
	var focus_menu = "menu_" + menu_active;

	if (os.search(/mac/) != -1) {
		//alert("mac!");
	} else {
		//alert("not mac!");
		document.getElementById(focus_menu).focus();
	}
	
	if ((historical_sel != 0) && (menu_active == "add_layers")){
		document.getElementById("Historic_option").selectedIndex = historical_sel;
	}
	update_linkform();
}



function change_menu(themenu) {

	menu_active = themenu;
	create_add_layers_menu();
	create_current_layers_menu();
	setup_menu_panel();
}



function FP_NewSearch() {
	menu_active = "find_places";
	setup_menu_panel();
}




// *****************************************************************************************************
// ******************************** Zoom events (NB not map click zoom) ********************************
// *****************************************************************************************************


// for zoom bar, hover over effect
function zoom_OverImage(imageObj) {

	if ( imageDownID != imageObj.name ) {

		if ( imageObj.name == "Zplus" ) {
			imageObj.src = "images/zoomlevel/Zoom-bar_plus2.gif";
		}
		else if ( imageObj.name == "Zminus" ) {
			imageObj.src = "images/zoomlevel/Zoom-bar_minus2.gif";
		}
	}
	ChangeRollovercursor(imageObj.id);
}


// for zoom bar, to reset after hover over effect
function zoom_OutImage(imageObj) {

	if ( imageDownID != imageObj.name ) {

		if ( imageObj.name == "Zplus" ) {
			imageObj.src = "images/zoomlevel/Zoom-bar_plus.gif";
		}
		else if ( imageObj.name == "Zminus" ) {
			imageObj.src = "images/zoomlevel/Zoom-bar_minus.gif";
		}
	}
}



var zoomselect = "off";
var zoomdrag = "off";


function zoombar_on() {							//NEW 3.2
	zoomselect = "on";
	zoompointer.onmousedown = moveme_onmousedown;			//NEW 3.2
	zoompointer.onmouseup = moveme_onmouseup;
	zoompointer.onmousemove = moveme_onmousemove;
}


function zoombar_off() {						//NEW 3.2
	zoomselect = "off";
	moveme_onmouseup();
}



function moveme_onmousedown(e) {					//NEW 3.2

	if (zoomselect == "on") {
		zoomdrag = "on";
		if (BrowserName == "Netscape" ) {
			mouseX = e.pageX;
		} else {
			mouseX = event.clientX - 2;
			if (BrowserName == "Microsoft Internet Explorer") {		//IE correction
				mouseX += document.body.scrollLeft;
			}
		}
		return false
	}
}

function moveme_onmouseup(e) {						//NEW 3.2

	if (zoomdrag == "on") {	
		zoomdrag = "off"
 		ZoomBarChanged();
 	}
}

function moveme_onmousemove(e) {					//NEW 3.2

	if ((zoomselect == "on") && (zoomdrag == "on")) {

		mouseX = 0;

		if (BrowserName == "Netscape" ) {
			mouseX = e.pageX;
		} else {
			mouseX = event.clientX - 2;
			if (BrowserName == "Microsoft Internet Explorer") {		//IE correction
				mouseX += document.body.scrollLeft;
			}
		}

		obj = document.getElementById("zoombar_div");
		var oX = obj.offsetLeft;
		oX = oX + obj.parentNode.offsetLeft; 			// finds the absolute position of the object

		if ((os.search(/mac/) != -1) && (BrowserName == "Netscape" )) {				// if its a mac
			oX = oX + 10;
		} 

		var theX = mouseX - oX;					// finds the relative position of the object
		var max_right = 145; 
		var min_left = 20;

		if (theX >= max_right) {
			if (BrowserName == "Netscape" ) {	
				zoompointer.style.left=145;
			} else {		
				zoompointer.style.pixelLeft=145;
			}
			zoomdrag = "off"
 			ZoomBarChanged();

 		} else if (theX <= min_left) {
			if (BrowserName == "Netscape" ) {	
				zoompointer.style.left=28;
			} else {		
				zoompointer.style.pixelLeft=28;
			}
			zoomdrag = "off"
 			ZoomBarChanged();

 		} else {	
			if (BrowserName == "Netscape" ) {	
				zoompointer.style.left=theX+3;
			} else {		
				zoompointer.style.pixelLeft=theX+5;
			}
		}
	}

	return false
}


function ZoomBarChanged() {						//NEW 3.2

	var zoompointer_left = 0;
	if (BrowserName == "Netscape" ) {	
		zoompointer_left = zoompointer.style.left;
		zoompointer_left = Number(zoompointer_left.substr(0,(zoompointer_left.length-2)));
	} else {		
		zoompointer_left = zoompointer.style.pixelLeft;
	}

	var zoom_ratio = (145-28) / Math.sqrt(Math.sqrt(max_zoom - min_zoom));
	last_zoom = zoom;
	last_easting = easting;
	last_northing = northing;
	zoom = ((zoompointer_left - 28) / zoom_ratio);
	zoom = min_zoom + (zoom * zoom * zoom * zoom);

	if (zoom >= max_zoom) {
		zoom = max_zoom;
		if ((BrowserName == "Netscape" ) && (zoompointer.style.left > 145)) {	
			zoompointer.style.left=145;
		} else if (zoompointer.style.left > 145){		
			zoompointer.style.pixelLeft = 145;
		}
	}
	if (zoom <= min_zoom) {
		zoom = min_zoom;
		if ((BrowserName == "Netscape" ) && (zoompointer.style.left < 28)) {	
			zoompointer.style.left=28;
		} else if (zoompointer.style.left < 28) {		
			zoompointer.style.pixelLeft = 28;
		}
	}
	zoom = String(Math.round(zoom));

	if (zoom != last_zoom) {
		setmap_labels();
		dis_anim();
		getMap();
	}

	// THIS BIT TO UPDATE LAYERS WITH ZOOM SETTINGS
	if ((menu_active == "current_layers") || (menu_active == "add_layers")) {
		create_add_layers_menu();
		create_current_layers_menu();
		setup_menu_panel();
	}
}


function update_zoom_bar() {						//NEW 3.2

	var zoompointer_left = 0;
	var zoom_ratio = (145-28) / Math.sqrt(Math.sqrt(max_zoom - min_zoom));
	zoompointer_left = (Math.sqrt(Math.sqrt(zoom - min_zoom)) * zoom_ratio);

	if (BrowserName == "Netscape" ) {	
		zoompointer.style.left = zoompointer_left + 28;
	} else {		
		zoompointer.style.pixelLeft = zoompointer_left + 28;
	}

	// THIS BIT TO UPDATE LAYERS WITH ZOOM SETTINGS
	if ((menu_active == "current_layers") || (menu_active == "add_layers")) {
		create_add_layers_menu();
		create_current_layers_menu();
		setup_menu_panel();
	}
}

function zoomin() {							//NEW 3.2

	if (zoom <= min_zoom) {
		// Do nothing
	} else {
		calc_zoom_step('in');
	}
}



function zoomout() {							//NEW 3.2

	if (zoom >= max_zoom) {
		// Do nothing
	} else {
		calc_zoom_step('out');
	} 
}


function calc_zoom_step(zoom_direction) {				//NEW 3.2

	var zoompointer_left = 0;
	if (BrowserName == "Netscape" ) {	
		zoompointer_left = zoompointer.style.left;
		zoompointer_left = Number(zoompointer_left.substr(0,(zoompointer_left.length-2)));
	} else {		
		zoompointer_left = zoompointer.style.pixelLeft;
	}

	var max_right = 145; 
	var min_left = 20;
	var thestep = (145 - 28) / 12;

	if (zoom_direction == "in") {

		if (zoompointer_left > min_left) {
			if (BrowserName == "Netscape" ) {	
				zoompointer.style.left = zoompointer_left - thestep;
			} else {		
				zoompointer.style.pixelLeft = zoompointer_left - thestep;
			}			
			ZoomBarChanged();

		} else {
			zoom = zoom;
		}

	} else {
		if (zoompointer_left < max_right) {
			if (BrowserName == "Netscape" ) {	
				zoompointer.style.left = zoompointer_left + thestep;
			} else {		
				zoompointer.style.pixelLeft = zoompointer_left + thestep;
			}			
			ZoomBarChanged();

		} else {
			zoom = zoom;
		}
	}
}


function zoombar_click(e) {							//NEW 3.2

	mouseX = 0;

	if (BrowserName == "Netscape" ) {
		mouseX = e.pageX;
	} else {
		mouseX = event.clientX - 2;
		if (BrowserName == "Microsoft Internet Explorer") {		//IE correction
			mouseX += document.body.scrollLeft;
		}
	}

	obj = document.getElementById("zoombar_div");
	var oX = obj.offsetLeft;
	oX = oX + obj.parentNode.offsetLeft; 			// finds the absolute position of the object

	var theX = mouseX - oX;					// finds the relative position of the object
	var max_right = 145; 
	var min_left = 20;

	if ((theX <= max_right) && (theX >= min_left)) {
		if (BrowserName == "Netscape" ) {	

			zoompointer.style.left=theX+3;
		} else {		
			zoompointer.style.pixelLeft=theX+5;
		}
		ZoomBarChanged();
	}
	return false
}


function wheel(event) {								//NEW 3.2

        var delta = 0;

        if (!event) {					// For IE. 
                event = window.event;
	}
        if (event.wheelDelta) { 			// IE/Opera.
                delta = event.wheelDelta/120;
        }  else if (event.detail) { 			// Mozilla case. 
		delta = -event.detail/3;		// In Mozilla, sign of delta is different than in IE. Also, delta is multiple of 3 
	}

	// If delta is nonzero, handle it. Basically, delta is now positive if wheel was scrolled up, and negative, if wheel was scrolled down.
       	if (delta) {
                wheel_zoom(delta);
	}
	//Prevent default actions caused by mouse wheel
        if (event.preventDefault) {
                event.preventDefault();
	}

	event.returnValue = false;
}


function wheel_zoom(delta) {							//NEW 3.2

    	if (delta < 0) {		// zoom out
		zoomout();
        } else {			// zoom in
		zoomin();
	}
}




// *****************************************************************************************************
// ******************************************* Pan bar events ******************************************
// *****************************************************************************************************



// for pan bars, hover over effect
function Pan_OverImage(imageObj) {

	if ( imageObj.name == "uppb" ) {
		imageObj.src = "images/panbars/upp2.gif";
	}
	else if ( imageObj.name == "downb" ) {
		imageObj.src = "images/panbars/down2.gif";
	}
	else if ( imageObj.name == "leftb" ) {
		imageObj.src = "images/panbars/left2.gif";
	}
	else if ( imageObj.name == "rightb" ) {
		imageObj.src = "images/panbars/right2.gif";
	}
	else if ( imageObj.name == "nwb" ) {
		nwb1.src = "images/panbars/corner_nw_a2.gif";
	}
	else if ( imageObj.name == "neb" ) {
		neb1.src = "images/panbars/corner_ne_a2.gif";
	}
	else if ( imageObj.name == "seb" ) {
		seb1.src = "images/panbars/corner_se_a2.gif";
	}
	else if ( imageObj.name == "swb" ) {
		swb1.src = "images/panbars/corner_sw_a2.gif";
	}
	ChangeRollovercursor(imageObj.id);
}


// for pan bars, to reset after hover over effect
function Pan_OutImage(imageObj) {

	if ( imageObj.name == "uppb" ) {
		imageObj.src = "images/panbars/upp.gif";
	}
	else if ( imageObj.name == "downb" ) {
		imageObj.src = "images/panbars/down.gif";
	}
	else if ( imageObj.name == "leftb" ) {
		imageObj.src = "images/panbars/left.gif";
	}
	else if ( imageObj.name == "rightb" ) {
		imageObj.src = "images/panbars/right.gif";
	}
	else if ( imageObj.name == "nwb" ) {
		nwb1.src = "images/panbars/corner_nw_a.gif";
	}
	else if ( imageObj.name == "neb" ) {
		neb1.src = "images/panbars/corner_ne_a.gif";
	}
	else if ( imageObj.name == "seb" ) {
		seb1.src = "images/panbars/corner_se_a.gif";
	}
	else if ( imageObj.name == "swb" ) {
		swb1.src = "images/panbars/corner_sw_a.gif";
	}
}



function pan(thedirection) {

	dis_anim();
	last_zoom = zoom;
	last_easting = easting;
	last_northing = northing;

	switch (thedirection)
	{
	case "west":
		easting = String(Math.round(parseInt(easting) - (parseInt(zoom) / 2)));
  		break
	case "east":
		easting = String(Math.round(parseInt(easting) + (parseInt(zoom) / 2)));
  		break
	case "north":
		northing = String(Math.round(parseInt(northing) + (parseInt(zoom) / 3)));
  		break
	case "south":
		northing = String(Math.round(parseInt(northing) - (parseInt(zoom) / 3)));
  		break
	case "nw":
		easting = String(Math.round(parseInt(easting) - (parseInt(zoom) / 2)));
		northing = String(Math.round(parseInt(northing) + (parseInt(zoom) / 3)));
  		break
	case "ne":
		easting = String(Math.round(parseInt(easting) + (parseInt(zoom) / 2)));
		northing = String(Math.round(parseInt(northing) + (parseInt(zoom) / 3)));
  		break
	case "se":
		easting = String(Math.round(parseInt(easting) + (parseInt(zoom) / 2)));
		northing = String(Math.round(parseInt(northing) - (parseInt(zoom) / 3)));
  		break
	case "sw":
		easting = String(Math.round(parseInt(easting) - (parseInt(zoom) / 2)));
		northing = String(Math.round(parseInt(northing) - (parseInt(zoom) / 3)));
  		break
	}

	getMap();
	setmap_labels();
}




// *****************************************************************************************************
// ******************************************* Map Tool events *****************************************
// *****************************************************************************************************



function ChangeRollovercursor(id)
{
  var element = document.getElementById(id);
  if ( element ) {
    element.style.cursor = "pointer";
    element.style.cursor = "hand";
  }
}



// for buttons, hover over effect
function OverImage(imageObj) {

	if ( imageDownID != imageObj.name ) {

		if ( imageObj.name == "zoominb" ) {
			imageObj.src = "images/buttons/zoom_in2.gif";
		}
		else if ( imageObj.name == "zoomoutb" ) {
			imageObj.src = "images/buttons/zoom_out2.gif";
		}
		else if ( imageObj.name == "panb" ) {
			imageObj.src = "images/buttons/pan2.gif";
		}
		else if ( imageObj.name == "infob" ) {
			imageObj.src = "images/buttons/info2.gif";
		}

	}
	ChangeRollovercursor(imageObj.id);
}


// for buttons, to reset after hover over effect
function OutImage(imageObj) {

	if ( imageDownID != imageObj.name ) {

		if ( imageObj.name == "zoominb" ) {
			imageObj.src = "images/buttons/zoom_in.gif";
		}
		else if ( imageObj.name == "zoomoutb" ) {
			imageObj.src = "images/buttons/zoom_out.gif";
		}
		else if ( imageObj.name == "panb" ) {
			imageObj.src = "images/buttons/pan.gif";
		}
		else if ( imageObj.name == "infob" ) {
			imageObj.src = "images/buttons/info.gif";
		}

	}
}




//called when a map tool button is clicked
function SelectTool(what,imageObjname) {

	if ( imageDownID != imageObjname ) {

		if ( imageObjname == "zoominb" ) {
			zoominb.src = "images/buttons/zoom_in2.gif";
			zoomoutb.src = "images/buttons/zoom_out.gif";
			panb.src = "images/buttons/pan.gif";
			infob.src = "images/buttons/info.gif";
			theMap.onmousedown = mouseZoomDown;
			theMap.onmouseup = mouseZoomUp;
			theMap.onmousemove = moveZoomImage;
			toolmode = 1;
		}
		else if ( imageObjname == "zoomoutb" ) {
			zoominb.src = "images/buttons/zoom_in.gif";
			zoomoutb.src = "images/buttons/zoom_out2.gif";
			panb.src = "images/buttons/pan.gif";
			infob.src = "images/buttons/info.gif";
			theMap.onmousedown = mouseZoomDown;
			theMap.onmouseup = mouseZoomUp;
			theMap.onmousemove = moveZoomImage;
			toolmode = 2;
		}
		else if ( imageObjname == "panb" ) {
			zoominb.src = "images/buttons/zoom_in.gif";
			zoomoutb.src = "images/buttons/zoom_out.gif";
			panb.src = "images/buttons/pan2.gif";
			infob.src = "images/buttons/info.gif";
			theMap.onmousedown = mousePanDown;
			theMap.onmouseup = mousePanUp;
			theMap.onmousemove = moveImage;
			theMap.onmouseout = pan_out;
			toolmode = 3;
		}
		else if ( imageObjname == "infob" ) {
			zoominb.src = "images/buttons/zoom_in.gif";
			zoomoutb.src = "images/buttons/zoom_out.gif";
			panb.src = "images/buttons/pan.gif";
			infob.src = "images/buttons/info2.gif";
			theMap.onmousedown = InfoClick;
			theMap.onmouseup = NoMouseEvent;
			theMap.onmousemove = NoMouseEvent;
			toolmode = 4;
		}

		imageDownID = imageObjname;
		change_cursor(what);
	}
}



//called by SelectTool function to set cursor appropriate to map tool
function change_cursor(thevalue) {

	if ((thevalue == "magnify") || (thevalue == "mag_minus") || (thevalue == "cross_rm")) {
		cursor_val = "url(images/" + thevalue + ".cur)";
	} 

  	var element = document.getElementById("theMap");

	if (BrowserName == "Netscape") {				// only IE supports custom url style

		if ((thevalue == "magnify") || (thevalue == "mag_minus") || (thevalue == "cross_rm")) {
			element.style.cursor = "crosshair";
		} else {
    			element.style.cursor = "pointer";
    			element.style.cursor = "hand";
		}
	} else if (thevalue == "hand") {
    		element.style.cursor = "hand";
	} else {
		element.style.cursor = cursor_val;
	}
}






// *****************************************************************************************************
// ***************************************** Map Events ************************************************
// *****************************************************************************************************


var old_src_txt = "";
var new_src_txt = "";

function getMap() {

	update_linkform();
	if (add_chosen == "yes") {
	   theMap.src = serverpath + "getmapimage.asp?theme=" + thetheme + "&layers=" + current_layers + historical_layers + current_background_layers + "&mapwidth=" + mapwidth + "&mapheight=" + mapheight + "&easting=" + easting + "&northing=" + northing + "&zoom=" + zoom + "&SymbolPoints=" + addeasting + "," + addnorthing + "&SymbolPointStyle=red_circle";
	   new_src_txt = serverpath + "getmapimage.asp?theme=" + thetheme + "&layers=" + current_layers + historical_layers + current_background_layers + "&mapwidth=" + mapwidth + "&mapheight=" + mapheight + "&easting=" + easting + "&northing=" + northing + "&zoom=" + zoom + "&SymbolPoints=" + addeasting + "," + addnorthing + "&SymbolPointStyle=red_circle";
	} else {
	   theMap.src = serverpath + "getmapimage.asp?theme=" + thetheme + "&layers=" + current_layers + historical_layers + current_background_layers + "&mapwidth=" + mapwidth + "&mapheight=" + mapheight + "&easting=" + easting + "&northing=" + northing + "&zoom=" + zoom;
	   new_src_txt = serverpath + "getmapimage.asp?theme=" + thetheme + "&layers=" + current_layers + historical_layers + current_background_layers + "&mapwidth=" + mapwidth + "&mapheight=" + mapheight + "&easting=" + easting + "&northing=" + northing + "&zoom=" + zoom;
	} 

	theMap.oncontextmenu = function() {return false;};
	update_map_location();

	if (old_src_txt == new_src_txt) {		// This is a workaround for Opera to switch animation off if map is the same, as Opera will not fire the onLoad event if it is the same!!!
	   anim_off();
	} else {
	   old_src_txt = new_src_txt;
	}
}



function dis_anim() {
	animation_div.innerHTML = "<img border=\"0\" src=\"images/circle_anim2.gif\" alt=\"\" title=\"\">";
}

function anim_off() {
	animation_div.innerHTML = "";
}





var mapfunctions = false;
var mouseover = false;
var rX = 0;
var rY = 0;
var mouseX = 0;
var mouseY = 0;
var xcoor = 0;
var ycoor = 0;

function outOfMap() {
	mapfunctions = false;
}

function onMap() {
	mapfunctions = true;
	//var focus_menu = "menu_" + menu_active;
	//document.getElementById(focus_menu).focus();

	if (BrowserName == "Netscape" ) {
     	   theMap.addEventListener('DOMMouseScroll', wheel, false);         	// DOMMouseScroll is for mozilla.
	} else {
		theMap.onmousewheel = wheel;					// IE/Opera.	
	}				
}



count_refreshes = 0;
// Move the image back to it's original position
function refreshMap() {

	count_refreshes = count_refreshes + 1;
	if (BrowserName == "Netscape" ) {	

		theMap.style.left=0;
		theMap.style.top=0;
	} else {
		theMap.style.pixelLeft = 0;
		theMap.style.pixelTop = 0;
	}
	anim_off();

	if (count_refreshes == 2) {
		words_wisdom_div.innerHTML = "";
		theMap.height = "380";
		theMap.width = "615";
	}
}



function InfoClick(e) {

	// IE doesn't pass the event object
	if (e == null) {
		e = window.event;
	}

    	// for IE, left click == 1; for Firefox, left click == 0
	if ((e.button == 1 && window.event != null) || e.button == 0) {

		mouseX = 0;
		mouseY = 0;
		rX = 0;
		rY = 0;

		if (BrowserName == "Netscape" ) {		
			mouseX = e.pageX;
			mouseY = e.pageY;
		} else {	
			mouseX = e.clientX - 2;
			mouseY = e.clientY - 2;	
			if (BrowserName == "Microsoft Internet Explorer") {		//IE correction
				mouseX += document.body.scrollLeft;
				mouseY += document.body.scrollTop;
			}
		}

		obj = document.getElementById("theMap");
		var oX = obj.offsetLeft;
		var oY = obj.offsetTop;
		oX = oX + obj.parentNode.offsetLeft;		// finds the absolute position of the object
		oY = oY + obj.parentNode.offsetTop;

		rX = mouseX - oX;					// finds the relative position of the object
		rY = mouseY - oY;

		Map_Info();
	}
}


function NoMouseEvent(e) {
	// Do Nothing	
	return false
}




// *****************************************************************************************************
// ************************************ MAP LOCATION TITLE *********************************************
// *****************************************************************************************************


function update_map_location() {							//NEW 3.2

	var all_exclude_layers = GSTLayerList.join(",");
	all_exclude_layers = all_exclude_layers.replace(/Portal_Map_Locs/,"");
	all_exclude_layers = all_exclude_layers.replace(/,,/g,",");

	if (all_exclude_layers != "") {
		var sURL = serverpath + "dialogbox_InfoPointViewer.asp?";
		sURL += "infomode=xml&theme=" + thetheme + "&easting=" + easting + "&northing=" + northing + "&MapWidth=" + zoom + "&Layers=" + all_exclude_layers;
		xmlinfoRequestObj = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")
		xmlinfoRequestObj.onreadystatechange = proccessmaplocXML;
		xmlinfoRequestObj.open("GET", sURL, true);
		xmlinfoRequestObj.send(null);
	}
}



function proccessmaplocXML() {	

					//NEW 3.2
	if (xmlinfoRequestObj.readyState == 4 && (xmlinfoRequestObj.status == 200 || xmlinfoRequestObj.status == 304)) {
		xmlDoc = xmlinfoRequestObj.responseXML;
		createMapLoc_title();
	}
}



function createMapLoc_title() {							//NEW 3.2

	var x = 0;
	var map_loc_subname = "";
	var map_loc_name = "";
	var map_loc_authority = "";
	var map_loc_region = "";
	var map_loc_country = "";

	rootNode = xmlDoc.documentElement;

	if ( rootNode.childNodes.length > 0 ) {

		if ( rootNode.nodeName.toLowerCase() == "planaccesserror" ) {

		} else {
			var LOC_Elements = rootNode.childNodes.item(0)
			var ResultNo = "";

			// Populate results table
			for (x = 0; x < rootNode.childNodes.length; x++) {

				// Get the number of columns in the XML document
				var featureRows = rootNode.childNodes.item(x).childNodes.length;

				for ( ll = 0; ll < featureRows; ll++ ) {

					// Find the Easting and Northing & Distance value for the row - Loop around all the fields in the feature
					var dataRows = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.length;
					for ( l = 0; l < dataRows; l++ ) {

						var LOCnodeName = "";
						var LOCnodeText = "";

						if (BrowserName == "Netscape") {
							if(rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).hasChildNodes()){
								LOCnodeText = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).firstChild.nodeValue;
								LOCnodeName = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).nodeName;
							}
						} else {
							LOCnodeText = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).text;
							LOCnodeName = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).nodeName;
						}

						if ( LOCnodeName == "Sub_name" || LOCnodeName == "SUB_NAME" ) {
							map_loc_subname = LOCnodeText;

						} else if ( LOCnodeName == "Name" || LOCnodeName == "NAME" ) {
							map_loc_name = LOCnodeText;

						} else if ( LOCnodeName == "Authority" || LOCnodeName == "AUTHORITY" ) {
							map_loc_authority = LOCnodeText;

						} else if ( LOCnodeName == "Region" || LOCnodeName == "REGION" ) {
							map_loc_region = LOCnodeText;

						} else if ( LOCnodeName == "Country" || LOCnodeName == "COUNTRY" ) {
							map_loc_country = LOCnodeText;
						} 
					}
				}
			}
		}
	}

	var map_location_text = map_loc_country + " > " + map_loc_region;

	map_location_text += " > " + "<a class=\"map_title_link\" href=\"javascript:map_title_link('authority','" + map_loc_authority + "')\" title=\"Zoom to " + map_loc_authority + "\">" +  map_loc_authority + "</a>";

	if ((zoom <= 25000) && (map_loc_name != "")) {
		map_location_text +=  " > " + "<a class=\"map_title_link\" href=\"javascript:map_title_link('name','" + map_loc_name + "')\" title=\"Zoom to " + map_loc_name + "\">" + map_loc_name + "</a>";
	}

	if (zoom <= 8000 && map_loc_subname != "") {
		map_location_text +=  " > " + map_loc_subname;
	}

	map_location_text = map_location_text.replace(/ >  > /g," > ");
	if (map_loc_country == "" && map_loc_region == "" && map_loc_name == "" && map_loc_subname == "") {
		map_location_text = "";
	}

	map_location_label.innerHTML = map_location_text;
	xmlDoc = null;
}


function map_title_link(the_level,the_value) {		
				//NEW 3.2

	map_location_label.innerHTML = "Changing map to " + the_value + ". Please wait...";
	var sURL = serverpath + "dialogbox_MapInformation.asp?theme=" + thetheme + "&mapwidth=" + mapwidth + "&mapheight=" + mapheight + "&layername=Portal_Map_Locs&mapmode=GetBounds";
	sURL += "&FieldName=" + the_level + "&fieldvalue=" + the_value;

	xmlinfoRequestObj = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")
	xmlinfoRequestObj.onreadystatechange = proccessmaploclinkXML;
	xmlinfoRequestObj.open("GET", sURL, true);
	xmlinfoRequestObj.send(null);
}


function proccessmaploclinkXML() {								//NEW 3.2
	if (xmlinfoRequestObj.readyState == 4 && (xmlinfoRequestObj.status == 200 || xmlinfoRequestObj.status == 304)) {
		xmlDoc = xmlinfoRequestObj.responseXML;
		MapLoc_title_move();
	} 
}


function MapLoc_title_move() {									//NEW 3.2

	var x = 0;
	var map_loc_x = "";
	var map_loc_y = "";
	var map_loc_width = "";

	LayerIndex = 0;
	var LOCnodeName = "";
	var LOCnodeText = "";

	rootNode = xmlDoc.documentElement;

	for ( x = 0; x < rootNode.childNodes.length; x++ ) {

		if (BrowserName == "Netscape") {
			LOCnodeText = rootNode.childNodes.item(x).firstChild.nodeValue;
			LOCnodeName = rootNode.childNodes.item(x).nodeName;
		} else {
			LOCnodeText = rootNode.childNodes.item(x).text;
			LOCnodeName = rootNode.childNodes.item(x).nodeName;
		}


		if ( LOCnodeName == "CenterX" || LOCnodeName == "CENTERX" ) {
			map_loc_x = LOCnodeText;
				 
		} else if ( LOCnodeName == "CenterY" || LOCnodeName == "CENTERY" ) {
			map_loc_y = LOCnodeText;

		} else if ( LOCnodeName == "MapWidth" || LOCnodeName == "MAPWIDTH" ) {
			map_loc_width = LOCnodeText;
		}
	}


	if (map_loc_x != "" && map_loc_y != "" && map_loc_width != "") {
		last_zoom = zoom;
		if (map_loc_width > max_zoom) {
			zoom = max_zoom;
		} else if (map_loc_width < min_zoom) {
			zoom = min_zoom;
		} else {
			zoom = map_loc_width;
		}
		last_easting = easting;
		last_northing = northing;
		easting = map_loc_x;
		northing = map_loc_y;
		dis_anim();
		getMap();
		setmap_labels();
		update_zoom_bar();
	}

	xmlDoc = null;
}



function previous_map() {									//NEW 3.2

	var switch_easting = easting;
	var switch_northing = northing;
	var switch_zoom = zoom;

	easting = last_easting;
	northing = last_northing;
	zoom = last_zoom;
	last_easting = switch_easting;
	last_northing = switch_northing;
	last_zoom = switch_zoom;

	dis_anim();
	getMap();
	setmap_labels();
	update_zoom_bar();
}


// *****************************************************************************************************
// ************************************** Coordinate Events ********************************************
// *****************************************************************************************************


function scalebar_setup() {								//NEW 3.2

	var scale_interval = Number(zoom) / mapwidth * 50;	//ratio x 50 pixels
	var scalebar_txt = "";
	scalebar_txt += "<table border=0 width=\"180\"><tr>";

	if (scale_interval > 500) {				//present in km
		scalebar_txt += "<td class=\"scale_bar\" width=\"46\" align=\"left\">&nbsp;&nbsp;0 km</td>";
		scalebar_txt += "<td class=\"scale_bar\" width=\"47\" align=\"center\">" + Math.round(scale_interval/1000*10)/10 + "</td>";
		scalebar_txt += "<td class=\"scale_bar\" width=\"49\" align=\"center\">" + Math.round(scale_interval * 2/1000*10)/10 + "</td>";
		scalebar_txt += "<td class=\"scale_bar\" width=\"38\" align=\"right\">" + Math.round(scale_interval * 3/1000*10)/10 + "km</td>";

	} else {						//present in m
		scalebar_txt += "<td class=\"scale_bar\" width=\"43\" align=\"left\">0 m</td>";
		scalebar_txt += "<td class=\"scale_bar\" width=\"40\" align=\"center\">" + Math.round(scale_interval) + "</td>";
		scalebar_txt += "<td class=\"scale_bar\" width=\"42\" align=\"center\">" + Math.round(scale_interval * 2) + "</td>";
		scalebar_txt += "<td class=\"scale_bar\" width=\"35\" align=\"right\">" + Math.round(scale_interval * 3) + "m</td>";
	}

	scalebar_txt += "</tr><tr><td height=\"3\" colspan=\"4\"></td></tr>";

	if (Number(zoom) > 1000) {				//present in km
		scalebar_txt += "<tr><td align=\"center\" colspan=\"4\" class=\"bottom_panel_text\">Map width: " + String(Math.round(Number(zoom)/1000)) + "km</td></tr>";

	} else {						//present in m
		scalebar_txt += "<tr><td align=\"center\" colspan=\"4\" class=\"bottom_panel_text\">Map width: " + String(Math.round(Number(zoom))) + "m</td></tr>";
	}

	scalebar_txt += "</table>";
	scalebar_div.innerHTML = scalebar_txt;
}



function setmap_labels() {

	scalebar_setup();

	//set grid_ref label on map
	//var lets = easting.substring(0,1) + northing.substring(0,1);
	var lets = "";

	var eastinglength = easting.length
	var northinglength = northing.length
	
	switch (eastinglength)
	{
	case 6:
  		lets = easting.substring(0,1);
  		break
	case 5:
  		lets = "0";
  		break
	default:
  		alert("easting too long; length: " + eastinglength + "; easting: " + easting);
	}

	switch (northinglength)
	{
	case 6:
  		lets = lets + northing.substring(0,1);
  		break
	case 7:
  		lets = lets + northing.substring(0,2);
  		break
	case 5:
  		lets = lets + "0";
  		break
	default:
  		alert("northing too long; length: " + northinglength + "; northing: " + northing);
	}

	var gridref_lets = getcoord_lets(lets);
	gridref = gridref_lets;

	if (gridref != "") {
		gridref += easting.substring(1,4) + northing.substring(1,4);
	} else {
		gridref = "out of area";
	}

	ConfigureForGRS80();
	convertEastNorth(easting, northing);
	var lat_long_str = ", Lat: " + g_Latitude.toFixed(3) + "° , Long: " + g_Longitude.toFixed(3) + "°";
	var thechange = "<a  href=\"javascript:change_coords_dialog(\'grid_ref\',\'"+ gridref_lets + "\')\" title=\"Change Map Coordinates dialog box\">Change Map Coordinates</a>";
	info_bar.innerHTML = "Map Centered at OS Grid ref: " + gridref + ", Easting: " + easting + ", Northing: " + northing + lat_long_str + ". <br>" + thechange;
}


function change_coords_dialog(thecoordsys,extrainfo,error_message) {

	var coord_change2 = "";

	if (thecoordsys == "grid_ref") {

		coord_change2 = "<table border=0 width=\"580\"><tr><td width=8></td><td class=\"panel_text\">Type of coordinates:<br>";
		coord_change2 += "<input name=\"coordsys\" id=\"grid_ref\" type=\"radio\" value=\"grid_ref\" onClick=\"change_coords_dialog(\'grid_ref\',\'" + extrainfo + "\')\" checked style=\"cursor:pointer\"> Ordnance Survey grid-ref<br>";
      	 	coord_change2 += "<input name=\"coordsys\" id=\"E_N\" type=\"radio\" value=\"E_N\" onClick=\"change_coords_dialog(\'E_N\',\'" + extrainfo + "\')\" style=\"cursor:pointer\"> OS Easting & Northings<br>";
       	 	coord_change2 += "<input name=\"coordsys\" id=\"Lat_Long\" type=\"radio\" value=\"Lat_Long\" onClick=\"change_coords_dialog(\'Lat_Long\',\'" + extrainfo + "\')\" style=\"cursor:pointer\"> Latitude & Longitude</td>";

		coord_change2 += "<td class=\"panel_text\">Please enter the grid reference:<br>&nbsp;";
		coord_change2 += "<select class=\"combobox\" name=\"grid_ref_drop\" id=\"grid_ref_drop\" size=\"1\" style=\"font-size: 8pt\">";

		if (extrainfo == "") {
			coord_change2 += "<option value=\"blank\" id=\"blank\" selected=\"selected\"> </option>";
		}

		for ( y = 0; y < OS_lets_array.length; y++ ) {
			if (OS_lets_array[y] == extrainfo) {
				coord_change2 += "<option value=\"" + OS_lets_array[y] + "\" id=\"" + OS_lets_array[y] + "\" selected=\"selected\">" + OS_lets_array[y] + "</option>";
			} else {
				coord_change2 += "<option value=\"" + OS_lets_array[y] + "\" id=\"" + OS_lets_array[y] + "\">" + OS_lets_array[y] + "</option>";
			}
		}
		coord_change2 += "</select>";

		coord_change2 += "&nbsp;&nbsp;<input type=\"text\" name=\"firstval\" id=\"firstval\" value=\"" + easting.substring(1,4) + "\" size=\"3\" class=\"panel_box\" style=\"font-size: 10pt\">";
		coord_change2 += "&nbsp;&nbsp;<input type=\"text\" name=\"secval\" id=\"secval\" value=\"" + northing.substring(1,4) + "\" size=\"3\" class=\"panel_box\" style=\"font-size: 10pt\"><br>";
		coord_change2 += " e.g.  SK&nbsp;&nbsp;350&nbsp;&nbsp;670<br><br>";
		coord_change2 += "Map width: <input type=\"text\" name=\"zoomval\" id=\"zoomval\" value=\"" + String(Math.round(Number(zoom))) + "\" size=\"6\" class=\"panel_box\" style=\"font-size: 10pt\"> metres";
		if (error_message) {
			coord_change2 += "<br><i>" + error_message + "</i></td>";
		} else {
			coord_change2 += "</td>";
		}
		coord_change2 += "<td><input type=\"button\" name=\"grchangeb\" id=\"grchangeb\" onclick=\"change_coords(\'grid_ref\',\'" + extrainfo + "\')\" align=\"right\" value=\"Update\" style=\"cursor:pointer\">&nbsp;&nbsp;";
		coord_change2 += "<input type=\"button\" name=\"cocancelb\" id=\"cocancelb\" onclick=\"change_coords(\'cancel\',\'" + extrainfo + "\')\" align=\"right\" value=\"Cancel\" style=\"cursor:pointer\"></td>";
      		coord_change2 += "</tr></table>";

	} else if (thecoordsys == "E_N") {
		coord_change2 = "<table border=0 width=\"580\"><tr><td width=8></td><td class=\"panel_text\">Type of coordinates:<br>";
		coord_change2 += "<input name=\"coordsys\" id=\"grid_ref\" type=\"radio\" value=\"grid_ref\" onClick=\"change_coords_dialog(\'grid_ref\',\'" + extrainfo + "\')\" style=\"cursor:pointer\"> Ordnance Survey grid-ref<br>";
       	 	coord_change2 += "<input name=\"coordsys\" id=\"E_N\" type=\"radio\" value=\"E_N\" checked onClick=\"change_coords_dialog(\'E_N\',\'" + extrainfo + "\')\" style=\"cursor:pointer\"> OS Easting & Northings<br>";
       	 	coord_change2 += "<input name=\"coordsys\" id=\"Lat_Long\" type=\"radio\" value=\"Lat_Long\" onClick=\"change_coords_dialog(\'Lat_Long\',\'" + extrainfo + "\')\" style=\"cursor:pointer\"> Latitude & Longitude</td>";
		coord_change2 += "<td class=\"panel_text\">Please enter the coordinates:<br>";
		coord_change2 += "<input type=\"text\" name=\"en_firstval\" id=\"en_firstval\" value=\"" + easting + "\" size=\"6\" class=\"panel_box\" style=\"font-size: 10pt\"> Easting<br>";
		coord_change2 += "<input type=\"text\" name=\"en_secval\" id=\"en_secval\" value=\"" + northing + "\" size=\"6\" class=\"panel_box\" style=\"font-size: 10pt\"> Northing<br><br>";
		coord_change2 += "Map width: <input type=\"text\" name=\"zoomval\" id=\"zoomval\" value=\"" + String(Math.round(Number(zoom))) + "\" size=\"6\" class=\"panel_box\" style=\"font-size: 10pt\"> metres";
		if (error_message) {
			coord_change2 += "<br><i>" + error_message + "</i></td>";
		} else {
			coord_change2 += "</td>";
		}
		coord_change2 += "<td><input type=\"button\" name=\"enchangeb\" id=\"enchangeb\" onclick=\"change_coords(\'E_N\',\'" + extrainfo + "\')\" align=\"right\" value=\"Update\" style=\"cursor:pointer\">&nbsp;&nbsp;";
		coord_change2 += "<input type=\"button\" name=\"cocancelb\" id=\"cocancelb\" onclick=\"change_coords(\'cancel\',\'" + extrainfo + "\')\" align=\"right\" value=\"Cancel\" style=\"cursor:pointer\"></td>";
      		coord_change2 += "</tr></table>";
	} else if (thecoordsys == "Lat_Long") {
		coord_change2 = "<table border=0 width=\"580\"><tr><td width=8></td><td class=\"panel_text\">Type of coordinates:<br>";
		coord_change2 += "<input name=\"coordsys\" id=\"grid_ref\" type=\"radio\" value=\"grid_ref\" onClick=\"change_coords_dialog(\'grid_ref\',\'" + extrainfo + "\')\" style=\"cursor:pointer\"> Ordnance Survey grid-ref<br>";
       	 	coord_change2 += "<input name=\"coordsys\" id=\"E_N\" type=\"radio\" value=\"E_N\" onClick=\"change_coords_dialog(\'E_N\',\'" + extrainfo + "\')\" style=\"cursor:pointer\"> OS Easting & Northings<br>";
       	 	coord_change2 += "<input name=\"coordsys\" id=\"Lat_Long\" type=\"radio\" value=\"Lat_Long\" checked onClick=\"change_coords_dialog(\'Lat_Long\',\'" + extrainfo + "\')\" style=\"cursor:pointer\"> Latitude & Longitude</td>";
		coord_change2 += "<td class=\"panel_text\">Please enter the coordinates<br>(in decimal degrees):<br>";
		coord_change2 += "<input type=\"text\" name=\"ll_firstval\" id=\"ll_firstval\" value=\"" + g_Latitude.toFixed(3) + "\" size=\"6\" class=\"panel_box\" style=\"font-size: 10pt\"> Latitude<br>";
		coord_change2 += "<input type=\"text\" name=\"ll_secval\" id=\"ll_secval\" value=\"" + g_Longitude.toFixed(3) + "\" size=\"6\" class=\"panel_box\" style=\"font-size: 10pt\"> Longitude<br><br>";
		coord_change2 += "Map width: <input type=\"text\" name=\"zoomval\" id=\"zoomval\" value=\"" + String(Math.round(Number(zoom))) + "\" size=\"6\" class=\"panel_box\" style=\"font-size: 10pt\"> metres";
		if (error_message) {
			coord_change2 += "<br><i>" + error_message + "</i></td>";
		} else {
			coord_change2 += "</td>";
		}
		coord_change2 += "<td><input type=\"button\" name=\"llchangeb\" id=\"llchangeb\" onclick=\"change_coords(\'Lat_Long\',\'" + extrainfo + "\')\" align=\"right\" value=\"Update\" style=\"cursor:pointer\">&nbsp;&nbsp;";
		coord_change2 += "<input type=\"button\" name=\"cocancelb\" id=\"cocancelb\" onclick=\"change_coords(\'cancel\',\'" + extrainfo + "\')\" align=\"right\" value=\"Cancel\" style=\"cursor:pointer\"></td>";
      		coord_change2 += "</tr></table>";
	}
											
	coord_change_div.innerHTML = coord_change2;
	coord_change_div.style.visibility = "visible";
	document.getElementById(thecoordsys).focus();
}



function change_coords(thecoordsys,extrainfo) {


//onclick run changes, get map etc


	var thezoomval = 0;

	if (thecoordsys == "grid_ref") {

	   var thelets = document.getElementById("grid_ref_drop").value;

	   if ( ( (Number(document.getElementById("firstval").value)) || (document.getElementById("firstval").value == "000") ) && 
		( (Number(document.getElementById("secval").value)) || (document.getElementById("secval").value == "000") ) ) {

		var gr_easting = Number(document.getElementById("firstval").value) * 100;
		var gr_northing = Number(document.getElementById("secval").value) * 100;

		return_value = lets_tocoord_add(thelets);
		gr_easting = gr_easting + return_value[0];
		gr_northing = gr_northing + return_value[1];

		last_zoom = zoom;
		last_easting = easting;
		last_northing = northing;
		easting = String(gr_easting);
		northing = String(gr_northing);
		coord_change_div.style.visibility = "hidden";
		thezoomval = Number(document.getElementById("zoomval").value);
	  	if (thezoomval > 0) {
			zoom = String(thezoomval);
			update_zoom_bar();
	   	}
		dis_anim();
		getMap();
		setmap_labels();

	   } else {

		change_coords_dialog('grid_ref',extrainfo,'Numerical entry only, please');
	   }

	} else if (thecoordsys == "E_N") {

		if ((document.getElementById("en_firstval").value != "") && (document.getElementById("en_secval").value != "")) {
			var en_easting = Number(document.getElementById("en_firstval").value);
			var en_northing = Number(document.getElementById("en_secval").value);

			if ( (en_easting || en_easting == 0) && (en_northing || en_northing == 0) ) {
				last_zoom = zoom;
				last_easting = easting;
				last_northing = northing;
				easting = String(en_easting);
				northing = String(en_northing);
				if (easting == "0") {
					last_easting = easting;
					easting = "1";
				}
				if (northing == "0") {
					last_northing = northing;
					northing = "1";
				}
				coord_change_div.style.visibility = "hidden";
				thezoomval = Number(document.getElementById("zoomval").value);
	  			if (thezoomval > 0) {
					zoom = String(thezoomval);
					update_zoom_bar();
	   			}
				dis_anim();
				getMap();
				setmap_labels();
			} else {

				change_coords_dialog('E_N',extrainfo,'Numerical entry only, please');
			}

		} else {

			change_coords_dialog('E_N',extrainfo,'Please enter values in both fields');
		}


	} else if (thecoordsys == "Lat_Long") {

		if ((document.getElementById("ll_firstval").value != "") && (document.getElementById("ll_secval").value != "")) {
			var latitude = Number(document.getElementById("ll_firstval").value);
			var longitude = Number(document.getElementById("ll_secval").value);

			if ( (latitude || latitude == 0) && (longitude || longitude == 0) ) {
				ConfigureForGRS80();
				convertLatLong(latitude, longitude);
				last_zoom = zoom;
				last_easting = easting;
				last_northing = northing;
				easting = Math.round(g_Easting);
				northing = Math.round(g_Northing); 
				easting = String(easting);
				northing = String(northing);
				coord_change_div.style.visibility = "hidden";
				thezoomval = Number(document.getElementById("zoomval").value);
	  			if (thezoomval > 0) {
					zoom = String(thezoomval);
					update_zoom_bar();
	   			}
				dis_anim();
				getMap();
				setmap_labels();
			} else {
				change_coords_dialog('Lat_Long',extrainfo,'Numerical entry only, please');
			}

		} else {
			change_coords_dialog('Lat_Long',extrainfo,'Please enter values in both fields');
		}


	} else if (thecoordsys == "cancel") {
		coord_change_div.innerHTML = "";
		coord_change_div.style.visibility = "hidden";
	}

}



var g_Latitude = 0; 
var g_Longitude = 0;
var g_Easting = 0; 
var g_Northing = 0;

// coord transform variables
var a = 0.0; 
var b = 0.0; 
var Fo = 0.0; 
var e0 = 0.0; 
var n0 = 0.0; 
var af0 = 0.0; 
var bf0 = 0.0; 
var LAM0 = 0.0; 
var PHI0 = 0.0; 
var Pi = 0.0;
var e2 = 0.0; 
var n = 0.0; 


//1) configure the co-ordinate system - details from OS transform spreadsheet
function ConfigureForGRS80() { 
        a = 6378137.000; 		// semi-major axis
        b = 6356752.3141; 		// semi-minor axis
        Fo = 0.999601271700; 		// central meridian scale
        e0 = 400000.000; 		// true origin easting
        n0 = -100000.000; 		// true origin northing
        PHI0 = degreesToRadians(degsMinsSecsToDecimal(false, 49, 0, 0)); // latitude of false origin
        LAM0 = degreesToRadians(degsMinsSecsToDecimal(true, 2, 0, 0)); 	 // longitude of false origin
} 


//2) do the conversion, passing in the latitude and longitude in the easting and northing in metres 

function convertEastNorth(east_m, north_m) {


	//compute af0, bf0, e squared (e2), and n
        af0 = a * Fo; 
        bf0 = b * Fo; 
        e2 = (Math.pow(af0, 2) - Math.pow(bf0, 2)) / Math.pow(af0, 2); 
        n = (af0 - bf0)/(af0 + bf0); 

	// compute initial value for latitude (PHI in radians)
        var PHId = InitialLat(north_m, n0, af0, PHI0, n, bf0); 

	// compute nu rho and eta2 using value for PHId
        var nu = af0 / Math.sqrt(1 - (e2 * Math.pow(Math.sin(PHId), 2) )); 
        var rho = (nu * (1 - e2)) / (1 - (e2 * Math.pow(Math.sin(PHId), 2) )); 
        var eta2 = (nu / rho) - 1; 

        var Et = east_m - e0; 

	//compute latitude
        var VII = Math.tan(PHId) / (2 * nu * rho); 
        var VIII = (Math.tan(PHId) / (24 * rho * Math.pow(nu, 3))) * (5 + (3 * Math.pow(Math.tan(PHId), 2)) + eta2 - (9 * Math.pow(Math.tan(PHId), 2) * eta2)); 
        var IX = (Math.tan(PHId) / (720 * rho * Math.pow(nu, 5))) * (61 + (90 * Math.pow(Math.tan(PHId), 2)) + (45 * Math.pow(Math.tan(PHId), 4))); 
        g_Latitude = radiansToDegrees(PHId-(Math.pow(Et, 2) * VII) + (Math.pow(Et, 4) * VIII) - (Math.pow(Et, 6)*IX)) + 0.000585; 

	//compute longitude
        var X = Math.pow(Math.cos(PHId), -1) / nu; 
        var XI = (Math.pow(Math.cos(PHId), -1) / (6 * Math.pow(nu, 3))) * ((nu / rho) + (2 * Math.pow(Math.tan(PHId), 2))); 
        var XII = Math.pow(Math.cos(PHId), -1) / (120 * Math.pow(nu, 5)) * (5 + (28 * Math.pow(Math.tan(PHId), 2)) + (24 * Math.pow(Math.tan(PHId), 4))); 
        var XIIA = (Math.pow(Math.cos(PHId), -1) / (5040 * Math.pow(nu, 7))) * (61 + (662 * Math.pow(Math.tan(PHId), 2)) + (1320 * Math.pow(Math.tan(PHId), 4)) + (720 * Math.pow(Math.tan(PHId), 6))); 
        g_Longitude = radiansToDegrees(LAM0 + (Et * X) - (Math.pow (Et, 3) * XI) + (Math.pow(Et, 5) * XII)-(Math.pow(Et, 7) * XIIA)) - 0.001449; 
} 


function InitialLat(North, n0, af0, PHI0, n, bf0) { 

    var PHI1 = ((North - n0) / af0) + PHI0; 
    var M = Marc(bf0, n, PHI0, PHI1); 
    var PHI2 = ((North - n0 - M) / af0) + PHI1; 	// new PHI value
    while (Math.abs(North - n0 - M) > 0.00001) { 
        PHI2 = ((North - n0 - M) / af0) + PHI1; 
        M = Marc(bf0, n, PHI0, PHI2); 
        PHI1 = PHI2; 
    } 
    return PHI2; 
} 

function Marc(bf0, n, P1, P2) { 		// compute meridional arc

    return bf0 * (((1 + n + ((5 / 4) * Math.pow(n, 2)) + ((5 / 4) * Math.pow(n, 3))) * (P2 - P1)) - (((3 * n) + (3 * Math.pow(n, 2)) + ((21 / 8) * Math.pow(n, 3))) * (Math.sin (P2 - P1)) * (Math.cos (P2 + P1))) + ((((15 / 8) * Math.pow(n, 2)) + ((15 / 8) * Math.pow(n, 3))) * (Math.sin (2 * (P2 - P1))) * (Math.cos (2 * (P2 + P1)))) - (((35 / 24) * Math.pow(n, 3)) * (Math.sin (3 * (P2 - P1))) * (Math.cos (3 * (P2 + P1))))); 
} 

function radiansToDegrees(rads) { 
       return rads * (180 / Math.PI); 
} 

function degreesToRadians(degs) { 
        return degs * (Math.PI / 180); 
} 

function degsMinsSecsToDecimal(isWest, degs, mins, secs) { 
       var result; 
       result = (((secs / 60) + mins)/60) + degs; 
       if (isWest) 
               return -(result); 
       else 
               return result; 
} 


function convertLatLong(lat_deg, long_deg) { 

        var PHI = degreesToRadians(lat_deg); 

        var nu = af0 / (Math.sqrt(1 - (e2 * Math.pow(Math.sin(PHI), 2)))); 
        var rho = (nu * (1 - e2)) / (1 - (e2 * Math.pow(Math.sin(PHI), 2))); 
        var eta2 = (nu / rho) - 1; 
        var LAM = degreesToRadians(long_deg); 
        var P = LAM - LAM0; 
        var M = Marc(bf0, n, PHI0, PHI); 
        var I = M + n0; 

        var II = (nu/2) * (Math.sin(PHI)) * (Math.cos(PHI)); 
        var III = ((nu/24) * (Math.sin(PHI)) * Math.pow(Math.cos(PHI), 3)) * (5 - Math.pow (Math.tan(PHI), 2) + (9 * eta2)); 
        var IIIA = ((nu/720) * Math.sin(PHI) * Math.pow(Math.cos(PHI), 5)) * (61- (58 * Math.pow(Math.tan(PHI), 2)) + Math.pow(Math.tan(PHI), 4)); 
        var IV = nu * Math.cos(PHI); 
        var V = (nu/6) * Math.pow(Math.cos(PHI), 3) * ((nu/rho) - Math.pow(Math.tan(PHI), 2)); 
        var VI = (nu/120) * Math.pow(Math.cos(PHI), 5) * (5- (18 * Math.pow(Math.tan(PHI), 2)) + Math.pow(Math.tan(PHI), 4) + (14 * eta2) - (58 * Math.pow(Math.tan(PHI), 2) * eta2)); 
        g_Easting = e0 + (P * IV) + (Math.pow(P, 3) * V) + (Math.pow (P, 5) * VI) + 97; 
        g_Northing = I + (Math.pow(P, 2) * II) + (Math.pow(P, 4) * III) + (Math.pow(P, 6) * IIIA) - 69; 
} 




function getcoord_lets(lets) {							//NEW 3.2

	var gridref_lets = "";
	
	switch (lets)
	{
	case "00":
  		gridref_lets = "SV";
  		break
	case "10":
  		gridref_lets = "SW";
  		break
	case "20":
  		gridref_lets = "SX";
  		break
	case "30":
  		gridref_lets = "SY";
  		break
	case "40":
  		gridref_lets = "SZ";
  		break
	case "50":
  		gridref_lets = "TV";
  		break
	case "11":
  		gridref_lets = "SR";
  		break
	case "21":
  		gridref_lets = "SS";
  		break
	case "31":
  		gridref_lets = "ST";
  		break
	case "41":
  		gridref_lets = "SU";
  		break
	case "51":
  		gridref_lets = "TQ";
  		break
	case "61":
  		gridref_lets = "TR";
  		break
	case "12":
  		gridref_lets = "SM";
  		break
	case "22":
  		gridref_lets = "SN";
  		break
	case "32":
  		gridref_lets = "SO";
  		break
	case "42":
  		gridref_lets = "SP";
  		break
	case "52":
  		gridref_lets = "TL";
  		break
	case "62":
  		gridref_lets = "TM";
  		break
	case "23":
  		gridref_lets = "SH";
  		break
	case "33":
  		gridref_lets = "SJ";
  		break
	case "43":
  		gridref_lets = "SK";
  		break
	case "53":
  		gridref_lets = "TF";
  		break
	case "63":
  		gridref_lets = "TG";
  		break
	case "24":
  		gridref_lets = "SC";
  		break
	case "34":
  		gridref_lets = "SD";
  		break
	case "44":
  		gridref_lets = "SE";
  		break
	case "54":
  		gridref_lets = "TA";
  		break
	case "15":
  		gridref_lets = "NW";
  		break
	case "25":
  		gridref_lets = "NX";
  		break
	case "35":
  		gridref_lets = "NY";
  		break
	case "45":
  		gridref_lets = "NZ";
  		break
	case "16":
  		gridref_lets = "NR";
  		break
	case "26":
  		gridref_lets = "NS";
  		break
	case "36":
  		gridref_lets = "NT";
  		break
	case "46":
  		gridref_lets = "NU";
  		break
	case "07":
  		gridref_lets = "NL";
  		break
	case "17":
  		gridref_lets = "NM";
  		break
	case "27":
  		gridref_lets = "NN";
  		break
	case "37":
  		gridref_lets = "NO";
  		break
	case "08":
  		gridref_lets = "NF";
  		break
	case "18":
  		gridref_lets = "NG";
  		break
	case "28":
  		gridref_lets = "NH";
  		break
	case "38":
  		gridref_lets = "NJ";
  		break
	case "48":
  		gridref_lets = "NK";
  		break
	case "09":
  		gridref_lets = "NA";
  		break
	case "19":
  		gridref_lets = "NB";
  		break
	case "29":
  		gridref_lets = "NC";
  		break
	case "39":
  		gridref_lets = "ND";
  		break
	case "110":
  		gridref_lets = "HW";
  		break
	case "210":
  		gridref_lets = "HX";
  		break
	case "310":
  		gridref_lets = "HY";
  		break
	case "410":
  		gridref_lets = "HZ";
  		break
	case "311":
  		gridref_lets = "HT";
  		break
	case "411":
  		gridref_lets = "HU";
  		break
	case "412":
  		gridref_lets = "HP";
  		break
	default:
  		gridref_lets = "";
	}
	return gridref_lets;
}




function lets_tocoord_add(lets) {							//NEW 3.2

	var return_easting = 0;
	var return_northing = 0;
	
	switch (lets)
	{
	case "SV":
		return_easting = 0;
		return_northing = 0;
  		break
	case "SW":
		return_easting = 100000;
		return_northing = 0;
  		break
	case "SX":
		return_easting = 200000;
		return_northing = 0;
  		break
	case "SY":
		return_easting = 300000;
		return_northing = 0;
  		break
	case "SZ":
		return_easting = 400000;
		return_northing = 0;
  		break
	case "TV":
		return_easting = 500000;
		return_northing = 0;
  		break
	case "SR":
		return_easting = 100000;
		return_northing = 100000;
  		break
	case "SS":
		return_easting = 200000;
		return_northing = 100000;
  		break
	case "ST":
		return_easting = 300000;
		return_northing = 100000;
  		break
	case "SU":
		return_easting = 400000;
		return_northing = 100000;
  		break
	case "TQ":
		return_easting = 500000;
		return_northing = 100000;
  		break
	case "TR":
		return_easting = 600000;
		return_northing = 100000;
  		break
	case "SM":
		return_easting = 100000;
		return_northing = 200000;
  		break
	case "SN":
		return_easting = 200000;
		return_northing = 200000;
  		break
	case "SO":
		return_easting = 300000;
		return_northing = 200000;
  		break
	case "SP":
		return_easting = 400000;
		return_northing = 200000;
  		break
	case "TL":
		return_easting = 500000;
		return_northing = 200000;
  		break
	case "TM":
		return_easting = 600000;
		return_northing = 200000;
  		break
	case "SH":
		return_easting = 200000;
		return_northing = 300000;
  		break
	case "SJ":
		return_easting = 300000;
		return_northing = 300000;
  		break
	case "SK":
		return_easting = 400000;
		return_northing = 300000;
  		break
	case "TF":
		return_easting = 500000;
		return_northing = 300000;
  		break
	case "TG":
		return_easting = 600000;
		return_northing = 300000;
  		break
	case "SC":
		return_easting = 200000;
		return_northing = 400000;
  		break
	case "SD":
		return_easting = 300000;
		return_northing = 400000;
  		break
	case "SE":
		return_easting = 400000;
		return_northing = 400000;
  		break
	case "TA":
		return_easting = 500000;
		return_northing = 400000;
  		break
	case "NW":
		return_easting = 100000;
		return_northing = 500000;
  		break
	case "NX":
		return_easting = 200000;
		return_northing = 500000;
  		break
	case "NY":
		return_easting = 300000;
		return_northing = 500000;
  		break
	case "NZ":
		return_easting = 400000;
		return_northing = 500000;
  		break
	case "NR":
		return_easting = 100000;
		return_northing = 600000;
  		break
	case "NS":
		return_easting = 200000;
		return_northing = 600000;
  		break
	case "NT":
		return_easting = 300000;
		return_northing = 600000;
  		break
	case "NU":
		return_easting = 400000;
		return_northing = 600000;
  		break
	case "NL":
		return_easting = 0;
		return_northing = 700000;
  		break
	case "NM":
		return_easting = 100000;
		return_northing = 700000;
  		break
	case "NN":
		return_easting = 200000;
		return_northing = 700000;
  		break
	case "NO":
		return_easting = 300000;
		return_northing = 700000;
  		break
	case "NF":
		return_easting = 0;
		return_northing = 800000;
  		break
	case "NG":
		return_easting = 100000;
		return_northing = 800000;
  		break
	case "NH":
		return_easting = 200000;
		return_northing = 800000;
  		break
	case "NJ":
		return_easting = 300000;
		return_northing = 800000;
  		break
	case "NK":
		return_easting = 400000;
		return_northing = 800000;
  		break
	case "NA":
		return_easting = 0;
		return_northing = 900000;
  		break
	case "NB":
		return_easting = 100000;
		return_northing = 900000;
  		break
	case "NC":
		return_easting = 200000;
		return_northing = 900000;
  		break
	case "ND":
		return_easting = 300000;
		return_northing = 900000;
  		break
	case "HW":
		return_easting = 100000;
		return_northing = 1000000;
  		break
	case "HX":
		return_easting = 200000;
		return_northing = 1000000;
  		break
	case "HY":
		return_easting = 300000;
		return_northing = 1000000;
  		break
	case "HZ":
		return_easting = 400000;
		return_northing = 1000000;
  		break
	case "HT":
		return_easting = 300000;
		return_northing = 1100000;
  		break
	case "HU":
		return_easting = 400000;
		return_northing = 1100000;
  		break
	case "HP":
		return_easting = 400000;
		return_northing = 1200000;
  		break
	default:
		return_easting = 0;
		return_northing = 0;
	}
	return [return_easting,return_northing];
}




// *****************************************************************************************************
// *************************************** Pan Tool Events *********************************************
// *****************************************************************************************************


var xshift = 0;
var yshift = 0;
var pandrag = "off";


//called when user clicks down on map to pan
function mousePanDown(e) {

	if ( mapfunctions == true && toolmode == 3) {

		if (BrowserName == "Netscape" ) {
			mouseX = e.pageX;
			mouseY = e.pageY;
		} else {
			mouseX = event.clientX - 2;
			mouseY = event.clientY - 2;
			if (BrowserName == "Microsoft Internet Explorer") {		//IE correction
				mouseX += document.body.scrollLeft;
				mouseY += document.body.scrollTop;
			}
		}
		mouseover = true;

		if (BrowserName == "Netscape" ) {
			pleft=0;
			ptop=0;
		} else {
			pleft = theMap.style.pixelLeft;
			ptop = theMap.style.pixelTop;
		}
		xshift = 0;
		yshift = 0;
		xcoor = mouseX;
		ycoor = mouseY;
	}	
	return false
}



//called when user lets go of mouse after clicking down on map to pan or zoom
function mousePanUp(e) {	

	pandrag = "off"
	mouseover = false;
 	PanCoordinates();

}



function moveImage(e) {
		
	if ( mapfunctions == true && toolmode == 3 ) {
		
		if ( mouseover == true ) {
			
			pandrag = "on"

			if (BrowserName == "Netscape" ) {
				mouseX = e.pageX;
				mouseY = e.pageY;
			} else {
				mouseX = event.clientX - 2;
				mouseY = event.clientY - 2;
				if (BrowserName == "Microsoft Internet Explorer") {		//IE correction
					mouseX += document.body.scrollLeft;
					mouseY += document.body.scrollTop;
				}
			}
				
			if (BrowserName == "Netscape" ) {	
				theMap.style.left=pleft+mouseX-xcoor;
				theMap.style.top=ptop+mouseY-ycoor;
			} else {		
				theMap.style.pixelLeft=pleft+mouseX-xcoor;
				theMap.style.pixelTop=ptop+mouseY-ycoor;
			}
			xshift = pleft+mouseX-xcoor;	
			yshift = ptop+mouseY-ycoor;					
			return false
		}
	}
}



function pan_out(e) {

	if ( mouseover == true && toolmode == 3  && pandrag == "on") {

		pandrag = "off"
		mouseover = false;
 		PanCoordinates();
	} 
}



function PanCoordinates() {

	dis_anim();
	// Calculate the current bounds
	var mapX1 = parseInt(easting) - ( parseInt(zoom) / 2 );
	var mapX2 = parseInt(easting) + ( parseInt(zoom) / 2 );
	var mapY1 = parseInt(northing) - ( (parseInt(zoom) / mapwidth * mapheight) / 2 );
	var mapY2 = parseInt(northing) + ( (parseInt(zoom) / mapwidth * mapheight) / 2 );

	var oldCentreX = (mapX2-mapX1)/2+mapX1;
	var oldCentreY = (mapY1-mapY2)/2+mapY2;

	// Calculate the new X and Y
	last_zoom = zoom;
	last_easting = easting;
	last_northing = northing;
	easting = String(Math.round(oldCentreX - (xshift * parseInt(zoom) / mapwidth)));
	northing = String(Math.round(oldCentreY + (yshift * parseInt(zoom) / mapwidth)));

	getMap();
	setmap_labels();
}




// *****************************************************************************************************
// *************************************** Zoom Tool Events ********************************************
// *****************************************************************************************************


var xcoor2 = 0;
var ycoor2 = 0;
var rectx1 = 0;
var recty1 = 0;
var rectx2 = 0;
var recty2 = 0;


//called when user clicks down on map to pan
function mouseZoomDown(e) {

	if ( mapfunctions == true && (toolmode == 1 || toolmode == 2)) {

		mouseX = 0;
		mouseY = 0;
		xcoor = 0;
		ycoor = 0;

		if (BrowserName == "Netscape" ) {		
			mouseX = e.pageX;
			mouseY = e.pageY;
		} else {	
			mouseX = event.clientX - 2;
			mouseY = event.clientY - 2;
			if (BrowserName == "Microsoft Internet Explorer") {		//IE correction
				mouseX += document.body.scrollLeft;
				mouseY += document.body.scrollTop;
			}
			if ( BrowserName == "Microsoft Internet Explorer" ) {
				theMap.setCapture(); 			//Needed for IE so that map div does not get mixed up with rectangle div - sets all capture events to this element only
			}
		}
		mouseover = true;

		obj = document.getElementById("theMap");
		var oX = obj.offsetLeft;
		var oY = obj.offsetTop;
		oX = oX + obj.parentNode.offsetLeft;		// finds the absolute position of the object
		oY = oY + obj.parentNode.offsetTop;

		xcoor = mouseX - oX;				// finds the relative position of the object
		ycoor = mouseY - oY;

		xcoor2 = 0;
		ycoor2 = 0;
		xshift = 0;
		yshift = 0;

		rectx1 = xcoor;
		recty1 = ycoor;
		rectangle.style.width = 1;
		rectangle.style.height = 1;
		rectangle.style.left = rectx1 + "px";
		rectangle.style.top = recty1 + "px";
		rectangle.style.borderColor = "#000000";
		rectangle.style.position = "absolute"; 
		rectangle.style.borderStyle = "solid"
		rectangle.style.borderWidth = 2;
		rectangle.style.visibility = "visible";
	}	
	return false

}



//called when user lets go of mouse after clicking down on map to pan
function mouseZoomUp(e) {	

	if (BrowserName == "Microsoft Internet Explorer") {
		theMap.releaseCapture();  			//Needed for IE so that map div does not get mixed up with rectangle div - switches off previous capture command
	}
	mouseover = false;
 	ZoomCoordinates();
}





function moveZoomImage(e) {
	

	if ( mapfunctions == true && (toolmode == 1 || toolmode == 2)) {
		
		if ( mouseover == true ) {

			rectx2 = xcoor;
			recty2 = ycoor;
			mouseX = 0;
			mouseY = 0;

			if (BrowserName == "Netscape" ) {		
				mouseX = e.pageX;
				mouseY = e.pageY;
			} else {	
				mouseX = event.clientX - 2;
				mouseY = event.clientY - 2;
				if (BrowserName == "Microsoft Internet Explorer") {		//IE correction
					mouseX += document.body.scrollLeft;
					mouseY += document.body.scrollTop;
				}
			}

			obj = document.getElementById("theMap");
			var oX = obj.offsetLeft;
			var oY = obj.offsetTop;
			oX = oX + obj.parentNode.offsetLeft;		// finds the absolute position of the object
			oY = oY + obj.parentNode.offsetTop;

			xcoor2 = mouseX - oX;				// finds the relative position of the object
			ycoor2 = mouseY - oY;	
			rectx2 = xcoor2;
			recty2 = ycoor2;

			if (xcoor2 > xcoor) { xshift = xcoor2-xcoor; } 
				else { xshift = xcoor-xcoor2; }
			if (ycoor2 > ycoor) { yshift = ycoor2-ycoor; } 
				else { yshift = ycoor-ycoor2; }	
			

			if (xcoor2 > xcoor) {						
				rectangle.style.left = rectx1 + "px";
				rectangle.style.width = rectx2 - rectx1;
			} else {
				rectangle.style.left = rectx2 + "px";
				rectangle.style.width = rectx1 - rectx2;
			}

			if (ycoor2 > ycoor) {						
				rectangle.style.top  = recty1 + "px";
				rectangle.style.height = recty2 - recty1;
			} else {
				rectangle.style.top  = recty2 + "px";
				rectangle.style.height = recty1 - recty2;
			}

			return false
		}
	}
}




function ZoomCoordinates() {

	if (BrowserName == "Microsoft Internet Explorer") {
		theMap.releaseCapture();  			//Needed for IE so that map div does not get mixed up with rectangle div - switches off previous capture command
	}
	rectangle.style.visibility = "hidden";
	dis_anim();

	// Calculate the current bounds
	var mapX1 = parseInt(easting) - ( parseInt(zoom) / 2 );
	var mapY2 = parseInt(northing) + ( (parseInt(zoom) / mapwidth * mapheight) / 2 );

	if ((xcoor2 != xcoor) && (ycoor2 != ycoor) && ((xcoor2 != 0) && (ycoor2 != 0))) {		// if has used marquee zoom

		var newleft = mapX1 + (xcoor * parseInt(zoom) / mapwidth);
		var newright = mapX1 + (xcoor2 * parseInt(zoom) / mapwidth);
		var newtop = mapY2 - (ycoor * parseInt(zoom) / mapwidth);
		var newbottom = mapY2 - (ycoor2 * parseInt(zoom) / mapwidth);

		last_easting = easting;
		last_northing = northing;
		last_zoom = zoom;
		easting = String(Math.round(newleft + ((newright - newleft)/2)));
		northing = String(Math.round(newtop - ((newtop - newbottom)/2)));

		if (toolmode == 1) {									// zoom in

			if ((xshift / mapwidth) > (yshift / mapheight)) {

				zoom = Math.round(xshift * parseInt(zoom) / mapwidth);
			} else {
				xshift = mapwidth * (yshift / mapheight);
				zoom = Math.round(xshift * parseInt(zoom) / mapwidth);
			}


		} else if (toolmode == 2) {								// zoom out

			var the_ratio =  mapwidth / xshift;
			zoom = Math.round(zoom * the_ratio);

		}	

		if (zoom >= max_zoom) {
			zoom = max_zoom;
		} else if (zoom <= min_zoom) {
			zoom = min_zoom;
		}

		getMap();
		setmap_labels();

	} else {											//if single click zoom

		last_easting = easting;
		last_northing = northing;
		last_zoom = zoom;
		easting = String(Math.round(mapX1 + (xcoor * parseInt(zoom) / mapwidth)));
		northing = String(Math.round(mapY2 - (ycoor * parseInt(zoom) / mapwidth)));

		if (toolmode == 1) {					//zoom in
			if (zoom > min_zoom) {
				calc_zoom_step('in');
			}

		} else if (toolmode == 2) {				//zoom out
			if (zoom < max_zoom) {
				calc_zoom_step('out');
			}
		}
		if (zoom > max_zoom) {
			zoom = max_zoom;
		} else if (zoom < min_zoom) {
			zoom = min_zoom;
		}
	}

	update_zoom_bar();
}







// *****************************************************************************************************
// ***************************************** INFO TOOL *************************************************
// *****************************************************************************************************



// called when user clicks on map with map tool
function Map_Info() {


	var sHTML = "";

	sHTML = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" alt=\"\" title=\"\"></td>";
	sHTML += "<td width=\"250\" id=\"menu_search_results\" class=\"sel_menu_bar\">Search results</td></tr>";
	sHTML += "<tr><td colspan=\"2\" class=\"menu_panel\" valign=\"top\">";
	// PANEL TABLE
	sHTML += "<div id=\"Infores\" STYLE=\"overflow:auto; height=390; width=270; text-align:left\" NOWRAP>";
	sHTML += "<table width=\"100%\" class=\"menu_panel_table\">";
	sHTML += "<tr><td width=\"100%\" class=\"menu_subpanel\" valign=\"top\">Looking for information<br>at the point you clicked...<br>&nbsp<br>Please wait</td></tr>";
	sHTML += "<tr><td class=\"panel_text\" height=\"5\" width=\"60\" colspan=\"3\"></td></tr>";
	sHTML += "</table></div>";
	// END OF PANEL TABLE
	sHTML += "</td></tr>";
	sHTML += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";

	menu_search_results_txt_on = sHTML;
	menu_active = "search_results";
	setup_menu_panel(); 


	// Calculate the current bounds
	var mapX1 = parseInt(easting) - ( parseInt(zoom) / 2 );
	var mapY2 = parseInt(northing) + ( (parseInt(zoom) / mapwidth * mapheight) / 2 );
	var i_easting = String(mapX1 + (rX * parseInt(zoom) / mapwidth));
	var i_northing = String(mapY2 - (rY * parseInt(zoom) / mapwidth));

	setinfolayers();

	var sURL = serverpath + "dialogbox_InfoPointViewer.asp?";
	sURL += "infomode=xml&theme=" + thetheme + "&easting=" + i_easting + "&northing=" + i_northing + "&MapWidth=" + zoom + "&Layers=" + not_current_INFO_layers;
	xmlinfoRequestObj = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")
	xmlinfoRequestObj.onreadystatechange = proccessinfoXML;
	xmlinfoRequestObj.open("GET", sURL, true);
	xmlinfoRequestObj.send(null);
}


function proccessinfoXML() {
	if (xmlinfoRequestObj.readyState == 4 && (xmlinfoRequestObj.status == 200 || xmlinfoRequestObj.status == 304)) {
		xmlDoc = xmlinfoRequestObj.responseXML;
		createInfoTable();
	}
}



// Populate the selection results table
function createInfoTable() {


	var x = 0;
	var en = 0;
	var sHTML = "";

	rootNode = xmlDoc.documentElement;

	if ( rootNode.childNodes.length > 0 ) {

		sHTML = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" alt=\"\" title=\"\"></td>";
		sHTML += "<td width=\"250\" id=\"menu_search_results\" class=\"sel_menu_bar\">Search results</td></tr>";
		sHTML += "<tr><td colspan=\"2\" class=\"menu_panel\" valign=\"top\">";
		// PANEL TABLE
		sHTML += "<div id=\"Infores\" STYLE=\"overflow:auto; height:390; width:270; text-align:left\" NOWRAP>";
		sHTML += "<table width=\"100%\" class=\"menu_panel_table\">";


		if ( rootNode.nodeName.toLowerCase() == "planaccesserror" ) {

			sHTML += "<tr><td width=\"100%\" class=\"menu_subpanel\" valign=\"top\">No features found<br>at Selected Point.</td></tr>";

		} else {

			if (rootNode.childNodes.item(0).childNodes.item(0).text == "0") {
				sHTML += "<tr><td width=\"100%\" class=\"menu_subpanel\" valign=\"top\">No features found<br>at Selected Point.</td></tr>";
			} else {
				sHTML += "<tr><td width=\"100%\" class=\"panel_title\" colspan=\"3\" valign=\"top\">Features found at your<br>map click location:</td></tr>";
				sHTML += "<tr><td class=\"panel_text\" height=\"5\" width=\"60\" colspan=\"3\"></td></tr>";
			}


			var INFO_Elements = rootNode.childNodes.item(0)
			var ResultNo = "";

			// Populate results table
			for (x = 0; x < rootNode.childNodes.length; x++) {

				// Get the number of columns in the XML document
				var featureRows = rootNode.childNodes.item(x).childNodes.length;
				sHTML += "<tr><td class=\"panel_text\" height=\"5\" width=\"70\"><img border=\"0\" src=\"images/fmnheader.gif\" alt=\"\" title=\"\"></td><td class=\"panel_text\" width=\"30\"></td><td class=\"panel_text\" width=\"160\"></td></tr>";


				for ( ll = 0; ll < featureRows; ll++ ) {

					var dataset_title = rootNode.childNodes.item(x).getAttribute("Name");	//xxx
					var display_title = dataset_to_display(dataset_title);	//xxx

					sHTML += "<tr><td class=\"panel_text_highlight_title\" colspan=\"3\">" + display_title + "</td></tr>";	//xxx
					var infoEasting = "";
					var infoNorthing = "";

					// Find the Easting and Northing & Distance value for the row - Loop around all the fields in the feature
					var dataRows = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.length;
					for ( l = 0; l < dataRows; l++ ) {

						var INFOnodeName = "";
						var INFOnodeText = "";

						if (BrowserName == "Netscape") {
							if(rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).hasChildNodes()){
								INFOnodeText = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).firstChild.nodeValue;
								INFOnodeName = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).nodeName;
							}
						} else {
							INFOnodeText = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).text;
							INFOnodeName = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).nodeName;
						}

						if ( INFOnodeName == "EASTING" || INFOnodeName == "Easting" ) {
							if (BrowserName == "Netscape") {
								infoEasting = INFOnodeText = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).firstChild.nodeValue;
							} else {
								infoEasting = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).text;
							}
					 
						} else if ( INFOnodeName == "NORTHING" || INFOnodeName == "Northing" ) {
							if (BrowserName == "Netscape") {
								infoNorthing = INFOnodeText = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).firstChild.nodeValue;
							} else {
								infoNorthing = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).text;
							}
						} 
					}


					// Loop around all the fields in the feature
					//var dataRows = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.length;
					for ( l = 0; l < dataRows; l++ ) {

						var INFOnodeName = "";
						var INFOnodeText = "";

						if (BrowserName == "Netscape") {
							if(rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).hasChildNodes()){
								INFOnodeText = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).firstChild.nodeValue;
								INFOnodeName = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).nodeName;
							}
						} else {
							INFOnodeText = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).text;
							INFOnodeName = rootNode.childNodes.item(x).childNodes.item(ll).childNodes.item(l).nodeName;
						}

						if ( INFOnodeName == "EASTING" || INFOnodeName == "Easting" ) {
							// Do nothing							 
						} else if ( INFOnodeName == "NORTHING" || INFOnodeName == "Northing" ) {
							// Do nothing

						} else {

							if (INFOnodeText != "" && INFOnodeText != "N/A" && INFOnodeText != "n/a") {

								INFOnodeName = INFOnodeName.replace(/_/g," ");
								INFOnodeName = (INFOnodeName.toUpperCase()).substring(0,1) + (INFOnodeName.toLowerCase()).substring(1,INFOnodeName.length);
								sHTML += "<tr><td class=\"panel_text\" valign=\"top\" colspan=\"2\">" + INFOnodeName + ":</td>";
								sHTML += "<td class=\"panel_text\" valign=\"top\">";

								if ( INFOnodeText.substring(0,5) == 'http:' ) {
									sHTML += "<a href='" + INFOnodeText + "' target='_blank' title='Open webpage in new window'>Open webpage in new window</a></td>";
								} else if ( INFOnodeText.substring(0,6) == 'https:' ) {
									sHTML += "<a href='" + INFOnodeText + "' target='_blank' title='Open webpage in new window'>Open webpage in new window</a></td>";
								} else if ( INFOnodeText.substring(0,7) == 'mailto:' ) {
									sHTML += "<a href=\"" + INFOnodeText + "\" title='Send email'>Send email</a></td>";
								} else {
									INFOnodeText = INFOnodeText.replace(/,,/g,",");
									INFOnodeText = INFOnodeText.replace(/,/g,", ");
									INFOnodeText = INFOnodeText.replace(/, , /g,", ");
									sHTML += INFOnodeText + "</td></tr>";
								}
							}
						}
					}
					if (infoEasting != "" && infoNorthing != "") {
						var transportdirectlink = "http://www.transportdirect.info/transportdirect/en/journeyplanning/jplandingpage.aspx?id=BusinessLinks&oo=&on=&o=&do=en&dn=Selected " + rootNode.childNodes.item(x).getAttribute("Name") + "&d=" + infoEasting + "," + infoNorthing + "&p=0";
						sHTML += "<tr><td class=\"panel_text\" valign=\"top\" colspan=\"2\">DIRECTIONS:</td>";
						sHTML += "<td class=\"panel_text\"><a href=\"" + transportdirectlink + "\" target='_blank' title='Open Travel Planner website in new window'><b>Travel Planner</b> (opens in a new window)</a></td></tr>";
					}
				}
				sHTML += "<tr><td class=\"panel_text\" height=\"5\" width=\"60\" colspan=\"3\"></td></tr>";
			}
		}
		sHTML += "</table></div>";
		// END OF PANEL TABLE
		sHTML += "</td></tr>";
		sHTML += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";

		menu_search_results_txt_on = sHTML;
		menu_active = "search_results";
		setup_menu_panel(); 

	}
	xmlDoc = null;
	document.body.style.cursor = 'default';

}





// *****************************************************************************************************
// ***************************************** Location Search ********************************************
// *****************************************************************************************************




function location_array(location_list,easting_list,northing_list) {

	Loc_name_array = location_list.split(",");
	Loc_name_array = Loc_name_array.reverse();

	Loc_east_array = easting_list.split(",");
	Loc_east_array = Loc_east_array.reverse();

	Loc_north_array = northing_list.split(",");
	Loc_north_array = Loc_north_array.reverse();
}



function loc_find(theselectinput) {

	if (theselectinput == "loc") {
		var loc_list = document.getElementById("location_option")
		var f = loc_list.options[loc_list.selectedIndex].value;
	} else if (theselectinput == "fmn") {
		var loc_list = document.getElementById("fmnlocation_option")
		var f = loc_list.options[loc_list.selectedIndex].value;
	}

	if (f == 0) {
		// do nothing
	} else {
		dis_anim();
		//thelocation = f.options[f].value;
		last_easting = easting;
		last_northing = northing;
		easting = Loc_east_array[f];
		northing = Loc_north_array[f];
		last_zoom = zoom;
		zoom = 4000;
		getMap();
		update_zoom_bar();
		setmap_labels();
	}
}



// *****************************************************************************************************
// ***************************************** Address Search ********************************************
// *****************************************************************************************************


function ignoreSpaces(string) {
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	temp += splitstring[i];
	return temp;
}



function checkEnter(e) {

	var characterCode; 			//literal character code will be stored in this variable

	// IE doesn't pass the event object
	if (e == null) {
		e = window.event;
	} 
	if (e && e.which) { 			//if which property of event object is supported (NN4)
		characterCode = e.which; 	//character code is contained in NN4's which property
	} else {
		characterCode = e.keyCode;	//character code is contained in IE's keyCode property
	}

	if (characterCode == 13) { 		//if generated character code is equal to ascii 13 (if enter key)
		Add_Search();		 	//submit the form
	} 
}




function Add_Clear() {
	display_off();
	AddName = "";
	AddNum = "";
	AddRoad = "";
	AddPC = "";
	create_findplaces_menu();
	setup_menu_panel();
}




function Add_Search() {

	display_off();
	document.body.style.cursor = 'wait';

	AddName = document.getElementById("addName").value;
	AddNum = document.getElementById("addNumber").value;
	AddRoad = document.getElementById("addRoad").value;
	AddPC = document.getElementById("addPostcode").value;

	//If FMN postcode is blank, reset it to same as Address Search
	if (( AddPC != "" ) && (FMNPC == "")) {
		FMNPC = AddPC;
	}

	create_findplaces_menu();

	if ((AddName == '') && (AddNum == '') && (AddRoad == '') && (AddPC == '')){
		alert("Please enter some address details.");
		xmlDoc = null;
		document.body.style.cursor = 'default';
		return;
	} else {

		var sHTML = "";
		sHTML = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" alt=\"\" title=\"\"></td>";
		sHTML += "<td width=\"250\" id=\"menu_search_results\" class=\"sel_menu_bar\">Search results</td></tr>";
		sHTML += "<tr><td colspan=\"2\" class=\"menu_panel\" valign=\"top\">";
		// PANEL TABLE
		sHTML += "<div id=\"addresses\" STYLE=\"overflow:auto; height=390; width=270; text-align:left\" NOWRAP>";
		sHTML += "<table width=\"100%\" class=\"menu_panel_table\">";
		sHTML += "<tr><td width=\"100%\" class=\"menu_subpanel\" valign=\"top\">Searching for your address...<br>&nbsp<br>Please wait</td></tr>";
		sHTML += "<tr><td class=\"panel_text\" height=\"5\" width=\"60\" colspan=\"3\"></td></tr>";
		sHTML += "</table></div>";
		// END OF PANEL TABLE
		sHTML += "</td></tr>";
		sHTML += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";

		menu_search_results_txt_on = sHTML;
		menu_active = "search_results";
		setup_menu_panel(); 

		var sURL = serverpath + "panel_AP_SearchView.asp?AddressMode=xml&pagelength=100";   // pagelength changed from 5000 April 2011 for new PSMA Royal Mail PAF requirement
		if ( AddName != "" ) { sURL += "&name=" + AddName; }
		if ( AddNum != "" ) { sURL += "&number=" + AddNum; }
		if ( AddRoad != "" ) { sURL += "&road=" + AddRoad; }
		if ( AddPC != "" ) { sURL += "&postcode=" + AddPC; }

		xmladdRequestObj = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")
		xmladdRequestObj.onreadystatechange = proccessaddXML;
		xmladdRequestObj.open("GET", sURL, true);
		xmladdRequestObj.send(null);
	}

}



function proccessaddXML() {
	if (xmladdRequestObj.readyState == 4 && (xmladdRequestObj.status == 200 || xmladdRequestObj.status == 304)) {
		xmlDoc = xmladdRequestObj.responseXML;
		ReadAddressData();
	}
}


var AddressList = "";

function ReadAddressData() {

	var ASEasting = 0;
	var ASNorthing = 0;
	var sHTML = "";
	rootNode = xmlDoc.documentElement;


	if ( rootNode.childNodes.length > 0 ) {

		sHTML = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" alt=\"\" title=\"\"></td>";
		sHTML += "<td width=\"250\" id=\"menu_search_results\" class=\"sel_menu_bar\">Search results</td></tr>";
		sHTML += "<tr><td colspan=\"2\" class=\"menu_panel\" valign=\"top\">";
		// PANEL TABLE
		sHTML += "<div id=\"addresses\" STYLE=\"overflow:auto; height:390; width:270; text-align:left\" NOWRAP>";
		sHTML += "<table width=\"100%\" class=\"menu_panel_table\">";

		if ( rootNode.nodeName.toLowerCase() != "planaccesserror" ) {


			var chk_val = 0;
			if (BrowserName == "Netscape") {
				chk_val = rootNode.childNodes.item(0).childNodes.item(0).firstChild.nodeValue;
			} else {
				chk_val = rootNode.childNodes.item(0).childNodes.item(0).text;
			}

			if (chk_val == "0") {
				sHTML += "<tr><td width=\"100%\" class=\"menu_subpanel\" valign=\"top\">No Addresses found.<br>Please search again.</td></tr>";
			} else {
				sHTML += "<tr><td width=\"100%\" class=\"panel_title\" valign=\"top\">Click Address to Locate<br>on the Map</td></tr>";
			}

			for ( x = 1; x < rootNode.childNodes.length; x++ ) {				//loop through addresses

				sHTML += "<tr>";
				var sText = "";
				var AddressElement = rootNode.childNodes.item(x)

				var theswitch = 0;
				var theposttown = "";

				// Find the Easting and Northing for the row
				for ( i = 0; i < AddressElement.childNodes.length; i++ ) {			//loop through fields

					var AddressLineElement = AddressElement.childNodes.item(i);
					var ASNodeName = AddressLineElement.nodeName;

					var ASNodeVal = "";
					if (BrowserName == "Netscape") {
						if(AddressLineElement.hasChildNodes()){
							ASNodeVal = AddressLineElement.firstChild.nodeValue;
						}
					} else {
						ASNodeVal = AddressLineElement.text;
					}


					if ( ASNodeName == "X" || ASNodeName.substring(0,7) == "Easting" ) {
						ASEasting = ASNodeVal;	

					} else if ( ASNodeName == "Y" || ASNodeName.substring(0,8) == "Northing" ) {
						ASNorthing = ASNodeVal;
					}


					if ( ASEasting.toString().length == 7 ) {
						ASEasting = parseInt( ASEasting /= 10 );
					}
					if ( ASNorthing.toString().length == 7 ) {
						ASNorthing = parseInt( ASNorthing /= 10 );
					}


					if ( ASNodeName == "X" || ASNodeName.substring(0,7) == "Easting" || ASNodeName == "Y" || ASNodeName.substring(0,8) == "Northing" || ASNodeName.substring(0,8) == "URSN" ) {
						// Do Nothing as the nodeName is a Easting, Northing or USRN Field
					} else {
						// Capture the field info
						// Only add element if there is data on the node
						ASNodeVal = ASNodeVal.replace(/'/g, "");
						if (( ASNodeVal.length > 0 ) && (ASNodeName != "Posttown")) {
							sText += ASNodeVal + ", ";
							if (theswitch == 1) {					//the switch is to swap posttown with dependent locality as they are returned out of order by the xml
								theswitch = 0;
								sText += theposttown + ", ";
							}
						} else if (( ASNodeVal.length > 0 ) && (ASNodeName == "Posttown")) {
							theswitch = 1;
							theposttown = ASNodeVal;
						} else if (theswitch == 1) {
							theswitch = 0;
							sText += theposttown + ", ";
						}
					}
				}

				// Remove the last ,
				sText = sText.substring(0, sText.length - 2);
				sHTML += "<td class=\"panel_text\" valign=\"top\" id=\"addnum_" + x + "\" onclick=\"AddMoveMap(" + ASEasting + "," + ASNorthing + ",\'" + sText + "\')\" tabindex=\"0\" onkeydown=\"return event.keyCode != 13 || AddMoveMap(" + ASEasting + "," + ASNorthing + ",\'" + sText + "\');\" onmouseover=\"highlight_add(" + x + ")\" onmouseout=\"unhighlight_add(" + x + ")\"  onfocus=\"highlight_add(" + x + ")\" onblur=\"unhighlight_add(" + x + ")\" style=\"cursor:pointer\">" + sText + "</td>";
				sHTML += "</tr>";
			}
		}

		sHTML += "<tr height=\"5\"><td>&nbsp;</td></tr>";
		sHTML += "<tr height=\"35\"><td><input type=\"button\" name=\"addnewsearchb\" id=\"addnewsearchb\" onclick=\"FP_NewSearch()\" align=\"left\" value=\"New Search\" style=\"cursor:pointer\"></td></tr>";

		sHTML += "</table></div>";
		// END OF PANEL TABLE
		sHTML += "</td></tr>";
		sHTML += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";
		menu_search_results_txt_on = sHTML;
		menu_active = "search_results";
		setup_menu_panel(); 
		AddressList = sHTML;


	}// End If
	xmlDoc = null;
	document.body.style.cursor = 'default';
}




function highlight_add(theaddnum) {

	var theaddid = "addnum_" + theaddnum;
	document.getElementById(theaddid).className="addtable_rollover";
}



function unhighlight_add(theaddnum) {

	var theaddid = "addnum_" + theaddnum;
	document.getElementById(theaddid).className="panel_text";
}




function AddMoveMap(iEasting, iNorthing, ADD_str) {
	
	dis_anim();
	var theadd_array = ADD_str.split(",");

	//Change menu
	sHTML = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" alt=\"\" title=\"\"></td>";
	sHTML += "<td width=\"250\" id=\"menu_search_results\" class=\"sel_menu_bar\">Search results</td></tr>";
	sHTML += "<tr><td colspan=\"2\" class=\"menu_panel\" valign=\"top\">";
	// PANEL TABLE
	sHTML += "<div id=\"addresses\" STYLE=\"overflow:auto; height:390; width:270; text-align:left\" NOWRAP>";
	sHTML += "<table width=\"100%\" class=\"menu_panel_table\">";
	sHTML += "<tr><td width=\"100%\" class=\"panel_title\" valign=\"top\">Your chosen Address<br>(now displaying on the map)</td></tr>";
	sHTML += "<tr><td height=\"5\" colspan=\"2\"></td></tr>";
	sHTML += "<tr><td class=\"panel_text\" valign=\"top\" id=\"chosenadd\">";
	for (x in theadd_array) {
		sHTML += theadd_array[x] + "<br>"; 
	}
	sHTML += "</td></tr>";
	sHTML += "<tr height=\"5\"><td>&nbsp;</td></tr>";
	sHTML += "<tr height=\"35\"><td>";
	sHTML += "<input type=\"button\" name=\"retaddlistb\" id=\"retaddlistb\" onclick=\"Ret_Add_List()\" align=\"left\" value=\"Return to list\" style=\"cursor:pointer\">";
	sHTML += "<input type=\"button\" name=\"addnewsearchb\" id=\"addnewsearchb\" onclick=\"FP_NewSearch()\" align=\"left\" value=\"New Search\" style=\"cursor:pointer\"></td></tr>";
	sHTML += "</table></div>";
	// END OF PANEL TABLE

	sHTML += "</td></tr>";
	sHTML += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";
	menu_search_results_txt_on = sHTML;
	menu_active = "search_results";
	setup_menu_panel(); 

	//set parameters for getmapimage to show location symbol
	add_chosen = "yes";
	addeasting = iEasting;
	addnorthing = iNorthing;

	//Change map
	last_zoom = zoom;
	last_easting = easting;
	last_northing = northing;
	zoom = 250;
	easting=String(iEasting);
	northing=String(iNorthing);
	getMap();
	update_zoom_bar();
	setmap_labels();
}



function Ret_Add_List() {
	
	menu_search_results_txt_on = AddressList;
	menu_active = "search_results";
	setup_menu_panel(); 
}



// *****************************************************************************************************
// **************************************** Find my nearest ********************************************
// *****************************************************************************************************

var iEasting = 0;
var iNorthing = 0;
var sPostcode = "";
var PCfound = 0;
var FMNPC_easting = 0;
var FMNPC_northing = 0;


function setup_initial_fmn_layers() {				//NEW 3.2		// add in FMN layers to be used as default

	fmn_live_list_array[0] = "Bus Stations";
	fmn_live_list_array[1] = "Car Parks";
	fmn_live_list_array[2] = "Council Offices";
	fmn_live_list_array[3] = "Dental Surgeries";
	fmn_live_list_array[4] = "Doctors Surgeries";
	fmn_live_list_array[5] = "Leisure Centres";
	fmn_live_list_array[6] = "Libraries";
	fmn_live_list_array[7] = "Post Offices";
	fmn_live_list_array[8] = "Primary Schools";
	fmn_live_list_array[9] = "Public Toilets";
	fmn_live_list_array[10] = "Recycling Points";
	fmn_live_list_array[11] = "Secondary Schools";
	fmn_live_list_array[12] = "Tourist Information Centres";
	fmn_live_list_array[13] = "Train Stations";
	fmn_live_list_array[14] = "Hospitals with A and E";


	var x = 0;
	var fmn_initial_layer_test = "";
	for (x in fmn_live_list_array) {				//check all these are available as FMN layers in the PlanAccess console
		fmn_initial_layer_test = fmn_live_list_array[x];
		var y = 0;
		var fmn_db_layer_test = "";
		chk = 0;
		for (y in fmn_db_list_array) {
			fmn_db_layer_test = fmn_db_list_array[y];
			if (fmn_db_layer_test == fmn_initial_layer_test) {
				chk = 1;
			}
		}
		if (chk == 0) {
			fmn_live_list_array[x] = "";
		}
	}

	var x = 0;
	var fmn_layer_test = "";
	finish_chk = 0;
	while (finish_chk == 0) {
		finish_chk = 1;
		for (x in fmn_live_list_array) {				//remove any layers identified above
			fmn_layer_test = fmn_live_list_array[x];
			if (fmn_layer_test == "") {
				fmn_live_list_array.splice(x,1);
				x = x - 1;
				finish_chk = 0;
			}
		}
	}
	fmn_live_list_array.sort();
}


function add_layer_to_fmn_array(the_new_layer) {		//NEW 3.2

	var x = 0;
	var fmn_live_layers = fmn_live_list_array.join(",");
	if (fmn_live_layers.match(the_new_layer) == the_new_layer) {
		// do nothing - already in list
	} else {
		var fmn_db_layers = fmn_db_list_array.join(",");
		if (fmn_db_layers.match(the_new_layer) == the_new_layer) {
			fmn_live_list_array.push(the_new_layer);
			fmn_live_list_array.sort();
			create_findplaces_menu();
		} else {
			// do nothing - not available as fmn layer
		}
	}
}


function fmn_array(fmn_list) {

	fmn_db_list_array = fmn_list.split(",");
	fmn_db_list_array = fmn_db_list_array.reverse();
}





function FMN_Search(){


	var f = document.getElementById("fmnLayerName");
	FMNPC = document.getElementById("fmnPostcode").value;

	var FMNplace = document.getElementById("fmnlocation_option");
	FMNloc_index = FMNplace.options[FMNplace.selectedIndex].value;

	FMNrad = document.getElementById("fmnradius").value;

	if (document.getElementById('fmn_km').checked) {
		 FMNmeasure = "km";
	} else {
		 FMNmeasure = "miles";
	}

	if (document.getElementById('fmnlocpc').checked) {
		 FMNloc_type = "fmn_optionpc";
	} else {
		 FMNloc_type = "fmn_optionloc";
	}

	//If Address Search postcode is blank, reset it to same as FMN
	if (( FMNPC != "" ) && (AddPC == "")) {
		AddPC = FMNPC;
	}

	create_findplaces_menu();
	update_linkform();

	if (f.selectedIndex == 0) {
		// do nothing
	} else {
		fmn_layer = f.options[f.selectedIndex].value;

		if ( ((FMNPC == '') && (FMNloc_index == 0) ) || (FMNrad == '')) {				// if data missing
	    	alert("Please enter a postcode or choose a placename, and enter a search radius.");

		} else if ((FMNPC == '') && (FMNloc_type == 'fmn_optionpc')) {					// if PC search chosen, but no PC
			alert("You have chosen a postcode search, please enter a postcode.");

		} else if ((FMNPC != '') && (FMNloc_type == 'fmn_optionpc')) {					// if PC search chosen and postcode given
			//Start FMN Postcode Search functions
			RequestPostcodeCoordinates();

		} else if ((FMNloc_index == 0) && (FMNloc_type == 'fmn_optionloc')) {				// if PC search chosen, but no PC
			alert("You have chosen a placename search, please choose a placename from the list.");

		} else if ((FMNloc_index != 0) && (FMNloc_type == 'fmn_optionloc')) {				// if PC search chosen and postcode given
			//Start FMN Postcode Search functions
			iEasting = Loc_east_array[FMNloc_index];
			iNorthing = Loc_north_array[FMNloc_index];
			CallFindMyNearest();
		}
	}
}




function RequestPostcodeCoordinates() {

	document.body.style.cursor = 'wait';

	// Is the postcode the same as the orignal search
	if (( sPostcode == FMNPC ) && (PCfound == 1)) {

		iEasting = FMNPC_easting;
		iNorthing = FMNPC_northing;
		CallFindMyNearest()

	} else {			//Postcode Search

		PCfound = 0;
		sPostcode = FMNPC;
		var sURL = serverpath + "dialogbox_InfoPointViewer.asp?";
		sURL += "infomode=xml&theme=codepoint_fmn&layername=DShire_Points&fieldname=Postcode_com&fieldvalue=" + FMNPC;

		xmlpcRequestObj = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")
		xmlpcRequestObj.onreadystatechange = proccesspcXML;
		xmlpcRequestObj.open("GET", sURL, true);
		xmlpcRequestObj.send(null);
	}
}



function proccesspcXML() {
	if (xmlpcRequestObj.readyState == 4 && (xmlpcRequestObj.status == 200 || xmlpcRequestObj.status == 304)) {
		xmlDoc = xmlpcRequestObj.responseXML;
		ExtractPostcodeCoordinates();
	}
}



function ExtractPostcodeCoordinates() {

	rootNode = xmlDoc.documentElement;

	if ( rootNode.childNodes.length > 0 ) {

		if ( rootNode.nodeName.toLowerCase() == "planaccesserror" ) {

			alert('Postcode not found');
			PCfound = 0;
                        document.body.style.cursor = 'default';

		} else {

			var EastingElement = rootNode.getElementsByTagName("Easting")
			var NorthingElement = rootNode.getElementsByTagName("Northing")

			if ( BrowserName == "Netscape" ) {
				iEasting = EastingElement.item(0).firstChild.nodeValue;
				iNorthing = NorthingElement.item(0).firstChild.nodeValue;
			} else {
				iEasting = EastingElement.item(0).text;
				iNorthing = NorthingElement.item(0).text;
			}
			
			FMNPC_easting = iEasting;
			FMNPC_northing = iNorthing;

			// Make a find my nearest call
			PCfound = 1;
			CallFindMyNearest();
		}

	} else {
		document.body.style.cursor = 'default';
		PCfound = 0;
		alert('Postcode not found');
	}
}




	
function CallFindMyNearest() {

	var CodePointtheme = 'codepoint';

	if (FMNmeasure == "miles") {			
		var sURL = serverpath + "dialogbox_FindMyNearestResults.asp?fmnmode=xml&theme=GDC_CodepointFMN&FMNEasting=" + iEasting + "&FMNNorthing=" + iNorthing + "&FMNRadius=" + FMNrad + "&measurement=mile&FMNLayer=" + fmn_layer + "&SortFeatures=true";
	} else {
		var sURL = serverpath + "dialogbox_FindMyNearestResults.asp?fmnmode=xml&theme=GDC_CodepointFMN&FMNEasting=" + iEasting + "&FMNNorthing=" + iNorthing + "&FMNRadius=" + FMNrad + "&measurement=kilometres&FMNLayer=" + fmn_layer + "&SortFeatures=true";
	}

	var sHTML = "";
	sHTML = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" alt=\"\" title=\"\"></td>";
	sHTML += "<td width=\"250\" id=\"menu_search_results\" class=\"sel_menu_bar\">Search results</td></tr>";
	sHTML += "<tr><td colspan=\"2\" class=\"menu_panel\" valign=\"top\">";
	// PANEL TABLE
	sHTML += "<div id=\"FMNres\" STYLE=\"overflow:auto; height=390; width=270; text-align:left\" NOWRAP>";
	sHTML += "<table width=\"100%\" class=\"menu_panel_table\">";
	sHTML += "<tr><td width=\"100%\" class=\"menu_subpanel\" valign=\"top\">Loading your 'Find My Nearest' Search...<br>&nbsp<br>Please wait</td></tr>";
	sHTML += "<tr><td class=\"panel_text\" height=\"5\" width=\"60\" colspan=\"3\"></td></tr>";
	sHTML += "</table></div>";
	// END OF PANEL TABLE
	sHTML += "</td></tr>";
	sHTML += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";

	xmlfmnRequestObj = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")
	xmlfmnRequestObj.onreadystatechange = proccessfmnXML;
	xmlfmnRequestObj.open("GET", sURL, true);
	xmlfmnRequestObj.send(null);

	menu_search_results_txt_on = sHTML;
	menu_active = "search_results";
	setup_menu_panel(); 
}




function proccessfmnXML() {
	if (xmlfmnRequestObj.readyState == 4 && (xmlfmnRequestObj.status == 200 || xmlfmnRequestObj.status == 304)) {
		xmlDoc = xmlfmnRequestObj.responseXML;
		ParseFindMyNearestResults();
	}
}




function ParseFindMyNearestResults() {

	var x = 0;
	var i = 0;
	var en = 0;
	var sHTML = "";
	var FMNEasting = 0;
	var FMNNorthing = 0;
	var FMNDistance = 0;
	var FMNDistance_km = 0;
	var FMNDistance_miles = 0;

	rootNode = xmlDoc.documentElement;

	if ( rootNode.childNodes.length > 0 ) {

		sHTML = "<tr><td width=\"22\" height=\"25\" class=\"sel_menu_bar\"><img border=\"0\" src=\"images/menu_arrow_down.gif\" width=\"20\" height=\"14\" alt=\"\" title=\"\"></td>";
		sHTML += "<td width=\"250\" id=\"menu_search_results\" class=\"sel_menu_bar\">Search results</td></tr>";
		sHTML += "<tr><td colspan=\"2\" class=\"menu_panel\" valign=\"top\">";
		// PANEL TABLE
		sHTML += "<div id=\"FMNres\" STYLE=\"overflow:auto; height:390; width:270; text-align:left\" NOWRAP>";
		sHTML += "<table width=\"100%\" class=\"menu_panel_table\">";

		if ( rootNode.nodeName.toLowerCase() == "planaccesserror" ) {

			sHTML += "<tr><td width=\"100%\" class=\"menu_subpanel\" valign=\"top\">No " + fmn_layer + " <br>found within " + FMNrad + " " + FMNmeasure + ".</td></tr>";

		} else {

			if (rootNode.childNodes.item(0).childNodes.item(0).text == "0") {
				sHTML += "<tr><td width=\"100%\" class=\"menu_subpanel\" valign=\"top\">No " + fmn_layer + " <br>found within " + FMNrad + " " + FMNmeasure + ".</td></tr>";
			} else {
				var the_location_txt = "";
				if (FMNloc_type == "fmn_optionpc") {
		 			the_location_txt = FMNPC.toUpperCase();
				} else {
					the_location_txt = Loc_name_array[FMNloc_index];
				}
				var img_src = "http://derbyshiremaps.derbyshire.gov.uk/portal_32/images/MAPKEY/" + fmn_layer + ".gif";
				sHTML += "<tr><td width=\"100%\" class=\"panel_title\" colspan=\"3\" valign=\"top\"><img border=\"0\" src=\"" + img_src + "\" alt=\"Map feature symbol\" title=\"symbol showing how this information will appear on the map\"> " + fmn_layer + "<br>search results within<br>" + FMNrad + " " + FMNmeasure + " of " + the_location_txt + "</td></tr>";
				sHTML += "<tr><td class=\"panel_text\" height=\"4\" colspan=\"3\"></td></tr>";
				sHTML += "<tr><td class=\"menu_bar_spacer\" height=\"3\" colspan=\"3\"></td></tr>";
				AddFMNlayer(fmn_layer);
			}

			var FindMyNearestElements = rootNode.childNodes.item(0)
			var ResultNo = "";

			// Populate results table
			for ( x = 0; x < FindMyNearestElements.childNodes.length; x++ ) {

				var FindMyNearestElement = FindMyNearestElements.childNodes.item(x);
				ResultNo = String(x + 1);

				// Find the Easting and Northing & Distance value for the row
				for ( en = 0; en < FindMyNearestElement.childNodes.length; en++ ) {

					if ( FindMyNearestElement.childNodes.item(en).nodeName == "Easting" ) {

						if (BrowserName == "Netscape") {
							FMNEasting = FindMyNearestElement.childNodes.item(en).firstChild.nodeValue;
						} else {
							FMNEasting = FindMyNearestElement.childNodes.item(en).text;
						}

					} else if ( FindMyNearestElement.childNodes.item(en).nodeName == "Northing" ) {

						if (BrowserName == "Netscape") {
							FMNNorthing = FindMyNearestElement.childNodes.item(en).firstChild.nodeValue;
						} else {
							FMNNorthing = FindMyNearestElement.childNodes.item(en).text;
						}

					} else if ( FindMyNearestElement.childNodes.item(en).nodeName == "Distance" ) {

						if (BrowserName == "Netscape") {
							FMNDistance = FindMyNearestElement.childNodes.item(en).firstChild.nodeValue;
						} else {
							FMNDistance = FindMyNearestElement.childNodes.item(en).text;
						}
					} 
				}

				//distances returned as m for kilometres search, and miles for mile search
				if (FMNmeasure == "miles") {
					FMNDistance_km = FMNDistance * 1.609344;
					FMNDistance_km = FMNDistance_km.toFixed(1);	
					FMNDistance_miles = FMNDistance * 1;
					FMNDistance_miles = FMNDistance_miles.toFixed(1);	
				} else {
					FMNDistance_km = FMNDistance / 1000;
					FMNDistance_km = FMNDistance_km.toFixed(1);	
					FMNDistance_miles = FMNDistance_km * 0.621371192;
					FMNDistance_miles = FMNDistance_miles.toFixed(1);
				}

				FMNPC = FMNPC.toUpperCase();
				sHTML += "<tr><td class=\"panel_text\" height=\"5\" width=\"70\"><img border=\"0\" src=\"images/fmnheader.gif\" alt=\"\" title=\"\" width=\"70\"></td><td class=\"panel_text\" width=\"40\"></td><td class=\"panel_text\" width=\"120\"></td></tr>";
				sHTML += "<tr><td class=\"panel_text_highlight_title\" align=\"center\" valign=\"middle\" height=\"21\" colspan=\"2\">" + FMNDistance_km + " km / " + FMNDistance_miles + " miles</td>";
				sHTML += "<td class=\"panel_text\" align=\"right\"><input type=\"button\" name=\"findonmapb\" id=\"findonmapb\" onclick=\"FMNMoveMap(" + FMNEasting + "," + FMNNorthing +")\" align=\"right\" valign=\"middle\" value=\"Find on Map\" style=\"cursor:pointer\"></td></tr>";

				for ( i = 0; i < FindMyNearestElement.childNodes.length; i++ ) {

					if ( FindMyNearestElement.childNodes.item(i).nodeName == "EASTING" || FindMyNearestElement.childNodes.item(i).nodeName == "Easting" ) {
						// Do nothing
					} else if ( FindMyNearestElement.childNodes.item(i).nodeName == "NORTHING" || FindMyNearestElement.childNodes.item(i).nodeName == "Northing" ) {
						// Do nothing
					} else if ( FindMyNearestElement.childNodes.item(i).nodeName == "Distance" ) {
						// Do nothing
					} else if ( FindMyNearestElement.childNodes.item(i).nodeName == "Zoom" ) {
						// Do nothing
					} else if ( FindMyNearestElement.childNodes.item(i).nodeName == "UPRN" ) {
						// Do nothing
					} else {

						var FMNnodeName = "";
						var FMNnodeText = "";

						if (BrowserName == "Netscape") {
							FMNnodeText = FindMyNearestElement.childNodes.item(i).firstChild.nodeValue;
							FMNnodeName = FindMyNearestElement.childNodes.item(i).nodeName;
						} else {
							FMNnodeText = FindMyNearestElement.childNodes.item(i).text;
							FMNnodeName = FindMyNearestElement.childNodes.item(i).nodeName;
						}

						if (FMNnodeText != "" && FMNnodeText != "N/A" && FMNnodeText != "n/a") {

							FMNnodeName = FMNnodeName.replace(/_/g," ");
							FMNnodeName = (FMNnodeName.toUpperCase()).substring(0,1) + (FMNnodeName.toLowerCase()).substring(1,FMNnodeName.length);
							sHTML += "<tr><td class=\"panel_text\" valign=\"top\" colspan=\"1\">" + FMNnodeName + ":</td>";
							sHTML += "<td class=\"panel_text\" valign=\"top\" colspan=\"2\">";

							if ( FMNnodeText.substring(0,5) == 'http:' ) {
								sHTML += "<a href='" + FMNnodeText + "' target='_blank' title='Open webpage in new window'>Open webpage in new window</a></td>";
							} else if ( FMNnodeText.substring(0,6) == 'https:' ) {
								sHTML += "<a href='" + FMNnodeText + "' target='_blank' title='Open webpage in new window'>Open webpage in new window</a></td>";
							} else if ( FMNnodeText.substring(0,7) == 'mailto:' ) {
								sHTML += "<a href=\"" + INFOnodeText + "\" title='Send email'>Send email</a></td>";
							} else {
								FMNnodeText = FMNnodeText.replace(/,,/g,",");
								FMNnodeText = FMNnodeText.replace(/,/g,", ");
								FMNnodeText = FMNnodeText.replace(/, , /g,", ");
								sHTML += FMNnodeText + "</td></tr>";
							}
						}
					}
				}
																																							
				if (FMNEasting != "" && FMNNorthing != "") {
					var transportdirectlink = "http://www.transportdirect.info/transportdirect/en/journeyplanning/jplandingpage.aspx?id=BusinessLinks&oo=en&on=" + the_location_txt + "&o=" + iEasting + "," + iNorthing + "&do=en&dn=Selected " + fmn_layer + "&d=" + FMNEasting + "," + FMNNorthing + "&p=0";
					sHTML += "<tr><td class=\"panel_text\" valign=\"top\" colspan=\"1\">DIRECTIONS:</td>";
					sHTML += "<td class=\"panel_text\" colspan=\"2\"><a href=\"" + transportdirectlink + "\" target='_blank' title='Open Travel Planner website in new window'><b>Travel Planner</b> (opens in a new window)</a></td></tr>";
				}

				sHTML += "<tr><td class=\"panel_text\" height=\"4\" colspan=\"3\"></td></tr>";
				sHTML += "<tr><td class=\"menu_bar_spacer\" height=\"3\" colspan=\"3\"></td></tr>";
			}
		}

		sHTML += "<tr height=\"35\"><td colspan=\"3\"><input type=\"button\" name=\"fmnnewsearchb\" id=\"fmnnewsearchb\" onclick=\"FP_NewSearch()\" align=\"left\" value=\"New Search\" style=\"cursor:pointer\"></td></tr>";
		sHTML += "</table></div>";
		// END OF PANEL TABLE
		sHTML += "</td></tr>";
		sHTML += "<tr><td width=\"100%\" height=\"2\" colspan=\"2\" class=\"menu_bar_spacer\"></td></tr>";

		menu_search_results_txt_on = sHTML;
		menu_active = "search_results";
		setup_menu_panel(); 

	}
	xmlDoc = null;
	document.body.style.cursor = 'default';
}
			


function FMNMoveMap(iEasting, iNorthing) {
	
	dis_anim();
	last_zoom = zoom;
	last_easting = easting;
	last_northing = northing;
	easting=String(iEasting);
	northing=String(iNorthing);
	zoom = 750;
	getMap();
	update_zoom_bar();
	setmap_labels();
}



function AddFMNlayer(fmn_layer) {

	var x = 0;
	var fmn_layer_test = "";

	for (x in layers_list_array) {

		fmn_layer_test = layers_list_display_array[x];	//xxx

		if (fmn_layer_test.toUpperCase() != fmn_layer.toUpperCase() ) {
			// do nothing
		} else {
			
			if (layers_list_active_array[x] == "Yes") {
				//still do nothing
			} else {
				dis_anim();
				layers_list_active_array[x] = "Yes";
				Layer_check(x);
				var y = 0;
				current_layers = "";
				for (y in layers_list_array) {
					if (layers_list_active_array[y] == "Yes") {
						current_layers += layers_list_array[y] + ",";
					}
				}
				getMap();
				break
			}
		}
	}
}
