
var maximo = 1;

function buscaCidades(estadoId){
    $("#cidadePf").html("<option value=\"\">Aguarde...</option>");
    if(estadoId > 0){
        jQuery.post("/cadastro/ajax.buscacidade", {
            'estado':estadoId
        }, function(data) {
            $("#cidadePf").html(data);
        }
        );
    }
}

function buscaCidadesJuridico(estadoId){
    $("#cidadePj").html("<option value=\"\">Aguarde...</option>");
    if(estadoId > 0){
        jQuery.post("/cadastro/ajax.buscacidade", {
            'estado':estadoId
        }, function(data) {
            $("#cidadePj").html(data);
        }
        );
    }
}

function loginUsuario(IDemail,IDsenha,Redirect){

    var valorEmail = $('#'+IDemail).attr('value');
    var valorSenha = $('#'+IDsenha).attr('value');

    if(($('#'+IDemail).attr('value') == "") || ($('#'+IDemail).attr('value') == "E-mail")){
        alert('E-mail é campo obrigatório');
        $('#'+IDemail).attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
        return false;
    }else{
        $('#'+IDemail).attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
        var email_pattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(!valorEmail.match(email_pattern)){
            alert('E-mail é inválido');
            $('#'+IDemail).attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
            return false;
        }
    }
    if(($('#'+IDsenha).attr('value') == "") || ($('#'+IDsenha).attr('value') == "Senha")){
        alert('Senha é campo obrigatório');
        $('#'+IDsenha).attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
        return false;
    }else{
        $('#'+IDsenha).attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
    }
    $('#load').fadeIn('300');
    $("#load").attr("style", "display: block");
    jQuery.post("/index.php/index/login", {
        'usuario':valorEmail,
        'senha':valorSenha
    }, function(data) {
        if(data == 'ok'){
            $('#load').fadeOut('300');
            if(Redirect){
                document.location.href = Redirect;
            }else{
                document.location.reload();
            }
        }else{
            $('#load').fadeOut('300');
            alert("Usuário ou Senha inválido(s).");
        }
    });
}

function displayCarrinho(div,btAbrir,btFechar){
    if(document.getElementById("carrinhoHeader").style.display == "none"){
        $('#'+div).fadeIn(1000);
        $('#'+btFechar).fadeIn(1000);
        $('#'+btAbrir).fadeOut(1200, $('#'+btAbrir).hide(0));
        $('#'+btFechar).attr('class','ativo');
    }else{
        $('#'+div).fadeOut(1000, $('#'+div).fadeOut(800));
        $('#'+btFechar).fadeOut(1500, $('#'+btFechar).hide(0));
        $('#'+btAbrir).fadeIn(1000);
        $('#'+btFechar).attr("class","");
    }
}

function excluiProdCar(idProd,tipoCar){
    $('#load').fadeIn('150');
    $("#load").attr("style", "display: block");
    jQuery.post("/livraria/carrinho", {
        'idProdDel':idProd
    }, function(data) {
        if(data != ''){
            $('#load').fadeOut('150');
            if(tipoCar == 1){
                atualizaQtdCar('formCarrinhoPrinc','1');
            }else{
                atualizaQtdCar('formCarrinho','2');
            }
        }else{
            $('#load').fadeOut('150');
            alert("Não foi possível excluir o produto, tente novamente!");
        }
    });
}

function atuQtdBt(){
    var status = 1;
    jQuery.post("/livraria/carrinho", {
        'atuQtd':status
    }, function(data) {
        if(data != ''){
            $("#btComprar").html("Carrinho("+data+")");
        }
    });
}

