$(document).ready(function(){
    jQuery.validator.addMethod('noSpace', function(value, element) {
        return value.indexOf(' ') < 0 && value != '';
    }, 'No space please and don\'t leave it empty.');

    jQuery.validator.addMethod('integer', function(value, element) {
        return this.optional(element) || /^-?\d+$/.test(value);
    }, 'A positive or negative non-decimal number please');

    // VALIDA VALOR FINAL ><= VALOR
    jQuery.validator.addMethod('valueInterval', function(value, element, parameters){
        if (value != undefined && value != '' && $(parameters[0]).val() != undefined && $(parameters[0]).val() != '') {
            var firstValue = value;
            firstValue = replaceAll(firstValue, 'R$', '');
            firstValue = replaceAll(firstValue, ' ', '');
            firstValue = replaceAll(firstValue, '.', '');
            firstValue = replaceAll(firstValue, ',', '.');
            firstValue = parseFloat(firstValue);
            var secondValue = $(parameters[0]).val();
            secondValue = replaceAll(secondValue, 'R$', '');
            secondValue = replaceAll(secondValue, ' ', '');
            secondValue = replaceAll(secondValue, '.', '');
            secondValue = replaceAll(secondValue, ',', '.');
            secondValue = parseFloat(secondValue);

            var isValid = false;
            switch(parameters[1]){
                case 'gt':
                    isValid = firstValue > secondValue;
                    break;
                case 'lt':
                    isValid = firstValue < secondValue;
                    break;
                case 'get':
                    isValid = firstValue >= secondValue;
                    break;
                case 'let':
                    isValid = firstValue <= secondValue;
                    break;
                case 'eq':
                    isValid = firstValue == secondValue;
                    break;
            }
            return isValid;
        } else
            return true;
    }, 'Campo exige um Valor Superior ou Anterior.');
    
    // VALIDA DATA BRASIL dd/mm/YYYY
    jQuery.validator.addMethod('dateBR', function(value, element) {
        if (value != '') {
            if(value.length != 10)
                return false;
            var data = value;
            var dia = data.substr(0,2);
            var barra1 = data.substr(2,1);
            var mes = data.substr(3,2);
            var barra2 = data.substr(5,1);
            var ano = data.substr(6,4);
            if (data.length != 10 || barra1 != '/' || barra2 !='/' || isNaN(dia) || isNaN(mes) || isNaN(ano) || dia > 31 || mes > 12 )
                return false;
            if ((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia == 31)
                return false;
            if (mes == 2 && (dia > 29 || (dia == 29 && ano%4 != 0)))
                return false;
            if (ano < 1900)
                return false;
        }
        return true;
    }, 'Campo exige uma Data válida.');  // Mensagem padrão
    
});
