// JavaScript Document
function showhidePopUpLayer(layerName,show) {
 if (document.getElementById) {
	obj = document.getElementById(layerName);
} else if (document.all) {
	obj = document.all.item(layerName);
} else {
	obj = null;
}
 if (obj==null) return;
 obj.style.visibility = show ? 'visible' : 'hidden';
 obj.style.display = show ? 'block' : 'none';
}

sfHover = function() {
	var sfEls = document.getElementById("nav_bar").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

// toggles all the checkboxes under "parentId" to match the state of "selector"
function toggleCheckboxGroup(selector, parentId) {
	parentElement = document.getElementById(parentId);		
	checkboxes = parentElement.getElementsByTagName('input');
	for (var i = 0; i < checkboxes.length; i++) {					
		checkboxes[i].checked = selector.checked;
	}						
}

/*
	Expandable Listmenu Script
	Author : Daniel Nolan
	http://www.bleedingego.co.uk/webdev.php
*/
function initMenus() {
	if (!document.getElementsByTagName) return;
	
	var aMenus = document.getElementsByTagName("LI");
	for (var i = 0; i < aMenus.length; i++) {
		var mclass = aMenus[i].className;
		if (mclass.indexOf("expandList") > -1) {
			var submenu = aMenus[i].childNodes;
			for (var j = 0; j < submenu.length; j++) {
				if (submenu[j].tagName == "A") {
					
					submenu[j].onclick = function() {
						var node = this.nextSibling;
											
						while (1) {
							if (node != null) {
								if (node.tagName == "UL") {
									var d = (node.style.display == "none")
									node.style.visibility = (d) ? 'visible' : 'hidden';
									node.style.display = (d) ? "block" : "none";
									this.className = (d) ? "contract" : "expand";
									return false;
								}
								node = node.nextSibling;
							} else {
								return false;
							}
						}
						return false;
					}
					
					submenu[j].className = (mclass.indexOf("open") > -1) ? "contract" : "expand";
				}
				
				if (submenu[j].tagName == "UL")
					submenu[j].style.display = (mclass.indexOf("open") > -1) ? "block" : "none";
			}
		}
	}
}

window.onload = initMenus;

//change color of selected tab on map chooser
function selectThisTab(listName,obj) {
	if (document.getElementById) {
    tabs = document.getElementById(listName);
    obj = document.getElementById(obj);
  } else if (document.all) {
    tabs = document.all.item(listName);
    obj = document.all.item(obj);
  } else {
    tabs = null;
    obj = null;
  }
  if (tabs==null) return;
  var tabList = tabs.getElementsByTagName("li");
  for (i = 0; i < tabList.length; i++) {
    tabList[i].className = "";
  }
  obj.className = "you_are_here";
}

//show info on map hover
function popUpText(e,LocationName) {
	if (document.getElementById) {
		mapbox = document.getElementById('mapInfo');
	} else if (document.all) {
		mapbox = document.all.item('mapInfo');
	} else {
		mapbox = null;
	}
  	if (mapbox==null) return;
	mapbox.style.visibility = 'visible';
 	mapbox.style.display = 'block';
  	dataMapValue = stateValues[LocationName];
  	if (dataMapValue == null) {
  	    dataMapValue = 'No data available';
  	}
  	mapbox.innerHTML = '<p><b>'+LocationName+'</b><br />'+dataMapValue+'</p>';
  	var posx = 0;
	var posy = 0;
	if(e.offsetX || e.offsetY) { //For Internet Explorer
		posx=e.offsetX;
		posy=e.offsetY;
		offsetX = 0;
		offsetY = 400;
	} else { //For FireFox
		posx=e.pageX;
  		posy=e.pageY;
		offsetX = -75;
		offsetY = 20;
 	}
	posx = posx+offsetX;
	posy = posy+offsetY;
	document.getElementById('mapInfo').style.top = posy +'px'; 
	document.getElementById('mapInfo').style.left = posx +'px'; 
}

function hideMapText () {
	if (document.getElementById) {
		mapbox = document.getElementById('mapInfo');
	  } else if (document.all) {
		mapbox = document.all.item('mapInfo');
	  } else {
		mapbox = null;
	  }
  if (mapbox==null) return;
 mapbox.style.visibility = 'hidden';
 mapbox.style.display = 'none';
}

function validateLocationSelection(container, min, max)
{
    var checkboxParent = document.getElementById(container);
    if (checkboxParent == null) { return true; }
    
    var checkedCount = 0;
    
    var inputs = checkboxParent.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        if (input.type == 'checkbox' && input.checked) {
            checkedCount += 1;
        }
    }
        
    if (min > 0 && checkedCount < min)
    {
        var locationText = min == 1 ? 'location' : 'locations';
        alert('Please select at least ' + min + ' ' + locationText);
        return false;
    }
    
    if (max > 0 && checkedCount > max)
    {
        var locationText = max == 1 ? 'location' : 'locations';
        alert('Please select at most ' + max + ' ' + locationText);
        return false;
    }
    
    return true;
}

