//Visualizar Busca por endereços
function carregaBuscaEndereco(formu, destino, apenasRede, duplicaMissao, arp, tipo)
{
  try {
    if (apenasRede==undefined || apenasRede==null) apenasRede='0';
    if (duplicaMissao==undefined || duplicaMissao==null) duplicaMissao='0';
    if (arp==undefined || arp==null) arp=false;
    if (tipo==undefined || tipo==null) tipo='html';
    var dir  = 'mod=busca_endereco&p=tela_endereco&formu='+formu+'&destino='+destino+'&apenasRede='+apenasRede+'&duplicaMissao='+duplicaMissao+'&tipo='+tipo;
    dialog('Selecionar Endere&ccedil;o',dir,880,500);
    //alert(apenasRede);
    if (arp) {
      //alert(document.getElementById('DHTMLSuite_modalBox_contentDiv').innerHTML.replace('alt="Fechar" style="display: block;"','alt="Fechar" style="display: none;"'));
      //document.getElementById('DHTMLSuite_modalBox_contentDiv').innerHTML = document.getElementById('DHTMLSuite_modalBox_contentDiv').innerHTML.replace('title="Fechar"','title="Fechar" style="display: none;"');
      modalDialog.setCloseAction("document.getElementById('dadosARP').responsavel.focus()");
    }
  }   
  catch(e) {}
}

function validaSelecaoEndereco(formu, destino) {
    if (document.getElementById('endereco_cep_sel').value=='') {
        alert('Deve ser selecionado um Endereço APH');
        return false;
    } else {
        alert('Deve ser selecionado um Endereço APH');
        selecionaEndereco(formu, destino);
    }
}
//Procura o endereço
function procuraEndereco(formulario, destino) 
{
    try {       
        document.getElementById('formEnderecoX').processar.value     = 1;   
        if(document.getElementById('formEnderecoX').endereco.value.length >= 2 || 
            document.getElementById('formEnderecoX').nome_fantasia.value.length >= 2 || 
            document.getElementById('formEnderecoX').uf.value !='' || 
            document.getElementById('formEnderecoX').cidade.value.length >= 2 || 
            document.getElementById('formEnderecoX').bairro.value.length >= 2 || 
    		document.getElementById('formEnderecoX').cep.value.length >= 5 ||
    		document.getElementById('formEnderecoX').cliente2.value!='' 
    	) {
          document.getElementById('formEnderecoX').form_orig.value = formulario;
          document.getElementById('formEnderecoX').destino.value = destino;
          var urlx  = '?mode=ajax&mod=busca_endereco&p=tela_endereco';
          var where = 'endereco_encontrados';   
          ajax_post(urlx, where, 'formEnderecoX', '');       
        } else {
          alert('Favor digitar um dos campos para pesquisa (no mínimo 2 caracteres)');
          return false;
        }
        return true;
    }   
    catch(e) {return false;}
}

function preencheSelecaoEndereco(seq) {
    if (document.getElementById('endereco_origem_sel')!=null)       document.getElementById('endereco_origem_sel').value        = document.getElementById('origemX_'+seq).innerHTML;  
    if (document.getElementById('endereco_local_sel')!=null)        document.getElementById('endereco_local_sel').value         = document.getElementById('localX_'+seq).innerHTML;  
    if (document.getElementById('endereco_cep_sel')!=null)          document.getElementById('endereco_cep_sel').value           = document.getElementById('cep_'+seq).innerHTML;  
    if (document.getElementById('endereco_endereco_sel')!=null)     document.getElementById('endereco_endereco_sel').value      = document.getElementById('endereco_'+seq).innerHTML;  
    if (document.getElementById('endereco_bairro_sel')!=null)       document.getElementById('endereco_bairro_sel').value        = document.getElementById('bairro_'+seq).innerHTML;  
    if (document.getElementById('endereco_cidade_sel')!=null)       document.getElementById('endereco_cidade_sel').value        = document.getElementById('cidade_'+seq).innerHTML;  
    if (document.getElementById('endereco_uf_sel')!=null)           document.getElementById('endereco_uf_sel').value            = document.getElementById('uf_'+seq).innerHTML;  
    if (document.getElementById('endereco_ref_sel')!=null)          document.getElementById('endereco_ref_sel').value           = document.getElementById('ref_'+seq).innerHTML;  
    if (document.getElementById('endereco_codbairro_sel')!=null)    document.getElementById('endereco_codbairro_sel').value     = document.getElementById('codbairro_'+seq).innerHTML;  
    if (document.getElementById('endereco_codcidade_sel')!=null)    document.getElementById('endereco_codcidade_sel').value     = document.getElementById('codcidade_'+seq).innerHTML;  
    if (document.getElementById('endereco_compl_sel')!=null)        document.getElementById('endereco_compl_sel').value         = document.getElementById('compl_'+seq).innerHTML;  
    if (document.getElementById('endereco_numero_sel')!=null)       document.getElementById('endereco_numero_sel').value        = document.getElementById('numero_'+seq).innerHTML;  
    if (document.getElementById('endereco_coordenada_sel')!=null)   document.getElementById('endereco_coordenada_sel').value    = document.getElementById('coordenada_'+seq).innerHTML;  
    if (document.getElementById('endereco_pagina_sel')!=null)       document.getElementById('endereco_pagina_sel').value        = document.getElementById('pagina_'+seq).innerHTML;  
  if (document.getElementById('cliente_'+seq)!=null) {
    if (document.getElementById('endereco_cliente_sel')!=null)      document.getElementById('endereco_cliente_sel').value       = document.getElementById('cliente_'+seq).innerHTML;
    if (document.getElementById('endereco_idcliente_sel')!=null)       document.getElementById('endereco_idcliente_sel').value        = document.getElementById('id_cliente_'+seq).innerHTML;
  }
}

