	function Ajax() 
	{
	  try {
	    return new ActiveXObject("Microsoft.XMLHTTP");
	  } catch(e) {
	    try {
	      return new ActiveXObject("Msxml2.XMLHTTP");
	    } catch(ex) {
	      try {
	        return new XMLHttpRequest();
	      } catch(exc) {
	        return false;
	      }
	    }
	  }
	}
	
	function ContaImpressao( id_usuario, id_veiculacao )
	{
		
		var ajax = new Ajax();
		ajax.open("POST","conta_impressao.php",true);
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.onreadystatechange = function() 
		{
		
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					var returnText = ajax.responseText.replace(/\+/g," ");

				} else {
					<!-- ajax.statusText; -->
				}
			}
		}
		
	
		ajax.send( "id_usuario=" + id_usuario + "&id_veiculacao=" + id_veiculacao );
		delete ajax;
	}
	
	function alterar_status(status,id,tabela,id_tabela,campo)
	{
	
		var ajax = new Ajax();
		ajax.open("POST","acoes.php",true);
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.onreadystatechange = function() 
		{
		
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					var returnText = ajax.responseText.replace(/\+/g," ");
					document.getElementById("ativo" + id).innerHTML = unescape(returnText);
				} else {
					<!-- ajax.statusText; -->

				}
			}
		}
		
	
		ajax.send("id=" + id + "&status=" + status + "&tabela=" + tabela + "&campo=" + campo + "&id_tabela=" + id_tabela);
		delete ajax;
	}
	
	function BuscaEmpresa( valor )
	{
	
		var ajax = new Ajax();
		ajax.open("POST","busca_empresa.php",true);
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.onreadystatechange = function() 
		{
		
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					var returnText = ajax.responseText.replace(/\+/g," ");
					document.getElementById("RertonoEmpresa").innerHTML = unescape(returnText);
				} else {
					<!-- ajax.statusText; -->
				}

			}
		}
		
		ajax.send("valor=" + valor );
		delete ajax;
	}
	
	function SelecionaEmpresa( IdEmpresa )
	{
		var ajax = new Ajax();
		ajax.open("POST","dados_empresa.php",true);
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.onreadystatechange = function() 
		{
		
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					var returnText = ajax.responseText.replace(/\+/g," ");
					document.getElementById("DadosEmpresa").innerHTML = unescape(returnText);
					document.getElementById("RertonoEmpresa").innerHTML = '';
				} else {
					<!-- ajax.statusText; -->
				}

			}
		}
		
		ajax.send("id_empresa=" + IdEmpresa);
		
		delete ajax;	
	}
	
	
	function ContadorCesta()
	{
	
		var ajax = new Ajax();
		ajax.open("POST","cesta.php",true);
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		ajax.onreadystatechange = function() 
		{
		
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					var returnText = ajax.responseText.replace(/\+/g," ");
					document.getElementById("ContadorCesta").innerHTML = unescape(returnText);
				} else {
					<!-- ajax.statusText; -->
				}
			}
		}
		
		ajax.send("parametro=contador");
		delete ajax;
	}
	
	function VerificaCadastro( cnpj, id_cliente )
	{
		var ajax = new Ajax();
		ajax.open("POST","verifica_cadastro.php",true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
		ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		ajax.setRequestHeader("Pragma", "no-cache");
		
		ajax.onreadystatechange = function() 
		{
		
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					
					var returnText = ajax.responseText.replace(/\+/g," ");
					if( cnpj != '' ){
						if( returnText.indexOf('Libera') == 90 || returnText.indexOf('Libera') == 0){
							document.getElementById('some').style.display='';'';
							//document.getElementById("alerta").innerHTML = '';
							document.getElementById('alerta').style.display='none';'n';							
						}else{
							document.getElementById("alerta").innerHTML = unescape(returnText);
							document.getElementById('some').style.display = 'none';'n';	
						}	
					}	
				} else {
					<!-- ajax.statusText; -->
				}
			}
		}
	
		ajax.send( "cnpj=" + cnpj + "&id_cliente=" + id_cliente );
		delete ajax;	    	
	}
	
	function VerificaPeca( arquivo, id_peca )
	{
		var ajax = new Ajax();
		ajax.open("POST","verifica_peca.php",true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
		ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		ajax.setRequestHeader("Pragma", "no-cache");
		
		ajax.onreadystatechange = function() 
		{
		
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					
					var returnText = ajax.responseText.replace(/\+/g," ");
					if( arquivo != '' ){
						if( returnText.indexOf('Libera') == 90 || returnText.indexOf('Libera') == 0){
							document.getElementById('some').style.display='';'';
							//document.getElementById("alerta").innerHTML = '';
							document.getElementById('alerta').style.display='none';'n';							
						}else{
							document.getElementById("alerta").innerHTML = unescape(returnText);
							document.getElementById('some').style.display = 'none';'n';	
						}	
					}	
				} else {
					<!-- ajax.statusText; -->
				}
			}
		}
	
		ajax.send( "arquivo=" + arquivo + "&id_peca=" + id_peca );
		delete ajax;	    	
	}	
