<!--

/*******************************************************************************
Contains functions to sort tables from row headers.  
Provided by Community MX by Rob Williams. Usage articls and extension located @
http://www.communitymx.com/content/article.cfm?cid=F5C6B


Modified 6/22/05 by Adrian Showalter @ Holmes BizNet
www.holmesbiznet.com
******************************************************************************/

function sortStringFunc(a, b) {
	if (a["key"].toUpperCase() > b["key"].toUpperCase()) {
		return 1;
	} else if (a["key"].toUpperCase() < b["key"].toUpperCase()) {
		return -1;
	} else {
		return 0;
	}

}

function sortNumericFunc(a, b) {
	a = parseFloat(a["key"]);
	b = parseFloat(b["key"]);
	if (a > b) {
		return 1;
	} else if (a < b) {
		return -1;
	} else {
		return 0;
	}
}

function sortByColumn (whichColumn) {
	
	//Only try to sort the table if the browser supports the W3C DOM 
	//(which the behavior relies on to do all the sorting work)
	
	if (document.getElementById) {
		//Get a reference to the table itself.
		workingTable = whichColumn.parentNode;
		while (workingTable.tagName != "TABLE") {
			workingTable = workingTable.parentNode;
		}
		
		//Get the table body
		workingTable = workingTable.lastChild;
		
		
		var tableRows = workingTable.getElementsByTagName("tr");
		
		//We can determine how many children in the given column is by comparing the given node with each of the child nodes
		//in the header row (row 0)
		var headerCells = tableRows[0].getElementsByTagName("th")
		
		for (var i=0; i < headerCells.length; i++) {
			if (headerCells[i] == whichColumn) {
				var columnIndex = i;
				
				//get the sort order from the attribute of the header
				var sortOrder = headerCells[i].getAttribute("sortOrder");
				if (sortOrder != 1 && sortOrder != -1) {
					//No sort order defined
					sortOrder = -1;
				}
				//invert the current sort order
				sortOrder = (sortOrder * -1)
				//Update the attribute
				headerCells[i].setAttribute("sortOrder", sortOrder);
				break;
			}
		}
	
	
		//Okay, let's build an array containing A) the key value to sort by, and B) a reference to the entire row
		var rowsArray = new Array();
		
		//Define a default sort function
		var sortFunc = sortStringFunc;
		
		//flag to determine if any alpha-numeric value is present in the column
		var isAlpha = false;
		
		var isNumeric = false;
		
		for (var i = 1; i < tableRows.length; i++) {
			var thisRowArr = new Array();
			
			var theseCells = tableRows[i].getElementsByTagName("td");
			
			//set the value to sort by to false by default.
			var thisKeyValue = false;
			
			//check to see if a sortOrder attribute has been set on the td first
			//(this overrides any other sorting methods)
			if (theseCells[columnIndex].getAttribute("sortOrder") != null) {
				thisKeyValue = theseCells[columnIndex].getAttribute("sortOrder");
			} else {
				//No sortOrder attribute has been set, so try to determine the contents of the cell in order to sort it.
				
					
				//Make sure that the table cell actually contains information
				if (theseCells[columnIndex].hasChildNodes()) {
					
					if (theseCells[columnIndex].firstChild.nodeType == 3 && theseCells[columnIndex].getElementsByTagName("a").length == 0) {
						//The table cell contains text first, so sort by that
						thisKeyValue = theseCells[columnIndex].firstChild.nodeValue;
					
					} else if (theseCells[columnIndex].getElementsByTagName("a").length > 0) {
						//The table cell contains at least one hyperlink, so sort by the link text
						thisKeyValue = theseCells[columnIndex].getElementsByTagName("a").item(0).firstChild.nodeValue;
					}
					
				}
				
			}
			
			//Only add the row to the list of sortable ones if a value to sort by could be found
			if (thisKeyValue != false) {
			
				thisRowArr["key"] = thisKeyValue;
				thisRowArr["node"] = tableRows[i];
				
				rowsArray.push(thisRowArr);
				
				
				//use this value to set the sort function
				if (thisKeyValue.match(/^[\d\.]+$/)) {
					isNumeric = true;
					//sortFunc = sortNumericFunc;
				} else {
					isAlpha = true;
				}
			}
		}
	
		if (isAlpha) {
			sortFunc = sortStringFunc;
		} else {
			if (isNumeric) {
				sortFunc = sortNumericFunc;
			}
		}
		
		//Now all we have to do is sort the array using a custom sort function
		rowsArray.sort(sortFunc);
		
		
		//Now output the rows in order
		
		if (sortOrder == 1) {
			for (var j=0; j < rowsArray.length; j++) {
				
				workingTable.appendChild(rowsArray[j]["node"]);
			}
		} else {
			//Output desc
			for (var j= rowsArray.length - 1; j >= 0; j--) {
				workingTable.appendChild(rowsArray[j]["node"]);
				
			}
		}
	}

}

//-->