// JavaScript Document
// was util.js

function ucFirst(s)
{
	var c = s.charAt(0);

	if (parseInt(s.length)==1){
		return c.toUpperCase();
	}
	else
	{
		return c.toUpperCase() + s.slice(1).toLowerCase();
	}
}
function fromCssToDom(thisLabel)
{
var mySplit = thisLabel.split('-');
// skip the first one
for (i=1;i<mySplit.length;i++)
{
	mySplit[i]=ucFirst(mySplit[i]);
}
thisLabel = mySplit.join('');
return thisLabel;
}
function findInArray(theValue, theArray) {
	var found = false;
	var arrayLength = theArray.length;
	for (var ctr=0; ctr < arrayLength; ctr++){
		if (theValue == theArray[ctr]){
			found=true;
			break;
		}
	}
	return found;
}
function trimString (str) {
  var thisStr = '' + str;
  if (thisStr != '' && (thisStr.search(/^\s+/g) || thisStr.search(/\s+$/g))) {
	  thisStr = thisStr.replace(/^\s+/g, '');
	  thisStr = thisStr.replace(/\s+$/g, '');
  }
  return thisStr;
}
function saidNo(messageString,urlString,setOnCancel,unSetOnCancel) {
	if (confirm(messageString)) {
		document.location.replace(urlString);
		}
	else {
		setOnCancel.checked = 1;
		unSetOnCancel.checked = 0;
		}
	}

/**
 * Throughout, whitespace is defined as one of the characters
 *  "\t" TAB \u0009
 *  "\n" LF  \u000A
 *  "\r" CR  \u000D
 *  " "  SPC \u0020
 *
 * This does not use Javascript's "\s" because that includes non-breaking
 * spaces (and also some other characters).
 */


/**
 * Determine whether a node's text content is entirely whitespace.
 *
 * @param nod  A node implementing the |CharacterData| interface (i.e.,
 *             a |Text|, |Comment|, or |CDATASection| node
 * @return     True if all of the text content of |nod| is whitespace,
 *             otherwise false.
 */
function is_all_ws( nod )
{
  // Use ECMA-262 Edition 3 String and RegExp features
  return !(/[^\t\n\r ]/.test(nod.data));
}


/**
 * Determine if a node should be ignored by the iterator functions.
 *
 * @param nod  An object implementing the DOM1 |Node| interface.
 * @return     true if the node is:
 *                1) A |Text| node that is all whitespace
 *                2) A |Comment| node
 *             and otherwise false.
 */

function is_ignorable( nod )
{
  return ( nod.nodeType == 8) || // A comment node
         ( (nod.nodeType == 3) && is_all_ws(nod) ); // a text node, all ws
}

/**
 * Version of |previousSibling| that skips nodes that are entirely
 * whitespace or comments.  (Normally |previousSibling| is a property
 * of all DOM nodes that gives the sibling node, the node that is
 * a child of the same parent, that occurs immediately before the
 * reference node.)
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The closest previous sibling to |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function node_before( sib )
{
  while ((sib = sib.previousSibling)) {
    if (!is_ignorable(sib)) return sib;
  }
  return null;
}

/**
 * Version of |nextSibling| that skips nodes that are entirely
 * whitespace or comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The closest next sibling to |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function node_after( sib )
{
  while ((sib = sib.nextSibling)) {
    if (!is_ignorable(sib)) return sib;
  }
  return null;
}

/**
 * Version of |lastChild| that skips nodes that are entirely
 * whitespace or comments.  (Normally |lastChild| is a property
 * of all DOM nodes that gives the last of the nodes contained
 * directly in the reference node.)
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The last child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function last_child( par )
{
  var res=par.lastChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.previousSibling;
  }
  return null;
}

/**
 * Version of |firstChild| that skips nodes that are entirely
 * whitespace and comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The first child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function first_child( par )
{
  var res=par.firstChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.nextSibling;
  }
  return null;
}

/**
 * Version of |data| that doesn't include whitespace at the beginning
 * and end and normalizes all whitespace to a single space.  (Normally
 * |data| is a property of text nodes that gives the text of the node.)
 *
 * @param txt  The text node whose data should be returned
 * @return     A string giving the contents of the text node with
 *             whitespace collapsed.
 */
function data_of( txt )
{
  var data = txt.data;
  // Use ECMA-262 Edition 3 String and RegExp features
  data = data.replace(/[\t\n\r ]+/g, " ");
  if (data.charAt(0) == " ")
    data = data.substring(1, data.length);
  if (data.charAt(data.length - 1) == " ")
    data = data.substring(0, data.length - 1);
  return data;
}

