﻿function IniciaSolicitacao() {
    if (document.getElementById("PanelProcessamento"))
        document.getElementById("PanelProcessamento").style.visibility = "visible";

    if (document.getElementById("gifLoad"))
        document.getElementById("gifLoad").style.visibility = "visible";
}

function FinalizaSolicitacao() {
    if (document.getElementById("PanelProcessamento"))
        document.getElementById("PanelProcessamento").style.visibility = "hidden";

    if (document.getElementById("gifLoad"))
        document.getElementById("gifLoad").style.visibility = "hidden";
}

function Erro_Padrao(error, userContext, methodName) {
    alert(error.get_message());
    FinalizaSolicitacao();
}

function ImprimirAtendimentosSelecionados(gridName, gridRows) {
    IniciaSolicitacao();
    var grid = document.getElementById(gridName);
    var gridRowsCount = grid.rows.length;
    var atendimentos = new Array();

    var indice = 1;
    
    if (gridRowsCount > gridRows + 1)
    {
        indice = 2;
        gridRowsCount = gridRowsCount - 1;
    }

    for (indice; indice < gridRowsCount; indice++) {
        var linhaMarcada = grid.rows[indice].cells[1].children[0].checked;

        if (linhaMarcada) {
            var posto = grid.rows[indice].cells[6].childNodes[0].nodeValue;
            var amostra = grid.rows[indice].cells[5].childNodes[0].nodeValue;
            atendimentos.push(posto + "|" + amostra);
        }
    }

    ResultadosOnLine.classes.ROL_Services.ImprimirAtendimentosSelecionados(atendimentos, Sucesso_ImprimirAtendimentosSelecionados, Erro_Padrao);
}

function Sucesso_ImprimirAtendimentosSelecionados(result, eventArgs) {
    if (result.length > 0)
        window.open(result);
    else
        alert("Selecione ao menos uma amostra.");

    FinalizaSolicitacao();
}
