	/* Populate the Layer Control with the layers from the current Theme */
	
	var LayerList = new Array();
	var LayerStatus = new Array();
	var HiddenLayers = new Array();
	
	// Constructor ( Not actual a construtor but a starting place )
	function LayerControl() {
	
		// Load the hidden layers
		ReadHiddenLayers()

		// Generate Layer Control
		FetchLayerList();

		// Generate Carto Images
		if (BrowserName == "Microsoft Internet Explorer") {
			FetchCartoLegendIE();
		} else {
			FetchCartoLegendMoz();
		}
	}
	
	// Read in all the non business layers which need to be excluded from the Layer Control
	function ReadHiddenLayers() {
	
		var xmlDoc;
		var x = 0;
		var sText = "";
		
		if (document.implementation && document.implementation.createDocument) {
			xmlDoc = document.implementation.createDocument("", "", null);
		}
		else if (window.ActiveXObject) {
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		}

		xmlDoc.async = false; // Tell the Xml document to wait until the document read
		xmlDoc.load( "xml/LayerControl.xml" );
		
		// Fetch the root of the document
		rootNode = xmlDoc.documentElement;

		for ( x = 0; x < rootNode.childNodes.length; x++ ) {

			if ( rootNode.childNodes.item(x).nodeName != "#text" ) {

				var HiddenLayer = rootNode.childNodes.item(x);

				// Add the image to the layer name
				sText = HiddenLayer.getAttribute("Name");
				HiddenLayers[x] = sText;
			}
		}

		xmlDoc = null;
		
	}
	
	// Check the current layer is not in the excluded list
	function CheckCurrentLayer( sLayer ) {

		var x = 0;

		for ( x = 0; x < HiddenLayers.length; x++ ) {

			if ( sLayer == HiddenLayers[x] ) {
				return false;
			}

		}

		return true;

	}
	
	// Fetch the list of layers for the geoset
	function FetchLayerList() {
	
		var xmlDoc;
		
		if (document.implementation && document.implementation.createDocument) {
			xmlDoc = document.implementation.createDocument("", "", null);
		}
		else if (window.ActiveXObject) {
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		}

		xmlDoc.async = false; // Tell the Xml document to wait until the document has been complete read
		xmlDoc.load( "http://" + Domain + "/" + Virtual + "/dialogbox_LayerInformation.asp?theme=" + theme );
		
		createLayersTable(xmlDoc);
		
		xmlDoc = null;
		
	}
	
	// Populate the Layer control
	function createLayersTable(xmlDoc) {
	
		var sHTML = "";
		var nHTML = "";
		var x = 0;
		var bResult;	

		rootNode = xmlDoc.documentElement;
		nHTML = "<table title=\"Layers\" id='LayerList' class='keytable' cellpadding='0' cellspacing='0' font-size:'80%' width='264'>"

		nHTML += "<tr><td>";				
		nHTML += "<div class=\"headitem\">Planning Documents</div>";
		nHTML += "</tr></td>";	

		nHTML += "<tr><td>";		
		var LayerNode = rootNode.childNodes.item(0);  //says there is a layer present and chooses which layer
		nHTML += "<tr><td><div class=\"topitem\"><img id='layer:" + LayerNode.getAttribute("Name") + "' src='../images/cartoloading.gif'>";
		if ( ( LayerNode.getAttribute("Visibility") == "True" ) )
		{
			nHTML += "<span class=\"keyitemmover\">Borough Local Plan (1999)</span>";
			nHTML += " <input type=\"checkbox\" checked id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></div>";
			LayerStatus[LayerIndex] = "true";
		} 
		else {
			nHTML += "<span class=\"keyitemmover\">Borough Local Plan (1999)</span>";
			nHTML += " <input type=\"checkbox\" id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></span>";
			LayerStatus[LayerIndex] = "false";
		}
    		LayerList[LayerIndex] = LayerNode.getAttribute("Name");
    		LayerIndex += 1;				
		nHTML += "</tr></td>";		
		
		nHTML += "<tr><td>";

		var LayerNode = rootNode.childNodes.item(1);  //says there is a layer present and chooses which layer
		nHTML += "<tr><td><div class=\"topitem\"><img id='layer:" + LayerNode.getAttribute("Name") + "' src='../images/cartoloading.gif'>";
		if ( ( LayerNode.getAttribute("Visibility") == "True" ) )
		{
			nHTML += "<span class=\"keyitemmovernotlive\">Core Strategy - Submission</span>";
			nHTML += " <input type=\"checkbox\" checked id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></div>";
			LayerStatus[LayerIndex] = "true";
		} 
		else {
			nHTML += "<span class=\"keyitemmovernotlive\">Core Strategy - Submission</span>";
			nHTML += " <input type=\"checkbox\" id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></span>";
			LayerStatus[LayerIndex] = "false";
		}
    		LayerList[LayerIndex] = LayerNode.getAttribute("Name");
    		LayerIndex += 1;				
		nHTML += "</tr></td>";		
		nHTML += "<tr><td>";		

		var LayerNode = rootNode.childNodes.item(2);  //says there is a layer present and chooses which layer
		nHTML += "<tr><td><div class=\"topitem\"><img id='layer:" + LayerNode.getAttribute("Name") + "' src='../images/cartoloading.gif'>";
		if ( ( LayerNode.getAttribute("Visibility") == "True" ) )
		{
			nHTML += "<span class=\"keyitemmovernotlive\">Core Strategy - Adoption</span>";
			nHTML += " <input type=\"checkbox\" checked id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></div>";
			LayerStatus[LayerIndex] = "true";
		} 
		else {
			nHTML += "<span class=\"keyitemmovernotlive\">Core Strategy - Adoption</span>";
			nHTML += " <input type=\"checkbox\" id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></span>";
			LayerStatus[LayerIndex] = "false";
		}
    		LayerList[LayerIndex] = LayerNode.getAttribute("Name");
    		LayerIndex += 1;				
		nHTML += "</tr></td>";
		var LayerNode = rootNode.childNodes.item(3);  //says there is a layer present and chooses which layer
		nHTML += "<tr><td><div class=\"topitem\"><img id='layer:" + LayerNode.getAttribute("Name") + "' src='../images/cartoloading.gif'>";
		if ( ( LayerNode.getAttribute("Visibility") == "True" ) )
		{
			nHTML += "<span class=\"keyitemmover\">Housing Allocation - Preferred</span>";
			nHTML += " <input type=\"checkbox\" checked id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></div>";
			LayerStatus[LayerIndex] = "true";
		} 
		else {
			nHTML += "<span class=\"keyitemmover\">Housing Allocation - Preferred</span>";
			nHTML += " <input type=\"checkbox\" id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></span>";
			LayerStatus[LayerIndex] = "false";
		}
    		LayerList[LayerIndex] = LayerNode.getAttribute("Name");
    		LayerIndex += 1;				
		nHTML += "</tr></td>";		
		var LayerNode = rootNode.childNodes.item(4);  //says there is a layer present and chooses which layer
		//nHTML += "<tr><td><div class=\"topitem\"><img id='layer:" + LayerNode.getAttribute("Name") + "' src='../images/cartoloading.gif'>";  // This line greyed out (18/03/2011) to stop showing on the menu, as the title of this document is not likely to be used
		
		if ( ( LayerNode.getAttribute("Visibility") == "True" ) )
		{
			//nHTML += "<span class=\"keyitemmovernotlive\">Housing Allocation - Submission</span>";
			//nHTML += " <input type=\"checkbox\" checked id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></div>";// This line greyed out (18/03/2011) to stop showing on the menu, as the title of this document is not likely to be used
			LayerStatus[LayerIndex] = "true";
		} 
		else {
			// nHTML += "<span class=\"keyitemmovernotlive\">Housing Allocation - Submission</span>";
			//nHTML += " <input type=\"checkbox\" id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></span>";// This line greyed out (18/03/2011) to stop showing on the menu, as the title of this document is not likely to be used
			LayerStatus[LayerIndex] = "false";
		}
    		LayerList[LayerIndex] = LayerNode.getAttribute("Name");
    		LayerIndex += 1;				
		nHTML += "</tr></td>";	
		var LayerNode = rootNode.childNodes.item(5);  //says there is a layer present and chooses which layer
		// nHTML += "<tr><td><div class=\"topitem\"><img id='layer:" + LayerNode.getAttribute("Name") + "' src='../images/cartoloading.gif'>"; // This line greyed out (18/03/2011) to stop showing on the menu, as the title of this document is not likely to be used
		if ( ( LayerNode.getAttribute("Visibility") == "True" ) )
		{
			// nHTML += "<span class=\"keyitemmovernotlive\">Housing Allocation - Adoption</span>";
			// nHTML += " <input type=\"checkbox\" checked id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></div>"; // This line greyed out (18/03/2011) to stop showing on the menu, as the title of this document is not likely to be used
			LayerStatus[LayerIndex] = "true";
		} 
		else {
			// nHTML += "<span class=\"keyitemmovernotlive\">Housing Allocation - Adoption</span>";
			// nHTML += " <input type=\"checkbox\" id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></span>"; // This line greyed out (18/03/2011) to stop showing on the menu, as the title of this document is not likely to be used
			LayerStatus[LayerIndex] = "false";
		}
    		LayerList[LayerIndex] = LayerNode.getAttribute("Name");
    		LayerIndex += 1;				
		nHTML += "</tr></td>";		
		var LayerNode = rootNode.childNodes.item(6);  //says there is a layer present and chooses which layer
		nHTML += "<tr><td><div class=\"topitem\"><img id='layer:" + LayerNode.getAttribute("Name") + "' src='../images/cartoloading.gif'>";
		if ( ( LayerNode.getAttribute("Visibility") == "True" ) )
		{
			nHTML += "<span class=\"keyitemmovernotlive\">CEE DPD - Options</span>";
			nHTML += " <input type=\"checkbox\" checked id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></div>";
			LayerStatus[LayerIndex] = "true";
		} 
		else {
			nHTML += "<span class=\"keyitemmovernotlive\">CEE DPD - Options</span>";
			nHTML += " <input type=\"checkbox\" id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></span>";
			LayerStatus[LayerIndex] = "false";
		}
    		LayerList[LayerIndex] = LayerNode.getAttribute("Name");
    		LayerIndex += 1;				
		nHTML += "</tr></td>";		
		var LayerNode = rootNode.childNodes.item(7);  //says there is a layer present and chooses which layer
		nHTML += "<tr><td><div class=\"topitem\"><img id='layer:" + LayerNode.getAttribute("Name") + "' src='../images/cartoloading.gif'>";
		if ( ( LayerNode.getAttribute("Visibility") == "True" ) )
		{
			nHTML += "<span class=\"keyitemmovernotlive\">CEE DPD - Submission</span>";
			nHTML += " <input type=\"checkbox\" checked id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></div>";
			LayerStatus[LayerIndex] = "true";
		} 
		else {
			nHTML += "<span class=\"keyitemmovernotlive\">CEE DPD - Submission</span>";
			nHTML += " <input type=\"checkbox\" id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></span>";
			LayerStatus[LayerIndex] = "false";
		}
    		LayerList[LayerIndex] = LayerNode.getAttribute("Name");
    		LayerIndex += 1;				
		nHTML += "</tr></td>";
		var LayerNode = rootNode.childNodes.item(8);  //says there is a layer present and chooses which layer
		nHTML += "<tr><td><div class=\"topitem\"><img id='layer:" + LayerNode.getAttribute("Name") + "' src='../images/cartoloading.gif'>";
		if ( ( LayerNode.getAttribute("Visibility") == "True" ) )
		{
			nHTML += "<span class=\"keyitemmovernotlive\">CEE DPD - Adoption</span>";
			nHTML += " <input type=\"checkbox\" checked id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></div>";
			LayerStatus[LayerIndex] = "true";
		} 
		else {
			nHTML += "<span class=\"keyitemmovernotlive\">CEE DPD - Adoption</span>";
			nHTML += " <input type=\"checkbox\" id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></span>";
			LayerStatus[LayerIndex] = "false";
		}
    		LayerList[LayerIndex] = LayerNode.getAttribute("Name");
    		LayerIndex += 1;				
		nHTML += "</tr></td>";
		var LayerNode = rootNode.childNodes.item(9);  //says there is a layer present and chooses which layer
		nHTML += "<tr><td><div class=\"topitem\"><img id='layer:" + LayerNode.getAttribute("Name") + "' src='../images/cartoloading.gif'>";
		if ( ( LayerNode.getAttribute("Visibility") == "True" ) )
		{
			nHTML += "<span class=\"keyitemmover\">Other Relevant Designations</span>";
			nHTML += " <input type=\"checkbox\" checked id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></div>";
			LayerStatus[LayerIndex] = "true";
		} 
		else {
			nHTML += "<span class=\"keyitemmover\">Other Relevant Designations</span>";
			nHTML += " <input type=\"checkbox\" id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\" onclick=\"UpdateLayerControl()\"></span>";
			LayerStatus[LayerIndex] = "false";
		}
    		LayerList[LayerIndex] = LayerNode.getAttribute("Name");
    		LayerIndex += 1;				
		nHTML += "<br /></tr></td>";

//	    nHTML += "<tr><td><div class=\"updatebutton\"><img src=\"images/buttons/update_up.gif\" onclick=\"UpdateLayerControl()\" alt=\"Update\" name=\"16\" onMouseOver=\"OverImage(this)\" onMouseOut=\"OutImage(this)\" title=\"Update\"></div>";
//	    nHTML += "</td></tr>";
		nHTML += "<td><tr><div class=\"smallfontctr\">greyed out documents below currently have no information to display on the map.</td></tr></table>";
		nHTML += "<br /><br />";

		layers.innerHTML = nHTML;		

//		sHTML = "<table title=\"Layers\" id='LayerList' cellpadding='0' cellspacing='0' border='solid 2px #000' font-size:'80%' width='230'>";
//		sHTML += "<tr>";
//		sHTML += "<td width=\"30\"><b> </b></td>";
//		sHTML += "<td width=\"180\"><b>Displayed Items</b></font></td>";
//		sHTML += "<td width=\"20\"><b>Visible</b></font></td>";
//		sHTML += "</tr>";	


	// This code below runs through all the layers and displays them - used when not manually intercepting!	
		for ( x = 0; x < rootNode.childNodes.length; x++ )
		{
			var LayerNode = rootNode.childNodes.item(x);
				bResult = CheckCurrentLayer(LayerNode.getAttribute("Name")) ;

				if (  bResult == true && LayerNode.getAttribute("CartoExcluded") == "False" )
				{
					sHTML += "<div class=\"hidden\">";
					sHTML += "<tr>";					
					sHTML += "<td width=\"30\"><img id='layer:" + LayerNode.getAttribute("Name") + "' src='images/cartoloading.gif'></td>";
					sHTML += "<td width=\"180\">" + LayerNode.getAttribute("Name") + "</td>";		
					
					// Visibility
					sHTML += "<td width=\"20\">";
					if ( ( LayerNode.getAttribute("Visibility") == "True" ) )
					{
						sHTML += "<input type=\"checkbox\" checked id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\">";
						LayerStatus[LayerIndex] = "true"; 
					} 
					else {
						sHTML += "<input type=\"checkbox\" id=\"" + LayerNode.getAttribute("Name") + "\" value=\"ON\">";
						LayerStatus[LayerIndex] = "false";
    				}	
						// Add Layer to Layers Array
					LayerList[LayerIndex] = LayerNode.getAttribute("Name");
					LayerIndex += 1;
					sHTML += "</td>";
					sHTML += "</tr>";
					sHTML += "</div>";
				} else {
					LayerList[LayerIndex] = LayerNode.getAttribute("Name");
					LayerStatus[LayerIndex] = "hidden";
					LayerIndex += 1;
		        }
	}
	 layers.innerHTML += sHTML;	
	
//		sHTML += "<tr>";
////		sHTML += "<td align=\"right\" width=\"230\" colspan=\"3\"><p><img src=../"images/buttons/update_up.gif/" onclick=\"UpdateLayerControl()\" alt=\"Update\" name=\"16\" onMouseOver=\"OverImage(this)\" onMouseOut=\"OutImage(this)\" title=\"Update\"></p></td>";
//		sHTML += "</tr>";
//
////		sHTML += "</table>";

}
	
	// Enables the user to enable or disable layers in the layer control
	function UpdateLayerControl() {

		var x = 0;
		var SelectObjects = document.getElementsByTagName("input")
		var SelectObject;

		var LayerSwitch;

		for ( x = 0; x < LayerStatus.length; x++ ) {

			if ( LayerStatus[x] == "hidden" ) {
				// Do Nothing
			} else {

				var SelectObject = document.getElementById(LayerList[x]);

				if ( SelectObject.checked == true ) { LayerSwitch = "true"; }
				else if ( SelectObject.checked == false ) { LayerSwitch = "false"; }

				LayerStatus[x] = LayerSwitch;
			}

		}

		UpdateMap()

	}
	
	// Update the layers in the layer control with there carto image ( IE Browsers )
	function FetchCartoLegendIE() {
	
		var xmlDoc;
		
		if (document.implementation && document.implementation.createDocument) {
			xmlDoc = document.implementation.createDocument("", "", null);
		}
		else if (window.ActiveXObject) {
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		}

		xmlDoc.async = false; // Tell the Xml document to wait until the document has been complete read
		xmlDoc.load( "http://" + Domain + "/" + Virtual + "/dialogbox_CartoLegend.asp?cartomode=xml&theme=" + theme );
		
		createCartoTable(xmlDoc);
		
		xmlDoc = null;
		
	}
	
	// Add the carto legend to the screen ( IE Browsers )
	function createCartoTable(xmlDoc) {

		var sHTML = "";

		rootNode = xmlDoc.documentElement;

		for ( x = 0; x < rootNode.childNodes.length; x++ ) {

			var CartoNode = rootNode.childNodes.item(x);

			// Add the image to the layer name
			var elem = document.getElementById("layer:" + CartoNode.getAttribute("Name"))

			elem.src = CartoNode.getAttribute("Image");
			elem.alt = CartoNode.getAttribute("ImageType");
			elem.title = CartoNode.getAttribute("ImageType");
		}

	}
	
	// Return the layers which are visible or are hidden to the layer control
	function ReturnVisibleLayers() {
		
		var sURL = "";
		
		// Add the layers from the layer control which have been set to show
		for ( x = 0; x < LayerIndex; x++ ) {

			if ( LayerStatus[x] == "true" || LayerStatus[x] == "hidden" ) {
				sURL += LayerList[x] + ",";
			}

		}
		
		return sURL;
	}
	
	// Update the layers in the layer control with there carto image ( Mozilla )
	function FetchCartoLegendMoz() {
		
		var xmlDoc;
		
		if (document.implementation && document.implementation.createDocument) {
			xmlDoc = document.implementation.createDocument("", "", null);
		}
		else if (window.ActiveXObject) {
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		}

		xmlDoc.async = false; // Tell the Xml document to wait until the document has been complete read
		xmlDoc.load( "http://" + Domain + "/" + Virtual + "/dialogbox_CartoLegend.asp?cartomode=binaryxml&theme=" + theme );
		
		createCartoTableMoz(xmlDoc);
		
		xmlDoc = null;
		
	}
	
	// Add the carto legend to the screen ( Mozilla )
	function createCartoTableMoz(xmlDoc) {
	
		var sHTML = "";

		rootNode = xmlDoc.documentElement;
		
		for ( x = 0; x < rootNode.childNodes.length; x++ ) {

			var CartoNode = rootNode.childNodes.item(x);

			// Add the image to the layer name
			var elem = document.getElementById("layer:" + CartoNode.getAttribute("Name"))
				
			if ( elem != null )  {
				elem.src = "data:image/gif;base64," + CartoNode.firstChild.nodeValue;
				elem.alt = CartoNode.getAttribute("ImageType");
				elem.title = CartoNode.getAttribute("ImageType");
			}
		}
		
	}
	
	// Return layers which are not visible
	function ReturnExcludedLayers() {
		
		var sURL = "";
		
		// Add the layers from the layer control which have been set to show
		for ( x = 0; x < LayerIndex; x++ ) {

			if ( LayerStatus[x] == "false" ) {
				sURL += LayerList[x] + ",";
			}

		}
		
		return sURL;
	}
