function CalcSplits( tableId )
{ 
	var tbl = document.getElementById(tableId);
	tbl.setAttribute("onclick","sortColumn(event)");
	var tbl = tbl.rows;
	
	var tr = null;
	var trCt = tbl.length;
	//var nTblColCt = parseInt(tbl.getAttribute("cols"));
	//tbl.setAttribute("cols", (nTblColCt + 2) + "");
	var trStart = 0 // ignoring TH tags tdCt - 4; // ti me 1, time 2, time 3
	for (var i = trStart; i < trCt; ++i) 
	{
		tr = tbl[i].cells;
		if (0 == i) // 1st header ... titles
		{
			var tdTitle = document.createElement("td");
			tdTitle.innerHTML = "Splits";
			tdTitle.setAttribute("class","split");
			tdTitle.setAttribute("style","border: none");
			tdTitle.setAttribute("colspan","2");
			tbl[i].appendChild(tdTitle);
			continue;
		}
		else if (1 == i) // 2nd header, col headers
		{
			var thBike = document.createElement("td");
			thBike.innerHTML = "Bike";
			thBike.setAttribute("class","split");
			var thRun = document.createElement("td");
			thRun.innerHTML = "Run";
			thRun.setAttribute("class","split");
			tbl[i].appendChild(thBike);
			tbl[i].appendChild(thRun);
			
			var thCt = tr.length;
			for (var j = 0; j < thCt; j++) 
			{
				tr[j].setAttribute("type", j < 3 || j > 4 ? "Number" : "CaseInsensitiveString");
				tr[j].setAttribute("onmouseover", "this.style.cursor='pointer'");
				tr[j].setAttribute("title", "Click to Sort by - '" + tr[j].innerHTML + (j >= thCt-2 ? " Split'" : "'"));
			}
			continue;
		}
		else
		{
			// Do whatever you want with each ROW here.
			var td = null;
			var tdCt = tr.length;
			var tdStart = tdCt - 4; // tdCt - 4; ie name, time 1, time 2, time 3

			// get the 'Name' cell index
			var tdName = tr[tdStart];
			// get the times from the td tags, while we are here and iterating, we can apply the class="tm"
			var strTdTime1 = tr[++tdStart].innerHTML; // swim ... cumulative 1
			tr[tdStart].setAttribute("class","tm");
			var strTdTime2 = tr[++tdStart].innerHTML;// bike ... cumulative 2
			tr[tdStart].setAttribute("class","tm");
			var strTdTime3 = tr[++tdStart].innerHTML;// run ... cumulative 3
			tr[tdStart].setAttribute("class","tm");
			
			if (strTdTime1.indexOf(":") < 0) { if (strTdTime1.indexOf(".") < 2 ) {strTdTime1 = "0" + strTdTime1;} strTdTime1 = "0:" + strTdTime1;}
			if (strTdTime2.indexOf(":") < 0) { if (strTdTime2.indexOf(".") < 2 ) {strTdTime2 = "0" + strTdTime2;} strTdTime2 = "0:" + strTdTime2;}
			if (strTdTime3.indexOf(":") < 0) { if (strTdTime3.indexOf(".") < 2 ) {strTdTime3 = "0" + strTdTime3;} strTdTime3 = "0:" + strTdTime3;}
			
			strTdTime1 = strTdTime1.replace(".", ":"); // swim ... cumulative 1
			strTdTime2 = strTdTime2.replace(".", ":");// bike ... cumulative 2
			strTdTime3 = strTdTime3.replace(".", ":");// run ... cumulative 3
			
			var strDateBase = "1980/01/01 ";
			var strDateBase1 = strDateBase + strTdTime1;
			var strDateBase2 = strDateBase + strTdTime2;
			var strDateBase3 = strDateBase + strTdTime3;
			
			var datSplitBike = new Date((Date.parse(strDateBase2) - Date.parse(strDateBase1)));
			var datSplitRun = new Date((Date.parse(strDateBase3) - Date.parse(strDateBase2)));
			
			var strSplitBike = "NaN" == datSplitBike.getUTCHours() + "" ? "-" : (datSplitBike.getUTCHours() + ":" + (datSplitBike.getMinutes() > 9 ? datSplitBike.getMinutes() : "0" + datSplitBike.getMinutes()) + ":" + (datSplitBike.getSeconds() > 9 ? datSplitBike.getSeconds() : "0" + datSplitBike.getSeconds()));
			var strSplitRun = "NaN" == datSplitRun.getUTCHours() + "" ? "-" : (datSplitRun.getUTCHours() + ":" + (datSplitRun.getMinutes() > 9 ? datSplitRun.getMinutes() : "0" + datSplitRun.getMinutes()) + ":" + (datSplitRun.getSeconds() > 9 ? datSplitRun.getSeconds() : "0" + datSplitRun.getSeconds()));
			//document.writeln("swim.time: " + strTdTime1 + " [" + strDateBase1 + " - " + Date.parse(strDateBase1) + "]&nbsp;&nbsp;bike.time: " + datSplitBike.toLocaleTimeString() + "&nbsp;&nbsp;Run.time: " + strTdTime3 + "<br/>");
			//document.writeln("Bike.Split: " + strSplitBike + "&nbsp;&nbsp;&nbsp;&nbsp;Run.Split: " + strSplitRun + "<br/>");
			
			var tdBike = document.createElement("td");
			tdBike.innerHTML = strSplitBike;
			tdBike.setAttribute("class","split");
			var tdRun = document.createElement("td");
			tdRun.innerHTML = strSplitRun;
			tdRun.setAttribute("class","split");
			tbl[i].appendChild(tdBike);
			tbl[i].appendChild(tdRun);
			
			// setup the row highlighting event and pointer type
			var	trRow = tbl[i];
			trRow.setAttribute("onclick","HighlighRow(event)")
			tdName.setAttribute("onmouseover", "this.style.cursor='pointer'");
			tdName.setAttribute("title", "- Click to Highlight Row -");
		}
	}	
}
function getParent(el, pTagName) 
{
	if (el == null) return null;
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())	// Gecko bug, supposed to be uppercase
		return el;
	else
		return getParent(el.parentNode, pTagName);
}
function HighlighRow(e)
{
	var tmp = e.target ? e.target : e.srcElement;
	var tHeadRow = getParent(tmp, "TR");
	//var el = getParent(tmp, "TD");

	if (null == tHeadRow)
	{
		return;
	}
	else
	{
		var strAtt = "class"
		var strAttVal = tHeadRow.getAttribute(strAtt);
		if (null == strAttVal || "" == strAttVal)
		{
			tHeadRow.setAttribute(strAtt,"highlight");
		}
		else
		{
			tHeadRow.setAttribute(strAtt,"");
		}
	}
}



