/**************************************************************
*
* CLASSE JAVASCRIPT BUSCA INICIAL
*
* CRIADO POR: JONATAN BRANDI
* DATA: 13/07/2010
* OBS:
* EDITADO POR: JONATAN BRANDI
* DATA: 13/07/2010
* OBS:
*
*************************************************************/

function set(posicao, campo, value){ 
	eval("this."+campo+"["+posicao+"] = '"+value+"';");
} 

function get(posicao, campo){ 
	eval("var var_return =  this."+campo+"["+posicao+"];");
	return var_return;
}

function replaceAll(str, de, para) {
    var pos = str.indexOf(de);
    while (pos > -1) {
        str = str.replace(de, para);
        pos = str.indexOf(de);
    }
    return (str);
}

function clickPesquisar(busca) {
    var fkempresa = busca.fkempresa;
    var v_desejo = "";
    var v_tipo = "";
    var v_cidade = "";
    var v_bairro = "";
    var v_cond = "";

    $(".btnConsultaHome").click(function () {
        var vWhere = " pkimovel in (select codimovel from pool2 where fkrede = '" + fkempresa + "') ";
        $(".chaveCombo").each(function () {
            if ($.trim($(this).val()).toLowerCase() != 'indiferente' && $.trim($(this).val()).toLowerCase() != 'selecione' && $.trim($(this).val()).toLowerCase() != 'selecione...' && $.trim($(this).val()).toLowerCase() != '') {
                switch ($(this).attr("campo")) {
                    case 'inf_finalidade':
                        switch ($.trim($(this).val()).toLowerCase()) {
                            case "comprar":
                                v_desejo = "comprar";
                                if (where_tipo == 1) {
                                    vWhere = vWhere + " and inf_valvenda < 1000000.00 and inf_subtipo <> 'COMERCIAL' and inf_subtipo <> 'INDUSTRIAL' ";
                                }
                                else if (where_tipo == 2) {
                                    vWhere = vWhere + " and inf_valvenda >= 1000000.00 and inf_subtipo <> 'COMERCIAL' and inf_subtipo <> 'INDUSTRIAL' ";
                                }
                                else if (where_tipo == 3) {
                                    vWhere = vWhere + " and (inf_subtipo = 'COMERCIAL' or inf_subtipo = 'INDUSTRIAL') ";
                                }
                                break;
                            case "alugar":
                                v_desejo = "alugar";
                                if (where_tipo == 1) {
                                    vWhere = vWhere + " and inf_vallocacao < 5000.00 and inf_subtipo <> 'COMERCIAL' and inf_subtipo <> 'INDUSTRIAL' ";
                                }
                                else if (where_tipo == 2) {
                                    vWhere = vWhere + " and inf_vallocacao >= 5000.00 and inf_subtipo <> 'COMERCIAL' and inf_subtipo <> 'INDUSTRIAL' ";
                                }
                                else if (where_tipo == 3) {
                                    vWhere = vWhere + " and (inf_subtipo = 'COMERCIAL' or inf_subtipo = 'INDUSTRIAL') ";
                                }
                                break;
                        }
                        break;
                    case 'inf_tipo':
                        v_tipo = $(this).val();
                        break;
                    case 'inf_cidade':
                        v_cidade = $(this).val();
                        break;
                    case 'inf_condominio':
                        v_cond = $(this).val();
                        break;
                    case 'inf_bairro':
                        var v_todos = 0;
                        var v_valores = '';
                        if (!($("#check_0").attr("checked"))) {
                            $(".comboChk").each(function () {

                                if ($(this).attr("checked")) {
                                    if (v_valores == '') {
                                        v_valores = $(this).val();
                                    }
                                    else {
                                        v_valores = v_valores + "," + $(this).val();
                                    }
                                }

                            });
                        }
                        if (v_valores != '')
                            v_bairro = v_valores;

                        break;

                }
            }

        });

        switch (parseInt(where_tipo)) {
            case 1:
                window.open(raiz + 'Resultado.aspx?desejo=' + v_desejo + '&tipo=' + v_tipo + '&cidade=' + v_cidade + '&bairro=' + v_bairro + '&cond=' + v_cond+ '&busca=' + where_tipo, "_self");
                break;
            case 2:
                window.open(raiz + 'ResultadoPrivate.aspx?desejo=' + v_desejo + '&tipo=' + v_tipo + '&cidade=' + v_cidade + '&bairro=' + v_bairro + '&cond=' + v_cond + '&busca=' + where_tipo, "_self");
                break; 
            case 3:
                window.open(raiz + 'ResultadoCorporate.aspx?desejo=' + v_desejo + '&tipo=' + v_tipo + '&cidade=' + v_cidade + '&bairro=' + v_bairro + '&cond=' + v_cond + '&busca=' + where_tipo, "_self");
                break;
            default:
                window.open(raiz + 'Resultado.aspx?desejo=' + v_desejo + '&tipo=' + v_tipo + '&cidade=' + v_cidade + '&bairro=' + v_bairro + '&cond=' + v_cond + '&busca=' + where_tipo, "_self");
                break;
        }
    });
}

