/*-------------------------------------
planve.js
Lovell Johns Ltd.
April 2008
JavaScript file for MapServer - LDF
Gateshead Council
---------------------------------------*/

//----------------------------------------------------------GLOBALS
var http;
var movingPointer = false;
var lodPositions = new Array(109,96,84,70);						//Pixel positions in the slider of each LOD
//var lods = new Array(24000,12000,6000,3000,1500,750,375);  					//View widths of each LOD
var lods = new Array(24000,6000,1500,375);  					//View widths of each LOD
//var lods = new Array(24000,4800,960,192);  					//View widths of each LOD

var currentLOD = 0;
var nofLODs = 4;
var startX = 305;
var startY = 310;
var startMoveX;
var startMoveY; 
var mapURL = "http://192.168.1.169/gc/gc.php"; 						//URL of the maim map applicaton page (Internal)
//var mapURL = " http://81.171.185.241/ldf/lbc/ldf5.php"; 						//URL of the maim map applicaton page (External)
var writtenURL = "written/cpt";																							//For link to HTML
//var writtenURL = "http://consultdemo.inovem.com/inovem/consult.ti/lovelljohns.croydon/viewCompoundDoc?docid=5140&" ;		//For link to iNovem 
var controlUp = true;														//Stores if the ctrl is pressed down - allows turning collapsing/expanding all legend groups
var useMSLegImages = false;										//Defien if using MapServer created lagend images or precreated ones
									
//******************Store browser version
var is_nav6up = false;
var is_ie6up = false;
var is_opera5up = false;
checkBrowser();

//----------------------------------------------------------MAP NAVIGATION

//******************Initial map load
function mapLoad(theURLStringToPass){
	toggleProgressMessage("on");
	//Show content of default tabs on left
	if(document.mapform.apptype.value != "reduced"){
		toggleTabs("searching","1");
	}		
	if(theURLStringToPass == "none"){			//**Nothing passed open in default state
		document.mapform.mapaction.value = "initial" 			
		var qstring ="";						//Pass nothing (use default values in updatemap.php)
		sndReq(qstring);
	}else{										//**Location passed
		var qstring = theURLStringToPass; 		//Pass passedURL string of parameters to compose a sppecific map 	
		sndReq(qstring);			
	}
}

//******************Run navigation when clicked on overview map
function quickNav(event){
	toggleProgressMessage("on");
	//Get clicked coordinate in pixels
	if(is_nav6up){
		var theX = String(event.layerX);
		var theY = String(event.layerY);	
	}else{
		var theX = String(event.offsetX);
		var theY = String(event.offsetY);
	}

	var theMapLayersString = getMapLayers(); 			//Get list of all the layers
	var theMapLayerInfo = theMapLayersString.split("|");
	document.mapform.mapaction.value = "quicknav";
	var qstring = "mapaction=" + document.mapform.mapaction.value + "&ldfoverviewmapx=" + theX + "&ldfoverviewmapy=" + theY + "&extent=" + document.mapform.extent.value + "&mapLayers=" + theMapLayerInfo[0] + "&layerGroups=" + theMapLayerInfo[1] + "&layerGroups2=" + theMapLayerInfo[2];
	sndReq(qstring);
}

//******************Run navigation when user chooses quick map link
function quickNav2(selObj){
	var theCoordString = selObj.options[selObj.selectedIndex].value;
	if(theCoordString != "#"){
		toggleProgressMessage("on");
		//Move the zoom pointer
		currentLOD = 1;
		moveZoomPointer();	
		var theSep = theCoordString.indexOf(",");
		var theX = theCoordString.substring(0,theSep);
		var theY = theCoordString.substring((theSep+1),theCoordString.length);
		var theMapLayersString = getMapLayers(); 			//Get list of all the layers
		var theMapLayerInfo = theMapLayersString.split("|");
		document.mapform.mapaction.value = "quicknav2";
		var qstring = "mapaction=" + document.mapform.mapaction.value + "&xclick=" + theX + "&yclick=" + theY + "&extent=" + document.mapform.extent.value + "&mapLayers=" + theMapLayerInfo[0] + "&layerGroups=" + theMapLayerInfo[1] + "&layerGroups2=" + theMapLayerInfo[2];
		sndReq(qstring);
	}	
}

