var FormCheck = {

  elements: [],
  errors: [],
  init: function(formElement) {
    var t = this;
    this.errorbox = arguments[1];
    this.form = formElement;
    this.form.submit(
      function() {
        return t.checkFields();
      }
    );

    $(".req", this.form).each(
      function() {
        t.registerField($(this));
      }
    );

    if (this.errorbox) {
      //$("input[@type='submit']", this.form).before('<div class="form_errors"></div>');
      $("p.submit", this.form).before('<div class="form_errors"></div>');
      this.errorbox = $("div.form_errors", this.form);
      this.errorbox.hide();
    }

  },

  registerField: function(field) {
    var label = $("label[@for='" + field.id() + "']");
    this.elements[this.elements.length] = {
      el: field,
      type: field.is("div") ? 2 : 1,
      label: label,
      text: label.find("span").text()
    }
  },

  checkFields: function() {
    this.errorbox ? this.errorbox.hide() : null;
    var error = false;
    var els = this.elements;
    for (i in els) {
      if (els[i].type == 1 && els[i].el.val() == "" || els[i].type == 2 && $("input:checked", els[i].el).size() < 1) {
        this.errors[this.errors.length] = Translations.error_1 + ": <strong>" + els[i].text + "</strong>";
        els[i].label.addClass("error");
      }
      else {
        $(els[i].label).removeClass("error");
      }
    }
    if (this.errors.length) {
      if (this.errorbox) {
        var text = '<p class="errors_title">' + Translations.error_0 + ':</p>';
        for (i in this.errors) {
          text += "<p>" + this.errors[i] + "</p>";
        }
        this.errorbox.html(text).show();
      }
      else {
        text = Translations.error_0 + ": \n\n" ;
        for (i in this.errors) {
          text += "· " + $(this.errors[i]).text() + "\n";
        }
        alert(text);
      }
      this.errors = [];
      return false;
    }
  }

}