//Retorna o endereco selecionado na busca
function selecionaEndereco(formu, destino)
{
  try {
     destino2 = (destino.match('orig') ) ? 'origem' : (destino.match('dest')) ? 'destino' : destino;
     destino = (destino=='') ? '' : '_'+destino;
	   if (destino2+destino!='') {
  	   if (formu=="frmEditaGuia") {
  	     destino2 = 'desc'+destino;
  	     eval("if (document.getElementById('"+formu+"')."+destino2+"!=null)  document.getElementById('"+formu+"')."+destino2+".value     = document.getElementById('endereco_origem_sel').value");
  	   } else {
  	     eval("if (document.getElementById('"+formu+"')."+destino2+destino+"!=null)  document.getElementById('"+formu+"')."+destino2+destino+".value     = document.getElementById('endereco_origem_sel').value");
  	   }
     }
     eval("if (document.getElementById('"+formu+"').tipo"+destino+"!=null)       document.getElementById('"+formu+"').tipo"+destino+".value          = document.getElementById('endereco_local_sel').value");
     eval("if (document.getElementById('"+formu+"').cep"+destino+"!=null)        document.getElementById('"+formu+"').cep"+destino+".value           = document.getElementById('endereco_cep_sel').value");
     eval("if (document.getElementById('"+formu+"').endereco"+destino+"!=null)   document.getElementById('"+formu+"').endereco"+destino+".value      = document.getElementById('endereco_endereco_sel').value");
     eval("if (document.getElementById('"+formu+"').numero"+destino+"!=null)     document.getElementById('"+formu+"').numero"+destino+".value        = document.getElementById('endereco_numero_sel').value");
     eval("if (document.getElementById('"+formu+"').compl"+destino+"!=null)      document.getElementById('"+formu+"').compl"+destino+".value         = document.getElementById('endereco_compl_sel').value");
     eval("if (document.getElementById('"+formu+"').cod_bairro"+destino+"!=null) document.getElementById('"+formu+"').cod_bairro"+destino+".value    = document.getElementById('endereco_codbairro_sel').value");
     eval("if (document.getElementById('"+formu+"').bairro"+destino+"!=null)     document.getElementById('"+formu+"').bairro"+destino+".value        = document.getElementById('endereco_bairro_sel').value");
     eval("if (document.getElementById('"+formu+"').cod_cidade"+destino+"!=null) document.getElementById('"+formu+"').cod_cidade"+destino+".value    = document.getElementById('endereco_codcidade_sel').value");
     eval("if (document.getElementById('"+formu+"').cidade"+destino+"!=null)     document.getElementById('"+formu+"').cidade"+destino+".value        = document.getElementById('endereco_cidade_sel').value");
     eval("if (document.getElementById('"+formu+"').uf"+destino+"!=null)         document.getElementById('"+formu+"').uf"+destino+".value            = document.getElementById('endereco_uf_sel').value");
    eval("if (document.getElementById('"+formu+"').cod_uf"+destino+"!=null)        document.getElementById('"+formu+"').cod_uf"+destino+".value           = document.getElementById('endereco_uf_sel').value");
     eval("if (document.getElementById('"+formu+"').pagina"+destino+"!=null)     document.getElementById('"+formu+"').pagina"+destino+".value        = document.getElementById('endereco_pagina_sel').value");
     eval("if (document.getElementById('"+formu+"').coordenada"+destino+"!=null) document.getElementById('"+formu+"').coordenada"+destino+".value    = document.getElementById('endereco_coordenada_sel').value");
    eval("if (document.getElementById('"+formu+"').referencia"+destino+"!=null) document.getElementById('"+formu+"').referencia"+destino+".value    = trim(document.getElementById('endereco_ref_sel').value)");
     
    if (document.getElementById('apenasRede').value=='1') {
        carregarClienteCaptacao();	  
	 }
  }   
  catch(e) {}
}

