var _Validator = function()
{
	this.errors = new Array();
	this.tests = new Array();
	
	this.test = function()
	{
		this.clear_errors();
		this.errors = new Array();
		var scope = this;
		for(var test in this.tests)
		{
			var test1 = this.tests[test];
			var validator = this.validators[test1[1]];
			validator(test1[0],scope);
		}
		if (this.errors.length != 0)
		{
			this.display_errors();
		}
		return (this.errors.length == 0)
	}
	
	this.display_errors = function()
	{
		for(var i in this.errors)
		{
			
			var err_field = this.errors[i][0] + '_error';
			var errors = '';
			$(err_field).innerHTML += this.errors[i][1] + '<br />';
			$(this.errors[i][0] + '_class').className = 'field error';
		}
	}
	
	this.clear_errors = function()
	{
		for(var test in this.tests)
		{
			var test1 = this.tests[test];
			$(test1[0] + '_error').innerHTML = '';
			$(test1[0] + '_class').className = 'field';
		}
	}
	
	this.add_error = function(field, message,scope)
	{
		this.errors.push([field,message]);
	}

	return this;
	
}
_Validator.prototype.validators = new Array();

_Validator.prototype.validators['validates_presence_of'] = function(field,scope)
{
	if ($(field).value == '')
	{
		scope.add_error(field, 'Не заполнено',scope);
		return false;
	}
	return true;
}

_Validator.prototype.validators['validates_email'] = function(field,scope)
{
	var reg = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if (!reg.test($(field).value))
	{
		scope.add_error(field,'Неверный формат',scope);
		return false;
	}
	return true;
}


/*Подписка на новости*/
var SubscribeValidator = new _Validator();

SubscribeValidator.tests = [
	['subscribe_name', 'validates_presence_of'],
	['subscribe_email', 'validates_email']
];

