/*
 * $Id: form.js 61 2009-12-08 22:09:16Z wru $
 */

/* requires string.js, date.js */

function CFormValidator (theid, themandatory, thefunction, thearguments, themessage)
{
    this.id        = theid;
    this.mandatory = themandatory;
    this.testfunc  = thefunction;
    this.arguments = thearguments;
    this.message   = themessage;
}

CFormValidator.IsValidEMailAddress = function (theelement)
{
    return (/^[^\]()[<>\.,;:'" @]+(\.[^\]()[<>\.,;:'" @]+)*@[^\]()[<>\.,;:'" @]+(\.[^\]()[<>\.,;:'" @]{2,})+$/.test (theelement.value));
}

CFormValidator.IsValidInteger = function (theelement, theminmax)
{
    var valid = theelement.value.isInteger ();
    if (valid && theminmax !== null) {
        var value = parseInt (theelement.value, 10);
        valid = (typeof theminmax.minval == "undefined" || value >= theminmax.minval) && (typeof theminmax.maxval == "undefined" || value <= theminmax.maxval);
    }
    return (valid);
}

CFormValidator.IsValidPassword = function (theelement)
{
    return (theelement.value.eraseTrailingSpaces ().length >= 6);
}

CFormValidator.prototype.Validate = function ()
{
    var elem    = document.getElementById (this.id);
    var isempty = elem.value.isEmpty ();
    if (this.mandatory && isempty || ! isempty && this.testfunc !== null && ! this.testfunc (elem, this.arguments)) {
        window.alert (this.message);
        elem.focus ();
        if (typeof elem.select != "undefined")
            elem.select ();
        return (false);
    }
    return (true);
}

function CForm (theform)
{
    if (! (this instanceof CForm))
        return (new CForm (theform));
    this.form = theform;
}

CForm.prototype.CollectValues = function (thebuttonid)
{
    var values = new Object ();
    values.values = new Object ();
    for (var i = 0; i < this.form.elements.length; ++i) {
        var elem = this.form.elements [i];
        if (typeof elem.name != "undefined" && elem.name != "") {
            var tmpvalues = new Array ();
            switch (elem.type) {
                case "button":
                case "reset":
                case "submit":
                    if (elem.id == thebuttonid)
                        tmpvalues.push (elem.value);
                    break;
                case "checkbox":
                case "radio":
                    if (elem.checked)
                        tmpvalues.push (elem.value);
                    break;
                case "file":
                case "hidden":
                case "password":
                case "text":
                case "textarea":
                    tmpvalues.push (elem.value);
                    break;
                case "select-one":
                case "select-multiple":
                    for (var j = 0; j < elem.length; ++j)
                        if (elem.options [j].selected)
                            tmpvalues.push (elem.options [j].value);
                    break;
            }
            if (tmpvalues.length > 0) {
                if (typeof (values.values [elem.name]) == "undefined")
                    values.values [elem.name] = new Array ();
                for (var j = 0; j < tmpvalues.length; ++j)
                    values.values [elem.name].push (tmpvalues [j]);
            }
        }
    }
    values.Serialize = function (theaddvalues, theencode, thesep)
    {
        var str = "";
        var sep = "";
        var enc = typeof theencode != "undefined";
        if (typeof thesep == "undefined")
            thesep = "&";
        for (var step = 0; step < (typeof theaddvalues == "undefined" ? 1 : 2); ++step) {
            var vals = (step == 0 ? this.values : theaddvalues);
            for (var i in vals) {
                var name = (enc ? theencode (i) : i);
                for (var j = 0; j < vals [i].length; ++j) {
                    var value = (enc ? theencode (vals [i] [j]) : vals [i] [j]);
                    str += sep + name + "=" + value;
                    sep = thesep;
                }
            }
        }
        return (str);
    }
    return (values);
}

CForm.EvEnChangeYear = function (theedit)
{
    var year = theedit.value;
    if (! year.isEmpty ()) {
        year = Date.adjustYear (year, -1).toString ();
        while (year.length < 4)
            year = "0" + year;
        theedit.value = year;
    }
}

CForm.FocusRadio = function (theradio, thelabel)
{
    theradio.checked = true;
    document.getElementById (thelabel).htmlFor = theradio.id;
}

CForm.prototype.Init = function ()
{
    var elems  = this.form.elements;
    var nelems = elems.length;
    for (var i = 0; i < nelems; ++i) {
        var elem = elems [i];
        if (typeof elem.type == "undefined" || elem.type == "hidden" || elem.type == "radio" && elem.checked === false ||
            typeof elem.disabled != "undefined" && elem.disabled === true || typeof elem.readOnly != "undefined" && elem.readOnly === true)
        {
            continue;
        }
        elem.focus ();
        if (typeof elem.select != "undefined")
            elem.select ();
        break;
    }
}

CForm.Validate = function (theelements)
{
    var valid = true;
    for (var i = 0; valid && i < theelements.length; valid = theelements [i++].Validate ());
    return (valid);
}
