// F. Permadi 2005.
// Highlights table row
// Copyright (C) F. Permadi
// This code is provided "as is" and without warranty of any kind.  Use at your own risk.



// These variables are for saving the original background colors
var savedStates=new Array();
var savedStateCount = 0;
var selectedElement = 0;
var selectedState = new Object();
var selectedColor = "#ff0000";

function SetVariable(PProperty, PValue) {
    PageMethods.Variable(PProperty, PValue, OnSetVariableSuccess, OnSetVariableFailure);
}

function OnSetVariableSuccess(result, userContext, methodName) {
    
}

function OnSetVariableFailure(error, userContext, methodName) {
    ClosePage(false, '', error.get_message());
}


function SetValue(PElement, PValue) {
    var lbl = parent.document.getElementById(PElement);
    lbl.value = PValue;
}

function ShowConfirmation(PConfirm) {
    if (PConfirm == '') {
        return true;
    }
    else {
        return confirm(PConfirm);
    }
}

function SelectClick(PHiddenField, PConfirm, PAlert) {
    var lresult = false;
    if (selectedElement != 0) {
        if (PConfirm == '' || ShowConfirmation(PConfirm) == true) {
            document.getElementById(PHiddenField).value = selectedElement.getAttribute("rel");
            lresult = true;
        }
    }
    else {
        if (PAlert != '') {
            ZobrazitOznam('', PAlert);
        }
        document.getElementById(PHiddenField).value = '';
    }
    return lresult;
}

function GetClick(PConfirm, PAlert) {
    var lresult = 0;
    if (selectedElement != 0) {
        if (PConfirm == '' || ShowConfirmation(PConfirm) == true) {
            lresult = selectedElement.getAttribute("rel");
        }
    }
    else {
        if (PAlert != '') {
            ZobrazitOznam('', PAlert);
        }
    }
    return lresult;
}

/////////////////////////////////////////////////////
// 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"];    
    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) {
    if (savedState.element == selectedElement) {
        savedState.element.style["backgroundColor"] = selectedColor;
    }
    else {
        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 highlightTableRowVersion(myElement, highlightColor) {
    var i = 0;
    // Restore color of the previously highlighted row
    for (i; i < savedStateCount; i++) {
        restoreBackgroundStyle(savedStates[i]);
    }
    savedStateCount = 0;

    // If you don't want a particular row to be highlighted, set it's id to "header"
    if (!myElement || (myElement && myElement.id && myElement.id == "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) {
            savedStates[savedStateCount] = saveBackgroundStyle(tableRow);
            savedStateCount++;
        }

        // myElement is a <TR>, then find the first TD
        var tableCell = findNode(myElement, "TD");

        // Loop through every sibling (a sibling of a cell should be a cell)
        // We then highlight every siblings
        while (tableCell) {
            // Make sure it's actually a cell (a TD)
            if (tableCell.tagName == "TD") {
                // If no style has been assigned, assign it, otherwise Netscape will 
                // behave weird.
                if (!tableCell.style) {
                    tableCell.style = {};
                }
                else {
                    savedStates[savedStateCount] = saveBackgroundStyle(tableCell);
                    savedStateCount++;
                }
                // Assign the highlight color
                tableCell.style["backgroundColor"] = highlightColor;
                if (myElement == selectedElement) { tableCell.style["backgroundColor"] = selectedColor; }

                // Optional: alter cursor
                tableCell.style.cursor = 'default';
            }
            // Go to the next cell in the row      
            tableCell = tableCell.nextSibling;
        }
    }
}

function highlightTableRowVersionA(myElement, highlightColor)
{
  var i=0;
  // Restore color of the previously highlighted row
  for (i; i < savedStateCount; i++) {
      restoreBackgroundStyle(savedStates[i]);
  }
  savedStateCount=0;

  // If you don't want a particular row to be highlighted, set it's id to "header"
  if (!myElement || (myElement && myElement.id && myElement.id=="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) {
            savedStates[savedStateCount] = saveBackgroundStyle(tableRow);
            savedStateCount++;
        }

        // myElement is a <TR>, then find the first TD
        var tableCell = findNode(myElement, "TD");
        var ltableCell;

        tableCell = tableCell.nextSibling;

        // Loop through every sibling (a sibling of a cell should be a cell)
        // We then highlight every siblings
        while (tableCell) {
            ltableCell = tableCell.nextSibling;
            if (!ltableCell) {
                break;
            }
            // Make sure it's actually a cell (a TD)
            if (tableCell.tagName == "TD") {
                // If no style has been assigned, assign it, otherwise Netscape will 
                // behave weird.
                if (!tableCell.style) {
                    tableCell.style = {};
                }
                else {
                    savedStates[savedStateCount] = saveBackgroundStyle(tableCell);
                    savedStateCount++;
                }
                // Assign the highlight color
                tableCell.style["backgroundColor"] = highlightColor;
                if (myElement == selectedElement) { tableCell.style["backgroundColor"] = selectedColor; }

                // Optional: alter cursor
                tableCell.style.cursor = 'default';
            }

            tableCell = ltableCell;
            // Go to the next cell in the row      
        }
    }
}

function RowClick(myElement, PSelectedColor) {
    if (selectedElement) {
        if (selectedElement != myElement) {
            selectedElement.style["backgroundColor"] = selectedState;
            SelectRow(myElement, PSelectedColor);
        }
    }
    else {
        SelectRow(myElement, PSelectedColor);
    }
}

function SelectRow(myElement, PSelectedColor) {
    selectedColor = PSelectedColor;
    var i = 0;
    for (i; i < savedStateCount; i++) {
        restoreBackgroundStyle(savedStates[i]);
    }
    selectedElement = myElement;
    selectedState = myElement.style["backgroundColor"]
    highlightTableRowVersionA(myElement, selectedColor)
}
