// These variables are for saving the original background colors
var savedStates=new Array();

/////////////////////////////////////////////////////
// This function takes an element as a parameter and
//   returns an object which contain the saved state
//   of the element's background color.
/////////////////////////////////////////////////////
function saveBackgroundStyle(myElement)
{
  saved=new Object();
  saved.element=myElement;
  saved.className=myElement.className;
  saved.backgroundColor=myElement.style["backgroundColor"];
  saved.id = myElement.id;
  return saved;
}

/////////////////////////////////////////////////////
// This function takes an element as a parameter and
//   returns an object which contain the saved state
//   of the element's background color.
/////////////////////////////////////////////////////
function restoreBackgroundStyle(savedState)
{

    var restore = true;
    row = savedState.element;

    if (row != undefined){

    var objCheckBoxes = getInputElementsByType(row, 'checkbox');

    if(objCheckBoxes.length)
        restore = objCheckBoxes[0].checked ? false : true;

    if (restore) {

        savedState.element.style["backgroundColor"]=savedState.backgroundColor;
        if (savedState.className)
        {
          savedState.element.className=savedState.className;
        }

    }
    }


}

/////////////////////////////////////////////////////
// This function is used by highlightTableRow() to find table cells (TD) node
/////////////////////////////////////////////////////
function findNode(startingNode, tagName)
{
  // on Firefox, the TD node might not be the firstChild node of the TR node
  myElement=startingNode;
  var i=0;
  while (myElement && (!myElement.tagName || (myElement.tagName && myElement.tagName!=tagName)))
  {
    myElement=startingNode.childNodes[i];
    i++;
  }
  if (myElement && myElement.tagName && myElement.tagName==tagName)
  {
    return myElement;
  }
  // on IE, the TD node might be the firstChild node of the TR node
  else if (startingNode.firstChild)
    return findNode(startingNode.firstChild, tagName);
  return 0;
}

/////////////////////////////////////////////////////
// Highlight table row.
// newElement could be any element nested inside the table
// highlightColor is the color of the highlight
/////////////////////////////////////////////////////
function highlightTableRow(myElement, newClass)
{
restoreHighlightedRows();
highlightSelectedRow(myElement, newClass);

}


function restoreHighlightedRows() {

  // Restore color of the previously highlighted row

     for( var savedState in savedStates ) {

        restoreBackgroundStyle(savedStates[savedState]);

    }


}

function highlightSelectedRow(myElement, newClass) {

  // If you don't want a particular row to be highlighted, set it's id to "header"
  if (!myElement || (myElement && myElement.id && myElement.id=="tbl_header") )
    return;

  // Highlight every cell on the row
  if (myElement)
  {
    var tableRow=myElement;

    // Save the backgroundColor style OR the style class of the row (if defined)
    if (tableRow)
    {

        if(tableRow.className != newClass) {
	  savedStates[tableRow.id]=saveBackgroundStyle(tableRow);
      tableRow.className = newClass;
      tableRow.style.cursor='pointer';
      }
    }

  }

}


function DoNav(theUrl)
{
  document.location.href = theUrl;
}

function checkUncheckAll(e, tbl_id) {

    var table = document.getElementById(tbl_id);
    var objCheckBoxes = getInputElementsByType(table, 'checkbox');

    if(!objCheckBoxes)
	    return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes) {
		objCheckBoxes.checked = e.value != TBL_SELECTALL ? false : true;
        objCheckBoxes.checked ? highlightSelectedRow(document.getElementById('row_'+objCheckBoxes.id),'cellb4') : restoreHighlightedRows();
    } else {

        for(var i = 0; i < countCheckBoxes; i++){
			objCheckBoxes[i].checked = e.value != TBL_SELECTALL ? false : true;
            objCheckBoxes[i].checked ? highlightSelectedRow(document.getElementById('row_'+objCheckBoxes[i].id),'cellb4') : restoreHighlightedRows();
        }

    }

     e.value = e.value == TBL_SELECTALL ? TBL_DESELECTALL : TBL_SELECTALL;

}

function getInputElementsByType(e,type){

    var oInp = e.getElementsByTagName('input');

    var oElem = new Array();
    var j=0;

    for(var i=0;i<oInp.length;i++){
      if(oInp[i].getAttribute('type')==type){
      oElem[j] = oInp[i];j++;
      }
    }

    return oElem;

}