// 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;  
}  

// CRIA O OBJECT PARA HTTP REQUEST
var xmlhttp = createRequestObject();

//Fila de conexões
fila=[]
ifila=0
	
// FUNÇÃO PARA CRIAR O OBJETO HTTP REQUEST
function createRequestObject() {
    try{
    	http = new XMLHttpRequest();
	}catch(ee){
	    try{
	        http = new ActiveXObject("Msxml2.XMLHTTP");
	    }catch(e){
	        try{
	            http = new ActiveXObject("Microsoft.XMLHTTP");
	        }catch(E){
	            http = false;
	        }
	    }
	}
	return http;
}

function makePOSTRequest(url, parameters, id) {
	xmlhttp.open('POST', url, true);
	
	xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //Mostra o HTML recebido
            retorno=unescape(xmlhttp.responseText.replace(/\+/g," "))
            document.getElementById(id).innerHTML=retorno
        }
    }

	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", parameters.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(parameters);
}


// FUNÇÃO PARA PEGAR O CONTEUDO
function getConteudo(url,id) {
	//Carregando...
	document.getElementById(id).innerHTML="<span class='carregando'>"+"Carregando...</span>"
	
	//Adiciona à fila
	fila[fila.length]=[id,url]
	
	//Se não há conexões pendentes, executa
	if((ifila+1)==fila.length)ajaxRun()
}

//Executa a próxima conexão da fila
function ajaxRun(){
    //Abre a conexão
    xmlhttp.open("POST",fila[ifila][1],true);
	
	//Função para tratamento do retorno
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //Mostra o HTML recebido
            retorno=unescape(xmlhttp.responseText.replace(/\+/g," "))
            document.getElementById(fila[ifila][0]).innerHTML=retorno
            
			//Roda o próximo
            ifila++
            if(ifila<fila.length)setTimeout("ajaxRun()",20)
        }
    }
    //Executa
    xmlhttp.send(null)
}

