
function RequisicaoBrowser () {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest;
    }
    
    var aryOtherTypes = ['Microsoft.XMLHTTP','Msxml2.XMLHTTP','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Microsoft.XMLDOM'];
    
    for (intA=0; intA < aryOtherTypes.length; intA++) {
        try {
            return new ActiveXObject(aryOtherTypes[intA]);
        } catch(e) {
        }
    }    
}

function LoginPortal () {
    if (document.Login.Codigo.value == "") { 
        alert("Preencha o campo 'Login'");
        return;
    }
    
    if (document.Login.Senha.value == "") { 
        alert("Preencha o campo 'Senha'");
        return;
    }  
    
    var objBrowser = new RequisicaoBrowser ();
    
    objBrowser.open("POST", "portal/pagRequest.php?Request=Login", true); 
    objBrowser.onreadystatechange = function () {
        if (objBrowser.readyState == 4) {
            if (objBrowser.status == 200) {
                this.strResposta = objBrowser.responseText;
                this.strResposta = this.strResposta.replace(/\+/g," ");
                this.strResposta = unescape(this.strResposta);

                if (this.strResposta) {
                    document.getElementById("MensagemLogin").innerHTML = this.strResposta;
                    return;
                }                
                CarregarDados(0,"?Codigo=" + document.getElementById("Codigo").value,0)
            }
        }        
    }               
    objBrowser.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        
    objBrowser.send(LocalizarElementosFormulario(document.getElementById("Login").elements));
}

function LocalizarElementosFormulario (objElementosForm) {
    var strElementosEnviar = "";        
    
    this.ConcatenarElementos = function (strName, strValue) { 
        if (strElementosEnviar.length > 0) { 
            strElementosEnviar += "&";
        }
        strElementosEnviar += encodeURIComponent(strName) + "=" + encodeURIComponent(strValue);
    }        
   
    for (intElementosForm=0; intElementosForm < objElementosForm.length; intElementosForm++) {
        switch(objElementosForm[intElementosForm].type) {
            case 'text': case 'password': case 'hidden': case 'textarea': 
                this.ConcatenarElementos(objElementosForm[intElementosForm].name,objElementosForm[intElementosForm].value);
                break;
        }
    }
    return strElementosEnviar;
}

function CarregarDados (intOpcao,strParametros,objCorpo) {
    var objBrowser   = new RequisicaoBrowser ();
    var objDadosTela = new MostrarDadosTela ();

    if (objCorpo == 0) {
        objCorpo = document.getElementById("EstruturaCorpo");
    }
    
    if (objCorpo == 1) {
        objCorpo = document.getElementById("EstruturaCorpoSecundario");
    }

//    objDadosTela.MostrarStatusCarregando (objCorpo);   

    var strArquivo = objDadosTela.LocalizarArquivo (intOpcao);
    strArquivo = strArquivo + strParametros;
    window.open("portal/" + strArquivo,"","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,top=5,left=5,width=781,height=540");
//    objBrowser.open("POST","portal/" + strArquivo, true); 
//    window.open("POST","portal/" + strArquivo, true); 
//    objBrowser.onreadystatechange = function () {
//        if (objBrowser.readyState == 4) {
//            if (objBrowser.status == 200) {
//                this.strResposta = objBrowser.responseText;
//                this.strResposta = this.strResposta.replace(/\+/g," ");
//                this.strResposta = unescape(this.strResposta);
//                objDadosTela.MostrarInformacoes (objCorpo,this.strResposta);
//                
//                ExecScript(objBrowser.responseText);
//            }
//        }        
//    }                     
//    objBrowser.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        
//    objBrowser.send(null);
}

function MostrarDadosTela () {
    this.LimparTela = function (objTela) {
        objTela.innerHTML = "";
    }
    
//    this.MostrarStatusCarregando = function (objCorpo) { 
//        this.LimparTela (objCorpo);
        
//        this.objNovoElemento = document.createElement("div");
//        this.objNovoElemento.className = "CarregarDados";
//        this.objNovoElemento.innerHTML = "Carregando, aguarde...";
        
//        objCorpo.appendChild(this.objNovoElemento);
//    }
    
    this.MostrarInformacoes = function (objCorpo,objResposta) {
        objCorpo.innerHTML = objResposta;
    }
    
    this.LocalizarArquivo = function (intOpcao) {
        switch (intOpcao) {
            case 0 :
                return "pagHome.php"
                break;  
        }
    }
}

