function clearKids(theElement) {
	while (theElement.firstChild) {
		theElement.removeChild(theElement.firstChild);
	}
}
function startShow() {
	if (!document.getElementsByTagName || (!document.getElementById || (!document.createElement || !document.createTextNode))) return false;
	tableSort();
}
function numSort(a,b) {
	return a-b;
}
function doSort(theArray,isAscending) {
	var newArray = new Array();
	var rawArray = theArray;
	var doNumSort = true;
	for (var i=0; i<rawArray.length; i++) {
		var thisElement = rawArray[i];
		if (isNaN(thisElement)) {
			doNumSort = false;
			break;
		} else {
			thisElement -= 0; 
		}
	}
	if (!doNumSort) {
		newArray = rawArray.sort();
	} else {
		newArray = rawArray.sort(numSort);
	}
	if (!isAscending) {
		return newArray;
	} else {
		return newArray.reverse();
	}
}
function tableSort() {
	var theTables = document.getElementsByTagName("table");
	for (var i=0; i<theTables.length; i++) {
		var thisTable = theTables[i];
		if (/sorTable/.test(thisTable.className)) {
			var theTHeads = thisTable.getElementsByTagName("th");
			for (var j=0; j<theTHeads.length; j++) {
				var thisHead = theTHeads[j];
				var thisHeadLabel = thisHead.firstChild.data;
				clearKids(thisHead);
				var thisLink = document.createElement("a");
				thisLink.setAttribute("href", "#");
				thisLink.setAttribute("title",j);
				//thisLink.setAttribute("title","sort table by "+thisHeadLabel.toLowerCase());
				thisLink.onclick = function() {sortTableBy(this); return false;};
				thisLink.appendChild(document.createTextNode(thisHeadLabel));
				thisHead.appendChild(thisLink);
			}
			stripeTable(thisTable);
		}
	}
}
function sortTableBy(theLink) {
	var theIndex = theLink.title.charAt(theLink.title.length - 1);
	theIndex -= 0;
	var theTable = theLink.parentNode.parentNode.parentNode.parentNode;
	var theSortDirection = showSortedHeader(theTable,theIndex);
	var theTableBody = theTable.getElementsByTagName("tbody");
	for (var i=0; i<theTableBody.length; i++) {
		var thisBody = theTableBody[i];
		var theRows = thisBody.getElementsByTagName("tr");
		var rowArray = new Array();
		var keyArray = new Array();
		for (var j=0; j<theRows.length; j++) {
			var thisRow = theRows[j];
			var theCells = thisRow.getElementsByTagName("td");
			for (var k=0; k<theCells.length; k++) {
				var thisCell = theCells[k];
				var theKey;
				if (k == theIndex) {
					thisCell.className = "activeColumn";
					theKey = thisCell.firstChild.data;
					if (theKey == undefined) {
						theKey = thisCell.firstChild.firstChild.data;
					}
				} else {
					thisCell.className = "";
				}
			}
			theKey = theKey.toLowerCase();
			rowArray.push(thisRow);
			keyArray.push(theKey);
		}
		keyArray = doSort(keyArray,theSortDirection);
		clearKids(thisBody);
		for (var j=0; j<keyArray.length; j++) {
			var thisKey = keyArray[j];
			for (var k=0; k<rowArray.length; k++) {
				var thisRow = rowArray[k];
				var theCells = thisRow.getElementsByTagName("td");
				var thisCell = theCells[theIndex];
				var thisValue = thisCell.firstChild.data;
				if (thisValue == undefined) {
					thisValue = thisCell.firstChild.firstChild.data;
				}
				if (thisValue.toLowerCase() == thisKey.toString()) {
					thisBody.appendChild(thisRow);
				}
			}
		}
		stripeTable(theTable);
	}
}
function showSortedHeader(theTable,theIndex) {
	var theTHeads = theTable.getElementsByTagName("th");
	var ascendingSort = false;
	for (var i=0; i<theTHeads.length; i++) {
		var thisHead = theTHeads[i];
		if (i == theIndex) {
			if (/descendingSort/.test(thisHead.className)) {
				ascendingSort = true;
				thisHead.className = "activeHeader";
			} else {
				thisHead.className = "activeHeader descendingSort";
			}
		} else {
			thisHead.className = "";
		}
	}
	return ascendingSort;
}
function stripeTable(theTable) {
        var theBody = theTable.getElementsByTagName("tbody")[0];
        var theRows = theBody.getElementsByTagName("tr");
	for (var i=0; i<theRows.length; i++) {
                var thisRow = theRows[i];
		if (i%2 == 1) {
		        //addClass(thisRow,'rowOdd');
		        thisRow.className = 'rowOdd';
		} else {
		        //addClass(thisRow,'rowEven');
		        thisRow.className = 'rowEven';
                }
        }
}
//the below function is based on Scott Andrew's work
//http://www.scottandrew.com/weblog/articles/cbs-events
function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}
addEvent(window, 'load', startShow);