/**
 * Adds another method to document to get an array of elements.
 *
 * @param pattern	A RegEx object or string pattern
 * @param flags		Optional only used with string pattern.
 */
document.getElementsByIdRegEx = function() {
	var retVal = false;
	var pattern;
	if (arguments.length > 0) {
		if (typeof(arguments[0]) != 'string') {
			if (arguments[0].exec && arguments[0].test) {
				pattern = arguments[0];
			} else {
				alert('Argument type of object, RegExp object expected.');
			}
		} else {
			if (arguments.length == 2) {
				if (arguments[1].match(/^[igm]$/) || arguments[1] == '') {
					pattern = new RegExp(arguments[0], arguments[1]);
				} else {
					alert('Flags can be one of the following: g, i or m.');
				}
			} else {
				pattern = new RegExp(arguments[0]);
			}
		}

		if (pattern != null) {
			matchedElements = [];
			var elements = document.getElementsByTagName('*');
			for (var i=0; i<elements.length; i++) {
				if (elements[i].id) {
					if (elements[i].id.match(pattern)) { matchedElements.push(elements[i]); }
				}
			}
			
			retVal = matchedElements;
		}
	} 

	return(retVal);
}

function inArray(findThis, inThis) {
	var retval = false;
	for (var i=0;i<inThis.length;i++) {
		if (inThis[i] == findThis) {
			retval = true;
			break;
			}
		}
	return retval;
	}

function hasAncestor(elem, ancestor) {
	var retVal = false;
	if (elem.parentNode) {
		if (elem.parentNode == ancestor) {
			retVal = true;
		} else {
			retVal = hasAncestor(ancestor, elem.parentNode);
		}
	}
	return(retVal)
}

// menuVars.js
var hoverRe = new RegExp("Hover$");
var allARef = document.getElementsByTagName('a');
var thisPage;
var testThisPage;
var thisPath = document.location.href;
var splitPath = document.location.href.split('/');
var tossIt = splitPath.pop();
thisPath = splitPath.join('/') + '/';
var thisFolder = splitPath.pop();
var thisPathName = window.location.pathname;
var pathnameArray = thisPathName.split('/');
var testLeftOver =  splitPath.join('/');
if (testLeftOver == 'http:/')
{
	// we are in the main folder
	thisFolder = '';
}
var againstThisPage;
if (thisFolder != '')
{
	againstThisPage = [document.location.href, thisPath+'index.php', thisPath+'index.htm',thisPath+'index.html',thisPath+'index.asp'];
	if (pathnameArray.length > 2) 
	{
		//Remove the first and the last...
		pathnameArray.shift();
		pathnameArray.pop();
		againstThisPage.push(window.location.protocol+'//'+window.location.hostname+'/'+pathnameArray[0]+'/index.php');
	}
}
else
{
againstThisPage = [document.location.href];
}

var allNavs = new Array();
var sideNavs = new Array();
var allDivs = document.getElementsByTagName('div');
var tmpThing;
var tmpThingTwo;
var pullIn = 1;
var pullUp = 14;
var winTimer;
var thisElem;
var notSideNav = [];
var notThese = [];
var pathArray = new Array();
var thisFolder;
var prev_onload = window.onload; 
var allSectionNavARef = [];
var toOverRe = new RegExp(/^(.*)(\.[^.]+)$/);
var toNormalRe = new RegExp(/^(.*)(_on)(\.[^.]+)$/);
var toOverChangeTo = "$1_on$2";
var toNormalChangeTo = "$1$3";

function inArray(findThis, inThis) {
	var retval = false;
	for (var i=0;i<inThis.length;i++) {
		if (inThis[i] == findThis) {
			retval = true;
			break;
			}
		}
	return retval;
	}


