function addEvent(elm, evType, fn, useCapture)
{
  if (elm.addEventListener) { 
    elm.addEventListener(evType, fn, useCapture); 
    return true; 
  } else if (elm.attachEvent) { 
    var r = elm.attachEvent('on' + evType, fn); 
    return r; 
  } else {
    elm['on' + evType] = fn;
  }
}

function ascendDOM(e, target) {
  while (e.nodeName.toLowerCase() != target && 
      e.nodeName.toLowerCase() != 'html')
    e = e.parentNode;
  return (e.nodeName.toLowerCase() == 'html') ? null : e;
}

function setCellColor(element,clName)
{
	if (navigator.appName=="Microsoft Internet Explorer")
		element.className=clName;
	else
		element.setAttribute("class",clName);
}

function setRowColor(element,clName)
{
	var cells = element.getElementsByTagName('td');
	if(clName=="mark")
	{
		if(cells[0].className=="mark")
			for (i=0;i<cells.length;i++)
				setCellColor(cells[i],"normal");
		else
			for (i=0;i<cells.length;i++)
				setCellColor(cells[i],"mark");
		return;
	}
	else
	{
		if(cells[0].className=="mark")
			return;		
		for (i=0;i<cells.length;i++)
			setCellColor(cells[i],clName);
	}
}

function hover(e)
{
  var el;
  if (window.event && window.event.srcElement)
    el = window.event.srcElement;
  if (e && e.target)
    el = e.target;
  if (!el) return;
  var parent_row = ascendDOM(el, 'tr');
  if (parent_row == null)  return;	
  setRowColor(parent_row,'hover');
}


function normal(e)
{
 var el;
  if (window.event && window.event.srcElement)
    el = window.event.srcElement;
  if (e && e.target)
    el = e.target;
  if (!el) return;
  var parent_row = ascendDOM(el, 'tr');
  if (parent_row == null)  return;	
  setRowColor(parent_row,'');
}

function mark(e)
{
  var el;
  if (window.event && window.event.srcElement)
    el = window.event.srcElement;
  if (e && e.target)
    el = e.target;
  if (!el) return;
  var parent_row = ascendDOM(el, 'tr');
  if (parent_row == null)  return;	
  setRowColor(parent_row,'mark');
}

function addListeners()
{
  if (!document.getElementsByTagName) return;
  var rows = document.getElementsByTagName('tr');
  for (var i = 1; i < rows.length; i++) {
    addEvent(rows[i], "mouseover", hover, false);
    addEvent(rows[i], "mouseout", normal, false);
    addEvent(rows[i], "mousedown", mark, false);
  } 
}
var marked_row = new Array;

function setP(theRow, theRowNum, theAction, theDefaultColor)
{
var thePointerColor = '#BFEBFF';
var theMarkColor = '#CCFFCC';
    var theCells = null;
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;

    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }

    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    }

    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
        }
    }
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }

    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    }

    if (newColor) {
        var c = null;
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } 
        }

        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } 

    return true;
} 
