/**************************************************************
*
* CLASSE JAVASCRIPT REFERENCIA
*
* CRIADO POR: JONATAN BRANDI
* DATA: 13/07/2010
* OBS:
* EDITADO POR: JONATAN BRANDI
* DATA: 13/07/2010
* OBS:
*
*************************************************************/

function pesquisar(evtClick, fkempresa, msgErro, msgInicial) {
    if ($.trim(document.getElementById("inf_referencia").value) == '' || $.trim(document.getElementById("inf_referencia").value) == msgInicial) {
        alert('Preencha a referência para continuar.')
        return false;
    }

    $("#btn_pesquisar").hide();
    $("#aguarde").show();

    var v_referencia = document.getElementById("inf_referencia").value;

    $("#inf_referencia").val("Aguarde...")

    $.post(raiz+"Components/buscasReferencia/referenciaBasica/Ajax/processa.aspx", { fkempresa: fkempresa, referencia: v_referencia }, function (resposta) {
        if (resposta != '-1') {
            eval(evtClick.replace(/\[pkimovel\]/g, resposta).replace(/\[fkempresa\]/g, fkempresa));
            $("#inf_referencia").val(msgInicial);
        }
        else {
            document.getElementById("inf_referencia").value = msgErro;
        }
        $("#btn_pesquisar").show();
        $("#aguarde").hide();
    });
}

function clickPesquisar(referencia) {
    var evtClick = referencia.evtClick;
    var fkempresa = referencia.fkempresa;
    var msgErro = referencia.msgErro;

    $("#inf_referencia").val(referencia.msgInicial);

    $("#inf_referencia").focus(function () {
        if ($(this).val() == referencia.msgInicial)
            $(this).val('');
    });

    $("#inf_referencia").blur(function () {
        if ($.trim($(this).val()) == "")
            $(this).val(referencia.msgInicial);
    });

    $("#btn_pesquisar").click(function () {
        pesquisar(evtClick, fkempresa, msgErro, referencia.msgInicial);
    });
}

function teclaInput(referencia) {
    var evtClick = referencia.evtClick;
    var fkempresa = referencia.fkempresa;
    var msgErro = referencia.msgErro;

    $("#inf_referencia").keypress(function () {
        var keynum;

        if (window.event) {
            keynum = event.keyCode
        } else if (event.which) {
            keynum = event.which
        }
        if (keynum == 13) {
            pesquisar(evtClick, fkempresa, msgErro);
            return false;
        }
        else {
            if (document.getElementById("inf_referencia").value == msgErro)
                document.getElementById("inf_referencia").value = "";
        }
    });

    $("#inf_referencia").focus(function () {
        if (document.getElementById("inf_referencia").value == msgErro)
            document.getElementById("inf_referencia").value = "";
    });
}

function print(referencia) {

    $(this.destino).append(this.layout);
    referencia.clickPesquisar(referencia)
    referencia.teclaInput(referencia)    
}

function Referencia(fkempresa, destino, layout, evtClick, msgErro, msgInicial) {
    // Código fk empresa
    this.fkempresa = fkempresa;
	//Código da empresa
	this.destino = destino;
    // Layout da consulta
	this.layout = layout;
	// Evento a ser executado quando encontra referencia
	this.evtClick = evtClick;
    // Mensagem de Erro 
	this.msgErro = msgErro;
	// Mensagem inicial 
	this.msgInicial = msgInicial;

    //Métodos
	this.print = print;
	this.clickPesquisar = clickPesquisar;
	this.teclaInput = teclaInput;
}

