

/* Function to addEventListener to onload
 * @param func - a function which should be executed once the page has loaded
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * it will work even if something has previously been assigned to window.onload
 * without using addLoadEvent itself. 
*/

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

/*
 * Function to clear a text field
 * @param thefield
 *     the field to clear
 */

function cleartext(thefield) {
	if (thefield.defaultValue == thefield.value) {
		thefield.value = "";
	}
}

/*
 * Function to change the URL via a select menu
 * @param elem
 *     the ID of the form to select box
 */

function changeurl(elem) {
	for (i = 0; i < elem.length; i++) {
		if(elem.options[i].selected == true) {
			window.location = elem.options[i].value;
		}
	}
}

/*
 * Function to open popup window
 * @param url
 *     The url to open
 * @param win_width
 *     The window width
 * @param win_height
 *     The window height
*/

function openWin(url, win_width, win_height) {
		
	win_left = (screen.width - win_width) / 2;
	win_top = (screen.height - win_height) / 2;
	
	window.open(url , '', 'width='+win_width+', height='+win_height+', left='+win_left+', top='+win_top+', resizable=1, scrollbars=1');
}

/*
 * Function to show/hide an element by ID
 * @param hide_elem
 *     returns the ID of the element to hide
 */
 
function show_hide_id(hide_elem) {
	
	elem_to_hide = document.getElementById(hide_elem);
	
	if (elem_to_hide != null) { // ensure the list exists...
		if (elem_to_hide.style.display == 'none') {
			elem_to_hide.style.display = 'block'; // show it
		}
		else {
			elem_to_hide.style.display = 'none'; // hide it
		}
	}
}

/*
 * Function for image rollovers
 * @param elem
 *     returns the anchor element
 */ 
function swap(root, elem) {
	if(root == '/')
		root = '';
	cur_src = elem.firstChild.src;
	cur_src = cur_src.substring(cur_src.lastIndexOf('/')+1);

	if (cur_src.indexOf('h_') == -1 && cur_src.indexOf('a_') == -1) {
		elem.firstChild.src = root + '/i/nav/' + 'h_' + cur_src;
		
	}
	else if(cur_src.indexOf('a_') == -1){
		elem.firstChild.src = root + '/i/nav/' + cur_src.substring(2);
	}
}
/*
 * Function to show/hide elements for recurrance scheduler
 * @param elem
 *     returns the element to show
 */
 
function show_recurrance(elem) {
	
	elem_to_show = document.getElementById(elem);
	
	// start by hiding all elements
	document.getElementById('daily').style.display = 'none';
	document.getElementById('weekly').style.display = 'none';
	document.getElementById('monthly').style.display = 'none';
	
	elem_to_show.style.display = 'block'; // show it
}

/*
 * Function to show one element while hiding others in an array
 * Using this on the tabbed sub menu control
 */

function show_hide_group(show_elem,arr_to_hide,arr_tabs_to_hide,tab_to_show) {
	
	var show_this = document.getElementById(show_elem);
	
	if (show_this != null) {
		if (show_this.style.display == 'none') {
			show_this.style.display = '';
			tab_to_show.className = 'selected';
		}
	}
	
	//loop through items to hide them
	for (var i = 0; i < arr_to_hide.length; i++) {
		var hide_this = document.getElementById(arr_to_hide[i]);
		if (hide_this != null) {
			if (hide_this.style.display == '') {
				hide_this.style.display = 'none';
			}
		}
	}
	
	//loop through tabs to hide them
	for (var i = 0; i < arr_tabs_to_hide.length; i++) {
		var hide_this = document.getElementById(arr_tabs_to_hide[i]);
		if (hide_this != null) {
			if (hide_this.className == 'selected') {
				hide_this.className = '';
			}
		}
	}
}


//This function is used to display the News by default
//on all the news pages. It's called in the html in a script block,
//and calls the show_hide_group function above when the page is loaded.
function ShowNewsTab() {
	var newsBlock = document.getElementById('news_tab');
	var	hide_these = new Array('blog_box','action_box');
	var hide_tabs = new Array('action_tab','blog_tab');
	show_hide_group('news_box',hide_these,hide_tabs,newsBlock);
}

//This function is used to display the Blog tab by default
//on all the blog pages. It's called in the html in a script block,
//and calls the show_hide_group function above when the page is loaded.
function ShowBlogTab() {
	var blogBlock = document.getElementById('blog_tab');
	var	hide_these = new Array('news_box','action_box');
	var hide_tabs = new Array('action_tab','news_tab');
	show_hide_group('blog_box',hide_these,hide_tabs,blogBlock);
}


/* FOR HANDLING DROP-DOWN MENUS WITH IE */

/*
 * Function to fix IE bug where <select> elements go over dropdowns
 * @param elem
 *     the element hovered over (link)
 * @param state
 *     the state of the hover (true for hover, false for unhover)
 */

function dropdownFix(elem, state) {
	ul = elem.parentNode.getElementsByTagName('ul')[0];
	if(ul != null)
	{
		navElem = document.getElementById('tabs');
		iframe = document.getElementById('iefix');
		if (state) {
			iframe.style.width = ul.offsetWidth - 7; // width of the ul minus the width of the png shadow
			iframe.style.height = ul.offsetHeight - 8; // height of the ul minus the height of the png shadow
			iframe.style.left = navElem.offsetLeft + elem.parentNode.offsetLeft - 1; // position of the #nav div + position of the li
			iframe.style.top = ul.offsetTop; // position of the ul from the top
			iframe.style.display = "block";
		}
		else {
			iframe.style.display = "none";
		}
	}
}

startList = function() {

 if (document.all && document.getElementById) {
	var navRoot = document.getElementById('tabs');
	for (i=0; i<navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];
		if (node.nodeName == "LI") {
				node.onmouseover = function() {
					this.className += " over";
					dropdownFix(this.firstChild, true);
  				}
  				node.onmouseout = function() {
  					this.className = this.className.replace(" over", "");
					dropdownFix(this.firstChild, false);
   				}
   		}
  	}
 }	
}
window.onload = startList;


//Functions for calculating and displaying client browser's date
function GetDay(nDay) {
	var Days = new Array("Sunday","Monday","Tuesday","Wednesday",
	                     "Thursday","Friday","Saturday");
	return Days[nDay]
}

function GetMonth(nMonth) {
	var Months = new Array("January","February","March","April","May","June",
	                       "July","August","September","October","November","December");
	return Months[nMonth] 	  	 
}

function DateString() {
	var Today = new Date();
	var suffix = "th";
	switch (Today.getDate())
	{
		case 1:
		case 21:
		case 31: 
			suffix = "st"; break;
		case 2:
		case 22:
			suffix = "nd"; break;
		case 3:
		case 23:
			suffix = "rd"; break;
	}

	var strDate = GetDay(Today.getDay()) + ", " + GetMonth(Today.getMonth()) + " " + Today.getDate() + suffix + ", " + Today.getFullYear();
	return strDate;
}