//******************Zoom map
function zoomMap(zoomType){

	if(zoomType == "out"){
		document.mapform.mapaction.value= "out";
		var newLOD = currentLOD -1;
	}else if(zoomType == "in"){
		document.mapform.mapaction.value = "in";
		var newLOD = currentLOD +1;
	}else if(zoomType == "full"){
		document.mapform.mapaction.value = "full";
		var newLOD = (nofLODs -1);
	} 
	
	if(newLOD == nofLODs){			//Can't zoom any further in
		var dontZoom = true;
	}else if(newLOD < 0){			//Can't zoom any further out
		var dontZoom = true;
	}else{
		var dontZoom = false;
		currentLOD = newLOD; 
	}	
	
	if(dontZoom == false){
		toggleProgressMessage("on");
		//Move the zoom pointer
		moveZoomPointer();
		var theMapLayersString = getMapLayers(); 			//Get list of all the layers
		var theMapLayerInfo = theMapLayersString.split("|");
		var qstring = "mapaction=" + document.mapform.mapaction.value + "&extent=" + document.mapform.extent.value + "&mapLayers=" + theMapLayerInfo[0] + "&layerGroups=" + theMapLayerInfo[1] + "&layerGroups2=" + theMapLayerInfo[2];
		sndReq(qstring);	
	}	
}

//******************Zoom to the specified map extent - called when the map zoomed by the slider
function zoomToExtent(){
	toggleProgressMessage("on");
	document.mapform.mapaction.value = "extent";
	var theMapLayersString = getMapLayers(); 			//Get list of all the layers
	var theMapLayerInfo = theMapLayersString.split("|");
	var qstring = "mapaction=" + document.mapform.mapaction.value + "&extent=" + document.mapform.extent.value + "&mapLayers=" + theMapLayerInfo[0] + "&layerGroups=" + theMapLayerInfo[1] + "&layerGroups2=" + theMapLayerInfo[2];
	sndReq(qstring);	
}

//******************Pan map
function panMap(panDirection){
	toggleProgressMessage("on");
	document.mapform.mapaction.value = "pan_" + panDirection;
	var theMapLayersString = getMapLayers(); 			//Get list of all the layers
	var theMapLayerInfo = theMapLayersString.split("|");
	var qstring = "mapaction=" + document.mapform.mapaction.value + "&extent=" + document.mapform.extent.value + "&mapLayers=" + theMapLayerInfo[0] + "&layerGroups=" + theMapLayerInfo[1] + "&layerGroups2=" + theMapLayerInfo[2];
	sndReq(qstring);
}

//******************Redraws the map - called when layer toggled
function redrawMap(selObj,layerType,groupIndex){

	toggleProgressMessage("on");

	//If user has clicked on a group layer - turn on or off the sub layers
	if(layerType == "parent"){
		if(selObj.checked){				//Turn all on		
			var noSubLayers = eval("document.mapform.mapLayers" + groupIndex + ".length");
			for(c=0;c<noSubLayers;c++){
				eval("document.mapform.mapLayers" + groupIndex + "[" + c + "].checked = true");
			}
		}else{							//Turn all off
			var noSubLayers = eval("document.mapform.mapLayers" + groupIndex + ".length");
			for(c=0;c<noSubLayers;c++){
				eval("document.mapform.mapLayers" + groupIndex + "[" + c + "].checked = false");
			}
		}
	}

	document.mapform.mapaction.value = "refresh";	
	var theMapLayersString = getMapLayers(); 			//Get list of all the layers
	var theMapLayerInfo = theMapLayersString.split("|");
	var qstring = "mapaction=" + document.mapform.mapaction.value + "&extent=" + document.mapform.extent.value + "&mapLayers=" + theMapLayerInfo[0] + "&layerGroups=" + theMapLayerInfo[1] + "&layerGroups2=" + theMapLayerInfo[2];
	sndReq(qstring);
}

//******************Pans map to location of Postcode or street search query
function zoomToPoint(passedX,passedY){
	toggleProgressMessage("on");
	//Move the zoom pointer
	currentLOD = 3;
	moveZoomPointer();	
	document.mapform.mapaction.value = "zoom";	
	document.mapform.xclick.value = passedX;
	document.mapform.yclick.value = passedY;
	var theMapLayersString = getMapLayers(); 			//Get list of all the layers
	var theMapLayerInfo = theMapLayersString.split("|");
	var qstring = "mapaction=" + document.mapform.mapaction.value + "&extent=" + document.mapform.extent.value + "&xclick=" + document.mapform.xclick.value + "&yclick=" + document.mapform.yclick.value + "&mapLayers=" + theMapLayerInfo[0] + "&layerGroups=" + theMapLayerInfo[1] + "&layerGroups2=" + theMapLayerInfo[2];
	sndReq(qstring);
}

