var domCompat = (document.getElementById) && (document.documentElement); // essential DOM compatability
var navUA = navigator.userAgent.toLowerCase();
var navEls = new Array(); // global array of top nav element IDs
var clearFlag = true; // indicates whether delay has passed or not (can be overridden)

function showHideDelay(elRef, noClear, generationsUp) {
	if (domCompat) { // DOM check
		var parentElId;
		for (var i=0; i<generationsUp; i++) { // loop upward until we reach our destination
			elRef = elRef.parentNode;
		}
		if (noClear) { // if we choose to override the clearFlag
			clearFlag = true;
		}
		if (clearFlag) { // check to see if we are clear to initiate a change
			clearFlag = false;
			setTimeout("showHide(\""+elRef.id+"\")",350);
		}
	} // end if domCompat
}
function showHide(elToShow) { // hide all subnavs except the desired one
	if (domCompat) { // DOM check
		var state;
		for (var j=0; j<navEls.length; j++) { // loop through all nav elements
			state = (navEls[j] == elToShow) ? "active" : ""; // determine the class to assign based on whether it is the specified el or not
			document.getElementById(navEls[j]).className = state; // assign the class
		}
		clearFlag = true; // a change has taken place; we are clear for another change to happen
	} // end if domCompat
}


function initNavMenuHandlers() { // externally add event handlers to nav elements for collapse/expanding menu
	if (domCompat) { // DOM check
		var subjectNav = document.getElementById('navMenuList');
		var subjectNavLinks = subjectNav.childNodes;
		for (var i=0; i<subjectNavLinks.length; i++) {
			if (subjectNavLinks[i].innerHTML) { // if it isn't a whitespace node
				a = subjectNavLinks[i].getElementsByTagName('a'); // build an array of all child links
				// add event handlers to the first link
				if (parentHasChildUl(a[0],1)) { // only add functionality if no destination is supplied
					a[0].onclick = function(){changeClassesOfSiblings(this,"active");return false;};
				} // end if !goesToPage
				for (j = 1; j<a.length; j++) { // add event handlers to subsequent (child) links
					if (parentHasChildUl(a[j],1)) { // only add functionality if no destination is supplied
						a[j].onclick = function(){changeClassesOfSiblings(this,"subActive");return false;};
					} // end if !goesToPage
				} // end for j
			} // end if not whitespace node
		} // end for i
	} // end if domCompat
}

function parentHasChildUl(el, generationsUp) {
	if (domCompat) { // DOM check
		for (var i=0; i<generationsUp; i++) { // loop upward until we reach our destination
			el = el.parentNode;
		} // end for i
		var children = el.getElementsByTagName('ul');
		if (children.length > 0) {
			return true;
		} else {
			return false;
		} // end if children.length
	} // end domCompat
}

function goesToPage(a) {
	if ((a.href.length > 1) && ((a.href.length) == (a.href.lastIndexOf('#') + 1))) { // if the hash is the last character of the href and the href contains more than just the hash, the href must be a null named anchor for this page
		return false;
	} else {
		return true;
	}
}

function changeClassesOfSiblings (el, onClass) {
	if (domCompat) { // DOM check
		var targets;
		var parent = el.parentNode.parentNode;
		var children = parent.childNodes; // from the destination, grab all of the children
		for (var i=0; i<children.length; i++) {
			if (children[i].innerHTML) { // if it isn't a whitespace node
				//alert (children[i].innerHTML);
				children[i].className = "";
			} // end if not whitespace node
		} // end for i
		el.parentNode.className = onClass;
	} // end if domCompat
}

function pullQueryString(hrefString) {
	var str1, str2, str3, str4, str5;
	var splitArray = hrefString.split('?');
	return splitArray[1];
}

function expandMenuItem(hrefString) {
	var lookingFor = pullCat1(hrefString);
	var subjectNav = document.getElementById('navMenuList');
	var subjectNavLinks = subjectNav.childNodes;
	for (var i=0; i<subjectNavLinks.length; i++) {
//	for (var i=0; i<10; i++) {
		if (subjectNavLinks[i].innerHTML) { // if it isn't a whitespace node
			a = subjectNavLinks[i].getElementsByTagName('a'); // build an array of all child links
			// add event handlers to the first link
			if (parentHasChildUl(a[0],1)) { // only add functionality if no destination is supplied
				if (pullCat1(a[0].href) == lookingFor) {
					changeClassesOfSiblings(a[0],"active");
					for (j = 1; j<a.length; j++) { // add event handlers to subsequent (child) links
						if (parentHasChildUl(a[j],1)) { // only add functionality if no destination is supplied
							if (pullQueryString(a[j].href) == pullQueryString(hrefString)){
								changeClassesOfSiblings(a[j],"subActive");
							}
						} // end if !goesToPage
					} // end for j
				}
			} // end if !goesToPage
		} // end if not whitespace node
	} // end for i
}


function locationOfIn(string, source) {
	place = source.indexOf(string) + 1;
	thestring = string;
	return place;
}

var editWin = null;
function openWin(theurl, winName, params) {
	if (winName=="") winName="Edit";
	if (params=="") params="status,scrollbars,menubar,location,width=500,height=400,left=10,top=10"
	if ((editWin == null) || (editWin.closed))
	  	editWin = window.open(theurl, winName, params);
	else {editWin.close();editWin = window.open(theurl, winName, params);}
	  if (!editWin.opener)
	        editWin.opener = self;
	  editWin.focus();
}


// get value of querystring param
function getQueryParamValue(param) {
	var q = document.location.search;
	var detectIndex = q.indexOf(param);
	if(q.length > 1 && detectIndex != -1) {
		return q.substring(q.indexOf("=", detectIndex)+1, q.indexOf("&", detectIndex));
	} else {
		return true;
	}
}

function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
	    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function Delete_Cookie(name,path,domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
