/**
 * Controller for the invoice web form
 *
 * @author MW, ELO Digital Office GmbH
 * @version 1.0
 *
 * @elowf
 * @requires sol.contract.forms.Contract
 */
sol.define("sol.contract.forms.Controller", {

  /**
   * Event function which is called when an input value has been changed.
   * @param {input} source Changed HTML element.
   */
  inputChanged: function (source) {
    var me = this;

    if (!source) {
      ELO.Configuration.ShowScriptErrors = true;
      ELO.Configuration.PrintAllTabs = true;
      me.contract = sol.create("sol.contract.forms.Contract");
      sol.common.forms.Utils.callRegisteredFunction("RF_sol_common_service_GetMergedConfig", {
        compose: "/contract/Configuration/contract.config"
      }, function (result) {
        me.contract.config = result.config;
        sol.common.forms.Utils.registerResetNewLineFunction("WF_MAP_USER", true);
        sol.common.forms.Utils.disableRowsByIndicatorField("WF_MAP_USER_MANDATORY", function (rowIndex) {
          return !!$val("WF_MAP_USER" + rowIndex) && !!$val("WF_MAP_USER_MANDATORY" + rowIndex);
        });
        me.inputChangedOrFormLoaded(source);
      });
    } else {
      me.inputChangedOrFormLoaded(source);
    }
  },

  /**
   * @private
   * @param {input} source Changed HTML element.
   */
  inputChangedOrFormLoaded: function (source) {
    var me = this, toggleField;

    if ($val("IX_MAP_DEBUG")) {
      if (!source) {
        sol.common.forms.Utils.removeBodyClass("permanent_active");
        sol.common.forms.Utils.removeBodyClass("fixedterm_active");
        sol.contract.forms.Utils.setReadOnly("IX_MAP_NEXT_POSSIBLE_CONTRACT_END", false);
      }
      return;
    }

    me.userListChanged();

    if (sol.contract.forms.Utils.fieldExists("IX_MAP_DURATION_TYPE")) {
      me.contract.setDurationTypeFields(source);
    }

    if (sol.contract.forms.Utils.fieldExists("IX_GRP_CONTRACT_START")) {
      me.contract.calcContract(source);
    }
    if (sol.contract.forms.Utils.fieldExists("IX_GRP_CONTRACT_CASHFLOW_SUM")) {
      me.contract.calcPaymentPlan(source);
      me.contract.calcAmounts(source);
    }
    if (sol.contract.forms.Utils.isField(source, "IX_MAP_CONTRACT_CASHFLOW_TO")) {
      sol.contract.forms.Utils.calcDateByDuration("IX_MAP_CONTRACT_CASHFLOW_DATE", source.name);
    }

    if (me.contract.config.fixedTerm && me.contract.config.fixedTerm.regular && me.contract.config.fixedTerm.regular.withTerminationPoint) {
      sol.common.forms.Utils.removeFieldClass("IX_MAP_TERMINATION_POINT", "fixedregular_disable");
      sol.common.forms.Utils.removeFieldClass("LBL_TERMINATION_POINT", "fixedregular_disable");
    }
    
    me.contract.togglePaymentDataFields(source);
  },

  /**
   * @private
   */
  userListChanged: function () {
    if ($var("WF_MAP_USER_ID1")) {
      sol.common.forms.Utils.applyUserImages("WF_MAP_USER", "WF_MAP_USER_ID");
    }
  },

  /**
   * Event function which is called when the user has pressed the save button.
   * @return {Boolean} Should the values be saved?
   */
  saveClicked: function () {
    var me = this;

    me.validationData = { allowEmptyNames: true };

    return true;
  },

  /**
   * Event function which is called when the user has pressed a forward button.
   * @param {String} id
   * @return {Boolean}
   */
  nextClicked: function (id) {
    return true;
  },

  addLineIds: {
    payment: 1
  },

  /**
   * Event function which is called when the user has pressed a button to remove a table line.
   * @param {String} addLineId
   * @param {String} groupIndex
   * @return {Boolean}
   */
  removeLineClicked: function (addLineId, groupIndex) {
    var me = this;

    if (addLineId == me.addLineIds.payment) {
      $update("IX_MAP_CONTRACT_CASHFLOW_SINGLE" + groupIndex, "");
      $update("IX_MAP_CONTRACT_CASHFLOW_AMOUNT" + groupIndex, "");
      me.contract.calcPaymentPlan();
    }

    return true;
  },

  onDynListItemSelected: function (dynListItem) {
    sol.common.forms.Utils.callInputChangedForDynKwlChanges(dynListItem);
  },

  validateName: function () {
    var me = this,
        result;

    if (me.validationData && me.validationData.allowEmptyNames) {
      return "";
    }

    result = me.contract.validateName();
    return result;
  },

  /**
   * Registers an update job using the global update registry.
   */
  registerUpdate: function () {
    sol.common.forms.Utils.registerUpdate();
  }
});