
function FormValidator( formName ) {
	this.validationForm = document.forms[formName];
	this.isValid = true;
	
	var rules = new Array(); 

	this.addValidationRule = function( rule ) {
		rules[rules.length] = rule;
	}
	
	this.validate = function() {
		this.isValid = true;
		for (var r = 0; r < rules.length; r++) {
			this[rules[r].func](rules[r].fieldName, rules[r].fieldLabel);
		}
		
		console.debug(rules);
		return this.isValid;
	}
	
	function _checkRegExp(f, fieldName, fieldLabel, exp, errorMsg) {
		var frmField = f[fieldName];
		var msgField = $('#' + fieldName + '_message' );
		
		frmField.value = frmField.value.toLowerCase().trim();
		
		if ( frmField.value && !frmField.value.match(exp) ) {
			msgField.css({display:"table-cell"});
			msgField.html( errorMsg );
			this.isValid = false;
			return false;
		} else {
			msgField.html( 'Please enter a value for ' + fieldLabel );
			msgField.hide();
		}
		return true;
	}
	
	/**
	 * Check that the field contains a valid directory name 
	 **/
	this.checkDirectoryName = function(fieldName, fieldLabel) {
		return _checkRegExp(this.validationForm, fieldName, fieldLabel, /^[a-z0-9_\-]+$/, 
				'Please enter a valid value for ' + fieldLabel + '. Use only [a-z0-9_-] characters' );
	}
	
	this.checkEmail = function(fieldName, fieldLabel) {
		return _checkRegExp(this.validationForm, fieldName, fieldLabel, /^[^\s@,;]+@[^\s@,;]+$/, 
				'Please enter a valid email address' );
	}
	
	this.checkPostcode = function(fieldName, fieldLabel) {
		return _checkRegExp(this.validationForm, fieldName, fieldLabel, /^[a-zA-Z]{1,2}[0-9]{1,2} ?[0-9]{1,2}[a-zA-Z]{1,2}$/, 
				'Please enter a valid postcode' );
	}
	
	this.checkNumber = function(fieldName, fieldLabel) {
		var frmField = this.validationForm[fieldName];
		frmField.value = parseFloat(frmField.value);
		return _checkRegExp(this.validationForm, fieldName, fieldLabel, /^[0-9\.]*$/, 
				'Please enter a valid number' );
	}
	
}

