﻿//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,idform){ 
    //Carregando...
    document.getElementById(id).innerHTML="<div style='background:url(/..imagens/load.gif) center fixed no-repeat;width:100%;height:100px;' class='carregando'>carregando...</div>";

    // Enviar dados do formulario
    if(idform != undefined){
        var form_string = camposForm(idform);
        ajaxRunForm(id,url,form_string);
    }else{
        //Adiciona à fila
        fila[fila.length]=[id,url]
        //Se não há conexões pendentes, executa 
        if((ifila+1)==fila.length)ajaxRun()
    }
}

function camposForm(oForm){
        var aParams = new Array();
        for (var i=0; i < oForm.length; i++) {
            var sParam = oForm[i].name; 
            sParam += "=";
            sParam += oForm[i].value;
            aParams.push(sParam);
        }
        return aParams.join("&");
}

//Executa a próxima conexão da fila 
function ajaxRun(){
    //Abre a conexão
    xmlhttp.open("GET",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)
}

function ajaxRunForm(id,url,form_string){
    //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){
            //Mostra o HTML recebido
            retorno=unescape(xmlhttp.responseText.replace(/\+/g," "))
            document.getElementById(id).innerHTML=retorno 
        }
    }
    //Executa
	
    xmlhttp.send(form_string)
}