function atualizaQtdCar(formId,tipoCar){
    var i;
    var idProd = "[";
    var qtdProd = "[";
    for(i=0; i < document.getElementById(formId).elements.length; i++){
        if(document.getElementById(formId).elements[i].className == "quantidade"){
            if(qtdProd != "[")  qtdProd += ",";
            qtdProd += document.getElementById(formId).elements[i].value;
        }
        if(document.getElementById(formId).elements[i].className == "idprod"){
            if(idProd != "[")  idProd += ",";
            idProd += document.getElementById(formId).elements[i].value;
        }
    }
    idProd += "]";
    qtdProd += "]";
    $('#load').fadeIn('150');
    $("#load").attr("style", "display: block");
    jQuery.post("/livraria/carrinho", {
        'tipoCar':tipoCar,
        'atlProd':formId,
        'idProd':idProd,
        'qtdProd':qtdProd
    }, function(data) {
        if(data != ''){
            $('#load').fadeOut('150');
            if(tipoCar == 2){
                $("#gerado").html(data);
                atuQtdBt();
            }else{
                $("#carrinhoNew").html(data);
            }
        }else{
            $('#load').fadeOut('150');
            alert("Não foi possível atualizar o carrinho, tente novamente!");
        }
    });
}

function limpaCar(tipoCar){
    $('#load').fadeIn('150');
    $("#load").attr("style", "display: block");
    jQuery.post("/livraria/carrinho", {
        'acao':'limpar'
    }, function(data) {
        if(data != ''){
            $('#load').fadeOut('150');
            if(tipoCar == 1){
                atualizaQtdCar('formCarrinhoPrinc','1');
                atuQtdBt();
            }else{
                atualizaQtdCar('formCarrinho','2');
            }
        }else{
            $('#load').fadeOut('150');
            alert("Não foi possível limpar o carrinho, tente novamente!");
        }
    });
}

function calculaFrete(){

    var cepDestino = document.getElementById("cep").value;
    var totalCaracter = document.getElementById("cep").value.length;

    if(cepDestino != "" && totalCaracter == 8){
        $("#cep").attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
    }else{
        alert('Favor digitar um cep válido');
        $("#cep").attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
        return false;
    }
    $('#load').fadeIn('150');
    $("#load").attr("style", "display: block");
    jQuery.post("/carrinho/calculafrete", {
        'cepDestino':cepDestino
    }, function(data) {
        if(data != ''){
            $("#cepField").html(data);
            $('#load').fadeOut('150');
        }else{
            alert("Não foi possível calcular o frete, tente novamente!");
            $('#load').fadeOut('150');
        }
    });

}

function finalizaCompras(url){
    $('#load').fadeIn('150');
    $("#load").attr("style", "display: block");
    jQuery.post("/carrinho/verificafrete", {
        }, function(data) {
            if(data == 'ok'){
                $('#load').fadeOut('150');
                $("#cep").attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
                document.location.href=url;
            }else{
                $('#load').fadeOut('150');
                alert("Favor calcular o valor do frete!");
                $("#cep").attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
            }
        });
}

function verificalogin(linkbase,id){
    $('#load').fadeIn('150');
    $("#load").attr("style", "display: block");
    jQuery.post("/cursos/verificalogin", {
        }, function(data) {
            if(data == 'ok'){
                $('#load').fadeOut('150');
                $("#finalizaCurso").submit();
            }else{
                $('#load').fadeOut('150');
                document.location.href = linkbase+'cursos/login/curso/'+id;
            }
        });
}

function validaNewsletter(newsNome,newsEmail){
    if(newsNome != "" && newsNome != "nome"){
        $("#newsNome").attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
    }else{
        alert('Nome é campo obrigatório');
        $("#newsNome").attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
        return false;
    }
    if(newsEmail != "" && newsEmail != "E-mail"){
        $("#newsEmail").attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
        var email_pattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(newsEmail.match(email_pattern)) return true;
        if(newsEmail == ''){
            $("#newsEmail").attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
        }else{
            alert('E-mail é inválido');
            $("#newsEmail").attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
            return false;
        }
    }else{
        alert('E-mail é campo obrigatório');
        $("#newsEmail").attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
        return false;
    }
}

function insertNewsLetter(newsNome,newsEmail){
    if(validaNewsletter(newsNome,newsEmail)){
        jQuery.post("/index/cadastraboletim", {
            'newsNome':newsNome,
            'newsEmail':newsEmail
        }, function(data) {
            if(data != 'existe'){
                alert("Boletim inserido com sucesso");
                $("#newsNome").attr("value","nome");
                $("#newsEmail").attr("value","E-mail");
            }else{
                alert("Este boletim já estava cadastrado previamente");
                $("#newsNome").attr("value","nome");
                $("#newsEmail").attr("value","E-mail");
            }
        });
    }
}

