var req;
var layer="<div id='Layer1' style='position:absolute; width:300px; z-index:1; left: 320px; top: 300px;' class='boxz'><table width='100%' class='infobox'><tr><td class='strong' align='center'><BR>Aguarde por favor...&nbsp;&nbsp;&nbsp;<BR><BR></td></tr></table></div>";
var limpa_layer="<div id='Layer1' style='position:absolute; width:300px; z-index:1; left: 320px; top: 300px;'>&nbsp;</div>";


function Arredonda(valor,casas){
  var novo = Math.round(valor*Math.pow(10,casas))/Math.pow(10,casas);
  return(novo);
}

function mudar(x,cor){
x.style.backgroundColor=cor;
}

function isset(variavel)
{ try{
    if(variavel) return 1;
  }
  catch(e){
    return -1;
  }
}

//o parâmetro de passagem é a string / 
//essa função evita que de erro quando alguém digitar / na busca
function limpa(valor)
{ if(isset(valor))
  { 
    if(valor.length>0)
    {  while(valor.search("/") != -1)
         valor=valor.replace("/","\\subst\\"); //a string \subst\ será resubstituida por / na listagem
       while(valor.search("&") != -1)
         valor=valor.replace("&","\\com\\"); //a string \com\ será resubstituida por & na listagem
    
      valor = url_encode(valor);
    }
  }
  else valor="";
   
  return valor; 
}

function url_encode(str) {  
	var hex_chars = "0123456789ABCDEF";  
	var noEncode = /^([a-zA-Z0-9\_\-\.])$/;  
	var n, strCode, hex1, hex2, strEncode = "";  

	for(n = 0; n < str.length; n++) {  
		if (noEncode.test(str.charAt(n))) {  
			strEncode += str.charAt(n);  
		} else {  
			strCode = str.charCodeAt(n);  
			hex1 = hex_chars.charAt(Math.floor(strCode / 16));  
			hex2 = hex_chars.charAt(strCode % 16);  
			strEncode += "%" + (hex1 + hex2);  
		}  
	}  
	return strEncode;  
}

