
var req;
var conteudo;
function carregar(strURL,tgDiv,dados) 
{ 
    req = null; 

	conteudo=document.getElementById(tgDiv)
    conteudo.innerHTML='<div class="carregando" align="center"><h2>Só um momento, os dados estão sendo carregados...</h2></div><BR>'
	
	strUrl = strURL+"&time="+horaatual();
	
	
    // Procura por um objeto nativo (Mozilla/Safari) 


	if (window.XMLHttpRequest) { 
        req = new XMLHttpRequest(); 
        req.onreadystatechange = processReqChange; 
        req.open("POST",strURL, true); 
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
		req.setRequestHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
        req.setRequestHeader('Cache-Control', 'post-check=0, pre-check=0');
        req.setRequestHeader('Pragma', 'no-cache');
		req.setRequestHeader("encoding", "text/html; charset=iso-8859-1");
        req.send(dados); 
    
	// Procura por uma versao ActiveX (IE) 
    
	} else if (window.ActiveXObject) { 
        req = new ActiveXObject("Microsoft.XMLHTTP"); 
        if (req) { 
            req.onreadystatechange = processReqChange; 
            req.open("POST", strURL, true); 
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
			req.setRequestHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
            req.setRequestHeader('Cache-Control', 'post-check=0, pre-check=0');
            req.setRequestHeader('Pragma', 'no-cache');
			req.setRequestHeader("encoding", "text/html; charset=iso-8859-1");
            req.send(dados); 
        } 
    } 
} 

function processReqChange() 
{ 
    // apenas quando o estado for "completado" 
    if (req.readyState == 4) { 
        // apenas se o servidor retornar "OK" 
        if (req.status == 200) { 
            // procura pela div id="atualiza" e insere o conteudo 
            // retornado nela, como texto HTML 
			var html = req.responseText; 
			//var html = html.replace(/\+/g," ");
			//conteudo.innerHTML = unescape(html);
			conteudo.innerHTML = html;
        } else { 
            alert("Os dados não foram carregado corretamente, \npor favor verifique se os dados estão corretos. \nErro: " + req.statusText); 
        } 
    } 
} 

function montadados(formulario,strurl,strdiv)
{
		
	var f = document.getElementById(formulario);
	var stringdeenvio = "";
			
	for (var i = 0; i < f.elements.length; i++) 
	{    	
		if (f.elements[i].type == "text" || f.elements[i].type == "select-one") 
		{
        stringdeenvio+=f.elements[i].name + "=" + url_encode(f.elements[i].value)+"&";
    	}
		
	}		
	
	carregar(strurl,strdiv, stringdeenvio);

}

function limparDIV(alvo){
	var div = document.getElementById(alvo);
	div.innerHTML = "";
}

function horaatual(){
	var now = new Date();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds()
	var strHora = hours+":"+minutes+":"+seconds;
	return strHora;
}


// url_encode version 1.0  
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;  
}  

// 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;  
} 