/*
* Common Javascript Functions
*
* @author	Duncan Mundell <duncan@lantech.co.nz>
* @version	1.0
* @updated	01/03/07
*/

/*
*	A List Apart Zebra Striped Tables
*	Thanks and recognition to ALAA @ http://www.alistapart.com/articles/zebratables
*/
// this function is needed to work around 
// a bug in IE related to element attributes
function hasClass(obj) {
 var result = false;
 if (obj.getAttributeNode("class") != null) {
	 result = obj.getAttributeNode("class").value;
 }
 return result;
}   

function stripe() {
	
	// the flag we'll use to keep track of 
	// whether the current row is odd or even
	var even = false;
	
	// set the alternating CSS Classes for the rows
	var evenCssClass = 'bglight';
	var oddCssClass = 'bgdark';
	
	// obtain a reference to all the tables in the page, if no tables are found abort
	var thetables = document.getElementsByTagName('table');

	if (! thetables) { return; }
	for (var t = 0; t < thetables.length; t++) {

		// by definition, tables can have more than one tbody
		// element, so we'll have to get the list of child
		// <tbody>'s 
		var tbodies = thetables[t].getElementsByTagName("tbody");
		
		// and iterate through them...
		for (var h = 0; h < tbodies.length; h++) {
		
			// find all the &lt;tr&gt; elements... 
			var trs = tbodies[h].getElementsByTagName("tr");
			
			// ... and iterate through them
			for (var i = 0; i < trs.length; i++) {
			
				// avoid rows that have a class attribute
				// or backgroundColor style
				if (! hasClass(trs[i]) && ! trs[i].style.backgroundColor) {
				
					// get all the cells in this row...
					var tds = trs[i].getElementsByTagName("td");
					
					// and iterate through them...
					for (var j = 0; j < tds.length; j++) {
						
						var mytd = tds[j];
						
						// avoid cells that have a class attribute
						// or backgroundColor style
						if (! hasClass(mytd) && ! mytd.style.backgroundColor) {
							mytd.className = even ? evenCssClass : oddCssClass;
						}
					}
				}
				// flip from odd to even, or vice-versa
				even =  ! even;
			}
		}
	}
}


/*
*	Add Event Code
*	Thanks to http://simonwillison.net/2004/May/26/addLoadEvent/
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	
	else {
		window.onload = function() {
		if (oldonload) {
			oldonload();
		}
	
		func();
		}
	}
}

addLoadEvent(stripe);
