dojo.provide("parus.balanceConsolidatedReportDialog");

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

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

dojo.require("parus.customEdit");

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

  _agentCodeLoaded: false,

  _agentNameLoaded: false,

  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);
  }

});