/*
    Criado em : 03/06/2009
    Autor     : Alessandro Gonzalez
    Descrição:
        Funções que trabalha de forma assincrona.
*/

var req;
var aDiv;

function loadXMLDoc(url, div, mLoad){
    req = null;
    aDiv = div;

/*    if(document.getElementById(aDiv).innerHTML != ""){
        document.getElementById(aDiv).innerHTML = "";
        return;
    }

*/    if(mLoad == "S"){
        imgCarregando = "<img src=\"../img/carregando.gif\">";
    } else {
        imgCarregando = "";
    }

    document.getElementById(aDiv).innerHTML = imgCarregando;

    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);

    } else if (window.ActiveXObject) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP.4.0");
        } catch(e) {
            try {
                req = new ActiveXObject("Msxml2.XMLHTTP.3.0");
            } catch(e) {
                try {
                    req = new ActiveXObject("Msxml2.XMLHTTP");
                } catch(e) {
                    try {
                        req = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch(e) {
                        req = false;
                    }
                }
            }
        }

        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange(){
    if (req.readyState == 4) {
        if (req.status == 200) {
            document.getElementById(aDiv).innerHTML = req.responseText;

            extraiScript(req.responseText);

        } else {
            alert("Ops!! Falhou:\n" + req.statusText);
        }
    }
}

function extraiScript(texto){
    //Maravilhosa função feita pelo SkyWalker.TO do imasters/forum
    //http://forum.imasters.com.br/index.php?showtopic=165277
    // inicializa o inicio ><
    var ini = 0;
    // loop enquanto achar um script
    while (ini!=-1){
        // procura uma tag de script
        ini = texto.indexOf('<script', ini);
        // se encontrar
        if (ini >=0){
            // define o inicio para depois do fechamento dessa tag
            ini = texto.indexOf('>', ini) + 1;
            // procura o final do script
            var fim = texto.indexOf('</script>', ini);
            // extrai apenas o script
            codigo = texto.substring(ini,fim);
            // executa o script
            //eval(codigo);
            /**********************
            * Alterado por Micox - micoxjcg@yahoo.com.br
            * Alterei pois com o eval não executava funções.
            ***********************/
            novo = document.createElement("script")
            novo.text = codigo;
            document.body.appendChild(novo);
        }
    }
}

function atualiza(valor,pagina,div,mLoad){
    if(mLoad == "") 
        mLoad = "S";
    
    loadXMLDoc(pagina + ".php?ID=" + valor, div, mLoad);
}


function AbreJanela(url,rolagem,lar,alt){
window.open(url,'_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,top=30,left=250,scrollbars=yes,resizable=no,menubar=no,width='+lar+',height='+alt);	
}