//----------------------------------------------------------GETTING POLICY INFORMATION SCRIPTS
//******************Get the policy where the user clicked (Policies at a point)
function getPolicy(event){
	if(document.mapform.activetool.value == "info"){
		if(currentLOD != 0){								//Don't try and retrieve if viewing graphic overview map			
			//Show progress animation while retrieving
			toggleProgressMessage("on");
			document.mapform.activetool.value = "";
			
			//Get clicked coordinate in pixels
			if(is_nav6up){
				var theX = String(event.layerX);
				var theY = String(event.layerY);		
			}else{
				var theX = String(event.offsetX);
				var theY = String(event.offsetY);
			}	
			var theMapLayersString = getMapLayers(); 			//Get list of all the layers
			var theMapLayerInfo = theMapLayersString.split("|");
			document.mapform.mapaction.value = "query";
			var qstring = "mapaction=" + document.mapform.mapaction.value + "&ldfmapx=" + theX + "&ldfmapy=" + theY + "&extent=" + document.mapform.extent.value + "&mapLayers=" + theMapLayerInfo[0] + "&layerGroups=" + theMapLayerInfo[1] + "&layerGroups2=" + theMapLayerInfo[2];	
			sndReq(qstring);
		}
	}
}

//******************Get the policies that intersect with the current view
function getPolicy2(){
	if(currentLOD != 0){								//Don't try and retrieve if viewing graphic overview map
		//Show progress animation while retrieving
		toggleProgressMessage("on");			
		document.mapform.mapaction.value = "queryview";
		var theMapLayersString = getMapLayers(); 			//Get list of all the layers
		var theMapLayerInfo = theMapLayersString.split("|");
		var qstring = "mapaction=" + document.mapform.mapaction.value + "&extent=" + document.mapform.extent.value + "&mapLayers=" + theMapLayerInfo[0] + "&layerGroups=" + theMapLayerInfo[1] + "&layerGroups2=" + theMapLayerInfo[2];
		sndReq(qstring);
	}
}

//******************Get the policies that fall at the passed point (Used to find policies that occur at a search point eg. address or postcode)
function getPolicy3(){
	if(currentLOD != 0){								//Don't try and retriev if viewing graphic overview map
		//Show progress animation while retrieving
		toggleProgressMessage("on");
		
		//Query point will be the centre of the view				
		var tempExtentString = new Array();
		tempExtentString = document.mapform.extent.value.split(" "); 
		var theX = Number(tempExtentString[0]) +  ((Number(tempExtentString[2]) - Number(tempExtentString[0]))/2);
		var theY = Number(tempExtentString[1]) +  ((Number(tempExtentString[3]) - Number(tempExtentString[1]))/2);		
		var theMapLayersString = getMapLayers(); 			//Get list of all the layers
		var theMapLayerInfo = theMapLayersString.split("|");	
		document.mapform.mapaction.value = "querypoint";
		var qstring = "mapaction=" + document.mapform.mapaction.value + "&ldfmapx=" + theX + "&ldfmapy=" + theY + "&extent=" + document.mapform.extent.value + "&mapLayers=" + theMapLayerInfo[0] + "&layerGroups=" + theMapLayerInfo[1] + "&layerGroups2=" + theMapLayerInfo[2];
		sndReq(qstring);
	}
}

//******************Tool selection
function selectTool(theTool){
	if(document.mapform.activetool.value == theTool){				//Turn of current tool
		//var layer = eval('document.all.mapmovecanvas.style');
		//layer.visibility = "hidden";
		document.mapform.activetool.value = "";
	}else{															//Turn on current tool
		if(currentLOD != 0){										//Cannot use tools when view overview map graphic
			document.mapform.activetool.value = theTool;
			
			/*if(theTool == "info"){
				//Make map move canvas hidden
				var layer = eval('document.all.mapmovecanvas.style');
				layer.visibility = "hidden";
				//Make drawing canvas hidden
				var layer = eval('document.all.drawingcanvas.style');
				layer.visibility = "hidden";
				var layer = eval('document.all.drawingcanvas2.style');
				layer.visibility = "hidden";	
			}else if(theTool == "pan"){
				//Make map move canvas visible
				var layer = eval('document.all.mapmovecanvas.style');
				layer.visibility = "visible";
				//Make drawing canvas hidden
				var layer = eval('document.all.drawingcanvas.style');
				layer.visibility = "hidden";
				var layer = eval('document.all.drawingcanvas2.style');
				layer.visibility = "hidden";
			
			}else if(theTool == "poly"){
				shapeType = "poly";
				//Make drawing canvas visible
				var layer = eval('document.all.drawingcanvas.style');
				layer.visibility = "visible";
				var layer = eval('document.all.drawingcanvas2.style');
				layer.visibility = "visible";
				//Make map move canvas hidden
				var layer = eval('document.all.mapmovecanvas.style');
				layer.visibility = "hidden";
			}else if(theTool == "line"){
				shapeType = "line";
				//Make drawing canvas visible
				var layer = eval('document.all.drawingcanvas.style');
				layer.visibility = "visible";
				var layer = eval('document.all.drawingcanvas2.style');
				layer.visibility = "visible";
				//Make map move canvas hidden
				var layer = eval('document.all.mapmovecanvas.style');
				layer.visibility = "hidden";
			}*/
		}
	}
}

