<!-- Begin

function setSortSequences(col, sortSeq)
  {
	// Set the sort sequence indicators according to the input parameters. They reflect the current state of the sort sequence of each column in the table. The function is called every time a column is sorted, to change the sort sequence for that column, and to set all other columns to unsorted. This enables a column to be sorted in the reverse order to its current state eg sort it ascending if it is currently descending, and sort it descending if it is currently ascending, by simply rewriting the table dynamically.
	// The following sort sequence indicator values are used :-

	//		ascending		descending	notsorted (or column does not exist)
	
	// Set all entries on the form to 'notsorted' first, then set the current one to its new sort sequence.

	var thisSortIndicator = eval("document.forms['SortIndicators'].col"+col+"sort");
	var sortIndicator;
	
	for (i=0; i<4; i++ )
	{
		sortIndicator = eval("document.forms['SortIndicators'].col"+i+"sort");
		sortIndicator.value = 'notsorted';
	}

	thisSortIndicator.value = sortSeq;

 }

function setDataType(cValue)
  {
    // THIS FUNCTION CONVERTS DATES AND NUMBERS FOR PROPER ARRAY
    // SORTING WHEN IN THE SORT FUNCTION
    var isDate = new Date(cValue);
    if (isDate == "NaN")
      {
        if (isNaN(cValue))
          {
            // THE VALUE IS A STRING, MAKE ALL CHARACTERS IN
            // STRING UPPER CASE TO ASSURE PROPER A-Z SORT
            cValue = cValue.toUpperCase();
            return cValue;
          }
        else
          {
            // VALUE IS A NUMBER, TO PREVENT STRING SORTING OF A NUMBER
            // ADD AN ADDITIONAL DIGIT THAT IS THE + TO THE LENGTH OF
            // THE NUMBER WHEN IT IS A STRING
            var myNum;
            myNum = String.fromCharCode(48 + cValue.length) + cValue;
            return myNum;
          }
      }
    else
      {
        // VALUE TO SORT IS A DATE, REMOVE ALL OF THE PUNCTUATION AND
        // AND RETURN THE STRING NUMBER
        //BUG - STRING AND NOT NUMERICAL SORT .....
        // ( 1 - 10 - 11 - 2 - 3 - 4 - 41 - 5  etc.)
        var myDate = new String();
        myDate = isDate.getFullYear() + " " ;
        myDate = myDate + isDate.getMonth() + " ";
        myDate = myDate + isDate.getDate(); + " ";
        myDate = myDate + isDate.getHours(); + " ";
        myDate = myDate + isDate.getMinutes(); + " ";
        myDate = myDate + isDate.getSeconds();
        //myDate = String.fromCharCode(48 + myDate.length) + myDate;
        return myDate ;
      }
  }

