// https://stackoverflow.com/a/6871820
// http://learn.jquery.com/plugins/basic-plugin-creation/
(function($) {

    var validator_can_submit = true;
    var validator_warn = false;

    var validator_methods = {
        init: function(options) {

            $(this).on('submit', function(e) {

                validator_can_submit = true;
                validator_warn = false;

                $(this).find('input, select, textarea').each(function() {
                    validator_clear($(this));
                    validator_required($(this));
                    validator_len($(this));
                    validator_minlen($(this));
                    validator_maxlen($(this));
                    validator_func($(this));
                });

                if (!validator_can_submit) {
                    return false;
                }

                if (validator_warn) {
                    if (!confirm("Inválido")) {
                        return false
                    }
                }

            });
        }
    };

    $.fn.validator = function(methodOrOptions) {
        if (validator_methods[methodOrOptions]) {
            return validator_methods[methodOrOptions].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof methodOrOptions === 'object' || !methodOrOptions) {
            // Default to "init"
            return validator_methods.init.apply(this, arguments);
        } else {
            console.log("--- Plugin error");
        }
    };

    function validator_clear(element) {
        $(element).parent().removeClass('has-success');
        $(element).parent().removeClass('has-error');
        $(element).parent().removeClass('has-warning');
    }

    function validator_required(element) {
        if (typeof $(element).data('required') === 'undefined') {
            return;
        }

        if ($(element).data('required') === true) {

            if ($(element).val() === "") {
                $(element).parent().addClass('has-error');
                validator_can_submit = false;
            }
            return;
        }

        if ($(element).val() === '') {
            $(element).parent().addClass('has-warning');
            validator_warn = true;
        }

    }

    function validator_minlen(element) {
        if (typeof $(element).data('minlen') === 'undefined') {
            return;
        }
        if ($(element).val().length === 0) {
            return;
        }
        if ($(element).val().length < $(element).data('minlen')) {
            validator_can_submit = false;
            $(element).parent().addClass('has-error');
        }
    }

    function validator_len(element) {
        if (typeof $(element).data('len') === 'undefined') {
            return;
        }
        if ($(element).val().length === 0) {
            return;
        }
        if ($(element).val().length !== $(element).data('len')) {
            validator_can_submit = false;
            $(element).parent().addClass('has-error');
        }
    }

    function validator_maxlen(element) {
        if (typeof $(element).data('maxlen') === 'undefined') {
            return;
        }
        if ($(element).val().length === 0) {
            return;
        }
        if ($(element).val().length > $(element).data('maxlen')) {
            validator_can_submit = false;
            $(element).parent().addClass('has-error');
        }
    }

    function validator_func(element) {
        if (typeof $(element).data('func') === 'undefined') {
            return;
        }

        if ($(element).val().length === 0) {
            return;
        }

        if (eval("typeof validate_" + $(element).data('func')) === "function") {
            /*
            if (! eval("validate_" + $(element).data('func') + "( "+$(element).val()+" )")) {
                validator_can_submit = false;
                $(element).parent().addClass('has-error');
            }*/
        }

    }

    //---------------------

    function validate_cpf(value = "") {
        value = value.replace(/\./g, '').replace(/-/g, '');
        var numeros, digitos, soma, i, resultado, digitos_iguais;
        digitos_iguais = 1;
        if (value.length < 11) {
            return false;
        }
        for (i = 0; i < value.length - 1; i++) {
            if (value.charAt(i) != value.charAt(i + 1)) {
                digitos_iguais = 0;
                break;
            }
        }
        if (!digitos_iguais) {
            numeros = value.substring(0, 9);
            digitos = value.substring(9);
            soma = 0;
            for (i = 10; i > 1; i--) {
                soma += numeros.charAt(10 - i) * i;
            }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0)) {
                return false;
            }
            numeros = value.substring(0, 10);
            soma = 0;
            for (i = 11; i > 1; i--) {
                soma += numeros.charAt(11 - i) * i;
            }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1)) {
                return false;
            }
            return true;
        } else {
            return false;
        }
    }


})(jQuery);