$(document).ready(function() {
    $("form").hide();
    //função para alternar os formulários
    $("input:radio[name=categorias]").click(function() {
        $("form").hide();
        idform = $(this).attr('class').split(' ')[0];
        $('#' + idform).show();
    });

    //Verifica quando a página é carregada se algum form já está selecionado, se tiver mantem
    frmDefault = false
    $("input:radio[name=categorias]").each(function() {
        if (this.checked == true) {
            $("form").hide();
            idform = $(this).attr('class').split(' ')[0];
            $('#' + idform).show();
            frmDefault = true;
        }
    })
    //Quando carregar a página pela primeira vez trazer marcado Consumidor
    if (frmDefault == false) {
        document.getElementById("Consumidor").checked = true;
        $('#Frm3').show();
        if ($("#Consumidor")) {
            this.checked = true;
        };
    }

    //Pesquisa o cep
    $('input[name^=cep]').blur(function() {
        if ($(this).val().indexOf("_") == -1) {
            form = $(this).attr('id').substr(($(this).attr('id').length - 4), 4);
            $(this).after("<span id='aguarde" + form + "'></span>")
            $.ajax({
                type: "get",
                cache: false,
                url: "../cep/exec.asp",
                data: "cep=" + $(this).val(),
                beforeSend: function() { $("#aguarde" + form).html(" <img src='../imagens/ajax-loader.gif' alt='' /> Aguarde...").fadeIn("slow") },
                complete: function() { $("#aguarde" + form).fadeOut("slow") },
                success: function(resposta) {
                    $("#aguarde" + form).remove();
                    $("#estado" + form).val(resposta.split("##")[0]);
                    $("#cidade" + form).val(resposta.split("##")[1]);
                    $("#bairro" + form).val(resposta.split("##")[2]);
                    $("#endereco" + form).val(resposta.split("##")[3]);
                    $("#complemento" + form).val(resposta.split("##")[4]);
                }
            });
        }
    });

    //habilita os campos ocultos
    $("div[class^=oculta]").hide();
    $("input:checkbox[name=perguntavitalcan]").click(function() {
        form = $(this).attr('id').substr(($(this).attr('id').length - 4), 4);
        if (this.checked == true) {
            $("div[class^=oculta" + form + "]").show();
        } else {
            $("div[class^=oculta" + form + "]").hide();
        }
    })

    //Verifica quando a página é carregada se algum campo oculto já está selecionado, se tiver mantem
    $("input:checkbox[name=perguntavitalcan]").each(function() {
        if (this.checked == true) {
            idform = $(this).attr('id').substr(($(this).attr('id').length - 4), 4);
            $('div[class=oculta' + idform + ']').show();
        }
    })

    //Aplicando as mascaras
    $("input:text[name^=telefone],input:text[name=fax],input:text[name=celular]").mask("(99) 9999-9999");
    $("input:text[name^=cep]").mask("99999999");
    $("input:text[name=nascimento],input:text[name=inicioatividades]").mask("99/99/9999");


    //Validando os formulários
    $("form").each(function() {
        $(this).validate({
            rules: {
                nome: "required",
                email: {
                    required: true,
                    email: true
                },
                telefone: 'required',
                bairro: 'required',
                cidade: 'required',
                estado: 'required',
                razaosocialcanil: {
                    required: "#perguntavitalcanFrm1:checked"
                },
                bairrocanil: {
                    required: "#perguntavitalcanFrm1:checked"
                },
                cidadecanil: {
                    required: "#perguntavitalcanFrm1:checked"
                },
                estadocanil: {
                    required: "#perguntavitalcanFrm1:checked"
                },
                telefonecanil: {
                    required: "#perguntavitalcanFrm1:checked"
                }
            }
        });
    })

    $("#comoconheceu").change(function() {
        if (this.value == 9) {
            $("#OutrosFrm3").removeClass("none")
        } else {
            $("#OutrosFrm3").addClass("none")
        }
    })

    $("#outrasmarcascanil").change(function() {
        if (this.value == "Outras") {
            $("#OutrosFrm1").removeClass("none")
        } else {
            $("#OutrosFrm1").addClass("none")
        }
    })


    $("input:radio[name=clinica]").click(function() {
        if (this.checked == true && this.id == "OutrosFrm5") {
            $("#dlOutrosFrm5").removeClass("none")
        } else {
            $("#dlOutrosFrm5").addClass("none")
        }
    })

    $("input:radio[name=possuicao]").click(function() {
        if (this.checked == true && this.id == "possuicaoSimFrm5") {
            $("#qtosCaesFrm5").removeClass("none")
        } else {
            $("#qtosCaesFrm5").addClass("none")
        }
    })

    if (location.href.indexOf("flag=1") != -1) {
        window.setTimeout("$('#envsuc').fadeOut('slow')", 5000);
    }
});