function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}

function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
    	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

// Handler for the onMouseOut event for a CrosshairGridView Cell
// It reverts highlighted cells back to their normal color
function cgvMouseOut(cell)
{

	removeClass(cell,'crosshair_bullseye');

   var columnIndex = cgvGetColumnIndex(cell);

   var row = cell.parentNode;
   var table = row.parentNode;
   var rows = table.childNodes;
   
   // Iterate over all the rows.  For most rows, we just have to switch the color
   // of the cell in the highlighted column.  But for the row containing the current
   // cell, we have to switch the color of all the cells in the row
   var rowNum = 0;
   for(var i = 0; i < rows.length; i++)
   {
      var row2 = rows[i];
      
      // Skip any pager header row(s)
      if (row2.className == null || (row2.className.indexOf("pager_row") < 0)) {
      
         // Choose the normal color (regular or alternating)
         
         // Is this the highlighted row?
         if(row2 == row)
         {
            var cells = row.childNodes;
            
            //  For the highlighted row, switch every cell back
            for(var j = 0; j < cells.length; j++)
            {
               if(cells[j] && (cells[j].nodeName == 'TD'))
               {
               		removeClass(cells[j],'crosshair')
               }
            }
         }
         else
         {
            // For the non-highlighted row, we only have to switch the cell in highlighted column
            var highlightCell = row2.childNodes[columnIndex];
            if(highlightCell && (highlightCell.nodeName == 'TD'))
            {
				removeClass(highlightCell,'crosshair');
            }
         }

         rowNum++;
      }
   }
}

// Handler for the onMouseOver event for a CrosshairGridView Cell
// It highlights the row and column of the current cell.  The cell itself
// gets the bullseye color
function cgvMouseOver(cell)
{
   var columnIndex = cgvGetColumnIndex(cell);

   var row = cell.parentNode;
   var table = row.parentNode;
   var rows = table.childNodes;

   // Iterate over all the rows.  For most rows, we just have to switch the color
   // of the cell in the highlighted column.  But for the row containing the current
   // cell, we have to switch the color of all the cells in the row
   for(var i = 0; i < rows.length; i++)
   {
      var row2 = rows[i];  

      // Is this the highlighted row?
      if(row2 == row)
      {
         //  For the highlighted row, highlight every cell
         var cells = row.childNodes;
         for(var j = 0; j < cells.length; j++)
         {
            if(cells[j] && (cells[j].nodeName == 'TD'))
            {
            	addClass(cells[j],'crosshair')
            }
         }
      }
      else
      {
         // For the non-highlighted row, only highlight the cell in highlighted column
         var highlightCell = row2.childNodes[columnIndex];
         if(highlightCell && (highlightCell.nodeName == 'TD'))
         {
         	addClass(highlightCell,'crosshair')
         }
      }
   }
   addClass(cell,'crosshair_bullseye');
   
//   cell.style.backgroundColor = bullseyeColor;
}

// Handler for the onMouseOut event for a CrosshairGridView Label Cell
// These cells only highlight rows, not columns.  And, they don't use
// the bullseye color
function cgvLabelMouseOut(cell)
{
   var columnIndex = cgvGetColumnIndex(cell);

   var row = cell.parentNode;
   var table = row.parentNode;
   var rows = table.childNodes;

   // Iterate over all the rows.  For most rows, we just have to switch the color
   // of the cell in the highlighted column.  But for the row containing the current
   // cell, we have to switch the color of all the cells in the row
   var rowNum = 0;
   for(var i = 0; i < rows.length; i++)
   {
      var row2 = rows[i];
      
      // Skip any pager header row(s)
      if (row2.className == null || (row2.className.indexOf("pager_row") < 0)) {

         // Is this the highlighted row?
         if(row2 == row)
         {

            var cells = row.childNodes;

            //  For the highlighted row, switch every cell back
            for(var j = 0; j < cells.length; j++)
            {
               if(cells[j] && (cells[j].nodeName == 'TD'))
               {
               		removeClass(cells[j],'crosshair');
               }
            }
            break;
         }

         rowNum++;
      }
   }
}

// Handler for the onMouseOver event for a CrosshairGridView Label Cell
// These cells only highlight rows, not columns.  And, they don't use
// the bullseye color
function cgvLabelMouseOver(cell)
{
   var row = cell.parentNode;
   var table = row.parentNode;
   var rows = table.childNodes;

   //  For the highlighted row, highlight every cell
   var cells = row.childNodes;
   for(var j = 0; j < cells.length; j++)
   {
      if(cells[j] && (cells[j].nodeName == 'TD'))
      {
      	addClass(cells[j],'crosshair')
      }
   }
}

// Utility function to find the column index of a cell
function cgvGetColumnIndex(cell)
{
   var row = cell.parentNode;
   var cellsInRow = row.childNodes
   for(var i = 0; i < cellsInRow.length; i++)
   {
      if(cellsInRow[i] == cell)
      {
         return i;
      }
   }
}