//----------------------------------------------------------LOCATION QUERY SCRIPTS
//******************Runs query to return the postCode or Street search results
function doMapSearch(){
	var searchString = document.mapform.searchinput.value;			//**Get entered search string.
	if(searchString != ""){
		toggleProgressMessage("on");	
		//Get type of search (Only Postcode at the moment)
		//if(document.mapform.searchtype[0].checked){									
			var theType = "pcode";		
		//}else if(document.mapform.searchtype[1].checked){				
		//	var theType = "street";
		//}		
		document.mapform.mapaction.value = "search";
		var theMapLayersString = getMapLayers(); 			//Get list of all the layers
		var theMapLayerInfo = theMapLayersString.split("|");	
		var qstring = "mapaction=" + document.mapform.mapaction.value + "&extent=" + document.mapform.extent.value + "&searchtype=" + theType + "&searchinput=" + searchString + "&mapLayers=" + theMapLayerInfo[0] + "&layerGroups=" + theMapLayerInfo[1] + "&layerGroups2=" + theMapLayerInfo[2];
		sndReq(qstring);		
	}else{
		alert("Ensure search string entered."); 
	}				
}

//----------------------------------------------------------GENERAL SCRIPTS
//******************Hides/displays the legend group elements
function toggleLegendGroup(theGroup) {
	if(controlUp == false){										//**Expand or collapse all
		//Define if to expand or collapse all
		var clickedIsOn = false;
		if (is_nav6up) {										//Navigator6/Firefox
			var theObj = document.getElementById(theGroup);
			if(theObj.className == 'collapsibleon'){		
				clickedIsOn = true;
			}										
		}else{													//Opera/IE
			var toEval = theGroup + ".className";		
			if(eval(toEval) == 'collapsibleon'){				
				clickedIsOn = true;			
			}
		}

		//Loop thru each layer and expand or collapse all
		for(c1=0;c1<document.mapform.mapGroupLayers.length;c1++){
			theGroup = "group" + String(c1+1);	
			var boxbtn = document.getElementById(theGroup + "img");		
			if (is_nav6up) {										//Navigator6/Firefox
				var theObj = document.getElementById(theGroup);				
				if(clickedIsOn){      						//Collapse all
					theObj.className = 'collapsibleoff';
					boxbtn.src = "images/expand.gif";
				}else{										//Expand all
					theObj.className = 'collapsibleon';
					boxbtn.src = "images/collapse.gif";
				}						
			}else{													//Opera/IE
				var toEval = theGroup + ".className";
				if(clickedIsOn){      						//Collapse all
					var toEval2 = theGroup + ".className='collapsibleoff'";
					boxbtn.src = "images/expand.gif";
				}else{
					var toEval2 = theGroup + ".className='collapsibleon'";
					boxbtn.src = "images/collapse.gif";
				}
				eval(toEval2);
			}
		}
	}else{													//**Expand or collapse selected	
		var boxbtn = document.getElementById(theGroup + "img");		
		if (is_nav6up) {										//Navigator6/Firefox
			var theObj = document.getElementById(theGroup);
			if(theObj.className == 'collapsibleon'){			//Collapse
				theObj.className = 'collapsibleoff';
				boxbtn.src = "images/expand.gif";
			}else{												//Expand
				theObj.className = 'collapsibleon';
				boxbtn.src = "images/collapse.gif";
			}										
		}else{													//Opera/IE
			var toEval = theGroup + ".className";		
			if(eval(toEval) == 'collapsibleon'){				//Collapse
				var toEval2 = theGroup + ".className='collapsibleoff'";
				boxbtn.src = "images/expand.gif";			
			}else{												//Expand
				var toEval2 = theGroup + ".className='collapsibleon'";
				boxbtn.src = "images/collapse.gif";
			}
			eval(toEval2);
		}
	}
	
}