function sortTable(col, table)
  {
	//alert("Entering sortTable");
	var sortSeq;
    var i;
	var tableToSort;
	var tableRows;

	if (document.all)
		{
		//alert("IE");
		tableToSort = document.all[table];
		}
	else if (document.getElementById)
		{
		//alert("Netscape");	
		//tableToSort = document.getElementById(table);
		//This will not be easily possible until NN5
		end;
		}

	//var tableRows = tableToSort.rows.length;
	
	if (table)
	{
		tableRows = tableToSort.rows.length;
		iCurCell = col + tableToSort.cols;
		//alert('Table Rows = '+tableRows);
	}
	else
	{
		alert('Cannot locate table to sort');
		return;
	}
	var sortIndicator = eval("document.forms['SortIndicators'].col"+col+"sort");
	//alert('Currently sorted into '+sortIndicator.value+' order');
	if (sortIndicator.value != "notsorted")
		{
		// This column is already sorted, so simply reverse the table order rather than re-sort 
		reverseSortOrder(col, table);
		// Reverse the column order indicator
		if (sortIndicator.value == "ascending")
			{
			sortIndicator.value = "descending";
			sortSeq = "descending";
			}
		else
			{
			sortIndicator.value = "ascending";
			sortSeq = "ascending";
			}

		//alert('Reversed to '+sortIndicator.value+' sort order');

		setSortSequences(col, sortSeq)	

		return
		}
	
	//alert('Setting up sort variables');
    var iCurCell = col + tableToSort.cols;
    var totalRows = tableToSort.rows.length;
    var bSort = 0;
    var colArray = new Array();
    var oldIndex = new Array();
    var indexArray = new Array();
    var bArray = new Array();
    var newRow;
    var newCell;
    var c;
    var j;
    // ** POPULATE THE ARRAY colArray WITH CONTENTS OF THE COLUMN SELECTED
	//alert('Table rows '+tableToSort.rows.length+'\n\r\n\rTable cols '+tableToSort.cols);
    for (i=1; i < tableToSort.rows.length; i++)
      {
        colArray[i - 1] = setDataType(tableToSort.cells(iCurCell).innerText);
        iCurCell = iCurCell + tableToSort.cols;
      }
    // ** COPY ARRAY FOR COMPARISON AFTER SORT
    for (i=0; i < colArray.length; i++)
      {
        bArray[i] = colArray[i];
      }
    // ** SORT THE COLUMN ITEMS
    //alert ( colArray );
    colArray.sort();
   // alert ( colArray );
    for (i=0; i < colArray.length; i++)
      { // LOOP THROUGH THE NEW SORTED ARRAY
        indexArray[i] = (i+1);
        for(j=0; j < bArray.length; j++)
          { // LOOP THROUGH THE OLD ARRAY
            if (colArray[i] == bArray[j])
              {  // WHEN THE ITEM IN THE OLD AND NEW MATCH, PLACE THE
                // CURRENT ROW NUMBER IN THE PROPER POSITION IN THE
                // NEW ORDER ARRAY SO ROWS CAN BE MOVED ....
                // MAKE SURE CURRENT ROW NUMBER IS NOT ALREADY IN THE
                // NEW ORDER ARRAY
                for (c=0; c<i; c++)
                  {
                    if ( oldIndex[c] == (j+1) )
                    {
                      bSort = 1;
                    }
                      }
                      if (bSort == 0)
                        {
                          oldIndex[i] = (j+1);
                        }
                          bSort = 0;
                        }
          }
    }
  // ** SORTING COMPLETE, ADD NEW ROWS TO BASE OF TABLE ....
  for (i=0; i<oldIndex.length; i++)
    {
      newRow = tableToSort.insertRow();
      for (c=0; c<tableToSort.cols; c++)
        {
          newCell = newRow.insertCell();
		  newCell.className = 'info';
          newCell.innerHTML = tableToSort.rows(oldIndex[i]).cells(c).innerHTML;
		  //alert(newCell.innerHTML);
        }
      }
  //MOVE NEW ROWS TO TOP OF TABLE ....
  for (i=1; i<totalRows; i++)
    {
      tableToSort.moveRow((tableToSort.rows.length -1),1);
    }
  //DELETE THE OLD ROWS FROM THE BOTTOM OF THE TABLE ....
  for (i=1; i<totalRows; i++)
    {
      tableToSort.deleteRow();
    }

  setSortSequences(col, sortSeq)	
  
  }


 function reverseSortOrder(col, table)
  {
    var iCurCell;
	var tableToSort;
	var tableRows;
	if (document.all)
		{
		//alert("IE");
		tableToSort = document.all[table];
		}
	else if (document.getElementById)
		{
		//alert("Netscape");	
		//tableToSort = document.getElementById(table);
		//This will not be easily possible until NN5
		end;
		}

	//var tableRows = tableToSort.rows.length;
	
	if (table)
	{
		tableRows = tableToSort.rows.length;
		iCurCell = col + tableToSort.cols;
		//tableToSort.rows.length undefined in Opera
		//alert('Table Rows to reverse = '+tableRows);
	}
	else
	{
		alert('Cannot locate table to sort');
		return;
	}

    var newRow;
    var newCell;
	var i;
	var EOT = tableRows;

   //tableToSort.cols is undefined in netscape
   //alert("Columns in table is "+tableToSort.cols);

  // Create a new row at the end of the table for every existing row, starting with the existing last row in the table, thus adding all the existing rows to the end of the table, in their reverse order.
	for (i=tableRows-1; i>0; i--)
   {
      newRow = tableToSort.insertRow(EOT);
	  EOT = tableToSort.rows.length;
      for (c=0; c<tableToSort.cols; c++)
        {
          //alert("Creating new cell "+c+" in new row number "+i); 
		  newCell = newRow.insertCell();
		  newCell.className = 'info';
          newCell.innerHTML = tableToSort.rows(i).cells(c).innerHTML;
        }
    }
  
  // Delete the first row in the table, as many times over as is required to remove all the original table contents (the rows in their original order), leaving the newly created rows in the reverse order.
  for (i=1; i<tableRows; i++)
    {
      tableToSort.deleteRow(1);
    }

  }

//  End -->