function atualizaCampos(atualizar) {    
    var v_eval = '';
    var i = 0;
    var acoes = atualizar.split(",");
    var total = acoes.length;


    if ($.trim($("#inf_finalidade").val()) == "Comprar"){
         if (where_tipo == 1) {
             var v_where = " and inf_valvenda < 1000000.00 and inf_subtipo <> 'COMERCIAL' and inf_subtipo <> 'INDUSTRIAL' ";
             $(".filtro_valor").html('<div class="filtro_titulo">Valor</div><div class="filtro_dado" id=filtro_valor_1 onclick="alteraValor(\'0\',\'200000\');">- Até 200 mil</div><div class="filtro_dado" id=filtro_valor_2 onclick="alteraValor(\'200000\',\'300000\');">- De 200 mil a 300 mil</div><div class="filtro_dado" id=filtro_valor_3 onclick="alteraValor(\'300000\',\'400000\');">- De 300 mil a 400 mil</div><div class="filtro_dado" id=filtro_valor_4 onclick="alteraValor(\'400000\',\'600000\');">- De 400 mil a 600 mil</div><div class="filtro_dado" id=filtro_valor_5 onclick="alteraValor(\'600000\',\'800000\');">- De 600 mil a 800 mil</div><div class="filtro_dado" id=filtro_valor_6 onclick="alteraValor(\'800000\',\'1000000\');">- De 800 mil a 1 Mi</div><div class="filtro_dado" id=filtro_valor_7 onclick="alteraValor(\'1000000\',\'\');">- Acima de 1 Mi</div>');
        }
        else if (where_tipo == 2) {
            var v_where = " and inf_valvenda >= 1000000.00 and inf_subtipo <> 'COMERCIAL' and inf_subtipo <> 'INDUSTRIAL' ";
            $(".filtro_valor").html('<div class="filtro_titulo">Valor</div><div class="filtro_dado" id="filtro_valor_7" onclick="alteraValor(\'1000000\',\'1500000\');">- De 1 Mi. a 1,5 Mi</div><div class="filtro_dado" id="filtro_valor_8" onclick="alteraValor(\'1500000\',\'2000000\');">- De 1,5 Mi. a 2 Mi</div><div class="filtro_dado" id="filtro_valor_9" onclick="alteraValor(\'2000000\',\'2500000\');">- De 2 Mi. a 2,5 Mi</div><div class="filtro_dado" id="filtro_valor_10" onclick="alteraValor(\'2500000\',\'3000000\');">- De 2,5 Mi. a 3 Mi</div><div class="filtro_dado" id="filtro_valor_11" onclick="alteraValor(\'3000000\',\'4000000\');">- De 3 Mi. a 4 Mi</div><div class="filtro_dado" id="filtro_valor_12" onclick="alteraValor(\'4000000\',\'\');">- Acima de 4 Mi.</div>');
        }
        else if (where_tipo == 3) {
            var v_where = " and (inf_subtipo = 'COMERCIAL' or inf_subtipo = 'INDUSTRIAL') ";
            $(".filtro_valor").html('<div class="filtro_titulo">Valor</div><div class="filtro_dado" id=filtro_valor_13 onclick="alteraValor(\'0\',\'200000\');">- Até 200 mil</div><div class="filtro_dado" id=filtro_valor_14 onclick="alteraValor(\'200000\',\'300000\');">- De 200 mil a 300 mil</div><div class="filtro_dado" id=filtro_valor_15 onclick="alteraValor(\'300000\',\'400000\');">- De 300 mil a 400 mil</div><div class="filtro_dado" id=filtro_valor_16 onclick="alteraValor(\'400000\',\'600000\');">- De 400 mil a 600 mil</div><div class="filtro_dado" id=filtro_valor_17 onclick="alteraValor(\'600000\',\'800000\');">- De 600 mil a 800 mil</div><div class="filtro_dado" id=filtro_valor_18 onclick="alteraValor(\'800000\',\'1000000\');">- De 800 mil a 1 Mi</div><div class="filtro_dado" id=filtro_valor_19 onclick="alteraValor(\'1000000\',\'\');">- Acima de 1 Mi</div>');
        }   
    }
    else {
        if (where_tipo == 1) {
            var v_where = " and inf_vallocacao < 5000.00 and inf_subtipo <> 'COMERCIAL' and inf_subtipo <> 'INDUSTRIAL' ";
            $(".filtro_valor").html('<div class="filtro_titulo">Valor</div><div class="filtro_dado" id=filtro_valor_19>- Até R$ 500</div><div class="filtro_dado" id=filtro_valor_20>- De R$ 500 a 1 mil</div><div class="filtro_dado" id=filtro_valor_21>- De 1 mil a 1,5 mil</div><div class="filtro_dado" id=filtro_valor_22>- De 1,5 mil a 2 mil</div><div class="filtro_dado" id=filtro_valor_23>- De 2 mil a 3 mil</div><div class="filtro_dado" id=filtro_valor_24>- De 3 mil a 5 Mil</div>');
        }
        else if (where_tipo == 2) {
            var v_where = " and inf_vallocacao >= 5000.00 and inf_subtipo <> 'COMERCIAL' and inf_subtipo <> 'INDUSTRIAL' ";
            $(".filtro_valor").html('<div class="filtro_titulo">Valor</div><div class="filtro_dado" id=filtro_valor_25>- De 5 Mil a 6 Mil</div><div class="filtro_dado" id=filtro_valor_26>- De 6 Mil a 7 mil</div><div class="filtro_dado" id=filtro_valor_27>- De 7 mil a 8 mil</div><div class="filtro_dado" id=filtro_valor_28>- De 8 mil a 9 mil</div><div class="filtro_dado" id=filtro_valor_29>- De 9 mil a 10 mil</div><div class="filtro_dado" id=filtro_valor_30>- Acima de 10 Mil</div>');
        }
        else if (where_tipo == 3) {
            var v_where = " and (inf_subtipo = 'COMERCIAL' or inf_subtipo = 'INDUSTRIAL') ";
            $(".filtro_valor").html('<div class="filtro_titulo">Valor</div><div class="filtro_dado" id=filtro_valor_31>- Até R$ 500</div><div class="filtro_dado" id=filtro_valor_32>- De R$ 500 a 1 mil</div><div class="filtro_dado" id=filtro_valor_33>- De 1 mil a 1,5 mil</div><div class="filtro_dado" id=filtro_valor_34>- De 1,5 mil a 2 mil</div><div class="filtro_dado" id=filtro_valor_35>- De 2 mil a 3 mil</div><div class="filtro_dado" id=filtro_valor_36>- Acima de 3 mil</div>');
        }
    }
    

    for (var x = 0; x < total; x++) {
        switch (acoes[x]) {
            case 'inf_tipo':
                if (typeof ($("#inf_tipo").val()) != 'undefined') {

                    $("#inf_tipo").val("Carregando...");

                    $('.filtro_tipo').html('<div class=filtro_titulo>Tipo</div>');
                    $('.filtro_tipo').append('<div class=filtro_dado> Carregando...</div>');

                    $("#hdn_inf_tipo").val("");
                    $("#cont_inf_tipo").html("");
                    v_eval = v_eval + "$.post(raiz+'Components/buscaHome/Ajax/processa1.aspx', {  v_default: '', action: 'carregaTipo', fkempresa: '" + fkrede + "', finalidade: '" + $.trim($("#hdn_inf_finalidade").val()).toLowerCase() + "', v_where: v_where}, function (resposta) { eval(resposta); ";
                    i++;
                }
                break;
            case 'inf_cidade':
                if (typeof ($("#inf_cidade").val()) != 'undefined') {
                    $("#inf_cidade").val("Carregando...");

                    selecionaTipo(replaceAll($("#hdn_inf_tipo").val(), " ", "&nbsp;"));

                    $('.filtro_cidade').html('<div class=filtro_titulo>Cidade</div>');
                    $('.filtro_cidade').append('<div class=filtro_dado> Carregando...</div>');

                    $("#hdn_inf_cidade").val("");
                    $("#cont_inf_cidade").html("");
                    v_eval = v_eval + "$.post(raiz+'Components/buscaHome/Ajax/processa1.aspx', {  v_default: '', action: 'carregaCidade', fkempresa: '" + fkrede + "', finalidade: '" + $.trim($("#hdn_inf_finalidade").val()).toLowerCase() + "', tipo: $('#hdn_inf_tipo').val(), v_where: v_where }, function (resposta) { eval(resposta); ";
                    i++
                }
                break;
            case 'inf_bairro':
                if (typeof ($("#inf_bairro").val()) != 'undefined') {
                    $("#inf_bairro").val("Carregando...");

                    selecionaCidade($("#hdn_inf_cidade").val());

                    $("#hdn_inf_bairro").val("");
                    $("#cont_inf_bairro").html("<b>Carregando...</b>");
                    v_eval = v_eval + "$.post(raiz+'Components/buscaHome/Ajax/processa1.aspx', {  v_default: '', bairros: '', action: 'carregaBairro', fkempresa: '" + fkrede + "', finalidade: '" + $.trim($("#hdn_inf_finalidade").val()).toLowerCase() + "', tipo: $('#hdn_inf_tipo').val(), cidade: $('#hdn_inf_cidade').val(), v_where: v_where }, function (resposta) { if(resposta == ''){  $('#inf_bairro').val('Nenhum bairro encontrado');  $('#cont_inf_bairro').html('<b>Nenhum bairro encontrado</b>');} else { $('#cont_inf_bairro').html(''); eval(resposta); }";
                    i++
                }
                break;
        }
    }   
    for (x = 1; x <= i; x++)
        v_eval = v_eval + "});"

    eval(v_eval);
} 


