
// Script ajax desenvolvido por Silvio Garbes Lara
// silviogarbes@gmail.com
// Utilizado as melhores praticas sobre ajax encontrada na internet.
// versao 15:32 02/08/2008

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

//Fila de conex&#245;es
fila=[]
ifila=0
filaForm=[]
ifilaForm=0

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

    // Enviar dados do formulario
    if(form != undefined){
        // onClick="ajaxHTML('id_div','url_abrir',this.form)"
        // onSubmit="ajaxHTML('id_div','url_abrir',this)"

        var form_string = camposForm(form);

        //Carregando...
        document.getElementById(id).innerHTML="<span class='carregando'>Carregando...</span>"

        //Adiciona &#224; fila
        filaForm[filaForm.length]=[id,url,form_string]
        //Se n&#227;o h&#225; conex&#245;es pendentes, executa
        if((ifilaForm+1)==filaForm.length)ajaxRunForm()


        //ajaxRunForm(id,url,form_string);
    }else{
        // onClick="ajaxHTML('id_div','url_abrir')"

        //Carregando...
        document.getElementById(id).innerHTML="<span class='carregando'>Carregando...</span>"

        //Adiciona &#224; fila
        fila[fila.length]=[id,url]
        //Se n&#227;o h&#225; conex&#245;es pendentes, executa
        if((ifila+1)==fila.length)ajaxRun()
    }

}



//Executa a pr&#243;xima conex&#227;o da fila
function ajaxRun(){
    //Abre a conex&#227;o
    xmlhttp.open("GET",fila[ifila][1],true);
    //Fun&#231;&#227;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&#243;ximo
            ifila++
            if(ifila<fila.length)setTimeout("ajaxRun()",20)
        }
    }
    //Executa
    xmlhttp.send(null)
}

function ajaxRunForm(){
    //Abre a conex&#227;o
    xmlhttp.open("POST",filaForm[ifilaForm][1],true);
    xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    //Fun&#231;&#227;o para tratamento do retorno
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //Mostra o HTML recebido
            retorno=unescape(xmlhttp.responseText.replace(/\+/g," "))
            document.getElementById(filaForm[ifilaForm][0]).innerHTML=retorno
            //Roda o pr&#243;ximo
            ifilaForm++
            if(ifilaForm<filaForm.length)setTimeout("ajaxRunForm()",20)
        }
    }
    //Executa
    xmlhttp.send(filaForm[ifilaForm][2])
}

function camposForm(form){
        var aParams = new Array();
        var sParam

        for (var i=0; i < document.getElementById(form.id).elements.length; i++) {

            switch(document.getElementById(form.id).elements[i].type){

              case 'radio': case 'checkbox':
                if(document.getElementById(form.id).elements[i].checked){
                  sParam = encodeURIComponent(document.getElementById(form.id).elements[i].name);
                  sParam += "=";
                  sParam += encodeURIComponent(htmlEntities(document.getElementById(form.id).elements[i].value));
                  aParams.push(sParam);
                }
                break;

              case 'select-multiple':
                for(var j=0; j<document.getElementById(form.id).elements[i].options.length; j++){
                  if(document.getElementById(form.id).elements[i].options[j].selected){
                    sParam = encodeURIComponent(document.getElementById(form.id).elements[i].name);
                    sParam += "=";
                    sParam += encodeURIComponent(htmlEntities(document.getElementById(form.id).elements[i].options[j].value));
                    aParams.push(sParam);
                  }
                }
                break;

              default: 
                sParam = encodeURIComponent(document.getElementById(form.id).elements[i].name);
                sParam += "=";
                sParam += encodeURIComponent(htmlEntities(document.getElementById(form.id).elements[i].value));
                aParams.push(sParam);
                break;
            }

        }

        return aParams.join("&");
}

function htmlEntities(texto){
    //by Micox - elmicox.blogspot.com - www.ievolutionweb.com
    //Alterado por Silvio Garbes
    var i,carac,letra,novo='';

    for(i=0;i<texto.length;i++){
        carac = texto.charCodeAt(i);
        //se for numero ou letra normal
        if(carac < 128){
              novo += texto.charAt(i);
        }else{
            novo += "&#" + carac + ";";
        }
    }
    return novo;
}