function Validator() {}

Validator.prototype = new MsgMgr();
Validator.prototype.constructor = MsgMgr;
Validator.superClass = MsgMgr.prototype;

/**
 * @param field - really the id of the field
 * @param Array(validators) array of the validations required
 */
Validator.prototype.validate = function(field, validators) {
	
	var emailRegex = /^([^@\s]+)@((?:[-a-zA-Z0-9]+\.)+[a-zA-Z]{2,})$/;

	if (!$(field)) {
		throw new Error ('Field ' + field + ' does not exist', 'Field ' + field + ' does not exist');
	}

	// first trim the field - if its text

	// for each of the validators required on this field
	for(i=0;i<validators.length;i++) {

		var type = validators[i].type;

		switch(validators[i].type) {

			case 'required':

				// if not value is provided
				if (!$F(field)) {
					title	= validators[i].title || '';
					text 	= validators[i].text || ' - ' +  field + ' is required';
					level	= validators[i].level || '';
					
					// add the error message
					this.add(new Msg({id:field, title:title, text:text, level:level}));

					// return from this validation
					return false;
				}
				break;

			case 'checked':
			
				// if the field is not checked
				if (!$(field).checked) {
					title	= validators[i].title || '';
					text 	= validators[i].text || ' - ' +  field + ' is required';
					level	= validators[i].level || '';
					
					// add the error message
					this.add(new Msg({id:field, title:title, text:text, level:level}));

					return false;
				}

				break;

			case 'email':

				if (!($F(field).match(emailRegex))) {

					title	= validators[i].title || '';
					text 	= validators[i].text || ' - The email address is invalid';
					level	= validators[i].level || '';
					
					// add the error message
					this.add(new Msg({id:field, title:title, text:text, level:level}));

					// return from this validation 
					return false;
				}

				break
		}
	}

	// if we have got to here - ensure we remove any errors relating to this field
	this.clearMsgById(field);

	// return true allowing validation success
	return true;
}
