
ar_end = new Array();
index_end = 0;
function registraEndereco(icep,cep,frame,rua,bai,id,cid,uf){
  ar_end[icep] = new Array();
  ar_end[icep][0] = cep;
  ar_end[icep][1] = frame;
  ar_end[icep][2] = rua;
  ar_end[icep][3] = bai;
  ar_end[icep][4] = id;
  ar_end[icep][5] = cid;
  ar_end[icep][6] = uf;
}
function buscaEndereco(icep){
  obj = document.getElementById(ar_end[icep][0]) ;
  if (obj.value){
      dv = ar_end[icep][0]+'_div_msg';
      document.getElementById(dv).innerHTML = '&nbsp;Aguarde... <a href="javascript:cancelaBuscaEndereco('+icep+')">cancelar</a>';
      document.getElementById(ar_end[icep][1]).src = "cep.php?yx&exe="+icep+"&cep=" + document.getElementById(ar_end[icep][0]).value ;
  }
}
function cancelaBuscaEndereco(icep){
    dv = ar_end[icep][0]+'_div_msg';
    document.getElementById(dv).innerHTML = '&nbsp;Descubra o seu CEP <a target=_blank href="http://www.correios.com.br/servicos/cep/default.cfm"><u>clicando aqui</u></a>!';
}
function erroEndereco(icep,erro){
  cancelaBuscaEndereco(icep) ;
  alert(erro);
  document.getElementById(ar_end[icep][0]).focus();
}
function buscaCEP(icep){
  document.getElementById(ar_end[icep][1]).src = "cep.php?x&exe="+icep+"&rua=" + document.getElementById(ar_end[icep][2]).value + "&cid=" + document.getElementById(ar_end[icep][4]).value + "&uf=" + document.getElementById(ar_end[icep][5]).value;
}
function atualizaEendereco(icep,cep,r,b,id,c,e){
  cancelaBuscaEndereco(icep) ; 
  if (c=="erro"){
     alert(r);
  } else {
     obj = document.getElementById(ar_end[icep][0]) ;
     if (obj) obj.value = cep;
     obj = document.getElementById(ar_end[icep][2]) ;
     if (obj) obj.value = r;
     obj = document.getElementById(ar_end[icep][3]) ;
     if (obj) obj.value = b;
     obj = document.getElementById(ar_end[icep][4]) ;
     if (obj) obj.value = id;
     obj = document.getElementById(ar_end[icep][5]) ;
     if (obj) obj.value = c;
     obj = document.getElementById(ar_end[icep][6]) ;
     if (obj) obj.value = e;
  }
  document.getElementById(ar_end[icep][1]).style.visibility = 'hidden';
}
function selecionaCEP(icep){
  document.getElementById(ar_end[icep][1]).style.visibility = 'visible';
}
function montaEndereco(cep,frame,rua,num,compl,bai,id,cid,uf,com_table){
    if (com_table==1){
       document.write('<table cellspacing=0 cellpadding=0 border=0>');
    }
    document.write('<tr><td>CEP');
    document.write('<iframe frameborder=0 style="position:aabsolute;visibility:aidden;border:1px solid gray;" width=500 height=120 name='+frame+'></iframe>');
    document.write('</td><td> <table cellspacing=0 cellpadding=0 border=0><tr><td><input size=8  type=text name='+cep+' value="78048640"> </td>');
    document.write('<td><input type=button value="Buscar" onclick="javascript:buscaEndereco(');
    document.write(index_end);
    document.write(');"> </td></tr></table> </td></tr>');
    document.write('<tr><td>Rua     </td><td> <input size=38 type=text name='+rua+'>* </td></tr>');
    document.write('<tr><td>Número  </td><td> <input size=8 type=text name='+num+'> </td></tr>');
    document.write('<tr><td>Complemento</td><td> <input size=38 type=text name='+compl+'> </td></tr>');
    document.write('<tr><td>Bairro  </td><td> <input size=38 type=text name='+bai+'> </td></tr>');
    document.write('<tr><td>Cidade  </td><td> <input size=38 type=text name='+cid+'><input type=hidden name='+id+'>* </td></tr>');
    document.write('<tr><td>Estado  </td><td> <table cellspacing=0 cellpadding=0 border=0><tr><td><input size=3  type=text name='+uf+'>*&nbsp; </td><td><input type=button value="Buscar" onclick="javascript:buscaCEP(');
    document.write(index_end);
    document.write(')"> *Campos obrigatórios</td></tr></table> </td></tr>');
    if (com_table==1){
       document.write('</table>');
    }
    //document.write('<iframe frameborder=0 style="position:absolute;visibility:visible;border:1px solid gray;" width=500 height=120 name='+frame+'></iframe>');
    registraEndereco(index_end,cep,frame,rua,bai,id,cid,uf);
    index_end ++;
}