function imovelClick(randimo) {
    var evtClick = randimo.evtClick;
    var fkempresa = randimo.fkempresa;
    $(".box_randimo").click(function () {
        eval(evtClick.replace(/\[pkimovel\]/g, $(this).attr('pkimovel')).replace(/\[fkempresa\]/g, fkempresa));
    });
}

function print() {
    $(this.destino).append(this.layout);

    var fkempresa = busca.fkempresa;
    var v_eval = '';
    var i = 0;


    $("#inf_tipo").val("Carregando...");
    $("#inf_cidade").val("Carregando...");
    $("#inf_bairro").val("Carregando...");



  
    
    if ($.trim(this.desejo) == "alugar") {
        if (where_tipo == 1) 
            var v_where = " and inf_vallocacao < 5000.00 and inf_subtipo <> 'COMERCIAL' and inf_subtipo <> 'INDUSTRIAL' ";          
        else if (where_tipo == 2)
            var v_where = " and inf_vallocacao >= 5000.00 and inf_subtipo <> 'COMERCIAL' and inf_subtipo <> 'INDUSTRIAL' ";     
        else if (where_tipo == 3)
            var v_where = " and (inf_subtipo = 'COMERCIAL' or inf_subtipo = 'INDUSTRIAL') ";
        $("#inf_finalidade").val('Alugar');
        $("#hdn_inf_finalidade").val('alugar');
    }
    else{
        if (where_tipo == 1)
            var v_where = " and inf_valvenda < 1000000.00 and inf_subtipo <> 'COMERCIAL' and inf_subtipo <> 'INDUSTRIAL' ";
        else if (where_tipo == 2)
            var v_where = " and inf_valvenda >= 1000000.00 and inf_subtipo <> 'COMERCIAL' and inf_subtipo <> 'INDUSTRIAL' ";
        else if (where_tipo == 3)
            var v_where = " and (inf_subtipo = 'COMERCIAL' or inf_subtipo = 'INDUSTRIAL') ";

        $("#inf_finalidade").val('Comprar');
        $("#hdn_inf_finalidade").val('comprar');
    }


     v_eval = v_eval + "$.post(raiz+'Components/buscaHome/Ajax/processa1.aspx', {v_default: '" + this.v_tipo + "', action: 'carregaTipo', fkempresa: '" + fkrede + "', finalidade: '" + $.trim($("#hdn_inf_finalidade").val()).toLowerCase() + "', v_where: v_where}, function (resposta) { eval(resposta); ";
    i++;

    v_eval = v_eval + "$.post(raiz+'Components/buscaHome/Ajax/processa1.aspx', {v_default: '" + this.v_cidade + "', action: 'carregaCidade', fkempresa: '" + fkrede + "', finalidade: '" + $.trim($("#hdn_inf_finalidade").val()).toLowerCase() + "', tipo: $('#hdn_inf_tipo').val(), v_where: v_where }, function (resposta) { eval(resposta); ";
    i++;

    v_eval = v_eval + "$.post(raiz+'Components/buscaHome/Ajax/processa1.aspx', {v_default: '" + this.v_bairro + "', bairros: '" + this.v_bairro + "', action: 'carregaBairro', fkempresa: '" + fkrede + "', finalidade: '" + $.trim($("#hdn_inf_finalidade").val()).toLowerCase() + "', tipo: $('#hdn_inf_tipo').val(), cidade: $('#hdn_inf_cidade').val(), v_where: v_where }, function (resposta) { eval(resposta); ";
    i++;

    for (x = 1; x <= i; x++)
        v_eval = v_eval + "});"

    eval(v_eval);

    clickPesquisar(busca);
}

