/* Add mouse action events to highlight class rowLight table rows */
var i_rowLightState = 0;
var b_rowLightDebug = 0;

/* If the browser doesn't support getElementsByTagName method then    */
/* none of this logic is going to work worth toffee                   */
if (typeof(document.getElementsByTagName) == "undefined")
{
   i_rowLightState = -1;
}

/* If we are running locally then switch on debugging mode, which     */
/* produces alert boxes for all details, rather than status messages  */
if (document.URL.toLowerCase().substr(0, 7) == "file://")
{
/* To avoid this becoming annoying during testing of other elements   */
/* we only activate debugging when using a mapped drive not the UNC   */
   if (document.URL.toLowerCase().lastIndexOf(":") > 5)
      b_rowLightDebug = 1;
}
/* end of always executed code, remainder are function definitions */

function rowLighting(i_newState)
{
   var o_allTables, o_allRows, o_allCells, o_allText;
   var o_table, o_row;
   var i_tableIndex, i_rowIndex;
   var i_tableCount = 0, i_rowCount = 0;

   if (i_rowLightState < 0)
   {
      rowLightMessage("Table row highlighting unavailable due to lack of browser capability");
      return;
   }

   if (i_newState == i_rowLightState)
   {
      rowLightMessage("Table row highlighting already in state: " + i_newState);
      return;
   }

/* Spin over all the tables in the document looking for our class */
   o_allTables = document.getElementsByTagName("TABLE");
   for (i_tableIndex = 0 ; i_tableIndex < o_allTables.length ; ++i_tableIndex)
   {
      o_table = o_allTables[i_tableIndex];
      if (o_table.className == "rowLight"
       || o_table.getAttribute("class") == "rowLight")
      {
/* We've found a table of the desired class, so check each row */
         ++i_tableCount;
         o_allRows = o_table.getElementsByTagName("TR");
/* !!   alert(o_allRows.length + " rows found within table. bgColor='" + o_allRows[0].bgColor + "'; styleBG='" + o_allRows[0].style.backgroundColor+ "'"); */
         for (i_rowIndex = 0 ; i_rowIndex < o_allRows.length ; ++i_rowIndex)
         {
            o_row =  o_allRows[i_rowIndex];
            o_allCells = o_row.getElementsByTagName("TD");
            o_allText = o_allCells[0].getElementsByTagName("*");
/* !!   alert("Row " + i_rowIndex + "; " + o_allCells.length + " cells; 0 value='" + o_allCells[0].innerHTML + "'; 0 nV='" + o_allCells[0].nodeValue + "'"); */
/* !! we need a good way of identifying which rows need highlighting, */
/* !! preferably without requiring the presence or absence of a class
            if (i_newState && (o_row.className == "" || o_row.getAttribute("class") == ""))
   !! */
            if (o_allCells.length < 2)
            {
/* Assume that a single data cell has no need for highlighting */
            }
            else if (o_allCells[0].innerHTML == "")
            {
/* Assume that no subordinate entities means an empty first cell */
            }
            else if (o_allCells[0].innerHTML == "&nbsp;")
            {
/* Another representation of an empty first cell */
            }
            else if (o_allCells[0].innerHTML.length == 1 && o_allCells[0].innerHTML.charCodeAt(0) == 160)
            {
/* Opera doesn't return the entity name for a non-breaking space so   */
/* we have to trap the character instead                              */
            }
            else if (i_newState)
            {
/* !! this doesn't work in all browsers (eg. Opera)
               o_row.onmouseover = function(){ this.bgColor='darkgray'; };
               o_row.onmouseout = function(){ this.bgColor='silver'; };
   !! */
               o_row.onmouseover = function(){ this.style.backgroundColor = 'darkgray'; };
               o_row.onmouseout = function(){ this.style.backgroundColor = 'silver'; };
               ++i_rowCount;
            }
            else
            {
               o_row.onmouseover = "";
               o_row.onmouseout = "";
            }
         }
      }
   }

   if (i_tableCount == 0)
   {
      rowLightMessage("No tables found in class rowLight. No events changed");
   }
   else if (i_newState)
   {
      rowLightMessage(i_tableCount + " table(s) found in class rowLight. " + i_rowCount + " events added");
   }
   else
   {
      rowLightMessage(i_tableCount + " table(s) found in class rowLight. Row highlighting reset");
   }

   i_rowLightState = i_newState;
   return;
}

/* !! not sure that this is necessary or helpful, since I've yet to   */
/* !! find a browser that supports childNodes but not innerText       */
function getInnerText(o_element)
{
   var o_nodes, i_nodeIndex, s_result = "";

   if (typeof(o_element) == "string")
      return o_element;
   if (typeof(o_element) == "undefined")
      return "";
   if (typeof(o_element.innerText) != "undefined")
      return o_element.innerText;
   if (typeof(o_element.childNodes) == "undefined")
      return "";

   var o_nodes = o_element.childNodes;
   for (var i_nodeIndex = 0; i_nodeIndex < o_nodes.length; ++i_nodeIndex)
   {
      switch (o_nodes[i_nodeIndex].nodeType)
      {
         case 1:
/* It's another element, so expand its innards */
            s_result += getInnerText(o_nodes[i_nodeIndex]);
            break;
         case 3:
/* It's a text node, so add its value to our result string */
            s_result += o_nodes[i_nodeIndex].nodeValue;
            break;
      }
   }
   return s_result;
}


function rowLightMessage(s_msg)
{
   if (b_rowLightDebug == 1)
      alert(s_msg);
   else
      window.status = s_msg;
}


/* This box documents the modification history of this routine */
/*********************************************************************/
/*                                                                   */
/*  Version   Time     Date      Who                                 */
/*  1.00.00  07:24  24-Dec-2003  Steve Pitts                         */
/*        >  Original version                                        */
/*  1.00.01  09:25  21-Mar-2009  Steve Pitts                         */
/*        >  Replaced fixed debugging code with variable driven      */
/*        >  message output routine and added check for an empty     */
/*        >  cell containing just a single &nbsp; entity             */
/*  1.00.02  20:57  22-Mar-2009  Steve Pitts                         */
/*        >  Added additional check for non-breaking space           */
/*        >  character rather than the entity name                   */
/*  1.00.03  13:37   4-May-2009  Steve Pitts                         */
/*        >  Stopped debugging activation for local UNC names        */
/*                                                                   */
/*********************************************************************/
/* end of modification history */

