addEvent(window, "load", alternate_init);

function getElementsByClass(node,searchClass,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;
}

function alternate_init() {
	// Find all items with class sortable and make them sortable
	if (!document.getElementsByTagName) return;

    //alternate list items
	items = getElementsByClass(document,"alternate_rows", "*");
    alternate_items(items, "li");

    // Find all tables with class sortable and make them sortable
	if (!document.getElementsByTagName) return;
    var tbls;
    var ti;
	tbls = document.getElementsByTagName("table");
	for (ti=0;ti<tbls.length;ti++) {
		thisTbl = tbls[ti];
		if ((' '+thisTbl.className+' ').indexOf("alternate_rows") != -1) {
			alternate_table(thisTbl);
		}
	}
}

function alternate_items(items,subElement) {
	items = getElementsByClass(document,"alternate_rows", "*");
    var thisItem;
    var item;
	for (item=0;item<items.length;item++) {
		thisItem = items[item];
		if ((' '+thisItem.className+' ').indexOf("alternate_rows") != -1) {
			alternate(thisItem);
		}
	}
}

function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,	NS6 and Mozilla
// By Scott Andrew
{
	if (elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent){
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	} else {
		alert("Handler could not be removed");
	}
} 

function replace(s, t, u) {
  /*
  **  Replace a token in a string
  **    s  string to be processed
  **    t  token to be found and removed
  **    u  token to be inserted
  **  returns new String
  */
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + u;
  if ( i + t.length < s.length)
    r += replace(s.substring(i + t.length, s.length), t, u);
  return r;
}

function alternate(list) {
	// Take object list and get all it's tbodies.
	var listItems = list.getElementsByTagName("li");
    // Loop through these items
    for (var i = 0; i < listItems.length; i++) {
        // Check if i is even, and apply classes for both possible results
        if ( (i % 2) == 0  ) {
            if (listItems[i].className == 'odd' || !(listItems[i].className.indexOf('odd') == -1) ) {
                listItems[i].className = replace(listItems[i].className, 'odd', 'even');
            } else {
                listItems[i].className += " even";
            }
        } else {
            if (listItems[i].className == 'even' || !(listItems[i].className.indexOf('even') == -1) ) {
                listItems[i].className = replace(listItems[i].className, 'even', 'odd');
            }
            listItems[i].className += " odd";
        } 
    }
}

function alternate_table(table) {
	// Take object table and get all it's tbodies.
	var tableBodies = table.getElementsByTagName("tbody");
	// Loop through these tbodies
	for (var i = 0; i < tableBodies.length; i++) {
		// Take the tbody, and get all it's rows
		var tableRows = tableBodies[i].getElementsByTagName("tr");
		// Loop through these rows
		for (var j = 0; j < tableRows.length; j++) {
			// Check if j is even, and apply classes for both possible results
			if ( (j % 2) == 0  ) {
				if (tableRows[j].className == 'odd' || !(tableRows[j].className.indexOf('odd') == -1) ) {
					tableRows[j].className = replace(tableRows[j].className, 'odd', 'even');
				} else {
					tableRows[j].className += " even";
				}
			} else {
				if (tableRows[j].className == 'even' || !(tableRows[j].className.indexOf('even') == -1) ) {
					tableRows[j].className = replace(tableRows[j].className, 'even', 'odd');
				}
				tableRows[j].className += " odd";
			} 
		}
	}
}


