  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 Ajax(){
	
	/*************************
	 *** PROPERTIES
	 ************************/
	xmlhttp = undefined;
	fila    = [];
	ifila   = 0;
	method  = "GET";
	sync    = true;
	loadmsg = "<div align='center' class='text'><br><br><br><br>Carregando...<br><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' width='50' height='50' id='carregando' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='carregando.swf' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><embed src='carregando.swf' quality='high' bgcolor='#ffffff' width='50' height='50' name='carregando' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></div>";
	flagloadmsg = true;
	
	/*************************
	 *** METHODS ALIASES
	 ************************/
	this.Ajax       = Ajax;
	this.startAjax  = startAjax;
	this.putInPlace = putInPlace;
	this.requestGetFor  = requestGetFor;
	this.requestGetBy   = requestGetBy;
	this.requestPostFor = requestPostFor;
	this.requestPostBy  = requestPostBy;
	this.run        = run;
	this.setSync    = setSync;
	this.fetchForm  = fetchForm;
	this.setLoadMsg = setLoadMsg;
	
	/*************************
	 *** CONSTRUCTOR
	 ************************/
	Ajax();
	
	/*************************
	 *** METHODS DEFINITION
	 ************************/

	function Ajax()
	{
		startAjax();
	}

	function setSync(arg)
	{
		sync = arg;
	}

	function setLoadMsg(msg)
	{
		flagloadmsg = true;
		if(msg != undefined)
			loadmsg = msg;
	}

	function startAjax()
	{
		try{
			xmlhttp = new XMLHttpRequest();
		}catch(ee){
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(E){
					//xmlhttp = false;
				}
			}
		}
	}
	
  function requestGetFor(url, id, msg){
			//ajaxHTML(id,url){
      //Carregando...
			//alert(loadmsg);
      if(msg==1) document.getElementById(id).innerHTML="<span class='carregando'>" + loadmsg + "</span>"
      //Adiciona à fila
      fila[fila.length]=[id,url]
      //Se não há conexões pendentes, executa
      if((ifila+1)==fila.length)ajaxRun()
  }
	
	function requestGetBy(url, func)
	{			
		//Adiciona à fila
		fila[fila.length] = {"url":url, "func":func, "method":"GET", "send":null};
    	//Se não há conexões pendentes, executa
    	if((ifila+1) <= fila.length)run();
	}
	
	function requestPostFor(url, form_name, id, msg)
	{
		//Adiciona à fila
		fila[fila.length] = {"url":url, "id":id, "func":"putInPlace", "method":"POST", "send":fetchForm(form_name)};
    	//Se não há conexões pendentes, executa
    	if((ifila+1) <= fila.length)run();
	}

	function requestPostBy(url, form_name, func)
	{
		//Adiciona à fila
		fila[fila.length] = {"url":url, "func":func, "method":"POST", "send":fetchForm(form_name)};
    	//Se não há conexões pendentes, executa
    	if((ifila+1) <= fila.length)run();
	}

	function putInPlace(cont)
	{
		obj = document.getElementById(fila[ifila]["id"]);
		obj.innerHTML = cont;
		return true;
	}

	function fetchForm(form_name)
	{
		temp = "";
		size = document.forms[form_name].elements.length;
		for(i = 0; i < size; i++) {
			if(temp.length) temp += "&";
			input = document.forms[form_name].elements[i];
			switch(input.type) {
				case "checkbox":
				case "radio":
					if(input.checked)
						temp += input.name + "=" + input.value; 
				break;

				default: 
				temp += input.name + "=" + input.value; 
					
			}
		}
		return temp;
	}

	
	function run(){
		//Carregando...
		//if(fila[ifila]["func"] == "putInPlace" && flagloadmsg){
		  //putInPlace("<span class='carregando'>" + loadmsg + "</span>");
			//alert("carregando...");
		//}
		
		//Abre a conexão
	  xmlhttp.open(fila[ifila]["method"], fila[ifila]["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," "));
				eval(fila[ifila]["func"] + "(\"" + retorno + "\")");
        //Roda o próximo
        ifila++;
        if(ifila < fila.length)setTimeout("run()", 20);
     	}
		}
		if(fila[ifila]["method"] == "POST") {
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
	  xmlhttp.send(fila[ifila]["send"]);
	}

	
  function ajaxHTML(id,url){
  
      //Obtém o objeto HTML
      objetoHTML=document.getElementById(id)
  
      //Exibe "Carregando..."
      objetoHTML.innerHTML="<span class='carregando'>"+
                           "Carregando...</span>"
  
      //Abre a conexão
      xmlhttp.open("GET",url);
  
      //Função para tratamento do retorno
      xmlhttp.onreadystatechange=function() {
          if (xmlhttp.readyState==4){
              //Mostra o HTML recebido
              retorno=unescape(xmlhttp.responseText.replace(/\+/g," "))
              objetoHTML.innerHTML=retorno
          }
      }
  
      //Executa
      xmlhttp.send(null)
  }
	
}	

