dojo.provide("parus.designerIndexDialog");

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.editDictionaryDialog");
dojo.require("parus.designerIndexInputPatternDialog");

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

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

    if (store && item) {
      this.codeEdit.attr("value", store.getValue(item, "CODE"));
      this.nameEdit.attr("value", store.getValue(item, "NAME"));
      this.dataTypeSelect.attr("value", store.getValue(item, "BITYPE"));

      if (store.getValue(item, "BITYPE") == "0") {
        this.strLenEdit.attr("value", store.getValue(item, "STRLEN"));
        this.begStrValueEdit.attr("value", store.getValue(item, "BEGSTRVAL"));
        this.patternEdit.attr("value", store.getValue(item, "PATTERN_CODE"));
        this.trimStrCheckBox.attr("checked", store.getValue(item, "TRIMSTRVAL") == "1");
      }
      else if (store.getValue(item, "BITYPE") == "1") {
        this.intPartEdit.attr("value", store.getValue(item, "INTPART"));
        this.fracPartEdit.attr("value", store.getValue(item, "FRACPART"));
        this.begNumValueEdit.attr("value", store.getValue(item, "BEGNUMVAL"));
        this.minNumValueEdit.attr("value", store.getValue(item, "MINNUMVAL"));
        this.maxNumValueEdit.attr("value", store.getValue(item, "MAXNUMVAL"));
      }
      else if (store.getValue(item, "BITYPE") == "2") {
        this.begDateValueEdit.attr("value", parus.formats.stringToDate(store.getValue(item, "BEGDATEVAL")));
        this.minDateValueEdit.attr("value", parus.formats.stringToDate(store.getValue(item, "MINDATEVAL")));
        this.maxDateValueEdit.attr("value", parus.formats.stringToDate(store.getValue(item, "MAXDATEVAL")));
      }
    }

    this.dataTypeChanged();
  },

  dataTypeChanged: function() {
    var dataType = this.dataTypeSelect.attr("value");

    this.checkValuePane(this.strValuePane, dataType == "0");
    this.checkValuePane(this.numValuePane, dataType == "1");
    this.checkValuePane(this.dateValuePane, dataType == "2");
  },

  checkValuePane: function(valuePane, visible) {
    valuePane.domNode.style.display = visible ? "" : "none";

    dojo.forEach(valuePane.getChildren(), function(widget) {
      widget.attr("disabled", !visible);
    });
  },

  strLenChanged: function() {
    var maxLen = parseInt(this.strLenEdit.attr("value"));
    if (maxLen) this.begStrValueEdit.maxLength = maxLen;
  },

  intPartChanged: function() {
    if (!isNaN(this.intPartEdit.attr("value"))) {
      var intPart = parseInt(this.intPartEdit.attr("value"));
      var maxFracPart = intPart - 1;

      if (!isNaN(this.fracPartEdit.attr("value"))) {
        var fracPart = parseInt(this.fracPartEdit.attr("value"));
        if (fracPart > maxFracPart) this.fracPartEdit.attr("value", maxFracPart);
      }

      this.setControlsMaxNumber();
    }
  },

  fracPartChanged: function() {
    var fracPart = parseInt(this.fracPartEdit.attr("value"));

    this.begNumValueEdit.constraints.fractional = !isNaN(fracPart) && (fracPart > 0);
    this.minNumValueEdit.constraints.fractional = !isNaN(fracPart) && (fracPart > 0);
    this.maxNumValueEdit.constraints.fractional = !isNaN(fracPart) && (fracPart > 0);

    if (!isNaN(fracPart)) {
      this.begNumValueEdit.constraints.places = fracPart;
      this.minNumValueEdit.constraints.places = fracPart;
      this.maxNumValueEdit.constraints.places = fracPart;

      this.setControlsMaxNumber();
    }
  },

  setControlsMaxNumber: function() {
    var maxNumber = this.getMaxNumber();
    this.begNumValueEdit.constraints.max = maxNumber;
    this.minNumValueEdit.constraints.max = maxNumber;
    this.maxNumValueEdit.constraints.max = maxNumber;
  },

  getMaxNumber: function() {
    var intPart = parseInt(this.intPartEdit.attr("value"));

    if (!isNaN(intPart)) {
      var fracPart = parseInt(this.fracPartEdit.attr("value"));
      if (!isNaN(fracPart)) intPart -= fracPart;

      var maxInt = parus.formats.stringOfChar("9", intPart);

      if (!isNaN(fracPart)) {
        var maxFrac = parus.formats.stringOfChar("9", fracPart);
        return parseFloat(maxInt + "." + maxFrac);
      }
      else return parseInt(maxInt);
    }
  },

  getExecParams: function() {
    var dataType = this.dataTypeSelect.attr("value");

    var params = {
      CODE: this.codeEdit.attr("value"),
      NAME: this.nameEdit.attr("value"),
      BITYPE: this.dataTypeSelect.attr("value")
    };

    if (dataType == "0") dojo.mixin(params, {
      STRLEN: this.strLenEdit.attr("value"),
      BEGSTRVAL: this.begStrValueEdit.attr("value"),
      PATTERN: this.patternEdit.attr("value"),
      TRIMSTRVAL: (this.trimStrCheckBox.attr("checked") ? "1" : "0")
    });

    if (dataType == "1") dojo.mixin(params, {
      INTPART: this.intPartEdit.attr("value"),
      FRACPART: this.fracPartEdit.attr("value"),
      BEGNUMVAL: !isNaN(this.begNumValueEdit.attr("value")) ? this.begNumValueEdit.attr("value") : null,
      MINNUMVAL: !isNaN(this.minNumValueEdit.attr("value")) ? this.minNumValueEdit.attr("value") : null,
      MAXNUMVAL: !isNaN(this.maxNumValueEdit.attr("value")) ? this.maxNumValueEdit.attr("value") : null,

      TRIMSTRVAL: (this.trimStrCheckBox.attr("checked") ? "1" : "0") //parus bug workarround
    });

    if (dataType == "2") dojo.mixin(params, {
      BEGDATEVAL: parus.formats.dateToString(this.begDateValueEdit.attr("value")),
      MINDATEVAL: parus.formats.dateToString(this.minDateValueEdit.attr("value")),
      MAXDATEVAL: parus.formats.dateToString(this.maxDateValueEdit.attr("value")),

      TRIMSTRVAL: (this.trimStrCheckBox.attr("checked") ? "1" : "0") //parus bug workarround
    });

    return params;
  },

  executeInsert: function() {
    var response = parus.database.execute("designer.BalanceIndexes.Insert", dojo.mixin(this.getExecParams(), {
      CRN: this.crn
    }));

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

  executeUpdate: function() {
    var response = parus.database.execute("designer.BalanceIndexes.Update", dojo.mixin(this.getExecParams(), {
      RN: this.rn
    }));

    return !!response;
  },

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

    return !!response;
  }

});