
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.requestPage    = requestPage;
    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 requestPage(url,alvo)
    {
        if (url!='') retornaPagina(url,alvo);
    }

    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 retornaPagina(url,alvo) {
           
        xmlhttp.open('GET',url,true);
        xmlhttp.onreadystatechange = function()
        {
            
            if (xmlhttp.readyState == 4) {

                document.getElementById(alvo).innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.send();
    }

    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][0]).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"]);
    }

}	
