/** - Arquivo de leitura e apresentação do ajax FINANSite. */

/** -- construtor da classe
* @param tab índice da tabela
* @param coluna índice do ponto de partida no xml
*/
function Cotacao(tab, coluna) {
    this.ini_coluna = coluna;
    this.tabela_ini = tab;
}

//Cotacao.prototype.primeira_vez = true;
Cotacao.prototype.xmlHttp = "";
Cotacao.prototype.xmlDoc = "";
Cotacao.prototype.ini_coluna = 0;
Cotacao.prototype.prefixo_i = 0;
Cotacao.prototype.tabela_ini = 0;
Cotacao.prototype.str_http = "";
Cotacao.prototype.bMaxMin = false;
Cotacao.prototype.bVolume = false;
Cotacao.prototype.nome_arquivos = new Array();

Cotacao.prototype.habilitaMaxMin = function () {
    this.bMaxMin = true;
}

Cotacao.prototype.habilitaVolume = function () {
    this.bVolume = true;
}

Cotacao.prototype.pregaoAberto = function () {
    if (window.XMLHttpRequest) {
        if (this.xmlDoc.childNodes[0].nodeName != "xml") {
            var pregao_aberto = this.xmlDoc.childNodes[0].childNodes[2].attributes[0].value;
        }
        else if (this.xmlDoc.childNodes[1].nodeName != "xml") {
            var pregao_aberto = this.xmlDoc.childNodes[1].childNodes[2].attributes[0].value;
        }
    }
    else if (window.ActiveXObject) {
        if (this.xmlDoc.childNodes[1]) {
            var pregao_aberto = this.xmlDoc.childNodes[1].childNodes[2].attributes[0].value;
        }
    }

    if (pregao_aberto == 1) {
        return true;
    }
    else {
        return false;
    }
}

Cotacao.prototype.escreveXMLMaxMin = function () {

    if (this.pregaoAberto()) {
        var msgobj = this.xmlDoc.getElementsByTagName("ATIVO");
        var hora = this.xmlDoc.getElementsByTagName("HORA");
        var data_xml = this.xmlDoc.getElementsByTagName("DATA");

        for (i = 0; i < msgobj.length; i++) {
            for (j = this.ini_coluna; j < msgobj[0].childNodes.length; j++) {
                var nome_campo_xml = msgobj[i].childNodes[j].nodeName;
                if (msgobj[i].childNodes[j].firstChild) {
                    var valor_mostra = msgobj[i].childNodes[j].firstChild.data;
                }
                else {
                    var valor_mostra = "";
                }

                if (nome_campo_xml == "QTT" && valor_mostra != "") {
                    campo_id = "t" + this.tabela_ini + "l" + (i + 1) + "c" + (j - 2);

                    valor_mostra_int = parseInt(valor_mostra);
                    valor_mostra_int_k = valor_mostra_int / 1000;
                    if (valor_mostra_int_k > 1) {
                        valor_mostra_int_m = valor_mostra_int_k / 1000;
                        if (valor_mostra_int_m > 1) {
                            valor_mostra_int_b = valor_mostra_int_m / 1000;
                            if (valor_mostra_int_b > 1) {
                                valor_mostra = valor_mostra_int_b + "B";
                            }
                            else {
                                valor_mostra = valor_mostra_int_m + "M";
                            }
                        }
                        else {
                            valor_mostra = valor_mostra_int_k + "k";
                        }
                    }
                    else {
                        valor_mostra = valor_mostra_int;
                    }
                }
                else {
                    campo_id = "t" + this.tabela_ini + "l" + (i + 1) + "c" + (j - 1);
                }
                escrever_campo(campo_id, valor_mostra);
            }
        }

        data_atualizado = data_xml[0].firstChild.data;
        campo_id_data = "id_data_t" + this.tabela_ini;
        escrever_campo(campo_id_data, data_atualizado);
        hora_atualizado = hora[0].firstChild.data;
        campo_id_hora = "id_hora_t" + this.tabela_ini;
        escrever_campo(campo_id_hora, hora_atualizado);
    }
}