// striper.js
// JavaScript Document
var whichOne = 1; // which style do we use -- increments and rolls over
var howMany = 2; // default
var whatStyle = 'HS2'; // default
var theseRows;
var whatClass = 'greyWhite'; // for now we just make sure the style sheet is loaded before we do this
var addedThese = {}; // put them here as we add them
/*
all styles:
HS2 2 horizontal colors
VS2 2 vertical colors
*/
function stripeOn() {
	var tArray = document.getElementsByTagName("table");
	var topRow = null;
	for(var i=0;i<tArray.length;i++) {
		if (tArray[i].className.match(/^tf_/)) {
			// split it up
			var tS = tArray[i].className.split('_');
			whatStyle = tS[1];
			whatClass = tS[2];
			if (addedThese[tS[2]]) {
				// already got it
				}
			else {
				var objHead = document.getElementsByTagName('head');
				if (objHead[0]) {
					var objCSS = objHead[0].appendChild(document.createElement('link'));
					objCSS.id = tS[2];
					objCSS.rel = 'stylesheet';
					var domainpart = document.location.href.split('/');
					objCSS.href = 'http://' + domainpart[2] + '/css/tableStripers/'+tS[2]+'.css';
					//alert(objCSS.href);
					objCSS.type = 'text/css';
					}
				}
			var theseTables = tArray[i].getElementsByTagName("table");
			for (var tKiller=0;tKiller<theseTables.length;tKiller++) {
				var thoseRows = theseTables[tKiller].getElementsByTagName("tr");
				for (var rIndex=0;rIndex<thoseRows.length;rIndex++) {
					thoseRows[rIndex].setAttribute("ignoreMe", "true");
				}
			}
			var theseRows = tArray[i].getElementsByTagName("tr");
			for (var j=0;j<theseRows.length;j++) {	
				if (theseRows[j].getAttribute("ignoreMe") == "true") {
					//don't apply striper to these rows in tables inside outer table
				}
				else {
					if (j==0 && whatClass.match(/Top/)) {
						topRow = theseRows[j];
						// use the 'TopRow' styles
						theseRows[j].className = 'tr_'+whatClass + 'TopRow';
						var theseCels = theseRows[j].getElementsByTagName("td");
						for(var k=0;k<theseCels.length;k++) {
							if (theseCels[k].parentNode.getAttribute("ignoreMe") == "true")
							{
								// skipping it
							}
							else
							{
								if (k == 0) {
									theseCels[k].className='td_'+whatClass + 'TopRowLeft';
									}
								else if (k == theseCels.length - 1) {
									theseCels[k].className='td_'+whatClass + 'TopRowRight';
									}
								else {
									theseCels[k].className='td_'+whatClass + 'TopRow';
									}
								}
							}
						}
					else if (j==theseRows.length - 1 && whatClass.match(/Bottom/)) {
						// use the 'BottomRow' styles
						theseRows[j].className = 'tr_'+whatClass + 'BottomRow';
						var theseCels = theseRows[j].getElementsByTagName("td");
						for(var k=0;k<theseCels.length;k++) {
							if (theseCels[k].parentNode.getAttribute("ignoreMe") == "true")
							{
								// skipping it
							}
							else
							{
								if (k == 0) {
									theseCels[k].className='td_'+whatClass + 'BottomRowLeft';
									}
								else if (k == theseCels.length - 1) {
									theseCels[k].className='td_'+whatClass + 'BottomRowRight';
									}
								else {
									theseCels[k].className='td_'+whatClass + 'BottomRow';
									}
								}
							}
						}
					else {
						theseRows[j].className = 'tr_'+whatClass+whichOne + '';
						var theseCels = theseRows[j].getElementsByTagName("td");
						for(var k=0;k<theseCels.length;k++) {
							if (theseCels[k].parentNode.getAttribute("ignoreMe") == "true")
							{
								// skipping it
							}
							else
							{
								if (k == 0) {
									theseCels[k].className='td_'+whatClass + whichOne + 'Left';
									}
								else if (k == theseCels.length - 1) {
									theseCels[k].className='td_'+whatClass + whichOne + 'Right';
									}
								else {
									theseCels[k].className='td_'+whatClass + whichOne;
									}
								}
							}
						whichOne++;
						if (whichOne > howMany) {
							whichOne = 1;
							}
						}
					}
				}
			}
		}
	}


