dojo.provide("parus.balancePrimaryReportFormingDialog");

dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");

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

dojo.declare("parus.balancePrimaryReportFormingDialog", [parus.Dialog], {

  _agentCodeLoaded: false,

  _agentNameLoaded: false,

  formCode: null,

  agentsChanged: function(sender) {

    if (sender) if (sender.disabled) return;

    if ((this.agentCodeEdit.value == "") && (this.agentNameEdit.value == "")) {
      this.agentCodeEdit.attr("disabled", false);
      this.agentNameEdit.attr("disabled", false);
    }
    else if ((this.agentCodeEdit.value != "") && (sender.dojoAttachPoint == "agentCodeEdit")) {
      if (this._agentCodeLoaded) {
        this._agentCodeLoaded = false;
      }
      else {
        this.agentNameEdit.attr("disabled", true);
        this.agentNameEdit.attr("value", null);
      }
    }
    else if ((this.agentNameEdit.value != "") && (sender.dojoAttachPoint == "agentNameEdit")) {
      if (this._agentNameLoaded) {
        this._agentNameLoaded = false;
      }
      else {
        this.agentCodeEdit.attr("disabled", true);
        this.agentCodeEdit.attr("value", null);
      }
    }

    setTimeout(dojo.hitch(this, function() {

      var agentInfo = parus.parus8.rowNodes(parus.database.execute("getAgentInfo", {
        "AGENT_CODE": this.agentCodeEdit.disabled ? "" : this.agentCodeEdit.value,
        "AGENT_NAME": this.agentNameEdit.disabled ? "" : this.agentNameEdit.value
      }))[0];

      if (this.agentCodeEdit.disabled) this.agentCodeEdit.attr("value", parus.parus8.nodeField(agentInfo, "RAGNABBR"));
      if (this.agentNameEdit.disabled) this.agentNameEdit.attr("value", parus.parus8.nodeField(agentInfo, "RAGNNAME"));
      if (this.agentHeadEdit.disabled) this.agentHeadEdit.attr("value", parus.parus8.nodeField(agentInfo, "RHEAD"));

      if ((this.agentCodeEdit.value == "") && (this.agentNameEdit.value == "")) {
        this.agentCodeEdit.attr("disabled", false);
        this.agentNameEdit.attr("disabled", false);
      };


    }), 100);

  },

  getDatesCondition: function() {
    return {
      "FORM_CODE": this.formCode || parus.parus8.CLASS_EmptyData
    };
  },

  onShow: function() {
    this.restoreFormLastValues();
  },

  onClose: function() {
    this.saveFormLastValues();
  },

  saveFormLastValues: function() {

    this._lastValues["userSize"] = this.userSize;
    this._lastValues["catalogInput"] = this.catalogInput.attr("value");

    parus.userdata.set(this.declaredClass, this._lastValues);
  },

  restoreFormLastValues: function() {
    this._lastValues = parus.userdata.get(this.declaredClass) || {};

    this.userSize = this._lastValues["userSize"] || null;
    this.catalogInput.attr("value", this._lastValues["catalogInput"]);

  }

});