dojo.provide("parus.designerCheckDialog");

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

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

dojo.require("parus.editableDictionaryDialog");
dojo.require("parus.editDictionaryDialog");

dojo.declare("parus.designerCheckDialog", [parus.editDictionaryDialog], {

  onLoadParams: function(store, item, editMode) {
    this.inherited(arguments);

    if (store && item) {
      this.userProcEdit.attr("value", store.getValue(item, "FORMULA_CODE"));
      this.nameEdit.attr("value", store.getValue(item, "FORMULA_NAME"));
      this.levelSelect.attr("value", store.getValue(item, "ERROR_LEVEL"));
    }

    this.userProcEdit.onApplyDictionary = dojo.hitch(this, this.userProcChanged);
  },

  userProcChanged: function(dialog) {
    //changed by dictionary dialog
    if (dialog) this.nameEdit.attr("value", dialog.getFieldValue("NAME"));
    //changed by control text
    else if (!this.userProcEdit.showingDictionary && !this.userProcEdit.attr("value")) //avoid triggering callback when dialog dictionary changes
    this.nameEdit.attr("value", "");
  },

  executeInsert: function(duplicate) {
    var response = parus.database.execute("designer.BalanceChecks.Insert", {
      PRN: this.prn,
      LEVEL: this.levelSelect.attr("value"),
      FORMULA_CODE: this.userProcEdit.attr("value")
    });

    if (response) return parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "NRN");
  },

  executeUpdate: function() {
    var response = parus.database.execute("designer.BalanceChecks.Update", {
      RN: this.rn,
      LEVEL: this.levelSelect.attr("value"),
      FORMULA_CODE: this.userProcEdit.attr("value")
    });

    return !!response;
  },

  executeDelete: function(store, item) {
    parus.database.execute("designer.BalanceChecks.Delete", {
      RN: store.getValue(item, "RN")
    });
  }

});