function indiqueAmigo(){
    //VALIDAÇÃO DO NOME E E-MAIL DO REMETENTE
    if($("#seuNome").attr('value') != "" && $("#seuNome").attr('value') != "Seu nome"){
        $("#seuNome").attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
        var seuNome = $("#seuNome").attr('value');
    }else{
        alert('Seu Nome é campo obrigatório');
        $("#seuNome").attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
        $("#seuNome").onfocus;
        return false;
    }
    if($("#seuEmail").attr('value') != "" && $("#seuEmail").attr('value') != "Seu Email"){
        $("#seuEmail").attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
        var seuEmail = $("#seuEmail").attr('value');
        var email_pattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(seuEmail.match(email_pattern)){
            $("#seuEmail").attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
        }else{
            alert('E-mail é inválido');
            $("#seuEmail").attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
            $("#seuEmail").onfocus;
            return false;
        }
    }else{
        alert('Seu E-mail é campo obrigatório');
        $("#seuEmail").attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
        $("#seuEmail").onfocus;
        return false;
    }
    //VALIDAÇÃO DO NOME E E-MAIL DO DESTINATÁRIO
    if($("#nomeAmigo").attr('value') != "" && $("#nomeAmigo").attr('value') != "Nome do Amigo"){
        $("#nomeAmigo").attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
        var nomeAmigo = $("#nomeAmigo").attr('value');
    }else{
        alert('Nome do Amigo é campo obrigatório');
        $("#nomeAmigo").attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
        $("#nomeAmigo").onfocus;
        return false;
    }
    if($("#emailAmigo").attr('value') != "" && $("#emailAmigo").attr('value') != "Email do Amigo"){
        $("#emailAmigo").attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
        var emailAmigo = $("#emailAmigo").attr('value');
        var emailAmigo_pattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(emailAmigo.match(emailAmigo_pattern)){
            $("#emailAmigo").attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
        }else{
            alert('E-mail é inválido');
            $("#emailAmigo").attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
            $("#emailAmigo").onfocus;
            return false;
        }
    }else{
        alert('Email do Amigo é campo obrigatório');
        $("#emailAmigo").attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
        $("#emailAmigo").onfocus;
        return false;
    }

    //SE VALIDADE ENVIARÁ O E-MAIL DE INDICAÇÃO DE SITE
    jQuery.post("/index/indiqueamigo", {
        'seuNome':seuNome,
        'seuEmail':seuEmail,
        'nomeAmigo':nomeAmigo,
        'emailAmigo':emailAmigo
    }, function(data) {
        if(data == 'ok'){
            alert("Indicação enviada com sucesso");
        }else{
            alert("Problemas no envio, favor tentar novamente");
        }
    });
}

function postForum(catForum,idAssinante){
    if($("#comentario").attr("value") == ""){
        alert('Mensagem é campo obrigatório');
        $("#comentario").attr("style","border-color: #FF0000; border-style: solid; border-width: 1px;");
        return false;
    }else{
        $("#comentario").attr("style","border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px;");
        $('#load').fadeIn('150');
        $("#load").attr("style", "display: block");
        var textoPost = $("#comentario").attr("value");
        jQuery.post("/forum/insere", {
            'catForum':catForum,
            'idAssinante':idAssinante,
            'textoPost':textoPost
        }, function(data) {
            if(data == 'ok'){
                alert("Mensagem inserida com sucesso, aguarde a moderação da mesma!");
                $('#load').fadeOut('150');
                $("#comentario").attr("value","");
            }else{
                $('#load').fadeOut('150');
                alert("Não foi possível inserir sua mensagem, tente novamente!");
            }
        });
    }
}

var formatoData = 'yyyy-mm-dd';
var mensagemErro = "Campos em destaque contém valores inválidos ou sem preenchimento";
var mensagemErroEmail = "E-mail inválido";

