// script to stripe rows in a table
// based upon a script from
// http://www.alistapart.com/d/stripedtables/script.txt
//
// will skip rows that have either 
//	a class or background color applied to them.
// modified: oct 31 2006		by: mjb

// select elements by class
function stripeClass(cl) {
	var strTables = getElementsByClass(cl,null,'TABLE');
	for (var tb=0; tb<strTables.length; tb++ ) {
		stripeRows(strTables[tb],arguments[1],arguments[2]);
	}
}

// select elements by id
function stripe(id) {
    // obtain a reference to the desired table
    // if no such table exists, abort
    if (document.getElementById) var table = document.getElementById(id);
    else if (document.all) var table = document.all[id];
    // fail silently
    if (table)  stripeRows(table,arguments[1],arguments[2]);
}    

function stripeRows(table) {
    var even = false;
  
    // if arguments are provided to specify the class
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var oddColor = arguments[1] ? arguments[1] : "oddrow";
    var evenColor = arguments[2] ? arguments[2] : "evenrow";
  
    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // <tbody>s 
    var tbodies = table.getElementsByTagName("TBODY");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {

		// find all the <tr> 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 || trs[i].getAttribute("bgcolor"))) {

				trs[i].className = even ? evenColor : oddColor;

				even =  ! even;
    	    }
		}
    }
}

// this function is needed to work around 
// a bug in IE related to element attributes
function hasClass(obj) {
	return  (obj.getAttributeNode("class")) ? obj.getAttributeNode("class").value : false ;
}   

// get elements by class name
// from http://www.dustindiaz.com/top-ten-javascript/
function getElementsByClass(searchClass,node,tag) {
   var classElements = new Array();
   if ( node == null )
      node = document;
   if ( tag == null )
      tag = '*';
   var els = node.getElementsByTagName(tag);
   var elsLen = els.length;
   var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
   for (i = 0, j = 0; i < elsLen; i++) {
      if ( pattern.test(els[i].className) ) {
         classElements[j] = els[i];
         j++;
      }
   }
   return classElements;
}

