//
// Contains general routines used by many other JavaScript files.
//

  // Trims whitespace from the beginning and end of the specified value.
function trim(s) {
    if (s != null) {
        while ((s.length > 0) && (s.charAt(0) == " ")) {
            s = s.substr(1);
        }
        while ((s.length > 0) && (s.charAt(s.length - 1) == " ")) {
            s = s.substring(0, s.length - 1);
        }
    }
    return s;
}
  
  // Trims whitespace from the end of the specified value.
function trimRight(s) {
    if (s != null) {
        while ((s.length > 0) && (s.charAt(s.length - 1) == " ")) {
            s = s.substring(0, s.length - 1);
        }
    }
    return s;
}

  // Trims whitespace from the beginning of the specified value.
function trimLeft(s) {
    if (s != null) {
        while ((s.length > 0) && (s.charAt(0) == " ")) {
            s = s.substr(1);
        }
    }
    return s;
}
  
  //Moves items from a menu box to text area
function moveItem(moveMenu, moveTextArea) {
    var textToMove = moveMenu.options[moveMenu.selectedIndex].value;
    if (textToMove == "") {
        return;
    }
    var textAreaText = moveTextArea.value;
    if (textToMove != "") {
        textAreaText += textToMove;
    }
    moveTextArea.value = textAreaText;
}


  // Detects what DHTML model is available in the browser.
  // Returns one of the following codes:
  //   0 - No DHTML support.
  //   1 - Internet Explorer 4.
  //   2 - Netscape 4.
  //   3 - Internet Explorer 5 and 6, Netscape 6, Mozilla 0.
function detectDHTML() {
    // Ranked in order of preference.
    if (document.getElementById) {
        return 3;
    }
    if (document.all) {
        return 1;
    }
    if (document.layers) {
        return 2;
    }
}

  // resets inactive element to ""
  // sets the value of hiddenElement to the value of activeElement
function resetElement(activeElement, inactiveElement, hiddenElement) {
    inactiveElement.selectedIndex = 0;
    hiddenElement.value = activeElement[activeElement.selectedIndex].value;
    return true;
}
function highlightTableRows(tableId) {
    var previousClass = null;
    var table = document.getElementById(tableId);
    var tbody = table.getElementsByTagName("tbody")[0];
    var rows = tbody.getElementsByTagName("tr");
    var highlightedRow = null;
    
    // add event handlers so rows light up and are clickable
    for (i = 0; i < rows.length; i++) {
        var levelCell = rows[i].cells[1];
        rows[i].className += " " + levelCell.innerHTML.toLowerCase();
        rows[i].onmouseover = function () {
            this.style.cursor = 'pointer';
            if (highlightedRow == null) {
                previousClass = this.className;
                this.className += " over";
            }
        };
        rows[i].onmouseout = function () {
            this.style.cursor = '';        
            if (highlightedRow == null) {
                this.className = previousClass;
            }
        };
        rows[i].onclick = function () {
            if (highlightedRow == this) {
                highlightedRow = null;
                this.className = previousClass;
            } else {
                if (highlightedRow != null) {
                    highlightedRow.className = previousClass;
                    previousClass = this.className;                    
                }
                highlightedRow = this;
                this.className += " over";
            }
//            var cell = this.getElementsByTagName("td")[0];
//            if (cell.getElementsByTagName("a").length > 0) {
//                var link = cell.getElementsByTagName("a")[0];
//                if (link.onclick) {
//                    call = link.getAttributeValue("onclick");
//                    // this will not work for links with onclick handlers that return false
//                    eval(call);
//                } else {
//                    location.href = link.getAttribute("href");
//                }
//                this.style.cursor = "wait";
//            }
        };
    }
}