function preencheFormulario(formulario,table,pk,method){
    var urlPadrao = location.href+'/preenche/1';

    $(function(){
        $('#darken').fadeIn('300');
    });
    if(method == 'get'){
        jQuery.get(urlPadrao, {
            'htmlform': formulario,
            'table':table,
            'pk':pk
        }, function(data) {
            populaCampos(data);
            $('#darken').fadeOut('300');
        });
    }else{
        if(method == 'json'){
            jQuery.getJSON(urlPadrao, function(json) {
                populaCampos(data);
                $('#darken').fadeOut('300');
            });
        }else{
            jQuery.post(urlPadrao, {
                'htmlform': formulario,
                'table':table,
                'pk':pk
            }, function(data) {
                populaCampos(data);
                $('#darken').fadeOut('300');
            });
        }
    }

}

function populaCampos(data){
    eval(data);
}

function escapeURLencodeNoBR(sStr)
{
    return escape(sStr).
    replace(/\+/g, '%2B').
    replace(/\"/g,'%22').
    replace(/\'/g, '%27').
    replace(/\//g,'%2F');
}

(function($) {
    $.fn.validate = function(args) {
        /* Load the default options. */
        var options = $.extend({}, $.fn.validate.defaults, args);
        var jQ = jQuery;
        return this.each(function() {
            /***** Plugin Goes Here *********/
            jQ(this).submit(function(){
                var valid=true;
                var validEmail=true;
                jQ(this).find(".validate").each(function(){
                    el = jQ(this);
                    var filtro = false;

                    if('filtro_' == el[0].name.substr(0, 7)){
                        filtro = true;
                        el[0].parentNode.setAttribute("style", "");
                    }else{
                        el[0].parentNode.setAttribute("style", "");
                    }
                    if(el.hasClass("email")) {
                        if(!valid_email(el.val())) {
                            add_error(el,filtro);
                            validEmail=false;
                        }
                    }
                    if(el.hasClass("date")) {
                        if(!valid_date(el.val(), formatoData)) {
                            add_error(el,filtro);
                            valid=false;
                        }
                    }
                    if(el.hasClass("number")) {
                        if(!valid_number(el.val())) {
                            add_error(el,filtro);
                            valid=false;
                        }
                    }
                    if(el.hasClass("required")) {
                        if(!valid_required(el.val())) {
                            add_error(el,filtro);
                            valid=false;
                        }
                    }
                });
                if(valid == false){
                    if($("#mensagem_erro")){
                        $("#mensagem_erro").attr("style", "display: block");
                        $("#mensagem_erro").attr("style", "font-family: \"Trebuchet MS\",Helvetica,Arial sans-serif; font-size: 16px; color: red;");
                        if(validEmail == false){
                            $("#mensagem_erro").html(mensagemErroEmail);
                        }else{
                            $("#mensagem_erro").html(mensagemErro);
                        }
                    }
                    var topo = document.URL+"#erro";
                    document.location.href = topo;
                }
                return valid;
            });
        });
    };
    //DESTACA O CAMPO COM ERRO
    function add_error(el,filtro) {
        if(!filtro){
            el[0].parentNode.setAttribute("style", "background-color:#F8E5E5");
        }else{
            el[0].parentNode.setAttribute("style", "");
        }
    }
    function valid_email(email) {
        var email_pattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(email.match(email_pattern)) return true;
        if(email == ''){
            return true;
        }
        return false;
    }
    function valid_number(number) {
        var number_pattern = /^([0-9\s])+$/;
        if(number.match(number_pattern)){
            return true;
        }
        if(number == ''){
            return true;
        }
        return false;
    }
    function valid_required(val) {
        if(val.length>0) return true;
        return false;
    }
    function valid_date(date_passed, date_format) {
        var date_pattern = null;
        if(date_format == "m/d/Y") date_pattern  = /(0[1-9]|1[012])+\/(0[1-9]|[12][0-9]|3[01])+\/(19|20)\d\d/;
        if(date_format == "m-d-Y") date_pattern  = /(0[1-9]|1[012])+\/(0[1-9]|[12][0-9]|3[01])+\/(19|20)\d\d/;
        if(date_format == "d/m/Y") date_pattern  = /(0[1-9]|[12][0-9]|3[01])+\/(0[1-9]|1[012])+\/(19|20)\d\d/;
        if(date_format == "d-m-Y") date_pattern  = /(0[1-9]|[12][0-9]|3[01])+\/(0[1-9]|1[012])+\/(19|20)\d\d/;
        if(date_passed.match(date_pattern)) return true;
        if(date_passed == ''){
            return true;
        }
        return false;
    }
})(jQuery);

function mudaAction(id,action){
    var el = document.getElementById(id);
    el.setAttribute("action", action);
    el.submit();
}

//VALIDACAO DO FORMULARIO -  DEVE CONTER A CLASS "validate" no objeto FORM
$(document).ready(function() {
    $("#fisica").bind("focus", function()
    {
        $('input[name="fisicajuridica"]')[0].checked = true;
        $(".juridica").hide(0);
        $(".fisica").fadeIn(1500);
        //
        document.getElementById("mensagem_erro").style.display = "none";
        //DEFINE ESTILO NORMAL DO FORM
        document.getElementById("cpfPf").parentNode.setAttribute("style", "");
        document.getElementById("nomePf").parentNode.setAttribute("style", "");
        document.getElementById("cepPf").parentNode.setAttribute("style", "");
        document.getElementById("enderecoPf").parentNode.setAttribute("style", "");
        document.getElementById("numeroPf").parentNode.setAttribute("style", "");
        document.getElementById("bairroPf").parentNode.setAttribute("style", "");
        document.getElementById("estadoPf").parentNode.setAttribute("style", "");
        document.getElementById("cidadePf").parentNode.setAttribute("style", "");
        document.getElementById("telefonePf").parentNode.setAttribute("style", "");
        document.getElementById("nascimentoPf").parentNode.setAttribute("style", "");
        document.getElementById("emailPf").parentNode.setAttribute("style", "");
        document.getElementById("confirmaEmailPf").parentNode.setAttribute("style", "");
        document.getElementById("senhaPf").parentNode.setAttribute("style", "");
        document.getElementById("confirmaSenhaPf").parentNode.setAttribute("style", "");
        //DEFINE CLASSES DE VALIDAÇÃO PESSOA FÍSICA
        $("#cpfPf").attr("class", "validate required");
        $("#nomePf").attr("class", "validate required");
        $("#cepPf").attr("class", "validate required");
        $("#enderecoPf").attr("class", "validate required");
        $("#numeroPf").attr("class", "validate required");
        $("#bairroPf").attr("class", "validate required");
        $("#estadoPf").attr("class", "validate required number");
        $("#cidadePf").attr("class", "validate required number");
        $("#telefonePf").attr("class", "validate required");
        $("#nascimentoPf").attr("class", "validate required");
        $("#emailPf").attr("class", "validate required");
        $("#confirmaEmailPf").attr("class", "validate required");
        $("#senhaPf").attr("class", "validate required");
        $("#confirmaSenhaPf").attr("class", "validate required");
        //LIMPA CLASSES DE VALIDAÇÃO PESSOA JURÍDICA
        $("#cnpjPj").attr("class", "");
        $("#nomePj").attr("class", "");
        $("#responsavelPj").attr("class", "");
        $("#cepPj").attr("class", "");
        $("#enderecoPj").attr("class", "");
        $("#numeroPj").attr("class", "");
        $("#bairroPj").attr("class", "");
        $("#estadoPj").attr("class", "");
        $("#cidadePj").attr("class", "");
        $("#telefonePj").attr("class", "");
        $("#emailPj").attr("class", "");
        $("#confirmaEmailPj").attr("class", "");
        $("#senhaPj").attr("class", "");
        $("#confirmaSenhaPj").attr("class", "");
    })

    $("#juridica").bind("focus", function()
    {
        $("form.validate").validate();
        //VERIFICA QUAL OPCAO SELECIONADA
        $('input[name="fisicajuridica"]')[1].checked=true
        $(".fisica").hide(0);
        $(".juridica").fadeIn(1500);
        //
        document.getElementById("mensagem_erro").style.display = "none";
        //DEFINE ESTILO NORMAL DO FORM
        document.getElementById("cnpjPj").parentNode.setAttribute("style", "");
        document.getElementById("nomePj").parentNode.setAttribute("style", "");
        document.getElementById("responsavelPj").parentNode.setAttribute("style", "");
        document.getElementById("cepPj").parentNode.setAttribute("style", "");
        document.getElementById("enderecoPj").parentNode.setAttribute("style", "");
        document.getElementById("numeroPj").parentNode.setAttribute("style", "");
        document.getElementById("bairroPj").parentNode.setAttribute("style", "");
        document.getElementById("estadoPj").parentNode.setAttribute("style", "");
        document.getElementById("cidadePj").parentNode.setAttribute("style", "");
        document.getElementById("telefonePj").parentNode.setAttribute("style", "");
        document.getElementById("emailPj").parentNode.setAttribute("style", "");
        document.getElementById("confirmaEmailPj").parentNode.setAttribute("style", "");
        document.getElementById("senhaPj").parentNode.setAttribute("style", "");
        document.getElementById("confirmaSenhaPj").parentNode.setAttribute("style", "");
        //DEFINE CLASSES DE VALIDAÇÃO PESSOA JURÍDICA
        $("#cnpjPj").attr("class", "validate required");
        $("#nomePj").attr("class", "validate required");
        $("#responsavelPj").attr("class", "validate required");
        $("#cepPj").attr("class", "validate required");
        $("#enderecoPj").attr("class", "validate required");
        $("#numeroPj").attr("class", "validate required");
        $("#bairroPj").attr("class", "validate required");
        $("#estadoPj").attr("class", "validate required number");
        $("#cidadePj").attr("class", "validate required number");
        $("#telefonePj").attr("class", "validate required");
        $("#emailPj").attr("class", "validate required");
        $("#confirmaEmailPj").attr("class", "validate required");
        $("#senhaPj").attr("class", "validate required");
        $("#confirmaSenhaPj").attr("class", "validate required");
        //LIMPA CLASSES DE VALIDAÇÃO PESSOA FÍSICA
        $("#cpfPf").attr("class", "");
        $("#nomePf").attr("class", "");
        $("#cepPf").attr("class", "");
        $("#enderecoPf").attr("class", "");
        $("#numeroPf").attr("class", "");
        $("#bairroPf").attr("class", "");
        $("#estadoPf").attr("class", "");
        $("#cidadePf").attr("class", "");
        $("#telefonePf").attr("class", "");
        $("#nascimentoPf").attr("class", "");
        $("#emailPf").attr("class", "");
        $("#confirmaEmailPf").attr("class", "");
        $("#senhaPf").attr("class", "");
        $("#confirmaSenhaPf").attr("class", "");
    })
    $("form.validate").validate();
});

function validaForm(tipo){
    if(tipo == 1){
        //DEFINE CLASSES DE VALIDAÇÃO PESSOA FÍSICA
        $("#cpfPf").attr("class", "validate required");
        $("#nomePf").attr("class", "validate required");
        $("#cepPf").attr("class", "validate required");
        $("#enderecoPf").attr("class", "validate required");
        $("#numeroPf").attr("class", "validate required");
        $("#bairroPf").attr("class", "validate required");
        $("#estadoPf").attr("class", "validate required number");
        $("#cidadePf").attr("class", "validate required number");
        $("#telefonePf").attr("class", "validate required");
        $("#nascimentoPf").attr("class", "validate required");
        $("#emailPf").attr("class", "validate required");
        $("#confirmaEmailPf").attr("class", "validate required");
        $("#senhaPf").attr("class", "validate required");
        $("#confirmaSenhaPf").attr("class", "validate required");
        //DEFINE ESTILO NORMAL DO FORM
        $("#cpfPf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#nomePf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#cepPf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#enderecoPf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#numeroPf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#bairroPf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#estadoPf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#cidadePf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#telefonePf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#nascimentoPf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#emailPf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#confirmaEmailPf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#senhaPf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#confirmaSenhaPf").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        //LIMPA CLASSES DE VALIDAÇÃO PESSOA JURÍDICA
        $("#cnpjPj").attr("class", "");
        $("#nomePj").attr("class", "");
        $("#responsavelPj").attr("class", "");
        $("#cepPj").attr("class", "");
        $("#enderecoPj").attr("class", "");
        $("#numeroPj").attr("class", "");
        $("#bairroPj").attr("class", "");
        $("#estadoPj").attr("class", "");
        $("#cidadePj").attr("class", "");
        $("#telefonePj").attr("class", "");
        $("#emailPj").attr("class", "");
        $("#confirmaEmailPj").attr("class", "");
        $("#senhaPj").attr("class", "");
        $("#confirmaSenhaPj").attr("class", "");
        $("#boletimPJ").attr("class", "");
        $("#informativoPj").attr("class", "");
    }if(tipo == 2){
        //DEFINE CLASSES DE VALIDAÇÃO PESSOA JURÍDICA
        $("#cnpjPj").attr("class", "validate required");
        $("#nomePj").attr("class", "validate required");
        $("#responsavelPj").attr("class", "validate required");
        $("#cepPj").attr("class", "validate required");
        $("#enderecoPj").attr("class", "validate required");
        $("#numeroPj").attr("class", "validate required");
        $("#bairroPj").attr("class", "validate required");
        $("#estadoPj").attr("class", "validate required number");
        $("#cidadePj").attr("class", "validate required number");
        $("#telefonePj").attr("class", "validate required");
        $("#emailPj").attr("class", "validate required");
        $("#confirmaEmailPj").attr("class", "validate required");
        $("#senhaPj").attr("class", "validate required");
        $("#confirmaSenhaPj").attr("class", "validate required");
        $("#boletimPJ").attr("class", "validate required");
        $("#informativoPj").attr("class", "validate required");
        //
        $("#cnpjPj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#nomePj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#responsavelPj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#cepPj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#enderecoPj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#numeroPj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#bairroPj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#estadoPj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#cidadePj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#telefonePj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#emailPj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#confirmaEmailPj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#senhaPj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#confirmaSenhaPj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#boletimPJ").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        $("#informativoPj").attr("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        //LIMPA CLASSES DE VALIDAÇÃO PESSOA FÍSICA
        $("#cpfPf").attr("class", "");
        $("#nomePf").attr("class", "");
        $("#cepPf").attr("class", "");
        $("#enderecoPf").attr("class", "");
        $("#numeroPf").attr("class", "");
        $("#bairroPf").attr("class", "");
        $("#estadoPf").attr("class", "");
        $("#cidadePf").attr("class", "");
        $("#telefonePf").attr("class", "");
        $("#nascimentoPf").attr("class", "");
        $("#emailPf").attr("class", "");
        $("#confirmaEmailPf").attr("class", "");
        $("#senhaPf").attr("class", "");
        $("#confirmaSenhaPf").attr("class", "");
    }
}

function validaDigitos(){
    //VALIDA DIGITAÇÃO DE E-MAIL PESSOA FISICA
    if($("#emailPf").attr("value") != "" && $("#confirmaEmailPf").attr("value") != ""){
        return validaCampos("emailPf","confirmaEmailPf");
    }
    //VALIDA DIGITAÇÃO DE E-MAIL PESSOA JURIDICA
    if($("#emailPj").attr("value") != "" && $("#confirmaEmailPj").attr("value") != ""){
        return validaCampos("emailPj","confirmaEmailPj");
    }
    //VALIDA DIGITAÇÃO DE SENHA PESSOA FISICA
    if($("#senhaPf").attr("value") != "" && $("#confirmaSenhaPf").attr("value") != ""){
        return validaCampos('senhaPf','confirmaSenhaPf');
    }
    //VALIDA DIGITAÇÃO DE SENHA PESSOA JURIDICA
    if($("#senhaPj").attr("value") != "" && $("#confirmaSenhaPj").attr("value") != ""){
        return validaCampos('senhaPj','confirmaSenhaPj');
    }
}

function validaCampos(campo, confirma){
    var campoValor = document.getElementById(campo).value;
    var confirmaValor = document.getElementById(confirma).value;
    //VALIDA DIGITAÇÃO DE E-MAIL PESSOA FISICA
    if(campoValor != "" && confirmaValor != ""){
        if(campoValor != confirmaValor){
            document.getElementById(campo).setAttribute("style", "border: 1px solid red !important;");
            document.getElementById(confirma).setAttribute("style", "border: 1px solid red !important;");
            alert('O campo de confirmação não confere');
            return false;
        }else{
            document.getElementById(campo).setAttribute("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
            document.getElementById(confirma).setAttribute("style", "border-color: #D9CBB3 #F4F0EA #F4F0EA #D9CBB3; border-style: solid; border-width: 1px; float: left;");
        }
    }
}

function enviaComentario(){
    var url_base = $("#url_base").val()
    jQuery.post(url_base+"index/comentar", {
        'comentario':$("#comentario").val(),
        'url_comentario':$("#url_comentario").val(),
        'origem':$("#origem").val(),
        'origem_pk':$("#origem_pk").val()
    }, function(data) {
        if(data == 'OK'){
            alert('Comentário enviado com sucesso. \n\n Após moderação de nossos administradores sera disponibilizado no site.');
            document.location.reload();
        }else{
            alert('Seu comentário não foi enviado verifique se todos os campos obrigátorios foram preenchidos.');
        }
    }
    );
}

/**
 * ENVIA VOTO PARA A ENQUETE
 */
function votaEnquete(formEnquete,urlPadrao){
    var pk = 0;
    if(urlPadrao == undefined)
        urlPadrao = '/';

    urlPadrao = urlPadrao + "index/ajax.votaenquete";
    var form = document.getElementById(formEnquete);
    for(i=0;i<form.length;i++){
        if(form[i].type == 'radio'){
            if(form[i].checked == true)
                pk = form[i].value;
        }
    }
    jQuery.post(urlPadrao, {
        'pk':pk,
        'enquete':document.getElementById('enqueteId').value
    }, function(data) {
        $("#opcoesEnquete").html(data);
        $("#opcoesEnquete").removeClass('enquete');
        $("#opcoesEnquete").addClass('enquete_result');
    });
}

function buscaCapitulos(capitulo,urlPadrao){
    $("#capitulo_fk").html("<option selected>aguarde...</option>");
    urlPadrao = urlPadrao + "consultoria/enviar/capitulo/id-"+capitulo;
    jQuery.post(urlPadrao, {
        'capitulo':capitulo
    }, function(data) {
        $("#capitulo_consultoria_fk").html(data);
    });
}


function txtEmail(flag){
    if(flag == 1){
        if($('#usuarioLogin').val() == ''){
            $('#usuarioLogin').removeClass();
            $('#usuarioLogin').addClass('comEmailTxt');
        }
    }else{
        $('#usuarioLogin').removeClass();
        $('#usuarioLogin').addClass('semEmailTxt');
    }
}

function txtSenha(flag){
    if(flag == 1){
        if($('#senhaLogin').val() == ''){
            $('#senhaLogin').removeClass();
            $('#senhaLogin').addClass('comSenhaTxt');
        }
    }else{
        $('#senhaLogin').removeClass();
        $('#senhaLogin').addClass('semSenhaTxt');
    }
}

function txtMascaraGenerico(flag,id,classVerdadeiro,classFalso){
    if(flag == 1){
        if($('#'+id).val() == ''){
            $('#'+id).removeClass();
            $('#'+id).addClass(classVerdadeiro);
        }
    }else{
        $('#'+id).removeClass();
        $('#'+id).addClass(classFalso);
    }
}

var tamfonte = 11;

$(document).ready(function(){
    $('#aumenta_fonte').click(function(){
        tamfonte += 2;
        if(tamfonte > 24) tamfonte=24;
        $('.conteudo p').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo strong').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo a').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo b').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo i').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo u').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo ul').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo ol').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo li').css({
            'font-size' : tamfonte+'px'
            });
    });
    $('#reduz_fonte').click(function(){
        tamfonte = tamfonte - 2;
        if(tamfonte <= 11){
            tamfonte=11;
        }
        $('.conteudo p').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo strong').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo a').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo b').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo i').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo u').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo ul').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo ol').css({
            'font-size' : tamfonte+'px'
            });
        $('.conteudo li').css({
            'font-size' : tamfonte+'px'
            });
    });
});