/*
Examples:
instead of: <body onload="myfunction()">
use: addEvent(window, 'load', myfunction);

instead of: window.onload = myfunction;
use: addEvent(window, 'load', myfunction);
*/
function addEvent(obj, evType, fn) {
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}
function addEventToId(id, evType, fn) {
	addEvent(document.getElementById(id), evType, fn);
}
function setStyleById(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}
function externalLinks() {//makes any link with rel="external" behave as target="_blank"
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "external" || anchor.className == "popup") ) {
			anchor.target = "_blank";
			if ( anchor.title == '' || anchor.title == undefined ) {
				anchor.title = "external link";
			}
		}
	}
}
addEvent(window, 'load', externalLinks);//Add externalLinks() to window.onLoad
function checkAll(boxname) {
	var inputs;
	var i;
	var doCheck = true;
	if ( arguments.length > 1 ) {
		doCheck = arguments[1];
	}
	if (!document.getElementsByTagName) return;
	
	inputs = document.getElementsByTagName("input");
	for ( i=0; i < inputs.length; i++ ) {
		if ( inputs[i].type == 'checkbox' && inputs[i].name == boxname ) {
			inputs[i].checked = doCheck;
		}
	}
}


//showNextEntry, setTimer and clearTimer are used to control the ticker on the homepage and wherever else it occurs

function loadHomeTicker() {
	arrIdx = 0;
	showNextEntry(arrIdx);
	setTimer(arrIdx);
}
function loadTrainingTicker() {
	arrIdx = 1;
	showNextEntry(arrIdx);
	setTimer(arrIdx);
}
		
function showNextEntry(arrIdx) {

	//initialize locals (see /layouts/Home.cfc for other ticker intializations)
	NextContent[arrIdx] = "";
	EntryHTML[arrIdx] = "";
	//load them up and display the ticker if there is anything in the ticker array
	if (arrTickerContent[arrIdx].length > 0) {
		NextContent[arrIdx] = arrTickerContent[arrIdx][NextEntry[arrIdx]];
	
		if (NextContent[arrIdx].length > 0)
			EntryHTML[arrIdx] = NextContent[arrIdx];
		
		if (arrTickerContent[arrIdx].length > 1) { //no need to loop if only one entry
			//move to the next entry or start from the top if we're at the end of the array
			if (NextEntry[arrIdx] < arrTickerContent[arrIdx].length - 1)
				NextEntry[arrIdx] = NextEntry[arrIdx] + 1;
			else
				NextEntry[arrIdx] = 0;
		} else {
			clearTimer(arrIdx);
		}
	} else { //clear the timer if the ticker is empty
		clearTimer(arrIdx);
	}
	
	//display the ticker
	document.getElementById(TickerElement[arrIdx]).innerHTML = EntryHTML[arrIdx];
}

function showNextHome() {
	showNextEntry(0);
}
function showNextTraining() {
	showNextEntry(1);
}
function setTimer(arrIdx) {
//Don't really like this solution, but setInterval will not let you pass an argument with the function called.
	if (arrIdx == 0)
		TimerID[arrIdx] = setInterval( "showNextHome()", 10000 );
	if (arrIdx == 1)
		TimerID[arrIdx] = setInterval( "showNextTraining()", 10000 );
}
function clearTimer(arrIdx) {
	clearInterval ( TimerID[arrIdx] );
}