
Kwo.Class.Form =  Class.create({

  form: null,

  initialize: function(elt) {
    this.form = $(elt);
    this.form.observe("submit", this.onSubmit.bindAsEventListener(this));
  },

  onSubmit: function(evt) {
    evt.stop();
    /*if (!Kwo.isAuth()) {
      var auth = new Kwo.Class.Auth();
      auth.onCallback = this.onSubmit.bind(this).curry(evt);
      return false;
    }*/
    this.form.select(".form-field").each(function (elt) {
      elt.removeClassName("form-field-failure");
    });
    Kwo.exec(this.form.readAttribute("action"), this.form, 
             {callback: this.onSubmitCallback.bind(this),
              disable: this.form});
    return false;
  },

  onSubmitCallback: function(res) {
    if (!("failures" in res["result"])) {
      this.form.hide();
      this.form.next("DIV").update(res["result"]["ack"]).show();
      return ;
    }
    var that = this;
    res["result"]["failures"].each(function(field_id) {
      that.form.down(".form-field-" + field_id).addClassName("form-field-failure");
    });
  }

});