function ExecScript(strInnerHTML) {
    var ini = 0;

    while (ini!=-1) {
        ini = strInnerHTML.indexOf('<script', ini);
        
        if (ini >=0) {
            ini = strInnerHTML.indexOf('>', ini) + 1;
            var fim = strInnerHTML.indexOf("<\/script>", ini);
            codigo = strInnerHTML.substring(ini,fim);
            var novo = document.createElement("script")
            novo.text = codigo;
            document.body.appendChild(novo);
        }
    }
}

function ListarInformativos(strCliente,intAno) {
    var objBrowser   = new RequisicaoBrowser ();
    var objDadosTela = new MostrarDadosTela ();

    objCorpo = document.getElementById("ListaInformativos");
    
//    objDadosTela.MostrarStatusCarregando (objCorpo);   
    strArquivo = "pagInformativo.php" + "?Cliente="+strCliente+"&Ano="+intAno;

//    objBrowser.open("POST","portal/" + strArquivo, true); 
    objBrowser.open("POST",strArquivo, true); 
    objBrowser.onreadystatechange = function () {
        if (objBrowser.readyState == 4) {
            if (objBrowser.status == 200) {
                this.strResposta = objBrowser.responseText;
                this.strResposta = this.strResposta.replace(/\+/g," ");
                this.strResposta = unescape(this.strResposta);
                objDadosTela.MostrarInformacoes (objCorpo,this.strResposta);
                
                ExecScript(objBrowser.responseText);
            }
        }        
    }                     
    objBrowser.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        
    objBrowser.send(null);
}

function ApresentaInformativo(strCliente,intAno,intMes){
    window.open("pagApresentaInformativo.php?Cliente=" + strCliente + "&Ano=" + intAno + "&Mes=" + intMes,"","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,top=20,left=20,width=860,height=500");
}

function ListarAnexos(strCliente){
    window.open("pagAnexos.php?Cliente=" + strCliente,"","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,top=20,left=20,width=781,height=393");
}

var intSequencia = 1;

function VisualizarImagemInformativo(strCliente,intAno,intMes,strSequencia) {

    var objBrowser   = new RequisicaoBrowser ();

    strArquivo = "pagRequest.php?Request=UltimaSequencia&Ano="+intAno+"&Mes="+intMes;

    objBrowser.open("POST",strArquivo, true); 
    objBrowser.onreadystatechange = function () {
        if (objBrowser.readyState == 4) {
            if (objBrowser.status == 200) {
                var intUltimaSequencia = objBrowser.responseText;
                            
                switch (strSequencia) {
                    case "Capa":
                        intSequencia = 1
                        break;  
                    case "Anterior":
                        if (intSequencia == 1) {
                            intSequencia = intUltimaSequencia;
                        } else {
                            intSequencia--;
                        }
                        break;  
                    case "Proximo":
                        if (intUltimaSequencia == intSequencia) {
                            intSequencia = 1;
                        } else {
                            intSequencia++;
                        }
                        break;  
                    case "Ultimo":
                        intSequencia = 0
                        break;  
                } 
            
                var objBrowserAux = new RequisicaoBrowser ();
                var objDadosTela = new MostrarDadosTela ();
            
                //objCorpo = document.getElementById("ImagemInformativo");
                objCorpo = window.parent.Corpo.document.body;
            
                strArquivo = "pagApresentaInformativoImagem.php" + "?Cliente="+strCliente+"&Ano="+intAno+"&Mes="+intMes+"&Sequencia="+intSequencia;
            
                objBrowserAux.open("POST",strArquivo, true); 
                objBrowserAux.onreadystatechange = function () {
                    if (objBrowserAux.readyState == 4) {
                        if (objBrowserAux.status == 200) {
                            this.strResposta = objBrowserAux.responseText;
                            this.strResposta = this.strResposta.replace(/\+/g," ");
                            this.strResposta = unescape(this.strResposta);
                            objDadosTela.MostrarInformacoes (objCorpo,this.strResposta);
                        }
                    }        
                }                     
                objBrowserAux.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        
                objBrowserAux.send(null);
            }
        }        
    }                     
    objBrowser.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        
    objBrowser.send(null);
}

