// ---------------------------------------------------------------
/**
 * Validator.js
 * 
 * A singleton online/form validation class.
 * Provide simple routines for form validation and
 * provide simple stategy rules for various types.
 */
// vt = V for Validator, or Vendetta
var vt = {	
	// ---------------------------------------------------------------
	/**
	 * Verify that the given string is not empty
	 * 
	 *@param String - Any string value
	 */
	notEmpty: function(value) {
		return value.trim().length > 0;	
	},
	// ---------------------------------------------------------------
	/**
	 * Verify that the given string is an email address
	 * 
	 *@param String - Email address to validate
	 */
	validEmail: function(value) {	
		value = value.trim();
		var pattern = /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/i;
		return pattern.test(value); 
	},
	// ----------------------------------------------------------------
	/**
	 * Verify that this is a valid string of comma delimited emails
	 *
	 *@param String - Comma seperated string of email addresses
	 */
	validEmailList: function(value) {
		var arr = value.split(",");
		if(arr.length == 1) {
			return this.validEmail(value);
		}
		for(var i = 0; i < arr.length; i++) {
			var val = arr[i].trim();
			if(!this.validEmail(val)) {
				return false;
			}
		}
		return true;
	},
	// ---------------------------------------------------------------
	/** 
	 * Run a verification on a form element and update the status icon
	 *
	 *@param value - Input string value to validate
	 *@param func - Function pointer for validation rules
	 *@param ref - ID name of html element to update status (Optional)
	 *
	 *@return Boolean - Whether the value passed the test
	 */
	validate : function(value, func, ref) {
		// Set state variables
		var pass = 'validation-status-pass';
		var fail = 'validation-status-fail';
	
		// Find the status element
		var status = $("#superbox").find("#" + ref);

		// Did it pass the validator method?
		if(func.call(this, value)) {
			if(ref != null && !status.hasClass(pass)) {
				status.hide();
				status.removeClass(fail);
				status.addClass(pass);
				// Can't fade within superbox in IE
				if($.browser.msie) {
					status.show();
				} else {
					status.fadeIn();
				}
			}
			return true;
		} else {
			if(ref != null && !status.hasClass(fail)) {
				status.hide();
				status.removeClass(pass);
				status.addClass(fail);
				// Can't fade within superbox in IE				
				if($.browser.msie) {
					status.show();
				} else {
					status.fadeIn();
				}
			}
			return false;
		}
	}
}