﻿//---------------------------------REGIÃO Busca--------------------------------------------------------
$(document).ready(function () {
    //Aplicando estilos no rádio e checkbox
    $(function () {
        $("input[type=radio], input[type=checkbox]").uniform();
    });

    //Incluindo ação de OnClick no botão de logar
    $('#bt_logar_intranet').click(function () {
        Logar();
    });

    //Ao clicar no botão Buscar verifica se esta buscando por código ou por filtros detalhados de imóvel
    $("#bt_buscar_imovel , #bt_buscar_codigo").click(function (event) {
        if ($(this).attr('id') == 'bt_buscar_imovel') {
            $('#busca_por_codigo').val('0');
        } else {
            $('#busca_por_codigo').val('1');
        }
    });

    //Função para alterar entre a aba de Busca por Código e a Busca por Dados do Imóvel    
    $("a.aba-busca, a.aba-buscaporcodigo").click(function (event) {
        event.preventDefault();
        if ($(this).attr("id") == 'aba_busca_codigo') {
            $('#busca-imovel').hide();
            $('#busca-porcodigo').show();
        } else {
            $('#busca-porcodigo').hide();
            $('#busca-imovel').show();
        }
    });

    //Função para alterar entre Abas de Residencial, Comercial e Industrial
    $("#filtro_tipo_imovel_res, #filtro_tipo_imovel_com, #filtro_tipo_imovel_ind, #filtro_tipo_imovel_rur").click(function (event) {
        $("#tipo_nao_escolhido").css("display", "none");
        if (this.value == "1") {
            $("#venda_tipo_imovel_com , #venda_tipo_imovel_ind, #venda_tipo_imovel_rur").css("display", "none");
            $("#venda_tipo_imovel_res").fadeIn("normal");
        } else if (this.value == "2") {
            $("#venda_tipo_imovel_res , #venda_tipo_imovel_ind, #venda_tipo_imovel_rur").css("display", "none");
            $("#venda_tipo_imovel_com").fadeIn("normal");
        } else if (this.value == "3") {
            $("#venda_tipo_imovel_res , #venda_tipo_imovel_com, #venda_tipo_imovel_rur").css("display", "none");
            $("#venda_tipo_imovel_ind").fadeIn("normal");
        } else if (this.value == "4") {
            $("#venda_tipo_imovel_res , #venda_tipo_imovel_com, #venda_tipo_imovel_ind").css("display", "none");
            $("#venda_tipo_imovel_rur").fadeIn("normal");
        }

    });

    //Funções no Click para ao escolher qualquer outra aba, a atual é fechada
    $("#bt_tipo_imovel").click(function (event) {
        esconderTodos("#div_tipo_imovel");
        $("#div_tipo_imovel").fadeToggle("fast");
    });
    $("#bt_cidade").click(function (event) {
        esconderTodos("#div_cidade");
        $("#div_cidade").fadeToggle("fast");
    });
    $("#bt_bairro").click(function (event) {
        var cidades = []
        $('#div_cidade  input:radio:checked').each(function (index) {
            cidades.push($(this).val());
        });
        if (cidades.length > 0) {
            esconderTodos("#div_bairro");
            $("#div_bairro").fadeToggle("fast", function () {
                if (parseInt($('#hd_controla_bairro').val()) > 0)
                    carregaBairro();
            });
        }
    });
    $("#bt_caracteristicas").click(function (event) {
        esconderTodos("#div_caracteristicas");
        $("#div_caracteristicas").fadeToggle("fast");
    });
    $("#bt_valor").click(function (event) {
        esconderTodos("#div_valor");
        $("#div_valor").fadeToggle("fast");
    });


    //Setando 0 para limitar a quantidade de requisições AJAX a cada mudança de cidade
    $('#div_cidade  input:radio').each(function () {
        $(this).click(function () {
            $('#hd_controla_bairro').val('1');
        });
    });
});