//******************Hides/displays the relavant tabs
function toggleTabs(passedTab,theSet) {
	if(theSet == "1"){
		var allTabs = new Array("overviewmap","searching","maplayerslayer","policyresultslayer");
	}else if(theSet == "1a"){
		var allTabs = new Array("overviewmap","searching","maplayerslayer","admin");
	}else{
		var allTabs = new Array("maplayerslayer","legend");
	}
	
	var tabsToHide = new Array();
	toHideCnt = 0;	
	for (c=0;c< allTabs.length;c++){
		if(allTabs[c] != passedTab){			
			tabsToHide[toHideCnt] = allTabs[c];
			toHideCnt += 1;
		}
	}	

	//SHOW
	//Define the tab to show
	var tabToShow = document.getElementById(passedTab);
	tabToShow.style.display = "block";
	//Define the image to show
	var onImage = document.getElementById(passedTab + "_img");
	onImage.src = "images/" + passedTab + "_on.jpg";
	
	//HIDE
	if(passedTab != "searchresultslayer"){
		for (c=0;c< tabsToHide.length;c++){
			var tabToHide = document.getElementById(tabsToHide[c]);
			if(tabToHide != null){
				tabToHide.style.display = "none";
			}
			var offImage = document.getElementById(tabsToHide[c] + "_img");
			if(offImage != null){
				offImage.src = "images/" + tabsToHide[c] + "_off.jpg";
			}
		}
	}
}

//******************Hides/displays the relavant policy results pages
function showResultPage(toShow,toHide){		
	if (is_nav6up) {										//Navigator6/Firefox
		var theObjToShow = document.getElementById("page_" + toShow);						
		theObjToShow.className = 'policypageon';
		var theObjToHide= document.getElementById("page_" + toHide);						
		theObjToHide.className = 'policypageoff';											
	}else{													//Opera/IE
		var toEval = "page_" + toShow + ".className='policypageon'";
		eval(toEval);		
		var toEval = "page_" + toHide + ".className='policypageoff'";
		eval(toEval);	
	}
}

//******************Hides/displays the "Map retrieving" message
function toggleProgressMessage(theAction) {
	var theObj = document.getElementById("progresslayer");
	if(theAction == "on"){				//Turn on
		theObj.style.display = "block";
	}else{								//Turn off
		theObj.style.display = "none";
	}
}

//******************Hides/displays the "Policy serach results" in layer over the map
function showPolicyResults(theAction) {
	var theObj = document.getElementById("policyresultslayer2");
	if(theAction == "on"){				//Turn on
		theObj.style.display = "block";	
	}else{								//Turn off
		theObj.style.display = "none";
	}
}

//****************Run mapserver mapscript functions to return mapping, query results etc....
function sndReq(qstring) {
	http = createRequestObject();
	http.open('get', 'updatemap.php?' + qstring); 
	http.onreadystatechange = updatemapResponse;
	http.send(null);
}

//****************Perform action based on returned values
function updatemapResponse() {
	if(http.readyState == 4){	
		toggleProgressMessage("off");
		//Get the returned values
		var response = http.responseText;
		//alert(response)		
		var tempArray = new Array();
		tempArray = response.split("|"); 				
		if(tempArray[0] == "mapsearch"){
			//Show the results
			document.getElementById("searchresultslayer").innerHTML = tempArray[1];	
			//Show the relevant tab
			toggleTabs("searching","1");
		}else if(tempArray[0] == "mapquery"){
			//Show the results			
			//var closeHTML = "<div id='windowhead' class='windowhead'><p align='right'><img src='images/close.jpg' height='15' width='15' alt='Click to close' onclick='showPolicyResults(\"off\");'/></p></div>";
			//document.getElementById("policyresultslayer2").innerHTML = closeHTML + tempArray[1];
			document.getElementById("policyresultslayer").innerHTML = tempArray[1];
			//showPolicyResults("on");
			toggleTabs("policyresultslayer","1");
			
			
		}else if(tempArray[0] == "mapnav"){
			//Set map extent, scalebar url, legend url, main map image url, overview image url with passed values
			document.mapform.extent.value = tempArray[1];									//**Store current view extent on hidden
			document.mapform.scalebarimage.src = tempArray[2];								//**Update new scalebar image
			var theMainMapObj = document.getElementById("ldfmap");							//**Update MAIN MAP image
			theMainMapObj.src =  tempArray[4];
			if(document.mapform.apptype.value != "reduced"){								//For the full application
			
				var theOVMapObj = document.getElementById("ldfoverviewmap");				//**Update new overview image
				theOVMapObj.src =  tempArray[5];
				document.getElementById("maplayerslayer").innerHTML = tempArray[6];			//**Update layer list
				
				//Get the individual legend images and add to the layer list to create combine layer/legand list
				addLegImages(tempArray[8]);
							
				if(tempArray[7] != "0"){
					//Need to update the LOD - it has been changed as the map has zoomed to a postcode location
					currentLOD = 3;
					moveZoomPointer();
				}				
			}	
			
			//Define and store the LOD
			var tempExtentString = new Array();
			tempExtentString = document.mapform.extent.value.split(" "); 
			var currentViewWidth = tempExtentString[2] - tempExtentString[0];
			for(c=0;c<lods.length;c++){
				if(currentViewWidth >= lods[c]){
					currentLOD = c;
					break;
				}
			}	
			//Move the zoom pointer
			moveZoomPointer();
			
			//Always ensure layer list legend visiible after requesting new map view
			//if(document.mapform.apptype.value != "reduced"){
			//	toggleTabs("maplayerslayer","1");
			//}								
		}		
	}
}

