dojo.provide("parus.balanceReportCheckProtocolPrint");

dojo.require("parus.parus8");
dojo.require("parus.database");

dojo.mixin(parus, {

  balanceReportCheckProtocolPrint: {

    print: function() {

      var query;
      var full;
      var handler;
      var title;
      dojo.xhrGet({

        url: "../.system/session.xml",
        handleAs: "xml",
        sync: true,
        preventCache: true,

        load: function(response) {

          if (!dojo.isIE) response.normalize();

          var LNodes = response.documentElement.getElementsByTagName("*");

          for (var i = 0; i != LNodes.length; i++)
          if (LNodes[i].nodeName == "query") query = LNodes[i].childNodes.item(0).nodeValue
          else if (LNodes[i].nodeName == "full") full = (LNodes[i].childNodes.item(0).nodeValue == "1")
          else if (LNodes[i].nodeName == "handler") handler = LNodes[i].childNodes.item(0).nodeValue
          else if (LNodes[i].nodeName == "title") title = LNodes[i].childNodes.item(0).nodeValue;

          return (response);

        }
      });

      parus.database.connected = handler;

      document.body.style.width = full ? "260mm" : "190mm";

      if (title.charAt(0) != "<") title = "<div align=\"center\">" + title + "</div>";

      document.body.insertAdjacentHTML("afterBegin", title);

      var titleNode = document.body.firstChild;

      titleNode.style.height = "auto";
      titleNode.style.fontWeight = "bold";
      titleNode.style.fontFamily = "Tahoma, Verdana, sans-serif";
      titleNode.style.margin = "10px";
      document.title = (!dojo.isIE || dojo.isIE >= 10) ? titleNode.textContent : titleNode.innerText;

      var table = document.body.appendChild(document.createElement("table"));

      table.width = "100%";
      table.border = "1";
      table.cellSpacing = "0";

      var row = table.insertRow(-1);
      row.style.fontWeight = "bold";
      row.insertCell(-1).appendChild(document.createTextNode("№№ п/п"));
      row.insertCell(-1).appendChild(document.createTextNode("Форма отчета"));
      row.insertCell(-1).appendChild(document.createTextNode("Разрез"));
      row.insertCell(-1).appendChild(document.createTextNode("Дата отчета"));
      row.insertCell(-1).appendChild(document.createTextNode("Контрагент"));
      row.insertCell(-1).appendChild(document.createTextNode("Перечень"));
      row.insertCell(-1).appendChild(document.createTextNode("Тип"));
      row.insertCell(-1).appendChild(document.createTextNode("Сообщение"));

      if (full) {
        row.insertCell(-1).appendChild(document.createTextNode("Описание левой части"));
        row.insertCell(-1).appendChild(document.createTextNode("Значение левой части"));
        row.insertCell(-1).appendChild(document.createTextNode("Оператор"));
        row.insertCell(-1).appendChild(document.createTextNode("Описание правой части"));
        row.insertCell(-1).appendChild(document.createTextNode("Значение правой части"));
        row.insertCell(-1).appendChild(document.createTextNode("Разность"));
      };

      var n = 1;

      dojo.forEach(parus.parus8.rowNodes(parus.database.query("ReportCheckResultsPrint", dojo.fromJson(query))),

      function(dataRow) {
        var row = table.insertRow(-1);
        row.style.fontSize = "smaller";

        row.insertCell(-1).appendChild(document.createTextNode(n++));
        row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "FORM_CODE") || String.fromCharCode(160)));
        row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "SECTION_CODE") || String.fromCharCode(160)));
        row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "BDATE") || String.fromCharCode(160)));
        row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "AGENT_CODE") || String.fromCharCode(160)));
        row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "CHECKLIST_CODE") || String.fromCharCode(160)));
        row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "RESULT") || String.fromCharCode(160)));
        row.insertCell(-1).innerHTML = (parus.parus8.nodeField(dataRow, "MESSAGE") || String.fromCharCode(160)).split("\n").join("<br/>");
        if (full) {
          row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "LEFT_DESC") || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "LEFT_RESULT") || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "REL_OPERATOR") || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "RIGHT_DESC") || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "RIGHT_RESULT") || String.fromCharCode(160)));
          row.insertCell(-1).appendChild(document.createTextNode(parus.parus8.nodeField(dataRow, "SUBTRACTION") || String.fromCharCode(160)));
        }

      });

      if (n == 1) dojo.place("<div>Нет ошибок и предупреждений.</div>", table, "replace");
    }
  }

});