// JavaScript Document

var mainNavSetup 		= false;
var mainNavDelay 		= .75 // sec
var mainNavID			= 'site_nav';
var subNavContainerID 	= 'site_subNavBar';
var mainNav_curr_idx 	= null;
var mainNav_sel_idx		= null;
var mainNav_isClosing	= false;
var mainNav_timer		= null;

var mainNav_btns		= new Array();
var mainNav_subNav 		= new Array();
/*************************************************************************
Variable to make sure there is a sub navigation item to work on
This only breaks on the admin navigation because the template does not 
include the traditional <? include($PHP_HTML_INCLUDE.'site_header.php');?>
instead it is put in manually on all admin pages.

I just changed any operations that worked on the subNavContainer or any 
arrays that its reference was put in to check if it is there by the var
subNav_there

Altered by James Hanson - EDC - 5-10-11

***************************************************************************/
var subNav_there 		= null;

function init() {
	
	var mainNav = document.getElementById(mainNavID);
	//Make sure there is a subnav container
	if(document.getElementById(subNavContainerID))
	{
	  var subNav_contain = document.getElementById(subNavContainerID);
	  //it is so set it to true
	  subNav_there = true;
	}
	else
	{
		//it isn't there so we must be on the admin side
		//set it to false
		subNav_there = false;
	}
	var mcList = mainNav.childNodes;
	if(subNav_there)
	{
	var cList = subNav_contain.childNodes;
	}
	
	for(var i=0; i<mcList.length; i++) {
		if(mcList[i].nodeName.toLowerCase()=='li') {
			mainNav_btns[mainNav_btns.length] = mcList[i];
		}
	}
	if(subNav_there)
	{
	  for(var i=0; i<cList.length; i++) {
		  if(cList[i].nodeName.toLowerCase()=='ul') {
			  mainNav_subNav[mainNav_subNav.length] = cList[i];
			  if(cList[i].style.display.toLowerCase()=='block') {
				  mainNav_sel_idx 	= mainNav_subNav.length-1;
				  mainNav_curr_idx 	= mainNav_subNav.length-1;
			  }
		  }
	  }
	}
}

function mainNav_mIn(index) {
	if(!mainNavSetup) {
		init();
		mainNavSetup = true;
	}
	
	if(mainNav_isClosing) {
		clearTimeout(mainNav_timer);
		mainNav_isClosing 	= false;
		mainNav_timer 		= null;
	}
	if(mainNav_curr_idx!=null) {
		mainNavBtn_setSel(mainNav_curr_idx, false);
		if(subNav_there)
		{
		mainNav_subNav[mainNav_curr_idx].style.display = 'none';
		}
	}
	
	mainNav_curr_idx = index;
	mainNavBtn_setSel(mainNav_curr_idx, true);
	if(subNav_there)
	{
	mainNav_subNav[mainNav_curr_idx].style.display = 'block';
	}
}

function subNav_mIn() {
	if(!mainNavSetup) return;
	if(mainNav_isClosing) {
		clearTimeout(mainNav_timer);
		mainNav_isClosing 	= false;
		mainNav_timer 		= null;
	}
}

function mainNav_mOut(isTimer) {
	if(!mainNavSetup) return;
	if(mainNav_isClosing && isTimer) {
		if(mainNav_curr_idx!=null) {
			mainNavBtn_setSel(mainNav_curr_idx, false);
			if(subNav_there)
			{
			mainNav_subNav[mainNav_curr_idx].style.display = 'none';
			}
			
			mainNav_curr_idx = null;
		}
		
		if(mainNav_sel_idx!=null) {
			mainNav_curr_idx = mainNav_sel_idx;
			mainNavBtn_setSel(mainNav_curr_idx, true);
			if(subNav_there)
			{
			mainNav_subNav[mainNav_curr_idx].style.display = 'block';
			}
		}
	} else if(!mainNav_isClosing) {
		mainNav_timer = setTimeout('mainNav_mOut(true)', mainNavDelay*1000);
		mainNav_isClosing = true;
	}
}

function mainNavBtn_setSel(index, bool) {
	var btn = mainNav_btns[index];
	var arr = btn.className.split(' ');
	
	if(bool) {
		btn.className = arr[0]+' '+arr[0]+'Sel';
	} else {
		btn.className = arr[0];
	}
}

/* Other Functions *******************************/
/* ***********************************************/

function hideShow(showID, hideList) {
	var showObj 		= document.getElementById(showID+'_subLinks');
	var showToggleObj 	= document.getElementById(showID+'_toggle');
	var showLinkObj 	= document.getElementById(showID+'_link');
	showObj.style.display = 'block';
	showToggleObj.className = 'siteMap_toggleLink_open';
	showLinkObj.style.color = '#2e769f';
	
	var hideObj;
	var hideToggleObj;
	var hideLinkObj;
	var idArr = hideList.split(',');
	for(var i=0; i<idArr.length; i++) {
		hideObj 		= document.getElementById(idArr[i]+'_subLinks');
		hideToggleObj 	= document.getElementById(idArr[i]+'_toggle');
		hideLinkObj 	= document.getElementById(idArr[i]+'_link');
		
		if(hideObj!=null) {
			hideObj.style.display = 'none';
			hideToggleObj.className = 'siteMap_toggleLink';
			hideLinkObj.style.color = '#709949';
		}
	}
	return false;
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