Cotacao.prototype.escreveXMLCotacao = function () {
    var msgobj = this.xmlDoc.getElementsByTagName(tag_ativos);
    for (i = 0; i < msgobj.length; i++) {
        if (msgobj[i].firstChild) {
            for (j = this.ini_coluna; j < msgobj[0].childNodes.length; j++) {
                if (this.ini_coluna == 0) {
                    campo_id = "t" + this.tabela_ini + "l" + (i + 1) + "c" + (j);
                }
                else {
                    campo_id = "t" + this.tabela_ini + "l" + (i + 1) + "c" + (j - 1);
                }

                if (msgobj[i].childNodes[j].firstChild) {
                    var nome_campo_xml = msgobj[i].childNodes[j].nodeName;
                    var valor_mostra = msgobj[i].childNodes[j].firstChild.data;
                    if (nome_campo_xml == campo_variacao) {
                        cor_variacao = pegaCor(valor_mostra);
                        valor_mostra = '<SPAN CLASS=' + cor_variacao + '>' + valor_mostra + '%</SPAN>';
                        if (mostrar_imagem) {
                            campo_seta = "cel_imagem" + (i + 1);
                            valor_seta = '<img src="' + _languageStyleFolder + '/Redecard/Img/' + cor_variacao + '.png" align=middle>';
                            escrever_campo(campo_seta, valor_seta);
                        }
                    }
                    escrever_campo(campo_id, valor_mostra);
                }
                else {
                    escrever_campo(campo_id, "-");
                }
            }
        }
        else {
            for (j = this.ini_coluna; j < msgobj[0].childNodes.length; j++) {
                campo_id = "t" + this.tabela_ini + "l" + (i + 1) + "c" + (j - 1);
                escrever_campo(campo_id, "-");
            }
        }
    }
}

Cotacao.prototype.createXMLHttpRequest = function () {
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { }
    try { return new XMLHttpRequest(); } catch (e) { }
    $.loaderIndicadorFinanceiro(false, false); //alert("XMLHttpRequest not supported");
    return null;
}

Cotacao.prototype.ler_xml = function (url) {
    var metodo = "GET";
    var _this = this;

    this.xmlHttp = this.createXMLHttpRequest();

    this.str_http = url.substr(0, 6);
    if (this.str_http == "http:/") {
        metodo = "POST";
    }

    this.xmlHttp.onreadystatechange = function () { _this.esperaXML() };

    gambi_ie_dt = new Date();
    gambi_ie_dt_hr = gambi_ie_dt.getTime();
    url += "?hora=" + gambi_ie_dt_hr;
    if (window.XMLHttpRequest) {
        try {
            this.xmlHttp.open(metodo, url, true);
        } catch (err) {
            // -- sem permissão
            $.loaderIndicadorFinanceiro(false, false); //alert(//err);
        }
        //this.xmlHttp.open(metodo, url, true);
    }
    else if (window.ActiveXObject) {
        this.xmlHttp.open(metodo, url, true);
    }

    this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
    this.xmlHttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");

    this.xmlHttp.setRequestHeader("Expires", "0");
    this.xmlHttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    this.xmlHttp.setRequestHeader("Pragma", "no-cache");

    this.xmlHttp.send(null);
}

Cotacao.prototype.escreve = function () {

    if (this.bMaxMin == true) {
        this.escreveXMLMaxMin();
    }
    else {
        if (this.bVolume == true) {
            this.escreveXMLMaxMin();
        }
        else {
            this.escreveXMLCotacao();
        }
    }

}

Cotacao.prototype.esperaXML = function () {
    /**
    * 0 - Não iniciado (Uninitialised)
    * 1 - Carregando (Loading)
    * 2 - Carregado (Loaded)
    * 3 - Interativo (Interactive)
    * 4 - Completado (Completed)
    */

    if (this.xmlHttp.readyState != 4) {
        return;
    }
    else {
        if (this.xmlHttp.status == 200) {
            this.xmlDoc = null;
            if (this.str_http == "http:/") {
                this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                this.xmlDoc.loadXML(this.xmlHttp.responseText);
            }
            else {
                this.xmlDoc = this.xmlHttp.responseXML;
            }
            this.escreve();
            $.loaderIndicadorFinanceiro(false, false);
        }
        else {
            $.loaderIndicadorFinanceiro(false, true);
        }
    }
}

Cotacao.prototype.carregar = function (arq_i) {
    this.prefixo_i = arq_i;
    $.loaderIndicadorFinanceiro(true, false);
    if (this.nome_arquivos[arq_i] != null) {
        this.ler_xml(this.nome_arquivos[arq_i]);
    }
    else {
        this.ler_xml(nome_arquivos[arq_i]);
    }
}

// ----------------------------------------------------------------------------------
// funções de apresentação por innerhtml

var nome_arquivos = new Array();
var DHTML = (document.getElementById || document.all || document.layers);
var xmlDocWin;
var ini_coluna_win;
function pegarDiv(nome) {
    if (document.getElementById) {
        this.obj = document.getElementById(nome);
    }
    else if (document.all) {
        this.obj = document.all[nome];
    }
    else if (document.layers) {
        this.obj = document.layers[nome];
    }
}

function escrever_campo(id, texto) {
    if (!DHTML) return;
    var x = new pegarDiv(id);
    if (x.obj) {
        x.obj.innerHTML = texto;
    }
    return;
}

function pegaCor(valor) {
    cor = 'semvar';
    temp = valor.replace(",", ".");
    if (parseFloat(temp) == 0)
        cor = 'semvar';
    else if (parseFloat(temp) > 0)
        cor = 'varpos';
    else if (parseFloat(temp) < 0)
        cor = 'varneg';
    return cor;
}

