/* Ajax Construction Kit  */
/* Validation Toolkit Functions  */

var Validator = new Class.create({

    validateNonEmpty: function(inputControl, helpControl) {
        // See if the input value contains any text
        return this.validateRegEx(/.+/,
            inputControl.value, helpControl,
            "Please enter a value.");
    },
    
    validateNumber: function(inputControl, helpControl) {
        // First see if the input value contains data
        if (!this.validateNonEmpty(inputControl, helpControl))
            return false;
    
        // Then see if the input value is a number
        return this.validateRegEx(/^[-]?\d*\.?\d*$/,
            inputControl.value, helpControl,
            "Please enter a number.");
    },
    
    validateEmail: function(inputControl, helpControl) {
        // First see if the input value contains data
        if (!this.validateNonEmpty(inputControl, helpControl))
            return false;
    
        // Then see if the input value is an email address
        return this.validateRegEx(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
            inputControl.value, helpControl,
            "Please enter an email address (for example, john@doe.com).");
    },
    
    validateDate: function(inputControl, helpControl) {
        // First see if the input value contains data
        if (!this.validateNonEmpty(inputControl, helpControl))
            return false;
    
        // Then see if the input value is a date
        return this.validateRegEx(/(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d/,
            inputControl.value, helpControl,
            "Please enter a date (for example, 01/14/1975).");
    },
    
    validateRegEx: function(regex, input, helpControl, helpMessage) {
      if (!regex.test(input)) {
        if (helpControl != null)
          helpControl.innerHTML = helpMessage;
        return false;
      }
      else {
        if (helpControl != null)
          helpControl.innerHTML = "";
        return true;
      }
    }
});
