/*
Template: common.js
Purpose:  Provides JavaScript required by the CSS fly-out menu.
Author:   Gerry Stanford (Some code snarfed from the web)
Date Written: November of 2007
*/

/* This applies the 'sfhover' class to li elements within the 'nav' UL element whenever 
they are 'moused over', and removes the class, using a regular expression, when 'moused out'. */
sfHover = function() {
	var sfEls = document.getElementById("nav").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"), "");
		}
	}
}

/* Loops over li's in the nav bar, setting the "urhere" class on whichever li's id
matches the className in ul_mainMenu (populated dynamically via CF) */
function initURHere() {
	if (!document.getElementById) return;
	urhereID = document.getElementById("navbar");
	if (!urhereID || !urhereID.className.length) return;
	var mnItms = document.getElementById("navbar").getElementsByTagName("LI");
	for (var i=0; i<mnItms.length; i++) {
		if (mnItms[i].id.length && mnItms[i].id == urhereID.className) {
			mnItms[i].className+=" urhere";
		}
	}
}

/* The following is to allow links to new windows, as HTML Strict does not allow
"target="_blank" in anchor tags. To use, just make sure the a tag to be linked
to a new window has a class of newwindow. */
function setLinkToNewWindow () {
    var links = document.getElementsByTagName('a');
    for (var i=0;i < links.length;i++) {
        if (links[i].className == 'newwindow') {
			links[i].onclick = function() {
                window.open(this.href);
                return false;
            };
        }
    }
}


/* Function to allow an event to be added to window.onload, as some individual
pages need to do so and the main window.onload is located in a seoarate script. */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
        	oldonload();
			func();
		}
	}
}

/* The followuing works for IE AND Firefox */
window.onload = function() { 
	initURHere();
	setLinkToNewWindow();
}

/* The following only appears to run in IE (and only needs to run in IE) */
if (window.attachEvent) window.attachEvent("onload", sfHover);


//
// TRIMMING FUNCTIONS 
//
// Trim the left side of a value. 
function ltrim(argvalue) {
	while (1) {
    	if (argvalue.substring(0, 1) != " ") break;
		argvalue = argvalue.substring(1, argvalue.length);
	}
	return argvalue;
}
// Trim the right side of a value. 
function rtrim(argvalue) {
	while (1) {
		if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ") break;
		argvalue = argvalue.substring(0, argvalue.length - 1);
	}
	return argvalue;
}
// Trim both sides of a value. 
function trim(argvalue) {
	var tmpstr = ltrim(argvalue);
	return rtrim(tmpstr);
}