//Função responsável por carregar o Bairro de acordo com a Cidade selecionada
function carregaBairro() {
    //Status Carregando
    if (parseInt($('#hd_controla_bairro').val()) > 0) 
    {
        $("#status_bairro").addClass("carregando");

        var rbt_bairro = $("#div_cidade input:radio:checked");
        var bln_aluguel = $('#quero_alugar').attr('checked');

        $("#div_cidade").fadeOut("fast");

        var bairro = includes_include_header2.carregaBairro(rbt_bairro.val(), bln_aluguel).value;

        if (bairro.length > 0) {
            $("#lista_bairros").empty();
            $("#lista_bairros").html(bairro);

            $('#hd_controla_bairro').val('0');

            //Setando Status Cidade
            verificaStatus();
        } else {
            $("#lista_bairros").empty();
            $("#lista_bairros").html("<li>Selecione uma cidade antes de escolher os bairros.</li>");
            //Setando Status Cidade
            verificaStatus();
        }
        $("#lista_bairros input[type=checkbox]").uniform();

        //Removendo Status Carrgando
        $("#status_bairro").removeClass("carregando");
        
        return false;
    }
    
    verificaStatus();

    $("#div_cidade").fadeOut("fast");
    return false;
}

function verificaStatus() {
    
    //Verificando Status do Tipo de Imovel
    //var attributes = [];
    if ($('#filtro_tipo_imovel_res').attr('checked')) {
        $("#tipo_nao_escolhido").css("display", "none");
        $("#status_tipo_imovel").addClass("ok");
        //$('#venda_tipo_imovel_res  input:checkbox:checked').each(function (index) {
        //    attributes.push($(this).val());
        //});
    } else if ($('#filtro_tipo_imovel_com').attr('checked')) {
        $("#tipo_nao_escolhido").css("display", "none");
        $("#status_tipo_imovel").addClass("ok");
        //$('#venda_tipo_imovel_com  input:checkbox:checked').each(function (index) {
        //    attributes.push($(this).val());
        //});
    } else if ($('#filtro_tipo_imovel_ind').attr('checked')) {
        $("#tipo_nao_escolhido").css("display", "none");
        $("#status_tipo_imovel").addClass("ok");
        //$('#venda_tipo_imovel_ind  input:checkbox:checked').each(function (index) {
        //    attributes.push($(this).val());
        //});
    } else if ($('#filtro_tipo_imovel_rur').attr('checked')) {
        $("#tipo_nao_escolhido").css("display", "none");
        $("#status_tipo_imovel").addClass("ok");
        //$('#venda_tipo_imovel_ind  input:checkbox:checked').each(function (index) {
        //    attributes.push($(this).val());
        //});
    }
    //if (attributes.length > 0) {
    //    $("#status_tipo_imovel").addClass("ok");
    //} else {
    //    $("#status_tipo_imovel").removeClass("ok");
    //}

    //Verificando Status da Cidade
    var attributes = [];
    $('#div_cidade input:radio:checked').each(function (index) {
        attributes.push($(this).val());
    });
    if (attributes.length > 0) {
        $("#status_cidade").addClass("ok");
        
    } else {
        $("#status_cidade").removeClass("ok");
    }

    //Verificando Status de Bairro
    var attributes = [];
    $('#lista_bairros input:checkbox:checked').each(function (index) {
        attributes.push($(this).val());
    });

    if (attributes.length > 0) {
        $("#status_bairro").addClass("ok");
    } else {
        $("#status_bairro").removeClass("ok");
    }

    //Verificando Status de Caracteristicas
    var attributes_dor = [];
    $('#lista_dormitorio input:radio:checked').each(function (index) {
        attributes_dor.push($(this).val());
    });
    var attributes_sui = [];
    $('#lista_suite input:radio:checked').each(function (index) {
        attributes_sui.push($(this).val());
    });
    var attributes_gar = [];
    $('#lista_garagem input:radio:checked').each(function (index) {
        attributes_gar.push($(this).val());
    });

    if (attributes_dor.length > 0 || attributes_sui.length > 0 || attributes_gar.length > 0) {
        if (attributes_dor[0] != '0' || attributes_sui[0] != '0' || attributes_gar[0] != 0)
            $("#status_caracteristicas").addClass("ok");
        else
            $("#status_caracteristicas").removeClass("ok");

    } else {
        $("#status_caracteristicas").removeClass("ok");
    }


    //Verifica Status de Valor
    if ($('#valor_minimo').val() != "" || $('#valor_maximo').val() != "")
        $('#status_valor').addClass('ok');
    else
        $('#status_valor').removeClass('ok');
}

