/**
 *  Sets the action parameter of the document's form element.
 *
 *  The document must only contain one form and the relative path is prepended
 *  to the action
 *
 *  @param action The target action without the relative path
 */
function setFormActionMethod(method) {
    var currentAction = document.forms[0].action;
    var lastIndexOf = currentAction.lastIndexOf("?");
    var relativePath = currentAction;
    if( lastIndexOf > -1 )
    {
        relativePath = currentAction.substring(0, (lastIndexOf));
    }
    document.forms[0].action = relativePath + "?method=" + method;
}


/**
 *  Sets the action parameter of the document's form element.
 *
 *  The document must only contain one form and the relative path is prepended
 *  to the action
 *
 *  @param action The target action without the relative path
 */
function setFormAction(action) {
    var currentAction = document.forms[0].action;
    var lastIndexOf = currentAction.lastIndexOf("/");
    var relativePath = currentAction.substring(0, (lastIndexOf + 1));

    document.forms[0].action = relativePath + action;
}

/**
 *  Determines whether the ASCII key code passed as a parameter is a digit
 *
 *  Can be used to ensure that only numeric data can be entered into a text element.
 *      ex.) <html:text onkeypress="return isDigit(window.event.keyCode)"/>
 *
 *  @param action The target action without the relative path
 */
function isDigit(keyCode) {
    if (keyCode >= 48 && keyCode <= 57)
        return true;
    else
        return false;
}

/**
 *  Simulates a selection of an area on the screen.  For instance a DIV element
 *  can change it's background color to simulate a hilite as a mouse moves over it.
 *
 *  @param element The element to apply the style changes
 */
function select(element) {
    // Change the background color to a gold hilite and the mouse cursor to a hand
    element.style.backgroundColor = "#E0C266";
    element.style.cursor = "hand";
}

/**
 *  Simulates the unselection of an area on the screen.  For instance as a mouse moves
 *  off an area on the screen and the hilite disappears
 *
 *  @param action The target action without the relative path
 */
function unselect(element) {
    element.style.backgroundColor = "#eae5e1";
}

/**
 * Enables an element depending on it's or another element's value
 *
 * @param elementToCheck the element who's value we want to check
 * @param elementToEnable the element we want to enable
 * @param value the value for which we want to check
 */
function enableElementPer(elementToCheck, elementToEnable, value) {
    if (elementToCheck.value == value)
        elementToEnable.disabled = false;

    else {
        elementToEnable.disabled = true;
        elementToEnable.value = "";
    }
}

/**
 * Enables an element depending on it's or another element's value
 *
 * @param elementToCheck the element who's value we want to check
 * @param elementToEnable the element we want to enable
 * @param isChecked the value for which we want to check
 */
function enableElementPerCheckbox(elementToCheck, elementToEnable, isChecked) {
    if (elementToCheck.checked == isChecked)
        elementToEnable.disabled = false;

    else {
        elementToEnable.disabled = true;
        elementToEnable.value = "";
    }
}


/**
 * Check a text area for max input length. Limit the user to the specified length
 * @param name The textarea form element name
 * @param maxlen  The max length allowed
 */
function limitTextAreaMaxLength(name, maxlen) {
    var element = "document.forms[0].elements['" + name + "']";
    var curlen = eval(element + ".value.length");

    if (curlen >= maxlen) {
        return false;
    }
    else {
        return true;
    }
}

/**
 * Check a text area for max input length. Limit the user to the specified length
 * @param name The textarea form element name
 * @param maxlen  The max length allowed
 */
function validateTextAreaMaxLength(label, name, maxlen) {
    var element = "document.forms[0].elements['" + name + "']";
    var curlen = eval(element + ".value.length");

    if (curlen <= maxlen) {
        return true;
    }
    else {
        if (confirm(label + " too large. Limited to " + maxlen + " characters. Truncate the comment?")) {
            eval(element + ".value = " +
                element + ".value.substr(0," + maxlen + ")");
        }
        return false;
    }
}

/**
 * If the element content has reached the specified limit, then
 * move focus to the second specified element.
 * @param maxLength The length at which the movement to the next
 *                      field occurs
 * @param target The name of the form element to which focus will be moved
 */
function moveFocusOnMaxLength(maxLength, target) {
    var dest = eval("document.forms[0].elements['" + target + "']");
    var curlen = event.srcElement.value.length;

    // "tab in" and "back tab in"
    if ((event.keyCode != 9) &&
        (event.keyCode != 16) &&
        (curlen >= maxLength)) {
        dest.focus();
        dest.select();
        return true;
    }
    else {
        return false;
    }

}


/**
 * Change the CSS class name of the element specified by id.
 *
 * @param id the id of the element to change
 * @param className the new CSS class name
 */
function setClassName(id, className)
{
    if (document.getElementById)
    {
        var nodeObj = document.getElementById(id)
        nodeObj.className = className;
    }
}


/**
 * Turn on the display of the asterisk of an element identified by the specified id.
 *
 * @param idToSet the element id to set
 */
function requiredAsteriskOn(idToSet)
{
    setClassName(idToSet, "required");
}


/**
 * Turn off the display of the asterisk of an element identified by the specified id.
 *
 * @param idToSet the element id to set
 */
function requiredAsteriskOff(idToSet)
{
    setClassName(idToSet, "notRequired");
}


/**
 * Display the required asterisk of an element identified by the specified id
 * depending on it's or another element's value
 *
 * @param elementToCheck the element who's value we want to check
 * @param idToChange the element we want to set the class
 * @param value the value for which we want to check
 */
function displayAsteriskPer(elementToCheck, idToChange, value)
{
    if (elementToCheck.value == value)
    {
        requiredAsteriskOn(idToChange);
    }
    else
    {
        requiredAsteriskOff(idToChange);
    }
}


/**
 * Take first textbox element passed into function and paste it in second if
 * the second textbox is empty
 *
 * @param srcBox the source textbox value
 * @param destBox the destination textbox of source value
 */
function textboxSrcToDest(srcBox, destBox)
{
    var src = eval("document.forms[0].elements['" + srcBox + "']");
    var dest = eval("document.forms[0].elements['" + destBox + "']");
    var emptyString = "";

    if (dest.value == emptyString)
    {
        dest.value = src.value;
    }
}


/**
 * Set the value of a hidden input element based on the state of the
 * checkbox control.
 *
 * @param checkbox the HTML checkbox control
 * @param hiddenElement the hidden input to track the checkbox state 
 */
function updateHiddenCheckbox(checkbox, hiddenElement)
{
    if (checkbox.checked == true)
    {
        hiddenElement.value = "true";
    }
    else
    {
        hiddenElement.value = "false";
    }
}


/**
 * Set the checkbox control based on the state of a hidden variable.
 *
 * @param checkbox the HTML checkbox control
 * @param hiddenElement the hidden input to track the checkbox state 
 */
function determineCheckboxPerHidden(checkbox, hiddenElement)
{
    if (hiddenElement.value == "true")
    {
        checkbox.checked = true;
    }
}