//sets site path links to hover state
function lightUp(){
	var pathSplit;
	var testPath;
	var testRe;
	for (var i=0;i<allARef.length;i++) {
		testThisPage = allARef[i].href;
		/*
		if (!allARef[i].id || allARef[i].id == ''){
			allARef[i].id = 'link_' + i;
		}
		*/
		if (allARef[i].className == "pageNavLink"){
			allSectionNavARef.push(allARef[i]);
		}
		for (var j=0;j<againstThisPage.length;j++) {
			if (testThisPage.toLowerCase()== againstThisPage[j].toLowerCase()) {
				if (!allARef[i].className.match(/Hover$/)) {
				allARef[i].className=allARef[i].className+'Hover';
				}
				if (allARef[i].className == "pageNavLinkHover"){
					thisPage=allARef[i].id;
				}
			}
		}	
	}
	
	//the current page we are on is not in the nav (for e.g. tabbed nav)
	if ((!thisPage) || (thisPage == '')) {
		pathSplit = document.location.href.split('/');
		testPath = pathSplit.pop();
		testPath = pathSplit.join('/') + '(?:/|/index\\.[^./]+)$';
		testRe = new RegExp("^" + testPath, "i");
		for (var i=0;i<allSectionNavARef.length;i++) {
			if (testRe.test(allSectionNavARef[i].href)) {
				if (!allSectionNavARef[i].className.match(/Hover$/)) {
				allSectionNavARef[i].className=allSectionNavARef[i].className+'Hover';
				}
				thisPage = allSectionNavARef[i].id;
			}
		}
	}
}
function initTextNav() {
	var aStr = '';
	//MAIN NAV STATE
	//get the folder of the current page (which, in this case is the 3rd element in pathArray)
	//the folder name and the id of the image are set up to use the same name
	//thus we can use the folder name to set the img source to hover
	//designate that this is current page by affixing 'Selected' to its id
	//this is used as a flag so we don't change its state in mouseover and out events
	// var aWin = window.open('', 'aWin');
	// aWin.document.body.innerHTML = '<pre>' + aStr + '</pre>';
	pathArray = document.location.href.split("/");
	thisFolder = pathArray[3];
	
	if (document.getElementById(thisFolder) && !document.getElementById(thisFolder).className.match(/Hover$/)) {
		document.getElementById(thisFolder).className=document.getElementById(thisFolder).className+'Hover';
		}
	}

// navDropdownFlyout.js

//Dropdown flyout menu functions
function setUpNavDivs() 
{
	for(var i=0;i<allDivs.length;i++) 
	{
		if (allDivs[i].className == 'navDiv') 
		{
			allDivs[i].setAttribute("id", 'navDiv_' + i);
			allNavs.push(allDivs[i]);
			// assumes that the layout is consistent, with 
			// a positioning div around the nav div
			tmpThing = allDivs[i].parentNode;
			//flyout menus positioning
			if (tmpThing.className != 'stopHere') 
			{
				tmpThingTwo = node_before(tmpThing);
				if (tmpThingTwo)
				{
					allDivs[i].style.left = (tmpThingTwo.offsetWidth - pullIn) + 'px';
					allDivs[i].style.top = '-' + Math.floor(((tmpThingTwo.offsetHeight/2) + pullUp)) + 'px';
					//debugging code
					//if there is an element in the page with id of 'heyYou' your debugging code will appear here
					if(document.getElementById('heyYou')) 
					{
						document.getElementById('heyYou').innerHTML += 'tmpThingTwo offsetWidth: ' + tmpThingTwo.offsetWidth + '\n';
						document.getElementById('heyYou').innerHTML += 'tmpThingTwo offsetHeight: ' + Math.floor(((tmpThingTwo.offsetHeight/2) + pullUp)) + '\n';
					}
				}
			}
		}
		if (allDivs[i].className == 'sideNavDiv') 
		{
			sideNavs.push(allDivs[i]);
		}
	}

	//when page loads navDivs are display: block visibility: hidden - otherwise we can't measure them. This sets them to display: none visibility: visible and display is toggled on mouseover.
	for(var i=0;i<allNavs.length;i++) 
	{
		allNavs[i].style.display='none';
		allNavs[i].style.visibility='visible';
	}
}

function waitASec(elem) {
	window.clearTimeout(winTimer);
	//winTimer = window.setTimeout("clearAll()", 1000);
	winTimer = window.setTimeout("clearAll()", 1000);
	if(document.getElementById('heyYou')) {
		document.getElementById('heyYou').innerHTML += 'waitASec setting timer\n';
		document.getElementById('heyYou').innerHTML += elem + ' ' + elem.id + '\n';
		}
	}
	
function clearAll() {
	window.clearTimeout(winTimer);
	if(document.getElementById('heyYou')) {
		document.getElementById('heyYou').innerHTML += 'clearAll clearing timer\n';
		}
	notThese = [];
	for(var i=0;i<allNavs.length;i++) {
		allNavs[i].style.display='none';
		}
}

