// Tenta criar o objeto xmlHTTP
try {
    xmlhttp = new XMLHttpRequest();
} catch(ee) {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(E) {
            xmlhttp = false;
        }
    }
}

//Fila de conex??es
fila = [];
ifila = 0;

// Carrega via XMLHTTP a url recebida e coloca seu valor no objeto com o id recebido
function ajaxHTML(id, url, tipo) {
	if (tipo == undefined) {
		tipo = 2;
	}

	// Trata a URL
	if (url.indexOf("?") == -1) { // Ainda n??o tem par??metros
		url = url + '?id=' + CriaID();
	} else {
		url = url + '&id=' + CriaID();
	}

    // 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("GET", fila[ifila][1], true);

	// Cabe??alhos para evitar cache
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	xmlhttp.setRequestHeader("Pragma", "no-cache");

    // Fun??ao para tratamento do retorno
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
            // Mostra o HTML recebido
            retorno = unescape(xmlhttp.responseText.replace(/\+/g," "));
            document.getElementById(fila[ifila][0]).innerHTML = retorno;
            // Executa o script da página, caso tenha
            executaScript(retorno);
	        // Roda o pr??oximo, independente se deu erro ou nao
	        ifila++;
	        if (ifila < fila.length) {
	            setTimeout("ajaxRun()", 20);
	        }
        } else if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
            document.getElementById(fila[ifila][0]).innerHTML = "Ocorreu um erro durante o processamento!";
	        // Roda o pr??oximo, independente se deu erro ou nao
	        ifila++;
	        if (ifila < fila.length) {
	            setTimeout("ajaxRun()", 20);
	        }
        }
    }
    // Executa
    xmlhttp.send(null);
}

function CriaID() {
	// Gera o ID
	var data = new Date();
	return data.getFullYear() + "" + data.getMonth() + "" + data.getDate() + "" + data.getHours() + "" + data.getMinutes() + "" + data.getSeconds() + "" + data.getMilliseconds();
}


function executaScript(texto){
    // inicializa o inicio ><
    var ini = 0;

    // loop enquanto achar um script
    while (ini != -1){

        // procura uma tag de script
        ini = texto.indexOf('<script', ini);

        // se encontrar
        if (ini >= 0){
            // define o inicio para depois do fechamento dessa tag
            ini = texto.indexOf('>', ini) + 1;

            // procura o final do script
            var fim = texto.indexOf('</script>', ini);

            // extrai apenas o script
            codigo = texto.substring(ini,fim);

            // executa o script
            eval(codigo);
        }
    }
}