
// global vars
var g_iSortColumn;
var g_iLastSortColumn = -1;
var g_iSortDirection  = 1;
var selectedTableRow;


/////////////////////////////////////////////////////////////////////////
// function sortCompare
// internal function used by sortTable function
function sortCompareFunction ( val1, val2 ) 
{
    var cells1= val1.getElementsByTagName("td");
    var cells2= val2.getElementsByTagName("td");

    if ( !cells1 || !cells2 ||
         g_iSortColumn < 0 ||
          cells1.length <= g_iSortColumn ||
          cells2.length <= g_iSortColumn  )
    {
         return 0;
    }

    // childNodes[0].firstChild.nodeValue or innerText is equivalent
    var node1 = cells1[g_iSortColumn];
    var node2 = cells2[g_iSortColumn];
    var ntext1 = node1.innerText;
    var ntext2 = node2.innerText;

    if (!ntext1 || !ntext2)
    { return 0; }

    if ( isNumber(ntext1) && isNumber(ntext2) ) {
       //alert( ntext1 + " is number" );
       return ( parseFloat(ntext1) - parseFloat(ntext2) ) * g_iSortDirection;
    }

    if ( ntext1 < ntext2 )
         return -1*g_iSortDirection;
         
    if ( ntext1 > ntext2 )
         return g_iSortDirection;
    
    return 0;
}


/////////////////////////////////////////////////////////////////////////
//      function sortTable()
//
// function to sort a table in a HTML page
// Sorts the table rows in the table with id as specified in 
// strTableId by the column 'iCol'.
// The placement of rows, that does not have at least 'iCol' columns,
// is not specified.
//
// Only 'TR' rows that are inside the first 'TBODY' tag will be sorted.
// Only 'TD' cells are compared, so 'TH' cells first in a table are
// not moved during sort.
//
// HTML for table must be well-formed.
//
// Think twice when using ROWSPAN & COLSPAN, as iCol is used to index
// columns on each row.
//
//     PARAMETERS 
//       iCol          : Column index to sort table by.
//       strTableId     : Id of table to sort.
//       iHeaders     : Number of header lines to ignore from sort.
//	iFooters      : Number of footer lines to ignore from sort.
//function sortCol(col) {
//}

function sortTable (iCol, strTableId, iHeaders, iFooters) {
    var tbl     = document.getElementById(strTableId);
    var tblBody = tbl.getElementsByTagName("tbody").item(0);
    var tblRows = tblBody.getElementsByTagName("tr");
    
    var arr = new Array( tblRows.length-iHeaders - iFooters );
    var i;
    var length = tblRows.length;
    // create array of rows so we can sort
    for ( i=iHeaders; i<length - iFooters; i++) {
         arr[i-iHeaders] = tblRows[i].cloneNode(true);
    }
    
    // set column that sort is based on
    g_iSortColumn = iCol;
    
    // find the sort direction
    if (g_iLastSortColumn==iCol) {
         g_iSortDirection *= -1;

         arr.reverse();
         // arr.sort ( sortCompareFunction );
    } else {
         g_iSortDirection = 1;
         g_iLastSortColumn=iCol;

         // peform the sort
         arr.sort ( sortCompareFunction );
    }

    // write sorted data from array to table
    for (i=iHeaders; i < length - iFooters; i++) {
         //tblBody.replaceChild( arr[i-iHeaders].cloneNode(true), tblRows[i] );
         tblBody.replaceChild( arr[i-iHeaders], tblRows[i] );
    }
   //for NS 6.x
   /* just to make sorting fast, ignore the NS case
   if (document.getElementById && ! document.all) {
               var oNodeList = tblBody.getElementsByTagName("tr");
               var szId;
               for (i=0;i<oNodeList.length;i++) {
                    szId = oNodeList.item(i).id;
                    if (szId.indexOf("tr") != -1) oNodeList.item(i).onclick = doselect;
               }
   }
   */
}


function isNumber(str)
{
    var num;

    str = str.toString();
    num = parseFloat(str);

    return !isNaN(num);
}
