//*=========================================================================
//Description:
//   Javascript include file that stores other important map and application 
//		functions
//
//   Dependent References/Classes:
//		 active.js
//       config.js
//		
//
//   Source:
//       Aralola Akinmade - Woolpert, August 2003
//
//   Version History:
//       Version 1.0 Aralola Akinmade, Woolpert, August 2003
//=========================================================================	
	//===================================================
	// Set the layer selected to activate it	
	//===================================================
	function setActiveLayer(layerID,str)
	{
		var allLayers;
		allLayers = document.getElementsByTagName("tr");
						
		//Reset all the background colors
		for (var i=0;i<allLayers.length;i++)
		{
			if (allLayers[i].id){
				if (allLayers[i].id.indexOf("layer") > -1) allLayers[i].style.background = "transparent";
			}	
		}		
		//Set the active layer
		if (document.all)
		{
			document.all(layerID).style.background = layerSelectedColor;
			document.all("hiddenActiveLayer").value = layerID.substr(5,layerID.length-5);
		}else{
			document.getElementById(layerID).style.background = layerSelectedColor;			
			document.getElementById("hiddenActiveLayer").value = layerID.substr(5,layerID.length-5);	
		}
		activeLayer = layerID.substr(5,layerID.length-5);
		activeLayerName = str;
		
		//Display active items
		displayActiveItems(state, activeLayerName);
	}
		
	//===================================================
	// Disable the Zoom Next and Zoom Last buttons if
	// necessary
	//===================================================
	
	function disableZoomLastNext(zoomLast, zoomNext)
	{
		 //alert('Zoom Last ' + zoomLast);
		 //alert('Zoom Next ' + zoomNext);	
		 
		 //Set the active layer
		if (document.all)
		{
			//Zoom Last
			if (zoomLast.toUpperCase() == "TRUE") 
			{
				document.all("ZOOMLAST").src = "images/goback_0.gif";
				document.all("ZOOMLAST").onclick = "";
				document.all("ZOOMLAST").style.background = "transparent";
			} else {			
				document.all("ZOOMLAST").src = "images/goback_1.gif";
				document.all("ZOOMLAST").onlick = "javascript:setState('ZOOMLAST');setToolImage('ZOOMLAST');";			
			}
			//Zoom Next
			if (zoomNext.toUpperCase() == "TRUE") 
			{
				document.all("ZOOMNEXT").src = "images/moveforward_0.gif";
				document.all("ZOOMNEXT").onclick = "";
				document.all("ZOOMNEXT").style.background = "transparent";
			} else {			
				document.all("ZOOMNEXT").src = "images/moveforward_1.gif";
				document.all("ZOOMNEXT").onlick = "javascript:setState('ZOOMNEXT');setToolImage('ZOOMNEXT');";			
			}
			
		}else{
		
			//Zoom Last
			if (zoomLast.toUpperCase() == "TRUE") 
			{
				document.getElementById("ZOOMLAST").src = "images/goback_0.gif";
				document.getElementById("ZOOMLAST").onclick = "";
				document.getElementById("ZOOMLAST").style.background = "transparent";
			} else {			
				document.getElementById("ZOOMLAST").src = "images/goback_1.gif";
				document.getElementById("ZOOMLAST").onlick = "javascript:setState('ZOOMLAST');setToolImage('ZOOMLAST');";			
			}
			//Zoom Next
			if (zoomNext.toUpperCase() == "TRUE") 
			{
				document.getElementById("ZOOMNEXT").src = "images/moveforward_0.gif";
				document.getElementById("ZOOMNEXT").onclick = "";
				document.getElementById("ZOOMNEXT").style.background = "transparent";
			} else {			
				document.getElementById("ZOOMNEXT").src = "images/moveforward_1.gif";
				document.getElementById("ZOOMNEXT").onlick = "javascript:setState('ZOOMNEXT');setToolImage('ZOOMNEXT');";			
			}
		}
	}
	
	//===================================================
	// Highlight the layer on mouseover	and mouseout
	//===================================================
	function highlightLayer_Over(layerID)
	{
		var str;
		if (document.all)
		{
			str = "layer" + document.all("hiddenActiveLayer").value;
			if (layerID != str) document.all(layerID).style.background = layerHighlightColor;
		}else{
			str = "layer" + document.getElementById("hiddenActiveLayer").value;
			if (layerID != str) document.getElementById(layerID).style.background = layerHighlightColor;			
		}
	}
	function highlightLayer_Out(layerID)
	{
		var str;
		if (document.all)
		{
			str = "layer" + document.all("hiddenActiveLayer").value;
			if (layerID != str) document.all(layerID).style.background = "transparent";
		}else{
			str = "layer" + document.getElementById("hiddenActiveLayer").value;
			if (layerID != str) document.getElementById(layerID).style.background = "transparent";			
		}
	}			
	//===================================================
	// Set Layer Visibility	
	//===================================================
	function setVisibility()
	{
		//show the loading map graphic
		showLayer("pnlLoadingMap");
  
		document.forms[0].submit();
	}

	//===================================================
	// Set Active Tool Image
	//===================================================
	function setToolImage(theTool) 
	{
	
		// Dull all images first.
		var allImages = document.getElementsByTagName("img");
	
		for (var i=0;i<allImages.length;i++)
		{
			allImages[i].style.border = 0;	
			allImages[i].style.background = "transparent";	
		}
	
		if (document.all)
		{
			document.all(theTool).style.border = toolBorderWidth;
			document.all(theTool).style.background = toolSelectedColor;
		}else{
			document.getElementById(theTool).style.border = toolBorderWidth;
			document.getElementById(theTool).style.background = toolSelectedColor;
		}
		//Display active items
		displayActiveItems(theTool,activeLayerName);	
		if (measuring) measuring = false;
	}
	//===================================================
	// Reload the Application
	//===================================================
	function reloadApp()
	{
		  //show the loading map graphic
		showLayer("pnlLoadingMap");
		document.forms[0].submit();
	}
	
	//===================================================
	// Display Active Items
	//===================================================
	function displayActiveItems(theTool, activeLayerName)
	{
		var str = "<b>Active Tool</b>: ";
		
		if (document.all)
		{			
			str += document.all(theTool).alt + " <br>  <b>Active Layer</b>: " + activeLayerName;
			document.all("pnlActiveItems").innerHTML = str;
		}else{
		
			str += document.getElementById(theTool).alt + " <br>  <b>Active Layer</b>: " + activeLayerName;
			document.getElementById("pnlActiveItems").innerHTML = str;			
		}
		//Check the state as well
		if (theTool == "FIND") displayFindForm();	
		if (theTool == "PRINT") displayPrintForm();	
		if (theTool == "GEOCODE") displayGeocodeForm();
		if (theTool == "MEASURE") displayMeasureForm(0,0);
				
	}
	//===================================================
	// Display the Results Window
	//===================================================
	function displayResultsWindow()
	{  
	 if (document.all) {
		var resultsWin = null;
		if (resultsWin && (!resultsWin.closed)) { resultsWin.close(); }
		resultsWin = window.open('results.aspx','Results','toolbar=no,menubar=yes,scrollbars=yes,location=no,height=375,width=600,resizable=yes,statusbar=yes');
		resultsWin.location.reload(true);
     } else {
		var resultsWin = window.open('results.aspx','Results','toolbar=no,menubar=yes,scrollbars=yes,location=no,height=375,width=600,resizable=yes,statusbar=yes');
	 }	
	 resultsWin.focus();
   }                 
	//===================================================
	// Display the Search/Find Form
	//===================================================
	function displayFindForm()
	{
	
		var str = "<p align='left'><b>Find Feature Tool</b></p>";
		
		if (document.all)
		{			
			str += "<p align='left'>Type in the value to find in " + activeLayerName + " <input class='LIGHTBOX' type=text name='searchValue' value='' size=25 maxlength=255><input class='DARKBOX' type=button name='btnFind' value='FIND' onclick='refreshMap()'></p>";
			document.all("pnlFunctions").innerHTML = str;
		}else{
			str += "<p align='left'>Type in the name to find in " + activeLayerName + " <input class='LIGHTBOX' type=text name='searchValue' value='' size=25 maxlength=255><input class='DARKBOX' type=button name='btnFind' value='FIND' onclick='refreshMap()'></p>";
			document.getElementById("pnlFunctions").innerHTML = str;	
		}
	}
	//===================================================
	// Display the Geocode Form
	//===================================================
	function displayGeocodeForm()
	{
		var searchWin = window.open("search.aspx","SearchForm","width=450,height=300,toolbar=no,menubar=no,scrollbars=yes,resizable=yes");
	}
	//===================================================
	// Display the Measure Form
	//===================================================
	function displayMeasureForm(curDist, totDist)
	{
		var str = "<p align='left'><b>Find Distance Tool</b> - The measurements below are in: <b>" + ScaleBarUnits + "</b></p>";		
		str += "<p align='left'><span style='width:350'><b>Current Distance:</b> " + parseFloat(curDist) + "  <b>Total Distance:</b> " + parseFloat(totDist) + "</span>&nbsp;&nbsp;<input class='DARKBOX' type=button name='btnClear' value='Clear Lines' onclick='clearVectors()'></p>";
			
		if (document.all)
		{
			 document.all("pnlFunctions").innerHTML = str;
		  	 if (document.all("lblMessages")) document.all("lblMessages").innerHTML = "";		
		} else {	
			 document.getElementById("pnlFunctions").innerHTML = str;	
			 if (document.getElementById("lblMessages")) document.getElementById("lblMessages").innerHTML = "";
		}
		
	}
	//===================================================
	// Display the Feedback Form
	//===================================================
	function displayFeedbackForm()
	{  
		if (document.all) {
			var feedbackWin = null;
			if (feedbackWin && (!feedbackWin.closed)) { feedbackWin.close(); }
			 feedbackWin = window.open('feedback.aspx','Feedback','toolbar=no,menubar=yes,scrollbars=yes,location=no,height=400,width=600,resizable=yes,statusbar=yes');
			 feedbackWin.location.reload(true);		
		} else {
			var feedbackWin = window.open('feedback.aspx','Feedback','toolbar=no,menubar=yes,scrollbars=yes,location=no,height=400,width=600,resizable=yes,statusbar=yes');		
		}	  
		feedbackWin.focus();   	
    }     
    //===================================================
	// Display the Help Page
	//===================================================
	function displayHelp()
	{
		helpURL = "help.aspx";
		if (document.all) {  
			var helpWin = null;			
			if (helpWin && (!helpWin.closed)) { helpWin.close(); }
			helpWin = window.open(helpURL,'Help','toolbar=no,menubar=yes,scrollbars=yes,location=no,height=500,width=600,resizable=yes,statusbar=yes');
			helpWin.location.reload(true);  	
		} else {
			var helpWin = window.open(helpURL,'Help','toolbar=no,menubar=yes,scrollbars=yes,location=no,height=500,width=600,resizable=yes,statusbar=yes');
		} 
		helpWin.focus(); 
    }     
    //===================================================
	// Display the Print Form
	//===================================================
	function displayPrintForm()
	{
		if (document.all) {
			var printWin = null;
			if (printWin && (!printWin.closed)) { printWin.close(); }
			printWin = window.open('print.aspx','print','toolbar=no,menubar=yes,scrollbars=yes,location=no,height=475,width=575,resizable=yes,statusbar=yes');
			printWin.location.reload(true);
		} else {
			var printWin = window.open('print.aspx','print','toolbar=no,menubar=yes,scrollbars=yes,location=no,height=475,width=575,resizable=yes,statusbar=yes'); 
		}	
			printWin.focus();  
     		
	}
	
	//===================================================
	// Clear the Functions Panel
	//===================================================
	function clearFunctionsPanel()
	{
		document.getElementById("pnlFunctions").innerHTML = "";
		if (document.getElementById("lblMessages")) document.getElementById("lblMessages").innerHTML = ""
	}
	//===================================================
	// Zoom to a feature selected from results
	//===================================================
	function zoomFeature(lyr, indx)
	{
		if (opener) {
			opener.top.activeLayer = lyr;
			opener.top.state = "ZOOMFEATURE_" + indx + "";
			opener.top.refreshMap();			
		} else {	
			activeLayer = lyr;
			state = "ZOOMFEATURE_" + indx + "";
			refreshMap();
		}	
	}
	//===================================================
	// Zoom to an address selected from results
	//===================================================
	function zoomAddress(lyr, indx)
	{
		if (opener) {
			opener.top.activeLayer = lyr;
			opener.top.state = "ZOOMADDRESS_" + indx + "";
			opener.top.refreshMap();
		}else {
			activeLayer = lyr;
			state = "ZOOMADDRESS_" + indx + "";
			refreshMap();
		}
	}
	//===================================================
	// Zoom to all features in the results
	//===================================================
		function zoomAllFeatures(lyr)
	{
		if (opener) {
			opener.top.activeLayer = lyr;
			opener.top.state = "ZOOMALL";
			opener.top.refreshMap();		
		} else {
			activeLayer = lyr;
			state = "ZOOMALL";
			refreshMap();
		}
	}
	//===================================================
	// Pan Directions
	//===================================================
	function panDirection(dPan)
	{
		state = dPan;
		refreshMap();
	}
	//===================================================
	// From the search forms
	//===================================================
	function performSearch(indx)
	{
	var str = indx.toLowerCase();
	
	if (opener) {
		
		opener.top.state = "GEOCODE_" + indx.toUpperCase();
		//Pass the input values to the parent window
		passInputsToParent(str);	
		opener.top.refreshMap();
		//self.close();
	} else {
		//If the page is an intro to the viewer
		state = "GEOCODE_" + indx.toUpperCase();
		document.forms[0].action = "viewer.aspx";	
		document.forms[0].submit();
	}	
	}
	//=========================================================
	// Pass values from search form window to parent window
	//=========================================================	
	function passInputsToParent(searchType)
	{
		for (var i=0;i<document.forms[0].elements.length;i++)
		{
			if (document.forms[0].elements[i].id.indexOf(searchType) != -1)
			{
				var input = opener.top.document.createElement('INPUT');
				var elemID = document.forms[0].elements[i].id;
				var indx = elemID.lastIndexOf("_");
				if (indx != -1)	{ elemID = elemID.substring(indx+1); }

				if (document.all)
				{
					input.type = 'hidden';
					input.name = elemID;
					input.value = document.forms[0].all(document.forms[0].elements[i].id).value
				}else{
					input.setAttribute('type', 'hidden');
					input.setAttribute('name', elemID);
					input.setAttribute('value', document.getElementById(document.forms[0].elements[i].id).value);
				}
					opener.top.document.forms[0].appendChild(input);						
			}	
		}
	}
	//=========================================================
	// Pass values from search form window to parent window
	//=========================================================	
	function passPrintSettingsToParent()
	{
	
		if (opener)
		{
		for (var i=0;i<document.forms[0].elements.length;i++)
		{
				var input = opener.top.document.createElement('INPUT');
				var elemID = document.forms[0].elements[i].id;
				//var indx = elemID.lastIndexOf("_");
				//if (indx != -1)	{ elemID = elemID.substring(indx+1); }

				if (document.all)
				{
					input.type = 'hidden';
					input.name = elemID;
					input.value = document.forms[0].all(document.forms[0].elements[i].id).value
				}else{
					input.setAttribute('type', 'hidden');
					input.setAttribute('name', elemID);
					input.setAttribute('value', document.forms[0].getElementById(document.forms[0].elements[i].id).value);
				}
					opener.top.document.forms[0].appendChild(input);						
			}	
					//Pass the input values to the parent window
					opener.top.refreshMap();
					self.close();
			
		} else {
				//If the page is not a pop up window
				document.forms[0].action = "viewer.aspx";	
		}	
	}

	
