/* Table-sorting function adapted (w/changes noted) from http://www.faqts.com/knowledge_base/view.phtml/aid/2098/fid/192 */
function createRowsArray (table) {
	var rows = new Array();
	var r = 0;
	if (table.tHead == null && table.tFoot == null) {
		for (var r1 = 0; r1 < table.rows.length; r1++, r++) {
			rows[r] = table.rows[r1];
		}
	} else {
		for (var t = 0; t < table.tBodies.length; t++) {
			for (var r1 = 0; r1 < table.tBodies[t].rows.length; r1++, r++) {
				rows[r] = table.tBodies[t].rows[r1];
			}
		}
	}
	return rows;
}
function insertSortedRows(table, rows) {
	if (document.all) { var rowsCopy = new Array(rows.length); }
	for (var r = 0; r < rows.length; r++) {
		if (document.all) { rowsCopy[r] = rows[r].cloneNode(true); }
		table.deleteRow(rows[r].rowIndex);
	}
	var tableSection = table.tBodies[table.tBodies.length - 1];
	for (var r = 0; r < rows.length; r++) {
		var row = document.all ? rowsCopy[r] : rows[r];
		tableSection.appendChild(row);
	}
}
function sortTable (table, sortFunc) {
	// Added object detection tests
	if (document.getElementById) {
		var rows = createRowsArray(table);
		if (rows.length > 0) {
			rows.sort(sortFunc);
			insertSortedRows(table, rows);
		}
	}
}
function sortRowsAlpha (row1 , row2) {
	if (document.getElementById ) {
		var column = sortRowsAlpha.col;
		var cell1 = row1.cells[column].firstChild.nodeValue;
		var cell2 = row2.cells[column].firstChild.nodeValue;
		// Next 4 lines added for cells with links, which have a value of null at this point
		if (cell1 == null) 
			cell1 = findFirstLinkChild(row1.cells[column]).innerText;
		if (cell2 == null)
			cell2 = findFirstLinkChild(row2.cells[column]).innerText;
		return cell1 < cell2 ? - 1 : (cell1 == cell2 ? 0 : 1);
	}
}
function sortRowsNumber (row1 , row2) {
	if (document.getElementById ) {
		var column = sortRowsNumber.col;
		// Next 4 lines added; start by removing commas
		cell1=row1.cells[column].firstChild.nodeValue.replace(/\,/,'');
		cell2=row2.cells[column].firstChild.nodeValue.replace(/\,/,'');
		var cell1 = parseInt(cell1);
		var cell2 = parseInt(cell2);
		return cell2 < cell1 ? - 1 : (cell2 == cell1 ? 0 : 1);
	}
}
function sortRowsLink (row1 , row2) {
	var column = sortRowsLink.col;
	var cell1 = findFirstLinkChild(row1.cells[column]).href;
	var cell2 = findFirstLinkChild(row2.cells[column]).href;
	return cell1 < cell2 ? - 1 : (cell1 == cell2 ? 0 : 1);
}
function findFirstLinkChild (el) {
	var child = el.firstChild;
	while (child.tagName != 'A') {
		 child = child.nextSibling;
	}
	return child;
}
function jsSortTable(table, col) {
	sortRowsAlpha.col = col;
	sortTable(table, sortRowsAlpha);
}
function jsSortTableNumerical (table, col) {
	sortRowsNumber.col = col;
	sortTable(table, sortRowsNumber);
}
function jsSortTableLinks (table, col) {
	sortRowsLink.col = col;
	sortTable(table, sortRowsLink);
}
function findTableParent (node) {
	if (document.getElementById ) {
		while (node.tagName.toUpperCase() != 'TABLE') {
    		node = node.parentNode;
		}
		return node;
	}
}
// Added: document.write() the instructions
if (document.getElementById) {
	document.write('<p><b>To sort the table</b> by a particular column, click on ' 
	+ 'one of the headings at the top of the table. '
	+ 'If your computer is slow, allow a few seconds for sorting.</p>');
}