function marcarTodos(id, objeto) {
    $('#' + id + '  input:checkbox').each(function () {
        if (objeto.checked) {
            $(this).parent("span").addClass("checked");
            $(this).attr("checked", true);
        } else {
            $(this).parent("span").removeClass("checked");
            $(this).attr("checked", false);
        }
    });
}

function esconderTodos(id) {
    verificaStatus();

    if (id == "#div_tipo_imovel") {
        $("#div_cidade , #div_bairro , #div_caracteristicas , #div_valor").fadeOut("fast");
    } else if (id == "#div_cidade") {
        $("#div_valor , #div_caracteristicas , #div_tipo_imovel , #div_bairro").fadeOut("fast");
    } else if (id == "#div_bairro") {
        $("#div_caracteristicas , #div_cidade , #div_tipo_imovel , #div_valor").fadeOut("fast");
    } else if (id == "#div_caracteristicas") {
        $("#div_valor , #div_bairro , #div_cidade , #div_tipo_imovel").fadeOut("fast");
    } else if (id == "#div_valor") {
        $("#div_caracteristicas , #div_bairro , #div_cidade , #div_tipo_imovel").fadeOut("fast");
    }
}

function proximoPasso(origem)
{
    switch (origem) {
        case 'tipoimovel':
            verificaStatus();
            //Sumindo com a DIV
            $("#div_tipo_imovel").fadeOut("fast");
            break;
        case 'cidade':
            verificaStatus();
            //Sumindo com a DIV
            $("#div_cidade").fadeOut("fast");
            break;
        case 'bairro':
            verificaStatus();
            //Sumindo com a DIV
            $("#div_bairro").fadeOut("fast");
            break;
        case 'caracteristica':
            verificaStatus();
            //Sumindo com a DIV
            $("#div_caracteristicas").fadeOut("fast");
            break;
        default:
            break;
    }
    return false;
}



//---------------------------------REGIÃO Login--------------------------------------------------------
function Logar() {
    var sucesso = true;

    $('#bt_logar_intranet').hide();
    $('#img_logando').show();

    // Define os estilos padrões
    document.getElementById('login').className = 'campotxt';
    document.getElementById('erro_login').innerHTML = '';
    //document.getElementById('erro_login').style.display = 'none';

    document.getElementById('senha').className = 'campotxt';
    document.getElementById('erro_senha').innerHTML = '';
    //document.getElementById('erro_senha').style.display = 'none';

    if (EmptyField(document.getElementById('login'))) {
        sucesso = false;
        //document.getElementById('erro_login').style.display = '';
        document.getElementById('login').className = 'campotxt erro';
        document.getElementById('erro_login').innerHTML = 'Por favor, informe seu login.';
    } else {
        if (document.getElementById('login').value.toUpperCase() == 'LOGIN') {
            sucesso = false;
            document.getElementById('login').className = 'campotxt erro';
            //document.getElementById('erronews_nome').style.display = '';
            document.getElementById('erro_login').innerHTML = 'Por favor, informe seu login.'
        }
    }

    if (EmptyField(document.getElementById('senha'))) {
        sucesso = false;
        //document.getElementById('erro_senha').style.display = '';
        document.getElementById('senha').className = 'campotxt erro';
        document.getElementById('erro_senha').innerHTML = 'Por favor, informe sua senha.';
    } else {
        if (document.getElementById('senha').value.toUpperCase() == 'SENHA') {
            sucesso = false;
            document.getElementById('senha').className = 'campotxt erro';
            //document.getElementById('erronews_nome').style.display = '';
            document.getElementById('erro_senha').innerHTML = 'Por favor, informe sua senha.'
        }
    }


    if (!sucesso) {
        $('#bt_logar_intranet').show();
        $('#img_logando').hide();
        return false;
    }

    //Verifica Login e senha no servidor.
    if (!includes_include_header2.ExecutaLogar(document.getElementById('login').value, document.getElementById('senha').value).value) {
        document.getElementById('erro_login').innerHTML = 'Dados incorretos.';
        document.getElementById('senha').value = '';

        $('#bt_logar_intranet').show();
        $('#img_logando').hide();

        return false;
    }
    else {
        if (document.getElementById('IncludeHeader_extrato').value == "ok") {
            window.location = 'intranet/alugar_form_extrato.aspx';
        }
        else {
            window.location = 'intranet/default.aspx';
        }
    }
}

