//<!-- ---------------------------------------------------------------------- -->
//<!-- SWDIST.JS - script for sorting table columns, hiding selected rows     -->
//<!-- Mods PCT, 2005.03.24						      -->
//<!-- Table sorting code based on					      -->
//<!-- "Table Sort Demo",Copyright 2002 by Mike Hall.                         -->
//<!-- FilterTable adapted from code by Lasse Reichstein Nielsen	      -->
//<!--************************************************************************-->
//<!--* Table Sort Demo 						     *-->
//<!--* http://www.brainjar.com/dhtml/tablesort/demo.html		     *-->
//<!--* Copyright 2002 by Mike Hall					     *-->
//<!--* Please see http://www.brainjar.com for terms of use.		     *-->
//<!--************************************************************************-->
//<!-- ---------------------------------------------------------------------- -->

function fnFilterTable(table, column, value, optvalue)
{
  var tblElement = document.getElementById(table);
  var rows = tblElement.rows;
  var n = rows.length;
  var i = 0;
  var testVal;

  for (i = 0; i < n; i++)
  {
     row = rows.item(i);
     // set default, block in IE, table-row in standards
     row.style.display = "";

     testVal = fnGetTextValue(row.cells.item(column));
     if (fnTestForValues(value, optvalue, testVal) == 1)
     {
	row.style.display = "";
     }
     else
     {
	row.style.display = "none";
     }
  }
}

function fnTestForValues(v1, v2, vtest)
{
  // ignore case
  var v1LC = v1.toLowerCase()
  var vtestLC = vtest.toLowerCase()

  if (vtestLC.indexOf(v1LC, 0) != -1)
  {
     return 1;
  }
  if (v2 != 0)
  {
     var v2LC = v2.toLowerCase();
     if (vtestLC.indexOf(v2LC, 0) != -1)
     {
	return 1;
     }
  }
  return 0;
}


function fnDetectWin32(gowin32, gonotwin32)
{
  var osplatform = navigator.platform;

  if (osplatform.toLowerCase() == "win32")
  {
     window.location.replace(gowin32);
  }
  else
  {
     window.location.replace(gonotwin32);
  }
}

//-----------------------------------------------------------------------------
// fnSortTable(id, col, rev)
//
//  id	- ID of the TABLE, TBODY, THEAD or TFOOT element to be sorted.
//  col - Index of the column to sort, 0 = first column, 1 = second column,  etc.
//  rev - If true, the column is sorted in reverse (descending) order initially.
//
//-----------------------------------------------------------------------------

function fnSortTable(id, col, rev)
{

  // Get the table or table section to sort.
  var tblElement = document.getElementById(id);

  // The first time this function is called for a given table, set up an array of reverse sort flags.
  if (tblElement.reverseSort == null)
  {
    tblElement.reverseSort = new Array();
    // assume the first column is initially sorted.
    tblElement.lastColumn = 1;
  }

  // If this column has not been sorted before, set the initial sort direction.
  if (tblElement.reverseSort[col] == null)
    tblElement.reverseSort[col] = rev;

  // If this column was the last one sorted, reverse its sort direction.
  if (col == tblElement.lastColumn)
    tblElement.reverseSort[col] = !tblElement.reverseSort[col];

  // Remember this column as the last one sorted.
  tblElement.lastColumn = col;

  // Set the table display style to "none" - necessary for Netscape 6 browsers.
  var oldDisplay = tblElement.style.display;
  tblElement.style.display = "none";

  // Sort the rows based on the content of the specified column using a selection sort.
  var tmpElement;
  var i, j;
  var minVal, minIdx;
  var testVal;
  var cmp;

  for (i = 0; i < tblElement.rows.length - 1; i++)
  {
    // Assume the current row has the minimum value.
    minIdx = i;
    minVal = fnGetTextValue(tblElement.rows[i].cells[col]);

    // Search the rows that follow the current one for a smaller value.
    for (j = i + 1; j < tblElement.rows.length; j++)
    {
      testVal = fnGetTextValue(tblElement.rows[j].cells[col]);
      cmp = fnCompareTextValues(minVal, testVal);
      // Negate the comparison result if the reverse sort flag is set.
      if (tblElement.reverseSort[col])
	cmp = -cmp;

      // Sort by the first column if those values are equal.
      if (cmp == 0 && col != 0)
	cmp = fnCompareTextValues(fnGetTextValue(tblElement.rows[minIdx].cells[0]), fnGetTextValue(tblElement.rows[j].cells[0]));

      // If this row has a smaller value than the current minimum, remember its position and update the current minimum value.
      if (cmp > 0)
      {
	minIdx = j;
	minVal = testVal;
      }
    }

    // By now, we have the row with the smallest value. Remove it from the
    // table and insert it before the current row.
    if (minIdx > i)
    {
      tmpElement = tblElement.removeChild(tblElement.rows[minIdx]);
      tblElement.insertBefore(tmpElement, tblElement.rows[i]);
    }
  }

  // Update table to make it look pretty.
  fnUpdateTable(tblElement, col);

  // Restore the table's display style.
  tblElement.style.display = oldDisplay;

  return false;
}

//-----------------------------------------------------------------------------
// Functions to get and compare values during a sort.
//-----------------------------------------------------------------------------

// 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;
}

function fnGetTextValue(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 += fnGetTextValue(el.childNodes[i]);
  }
  return fnNormalizeString(s);
}

function fnCompareTextValues(v1, v2)
{
  // ignore case
  var v1LC = v1.toLowerCase()
  var v2LC = v2.toLowerCase()
  v1 = v1LC;
  v2 = v2LC;

  // Compare the two values.
  if (v1 == v2)
    return 0;
  if (v1 > v2)
    return 1;
  return -1;
}


// Regular expressions for normalizing white space.
var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
var whtSpMult = new RegExp("\\s\\s+", "g");

function fnNormalizeString(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 colClsNm = "sortedcol";

// Regular expressions for setting class names.
var colTest = new RegExp(colClsNm, "gi");

function fnUpdateTable(tblElement, col)
{
  var i, j;
  var rowElement, cellElement;

  // Set style classes on each row to alternate their appearance.
  for (i = 0; i < tblElement.rows.length; i++)
  {
    rowElement = tblElement.rows[i];
    // Set style classes on each column to highlight the one that was sorted.
    for (j = 0; j < tblElement.rows[i].cells.length; j++)
    {
      cellElement = rowElement.cells[j];
      cellElement.className = cellElement.className.replace(colTest, "");
      if (j == col)
	cellElement.className += " " + colClsNm;
      cellElement.className = fnNormalizeString(cellElement.className);
    }
  }

  // Find the table header and highlight the column that was sorted.
  var el = tblElement.parentNode.tHead;
  rowElement = el.rows[el.rows.length - 1];
  // Set style classes for each column as above.
  for (i = 0; i < rowElement.cells.length; i++)
  {
    cellElement = rowElement.cells[i];
    cellElement.className = cellElement.className.replace(colTest, "");
    // Highlight the header of the sorted column.
    if (i == col)
      cellElement.className += " " + colClsNm;
    cellElement.className = fnNormalizeString(cellElement.className);
  }
}
