/* © Zooven, Inc 2006-2007 - ALL RIGHTS RESERVED */

/**
 * FormValidator - do all of the validation for a form, and display any field-level validation messages.
 */
zooven.validation.FormValidator = Class.create();
zooven.validation.FormValidator.prototype = {
/**
 * construct a new FormValidator.
 * fieldValidators      : an array of field validators to check
 * config               : an (optional) configuration object for the FormValidator
 */
    initialize : function(
            fieldValidators,
            config
            ) {
        // set the member variable for the array of field validators
        this.fieldValidators = fieldValidators;

        // if there was a config object with a showMessageCallback, set that - otherwise set the default.
        this.showMessageCallback = config && config.showMessageCallback ?
                                   config.showMessageCallback :
                                   function(element, message) {
                                       element.innerHTML = message;
                                       new Effect.Appear(element);
                                   };

        // if there was a config object with a hideMessageCallback, set that - otherwise set the default.
        this.hideMessageCallback = config && config.hideMessageCallback ?
                                   config.hideMessageCallback :
                                   function(element) {
                                       new Effect.Fade(element);
                                   };
    },

/**
 * validate - validates the form.
 *
 * calls all of the field validators, and handles any validation messages.
 *
 * returns true iff the entire form is valid
 */
    validate : function() {
        // assume it's valid until proven otherwise
        var valid = true;

        var _this = this;
        this.fieldValidators.each(function(fieldValidator) {
            // validate the field
            var fieldMessage = fieldValidator.validate();
            // the validation message field is ${field.id}__validation
            var validationMessageId = fieldValidator.field.id + '__validation';
            // if there was an error on this field...
            if (fieldMessage) {
                // if there's a message container...
                if ($(validationMessageId)) {
                    // call the "show message" callback
                    _this.showMessageCallback($(validationMessageId), fieldMessage)
                } else {
                    // otherwise, just alert the message
                    alert(fieldMessage);
                }
                // set valid to false
                valid = false;
            } else {
                // execute the callback for hiding the message container
                if ($(validationMessageId)) {
                    _this.hideMessageCallback($(validationMessageId))
                }
            }
        });

        // return the validity of the form
        return valid;
    }
}