 var xmlhttp;
//Tenta criar o objeto xmlHTTP
try{
    xmlhttp = new XMLHttpRequest();
    xmlhttp.overrideMimeType("text/xml");
    //if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml');
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}


//Fila de conexões
var fila=[];
var ifila=0;

function getLocalRestrito(){
   return getContexto()+"/restrito/";
}


//Carrega via XMLHTTP a url recebida e coloca seu valor
//no objeto com o id recebido
function ajaxHTML(id,url){
   ajaxHTMLLoad(id,url,true);
}


function ajaxHTMLLoad(id,url,carregador){
    //Carregando...
    if (carregador) setCarregando(document.getElementById(id));
    //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",getLocalRestrito()+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){
                window.setTimeout("ajaxRun()",20);
            }
        }
    }
    //Executa
    xmlhttp.send(null);
}

function setCarregando(obj){
    //Carregando...
    setMensagemCarregando(obj,'Aguarde,&nbsp;carregando...');
}

function setMensagemCarregando(obj,msg){
    //Carregando...
    obj.innerHTML= "<table border=0 cellspacing=0 cellpadding=0 class=v9><tr><td width=20 ><img src="+getContexto()+"/images/working.gif width=13 height=13></td><td style='color:#666666'>"+msg+"</td></tr></table>";
}

function ajaxObjHTML(obj,url, hid,show){
    setCarregando(obj);
     //Abre a conexão
    xmlhttp.open("GET",getLocalRestrito()+url,true);
    //Função para tratamento do retorno
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //Mostra o HTML recebido
            retorno=unescape(xmlhttp.responseText);//.replace(/\+/g," "));
            obj.innerHTML=retorno;
            hid.style.display = 'none';
            show.style.display = '';
        }
    }
    //Executa
    xmlhttp.send(null);
}

function ajaxHTMLPost(id,url,valores){
    //setCarregando(document.getElementById(id));
    //Abre a conexão
    xmlhttp.open("POST",getLocalRestrito()+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
            var retorno=unescape(xmlhttp.responseText);//.replace(/\+/g," ")
            document.getElementById(id).innerHTML=retorno;
        }
    }
    //Executa
    xmlhttp.send(valores);
}