function addslashes(str) {
  str=str.replace(/\'/g,'\\\'');
  str=str.replace(/\"/g,'\\"');
  str=str.replace(/\\/g,'\\\\');
  str=str.replace(/\0/g,'\\0');
  return str;
}
function stripslashes(str) {
  str=str.replace(/\\'/g,'\'');
  str=str.replace(/\\"/g,'"');
  str=str.replace(/\\\\/g,'\\');
  str=str.replace(/\\0/g,'\0');
  return str;
}

function ajaxRead(file,destino){
  var xmlObj = null;
  if(window.XMLHttpRequest){
      xmlObj = new XMLHttpRequest();
  } else if(window.ActiveXObject){
      xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
      return;
  }

xmlObj.onreadystatechange = function(){   
 if(xmlObj.readyState == 1){
  //**** USAR PARA ARQUIVOS HTML *****
    //updateObj(destino,layer);
 }

 if(xmlObj.readyState == 4){
  if (xmlObj.status == 200) { //se nao ocorreu nenhum erro
   //**** USAR PARA ARQUIVOS HTML *****
   //updateObj(destino,limpa_layer);
   updateObj(destino, xmlObj.responseText);
  } 
  else {
            alert("Houve um problema ao obter os dados:\n" + xmlObj.statusText);
        }
}
}
xmlObj.open ('GET', file, true);
xmlObj.send ('');
}

function ajaxRead2(file,destino){
  var xmlObj = null;
  if(window.XMLHttpRequest){
      xmlObj = new XMLHttpRequest();
  } else if(window.ActiveXObject){
      xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
      return;
  }

xmlObj.onreadystatechange = function(){   
 if(xmlObj.readyState == 1){
  //**** USAR PARA ARQUIVOS HTML *****
    updateObj(destino,layer);
 }

 if(xmlObj.readyState == 4){
  if (xmlObj.status == 200) { //se nao ocorreu nenhum erro
   //**** USAR PARA ARQUIVOS HTML *****
   //updateObj(destino,limpa_layer);
   updateObj(destino, xmlObj.responseText);
  } 
  else {
            alert("Houve um problema ao obter os dados:\n" + xmlObj.statusText);
        }
}
}
xmlObj.open ('GET', file, true);
xmlObj.send ('');
}

function updateObj(obj, data){
 if(obj)	
   document.getElementById(obj).innerHTML = data;
}

/* TRIM para javascript */
function Trim(str){
	return str.replace(/^\s+|\s+$/g,"");
}

function buscaCep(cep,endereco,numero,bairro,cidade,uf,destino){
  var xmlObj = null;
  if(window.XMLHttpRequest){
      xmlObj = new XMLHttpRequest();
  } else if(window.ActiveXObject){
      xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
      return;
  }

xmlObj.onreadystatechange = function(){   
 if(xmlObj.readyState == 1){
  //**** USAR PARA ARQUIVOS HTML *****
    abre = "<img src='https://www.pagamentodigital.com.br/img/ajax-loader.gif' border='0'> Aguarde Localizando Endereço";
    if(document.getElementById(destino)){
	    document.getElementById(destino).style.display = '';
	    updateObj(destino,abre);
    }
 }

 if(xmlObj.readyState == 4){
  if (xmlObj.status == 200) { //se nao ocorreu nenhum erro
   //**** USAR PARA ARQUIVOS HTML *****
    if(document.getElementById(destino)){
	    document.getElementById(destino).style.display = 'none';
    }
	resposta = xmlObj.responseText.split("|");

	if(resposta[0].length>0 && resposta[2].value!= 'undefined'){
		document.getElementById(endereco).value=resposta[0];
		document.getElementById(bairro).value=resposta[1];
		document.getElementById(cidade).value=resposta[2];
		document.getElementById(uf).value=Trim(resposta[3]);		
		if(numero != ''){
			if(document.getElementById(numero)){
				document.getElementById(numero).focus();
			}
		}
	}else{
		document.getElementById(endereco).value='';
		document.getElementById(bairro).value='';
		//document.getElementById(cidade).value='';
		//document.getElementById(uf).value='';
		
		if(numero != ''){
			if(document.getElementById(numero)){

				document.getElementById(numero).value='';	
			}
		}	
		document.getElementById(endereco).focus();
	}
  } 
  else {
            alert("Houve um problema ao obter os dados:\n" + xmlObj.statusText);
        }
}
}
xmlObj.open ('GET', 'https://www.pagamentodigital.com.br/lib/busca_cep.php?cep='+cep, true);
xmlObj.send ('');
}

function ajaxRead3(file,destino){
  var xmlObj = null;
  if(window.XMLHttpRequest){
      xmlObj = new XMLHttpRequest();
  } else if(window.ActiveXObject){
      xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
      return;
  }

xmlObj.onreadystatechange = function(){   
 if(xmlObj.readyState == 1){
  //**** USAR PARA ARQUIVOS HTML *****
	msg_aguarde = "<img src='https://www.pagamentodigital.com.br/img/ajax-loader.gif' border='0'> Aguarde..";
    updateObj(destino,msg_aguarde);
 }

 if(xmlObj.readyState == 4){
  if (xmlObj.status == 200) { //se nao ocorreu nenhum erro
   //**** USAR PARA ARQUIVOS HTML *****
   //updateObj(destino,limpa_layer);
   updateObj(destino, xmlObj.responseText);
  
  } 
  else {
            alert("Houve um problema ao obter os dados:\n" + xmlObj.statusText);
        }
}
}
xmlObj.open ('GET', file, true);
xmlObj.send ('');
}

// Mostra ajuda
//ex.: onFocus=SetHelp('Informe o valor com os centavos.') // coloque isso no campo que quer que dispare o help
//function SetHelp(txt) {
// document.getElementById('lblStatus1').innerHTML = txt;
//}


//Formata valor do campo conforme vai digitando os valores
//ex.: onKeyDown='FormataValor("valor", 13, event)
//não utilizar direto o método onkeydown, utilizar o define EVENTO_CHAMADA
//Obs.: "valor" é o nome do campo, 13 o tamanho máximo permitido de carac. do campo e event é a tecla pressionada'
function FormataValor(campo,tammax,teclapres) {
	if (teclapres.keyCode){
		var tecla = teclapres.keyCode;
	} else {
		var tecla = teclapres.which;
	}
	
	vr = campo.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;
	
	if ( tecla == 8 || (tecla >= 48 && tecla <= 57 && tam < tammax) || (tecla >= 96 && tecla <= 105 && tam < tammax) ){
		
		if (tecla != 8 )
			tam = vr.length + 1 ;
		
		if (tecla == 8 )
			tam = tam - 1 ;
		
		campo.value = vr.substr( 0, tam - 2 ) + '.' + vr.substr( tam - 2, tam ) ; 
		
		if ( tam <= 2 ) 
			campo.value = vr ; 
		
		//limpa zeros a esquerda
		if(campo.value.substr(0,1)=='0' && campo.value.substr(0,2)!='0.' && campo.value.length > 1){
			campo.value = campo.value.substr(1,tam);
		}
	} else {
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
			teclapres.returnValue=false;
		} else {
			teclapres.preventDefault();
		}
		return false;
	}
}
 
 function Bloqueia_Caracteres(evnt){
 //Função permite digitação de números
 
    if (evnt.keyCode == 8 ) return true;
    if (evnt.keyCode == 110 ) return true;
 
 	if (navigator.appName.indexOf('Microsoft') != -1){
 		if ((evnt.keyCode < 48 || evnt.keyCode > 57) || (evnt.charCode < 96 || evnt.charCode > 105) ){
 			return false
 		}
 	}else{
 		if ((evnt.charCode < 48 || evnt.charCode > 57) || (evnt.charCode < 96 || evnt.charCode > 105) && evnt.keyCode == 0){
 			return false
 		}
 	}
 }

 function Ajusta_Cep(input, evnt){
 //Ajusta máscara de CEP e só permite digitação de números
 	if (input.value.length == 5){
 		if(clientNavigator == "IE"){
 			input.value += "-";
 		}else{
 			if(evnt.keyCode == 0){
 				input.value += "-";
 			}
 		}
 	}
 //Chama a função Bloqueia_Caracteres para só permitir a digitação de números
 	return Bloqueia_Caracteres(evnt);
 }

 
