
	var iEasting = 0;
	var iNorthing = 0;
	var sPostcode = "";
	
	function FindMyNearest() {
		
		// Load Find My Nearest Towns
		LoadTownNames()
		
		// Load Find My Nearest
		LoadFindMyNearestLayers();
		
	}
	
	// Load the Town Names from the Xml document
	function LoadTownNames() {

		var xmlDoc;
		
		xmlDoc = createDomObject("xml/FindMyNearst.xml");		
		if ( xmlDoc == null ) { return; }			
		
		parseTownNames(xmlDoc);
		
		xmlDoc = null;

	}

	// Parse the town names
	function parseTownNames(xmlDoc) {

		var details;
		var XMLList;
		var sTownHTML = "";

		rootNode = xmlDoc.documentElement;

		if ( rootNode.childNodes.length > 0 ) {

			sTownHTML = "<select size=\"1\" id=\"FMNTown\">";

			for ( x = 0; x < rootNode.childNodes.length; x++ ) {

				var XMLList = rootNode.childNodes.item(x);

				if ( XMLList.nodeName == 'Town' ) {

					details = XMLList.getAttribute("Easting") + "," + XMLList.getAttribute("Northing")

					sTownHTML += "<option value=\"" + details + "\">" + XMLList.getAttribute("Name") + "</option>";

				}

			}

			sTownHTML += "</select>";

			TownListTag.innerHTML = sTownHTML;

		}		

	}
	
	// Read the Find My Layers from PlanAccess
	function LoadFindMyNearestLayers() {

		var xmlDoc;
		
		var sURL = Domain + "/" + Virtual + "/dialogbox_FindMyNearestLayers.asp";
		
		xmlDoc = createDomObject(sURL);		
		if ( xmlDoc == null ) { return; }
		
		parseFindMyNearestLayers(xmlDoc);
		
		xmlDoc = null;

	}

	// Parse the find my nearest layers
	function parseFindMyNearestLayers(xmlDoc) {

		var sLayerHTML = "";
		var sLayer = "";
		var x = 0;
		var XMLList;

		sLayerHTML = "<select style=\"position: relative; width: 180\" size=\"1\" id=\"FMNLayer\">";

		rootNode = xmlDoc.documentElement;

		if ( rootNode.childNodes.length > 0 ) {

			for ( x = 0; x < rootNode.childNodes.length; x++ ) {

				var XMLList = rootNode.childNodes.item(x);

				if (BrowserName == "Netscape") {
					sLayer = XMLList.firstChild.nodeValue;
				} else {
					sLayer = XMLList.text;
				}

				sLayerHTML += "<option value=\"" + sLayer + "\">" + sLayer + "</option>";

			}

		}

		sLayerHTML += "</select>";

		TownListTag1.innerHTML = sLayerHTML;

	}
	
	// Main function which is called on mouse click event
	function RequestPostcodeCoordinates() {
		
		document.body.style.cursor = 'wait';
		toolmode = 0;

		// Is the postcode textbox blank
		if ( FMNPostcode.value == "" ) {

			var SingleArraySplit = FMNTown.value.split(",");

			iEasting = parseInt(SingleArraySplit[0]);
			iNorthing = parseInt(SingleArraySplit[1]);			
			
			CallFindMyNearest();

		} else { // Postcode Search

			// Is the postcode the same as the original search
			if ( sPostcode.toUpperCase() == FMNPostcode.value.toUpperCase() ) {

				CallFindMyNearest()

			} else {

				sPostcode = FMNPostcode.value.toUpperCase();
				
				var xmlDoc;
		
				var sURL = Domain  + "/" + Virtual;
				sURL += "/dialogbox_InfoPointViewer.asp?infomode=xml&theme=" + CodePointTheme + "&layername=" + CodePointLayer + "&fieldname=" + PostCodeField + "&fieldvalue=" + FMNPostcode.value.toUpperCase();
				
				xmlDoc = createDomObject(sURL);		
				if ( xmlDoc == null ) { return; }
		
				ExtractPostcodeCoordinates(xmlDoc);
				
				xmlDoc = null;
				
			}

		}
	}
	
	function ExtractPostcodeCoordinates(xmlDoc) {

		rootNode = xmlDoc.documentElement;

        if ( rootNode.childNodes.length > 0 ) {

			if ( rootNode.nodeName.toLowerCase() == "planaccesserror" ) {

				if (BrowserName == "Netscape") {

					queryTag.innerHTML = "Postcode not found";

                } else {

					queryTag.innerHTML = "Postcode not found";

				}

				document.body.style.cursor = 'default';

            } else {

				var EastingElement = rootNode.getElementsByTagName(EastingField)
				var NorthingElement = rootNode.getElementsByTagName(NorthingField)
				
				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;
					
					// Reduce to 6 chars
					if ( iEasting.length > 6 ) {
						iEasting = iEasting.substr(0,6);
					}
					
					if ( iNorthing.length > 6 ) {
						iNorthing = iNorthing.substr(0,6);
					}
					
				}

				// Make a find my nearest call
				CallFindMyNearest()

			}

		} else {

			document.body.style.cursor = 'default';

			queryTag.innerHTML = "Postcode not found";

		}

	}
	
	function CallFindMyNearest() {

		var xmlDoc;		
	
		var sURL = Domain  + "/" + Virtual;
		sURL += "/dialogbox_FindMyNearestResults.asp?fmnmode=xml&theme=" + CodePointTheme + "&FMNEasting=" + iEasting + "&FMNNorthing=" + iNorthing + "&FMNRadius=" + FMNRadius.value + "&FMNLayer="
		sURL += FMNLayer.value + "&SortFeatures=true";

		xmlDoc = createDomObject(sURL);		
		if ( xmlDoc == null ) { return; }

		ParseFindMyNearestResults(xmlDoc);
		
		xmlDoc = null;

	}
	
	function ParseFindMyNearestResults(xmlDoc) {

		var x = 0;
		var i = 0;
		var en = 0;
		var sHTML = "";
		var FindMyNearestEasting = 0;
		var FindMyNearestNorthing = 0;
		var FindMyNearestZoom = 0;

		rootNode = xmlDoc.documentElement;

		QueryTitle.innerHTML = "";

		if ( rootNode.childNodes.length > 0 ) {

			if ( rootNode.nodeName.toLowerCase() == "planaccesserror" ) {
				if (BrowserName == "Netscape") {
					sHTML = rootNode.childNodes.item(0).childNodes.item(1).firstChild.nodeValue;
				} else {
					sHTML = rootNode.childNodes.item(0).childNodes.item(1).text;
				}
			} else {

				var FindMyNearestElements = rootNode.childNodes.item(0)

				sHTML = "<table id=\"resultsTbl\" class=\"QueryTableFonts\">";

				for ( x = 0; x < FindMyNearestElements.childNodes.length; x++ ) {

					var FindMyNearestElement = FindMyNearestElements.childNodes.item(x);

					if ( x == 0 ) {

						sHTML += "<thead id=\"resultsHead\"><tr>";

						for ( en = 0; en < FindMyNearestElement.childNodes.length; en++ ) {

							if ( FindMyNearestElement.childNodes.item(en).nodeName== "Easting" ) {
								// Do nothing
							} else if ( FindMyNearestElement.childNodes.item(en).nodeName == "Northing" ) {
								// Do nothing
							} else if ( FindMyNearestElement.childNodes.item(en).nodeName == "Zoom" ) {
								// Do nothing
							} else {
								if ( FindMyNearestElement.childNodes.item(en).nodeName == "Distance" ) {
									sHTML += "<td class=\"QueryHeaderColumn\" bgcolor=\"#9EBFE0\">Distance (Metres)";
								} else {
									sHTML += "<td class=\"QueryHeaderColumn\" bgcolor=\"#9EBFE0\">" +  FindMyNearestElement.childNodes.item(en).nodeName + "</td>";
								}
							}

						}

						sHTML += "</tr></thead>";

					}

					for ( en = 0; en < FindMyNearestElement.childNodes.length; en++ ) {

						if ( FindMyNearestElement.childNodes.item(en).nodeName == "Easting" ) {

							if (BrowserName == "Netscape") {
								FindMyNearestEasting = FindMyNearestElement.childNodes.item(en).firstChild.nodeValue;
							} else {
								FindMyNearestEasting = FindMyNearestElement.childNodes.item(en).text;
							}

						} else if ( FindMyNearestElement.childNodes.item(en).nodeName == "Northing" ) {

							if (BrowserName == "Netscape") {
								FindMyNearestNorthing = FindMyNearestElement.childNodes.item(en).firstChild.nodeValue;
							} else {
								FindMyNearestNorthing = FindMyNearestElement.childNodes.item(en).text;
							}

						} else if ( FindMyNearestElement.childNodes.item(en).nodeName == "Zoom" ) {

							if (BrowserName == "Netscape") {
								FindMyNearestZoom = FindMyNearestElement.childNodes.item(en).firstChild.nodeValue;
							} else {
								FindMyNearestZoom = FindMyNearestElement.childNodes.item(en).text;
							}
						}

					}

					sHTML += "<tbody id=\"resultsBody\"><tr style='cursor:hand' onclick='MoveMap(" + FindMyNearestEasting + "," + FindMyNearestNorthing + "," + FindMyNearestZoom + ")'>";

					for ( i = 0; i < FindMyNearestElement.childNodes.length; i++ ) {

						if ( FindMyNearestElement.childNodes.item(i).nodeName == "Easting" ) {
							// Do nothing
						} else if ( FindMyNearestElement.childNodes.item(i).nodeName == "Northing" ) {
							// Do nothing
						} else if ( FindMyNearestElement.childNodes.item(i).nodeName == "Zoom" ) {
							// Do nothing
						} else {

							var LayerText ="";

							if (BrowserName == "Netscape") {
								LayerText = FindMyNearestElement.childNodes.item(i).firstChild.nodeValue;
							} else {
								LayerText = FindMyNearestElement.childNodes.item(i).text;
							}

							if ( LayerText.substring(0,5) == 'http:' ) {
								sHTML += "<td class=\"QueryValueColumn\" bgcolor=\"#9EBFE0\"><a href='" + LayerText + "' target='_blank'><img src='images/paweb.gif' title='" + LayerText + "'></a></td>";
							} else if ( LayerText.substring(0,6) == 'https:' ) {
								sHTML += "<td class=\"QueryValueColumn\" bgcolor=\"#9EBFE0\"><a href='" + LayerText + "' target='_blank'><img src='images/paweb.gif' title='" + LayerText + "'></a></td>";
							} else if ( LayerText.substring(0,7) == 'mailto:' ) {
								sHTML += "<td class=\"QueryValueColumn\" bgcolor=\"#9EBFE0\"><a href='" + LayerText + "' target='_blank'><img src='images/pamail.gif' title='" + LayerText + "'></a></td>";
							} else {
								sHTML += "<td class=\"QueryValueColumn\" bgcolor=\"#9EBFE0\">" + LayerText + "</td>";
							}
						}

					}
					sHTML += "</tr></tbody>";

				}

				sHTML += "</table>"


				QueryTitle.innerHTML = "<h3><font color=\"#336699\">Results of Find My Nearest &#8211; Click on result to Locate on the Map</font></h3>";

			}

			queryTag.innerHTML = sHTML;
			document.body.style.cursor = 'default';
		}

	}