//****************Add individual legend images into the layer list
function addLegImages(theHTMLLegString){
	//Get the individual legend images and add to the layer list to create combine layer/legand list
	//HTML legend elements are of the form -
	//<p><img id="[metadata name=LAYER_SOURCE]" src="[leg_icon width=20 height=13]" width=20 height=13>&nbsp; [metadata name=LAYER_ALIAS]</p>
	var tempString = theHTMLLegString;
	var sep1 = 0
	var legcnt = 0;
	while(sep1 != -1){
		legcnt += 1;
		sep1 = tempString.indexOf("id=");
		if(sep1 == -1){					//No more legend items - exit loop
			break;
		}					
		if(legcnt == 500){				//Catch to ensure we don't loop infinately
			break;
		}
		var sep2 = tempString.indexOf("src=");
		var sep3 = tempString.indexOf("width=");
		var theID = tempString.substring((sep1+4),(sep2-2));
		theLegImageName = tempString.substring((sep2+5),(sep3-2));
		
		if(useMSLegImages == false){				//Use pre-created legend images (MapServer images rescale and don't give good indication of policy)
			theLegImageName = "images/legend/" + theID + ".gif";
		}
		
		var theLegImageObj = document.getElementById("leg_" + theID);				//**Update new overview image
		if(theLegImageObj != null){
			theLegImageObj.src = theLegImageName;
		}
		var sep4 = tempString.indexOf("</p>");
		var tempString1 = tempString.substring((sep4+4),tempString.length);
		tempString = tempString1;
	}
}

