// JavaScript Document
var xmlhttp;
var xmldoc;
//==================================
//CRIA OBJETO XML REQUEST
//==================================
try{
    xmlhttp = new XMLHttpRequest();
}
catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

//==================================
//CRIA OBJETO XML DOCUMENT
//==================================
try{
    xmldoc = new xmlDocument();
}
catch(eee){
    try{
        xmldoc = new ActiveXObject("Msxml2.DOMDocument");
    }catch(eeee){
        try{
            xmldoc = new ActiveXObject("Microsoft.XMLDOM");
        }catch(EE){
            xmldoc = null
        }
    }
}

/*function ajaxDoFormPost(f) {
	t = "";
	for (var i = 0; i < f.elements.length; i++)
		if (f.elements[i].name != "")
			t += "&" + f.elements[i].name + "=" + encodeURIComponent(f.elements[i].value);
	return t;
}*/

function ajaxDoFormPost(obj) {
    d = document.getElementById(obj);
    input = d.getElementsByTagName("input");
    select = d.getElementsByTagName("select");
    t = "";
    for (var i = 0; i < input.length; i++)
        if (input[i].type == "checkbox")
            t += "&" + input[i].name + "=" + encodeURIComponent(input[i].checked);
        else
            t += "&" + input[i].name + "=" + encodeURIComponent(input[i].value);
    for (var i = 0; i < select.length; i++)
        t += "&" + select[i].name + "=" + encodeURIComponent(select[i].value);
    return t;
}

function doRequest(url,f) {
	//Cria a conexão e envia para o fim da fila
	var req = new Req(url,f);
	ajaxFila.push(req);
	//Não deixa o xmlhttp desocupado
	if(!ajaxBusy){
		ajaxBusy = true;
		doRealRequest(ajaxFila.pop());
	}
}

function doRealRequest(req) {
	// Monta os dados
	var url=req.url, f=req.f;
	// Número randômico na url para eliminar o cache no IE
	url += (url.replace(/\?/,"")==url?"?":"&")+Math.random();
	// Guarda a função de retorno
	ajaxReturnFunction = f;
	
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState == 4) {
			ajaxReturnFunction(xmlhttp.responseText);
			//Se tem conexões esperando, executa a próxima
			var proximo = ajaxFila.pop();
			if (proximo) {
				doRealRequest(proximo);
			}
			else {
				ajaxBusy = false;
			}
		}
	}
	xmlhttp.send(null);
}

//Classe Requisição
//A assinatura é a mesma da doRequest
function Req(url,f) {
	this.url = url;
	this.f = f;
}

//Fila
function Fila() {
	this.requisicoes = [];
	//Insere um elemento no fim da fila
	this.push = function(i) {
		this.requisicoes[this.requisicoes.length] = i;
	}
	//Obtém e remove o primeiro da fila
	this.pop = function() {
		if (this.requisicoes.length==0)return false;
		var ret = this.requisicoes[0],r = [];
		for(var i=1;i<this.requisicoes.length;i++) {
			r[i-1] = this.requisicoes[i];
		}
		this.requisicoes = r;
		return ret;
	}
}

/*
  Variáveis globais
*/
//Função de retorno das requisições ajax comuns
ajaxReturnFunction = false;
//Objeto HTML onde o Ajax vai despejar o código
ajaxHTMLObj = false;
//Fila de requisições
ajaxFila = new Fila();
//Se o objeto xmlhttp tem requisições pendentes
ajaxBusy = false;