//-----------------------------------------------------------------------------
// $Id: sorttable.js,v 1.2 2006/11/20 17:06:55 ghoffman Exp $
// $Source: /bbsrc/web/docs/en/jscommon/sorttable.js,v $
// Description: JavaScript to sort HTML tables
//-----------------------------------------------------------------------------

var lastsorted = 0;
var sortReverse = new Array();

// This code is necessary for browsers that don't reflect the DOM constants
// (like IE).
if (document.ELEMENT_NODE == null) {
 	document.ELEMENT_NODE = 1;
  	document.TEXT_NODE = 3;
}

//-----------------------------------------------------------------------------
// sortTable(id, col, type) is the main function to sort columns of data.
// id = document object id for the rows 
// col = the column number to be sorted 
// type = is the data type (AlphaChars-0), (Numeric-1)
//-----------------------------------------------------------------------------
function sortTable(id, col, type) {

var reverse;
   //check for sort order. if this one was the last clicked,
   //then reverse the sort order
   if (col == lastsorted){
   		sortReverse[col] = !sortReverse[col];
		reverse = sortReverse[col];
	}
   else{	
	reverse = false;
   }
  // Get the table or table section to sort.
  var tableElmt = document.getElementById(id);

  // Set the table display style to "none" - necessary for Netscape 6 
  // browsers.
  var oldDsply = tableElmt.style.display;
  tableElmt.style.display = "none";

  // Sort the rows based on the content of the specified column using a
  // selection sort.

  var i, j;
  var value;
  var tempVal;
  var imageName;
  var numRows = tableElmt.rows.length;
  var sortItems = new Array(numRows); 

 
   for (i=numRows-1; i >= 0 ; i--){
  	sortItems[i] = new Array(2);
	value = getTextValue(tableElmt.rows[i].cells[col]);
	value = value.replace(/\,/g,""); //remove commas before sort
	value = value.replace("N.A.",""); //remove N.A. before sort
	value = value.replace("NaN",""); //remove NaN before sort
		sortItems[i][0] = value; 
	sortItems[i][1] = tableElmt.rows[i]; 
  }
  
  //sort the values in the selected column
  //need to check for numeric values and sort numerically
   if (type == 1){ //sort numbers
   		if (reverse){
			sortItems.sort(sortNumbersRev);
		}
		else{
   			sortItems.sort(sortNumbers);
		}
   }
   else if ((type == 0) && (reverse)){
   		sortItems.sort();
		sortItems.reverse();
   }
   else{
   		sortItems.sort();
   }
  
  tableRoot = tableElmt.parentNode;
  tableElmtClone = tableElmt.cloneNode(false);
  tableRoot.removeChild(tableElmt);
  tableElmt = tableElmtClone;
  tableRoot.appendChild(tableElmt);

  for (i = 0; i < numRows; i++) {
    	tableElmt.appendChild(sortItems[i][1]);
  }
	
  // Format Numbers and Make it look pretty.
  imageName = "image" + col;
  swapImage(reverse, imageName, col);
  makePretty(tableElmt, col);
  
  // Restore the table's display style.
  tableElmt.style.display = oldDsply;
  lastsorted = col;
  
  return false;
}

function sortNumbers(a, b){
//get values from the 2-dimensional array
	return a[0]-b[0];
}

function sortNumbersRev(a, b){
//get values from the 2-dimensional array
	return b[0]-a[0];
}

//-----------------------------------------------------------------------------
// Functions to get and compare values during a sort.
//-----------------------------------------------------------------------------
function getTextValue(el) {

  var i;
  var s;

  // Find and concatenate the values of all text nodes contained within the
  // element.
  s = "";
  for (i = 0; i < el.childNodes.length; i++)
    if (el.childNodes[i].nodeType == document.TEXT_NODE)
      s += el.childNodes[i].nodeValue;
    else if (el.childNodes[i].nodeType == document.ELEMENT_NODE &&
             el.childNodes[i].tagName == "BR")
      s += " ";
    else
      // Use recursion to get text within sub-elements.
      s += getTextValue(el.childNodes[i]);

  return normalizeString(s);
}

// Regular expressions for normalizing white space.
var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
var whtSpMult = new RegExp("\\s\\s+", "g");

function normalizeString(s) {

  s = s.replace(whtSpMult, " ");  // Collapse any multiple whites space.
  s = s.replace(whtSpEnds, "");   // Remove leading or trailing white space.

  return s;
}

//-----------------------------------------------------------------------------
// Functions to update the table appearance after a sort.
//-----------------------------------------------------------------------------

// Style class names.
var rowClsNm = "alternateRow";
var rowClsNmb = "whiteRow";

// Regular expressions for setting class names.
var rowTest = new RegExp(rowClsNm, "gi");

function makePretty(tblEl, col) {
  var i, j;
  var rowEl, cellEl;

  // Set style classes on each row to alternate their appearance.
  for (i = 0; i < tblEl.rows.length; i++) {
   rowEl = tblEl.rows[i];
    if (i % 2 != 0){
      rowEl.className = rowClsNm;
	}
    else{
      rowEl.className = rowClsNmb;
    }
  }
}

function formatNumber(number){

	var tmpNumStr = number.toString();
	var length = tmpNumStr.length;
	var returnval = "";

	for (i = length; (i - 3) > 0; i = i - 3) {
		 returnval = "," + tmpNumStr.substr(i - 3, 3) + returnval;
	}
	returnval =  tmpNumStr.substr(0, i) + returnval;

	return returnval;

}

function swapImage(imgReverse,imgName, col){
var temp, temp2;
var name;

//if image type is reverse(true or 1), then show down arrow image
	if (document.images){
		//swap the current sort column to the appropriate image
		if (imgReverse){
			temp = eval(imgName + "down.src");
			document[imgName].src = temp;
		}
		else{
			temp = eval(imgName + "up.src");
			document[imgName].src = temp;
		}
		//swap the lasted sorted column to its neutral state image
		if (lastsorted != col){
			name = "image" + lastsorted;
			//alert(name);
			temp2 = eval(name + ".src");
			document[name].src = temp2;
		}
	}
	return true;
}

// ---------------------------------------------------- 
// Some Code/Coding Concept were derived from:
// Copyright 2002 by Mike Hall                         
// Please see http://www.brainjar.com for terms of use. 
// http://www.brainjar.com/dhtml/tablesort/demo.html    
// ---------------------------------------------------- 


