﻿/* 
    ### Valida Form ###
    Data: 2.5.2011
    Por: Adelson
    by: NetEstudio
*/

(function ($) {
    $.fn.ValidaForm = function (options) {

        // Define os parametros defaults do plugin
        var settings = {
            seletor: this.selector,
            validar: {},
            retorno: function () { return false },
            submit: "submit"
        };

        if (options) {
            $.extend(settings, options);
        };

        //Padrões de expressão
        function Valida() {
            var Retorno = true;
            var Atrib = arguments[0];

            for (var i in settings.validar) {
                if (Atrib["class"].indexOf(i) > -1) {
                    Retorno = settings.validar[i].retorno(Atrib["value"]);
                    settings.retorno(Retorno, Atrib);
                }
            }
            return Retorno;
        }
        return $(this).each(function () {
            if ($(this).attr('class').indexOf(settings.submit) > -1) {
                $(this).click(function () {
                    var Retorno = true;
                    $(settings.seletor).each(function () {
                        
                        var Cmp = {
                            'name': $(this).attr('name'),
                            'id': $(this).attr('id'),
                            'value': $(this).val(),
                            'class': $(this).attr('class')
                        };

                        if (!Valida(Cmp))
                            Retorno = false;
                    });
                    return Retorno;
                });
            }
            else {
                $(this).blur(function () {
                    var Cmp = {
                        'name': $(this).attr('name'),
                        'id': $(this).attr('id'),
                        'value': $(this).val(),
                        'class': $(this).attr('class')
                    };
                    Valida(Cmp);
                });
            }
        });
    };
})(jQuery);
