(function($){

    /* Link External */
    $.linkExternal=function(){
        $('a[href^="http"]').attr('target', '_blank');
        return(true);
    };

    /* Clear Fields */
    $.fn.clearFields=function(){
        return this.each(function(){
            if($(this).attr('type')!='text')
                return(false);
            $(this).focus(function(){
                if($(this).val()==$(this).attr('title'))
                    $(this).select();
            });
            $(this).blur(function(){
                if($(this).val()=='')
                    $(this).val($(this).attr('title'));
            });
        });
    };

    /* Only Numbers */
    $.fn.onlyNumbers=function(){
        return this.each(function(){
            $(this).keypress(function(event){
                if(event.which!=0 && event.which!=8 && (event.which<48 || event.which>57))
                    event.preventDefault();
            });
        });
    };

    /* Max Length */
    $.fn.maxLength=function(chars){
        return this.each(function(){
            $(this).keypress(function(event){
                if(event.which!=0 && event.which!=8 && $(this).val().length>=chars)
                    event.preventDefault();
            });
        });
    };

    /* Validate Email */
    $.fn.validateEmail=function(){
        $(this).val($.trim($(this).val().toLowerCase()));
        var er=/^[a-z0-9_\.-]{2,}@([a-z0-9_-]{2,}\.)+[a-z]{2,4}$/;
        if(!er.exec($(this).val()))
            return(false);
        return(true);
    };

    /* Validate Link */
    $.fn.validateLink=function(){
        $(this).val($.trim($(this).val().toLowerCase()));
        var er=/^http(s)?:\/\/[a-z0-9_\.-]{2,}\.[a-z]{2,4}$/;
        if(!er.exec($(this).val()))
            return(false);
        return(true);
    };



	

    /* Validate Cep */
    $.fn.validateCep=function(){
        var value=parseInt($.trim('0'+ $(this).val().replace(/-/g, '')), 10);
        if(value!='0')
            $(this).val(value);
        var er=/^(\d{7,8})$/;
        if(!er.exec($(this).val()))
            return(false);
        return(true);
    };

    /* Validate Phone */
    $.fn.validatePhone=function(){
        var value=parseInt($.trim('0'+ $(this).val().replace(/ |\.|-|\(|\)|\//g, '')), 10);
        if(value!='0')
            $(this).val(value);
        var er=/^(\d{10})$/;
        if(!er.exec($(this).val()))
            return(false);
        return(true);
    };

    /* Validate Phone DDD */
    $.fn.validatePhoneDdd=function(){
        var value=parseInt($.trim('0'+ $(this).val()), 10);
        if(value!='0')
            $(this).val(value);
        var er=/^(\d{2})$/;
        if(!er.exec($(this).val()))
            return(false);
        return(true);
    };

    /* Validate Phone Number */
    $.fn.validatePhoneNumber=function(){
        var value=parseInt($.trim('0'+ $(this).val().replace(/\.|-|\//g, '')), 10);
        if(value!='0')
            $(this).val(value);
        var er=/^(\d{8})$/;
        if(!er.exec($(this).val()))
            return(false);
        return(true);
    };

    /* Validate Image */
    $.fn.validateImage=function(){
        var er=/\.jp(e)?g$/;
        if(!er.exec($(this).val().toLowerCase()))
            return(false);
        return(true);
    };

    /* Validate Date
	$.fn.validateDate=function(){
		var value=$.trim($(this).val());
		$(this).val(value);
		var er=/^([1-9]|[1-2][0-9]|[3][0-1])\/([1-9]|[1][0-2])\/(19[0-9][0-9])|(20[0-1][0-9])$/;
		if(!er.exec($(this).val()))
			return(false);
		return(true);
	};
	*/

    /* Validate Date Year */
    $.fn.validateDateYear=function(){
        var value=parseInt($.trim('0'+ $(this).val()), 10);
        if(value!='0')
            $(this).val(value);
        var er=/(19[0-9][0-9])|(20[0-1][0-9])$/;
        if(!er.exec($(this).val()))
            return(false);
        return(true);
    };

    /* Validate Date Month */
    $.fn.validateDateMonth=function(){
        var value=parseInt($.trim('0'+ $(this).val()), 10);
        if(value!='0')
            $(this).val(value);
        var er=/^([1-9]|[1][0-2])$/;
        if(!er.exec($(this).val()))
            return(false);
        return(true);
    };

    /* Validate Date Day */
    $.fn.validateDateDay=function(){
        var value=parseInt($.trim('0'+ $(this).val()), 10);
        if(value!='0')
            $(this).val(value);
        var er=/^([1-9]|[1-2][0-9]|[3][0-1])$/;
        if(!er.exec($(this).val()))
            return(false);
        return(true);
    };

})(jQuery);

/* Validate Cnpj */
function validateCnpj( cnpj ){
    var cnpj = $.trim(cnpj.replace(/\.|-|\//g, ''));   
    var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
    digitos_iguais=1;
    if(cnpj.length<14 && cnpj.length<15)
        return(false);
    for(i=0; i<cnpj.length-1; i++)
        if(cnpj.charAt(i)!=cnpj.charAt(i+1)){
            digitos_iguais=0;
            break;
        }
    if(!digitos_iguais){
        tamanho=cnpj.length - 2
        numeros=cnpj.substring(0, tamanho);
        digitos=cnpj.substring(tamanho);
        soma=0;
        pos=tamanho - 7;
        for(i=tamanho; i>=1; i--){
            soma+=numeros.charAt(tamanho - i) * pos--;
            if(pos<2)
                pos=9;
        }
        resultado=soma % 11 < 2 ? 0 : 11 - soma % 11;
        if(resultado!=digitos.charAt(0))
            return(false);
        tamanho=tamanho + 1;
        numeros=cnpj.substring(0,tamanho);
        soma=0;
        pos=tamanho - 7;
        for(i=tamanho; i>=1; i--){
            soma += numeros.charAt(tamanho - i) * pos--;
            if (pos < 2)
                pos=9;
        }
        resultado=soma % 11 < 2 ? 0 : 11 - soma % 11;
        if(resultado!=digitos.charAt(1))
            return(false);
        
        return(true);
    }else        
        return(false);
};

/* Validate Cpf */
function validateCpf ( cpf ){
    var cpf=$.trim( cpf.replace(/\.|-|\//g, ''));
    if(cpf!='0')
        $(this).val(cpf);
    if(cpf.length!=11 || cpf=='00000000000' || cpf=='11111111111' || cpf=='22222222222' || cpf=='33333333333' || cpf=='44444444444' || cpf=='55555555555' || cpf=='66666666666' || cpf=='77777777777' || cpf=='88888888888' || cpf=='99999999999')
        return(false);
    var sum=0;
    for(i=0; i<9; i++)
        sum+=parseInt(cpf.charAt(i))*(10-i);
    var rest=11-(sum%11);
    if(rest==10 || rest==11)
        rest=0;
    if(rest!=parseInt(cpf.charAt(9)))
        return(false);
    sum=0;
    for(i=0; i<10; i++)
        sum+=parseInt(cpf.charAt(i))*(11-i);
    rest=11-(sum%11);
    if(rest==10 || rest==11)
        rest=0;
    if(rest!=parseInt(cpf.charAt(10)))
        return(false);
    return(true);
};

function gera_token(){
	if( $('#cpf').val().length > 0 ){
		if( !validateCpf($('#cpf').val()) ){
			$('.erro_cpf_cnpj').html('O cpf digitado é inválido.');
			return false;
		}
	}
	else if( $('#cnpj').val().length > 0 ){
		if( !validateCnpj($('#cnpj').val()) ){
			$('.erro_cpf_cnpj').html('O cnpj digitado é inválido.');
			return false;
		}
	}
	$('#frm-lojista-token').submit();		
}