function Busca(fkempresa, destino, layout, varid, v_tipo, v_cidade, v_bairro, v_busca, v_desejo) {
    //Código da empresa
    this.fkempresa = fkempresa;
    // Div de destino da consulta
	this.destino = destino;
	//Id opcional antes dos nomes dos campos
	this.id = '';
    // Layout da consulta
	this.layout = layout;

	this.v_busca = v_busca;

	this.desejo = v_desejo;

	this.v_tipo = v_tipo;
	this.v_cidade = v_cidade;
	this.v_bairro = v_bairro;

	

	if (typeof (v_busca) == 'undefined')
	    where_tipo = "1";
	else
	    where_tipo = v_busca;

	$("#box_consulta").attr('class', 'box_consulta' + where_tipo);

	/*
		Métodos impressão do Randimo
	*/
	this.print = print;
	this.clickPesquisar = clickPesquisar;
	
	/*
		Métodos GET e SET da classe
	*/
	this.set = set; 
	this.get = get;
	
	/*
		Campos da tabela
	*/
	this.pkimovel = new Array();
	this.inf_referencia = new Array();
	this.inf_tipo = new Array();
	this.inf_bairro =  new Array();
	this.det_dormitorios = new Array();
	this.det_suite = new Array();
    this.inf_valvenda =  new Array();
	this.foto =  new Array();

}

