/**
*   == Script - invite_request_form_observer.js
*       Observes the invite request form.
*
*   == Author
*       Kevin Valdek (CannedApps) - created 2009-09-25
*/


var InviteRequestFormObserver = new Class({
    
  Implements: [Options],

  Binds: ['send', 'success', 'failure'],

  options: {

  },

  initialize: function(form, options) {
    this.setOptions(options);
    this.form = $(form);
    this.infoPane = this.form.getElement('.info');
    this.email = this.form.getElement('input[type=text]');

    this.attachValidator();
    this.attachEvents();
  },

  attachValidator: function() {
    this.validator = new UnobtrusiveFormValidator(this.form);

    // customize errors
    var list = this.validator.list.removeClass('error_list').dispose();
    list.inject(this.form.getElement('.error_list_wrapper'));
  },
    
  attachEvents: function() {
    this.form.set('send', {
      onSuccess: this.success,
      onFailure: this.failure
    });

    this.form.addEvent('submit', this.send);
  },

  send: function() {
    if(this.validator.validate()) {
      Loader.show();
      this.form.send();
    }
    else {
      this.email.addClass('wrong');
    }
    return false;
  },

  success: function(loadUrl) {
    var wrapper = this.form.getParent('.wrapper');

    new Request.HTML({
      url: loadUrl,
      update: wrapper,
      onSuccess: function() {
        Loader.hide();
        ActionChain.apply(wrapper);
      }
    }).get();
  }
    
});

