/*
o_form_validation.js
*/

function VerifyForm() {
        this.msgSeparator = '\n';
        this.blankValue   = '';
        this.languageCode = 'en';
        this._gripes      = new Array();
        this._Email       = /^([0-9a-z_&.+-]+!)*[0-9a-z_&.+-]+@(([0-9a-z]([0-9a-z-]*[0-9a-z])?\.)+[a-z]{2,3}|([0-9]{1,3}\.){3}[0-9]{1,3})$/i;
        this._EmailBad    = /^(((postmaster|root|hostmaster|mailer-daemon)@.*\.com)|.*@(localhost\.com|127\.0\.0\.1))$/i;
        this._Numbers     = /^\d+$/;
        this._Url         = /\w+:\/\/\w+/;

        this.language     = new Array();     // Language independent error messages.
        this.language.en  = new Array();
        this.language.fr  = new Array();

        // Error messages in English:
        this.language.en.header          = "Your form input has some problems:"
        this.language.en.footer          = "Please fill out the form properly and try again!"
        this.language.en.required        = "%FIELDNAME% is required";
        this.language.en.invalid         = "Invalid %FIELDNAME%";

        // Error messages in French:
        this.language.fr.header          = "Votre entrée de forme a quelques problèmes :"
        this.language.fr.footer          = "Veuillez compléter la forme correctement et essayez encore!"
        this.language.fr.required        = "%FIELDNAME% est obligatoire";
        this.language.fr.invalid         = "%FIELDNAME% incorrectes";

        this.addError       = _VerifyForm_addError;
        this.showErrors     = _VerifyForm_showErrors;
        this.hasValue       = _VerifyForm_hasValue;
        this.getValue       = _VerifyForm_getValue;
        this.validEmail     = _VerifyForm_validEmail;
        this.validUrl       = _VerifyForm_validUrl;
        this.isNumeric      = _VerifyForm_isNumeric;

        return this;
}

function _VerifyForm_addError(objField, strName) {
        if (!this._gripes.length) {
                if (objField.all || objField.focus) objField.focus();
                if (objField.value && (objField.all || objField.select)) objField.select();
        }

        if (!this.hasValue(objField))
                this._gripes[this._gripes.length] = this.language[this.languageCode].required.replace("%FIELDNAME%", strName);
        else
                this._gripes[this._gripes.length] = this.language[this.languageCode].invalid.replace("%FIELDNAME%", strName);
}

function _VerifyForm_showErrors() {
        if (this._gripes.length) {
                alert(this.language[this.languageCode].header +
                      this.msgSeparator + this.msgSeparator + this._gripes.join(this.msgSeparator) + this.msgSeparator + this.msgSeparator +
                      this.language[this.languageCode].footer);
                return false;
        }
        return true;
}

function _VerifyForm_hasValue(field) {
        if (!field.type && field.length) {
                for (var i = 0; i < field.length; i++)
                        if (field[i].type && this.hasValue(field[i]))
                                return true;
                        return false;
                }

        if (/select/.test(field.type))
                return (field.selectedIndex != -1 && (field.options[field.selectedIndex].value != this.blankValue));
        if (/(checkbox|radio)/.test(field.type))
                return (field.checked && (field.value != this.blankValue));
        if (/(button|reset|submit)/.test(field.type))
                return false;
        return (field.value != this.blankValue)
}

function _VerifyForm_getValue(field) {
        if (!field.type && field.length) {
                for (var i = 0; i < field.length; i++) {
                        if (field[i].type) {
                                var value = this.getValue(field[i]);
                                if (value) return value;
                        }
                }
                return null;
        }

        if (/select/.test(field.type))
                return ((field.selectedIndex != -1 && (field.options[field.selectedIndex].value != this.blankValue))
                       ? field.options[field.selectedIndex].value : null);
        if (/(checkbox|radio)/.test(field.type))
                return ( (field.checked && (field.value != this.blankValue)) ? field.value : null );
        return ((!/(button|reset|submit)/.test(field.type) && (field.value != this.blankValue)) ? field.value : null)
}

function _VerifyForm_validEmail(strEmail) {
        return (this._Email.test(strEmail) && !this._EmailBad.test(strEmail))
}

function _VerifyForm_validUrl(strUrl) {
        return (this._Url.test(strUrl))
}

function _VerifyForm_isNumeric(strNumeric) {
        return (this._Numbers.test(strNumeric))
}