var dom = (document.getElementsByTagName) ? true : false;
var ie5 = (document.getElementsByTagName && document.all) ? true : false;
var arrowUp, arrowDown;

if (ie5 || dom)
	initSortTable();

function initSortTable() 
{
	var tn; 
	arrowUp = document.createElement("SPAN");
	if (ie5) 
	{
		tn = document.createTextNode(" 6"); // original appendage - for webdings font (MS)
		arrowUp.className = "arrow";
	}
	else 
	{
		tn = document.createTextNode(" v"); // for other browsers
	}
	arrowUp.appendChild(tn);

	arrowDown = document.createElement("SPAN");
	if (ie5) 
	{
		tn = document.createTextNode(" 5"); // original appendage - for webdings font (MS)
		arrowDown.className = "arrow";
	}
	else 
	{
		tn = document.createTextNode(" ^"); // for other browsers
	}
	arrowDown.appendChild(tn);

/*  must add the section below, a css class, to your style sheet
.arrow	{ 
	font-family: webdings; 
	color: yellow; 
	font-size: 20px;
}
*/
}



function sortTable(tableNode, nCol, bDesc, sType) 
{
	var tBody = tableNode.tBodies[0];
	var trs = tBody.rows;
	var trl= trs.length;
	var a = new Array();
	
	for (var i = 0; i < trl; i++) {
		a[i] = trs[i];
	}
	
	var start = new Date;
	window.status = "Sorting data...";
	a.sort(compareByColumn(nCol,bDesc,sType));
	window.status = "Sorting data done";
	
	for (var i = 0; i < trl; i++) 
	{
		tBody.appendChild(a[i]);
		window.status = "Updating row " + (i + 1) + " of " + trl +
						" (Time spent: " + (new Date - start) + "ms)";
	}
	
	// check for onsort
	if (typeof tableNode.onsort == "string")
		tableNode.onsort = new Function("", tableNode.onsort);
	if (typeof tableNode.onsort == "function")
		tableNode.onsort();
}

