function Validator(objToValidate, errorCol, subClass)
{
	// Declaration
	if (typeof(objToValidate) == "undefined")
		throw new Error("objToValidate hasn't to be undefined.");

	var _objToValidate = objToValidate;
	
	var _errorCollection = errorCol;

	if (typeof(subClass.Validate) == "undefined")
		throw new Error("The inhered class has to implement a _validate method.");

	this.Validate = subClass.Validate;
	this.ErrorMessage = "*";
	this.ObjectToValidate = _objToValidate;
	this.p_get_ObjToValidate = p_get_ObjToValidate;
	this.p_onError = p_onError;

	function p_onError(error)
	{
		_errorCollection.Add(this, error);
	}
	
	function p_get_ObjToValidate()
	{
		return _objToValidate;
	}
}

function NotEmptyStringValidator(objToValidate, errorCol)
{
	this.Validate = _validate;
	this.ErrorMessage = "";

	this.prototype = new Validator(objToValidate, errorCol, this);
	var base = this.prototype;

	function _validate()
	{
		var obj = base.p_get_ObjToValidate();
		if (obj.value == "")
		{
			base.p_onError(this.ErrorMessage);
		}
	}
}

function DateValidator(objToValidate, errorCol)
{
	this.Validate = _validate;
	this.ErrorMessage = "";

	this.prototype = new Validator(objToValidate, errorCol, this);
	var base = this.prototype;

	function _validate()
	{
		var obj = base.p_get_ObjToValidate();
		var dateStr = obj.value;
		if (dateStr != "")
		{
			var regex = /([0-9]{1,2}).([0-9]{1,2}).[0-9]{2}([0-9]{2})/;
			if (!regex.test(dateStr))
				base.p_onError("Bitte Datum im TT.MM.YYYY eingeben.");
			else
			{
				var matches = regex.exec(dateStr);				
				var cDate = new Date();
				cDate.setDate(Number(matches[1]));
				cDate.setMonth(Number(matches[2]) - 1);
				cDate.setYear(Number(matches[3]));
				
				if (cDate.getDate() != Number(matches[1]) ||
					cDate.getMonth() != Number(matches[2]) - 1 ||
					cDate.getYear() != Number(matches[3]))
				{
					base.p_onError("Datum existiert nicht.");					
				}
			}
		}
	}
}

function RegexStringValidator(objToValidate, errorCol)
{
	this.Validate = _validate;
	this.ErrorMessage = "";
	this.RegexExpression = "";

	this.prototype = new Validator(objToValidate, errorCol, this);
	var base = this.prototype;

	function _validate()
	{
		var obj = base.p_get_ObjToValidate();
		if (!obj.value.match(this.RegexExpression))
		{
			base.p_onError(this.ErrorMessage);
		}
	}	
}

/*
function AjaxValidator(methodUrl, objToValidate, flats, errorCol, errorFunc)
{
	this.Validate = _validate;
	alert(flats);
	var _flats = flats;

	this.prototype = new Validator(objToValidate, errorCol, this);
	var base = this.prototype;
	var _ajax = p_createRequestObject();

	function _validate()
	{
		var obj = base.p_get_ObjToValidate();

		_ajax.open("get", methodUrl + "?value=" + obj.value);
    _ajax.onreadystatechange = p_onResult;
    _ajax.send(null);
	}
	
	function p_onResult()
	{
		if (_ajax.readyState == 4)
		{
			//alert(_ajax.responseXML.documentElement);
			for (var i = 0; i < _flats.length; i++)
			{
				_flats[i].disabled = true;
				for (var a = 0; a < _ajax.responseXML.documentElement.childNodes.length; a++)
				{
					if (_ajax.responseXML.documentElement.childNodes.item(a).attributes.getNamedItem("number").nodeValue == String(i))
					{
						_flats[i].disabled = false;
						break;
					}
				}
			}
			if (_ajax.responseText != "true")
				base.p_onError(base.p_get_ObjToValidate() + " ist nicht gültig.");
		}
	}

	function p_createRequestObject() 
	{
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    else
        ro = new XMLHttpRequest();

    return ro;
	}	
}
*/
function ErrorCollection()
{
	// Declaration
	var _errors = new Array();

	this.Add = _add;
	this.ClearKey = _clearKey;
	this.Clear = _clear;
	this.Print = _print;
	this.HasError = _hasError;
	this.OnError;
	this.OnClear;
	
	function _hasError()
	{
		return _errors.length > 0;
	}
	
	function _add(sender, error)
	{
		_errors.push(error);

		if (typeof(this.OnError) == "function")
			this.OnError(sender, error);
	}
	
	function _remove(validator)
	{
		
	}
	
	function _clear()
	{
		_errors = new Array();
	}
	
	function _clearKey(sender, key)
	{
		if (typeof(this.OnClear) == "function")
			this.OnClear(sender, key);		
	}

	function _print(outputFunc)
	{
		if (typeof(outputFunc) != "function")
			throw new Error("outputFunc has to be a function.");

		for (var i = 0; i < _errors.length; i++)
			outputFunc(_errors[i]);
	}
}