function Deslogar() {
    includes_include_header2.Deslogar();

    window.location = 'default.aspx';
}

function DeslogarIntranet() {
    includes_include_header_intranet.Deslogar();

    window.location = 'default.aspx';
}
//---------------------------------FIM-REGIÃO Login--------------------------------------------------------


//--------------------------------INICIO - LOGIN SISTEMA ADM -----------------------------------------------
function LogarAdm() {
    var sucesso = true;

    // Define os estilos padrões
    document.getElementById('lg_user').className = 'txtfield';
    document.getElementById('errolg_user').style.display = 'none';


    document.getElementById('lg_senha').className = 'txtfield';
    document.getElementById('errolg_senha').style.display = 'none';

    if (EmptyField(document.getElementById('lg_user'))) {
        sucesso = false;
        //document.getElementById('IncludeHeader_lg_user').className = 'error';
        document.getElementById('errolg_user').style.display = '';
        document.getElementById('errolg_user').innerHTML = 'Por favor informe seu login.'
    }

    if (EmptyField(document.getElementById('lg_senha'))) {
        sucesso = false;
        //document.getElementById('IncludeHeader_lg_senha').className = 'error';
        document.getElementById('errolg_senha').style.display = '';
        document.getElementById('errolg_senha').innerHTML = 'Por favor informe sua senha.'
    }
    if (EmptyField(document.getElementById('lg_codigo'))) {
        sucesso = false;
        //document.getElementById('IncludeHeader_lg_senha').className = 'error';
        document.getElementById('errolg_codigo').style.display = '';
        document.getElementById('errolg_codigo').innerHTML = 'Por favor informe o código.'
    }

    if (!sucesso) {
        //alert('Alguns campos não foram preenchidos da forma devida. Por favor verifique os campos marcados em vermelho.');
        return false;
    }

    //Verifica Login e senha no servidor.
    if (!_login.ExecutaLogarAdm(document.getElementById('lg_user').value, document.getElementById('lg_senha').value, document.getElementById('lg_codigo').value).value) {
        alert('Dados incorretos.');
        document.getElementById('lg_senha').value = '';
        return false;
    }
    else {
        window.location = 'intranet/default.aspx';
    }
}
//---------------------------------FIM-REGIÃO Login--------------------------------------------------------



//Função para dar um Enter cair sobre o evento de submit da pagina de contato
//param:evt = Evento do keyPress
//param:botao = Id do botão que dá o submit na página
function EnterContato(evt,botao)
{
    //Verificando se o que é digitado é somente campos numéricos
    var charCode = (evt.which) ? evt.which : evt.keyCode;
  
    if(charCode==13)
    {
         document.getElementById(botao).onclick();		
         return false;
    }
    return true;
}
