// This document contains site-specific javascript functions and utilities

//function sets "activeLink" class to "activeLinkClass" and all links targeted by "linksSelector" to "inactiveLinkClass"
function setActiveLink(activeLink, activeLinkClass, linksSelector, inactiveLinkClass){
	if (document.getElementsBySelector(linksSelector)){
		var links = document.getElementsBySelector(linksSelector);	
		for (var i = 0; i < links.length; i++) links[i].className = inactiveLinkClass;	
		activeLink.className = activeLinkClass;
	}
}

//Function changes the class of the last item of every list targeted by css 'listSelector' to 'lastItemClassName'
//Used for formatting list based trees
function format_bullet_tree (listSelector, lastItemClassName){
	if(document.getElementsBySelector(listSelector)) { 
		var lastListItem, lastListItemContent, i, flag;
		var lists = document.getElementsBySelector(listSelector);
		for (var i = 0; i < lists.length; i++) {

			if(lists[i].lastChild) { // check for children
    		lastListItem = lists[i].lastChild;
				flag = true;
    		while((lastListItem)&&(flag == true)) { // run over them
      		if(lastListItem.nodeType==1) { // element
						if (lastListItem.className != "base") lastListItem.className = lastItemClassName;	
						//alert(lastListItem.className);
						flag = false;
      		} else {
      			lastListItem = lastListItem.previousSibling;
					}
    		}
 			}
			
		}
	}
} //end function format_bullet_tree



function show_hide_all_steps(){

	var display = "hide";
	var nav_selector = "div.step";
	if (document.getElementsBySelector(nav_selector)) {
		var nav_contents = document.getElementsBySelector(nav_selector);
		if (nav_contents.length > 0){//SHOW
			display = "show";					
			for (var i = 0; i < nav_contents.length; i++) nav_contents[i].className = 'step_on_expanded';	
			nav_contents = document.getElementsBySelector("div.step_on");
			for (var i = 0; i < nav_contents.length; i++) nav_contents[i].className = 'step_on_expanded';		
		} else {//HIDE
			nav_contents = document.getElementsBySelector("div.step_on");
			for (var i = 0; i < nav_contents.length; i++) nav_contents[i].className = 'step';		
			nav_contents = document.getElementsBySelector("div.step_on_expanded");	
			for (var i = 0; i < nav_contents.length; i++) nav_contents[i].className = 'step';
		}
		
		var content_selector = "div.step_content";
		var step_contents = document.getElementsBySelector(content_selector);
		for (var i = 0; i < step_contents.length; i++) {
			if (display == "hide"){
				step_contents[i].style.display = 'none';
			} else {
				step_contents[i].style.display = '';
			}			
		}
	}//end if
}// end function show_hide_all_steps 
 

function show_hide_steps(active){
	
	var expanded_nav_selector = "div.step_on_expanded";
	var expanded_nav_contents = document.getElementsBySelector(expanded_nav_selector);

	if (expanded_nav_contents.length == 0){

		var content_selector = "div.step_content";
		var nav_selector = "div.step_on";			
		var subtract_height = 0; //
		var step_contents = document.getElementsBySelector(content_selector);
		for (var i = 0; i < step_contents.length; i++) {
			//alert (step_contents[i].id);
			if (step_contents[i].style.display != 'none'){
				subtract_height = subtract_height + step_contents[i].offsetHeight;
				step_contents[i].style.display = 'none';				
			}
		}
		if (active.length > 0){
			
			var active_content = active + "_content"; //made step content visible
			if (document.getElementById(active_content)){
				step_content = document.getElementById(active_content);
				step_content.style.display = '';
			}
			var steps = document.getElementsBySelector(nav_selector); //reset nav classes
			for (var i = 0; i < steps.length; i++) {
				steps[i].className = 'step';	
			}			
			if (document.getElementById(active)){ //set active nav class
				step = document.getElementById(active);
				step.className = 'step_on';
				//alert ("offsettop = " + window.pageYOffset);
			}			
			
		}	//end if active is set
	}//if not dealing with expanded steps
}//end function hide_steps


function show_hide(id){
	if(document.getElementById(id)) { 	
		var section = document.getElementById(id); 
		
		//alert (section.style.display);
		
		if (section.style.display == "none"){
			section.style.display = "";
			//alert("show " + id);
		} else {
			section.style.display = "none";
			//alert("hide " + id);			
		}//end if
	}//end if
}//end function show_hide


