// Validator object used for validation
//
// id is the ElementId by which getElementById can get the html element
// fieldName is the name to be used in error messages and the like
// required is a boolean value, indicating if the field is required.
//		if it's required then the field cannot be null or empty string
// validationFunction: a validation function, or an array containing validation functions
//		as currently defined, validation functions return a boolean value and have one 
//		string parameter.
function Validator(id, fieldName, required, validationFunction) 
{
	// set values
	this.id = id;
	this.name = fieldName;
	this.required = required == null ? false : required;
	this.ValidationFunction = validationFunction == '' ? null : validationFunction;
	// Default values
	this.isValid = false;
	this.error = "";
	
	// set Functions
	this.Validate = Validate;
	
	function Validate()
	{
		var element = document.getElementById(this.id);
		if(element == null) {
			this.isValid = false;
			return this.isValid;
		} else this.isValid = true;
		
		var value = element.value;
		
		if(this.required && value == '') {
			this.error += "<li>" + this.name + " is required.</li>";
			this.isValid = false;
		}
		
		if( this.isValid && value != '' && this.ValidationFunction != null) {
			
			if( this.ValidationFunction.sort !=  null && this.ValidationFunction.length != null ) {
				for(var i = 0; i < this.ValidationFunction.length; i++)	{
					if( !this.ValidationFunction[i](value) ) {
						this.error += "<li>" + this.name + " is invalid, please correct it.</li>";
						this.isValid = false;
						break;
					}
				}
			} else {
				if( !this.ValidationFunction(value) ) {
					this.error += "<li>" + this.name + " is invalid, please correct it.</li>";
					this.isValid = false;
				}
			}
		}
		return this.isValid;
	}
}

function ValidateEmail(text)
{
	if(text == null || text == '') return false;
	var regex = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	return regex.test(text);
}

function IsFreeEmail(text)
{
	return false;
}

function ValidatePhone(text)
{
	return true;
}

function ValidateUri(text)
{
	return true;
}
