// объект для запросов 
var cHydraFormValidator = function()
{
	this.container = false;
	this.result = true;
	this.error_message = '';
}


// метод инициализации объекта для запросов
cHydraFormValidator.prototype.init = function()
{
}

cHydraFormValidator.prototype.set_container = function(container_id)
{
	this.container = document.getElementById(container_id);
}

cHydraFormValidator.prototype.validate = function()
{
	var collection = this.container.getElementsByTagName('input');
	this.process_collection(collection);

	var collection = this.container.getElementsByTagName('textarea');
	this.process_collection(collection);

	var collection = this.container.getElementsByTagName('select');
	this.process_collection(collection);

	if (!this.result) 
	{
		alert(this.error_message);    
	}
	
	return this.result;
}

cHydraFormValidator.prototype.process_collection = function(collection)
{
	if (!collection) 
	{
		return false;
	}

	for (i = 0; i < collection.length; i++) 
	{
		var val = collection[i].value;

		if ((collection[i].type=='password') && (collection[i].getAttribute('confirm')==null) && (collection[i].disabled != true)) 
		{
			var confirm_elem = collection[i].nextSibling;
			
			if (confirm_elem.value!=collection[i].value) 
			{
				collection[i].style.background = "#f28d3a";
				confirm_elem.style.background = "#f28d3a";
				this.result = false;
				this.error_message += 'Введенный пароль не совпадает с подтвержением \n';  
			}

			if (collection[i].value == '') 
			{
				collection[i].style.background = "#f28d3a";
				confirm_elem.style.background = "#f28d3a";
				this.result = false;
				this.error_message += 'Нельзя сохранить пустой пароль \n';  
			}

		}

		if (collection[i].not_null) 
		{
		  if (val.length==0) 
		  {
			  collection[i].style.background = "#f28d3a";
			  this.result = false;
			  this.error_message += 'Значение должно быть задано для поля '+':'+collection[i].caption+'\n';
		  }
		}
		var pattern = collection[i].getAttribute('pattern');
		if (pattern) 
		{
			if (!val.match(pattern)) 
			{
				this.result = false;
				this.error_message += collection[i].ErrorMsg+':'+collection[i].caption;
			}
		}

	}

}
