
//=============================================================================
// MANIPULATING FORM ELEMENTS
//=============================================================================

// disable() - disables a form element and sets the background colour for
//     input.text, textarea and select elements.
function disable(formElement) {
    if (formElement == null) return;
    formElement.disabled = true;
    formElement.style.color = "#999999"; // make it look "disabled"
                                         // If this changes in verity.css, it needs to be changed here too.

    if ((formElement.type == "textarea") || (formElement.type == "select-one") || (formElement.type == "select-multiple")) {
        formElement.className = "disabled";
    } else if (formElement.type == "text" || formElement.type == "password") {
        formElement.className = "textDisabled";
    }

}

// enable() - enables a form element and sets the background colour for
//     input.text, textarea and select elements.
function enable(formElement) {
    formElement.disabled = false;
    formElement.style.color = "#000000"; // make it look "disabled"

    if ((formElement.type == "textarea") || (formElement.type == "select-one") || (formElement.type == "select-multiple")) {
        formElement.className = "";
    } else if (formElement.type == "text" || formElement.type == "password") {
        formElement.className = "text";
    }
}

// getSelectedRadioValue() - return the value of a selected radio button in a specified group,
//     or null if none selected.
function getSelectedRadioValue(radioGroup) {
    var i;                                                          // for counting

    // Make sure the object exists.
    if (radioGroup) {
        // Get the selected value
        for (i = 0; i < radioGroup.length; i++) {
            if (radioGroup[i].checked) {
                return radioGroup[i].value;
            }
        }
    }

    // Nothing was checked, return null.
    return null;
}


// getSelectedSelectValue() - return the value of a selected option in a select box,
//    or null if none selected.
function getSelectedSelectValue(sel) {

    // Make sure the object exists
    if (sel) {
        // Get the index of the selected item (returns -1 if none selected),
        // and return the value of the selected option.
        var o = sel.selectedIndex;
        if (o >= 0) {
            // If the empty option is chosen, it's the same as not choosing
            // anything, so return null.
            if (sel.options[o].value == "") {
                return null;
            } else {
                return sel.options[o].value;
            }
        }
    }

    // Nothing was checked, return null.
    return null;
}


// addOptionToSelect() - add a string to a select element.
//      Expects the form element, the text, and the value.
//      The added option is selected by default; send blnSelect as false to
//      avoid this.
function addOptionToSelect(sel, txt, val, blnSelect) {
    var found;                  // whether or not we found an option with the same value

    if (sel) {
        // Check for duplicates; if found, select that option.
        for (var i = 0; i < sel.length; i++) {
            if (sel.options[i].value == val) {
                sel.selectedIndex = i;
                found = 1;
                break;
            }
        }

        // If not found, add a new option to the bottom of the list and select it.
        if (found != 1) {
            sel.options[sel.length] = new Option(txt, val);
            if ((blnSelect == null) || (blnSelect == true)) {
                sel.selectedIndex = sel.length - 1;
            }
        }
    } else {
        alert("Unable to locate select element.");
    }
}


// deleteSelectedOptions() - delete selected options from a select element.
//     Expects the form element as an argument.
function deleteSelectedOptions(sel) {
    if (sel) {
        var si = sel.selectedIndex;

        if (si != -1) {
            // Keep checking until all selected options are deleted.
            while (si != -1) {
                sel.options[si] = null;
                si = sel.selectedIndex;
            }

        } else {
            alert("Please select an item or items to delete.");
        }
    } else {
        alert("Unable to locate select element.");
    }
}

// setSelect() - set selected values in a select box
// expects select field and array of string values as an argument
function setSelect(field, values) {
    if ( field != null && values != null && field.options) {
        for (var i = 0; i < field.options.length; i++) {
            field.options[i].selected = false;
            if ( values.constructor.toString().indexOf("Array") >= 0 ) {
                for (var j = 0; j < values.length; j++) {
                    if (field.options[i].value.toLowerCase() == values[j].toLowerCase()) {
                        field.options[i].selected = true;
                        break;
                    }
                }
            } else {
                if (field.options[i].value.toLowerCase() == values.toLowerCase()) {
                    field.options[i].selected = true;
                    break;
                }
            }
        }
    }
}

// getSelect() - get selected values in a select box
// returns array of values
function getSelect(field) {
    var values;
    values = new Array();
    if (field == null) return null;
    for (var i = 0; i < field.length; i++) {
        if (field.options[i].selected == true) {
            values[values.length] = field.options[i].value;
        }
    }

    return values;
}

// getSelectOptions() - get all option values in a select box
// returns array of values
function getSelectOptions(field) {
    var values;
    values = new Array();
    if (field == null) return null;
    for (var i = 0; i < field.length; i++) {
        values[values.length] = field.options[i].value;
    }

    return values;
}

// - setRadio() - set a value in a radio box
// expects radio element and value to set as an argument
function setRadio(field, value) {
    if (field) {
        for (var i = 0; i < field.length; i++) {
            if (field[i].value.toLowerCase() == value.toLowerCase()) {
                field[i].checked = true;
            }
        }
    }
}


// getFormValue() - return the value of a form element, regardless of type.
// Supported types are:
//    input: hidden, password, text, checkbox, radio, button, select, reset
//    select (single or multiple)
//    textarea
//
// Returns:
//   String for hidden/password/text/radio/button/single select/button/select/reset.
//   String for checkbox if checked, otherwise null.
//   String array for multiple select.
//   Null if element type is not supported.
function getFormValue(elm) {
    var val = null;

    if (elm && elm.type) {
        if (elm.type == "hidden" || elm.type == "password" || elm.type == "text" || elm.type == "textarea"
                || elm.type == "button" || elm.type == "submit" || elm.type == "reset") {
            val = elm.value;
        } else if (elm.type == "select-one") {
            val = getSelectedSelectValue(elm);
        } else if (elm.type == "select-multiple") {
            val = getSelect(elm);
        } else if (elm.type == "radio") {
            val = getSelectedRadioValue(elm);
        } else if (elm.type == "checkbox") {
            if (elm.checked) {
                val = elm.value;
            }
        }
    }

    return val;
}


// usage:
// <form name="formname">
// <input type="checkbox" value="1" name="cbxname">1
// <input type="checkbox" value="2" name="cbxname">2
// <a onclick="checkAll(this.formname.cbxname,true);">Select All</a> |
// <a onclick="checkAll(this.formname.cbxname,false);">Clear All</a>

// Checks or unchecks all the checkboxes in objCheckboxArr
function checkAll(objCheckboxArr, blnMode)
{
    if ( objCheckboxArr != null && typeof objCheckboxArr == "object") {
        if ( objCheckboxArr.type == "checkbox" && !objCheckboxArr.disabled ) {
            objCheckboxArr.checked = blnMode;
        } else {
            var i;
            for (i = 0; i < objCheckboxArr.length; i++) {
                if ( objCheckboxArr[i] != null && objCheckboxArr[i].type == "checkbox" && !objCheckboxArr[i].disabled ) {
                    objCheckboxArr[i].checked = blnMode;
                }
            }
        }
    }
}
