/*
 * Copyright (C) 2003  Christian Cryder [christianc@granitepeaks.com]
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: FormControl.js,v 1.2 2004/02/01 05:16:28 christianc Exp $
 */

//--------------------- Public functions ----------------------------

/**
 * takes a form object as the first argument and then takes an infinite amount
 * of additional arguments which are expected to be functions.  This allows for
 * the possibility of executing multiple functions (in the order provided)
 * before actual submittal of the form.  Actually, any one of the named
 * functions may submit the form themselves.  However, ___doSubmit() always
 * provides a backstop objForm.submit() in case no extra function is provided
 * or the functions that are provided don't submit the form themselves.
 *
 * @see doSubmitAndLock(objForm)
 */
function doSubmitAndNoLock(objForm) {
    return ___doSubmit(arguments);
}

/**
 * same as doSubmitAndNoLock, only this locks all form controls of type
 * 'submit', 'button', and 'image'.  Other types of controls can't be
 * locked (disabled) because they are data-rich and modern browsers won't send
 * data of disabled controls to the server.
 *
 * @see doSubmitAndNoLock(objForm)
 */
function doSubmitAndLock(objForm) {
    for (i = 0; i < objForm.elements.length; i++) {
        if (objForm.elements[i].type == 'submit' || objForm.elements[i].type == 'button' || objForm.elements[i].type == 'image') {
            objForm.elements[i].disabled = true;
        }
    }
    return ___doSubmit(arguments);
}


//--------------------- Utility functions ---------------------------


/**
 * Takes an arguments object.  Treats the first argument as the form object
 * and the rest as function to be called, with the form object as the only
 * argument.
 */
function ___doSubmit(args) {
    if (___propperArgs(arguments) && args[0].submit) {
        //args is an arguments object and the first argument of that
        //arguments object is a form object.
        
        //loop through other arguments which are expected to be functions, but
        //we'll double check anyway and ignore each argument that isn't a
        //function
        var ok = true;
        for (var i=1; i < args.length; i++) {
            if (args[i].call) {
                ok = args[i].call(null, args[0]);
                if (!ok) break;
            }
        }

        //to disable form submittal, commment out the submit()
        if (ok) args[0].submit();
    } else {
        alert('Impropper arguments!  Must be a single arguments object with the first argument of said object being a form object');
    }
    return false;
}

/**
 * checks, first, that the args argument is an arguments object and then
 * checks that the actual length of the arguments object matches the
 * expected length.
 */
function ___propperArgs(args) {
    if (!args.callee) return false;
    var actual = args.length;
    var expected = args.callee.length;
    if (actual == expected) return true;
    else return false;
}

