﻿var FormHelper = function(formID, rules, submitHandler, validationFailedHandler, ajaxFormOptions) {
    if (!formID) {
        throw "A valid form ID is required to create a FormHelper. The form ID '" + formID + "' is not valid.";
    }

    var _formElement = document.getElementById(formID);
    if (!_formElement) {
        throw "A valid form element is required to create a FormHelper. A Form with ID '" + formID + "' is not present.";
    }

    if (!submitHandler) {
        throw "A valid submit handler is required to create a FormHelper";
    }

    if (!ajaxFormOptions) {
        ajaxFormOptions = new Object();
        ajaxFormOptions.resetForm = true;
        ajaxFormOptions.clearForm = true;
    }

    //Declare private variables here
    var _formValidator;
    var _rules;

    if (!rules) {
        _rules = {};
    }
    else {
        _rules = rules;
    }

    //Declare private methods here

    //If this function returns false then the form will not be submitted else it will be submitted
    //If there is a validation error, this function will show the error labels and hide the info DIV for all input elements
    var _validateForm = function() {
        var isValid = _formValidator.checkForm();

        $(_formElement).find(".invalid").html("");

        if (!isValid) {
            for (var i = 0; i < _formValidator.errorList.length; i++) {
                $(_formValidator.errorList[i].element).parent().find(".info").css("display", "none");
                $(_formValidator.errorList[i].element).parent().find(".invalid").css("display", "block");
                $(_formValidator.errorList[i].element).parent().removeClass("formActive");
                $(_formValidator.errorList[i].element).parent().addClass("formInvalid");

                $(_formValidator.errorList[i].element).parent().find(".invalid").each(function(index) {
                    var temp = $(this).html();
                    temp = temp + _formValidator.errorList[i].message + "<BR />";
                    $(this).html(temp);
                });
            }

            if (validationFailedHandler)
            {
                validationFailedHandler(_formElement);
            }
        }
        else {
            if (submitHandler)
            {
                submitHandler(_formElement);
            }
        }

        return false;
    };

    //Add to the submit form event
    //Setup the display of information messages in input elements' focus etc
    var _initialize = function() {
        _formValidator = $(_formElement).validate({
            onsubmit: false,
            onfocusout: false,
            onkeyup: false,
            onclick: false,
            debug: true,
            rules: _rules
        });

        //Attach events to show and hide info DIV for all INPUT, SELECT and TEXTAREA elements
        $(_formElement).find("input,select,textarea").focus(function() {
            $(this).parent().find(".invalid").css("display", "none");
            $(this).parent().find(".info").css("display", "block");
            $(this).parent().removeClass("formInvalid");
            $(this).parent().addClass("formActive");
        }).blur(function() {
            $(this).parent().find(".invalid").css("display", "none");
            $(this).parent().find(".info").css("display", "none");
            $(this).parent().removeClass("formInvalid");
            $(this).parent().removeClass("formActive");
        });

        //Add listener to the form submit before it happens
        ajaxFormOptions.beforeSubmit = _validateForm;
        ajaxFormOptions.success = submitHandler;
        $(_formElement).ajaxForm(ajaxFormOptions);
    };

    //initialize the form
    _initialize();

    return {
        //Declare public variables here
        formValidator: _formValidator,

        //Declare public methods here
        validateForm: function() {
            return _validateForm();
        },

        addRules: function(rules) {
            return $(_formElement).rules("add", rules);
        },

        removeRules: function(rules) {
            return $(_formElement).rules("remove", rules);
        },

        getRules: function() {
            return $(_formElement).rules();
        }
    };
};