//function sets css class for 'id' to 'idClass' and sets css class for elements targeted by 'selector' to 'selectorClass'
function change_class_group(id, selector, idClass, selectorClass){
	
	var i;
	if (document.getElementsBySelector(selector)){
		var selectorElement;
		var selectorElements = document.getElementsBySelector(selector);
		for (i = 0; i < selectorElements.length; i++){
			selectorElement = selectorElements[i];
			if (selectorElement.getAttribute("id") != id) selectorElement.className = selectorClass;	
		}	
	}	
	if (id.length > 0){
		if (document.getElementById(id)){
			var idElement = document.getElementById(id);	
			idElement.className = idClass;	
		}
	}		
}//end function

//Function uses css display property to hide elements targeted by "hideSelector" and toggle the element targeted by "showId"
//if toggleFlag = true.  if toggleFlag = false, showId element is never hidden.  
function show_hide_group(showId, hideSelector, toggleFlag){

	var i;
	var hideElement;
	if (document.getElementsBySelector(hideSelector)){
		var hideElements = document.getElementsBySelector(hideSelector);
		for (i = 0; i < hideElements.length; i++){
			hideElement = hideElements[i];
			if ((hideElement.getAttribute("id") != showId) || (toggleFlag == false)) hideElement.style.display = 'none';	
		}	
	}	
	if (showId.length > 0){
		if (document.getElementById(showId)){
			var showElement = document.getElementById(showId);	
			
			if (showElement.style.display != 'none'){				
				showElement.style.display = 'none';				
			} else {
				showElement.style.display = '';
			}//end if		
		}
	}	
}//end show_hide_group

//either shows or hides all elements targeted by 'selector' based on visibility of first element targeted or value of 'display' 
function show_hide_by_selector(selector, display){
	
	if(document.getElementsBySelector(selector)) { 	
		var sections = document.getElementsBySelector(selector);
		if (display.length == 0){
			if (sections[0].style.display == "none"){
				display = "show";
			} else {
				display = "hide";			
			}//end if			
		}
		for (var i = 0; i < sections.length; i++) { 
			section = sections[i];
			if (display == "show"){
				section.style.display = "";
			} else {
				section.style.display = "none";			
			}//end if			
		}//end for
	}//end if
}//end function show_hide_by_selector



//sets the current tab on the page header 
function setPageTab(location){
	var navSelector = "table#navBarMenu td";
	var onClass = "on";
	var lastOnClass = "laston";
	if ((document.getElementsBySelector(navSelector))&&(location.length > 0)) {
		var navCells = document.getElementsBySelector(navSelector);
		var cellLinks, cellLocation, cellMax = navCells.length;
		for (var i = 0; i < cellMax; i++){
			if (cellLinks = navCells[i].getElementsByTagName("a")){
				cellLocation = trim(cellLinks[0].innerHTML.toLowerCase());
				location = trim(location.toLowerCase());
				if (location == cellLocation){
					if (i == cellMax)
						navCells[i].className = lastOnClass;
					else
						navCells[i].className = onClass;
					i = cellMax + 1;
				}
			}
		}//end for
	}//end if getElementBySelector and location is set
}//end function setPageTab

function setCellRollovers(location){//selector, on class, off class, over class

var selector = "table#navBarMenu td";
var on = "on";
var off = "";
var over = "hover";
var cells = document.getElementsBySelector(selector);
var url = String(document.location);
var cell_links, cellLists, cellListElements, cellMouseOver, cellMouseOut, x;

for (var i = 0; i < cells.length; i++) {
	
	//SET CELL ROLLOVERS
	cellMouseOver = "";
	cellMouseOut = "";			
	cell_links = cells[i].getElementsByTagName("a");
	if (cell_links.length > 0){
		
		href = cell_links[0].getAttribute('href'); //of first link in cell
		cellText = trim(cell_links[0].innerHTML.toLowerCase());
		cellText = cellText.replace("'", "");
		cellText = cellText.replace('"', '');
		cellText = cellText.replace('&amp;', '&');
		
		//alert("celltext = '" + cellText + "' location = '" + location + "'");
		if (location.length > 0) 
			location = trim(location.toLowerCase());
		else
			location = "";
		
		//if cell link points to current location or location = link text then set link class = on
		if ( ((((href.length == (url.length - url.indexOf(href)))&&(url.match(href) != null))||(url == href)&&(href != '#')&&(href.length > 0))) ||(location == cellText)){ 

			cells[i].className = cells[i].className + on;			
			cellMouseOver = "	var lists = this.getElementsByTagName('UL');\n	if (lists.length > 0) lists[0].className = 'navDrophover';";
			cellMouseOut = "	var lists = this.getElementsByTagName('UL');\n	if (lists.length > 0)  lists[0].className = 'navDrop';";					
		
		} else { // else add mouseover image event handler, and preload

			
			cellMouseOver = "	this.className = this.className + '" + over + "';\n	var lists = this.getElementsByTagName('UL');\n	if (lists.length > 0) lists[0].className = 'navDrophover';";
			cellMouseOut = "	this.className = this.className.replace('" + over + "','" + off + "');\n	var lists = this.getElementsByTagName('UL');\n	if (lists.length > 0)  lists[0].className = 'navDrop';";	
		} 
		
		//ADD MOUSEOVER AND MOUSEOUT HANDLERS TO CELL
		eval ("cells[i].onmouseover=function() {" + cellMouseOver + "}");
		eval ("cells[i].onmouseout=function() {" + cellMouseOut + "}");
				
		//SET MENU DROPDOWN BEHAVIOR
		cellLists = cells[i].getElementsByTagName("UL");
		if (cellLists.length > 0){//if cell has menu
				
			//ADD ROLLOVER BEHAVIOR TO LIST ITEMS
			cellListElements = cellLists[0].getElementsByTagName("LI");		
			for (x = 0; x < cellListElements.length; x++) {
					cellListElements[x].onmouseover=function() {this.className = over;}
					cellListElements[x].onmouseout=function() {this.className = this.className.replace(over, off);}
				}//end for		
		} //if cell has  menu
		
	}//end if cell has link
}//end for
}//end function setCellRollovers


