//=======================================================================

//  Sorting Utilities

//=======================================================================

var lastSortCol = -1

var lastSortOrder

var colHeaders



// sort the table by a given column

function sortTable(tbl,lSortCol,imageDown,imageUp,numRowsToSkip){
  var oTbl, oElt

	var lRow, lCol, lRow2, lMinIndex, lCmpResult

	var prevDisplay, minValue, sTestValue, maxRow, i

  var comparisonOverride

	

	// IE on a MAC really screws things up

	if ( (navigator.appVersion.indexOf("MSIE") != -1) && (navigator.appVersion.indexOf("Mac") != -1) ) return false



	// get the table that should be sorted

	oTbl = document.getElementById(tbl)

	if ((!oTbl) && (document.all)) oTbl = document.all[tbl]

	if (!oTbl) return false

	

	// +1 +else 98/13/2006 support sort order override 

	if ((typeof(arguments[5]) == "undefined") || (arguments[5] == "")) {

		if (lastSortCol == lSortCol){

			// if the user clicked the same column, then just switch the direction of the sort

			if (lastSortOrder == "ascending"){

				lastSortOrder = "descending"

			}else{

				lastSortOrder = "ascending"

			}

		} else {

			// otherwise, sort in ascending order by default

			lastSortOrder = "ascending"

		}

  } else {

		lastSortOrder = arguments[5]

	}

	

	// +5 02/07 support comparison function override

	if (typeof(arguments[6]) == "undefined") {

	  comparisonOverride = "" 

	} else {

	  comparisonOverride = arguments[6]

	}

	

  // Set the table display style to "none" - so that Netscape 6 browsers work properly.

  prevDisplay = oTbl.style.display;

  oTbl.style.display = "none";

	// remember the column headers of all columns. Do it only once - before the first time

	// when the user sorts for the first time

	if (!colHeaders) {

	  colHeaders = new Array(oTbl.rows[0].cells.length+1);

	  for (lCol = 0; lCol < oTbl.rows[0].cells.length ; lCol++) {    //- 1

		  colHeaders[lCol] = oTbl.rows[0].cells[lCol].innerHTML;

		}		

	}

	

	// remove the sort arrow from the column header of the column we previously sorted on

   if ( (lastSortCol >= 0) && (lastSortCol != lSortCol) ) {

	   oTbl.rows[0].cells[lastSortCol].innerHTML = colHeaders[lastSortCol]

	}



	// display a sort arrow next to the header of the column we are sorting on

	if (lastSortOrder == "ascending"){

	   oTbl.rows[0].cells[lSortCol].innerHTML = colHeaders[lSortCol] + "&nbsp;&nbsp;<img src='" + imageDown + "'>" 	

	} else {

	   oTbl.rows[0].cells[lSortCol].innerHTML = colHeaders[lSortCol] + "&nbsp;&nbsp;<img src='" + imageUp + "'>" 

	}



	lastSortCol = lSortCol

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

	// Start with row 1, since row 0 is column headers.

	maxRow = oTbl.rows.length - numRowsToSkip

   for (lRow = 1; lRow < maxRow-1; lRow++) {

     // Assume the current row has the minimum value.

     lMinIndex = lRow;

     minValue = getValue(oTbl.rows[lRow].cells[lSortCol]);

     // Search the rows following the current one for a smaller value.

     for (lRow2 = lRow + 1; lRow2 < maxRow; lRow2++) {

       sTestValue = getValue(oTbl.rows[lRow2].cells[lSortCol]);

			 // +1 02/07

       lCmpResult = compareValues(minValue, sTestValue, lastSortOrder,comparisonOverride);

       // If this row has a smaller value than the current minimum,

       // remember its position and update the current minimum value.

       if (lCmpResult > 0) {

         lMinIndex = lRow2;

         minValue = sTestValue;

       }

     }

	  // By now, we have the row with the smallest value. Remove it from

     // the table and insert it before the current row.

     if (lMinIndex > lRow) {

	    // we are looking for the TBODY element. In IE it is the child with index 0 (and there is just 1 child node 

	 	 // of the table object), whereas in Mozilla and Netspace it's child with index 1 (And there are 2 children)

		 // the following for loop works in either case

		 for (i = 0; i < oTbl.childNodes.length; i++) {

	      if (oTbl.childNodes[i].nodeName == "TBODY") {

			  oElt = oTbl.childNodes[i].removeChild(oTbl.rows[lMinIndex]);

			  oTbl.childNodes[i].insertBefore(oElt, oTbl.rows[lRow]);

			  break

		   }

		 }

     }

   }   



   // Restore the table's display style.

   oTbl.style.display = prevDisplay;

   recolorRows(oTbl,"tableoddrow","tableevenrow",maxRow);

   return false

}





// recolor the rows in alternating colors

// can be called, for example, after sorting on a particular column

function recolorRows(oTbl,color1,color2,maxRow){

  var lRow

	

  for (lRow = 1; lRow < maxRow ; lRow++) {

    // Assume the current row has the minimum value.

    oRow = oTbl.rows[lRow]

    if (oRow){

			if (lRow % 2 == 0){

				oRow.className = color2;  //backgroundColor = color2;

			}else{

				oRow.className = color1;  //backgroundColor = color1;

			}

		}

	}

	return

}



// keep it simple for now

function getValue(Elt){

  var sText,lPos

	

  if (!Elt) return ""

	sText = Elt.innerHTML

	// to handle the special 1-st row (Which displays address and also actions)

	lPos = sText.indexOf("&nbsp;")

	if (lPos != -1) sText = sText.substr(0,lPos)



	return striptags(sText)

}





// compare two values, use numeric comparison if vq and v2 are numbers (or dollar amounts)

// otherwise use string comparison

function compareValues(v1, v2, sortOrder, comparisonOverride) {

  var f1, f2;

	 

	// +1 02/07 support comparison override

	if (!comparisonOverride) {

		// if the values represent dollar amounts, convert them to floats too

		if ((v1.substr(0,1) == "$") && (v2.substr(0,1) == "$")){

			v1 = v1.substr(2,v1.length)

			v2 = v2.substr(2,v2.length)		

		} 

	

		// check if the values represent valid dates in mm/dd/yyyy format

		f1 = getDateIn(v1)

		f2 = getDateIn(v2)

	

		// if v1 or v2 does not represent a valid date , then getDateIn will return the string unchanged

		//  only if both are changed, then they represent dates

		if ( (f1 != v1) && (f2 != v2) ){

			v1 = f1

			v2 = f2

		}else {

			// check if the values are numeric, if so, convert them to floats.

			f1 = parseFloat(v1);

			f2 = parseFloat(v2);

			if (!isNaN(f1) && !isNaN(f2)) {

				v1 = f1;

				v2 = f2;

			}

		}

  }

	

  // Compare the two values.

  if (v1 == v2) return 0;

	if (sortOrder == "ascending"){

     if (v1 > v2) return 1;

     return -1;

	} else {

     if (v1 > v2) return -1;

     return 1;

	}

}




