

var FormUtils = new Object();
//
// This function actually clears form elements (no buttons, hiddens).
//
FormUtils.clearForm = function (frm)
{
    if (frm == null)
        return;

    for (var i=0; i < frm.elements.length; i++)
    {
        this.clearElement(frm.elements[i]);
    }
}

FormUtils.clearElement = function(ele)
{
    if (ele == null)
        return;

    if (ele.type == "checkbox")
    {
        ele.checked = false;
        ele.value = '';
    }
    else if (ele.type == "radio")
    {
        ele.checked = false;
    }
    else if (ele.type == "select-one" ||
             ele.type == "select-multiple")
    {
        for (var i = 0; i < ele.options.length; i++)
        {
            ele.options[i].selected = false;
        }

        // would look odd if select-one had no selectedIndex
        ele.selectedIndex = ele.type == "select-one" ? 0 : -1;
    }
    else if (ele.type == "text" ||
             ele.type == "password" ||
             ele.type == "textarea")
    {
            ele.value = '';
    }
}

// selectSupport.js

FormUtils.clearOptions = function (options)
{
    var size = options.length;

    var i = 0;
    while (options.length > 0)
    {
        options[0] = null;
        i++;
    }
}

///
// A version of copy where you copy the option pointer instead of creating a new option.
// This can be problematic with different browsers
////
FormUtils.copyArrayToOptions = function (a, options)
{
    for (var i=0; i < a.length; i++)
    {
        if (a[i] != null)
        {
            options[options.length] = a[i];
        }
    }
}

///
// A version of copy where you create a new option off of the src
////
FormUtils.copyArrayToOptionsByValue = function (a, options)
{
    for (var i=0; i < a.length; i++)
    {
        if (a[i] != null)
        {
            var opt = a[i];
            options[options.length] = new Option(opt.text, opt.value, false);
        }
    }
}
////
// Basic alphabetical comparator
// @param a option1
// @param b option2
////
FormUtils.optionCompare = function(a, b)
{
    if (a == null || b== null)
    {
        return 0;
    }
    if (a.text == b.text)
    {
        return 0;
    }
    else if (a.text < b.text)
    {
        return -1;
    }
    else
    {
        return 1;
    }
}

FormUtils.getSelectedOptionValues = function (select)
{
    var a = new Array();
    if (select.selectedIndex > -1)
    {
        for(var i=0; i < select.options.length; i++)
        {
            if (select.options[i].selected)
            {
                a[a.length] = select.options[i].value;
            }
        }
    }
    return a;
}

FormUtils.getAllOptionValues = function (select)
{
    var a = new Array();
    for (var i=0; i < select.options.length; i++)
    {
        var val = select.options[i].value;
        if (val != "")
        {
            a[a.length] = val;
        }
    }
    return a;
}

FormUtils.selectAllOption = function (select)
{
    var alloption = null;
    var selectedItems = false;
    for(var i = 0; i < select.length; i++)
    {
        var current = select.options[i];
        if (current.text == "All" || current.value == "")
        {
            alloption = current;
        }
        else
        {
            if (current.selected)
            {
                selectedItems = true;
            }
        }
    }
    if (alloption != null)
    {
        if (selectedItems && alloption.selected)
        {
            alloption.selected = false;
        }
    }
}

FormUtils.selectOptions = function (select, array)
{
    //first let's hash the options in the select by value and set selected to false
    var options = new Array();
    for (var i = 0; i < select.options.length; i++)
    {
        select.options[i].selected = false;
        var val = select.options[i].value;
        options[val] = select.options[i];
    }

    for (var j = 0; j < array.length; j++)
    {
        if (options[array[j]] !== undefined)
        {
            options[array[j]].selected = true;
        }
    }
}