function showAfter(elem) {
    var walkOut = "";
    var thisNode = "";
	var mySib;
	var myChild;
	var prevSib;
	var m;
	if(document.getElementById('heyYou')) {
		document.getElementById('heyYou').innerHTML += '\nshowAfter ' + elem + '\n';
		}
	window.clearTimeout(winTimer);
	notThese = [];
	thisElem = elem;
    if (elem) {
		mySib = node_after(elem);
		// catch the main ones
		if (mySib && (mySib.className == 'stopHere')) {
			// get the first child of that div
			mySib = first_child(mySib);
			if(document.getElementById('heyYou')) {
				document.getElementById('heyYou').innerHTML += 'mySib is first_child\n';
				}
			}
		if (mySib) {
			if(document.getElementById('heyYou')) {
				document.getElementById('heyYou').innerHTML += 'mySib ' + mySib + '\n';
				}
			
			if (mySib.tagName.toLowerCase() == 'div' && mySib.className != 'navDiv') {
				mySib = first_child(mySib);
				}
			if (mySib.tagName.toLowerCase() == 'div' && mySib.className == 'navDiv') {
				if(document.getElementById('heyYou')) {
					document.getElementById('heyYou').innerHTML += 'mySib ' + mySib.id + '\n';
					}
				mySib.style.display='block';
				notThese.push(mySib.id);
				}
			}
		walkOut = elem.parentNode;
		if(document.getElementById('heyYou')) {
			document.getElementById('heyYou').innerHTML += 'walkOut parent: ' + walkOut.id + '\n';
			}
		if (walkOut && walkOut.className == "navDiv" && walkOut.className != 'stopHere') {
			walkOut.style.display='block';
			notThese.push(walkOut.id);
			prevSib = node_before(walkOut);
			}
		while(walkOut && walkOut.className != 'stopHere') {
			walkOut = walkOut.parentNode;
			if (walkOut && walkOut.className != 'stopHere' && walkOut.className == 'navDiv') {
				if(document.getElementById('heyYou')) {
					document.getElementById('heyYou').innerHTML += 'walkOut ' + walkOut.id + '\n';
					}
				walkOut.style.display='block';
				notThese.push(walkOut.id);
				}
			}
        }
	clearIt();
	}
	
function clearIt() {
	window.clearTimeout(winTimer);
	if(document.getElementById('heyYou')) {
		document.getElementById('heyYou').innerHTML += 'clearIt clearing timer\n';
		}
	for(var i=0;i<allNavs.length;i++) {
		if (inArray(allNavs[i].id, notThese)) {
			allNavs[i].style.display='block';
			if(document.getElementById('heyYou')) {
				document.getElementById('heyYou').innerHTML += allNavs[i].id + ' is in notThese -- setting block\n';
				}
			}
		else {
			allNavs[i].style.display='none';
			if(document.getElementById('heyYou')) {
				document.getElementById('heyYou').innerHTML += allNavs[i].id + ' is not in in notThese -- setting none\n';
				}
			}
		}
	}

// navExpandCollapse.js
//Expand collapse menu functions

function hideSectionNavs() {
for(var i=0;i<sideNavs.length;i++) {
	if (sideNavs[i].className == 'sideNavDiv') {
		sideNavs[i].style.display='none';
		}
	}
}

function initSetOpen(elem) {
	var walkOut = "";
	var thisNode = "";
	var mySib;
	var myChild;
	var prevSib;
	var m;

	var thisLink;

	if (document.getElementById(elem)) {
		thisLink = document.getElementById(elem);	
	}

//if this object (link) exsts then get the next thing
	if (thisLink) {
		mySib = node_after(thisLink);
		if (mySib) {
			//if its defined and if its a div then show it
			if (mySib.tagName=='div'||mySib.tagName=='DIV') {
				mySib.style.display='block';
			}
		}
		//look for my parents
		walkOut = thisLink.parentNode;
		if (walkOut && walkOut.className == "sideNavDiv") {
			walkOut.style.display='block';
			notSideNav.push(walkOut.id);
			prevSib = node_before(walkOut);
			if (prevSib && prevSib.tagName.toLowerCase() == 'a') {
				if (!hoverRe.test(prevSib.className)) {
					prevSib.className = prevSib.className+'Hover';
				}
			}
		}
		while(walkOut && walkOut.className != 'stopHere') {
			walkOut = walkOut.parentNode;
			if (walkOut && walkOut.className == "sideNavDiv") {
				walkOut.style.display='block';
				notSideNav.push(walkOut.id);
				prevSib = node_before(walkOut);
				if (prevSib && prevSib.tagName.toLowerCase() == 'a') {
					if (!hoverRe.test(prevSib.className)) {
						prevSib.className = prevSib.className+'Hover';
					}
				}
			}
		}
		if (walkOut && walkOut.className == "stopHere")
		{
			prevSib = node_before(walkOut);
			if (prevSib && prevSib.className.toLowerCase() == 'a' && prevSib.className == 'horizNavLink')
			{
				prevSib.className =  prevSib.className+'Hover';
			}
		}
	}
}


