/**
 * Form class
 * Contains basic functions for checking various input fields
 * 
 * copyright 2008 06 24 - Huug Helmink
 * 
 */
var Form = new Class({
  /**
   * Options
   */
  Implements: Options,
  options: {
    emailClass: 'emailFld',
    phoneClass: 'phoneFld',
    requiredClass: 'reqFld',
    uriClass: 'uriFld',
    warningClass: 'fldWarning'
  },
  
  /**
   * Initialize the Form class
   *
   * @param form    The form to attach the class
   * @param options Additional options
   */
  initialize: function(form,options) {
    this.setOptions(options);
    
    this.form = form;
    this.ev = $empty; // Event container
    this.send = false; // Boolean to check if the form has to be send
    this.regEx = $empty; // A regular expression string
  },
  
  /**
   * Checks if the form can be send, otherwise stop form submit and alert a message
   *
   * @param msg   Message to alert when the form can't be send
   */
  checkSend: function(msg) {
    if (this.send == false) {
      this.ev.stop();
      alert(msg);
    }
    this.ev = $empty; // Clear up the Event container
    this.regEx = $empty; // Clear reqular expression
  },
  
  /**
   * Checks if all required fields are filled
   */
  requiredFields: function() {
    this.form.addEvent('submit',function(e) {
      this.ev = new Event(e);
      this.send = true;
      
      this.form.getElements('.' + this.options.requiredClass).each(function(fld) {
        if (fld.hasClass(this.options.warningClass)) fld.removeClass(this.options.warningClass);
        if (fld.get('value') == '') {
          fld.addClass(this.options.warningClass);
          this.send = false
        }
      },this);
      
      this.checkSend('Niet alle velden zijn ingevuld');
    }.bindWithEvent(this));
  },
  
  /**
   * Check for a valid email address
   */
  checkEmail: function() {
    this.regEx = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    
    this.form.addEvent('submit',function(e) {
      this.ev = new Event(e);
      this.send = true;
      
      this.form.getElements('.' + this.options.emailClass).each(function(fld) {
        if(!fld.get('value').test(this.regEx)) {
          fld.addClass(this.options.warningClass);
          this.send = false
        }
      },this);
      
      this.checkSend('U heeft geen geldig e-mail adres ingevuld');
    }.bindWithEvent(this));
  },
  
  /**
   * Checks for a valid phone number
   */
  checkPhone: function() {
    this.regEx = /^[0-9 \(\)\+\-]{10,20}$/;
    
    this.form.addEvent('submit',function(e) {
      this.ev = new Event(e);
      this.send = true;
      
      this.form.getElements('.' + this.options.phoneClass).each(function(fld) {
        if(!fld.get('value').test(this.regEx)) {
        alert(fld.get('value'));
          fld.addClass(this.options.warningClass);
          this.send = false
        }
      },this);
      
      this.checkSend('U heeft geen geldig telefoonnummer ingevuld');
    }.bindWithEvent(this));
  }//,
  
  /**
   * Check URI
   */
  /*checkURI: function() {
    this.regEx = /^((((https?|ftps?|gopher|telnet|nntp)://)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';\/?:@&=+$,A-Za-z0-9])+)([).!';\/?:,][[:blank:]])?$/;
    
    this.form.addEvent('submit',function(e) {
      this.ev = new Event(e);
      this.send = true;
      
      this.form.getElements('.' + this.options.uriClass).each(function(fld) {
        if(!fld.get('value').test(this.regEx)) {
          fld.addClass(this.options.warningClass);
          this.send = false
        }
      },this);
      
      this.checkSend('U heeft geen geldig URL ingevuld');
    }.bindWithEvent(this));
  }*/
});
