
function Ajax()
{
	
	/*************************
	 *** PROPERTIES
	 ************************/
	xmlhttp = undefined;
	fila    = [];
	ifila   = 0;
	method  = "GET";
	sync    = true;
	loadmsg = "Carregando...";
	flagloadmsg = false;
	
	/*************************
	 *** 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)
	{
		//Adiciona à fila	
		fila[fila.length] = {"id" : id,"url" : url, "func" : "putInPlace", "method":"GET", "send":null};
    	
		//Se não há conexões pendentes, executa
    	if((ifila+1) <= fila.length)run();
	}
	
	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)
	{
		//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>");
		
		//Abre a conexão
	    xmlhttp.open(fila[ifila]["method"], fila[ifila]["url"], sync);
	    //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);
   	    		//document.getElementById(fila[ifila][1]).innerHTML = retorno;
   	        	//Roda o próximo
   	         	ifila++;
   	     		if(ifila < fila.length) setTimeout("run()", 20);
         	}
		}

		//Executa
		if(fila[ifila]["method"] == "POST") {
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
    	xmlhttp.send(fila[ifila]["send"]);
	}

}	

/**
* 
* Função para carregar na div_corpo
* 
*/

var url, func;
function facende(y){
    val = y/100;
    document.getElementById('div_corpo').style.opacity = val;
    document.getElementById('div_corpo').style.filter = "progid:DXImageTransform.Microsoft.alpha( Opacity="+y+")";
    document.getElementById('div_corpo').style.zoom = 1; // for ie, set haslayout 
    document.getElementById('div_corpo').style.display="block"; 

    y+= 5;
    setTimeout(function(){
        if(y <= 100){
            facende(y);
        }
        else{
            document.getElementById('div_corpo').style.opacity = 1.0;
            document.getElementById('div_corpo').style.filter = ''; 
        }
    },10);
}

function fapaga(y){
    val = y/100;
    document.getElementById('div_corpo').style.opacity = val;
    document.getElementById('div_corpo').style.filter = "alpha(opacity="+y+")";
    y-= 5;
    setTimeout(function(){
        if(y >= 0){
            fapaga(y);
        }
        else{
            document.getElementById('div_corpo').style.opacity = 0.0;
            Ajax.requestGetBy(url,func);
        }
    },10);
}

function carrega(purl,pfunc){
    if(navigator.appName!='Microsoft Internet Explorer'){
        url = purl;
        func = pfunc;
        fapaga(100);
    }
    else{
        document.getElementById('div_corpo').innerHTML = "<center><img src='images/loading.gif'/></center>";
        Ajax.requestGetBy(purl,pfunc);
    }
}

function abertura(resposta){
    if(navigator.appName!='Microsoft Internet Explorer'){
        facende(0);
    }
    document.getElementById("div_corpo").innerHTML = resposta;
}

Ajax = new Ajax();
