var hiddenColor = '#333333';
var sortColumn = -1;
var SHOW_TEXT = " show ";

function tc(evt) 
{  
   var el = evt.target;
   if (("" + el) == "undefined" )
       el = event.srcElement;

   if ( el.bgColor == hiddenColor )
   {
       showCell( el );
   }
   else 
   {
	hideCell( el );
   }

}

function isHidden( cell ) {
    return (cell.childNodes[0].nodeValue == SHOW_TEXT ) ;
}

function getText( cell ) {
    if ( isHidden( cell ) ) {
        return cell.answer ;
    }
    else {
        return cell.childNodes[0].nodeValue ;
    }
}

function showCell( cell ) {
      if ( isHidden(cell) ) {
	      cell.style.color='#000000';
	      cell.bgColor='#ffffff';
	      cell.childNodes[0].nodeValue = cell.answer;
              cell.align = 'left';
      }
}

function hideCell( cell ) {
      cell.bgColor=hiddenColor;
      if ( !cell.style.height ) {
           cell.style.height = (cell.clientHeight - 2)+ "px";
      }
      cell.style.color='#777777'; 
      if ( !isHidden( cell )) {
          cell.answer = cell.childNodes[0].nodeValue;
          cell.childNodes[0].nodeValue = SHOW_TEXT ;
          cell.align = 'center';
      }
}

function HideAll() 
{
   var theTable = document.getElementById( 'theTable' )
   var aRow;
   var tableLength = theTable.rows.length;
   var i;
   var c;
   for ( i=1; i<tableLength; i++ )
   { 
      aRow = theTable.rows[i];
      for (c=0;c<aRow.cells.length;c++) {
        var cell = aRow.cells.item(c);
        hideCell( cell );
      }
   }
}


function lockTableDimensions() {
   var theTable = document.getElementById( 'theTable' )
   var aRow = theTable.rows[0];
   var numColumns = aRow.cells.length ;
   var col;
   for ( col=0; col<numColumns; col++ ) {
      var cell = aRow.cells.item(col);
      cell.style.width = cell.clientWidth + "px";
   }
   return ;
   var tableLength = theTable.rows.length;
   var i;
   for ( i=1; i<tableLength; i++ )
   { 
      aRow = theTable.rows[i];
      var cell = aRow.cells.item(0);
      cell.style.height = cell.offsetHeight + "px";
   }
}

function HideSome() 
{
   var theTable = document.getElementById( 'theTable' )
   var aRow;
   aRow = theTable.rows[0];
   var numColumns = aRow.cells.length ;
   var cell = null;
   var tableLength = theTable.rows.length;
   var i;
   for ( i=1; i<tableLength; i++ )
   { 
      aRow = theTable.rows[i];
      var randomColumn = Math.floor( Math.random()* (numColumns)) ;

      cell = aRow.cells.item(randomColumn);
      hideCell( cell );
   }
}

function ShowAll() 
{
   var theTable = document.getElementById( 'theTable' )
   var aRow;
   var tableLength = theTable.rows.length;
   var i;
   var c;
   for ( i=1; i<tableLength; i++ )
   { 
      aRow = theTable.rows[i];
      for (c=0;c<aRow.cells.length;c++) {
        var cell = aRow.cells.item(c);
        showCell( cell );
      }
   }
}


function indicateSortColumn( sortCol, sorted )
{
    
}

function sortTable( sortCol )
{
   sortCol--;
   var theTable = document.getElementById( 'theTable' )
   var aRow;
   var bRow;
   var aCell;
   var bCell;
   var aValue ;
   var bValue ; 

   var tableLength = theTable.rows.length;
   var i;
   var c;
   var j;

   for ( i=1; i<tableLength; i++ )
   { 
      for (j=1; j<tableLength-i; j++) {
         aRow = theTable.rows[j];
         bRow = theTable.rows[j+1];
         aValue = getText( aRow.cells.item(sortCol));
         bValue = getText( bRow.cells.item(sortCol));

         if ( aValue > bValue ) {         
              for (c=0;c<aRow.cells.length;c++) {
                aCell = aRow.cells.item(c);
                bCell = bRow.cells.item(c);
                swapCells( aCell, bCell );
              }
         }
      }
   }
}

function swapCells( aCell, bCell ) {

           var tmp = aCell.childNodes[0].nodeValue ;
           aCell.childNodes[0].nodeValue =  bCell.childNodes[0].nodeValue ;
           bCell.childNodes[0].nodeValue =  tmp;

           tmp = aCell.answer ;
           aCell.answer = bCell.answer ;
           bCell.answer = tmp ;

           tmp = aCell.bgColor ;
           aCell.bgColor = bCell.bgColor ;
           bCell.bgColor = tmp;

           tmp = aCell.style.color ;
           aCell.style.color = bCell.style.color ;
           bCell.style.color = tmp ;

           tmp = aCell.style.height ;
           aCell.style.height = bCell.style.height;
           bCell.style.height = tmp ;

           tmp = aCell.align ;
           aCell.align = bCell.align ;
           bCell.align = tmp ;
}


function Shuffle()
{
   var theTable = document.getElementById( 'theTable' )
   var aRow;
   var bRow;
   var i;
   var c;

   var tableLength = theTable.rows.length;

   for ( i=1; i<tableLength; i++ )
   { 
      var randomNumber=Math.floor( Math.random()* (tableLength-1)) + 1;
      aRow = theTable.rows[i];
      bRow = theTable.rows[randomNumber];
      for (c=0;c<aRow.cells.length;c++) {
        var aCell = aRow.cells.item(c);
        var bCell = bRow.cells.item(c);
        swapCells( aCell, bCell );
      }
   }
}

function toggleColumn( columnNumber )
{
   var theTable = document.getElementById( 'theTable' )
   var aRow;
   var cell;
   c = columnNumber - 1;

   // determine whether the first item in this colum is visible or hidden
   aRow = theTable.rows[1];
   cell = aRow.cells.item(c);


   var show = ( cell.bgColor == hiddenColor );


   for ( i=1; i<theTable.rows.length; i++ )
   { 
      aRow = theTable.rows[i];
      cell = aRow.cells.item(c);
      if ( show ) {
          showCell( cell );
      }
      else {
           hideCell( cell );
      }
   }

}