//****************Create request object
function createRequestObject() {
	var ro;		
	if (window.XMLHttpRequest) { 				// Mozilla, Safari,...
		ro = new XMLHttpRequest();
	}else if (window.ActiveXObject) { 			// IE
		try {
			ro = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			ro = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	return ro;	
}

//******************Get visible map layers
function getMapLayers(){

	if(document.mapform.apptype.value == "reduced"){					//***Using stripped application all layers turned on by default
		return "all|";
	}else{																//***Using full application define the layers that are visible
		var theVisibleLayers = "";
		var visLayCnt = 0;
		var theExpandedGroups = "";
		var expandedGroupsCnt = 0;
		var	theSelectedGroups = "";
		var selectedGroupsCnt = 0;
		
		for(c1=0;c1<document.mapform.mapGroupLayers.length;c1++){
			//Define what groups are expanded		
			if (is_nav6up) {										//Navigator6/Firefox
				var theObj = document.getElementById("group" + String(c1+1));
				if(theObj.className == 'collapsibleon'){
					expandedGroupsCnt += 1;
					if(expandedGroupsCnt == 1){
						theExpandedGroups += String(c1+1);
					}else{
						theExpandedGroups += "," + String(c1+1);
					}	 
				}										
			}else{													//Opera/IE
				var toEval = "group" + String(c1+1) + ".className";		
				if(eval(toEval) == 'collapsibleon'){
					expandedGroupsCnt += 1;
					if(expandedGroupsCnt == 1){
						theExpandedGroups += String(c1+1);
					}else{
						theExpandedGroups += "," + String(c1+1);
					}
				}
			}
			
			if(document.mapform.mapGroupLayers[c1].checked){								//Group Layer is turned on - check which sub layers are turned on
					
				selectedGroupsCnt += 1;
				if(selectedGroupsCnt == 1){
					theSelectedGroups += String(c1+1);
				}else{
					theSelectedGroups += "," + String(c1+1);
				}
					
				var noSubLayers = eval("document.mapform.mapLayers" + String(c1+1) + ".length");
				if(isNaN(noSubLayers)){
					var noSubLayers = 1;
				}		
				if(noSubLayers == 1){
					if(eval("document.mapform.mapLayers" + String(c1+1) + ".checked")){
						visLayCnt += 1;
						if(visLayCnt == 1){
							theVisibleLayers = eval("document.mapform.mapLayers" + String(c1+1) + ".value"); 
						}else{
							theVisibleLayers += " " + eval("document.mapform.mapLayers" + String(c1+1) + ".value"); 
						}	
					}
				}else{
					for(c2=0;c2<noSubLayers;c2++){
						if(eval("document.mapform.mapLayers" + String(c1+1) + "[" + c2 + "].checked")){
							visLayCnt += 1;
							if(visLayCnt == 1){
								theVisibleLayers = eval("document.mapform.mapLayers" + String(c1+1) + "[" + c2 + "].value");  //document.mapform.mapLayers[c].value;
							}else{
								theVisibleLayers += " " + eval("document.mapform.mapLayers" + String(c1+1) + "[" + c2 + "].value");  //document.mapform.mapLayers[c].value;
							}		
						}
					}
				}
					
			}
		}
		return theVisibleLayers + "|" + theExpandedGroups + "|" + theSelectedGroups;
	}
}

//******************Toggles the visibility of all layers
function toggleLayers(theAction){

	if(theAction == "on"){ 				//Turn all on
		for(c1=0;c1<document.mapform.mapGroupLayers.length;c1++){
			document.mapform.mapGroupLayers[c1].checked = true;
			var noSubLayers = eval("document.mapform.mapLayers" + String(c1+1) + ".length");
			if(isNaN(noSubLayers)){
				var noSubLayers = 1;
			}
			if(noSubLayers == 1){
				eval("document.mapform.mapLayers" + String(c1+1) + ".checked = true");
			}else{
				for(c2=0;c2<noSubLayers;c2++){
					eval("document.mapform.mapLayers" + String(c1+1) + "[" + c2 + "].checked = true");			
				}
			}
		}
	}else{								//Turn all off
		for(c1=0;c1<document.mapform.mapGroupLayers.length;c1++){
			document.mapform.mapGroupLayers[c1].checked = false;
			var noSubLayers = eval("document.mapform.mapLayers" + String(c1+1) + ".length");
			if(isNaN(noSubLayers)){
				var noSubLayers = 1;
			}
			if(noSubLayers == 1){
				eval("document.mapform.mapLayers" + String(c1+1) + ".checked = false");
			}else{
				for(c2=0;c2<noSubLayers;c2++){
					eval("document.mapform.mapLayers" + String(c1+1) + "[" + c2 + "].checked = false");			
				}
			}
		}
	}
	
	//Redraw the map
	toggleProgressMessage("on");
	document.mapform.mapaction.value = "refresh";	
	var theMapLayersString = getMapLayers(); 			//Get list of all the layers
	var theMapLayerInfo = theMapLayersString.split("|");	
	var qstring = "mapaction=" + document.mapform.mapaction.value + "&extent=" + document.mapform.extent.value +  "&mapLayers=" + theMapLayerInfo[0] + "&layerGroups=" + theMapLayerInfo[1] + "&layerGroups2=" + theMapLayerInfo[2];
	sndReq(qstring);

}

//******************Stores if the ctrl is pressed down - allows turning collapsing/expanding all legend groups
function keyPressDown(evt){
	if(evt.shiftKey){
		controlUp = false;
	}
}
function keyReleased(evt){
	controlUp = true;
}

//----------------------------------------------------------ZOOM POINTER SCRIPTS
//******************Start dragging zoom pointer
function dragZoomStart(){
	movingPointer = true;	
	//Make the moving canvas visible
	if (is_nav6up) {										//Navigator6/Firefox
		var layer = document.getElementById("movingcanvas");
		layer.style.visibility = "visible";												
	}else{													//Opera/IE
		var layer = eval('document.all.movingcanvas.style');
		layer.visibility = "visible";
	}		
}

//******************Moving zoom pointer
function dragZoomMove(event){

	var theX2 = event.offsetX;
	var theY2 = event.offsetY; 		
	if(movingPointer){	
		theX = 14 ;  
		theY = 45 + theY2;   
		if(theY <70){
			theY=70;
		}
		if(theY >146){
			theY=146;
		}

		if (is_nav6up) {										//Navigator6/Firefox
			var layer = document.getElementById("zoompointer");
			layer.visibility = "visible";
			//Set the new position
			layer.style.left = String(Number(theX)-0) + "px";  
			layer.style.top  =  String(Number(theY)-0) + "px";  												
		}else{													//Opera/IE
			var layer = eval('document.all.zoompointer.style');
			layer.visibility = "visible";
			//Set the new position
			layer.left = String(Number(theX)-0) + "px";  
			layer.top  =  String(Number(theY)-0) + "px";  
		}	

		//var layer = eval('document.all.zoompointer.style');	 
	
	
	}
}

//******************End dragging zoom pointer
function dragZoomEnd(){
	if(movingPointer){		
		//Hide the moving canvas		
		if (is_nav6up) {										//Navigator6/Firefox
			var layer = document.getElementById("movingcanvas");												
			layer.style.visibility = "hidden";
		}else{													//Opera/IE
			var layer = eval('document.all.movingcanvas.style');
			layer.visibility = "hidden";
		}
		
		
		movingPointer = false;

		//Get the layer positiom
		if (is_nav6up) {										//Navigator6/Firefox
			var layer = document.getElementById("zoompointer");
			var layerPosY = layer.style.top;												
		}else{													//Opera/IE
			var layer = eval('document.all.zoompointer.style');
			var layerPosY = layer.top;
		}
		
		layerPosY = Number(layerPosY.substring(0, (layerPosY.length-2)));
			
		var theNewViewWidth = 0;
		var theNewPointerPos = 0;
		for(c=0;c<lodPositions.length;c++){
			if(layerPosY >= lodPositions[c]){
				theNewViewWidth = lods[c];
				theNewPointerPos = lodPositions[c];
				currentLOD = c;
				break;
			}
		}
		
		if(theNewViewWidth != 0){	

			//Get the layer positiom
			if (is_nav6up) {										//Navigator6/Firefox
				//Adjust to snap to a pre-defined layer
				layer.style.top  = theNewPointerPos + "px"; 												
			}else{													//Opera/IE
				//Adjust to snap to a pre-defined layer
				layer.top  = theNewPointerPos + "px"; 
			}
				
			//Set the new extent string
			var currentMapExtent = document.mapform.extent.value;
			var currentMapExtentArray = new Array();
			currentMapExtentArray = currentMapExtent.split(" ");
			var currentViewWidth = Number(currentMapExtentArray[2]) - Number(currentMapExtentArray[0]);
			var currentCentreX = Number(currentMapExtentArray[0]) + (currentViewWidth /2);
			var currentCentreY = Number(currentMapExtentArray[1]) + (currentViewWidth /2);
			var newMapExtent = String(currentCentreX - (theNewViewWidth/2)) + " " + String(currentCentreY - (theNewViewWidth/2)) + " " + String(currentCentreX + (theNewViewWidth/2)) + " " + String(currentCentreY + (theNewViewWidth/2));
			document.mapform.extent.value = newMapExtent;
			zoomToExtent();
		}
	}
}

//******************Move the zoom pointer
function moveZoomPointer(){	
	if (is_nav6up) {										//Navigator6/Firefox
		var layer = document.getElementById("zoompointer");
		layer.style.top  = lodPositions[currentLOD] + "px"; 												
	}else{													//Opera/IE
		var layer = eval('document.all.zoompointer.style');
		layer.top  = lodPositions[currentLOD] + "px"; 
	}
}

//******************Opens policy in new widow
function openPolicyWin(passedID){
	if(passedID != ""){
		//Open in new window;
		var theURL = writtenURL + "partid=" + passedID; 	
		var winName = "policyinfo";
		var features = "toolbar=yes,scrollbars=yes,resizable=yes,width=800,height=700";
		var theWin = window.open(theURL,winName,features);		
		theWin.focus();									//Ensure map window is visible on top
	}
}

//----------------------------------------------------------Written Statement SCRIPTS
//******************************Opens a driagram window from the written statement
function openDiagram(theMapRef){
	//Open in new window;
	theURL = "diag.htm?" + theMapRef;	
	winName = "Diagram";
	features = "toolbar=no,scrollbars=yes,resizable=yes,width=600,height=600";
	window.open(theURL,winName,features);		
}

//----------------------------------------------------------ADMIN PUBLISHING SCRIPTS

//******************Retrieves the URL for the currnt map viewing
function getMapURL(){
	//Get list of all the layers that are visible
	var theMapLayersString = getMapLayers(); 			
	var theMapLayerInfo = theMapLayersString.split("|");	
	var tempExtentString = document.mapform.extent.value;   //Store extent as comma seperated string
	var theExtentString = tempExtentString.replace(/ /g, ",");
	document.mapform.linkurl.value = mapURL + "?mapaction=extent&extent=" + theExtentString + "&mapLayers=" + theMapLayerInfo[0];
}

//-------------------------------End of  File