FormUtils.removeOptionSelected = function (elSel)
{
  for (var i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
}

FormUtils.selectAllOptions = function (elSel)
{
  for (var i = elSel.length - 1; i>=0; i--) {
    elSel.options[i].selected = true;
  }
}

FormUtils.submitUnderway = false;

FormUtils.checkSubmit = function ()
{
    if (this.submitUnderway)
    {
        alert("Spoke is processing your previous request.  Please wait a few moments.");
        return false;
    }
    this.submitUnderway = true;
    return true;
}

FormUtils.validateCheckboxes = function (frm, warning)
{
    var hasChecked = false;
    for (var i = 0; i < frm.elements.length; i++)
    {
        if (frm.elements[i].type == "checkbox" && frm.elements[i].value != "" && frm.elements[i].checked && frm.elements[i].getAttribute("group") != null)
        {
            hasChecked = true;
            break;
        }
    }
    if (! hasChecked)
    {
        alert (warning);
        this.submitUnderway = false;
        return false;
    }
    return true;
}


// checkAll.js - utility function to check/uncheck all checkboxes

// The "check all" box onclick handler.  Checks or unchecks all the checkboxes
// in a checkbox group based on the "check all" box.
//
// oCheckAllBox - the "check all" box
// [bHighlight] - highlights each checkbox's row [default: true]
function doCheckAll(oCheckAllBox, bHighlight)
{
    bHighlight = defaultParam(bHighlight, true);
    var sGroupName = oCheckAllBox.getAttribute("id").replace(/All$/, "");
    var oaCheckboxes = getCheckboxesByGroupName(oCheckAllBox.form, sGroupName);
    for (var i = 0; i < oaCheckboxes.length; i++)
    {
        checkCheckbox(oaCheckboxes[i], isChecked(oCheckAllBox));
        highlightCheckboxRow(oaCheckboxes[i], bHighlight);
    }
}

// Gets all the checkboxes in a group in a form based on the group name.
//
// oForm      - the parent form
// sGroupName - the name of the checkbox group
function getCheckboxesByGroupName(oForm, sGroupName)
{
    var oaInputs = oForm.getElementsByTagName("input");
    var oaCheckboxes = new Array();
    for (var i = 0; i < oaInputs.length; i++)
    {
        if (oaInputs[i].getAttribute("type").toLowerCase() == "checkbox"
            && sGroupName == oaInputs[i].getAttribute("group"))
        {
            oaCheckboxes[oaCheckboxes.length] = oaInputs[i];
        }
    }
    return oaCheckboxes;
}

// The handler for a member of a checkbox group.
//
// oCheckbox - the checkbox that was clicked
// [bHighlight] - highlights each checkbox's row [default: true]
function checkMember(oCheckbox, bHighlight)
{
    highlightCheckboxRow(oCheckbox, defaultParam(bHighlight, true));

    // don't need to highlight all the others because presumably they're all
    // ready highlighted correctly (if we're highlighting)
    checkCheckAll(oCheckbox.form, oCheckbox.getAttribute("group"), false);
}

// Checks the "check all" checkbox if all the members of a checkbox group are
// checked.  Unchecks it otherwise.  Also highlights the checkbox row if
// necessary.
//
// oForm        - the parent form
// sGroupName   - the name of the checkbox group
// [bHighlight] - highlights each checkbox's row [default: true]
function checkCheckAll(oForm, sGroupName, bHighlight)
{
    if (document.getElementById(sGroupName + "All") == null)
    {
        return;
    }

    bHighlight = defaultParam(bHighlight, true);
    var oaCheckboxes = getCheckboxesByGroupName(oForm, sGroupName);

    // Workaround Firefox (v. 1.06) problem
    // Turns out that setAttribute("checked", value) is broken.  Any value would interpreted checked.
    checkCheckbox(document.getElementById(sGroupName + "All"),isAllChecked(oaCheckboxes));
    highlightAllChecked(oaCheckboxes, bHighlight);
}

// Determines if all the checkboxes are checked.
//
// oaCheckboxes - the member checkboxes of a checkbox group
function isAllChecked(oaCheckboxes)
{
    if (oaCheckboxes == null || oaCheckboxes.length == 0)
    {
        return false;
    }

    for (var i = 0; i < oaCheckboxes.length; i++)
    {
        if (!isChecked(oaCheckboxes[i]))
        {
            return false;
        }
    }
    return true;
}

// Highlights all the checked checboxes if necessary.
//
// sGroupName   - the name of the checkbox group
// [bHighlight] - highlights each checkbox's row [default: true]
function highlightAllChecked(oaCheckboxes, bHighlight)
{
    bHighlight = defaultParam(bHighlight, true);
    if (bHighlight)
    {
        for (var i = 0; i < oaCheckboxes.length; i++)
        {
            highlightCheckboxRow(oaCheckboxes[i], bHighlight);
        }
    }
}

// Programmatically checks a checkbox.  Only called from checkAll().
// Broken out into a method so it can be overriden if necessary.
//
// oCheckbox - checkbox to check or uncheck
// bChecked  - check or uncheck
function checkCheckbox(oCheckbox, bChecked)
{
//    oCheckbox.setAttribute("checked", bChecked);
    oCheckbox.checked = bChecked;
}

// oCheckbox - the checkbox to test
function isChecked(oCheckbox)
{
    return oCheckbox == null ? false : oCheckbox.checked;
}

// Highlights the row containing the checkbox.
//
// oCheckbox    - the checkbox
// [bHighlight] - highlights each checkbox's row [default: true]
function highlightCheckboxRow(oCheckbox, bHighlight)
{
    if (null != oCheckbox && "undefined" != oCheckbox)
    {
        bHighlight = defaultParam(bHighlight, true);
        if (bHighlight)
        {
            var oParentTR = getCheckboxParentTR(oCheckbox);
            if (null != oParentTR)
            {
                var oParentTRClass = oParentTR.className;
                oParentTRClass = defaultParam(oParentTRClass, "");
//                alert("match="+ oParentTRClass.match("\s*selected"))
                oParentTRClass = (isChecked(oCheckbox)
                    ? (null == oParentTRClass.match(/\s*selected/) ? oParentTRClass + " selected" : oParentTRClass)
                    : oParentTRClass.replace(/\s*selected/, ""));
//                alert('cls '+oParentTRClass)
                oParentTR.className = oParentTRClass;
            }
        }
    }
}

// Backtrack a maximum of 5 parents from the checkbox to look for the parent TR
//
// oCheckbox - the checkbox
function getCheckboxParentTR(oCheckbox)
{
    var oAncestor = oCheckbox.parentNode;
    for (var i = 0; i < 5; i++)
    {
        if (null == oAncestor) return null;
        if ("tr" == oAncestor.tagName.toLowerCase()) return oAncestor;
        oAncestor = oAncestor.parentNode;
    }
    return null;
}

// Returns the default if a parameter isn't used.
//
// oParam    - parameter to intitialize
// odDefault - default to set parameter to
function defaultParam(oParam, oDefault)
{
    if (null == oParam || "undefined" == oParam)
    {
        return oDefault;
    }
    return oParam;
}

function isAnyChecked(oForm, sGroupName)
{
    var oaInputs = oForm.getElementsByTagName("input");
    for (var i = 0; i < oaInputs.length; i++)
    {
        if ( isChecked(oaInputs[i]) && sGroupName == oaInputs[i].getAttribute("group"))
        {
            return true;
        }
    }

    return false;
}

FormUtils.JustInTimeForm = function (uri, params, method, formName, jitAppend)
{
    this.init(uri, params, method, formName, jitAppend);
}

FormUtils.JustInTimeForm.prototype =
{
    uri: null,
    params: null,
    method: "POST",
    jitform: null,
    formName: null,
    jitAppend: true,
    appended: false
}

FormUtils.JustInTimeForm.prototype.init = function (uri, params, method, formName, jitAppend)
{
    this.uri = uri;
    this.params = params;
    this.method = method;
    this.formName = formName;
    this.jitAppend = jitAppend;

    this.buildForm();
}

FormUtils.JustInTimeForm.prototype.buildForm = function()
{
    if (this.params != null)
    {
        this.jitform = document.createElement("form");
        this.jitform.setAttribute("action", this.uri);
        this.jitform.setAttribute("method", this.method);

        var nvpairs = this.params.split("&");
        for (var i = 0; i < nvpairs.length; i++)
        {
            var nv = nvpairs[i].split("=");
            var name = nv[0];
            var value = nv[1];

            if (name != null)
            {
                var formElement = document.createElement("input");
                formElement.setAttribute("type", "hidden");
                formElement.setAttribute("name", name);
                formElement.setAttribute("value", value);
                this.jitform.appendChild(formElement);
            }
        }
        if (!this.jitAppend)
        {
            this.appendForm();
        }
    }
}

FormUtils.JustInTimeForm.prototype.appendForm = function()
{
    if (!this.appended)
    {
        document.body.appendChild(this.jitform);
        this.appended = true;
    }
}

FormUtils.JustInTimeForm.prototype.submit = function()
{                                    
    if (this.jitform != null)
    {
        if (this.jitAppend)
        {
            this.appendForm();
        }
        this.jitform.submit();
        
    }
}

FormUtils.JustInTimeForm.prototype.setOnclickForElementById = function(id)
{
    var jitf = this;
    var elem = document.getElementById(id);
    if (elem)
    {
        elem.onclick = function (e)
        {           
            if (!e) e = window.event;
            e.cancelBubble = true;
            if (e.stopPropagation) e.stopPropagation();

            jitf.submit();
            return false;
        }
    }
}

//FormUtils.JustInTimeForm.prototype.onclick =