/** 
* Função para Buscar as Cidades Inline (Campo Auto-Preenchimento de acordo com o valor digitado)
* @access public
* @param Object objeto
* @param String id_conteiner
* @param String script
* @param String mod
* @param String sub
* @param String pag
* @return Boolean
*/
function buscaCidade(event, objeto,id_conteiner,origem, duplicaMissao, zeraCampo) {
  form = document.getElementById(objeto.form.id);
  if (zeraCampo==null || zeraCampo==undefined) zeraCampo = false;
  objeto.value = objeto.value.replace(/'/g,'');
  script = "selecionaCidade('"+form.id+"','"+origem+"','"+duplicaMissao+"'); document.getElementById('"+id_conteiner+"').style.display='none'; document.getElementById('"+id_conteiner+"').innerHTML=''";
  if (event!=null && event!=undefined) {
    tecla = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    var vStrcaract = new RegExp(/^[0-9a-zA-Z]+$/i);
    var vStrcaract = vStrcaract.test(String.fromCharCode(tecla));
    var vSetaUp = (event.keyCode==38);
    var vSetaDown = (event.keyCode==40);
    if (vSetaUp || vSetaDown) {
      if (document.getElementById(id_conteiner).style.display!='none' && trim(document.getElementById(id_conteiner).innerHTML)!='') {
        tipo = (vSetaUp?'up':'down');
        navegaObjetoCidade(id_conteiner, tipo);
      }
      return true;
    }
    if (event.keyCode==13) {
      if (document.getElementById(id_conteiner).style.display!='none' && trim(document.getElementById(id_conteiner).innerHTML)!='') {
        eval(script);
        return true;
      }
    }
    if (!(vStrcaract || tecla==8 || tecla==46)) return false;
  }
    if (typeof(exec)=='undefined') {
        exec = false;
    } else {
        window.clearTimeout(exec);
    }
    if (duplicaMissao==null || duplicaMissao==undefined) duplicaMissao='0';
    origem2 = (origem=='' ? '' : '_'+origem);
    if (document.getElementById('cod_pais'+origem2)!=null) {
      if (document.getElementById('cod_pais'+origem2).value!='55') return false;
    }
    exec = window.setTimeout(   function () {
                                  try{
                                    form = document.getElementById(objeto.form.id);
                                    var minLength = 3;
                                    eval('form.cod_cidade'+origem2+'.value=""');
                                    defineCloseObject(objeto, id_conteiner, zeraCampo);
                                    if (objeto.value.length < minLength){
                                      document.getElementById(id_conteiner).style.display='none';
                                      return false;
                                    }
                                    var str = objeto.value;
                                    var id_form = form.id;
                                    var uf = eval('form.uf'+origem2+'.value');
                                    form.processar.value     = 0;   
                                    var urlx = '?mode=ajax&mod=busca_endereco&p=cidades&uf='+uf+'&cidade='+str+'&id_conteiner='+id_conteiner+'&form='+id_form+'&origem='+origem+'&duplicaMissao='+duplicaMissao;
                                    retorno = simple_sjax_post(urlx, id_conteiner, form.id, true,'',false);
                                    if (str==objeto.value) {
                                        if(retorno.length > 0){
                                          if (is_ie()) {
                                            innerHTML_ie(id_conteiner,retorno);
                                          } else {
                                            document.getElementById(id_conteiner).innerHTML = retorno;
                                          }
                                          document.getElementById(id_conteiner).onclick=function() {eval(script);};
                                        }else{
                                          if (is_ie()) {
                                            innerHTML_ie(id_conteiner,"<div class=\"msgInline\">Nenhuma Cidade encontrada!</div>");
                                          } else {
                                            document.getElementById(id_conteiner).innerHTML = "<div class=\"msgInline\">Nenhuma Cidade encontrada!</div>";
                                          }
                                        }
                                    }
                                    document.getElementById(id_conteiner).style.display='block';
                                    return true;
                                  }catch(erro){alert(erro);}
                                  return false;
                                },500);
}

function navegaObjetoCidade(id_conteiner, tipo) {
    var cliAtual = parseInt(document.getElementById('seq_cidade_busca_sel').value);
    cliOld = cliAtual;
    cliAtual = (tipo=='up'?cliAtual-1:cliAtual+1);
    if (cliAtual<=0) cliAtual = 1;
    if (document.getElementById('cidade_codcidade_'+cliAtual)==null) cliAtual = cliOld;
    preencheSelecaoCidade(cliAtual);

    //document.getElementById('tr_paciente_inline_'+cliAtual).className = 'selectLine2Hover'; 
    if (document.getElementById('tr_cidade_busca_'+(cliAtual-1))!=null) document.getElementById('tr_cidade_busca_'+(cliAtual-1)).className = 'selectLine2';
    if (document.getElementById('tr_cidade_busca_'+(cliAtual+1))!=null) document.getElementById('tr_cidade_busca_'+(cliAtual+1)).className = 'selectLine2';
}

function preencheSelecaoCidade(seq) {
    var seqTmp = (document.getElementById('seq_cidade_busca_sel').value);
    if (document.getElementById('tr_cidade_busca_'+seqTmp)!=null) document.getElementById('tr_cidade_busca_'+seqTmp).className='selectLine2';

    document.getElementById('seq_cidade_busca_sel').value=seq;

    if (document.getElementById('cidade_codcidade_sel')!=null)       document.getElementById('cidade_codcidade_sel').value        = document.getElementById('cidade_codcidade_'+seq).value;  
    if (document.getElementById('cidade_cidade_sel')!=null)          document.getElementById('cidade_cidade_sel').value           = document.getElementById('cidade_cidade_'+seq).innerHTML;
    if (document.getElementById('cidade_estado_'+seq).type=='hidden') {    
        if (document.getElementById('cidade_estado_sel')!=null)      document.getElementById('cidade_estado_sel').value           = document.getElementById('cidade_estado_'+seq).value;
    } else {
        if (document.getElementById('cidade_estado_sel')!=null)      document.getElementById('cidade_estado_sel').value           = document.getElementById('cidade_estado_'+seq).innerHTML;
    }      
      
    document.getElementById('tr_cidade_busca_'+seq).className='selectLine2Hover';
}

//Retorna o endereco selecionado na busca
function selecionaCidade(formu, destino,duplicaMissao)
{
  try {
     destino = (destino=='') ? '' : '_'+destino;
     eval("if (document.getElementById('"+formu+"').cod_cidade"+destino+"!=null) document.getElementById('"+formu+"').cod_cidade"+destino+".value   = document.getElementById('cidade_codcidade_sel').value");
     eval("if (document.getElementById('"+formu+"').cidade"+destino+"!=null)    document.getElementById('"+formu+"').cidade"+destino+".value      = document.getElementById('cidade_cidade_sel').value");
     eval("if (document.getElementById('"+formu+"').uf"+destino+"!=null)    document.getElementById('"+formu+"').uf"+destino+".value      = document.getElementById('cidade_estado_sel').value");
     eval("if (document.getElementById('"+formu+"').cod_uf"+destino+"!=null)           document.getElementById('"+formu+"').cod_uf"+destino+".value      = document.getElementById('cidade_estado_sel').value");
     eval("if (document.getElementById('"+formu+"').cod_pais"+destino+"!=null)      document.getElementById('"+formu+"').cod_pais"+destino+".value      = '55'");
     eval("if (document.getElementById('"+formu+"').pais"+destino+"!=null)          document.getElementById('"+formu+"').pais"+destino+".value      = 'BRASIL'");
     
     if (duplicaMissao=='1') {
    destino = destino.replace('ida','volta');
    destino = destino.replace('Ida','Volta');
        formu = formu.replace('ida','volta');
        formu = formu.replace('Ida','Volta');
        if (destino.match('orig')) {
          destino = destino.replace('orig','dest');
          formu = formu.replace('Origem','Destino');
        }
        else if (destino.match('dest')) {
          destino = destino.replace('dest','orig');
          formu = formu.replace('Destino','Origem');
        }
      eval("if (document.getElementById('"+formu+"').cod_cidade"+destino+"!=null) document.getElementById('"+formu+"').cod_cidade"+destino+".value   = document.getElementById('cidade_codcidade_sel').value");
      eval("if (document.getElementById('"+formu+"').cidade"+destino+"!=null)    document.getElementById('"+formu+"').cidade"+destino+".value      = document.getElementById('cidade_cidade_sel').value");
      eval("if (document.getElementById('"+formu+"').uf"+destino+"!=null)    document.getElementById('"+formu+"').uf"+destino+".value      = document.getElementById('cidade_estado_sel').value");
        eval("if (document.getElementById('"+formu+"').cod_uf"+destino+"!=null)           document.getElementById('"+formu+"').cod_uf"+destino+".value      = document.getElementById('cidade_estado_sel').value");
	    eval("if (document.getElementById('"+formu+"').cod_pais"+destino+"!=null)      document.getElementById('"+formu+"').cod_pais"+destino+".value      = '55'");
	    eval("if (document.getElementById('"+formu+"').pais"+destino+"!=null)          document.getElementById('"+formu+"').pais"+destino+".value      = 'BRASIL'");
     }
  }   
  catch(e) {}
}

function buscaBairro(event, objeto,id_conteiner,origem,duplicaMissao) {
    form = document.getElementById(objeto.form.id);
    objeto.value = objeto.value.replace(/'/g,'');
    script = "selecionaBairro('"+form.id+"','"+origem+"','"+duplicaMissao+"'); document.getElementById('"+id_conteiner+"').style.display='none'; document.getElementById('"+id_conteiner+"').innerHTML=''";
    if (event!=null && event!=undefined) {
        tecla = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
        var vStrcaract = new RegExp(/^[0-9a-zA-Z]+$/i);
        var vStrcaract = vStrcaract.test(String.fromCharCode(tecla));
        var vSetaUp = (event.keyCode==38);
        var vSetaDown = (event.keyCode==40);
        if (vSetaUp || vSetaDown) {
            if (document.getElementById(id_conteiner).style.display!='none' && trim(document.getElementById(id_conteiner).innerHTML)!='') {
                tipo = (vSetaUp?'up':'down'); 
                navegaObjetoBairro(id_conteiner, tipo);
            }
            return true;
        }
        if (event.keyCode==13) {
            if (document.getElementById(id_conteiner).style.display!='none' && trim(document.getElementById(id_conteiner).innerHTML)!='') {
                eval(script);
                return true;
            }
        }
        if (!(vStrcaract || tecla==8 || tecla==46)) return false;
    }
    if (typeof(exec)=='undefined') {
        exec = false;
    } else {
        window.clearTimeout(exec);
    }
    if (duplicaMissao==null || duplicaMissao==undefined) duplicaMissao='0';
    origem2 = (origem=='' ? '' : '_'+origem);
    if (document.getElementById('cod_pais'+origem2)!=null) {
        if (document.getElementById('cod_pais'+origem2).value!='55') return false;
    }
    
    exec = window.setTimeout(   function () {
                                  try{
                                    form = document.getElementById(objeto.form.id);
                                    eval('form.cod_bairro'+origem2+'.value = ""');
      defineCloseObject(objeto, id_conteiner);
                                    var minLength = 3;
                                    if (objeto.value.length < minLength){
                                      document.getElementById(id_conteiner).style.display='none';
                                      return false;
                                    }
                                    var str = objeto.value;
                                    var id_form = form.id;
                                    var uf = eval('form.uf'+origem2+'.value');
                                    var codcidade = eval('form.cod_cidade'+origem2+'.value');
                                    var cidade = eval('form.cidade'+origem2+'.value');
                                    form.processar.value     = 0;   
                                    var urlx = '?mode=ajax&mod=busca_endereco&p=bairros&uf='+uf+'&codcidade='+codcidade+'&cidade='+escape(cidade)+'&bairro='+str+'&id_conteiner='+id_conteiner+'&form='+id_form+'&origem='+origem+'&duplicaMissao='+duplicaMissao;
      retorno = simple_sjax_post(urlx, id_conteiner, form.id, true,'',false);
                                    if (str==objeto.value) {
                                        if(retorno.length > 0){
                                          document.getElementById(id_conteiner).innerHTML = retorno;
          document.getElementById(id_conteiner).onclick=function() {eval(script);};
                                        }else{
                                          document.getElementById(id_conteiner).innerHTML = "<div class=\"msgInline\">Nenhum Bairro encontrado!</div>";
                                        }
                                    }
                                    document.getElementById(id_conteiner).style.display='block';
                                    return true;
                                  }catch(erro){alert(erro);}
                                  return false;
                                },500);
}


function navegaObjetoBairro(id_conteiner, tipo) {
  var cliAtual = parseInt(document.getElementById('seq_bairro_busca_sel').value);
  cliOld = cliAtual;
  cliAtual = (tipo=='up'?cliAtual-1:cliAtual+1);
  if (cliAtual<=0) cliAtual = 1;
  if (document.getElementById('bairro_codbairro_'+cliAtual)==null) cliAtual = cliOld;
  preencheSelecaoBairro(cliAtual);

  //document.getElementById('tr_paciente_inline_'+cliAtual).className = 'selectLine2Hover';
  if (document.getElementById('tr_bairro_busca_'+(cliAtual-1))!=null) document.getElementById('tr_bairro_busca_'+(cliAtual-1)).className = 'selectLine2';
  if (document.getElementById('tr_bairro_busca_'+(cliAtual+1))!=null) document.getElementById('tr_bairro_busca_'+(cliAtual+1)).className = 'selectLine2';
}

function preencheSelecaoBairro(seq) {
  var seqTmp = (document.getElementById('seq_bairro_busca_sel').value);
  if (document.getElementById('tr_bairro_busca_'+seqTmp)!=null) document.getElementById('tr_bairro_busca_'+seqTmp).className='selectLine2';

  document.getElementById('seq_bairro_busca_sel').value=seq;

    if (document.getElementById('bairro_codcidade_sel')!=null)       document.getElementById('bairro_codcidade_sel').value        = document.getElementById('bairro_codcidade_'+seq).value;  
    if (document.getElementById('bairro_codbairro_sel')!=null)       document.getElementById('bairro_codbairro_sel').value        = document.getElementById('bairro_codbairro_'+seq).value;  
    if (document.getElementById('bairro_bairro_sel')!=null)          document.getElementById('bairro_bairro_sel').value           = document.getElementById('bairro_bairro_'+seq).innerHTML;
    if (document.getElementById('bairro_estado_'+seq).type=='hidden') {    
        if (document.getElementById('bairro_estado_sel')!=null)      document.getElementById('bairro_estado_sel').value           = document.getElementById('bairro_estado_'+seq).value;
    } else {
        if (document.getElementById('bairro_estado_sel')!=null)      document.getElementById('bairro_estado_sel').value           = document.getElementById('bairro_estado_'+seq).innerHTML;
    }      
    if (document.getElementById('bairro_cidade_'+seq).type=='hidden') {    
        if (document.getElementById('bairro_cidade_sel')!=null)      document.getElementById('bairro_cidade_sel').value           = document.getElementById('bairro_cidade_'+seq).value;
    } else {
        if (document.getElementById('bairro_cidade_sel')!=null)      document.getElementById('bairro_cidade_sel').value           = document.getElementById('bairro_cidade_'+seq).innerHTML;
    }      
      
  document.getElementById('tr_bairro_busca_'+seq).className='selectLine2Hover';
}

//Retorna o endereco selecionado na busca
function selecionaBairro(formu, destino,duplicaMissao)
{
  try {
     destino = (destino=='') ? '' : '_'+destino;
     eval("if (document.getElementById('"+formu+"').cod_bairro"+destino+"!=null) document.getElementById('"+formu+"').cod_bairro"+destino+".value   = document.getElementById('bairro_codbairro_sel').value");
     eval("if (document.getElementById('"+formu+"').bairro"+destino+"!=null)    document.getElementById('"+formu+"').bairro"+destino+".value      = document.getElementById('bairro_bairro_sel').value");
     eval("if (document.getElementById('"+formu+"').cod_cidade"+destino+"!=null) document.getElementById('"+formu+"').cod_cidade"+destino+".value   = document.getElementById('bairro_codcidade_sel').value");
     eval("if (document.getElementById('"+formu+"').cidade"+destino+"!=null)    document.getElementById('"+formu+"').cidade"+destino+".value      = document.getElementById('bairro_cidade_sel').value");
     eval("if (document.getElementById('"+formu+"').uf"+destino+"!=null)        document.getElementById('"+formu+"').uf"+destino+".value          = document.getElementById('bairro_estado_sel').value");
    eval("if (document.getElementById('"+formu+"').cod_uf"+destino+"!=null)     document.getElementById('"+formu+"').cod_uf"+destino+".value      = document.getElementById('bairro_estado_sel').value");
    eval("if (document.getElementById('"+formu+"').cod_pais"+destino+"!=null)   document.getElementById('"+formu+"').cod_pais"+destino+".value      = '55'");
    eval("if (document.getElementById('"+formu+"').pais"+destino+"!=null)       document.getElementById('"+formu+"').pais"+destino+".value      = 'BRASIL'");
     if (duplicaMissao=='1') {
        destino = destino.replace('ida','volta');
        destino = destino.replace('Ida','Volta');
        formu = formu.replace('ida','volta');
        formu = formu.replace('Ida','Volta');
        if (destino.match('orig')) {
          destino = destino.replace('orig','dest');
          formu = formu.replace('Origem','Destino');
        }
        else if (destino.match('dest')) {
          destino = destino.replace('dest','orig');
          formu = formu.replace('Destino','Origem');
        }
      eval("if (document.getElementById('"+formu+"').cod_bairro"+destino+"!=null) document.getElementById('"+formu+"').cod_bairro"+destino+".value   = document.getElementById('bairro_codbairro_sel').value");
      eval("if (document.getElementById('"+formu+"').bairro"+destino+"!=null)    document.getElementById('"+formu+"').bairro"+destino+".value      = document.getElementById('bairro_bairro_sel').value");
      eval("if (document.getElementById('"+formu+"').cod_cidade"+destino+"!=null) document.getElementById('"+formu+"').cod_cidade"+destino+".value   = document.getElementById('bairro_codcidade_sel').value");
      eval("if (document.getElementById('"+formu+"').cidade"+destino+"!=null)    document.getElementById('"+formu+"').cidade"+destino+".value      = document.getElementById('bairro_cidade_sel').value");
      eval("if (document.getElementById('"+formu+"').uf"+destino+"!=null)        document.getElementById('"+formu+"').uf"+destino+".value          = document.getElementById('bairro_estado_sel').value");
      eval("if (document.getElementById('"+formu+"').cod_uf"+destino+"!=null)        document.getElementById('"+formu+"').cod_uf"+destino+".value      = document.getElementById('bairro_estado_sel').value");
      eval("if (document.getElementById('"+formu+"').cod_pais"+destino+"!=null)      document.getElementById('"+formu+"').cod_pais"+destino+".value      = '55'");
      eval("if (document.getElementById('"+formu+"').pais"+destino+"!=null)          document.getElementById('"+formu+"').pais"+destino+".value      = 'BRASIL'");
     }
  }   
  catch(e) {}
}


function buscarCepJS(cep,compl) {
  var origem = (compl.match('orig')?'origem':'destino');
  var campos = {
    local: document.getElementById(origem+'_'+compl),
    acomodacao: document.getElementById('tipo_'+compl),
    endereco: document.getElementById('endereco_'+compl),
    numero: document.getElementById('numero_'+compl),
    complemento: document.getElementById('compl_'+compl),
    cep: document.getElementById('cep_'+compl),
    cod_bairro: document.getElementById('cod_bairro_'+compl),
    bairro: document.getElementById('bairro_'+compl),
    cod_cidade: document.getElementById('cod_cidade_'+compl),
    cidade: document.getElementById('cidade_'+compl),
    cod_uf: document.getElementById('cod_uf_'+compl),
    uf: document.getElementById('uf_'+compl),
    cod_pais: document.getElementById('cod_pais_'+compl),
    pais: document.getElementById('pais_'+compl),
    pagina: document.getElementById('pagina_'+compl),
    coordenada: document.getElementById('coordenada_'+compl)
  };
  try {
    var form = campos.local.form;
      
    form.processar.value = 1;
    
    var cep_trat = cep.substr(0,5) +''+ cep.substr(6,3);
    var link = '?mode=ajax&mod=busca_endereco&p=cep&tipo=js&cep='+cep_trat;

    var ret = simple_sjax_post(link,form.id,form.id);
    var arrVal = ret.split('|');
    
    if (campos.local!=null)       campos.local.value        = 'Residência';
    if (campos.acomodacao!=null)  campos.acomodacao.value   = 'Residência';
    if (campos.endereco!=null)    campos.endereco.value     = arrVal[2];
    if (campos.numero!=null)      campos.numero.value       = arrVal[3];
    if (campos.complemento!=null) campos.complemento.value  = arrVal[4];
    if (campos.cep!=null)         campos.cep.value          = arrVal[5].substr(0,5) +'-'+ arrVal[5].substr(5,3);
    if (campos.cod_bairro!=null)  campos.cod_bairro.value   = arrVal[6];
    if (campos.bairro!=null)      campos.bairro.value       = arrVal[7];
    if (campos.cod_cidade!=null)  campos.cod_cidade.value   = arrVal[8];
    if (campos.cidade!=null)      campos.cidade.value       = arrVal[9];
    if (campos.cod_uf!=null)      campos.cod_uf.value       = arrVal[10];
    if (campos.uf!=null)          campos.uf.value           = arrVal[11];
    if (campos.cod_pais!=null)    campos.cod_pais.value     = arrVal[12];
    if (campos.pais!=null)        campos.pais.value         = arrVal[13];
    if (campos.pagina!=null)      campos.pagina.value       = arrVal[14];
    if (campos.coordenada!=null)  campos.coordenada.value   = arrVal[15];
    
  } catch(erro) {
    loading_start("error",erro,true,true);
  } 
}

//Dados de logradouro
function buscarCep(cep, origem, form, duplicaMissao, exibePais, tabIndex, obrigatorio, cepFlag) {
  
  var retorno = false;
  var label = "";
  if (duplicaMissao==null || duplicaMissao==undefined) duplicaMissao = false;
  if (exibePais==null || exibePais==undefined) exibePais = false;
  if (obrigatorio==null || obrigatorio==undefined) obrigatorio = false;
  if (tabIndex==null || tabIndex==undefined) tabIndex = 1;
  if (cepFlag==null || cepFlag==undefined) cepFlag = false;
  if (cep == "") {
     return false;
  }
  
  //Retira o separador '-' do cep passado.
  cep_trat = cep.substr(0,5) +''+ cep.substr(6,3);
  
  var link = '?mode=ajax&mod=busca_endereco&p=cep&duplicaMissao='+duplicaMissao+'&exibePais='+exibePais+'&tabindex='+tabIndex+'&obrigatorio='+obrigatorio;
  
  if (document.getElementById(form).cep_flag!=null) {
    document.getElementById(form).cep_flag.value = 1;
  } else {
    if (cepFlag==true) {
      link += '&cep_flag=1';
    }
  }
  document.getElementById(form).cep.value = cep_trat;
  document.getElementById(form).processar.value = 1;
  try {              
    retorno = simple_sjax_post(link, origem, form, true,'',false);
      if(retorno == false) {
        alert('Não existe endereço para este CEP');
      } else {
        //Imprime na Tela
        if (is_ie()) {
          innerHTML_ie(origem,retorno);
        } else {
          label = document.getElementById(origem);
          label.innerHTML = retorno; 
        }
      }
  }
  catch(e) { alert(e); }
  document.getElementById(form).cep.value = cep;
  
}

function getEndereco(id_area, cep, compl) {
  var campos = {
    local: document.getElementById('desc_'+compl),
    acomodacao: document.getElementById('tipo_'+compl),
    endereco: document.getElementById('endereco_'+compl),
    numero: document.getElementById('numero_'+compl),
    complemento: document.getElementById('compl_'+compl),
    cep: document.getElementById('cep_'+compl),
    cod_bairro: document.getElementById('cod_bairro_'+compl),
    bairro: document.getElementById('bairro_'+compl),
    cod_cidade: document.getElementById('cod_cidade_'+compl),
    cidade: document.getElementById('cidade_'+compl),
    cod_uf: document.getElementById('cod_uf_'+compl),
    uf: document.getElementById('uf_'+compl),
    cod_pais: document.getElementById('cod_pais_'+compl),
    pais: document.getElementById('pais_'+compl),
    pagina: document.getElementById('pagina_'+compl),
    coordenada: document.getElementById('coordenada_'+compl)
  };
  
  var form = campos.local.form;
  
  form.processar.value = 1;
  
  var link = '?mode=ajax&mod=busca_endereco&p=cep&tipo=js&id_area='+id_area+'&cep='+cep;  
  var ret = simple_sjax_post(link,form.id,form.id);
  var arrVal = ret.split('|');
  
  if (campos.local!=null)       campos.local.value        = arrVal[0];
  if (campos.acomodacao!=null)  campos.acomodacao.value   = arrVal[1];
  if (campos.endereco!=null)    campos.endereco.value     = arrVal[2];
  if (campos.numero!=null)      campos.numero.value       = arrVal[3];
  if (campos.complemento!=null) campos.complemento.value  = arrVal[4];
  if (campos.cep!=null)         campos.cep.value          = arrVal[5];
  if (campos.cod_bairro!=null)  campos.cod_bairro.value   = arrVal[6];
  if (campos.bairro!=null)      campos.bairro.value       = arrVal[7];
  if (campos.cod_cidade!=null)  campos.cod_cidade.value   = arrVal[8];
  if (campos.cidade!=null)      campos.cidade.value       = arrVal[9];
  if (campos.cod_uf!=null)      campos.cod_uf.value       = arrVal[10];
  if (campos.uf!=null)          campos.uf.value           = arrVal[11];
  if (campos.cod_pais!=null)    campos.cod_pais.value     = arrVal[12];
  if (campos.pais!=null)        campos.pais.value         = arrVal[13];
  if (campos.pagina!=null)      campos.pagina.value       = arrVal[14];
  if (campos.coordenada!=null)  campos.coordenada.value   = arrVal[15];
  
}

