	/* 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 = "";	
		
		xmlDoc = createDomObject("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;
		
		xmlDoc = createDomObject("http://" + Domain + "/" + Virtual + "/dialogbox_LayerInformation.asp?theme=" + theme);
						
		createLayersTable(xmlDoc);
		
		xmlDoc = null;
		
	}
	
	// Populate the Layer control
	function createLayersTable(xmlDoc) {
	
		var sHTML = "";
		var x = 0;
		var bResult;

		rootNode = xmlDoc.documentElement;

		sHTML = "<table title=\"Layers\" id='LayerList' cellpadding='0' cellspacing='0' width='250'>";

		sHTML += "<tr>";
		sHTML += "<td width=\"30\"><b>&nbsp;</b></td>";
		sHTML += "<td width=\"190\"><font color='#336699'><b>Layer</b></font></td>";
		sHTML += "<td width=\"30\"><font color='#336699'><b>ON/OFF</b></font></td>";
		sHTML += "</tr>";

		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 += "<tr style=\"visibility:visible\">";
					sHTML += "<td width=\"30\"><img id='layer:" + LayerNode.getAttribute("Name") + "' src='images/cartoloading.gif'></td>";
					sHTML += "<td width=\"190\">" + LayerNode.getAttribute("FriendlyName") + "</td>";

					// Visibility
					sHTML += "<td width=\"30\">";
					
					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>";

				} else if ( LayerNode.getAttribute("CartoExcluded") == "True" ) {

					LayerList[LayerIndex] = LayerNode.getAttribute("Name");
					LayerStatus[LayerIndex] = "cartoExcluded";
					LayerIndex += 1;


				} else {
					
					LayerList[LayerIndex] = LayerNode.getAttribute("Name");
					LayerStatus[LayerIndex] = "hidden";
					LayerIndex += 1;

				}
		}

		sHTML += "<tr>";
		sHTML += "<td align=\"right\" width=\"250\" colspan=\"3\"><p><img src=\"images/buttons/update.gif\" name=68 onclick=\"UpdateLayerControl()\" alt=\"Update\" title=\"Update\"></p></td>";
		sHTML += "</tr>";

		sHTML += "</table>";

		document.getElementById("layers").innerHTML = sHTML;

	}
	
	// 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" || LayerStatus[x] == "cartoExcluded" ) {
				// 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;
		
		xmlDoc = createDomObject("http://" + Domain + "/" + Virtual + "/dialogbox_CartoLegend.asp?cartomode=xml&theme=" + theme);		
		if ( xmlDoc == null ) { return; }						
		
		createCartoTable(xmlDoc);
		
		xmlDoc = null;
		
	}
	
	// Add the carto legend to the screen ( IE Browsers )
	function createCartoTable(xmlDoc) {

		var sHTML = "";

		rootNode = xmlDoc.documentElement;

        if ( rootNode == null ) {
            return;
        }
        
		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 = 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" || LayerStatus[x] == "cartoExcluded" ) {
				sURL += LayerList[x] + ",";
			}

		}
		
		return sURL;
	}
	
	// Update the layers in the layer control with there carto image ( Mozilla )
	function FetchCartoLegendMoz() {
		
		var xmlDoc;
		
		xmlDoc = createDomObject("http://" + Domain + "/" + Virtual + "/dialogbox_CartoLegend.asp?cartomode=binaryxml&theme=" + theme);		
		if ( xmlDoc == null ) { return; }				

		createCartoTableMoz(xmlDoc);
		
		xmlDoc = null;
		
	}
	
	// Add the carto legend to the screen ( Mozilla )
	function createCartoTableMoz(xmlDoc) {
	
		var sHTML = "";

		rootNode = xmlDoc.documentElement;
		
		if ( rootNode == null ) {
		    return;
		}
		
		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;
	}
