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


var InviteFormObserver = 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.number = this.form.getElement('input[type=text]');
    this.button = this.form.getElement('.proceed');

    this.attachEvents();
  },
    
  attachEvents: function() {
    this.form.set('send', {
      onSuccess: this.success,
      onFailure: this.failure,
      onComplete: function() {
        this.button.set('disabled', false);
        Loader.hide();
      }.bind(this)
    });

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

  /**
   * Very simple input validation, only rejects blanks.
   */
  send: function() {
    if(this.getNumber().trim() != '') {
      Loader.show();
      this.button.set('disabled', true);
      this.form.send();
    }
    return false;
  },

  success: function(redirectUrl) {
    window.location.href = redirectUrl;
  },

  failure: function() {
    this.infoPane.set('text', i18n.invite['not_found'] + ' ' + this.getNumber());
    this.number.addClass('wrong');
  },

  getNumber: function() {
    return this.number.get('value');
  }
    
});