function select_show_tab(selObj, tabCount) {//shows tab specified by 'id' and hides all other divs specified by ids'tab1' - tabCount, sets class of tab button	
		var divDhtml, i, id;			
		id = selObj.options[selObj.selectedIndex].value;		
		// LOOP THROUGH TABS 
		for(i=1; i<=tabCount; i++) {
			tab = "tab" + i;
			if(document.getElementById(tab)) {  // IF THIS TAB WAS FOUND IN THE DOCUMENT...
				divDhtml = document.getElementById(tab);
				if (id != 'all'){
					//alert(id);
					divDhtml.style.display = "None" ; // HIDE THIS TAB
				} else
					divDhtml.style.display = "" ; // SHOW THIS TAB
			}
		}
		// SHOW REQUESTED TAB
		if(document.getElementById(id)) {  // IF THIS TAB WAS FOUND IN THE DOCUMENT...
			divDhtml = document.getElementById(id);
			divDhtml.style.display = "" ; // SHOW THIS TAB
		}
		
}// end function


function show_tab(id, tabCount) {//shows tab specified by 'id' and hides all other divs specified by ids'tab1' - 'tab10', sets class of tab button	
		var divDhtml, i;			
				
		// LOOP THROUGH TABS 
		for(i=1; i<=tabCount; i++) {
			tab = "tab" + i;
			if(document.getElementById(tab)) {  // IF THIS TAB WAS FOUND IN THE DOCUMENT...
				divDhtml = document.getElementById(tab);
				if (id != 'all'){
					//alert(id);
					divDhtml.style.display = "None" ; // HIDE THIS TAB
				} else
					divDhtml.style.display = "" ; // SHOW THIS TAB
			}
		}
		// SHOW REQUESTED TAB
		if(document.getElementById(id)) {  // IF THIS TAB WAS FOUND IN THE DOCUMENT...
			divDhtml = document.getElementById(id);
			divDhtml.style.display = "" ; // SHOW THIS TAB
		}
		
}// end function


function add_targets(){

var links = document.links;
var i = document.links.length;
var url;

while(i--){
	if (links[i].target.length < 1){//if target doesn't already exist		

	if (!top.location.href.match("ecare")) {

		href = links[i].getAttribute('href');  		
			
			
			//alert(href + " lastindex = " + href.lastIndexOf("#"));
		if (href.match("#")&&(!(href.match("glossary")))){//if inline
			
			var inline;
			inline = href.substring(href.lastIndexOf("#"), href.length);
			
			if (location.href.match("#")){
				url = location.href.substring(0, location.href.indexOf("#"));
			} else {
				url = location.href;
			}
			links[i].setAttribute('href', url + inline);				

		} else { //not inline link		
			if (href.match("www")){ //add http to www links
				if (!href.match("http")){ 	
					links[i].setAttribute('href', "http://" + href);
				}
			}			
			if (href.match("http")){
				if ((href.match("wildblue")) || (href.match("10.255.10.160")) || (href.match("199.239.30.199")) || (href.match("10.147.8.43")) ){
					links[i].target = "_top";
				} else {
					//alert(href);
					links[i].target = "_blank";
				}
			}
		}//not inline link
		
	} else {//ecare

		if (href.match("#")){//if inline

		} else { //not inline link		
			if (href.match("www")){ //add http to www links
				if (!href.match("http")){ 	
					links[i].setAttribute('href', "http://" + href);
				}
			}			
			if (href.match("http")){
					links[i].target = "_blank";
			}
		}//not inline link
		
	}//if not ecare
	}//if no target
}//end while
}//end add_targets

function runFunctions(){ //onload function to call all other page functions



}