//===============================================
// Display/Open Panels
//===============================================
function displayPanel(thePanel, action, theBtn)
{
	// Declare variables
	var imgSwap = new Array();
	var isVisible = false;
	imgSwap[0] = new Image;
	imgSwap[0].src = "images/hide.gif";
	imgSwap[1] = new Image;
	imgSwap[1].src = "images/show.gif";	
			
	if (navigator.appName == "Netscape")
	{		
							
		if ((document.getElementById(theBtn).src.indexOf("show") != -1 ) || (action == "show"))
		{ 
			 document.getElementById(theBtn).src =imgSwap[0].src; 
			 action="block"; 
			 isVisible = true;
		}else if ((document.getElementById(theBtn).src.indexOf("hide") != -1 ) || (action == "hide"))
		{
			 document.getElementById(theBtn).src =imgSwap[1].src; 
			 action="none"; 
			 isVisible = false;
		}else{
			 document.getElementById(theBtn).src =imgSwap[1].src; 
			 action="none"; 
			 isVisible = false;
		}	
		document.getElementById(thePanel).style.display = action;
		document.getElementById("hiddenShowResults").value = isVisible;
						
	} else {
		if ((document.all(theBtn).src.indexOf("show.gif") != -1 ) || (action == "show"))
		{ 
			document.all(theBtn).src =imgSwap[0].src; 
			action="block"; 
			isVisible = true;
		} else if ((document.all(theBtn).src.indexOf("hide.gif") != -1 ) || (action == "hide"))
		{ 
			document.all(theBtn).src =imgSwap[1].src; 
			action="none"; 
			isVisible = false;
		} else {
			document.all(theBtn).src =imgSwap[1].src; 
			action="none"; 
			isVisible = false;
		}
		document.all(thePanel).style.display = action;	
		document.all("hiddenShowResults").value = isVisible;		
	}			
}

//===================================================
// Auto completion of drop down lists
//==================================================
var hideit = "";
//===================================================
// Java Script to Handle AutoSearch
//===================================================
function selectKeyDown()
{
    // Delete Key resets previous search keys
    if (window.event.keyCode == 46) clr();
}
function selectKeyPress()
{
    // Notes:
    //    1) previous keys are cleared onBlur/onFocus and with Delete key
    //    2) if the search doesn't find a match, this returns to normal 1 key 
    //        search setting returnValue = false below for ALL cases will 
    //        prevent default behavior
    
    //TODO:
    //    1) add Netscape handling
    
	  
   var sndr = window.event.srcElement;
   var key = window.event.keyCode;
   var chr = String.fromCharCode(key);

   var pre = hideit;

    // "i" -> ignoreCase
    var re = new RegExp("^" + pre + chr, "i"); 

    for(var i=0; i<sndr.options.length; i++)
    {
        if(re.test(sndr.options[i].text))
        {
            sndr.options[i].selected=true;
            hideit += chr;
            window.event.returnValue = false;
            break;
        }
    }
    
   
   

}
//===================================================
// Clear the auto-complete variable
//====================================================
function clr()
{
    hideit = "";
}