function xmlHTTPRequestObject() 
 {  
    var obj = false; 
    var objectIDs = new Array( 
        "Microsoft.XMLHTTP", 
        "Msxml2.XMLHTTP", 
        "MSXML2.XMLHTTP.3.0", 
        "MSXML2.XMLHTTP.4.0" 
    ); 
    var success = false; 

    for (i=0; !success && i < objectIDs.length; i++) { 
        try { 
            obj = new ActiveXObject(objectIDs[i]); 
            success = true; 
        } catch (e) { obj = false; } 
    } 

    if (!obj) 
        obj = new XMLHttpRequest(); 

    return obj; 
} 
    
    String.prototype.trim = function()
{
return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

    
  function manda(url,id) 
   { 
    layered = id ; 
     xmlhttp = xmlHTTPRequestObject(); 
    lay = document.getElementById('carrega'); 
    if(lay) 
     { 
     lay.style.display='none'; 
     } 
     else 
     { 
	 links = 'admin/imagens/';
     lay = document.createElement('div'); 
     lay.id = 'carrega'; 
     lay.innerHTML = '<div style="margin-top:20px;margin-left:20px;"><img src="'+links+'/carregando.gif"></div>'; 
     lay.style.border = 'thin solid #000000'; 
     lay.style.top = '0px'; 
     lay.style.right = '0px'; 
     lay.style.backgroundColor = '#FFFFFF'; 
     lay.style.color = '#000000'; 
     lay.style.width = '100px'; 
	 lay.style.height = '100px';
		
	 
     lay.style.filter = 'alpha(opacity=20); -moz-opacity:0.2"'; 
     document.body.insertBefore(lay,document.body.firstChild); 
     } 
     lay.style.display='block'; 
      
      xmlhttp.open('GET',url,true);  
     xmlhttp.onreadystatechange = processa;  
     xmlhttp.send(null);  
     
  }  
  
  
  
   function manda2(url) 
   { 
   id = 'carrega';
    layered = id ; 
     xmlhttp = xmlHTTPRequestObject(); 
    lay = document.getElementById('carrega'); 
    if(lay) 
     { 
     lay.style.display='none'; 
     } 
     else 
     { 
	 links = 'imagens/';
     lay = document.createElement('div'); 
     lay.id = 'carrega'; 
     lay.innerHTML = '<div style="position:absolute; left:50%; top:50%; width:31px; height:31px; margin-left: -15px; margin-top:-15px; border:thin solid #CCC; padding:50px; background:#fff; z-index:99999;"><img src="'+links+'/carregando.gif"></div>'; 
     lay.style.border = 'none'; 
     lay.style.top = '0px'; 
     lay.style.right = '0px'; 
     lay.style.backgroundColor = '#FFFFFF'; 
     lay.style.color = '#000000'; 
     lay.style.width = '0px'; 
	 lay.style.height = '0px';

	 
     lay.style.filter = 'alpha(opacity=80); -moz-opacity:0.8"'; 
     document.body.insertBefore(lay,document.body.firstChild); 
     } 
     lay.style.display='block'; 
      xmlhttp.open('GET',url,true);  
     xmlhttp.onreadystatechange = testecall ;  
     xmlhttp.send(null);       
  }  
  

   function gravaentrega(url,idz) 
   { 
   id = 'carrega';
    layered = idz ; 
     xmlhttp = xmlHTTPRequestObject(); 
    lay = document.getElementById('carrega'); 
    if(lay) 
     { 
     lay.style.display='none'; 
     } 
     else 
     { 
	 links = 'imagens/';
     lay = document.createElement('div'); 
     lay.id = 'carrega'; 
     lay.innerHTML = '<div style="margin-top:20px;margin-left:20px;"><img src="'+links+'/carregando.gif"></div>'; 
     lay.style.border = 'thin solid #000000'; 
     lay.style.top = '0px'; 
     lay.style.right = '0px'; 
     lay.style.backgroundColor = '#FFFFFF'; 
     lay.style.color = '#000000'; 
     lay.style.width = '100px'; 
	 lay.style.height = '100px';

	 
     lay.style.filter = 'alpha(opacity=80); -moz-opacity:0.8"'; 
     document.body.insertBefore(lay,document.body.firstChild); 
     } 
     lay.style.display='block';       
      xmlhttp.open('GET',url,true);  
     xmlhttp.onreadystatechange = recallentrega ;  
     xmlhttp.send(null);       
  }  
     
    
    
  function testecall()
 {
  if (xmlhttp.readyState == 4) 
     {  
      if (xmlhttp.status == 200) 
        {        
		
		var querystring = (unescape(xmlhttp.responseText));

		querystring = querystring.split("&");


campos =  new Array();
var loop = 0;

while (loop < querystring.length)
 {
   var inter = querystring[loop].split("=");
   var inter2 = new String(inter[1]);
   var inter3 = inter[0];
   querystring[loop] = inter2;
   inter2 = inter2.replace("+"," ");
   inter2 = inter2.replace("+"," ");
   inter2 = inter2.replace("+"," ");
   inter2 = inter2.replace("+"," ");
   inter2 = inter2.replace("+"," ");
   inter2 = inter2.replace("+"," ");
   campos[inter3] = inter2;
   if (campos['resultado'] == 0 )
    {
	 document.getElementById('ColValue[endereco]').value = 'Inválido';
	 document.getElementById('ColValue[bairro]').value = '--';
	 document.getElementById('ColValue[cidade]').value = '--';
	 document.getElementById('ColValue[estado]').value = '--';	
	 document.getElementById('ColValue[cep]').style.backgroundColor = '#FFA6A6'; 
     document.getElementById('ColValue[cep]').style.color = '#FFFFFF';
	}
	else
	{
	 document.getElementById('ColValue[numero]').focus();		
	 document.getElementById('ColValue[endereco]').value = campos['tipo_logradouro'] + ' ' + campos['logradouro'];
	 document.getElementById('ColValue[bairro]').value = campos['bairro'];
	 document.getElementById('ColValue[cidade]').value = campos['cidade'];
	 document.getElementById('ColValue[estado]').value = campos['uf'];
	 document.getElementById('ColValue[cep]').style.backgroundColor = '#FFFFFF'; 
     document.getElementById('ColValue[cep]').style.color = '#000000';	
	
	}
   loop = loop + 1;
 }
   lay.style.display='none'; 
        } 
       else 
       {  
        alert("Problemas ao carregar o arquivo.");  
        lay.style.display='none'; 
       }  
       
    }  
  
}  



function recallentrega() {  

    if (xmlhttp.readyState == 4) 
     {  
      if (xmlhttp.status == 200) 
        {  
        document.getElementById(layered).innerHTML = (xmlhttp.responseText); 
       // manda('paginas_ajax/atualizaendereco.php?acao=refresh','end_entrega');
        document.location = '?acao=detalhes';
         lay.style.display='none'; 
        } 
       else 
       {  
        alert("Problemas ao carregar o arquivo.");  
        lay.style.display='none'; 
       }  
       
    }  
  }  




function processa() {  

    if (xmlhttp.readyState == 4) 
     {  
      if (xmlhttp.status == 200) 
        {  
         
        document.getElementById(layered).innerHTML = url_decode(xmlhttp.responseText);  
         lay.style.display='none'; 
        } 
       else 
       {  
        alert("Problemas ao carregar o arquivo.");  
        lay.style.display='none'; 
       }  
       
    }  
  }  

// url_decode version 1.0  
function url_decode(str) {  
    var n, strCode, strDecode = "";  

    for (n = 0; n < str.length; n++) {  
        if (str.charAt(n) == "%") {  
            strCode = str.charAt(n + 1) + str.charAt(n + 2);  
            strDecode += String.fromCharCode(parseInt(strCode, 16));  
            n += 2;  
        } else {  
            strDecode += str.charAt(n);  
        }  
    }  
    return strDecode;  
}  


		
