var AdvantageWishDetails = Class.create();
AdvantageWishDetails.prototype = {
  initialize: function() {
    this.validationErrors = [];
  },
  validate: function() {
    if ($F('advantageWishFirmName').strip() == "") {
      this.addError('Palun lisa koha nimi või kirjeldus');
    }
  },
  addError: function(error) {
    this.validationErrors.push(error);
  },
  hasErrors: function() {
    return this.validationErrors.size() > 0;
  },
  showErrors: function() {
    this.validationErrors.each(function(error) {
      $('advantagesWishDetailsMessagesContainer').appendChild(Builder.node('p', {className: 'error'}, error));
    });
  },
  clearMessages: function() {
    this.validationErrors.clear();
    $('advantagesWishDetailsMessagesContainer').update();
  }
};

function initAdvantageWishDetailsForm() {
  var details = new AdvantageWishDetails();
  if ($('advantagesWishDetailsForm')) {
    $('advantagesWishDetailsFormCommandButton').enable();
    $('advantageWishFirmName').focus();

    $('advantagesWishDetailsForm').observe('submit', function(event) {
      $('advantagesWishDetailsForm').hide();
      details.clearMessages();
      details.validate();
      if (details.hasErrors()) {
        details.showErrors();
        Event.stop(event);
        $('advantagesWishDetailsForm').show();
      }
      else {
        $('advantagesWishDetailsForm').command.value = "send";
      }
    });
  }
}
addOnLoadListener(initAdvantageWishDetailsForm);
