	function newXMLHttpRequest() {
	
		var xmlreq = false;
		// Create XMLHttpRequest object in non-Microsoft browsers
		if (window.XMLHttpRequest) {
			xmlreq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try {
				// Try to create XMLHttpRequest in later versions of Internet Explorer
				xmlreq = new ActiveXObject("Msxml2.XMLHTTP");      
			} catch (e1) {
				// Failed to create required ActiveXObject      
				try {
					// Try version supported by older versions of Internet Explorer      
					xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e2) {
					// Unable to create an XMLHttpRequest by any means
					xmlreq = false;
				}
			}
		}
		return xmlreq;
	}

 function getReadyStateHandler(xmlreq, responseXmlHandler) {

   // Return an anonymous function that listens to the XMLHttpRequest instance
   return function () {
     // If the request's status is "complete"
     if (xmlreq.readyState == 4) {       
       // Check that we received a successful response from the server
       if (xmlreq.status == 200) {
         // Pass the XML payload of the response to the handler function.
         responseXmlHandler(xmlreq.responseXML);
       } else {
         // An HTTP problem has occurred
         alert("HTTP error "+ xmlreq.status +": "+ xmlreq.statusText);
       }
     }
   }
 }

function ajax(url, bloco, metodo){
	//Exibe "Carregando..."
	bloco = document.getElementById(bloco);
	bloco.innerHTML="<div><img src='./imagem/carregando.gif' border='0'/><span class='carregando'>Carregando...</span></div>";
	
	var xmlhttp = newXMLHttpRequest();
	//Abre a conexão
	xmlhttp.open(metodo,url,true);

	//Função para tratamento do retorno
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status == 200){
			//Mostra o HTML recebido
			retorno=unescape(xmlhttp.responseText.replace(/\+/g," "));
			bloco.innerHTML=retorno;
		}
	}
	//Executa
	xmlhttp.send(null);
}

fila  = [];
ifila = 0;
function ajaxF(url,id,metodo) {
    fila[fila.length] = [id,url];
    if((ifila + 1) == fila.length) {
		//Exibe "Carregando..."
		id = document.getElementById(id);
		id.innerHTML="<span class='carregando'><img src='./imagem/carregando.gif' border='0'/>Carregando...</span>";
        ajaxRun(metodo);
    }
}
function ajaxRun(metodo) {
	var id;
	var xmlhttp = newXMLHttpRequest();
    //Abre a conexão
    xmlhttp.open(metodo,fila[ifila][1],true);
    //Função para tratamento do retorno
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            //Mostra o HTML recebido
            retorno = xmlhttp.responseText;
            retorno = retorno.replace(/\+/g,"");
            retorno = unescape(retorno);
            document.getElementById(fila[ifila][0]).innerHTML = retorno;

			//extraiScript(retorno);
			//teste se tem algum javascript retornado e executa
			/*
			var conteudo = document.getElementById(fila[ifila][0]);
			var scripts  = conteudo.getElementsByTagName("script");
			if (scripts.length > 0){
				alert(scripts.length);
				for(i = 0; i < scripts.length; i++)	{
					s = scripts[i].innerHTML;
					eval(s);
				}
			}
			*/

            //Roda o próximo
            ifila++;
            if(ifila < fila.length) {
				//Exibe "Carregando..."
				id = document.getElementById(fila[ifila][0]);
				id.innerHTML="<span class='carregando'><img src='./imagem/carregando.gif' border='0'/>Carregando...</span>";
                setTimeout("ajaxRun()",20);
            }
        }
    }
    //Executa
    xmlhttp.send(null);
}

	function sjax(url, bloco, metodo){
		//Exibe "Carregando..."
		bloco = document.getElementById(bloco);
		bloco.innerHTML="<span class='carregando'><img src='./imagem/carregando.gif' border='0'/>Carregando...</span>";
		
		var xmlhttp = newXMLHttpRequest();
		//Abre a conexão
		xmlhttp.open(metodo,url,false);

		//Função para tratamento do retorno
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4 && xmlhttp.status == 200){
				//Mostra o HTML recebido
				retorno=unescape(xmlhttp.responseText.replace(/\+/g," "));
				bloco.innerHTML=retorno;
			}
		}
		//Executa
		xmlhttp.send(null);
	}
	
	function envia_form(url, bloco, str){
		//Exibe "Carregando..."
		bloco = document.getElementById(bloco);
		bloco.innerHTML="<span class='carregando'><img src='./imagem/carregando.gif' border='0'/>Carregando...</span>";
		
		var xmlhttp = newXMLHttpRequest();
		//Abre a conexão
		xmlhttp.open("POST",url,true);	
	
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		//Função para tratamento do retorno
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4 && xmlhttp.status == 200){
				//Mostra o HTML recebido
				retorno=unescape(xmlhttp.responseText.replace(/\+/g," "));
				bloco.innerHTML=retorno;
			}
		}
		//Executa
		xmlhttp.send(str);			
	}

function extraiScript(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);
            /* Alterei pois com o eval não executava funções.
            ***********************/
            novo = document.createElement("script")
            novo.text = codigo;
            document.body.appendChild(novo);
        }
    }
} 
