
//Creates listeners for checkboxes and p tags

var root, length, num;

var allPageTags=document.getElementsByTagName("*");

for (c=0; c<allPageTags.length; c++) {
	if (allPageTags[c].className=="leftNavSub") {

		listener.addEvent(allPageTags[c], "dblclick", function(e){
			cs();		
		}, true);

	}
}

for (i=0;i<leftNav[LEFT_NAV_INFO].length ;i++ )
{
	root = leftNav[LEFT_NAV_INFO][i][0];
	length = leftNav[LEFT_NAV_INFO][i][1];
	num = leftNav[LEFT_NAV_INFO][i][2];

	var chkAllBoxID = "chkAll" + root.substring(3);
	var lnkAllTxtID = "lnkAll" + root.substring(3);

	var chkAllBox = document.getElementById(chkAllBoxID);
	var lnkAllTxt = document.getElementById(lnkAllTxtID);

	listener.addEvent(chkAllBox, "click", function(e){
		updateNavLeft(this.id, false);		
	}, true);
	
	listener.addEvent(lnkAllTxt, "click", function(e){
		updateNavLeft("chk" + this.id.substring(3), true);		
	}, true);

	var chkBoxID, lnkTxtID, chkBox, lnkTxt;
	
	for (j=0; j<length; j++ )
	{			
		chkBoxID = root + j + "";
		lnkTxtID = "lnk" + root.substring(3) + j + "";

		chkBox = document.getElementById(chkBoxID);
		lnkTxt = document.getElementById(lnkTxtID);

		listener.addEvent(chkBox, "click", function(e){
			updateNavLeftSub(this.id, false);		
		}, true);
		
		listener.addEvent(lnkTxt, "click", function(e){
			updateNavLeftSub("chk" + this.id.substring(3), true);		
		}, true);			
	}
}

//Dynamically sets height of left nav onload and resize
function setNavHeight() {
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myHeight = document.body.clientHeight;
	}
  
	myHeight -= 249;

	document.getElementById('wrapAccordion').style.height=myHeight + "px";
}

//Finds current map center and determines if it is outside the predefined limits
function outsideBoundsCenter()
{
	currentLat = map.getCenter().y *1;
	currentLong = map.getCenter().x *1;

	if (map.getZoom() > 16 && setMapCenter && !mapLoad)
		map.setZoom(16);
	
	if(currentLong < BOUND_LIMIT_W || currentLong > BOUND_LIMIT_E || currentLat < BOUND_LIMIT_S || currentLat > BOUND_LIMIT_N)
		return true;
	else
		return false;
}

//Toggle Checkbox if link is clicked
function toggleCheck(chkBox)
{
	if ( document.getElementById(chkBox).checked==1)
		document.getElementById(chkBox).checked=false;
	else
		document.getElementById(chkBox).checked=true;
}

//Determine if all, none, or some Sub boxes are checked
function allNoneCheck(length, layerNum, chkAll, chkSub)
{
	var numChk = 0;

	var icon = chkSub.substring(3).toLowerCase() + "Icon"; //create Icon ID

	//Count number of boxes checked
	for (i = 0; i < length; i++ )
	{
		if (document.getElementById(chkSub + i).checked==true)	
			numChk ++;
	}

	if (numChk==length) //Check the all Box
	{
		document.getElementById(chkAll).checked=true;
	}
	else if (numChk==0) //uncheck the all Box, hide the icon
	{
		document.getElementById(chkAll).checked=false;
		layersChecked[layerNum]=false;
		document.getElementById(icon).style.display = 'none';
	}
	else //uncheck the all Box, display the icon
	{
		document.getElementById(chkAll).checked=false;
		document.getElementById(icon).style.display = 'block';
	}
}

//Check or uncheck all Sub boxes
//Called when an all checkbox is clicked
function allCheckUncheck(chkAll, chkSub, length)
{
	var icon = chkSub.substring(3).toLowerCase() + "Icon"; //create Icon ID

	if (document.getElementById(chkAll).checked==0)
	{
		document.getElementById(icon).style.display = 'none';

		for (i = 0; i < length; i++ )		
			document.getElementById(chkSub + i).checked=false;
	}
	else
	{
		document.getElementById(icon).style.display = 'block';

		for (i = 0; i < length; i++ )
			document.getElementById(chkSub + i).checked=true;
	}
}

//Get checkbox id Number
function getIDnum(chkBox)
{
	var num;

	var temp = chkBox.substring(chkBox.length-1);

	var temp2 = chkBox.substring(chkBox.length-2);

	if (isNumeric(temp2))
		return temp2;
	else
		return temp;
}

//Get LayerNum based on Checkbox Name
function getLayerNum(chkBox)
{
	if (chkBox.length>6)
	{
		chkBox = chkBox.substring(0,6);
	}

	for (i=0;i<leftNav[LEFT_NAV_INFO].length;i++)
	{
		if (chkBox == leftNav[LEFT_NAV_INFO][i][0].substring(0,6))
		{
			return leftNav[LEFT_NAV_INFO][i][2];
		}
	}
}

function isNumeric(x) {
// I use this function like this: if (isNumeric(myVar)) { }
// regular expression that validates a value is numeric
var RegExp = /^(-)?(\d*)(\.?)(\d*)$/; // Note: this WILL allow a number that ends in a decimal: -452.
// compare the argument to the RegEx
// the 'match' function returns 0 if the value didn't match
var result = x.match(RegExp);
return result;
}

function cs() //Un-highlights text when double clicked in nav
{ 
	var sel; 

	if(document.selection && document.selection.empty)
	{ 
		document.selection.empty() ; 
	} 
	else if(window.getSelection) 
	{ 
		sel=window.getSelection(); 
		
		if(sel && sel.removeAllRanges) 
			sel.removeAllRanges(); 
	} 
}