function CaseInsensitiveString(s) 
{
	return String(s).toUpperCase();
}

function parseDate(s) 
{
	return Date.parse(s.replace(/\-/g, '/'));
}

/* alternative to number function
 * This one is slower but can handle non numerical characters in
 * the string allow strings like the follow (as well as a lot more)
 * to be used:
 *    "1,000,000"
 *    "1 000 000"
 *    "100cm"
 */

function toNumber(s) {
	var strIn = s.replace(/[\*\-\NT]/g, "999999999")
    return Number(strIn.replace(/[^0-9^\.]/g, ""));
}
function toHexNumber(s) {
    return parseInt(s, 16);
}

function compareByColumn(nCol, bDescending, sType) {
	var c = nCol;
	var d = bDescending;
	
	var fTypeCast = String;
	
//	if (sType == "Nil");
//		return;
	if (sType == "Number")
//		fTypeCast = Number;
		fTypeCast = toNumber;
	else if (sType == "HexNumber")
		fTypeCast = toHexNumber;
	else if (sType == "Date")
		fTypeCast = parseDate;
	else if (sType == "CaseInsensitiveString")
		fTypeCast = CaseInsensitiveString;

	return function (n1, n2) 
	{
		if (fTypeCast(getInnerText(n1.cells[c])) < fTypeCast(getInnerText(n2.cells[c])))
			return d ? -1 : +1;
		if (fTypeCast(getInnerText(n1.cells[c])) > fTypeCast(getInnerText(n2.cells[c])))
			return d ? +1 : -1;
		return 0;
	};
}

function sortColumnWithHold(e) 
{
	// find table element
	var el = ie5 ? e.srcElement : e.target;
	var table = getParent(el, "TABLE");
	
	// backup old cursor and onclick
	var oldCursor = table.style.cursor;
	var oldClick = table.onclick;
	
	// change cursor and onclick	
	table.style.cursor = "wait";
	table.onclick = null;
	
	// the event object is destroyed after this thread but we only need
	// the srcElement and/or the target
	var fakeEvent = {srcElement : e.srcElement, target : e.target};
	
	// call sortColumn in a new thread to allow the ui thread to be updated
	// with the cursor/onclick
	window.setTimeout(function () 	{
										sortColumn(fakeEvent);
										// once done resore cursor and onclick
										table.style.cursor = oldCursor;
										table.onclick = oldClick;
									}
									, 200);
}

function sortColumn(e) 
{
	var tmp = e.target ? e.target : e.srcElement;
	var tHeadParent = getParent(tmp, "THEAD");
	var el = getParent(tmp, "TD");

	if (tHeadParent == null)
		return;
		
	if (el != null) 
	{
		var p = el.parentNode;
		var i;

		// typecast to Boolean
		el._descending = !Boolean(el._descending);

		if (tHeadParent.arrow != null) 
		{
			if (tHeadParent.arrow.parentNode != el) 
			{
				tHeadParent.arrow.parentNode._descending = null;	//reset sort order		
			}
			tHeadParent.arrow.parentNode.removeChild(tHeadParent.arrow);
		}

		if (el._descending)
			tHeadParent.arrow = arrowUp.cloneNode(true);
		else
			tHeadParent.arrow = arrowDown.cloneNode(true);

		el.appendChild(tHeadParent.arrow);

		// get the index of the td
		var cells = p.cells;
		var l = cells.length;
		for (i = 0; i < l; i++) 
		{
			if (cells[i] == el) break;
		}

		var table = getParent(el, "TABLE");
		// can't fail
		
		sortTable(table,i,el._descending, el.getAttribute("type"));
	}
}


function getInnerText(el) {
	if (ie5) return el.innerText;	//Not needed but it is faster
	
	var str = "";
	
	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //ELEMENT_NODE
				str += getInnerText(cs[i]);
				break;
			case 3:	//TEXT_NODE
				str += cs[i].nodeValue;
				break;
		}
	}
	return str;
}

