var Validate = function()
{

	return {

		_messages: {
			'empty' : 'Не может быть пустым',
			'int' : 'Только целочисленное значение',
			'float' : 'Только число или десятичная дробь',
			'email' : 'Неправильный формат E-mail',
			'url' : 'Неправильный формат URL',
			'phone' : 'Неправильный формат номера',
			'confirm' : 'Введенные данные не совпадают',
			'exists' : 'Уже используется',
			'valid' : ''
		},
		_only_check: null,
		_fields: null,
		_submit: 'submit_btn',
		_objects: {
			'display' : ['indicator',function(obj,value){ obj.setProperty('src','/design/standart/images/indicator'+value+'.gif') }],
			'message' : ['message',function(obj,value){ obj.set('html',value) }],
			'block' : ['pre_msg',function(obj,value){ obj.setStyle('display',value) }]
		},
		_send_callback: function(r){ ($type(Interface)=='object'?Interface.callback_set_value(r):'') },

		_empty: function(v){ return !(v==null || v.length==0 || v==0 || /^\s+$/.test(v)) },
		_int: function(v){ return /^\d+$/.test(v) },
		_float: function(v){ return /^[\d\.\,]+$/.test(v) },
		_email: function(v){ return this._empty(v) && /^[a-zA-Z0-9][a-zA-Z0-9_\-\.]+\@[a-zA-Z0-9\-_\.]+\.[a-zA-Z]{2,6}$/.test(v) },
		_url: function(v){ return this._empty(v) && /^((http|https|ftp):\/\/)?(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(v) },
		_phone: function(v){ return this._empty(v) && /^[0-9\(\)\-\s\+]+$/.test(v) },
		_confirm: function(v,v1){ return v==v1 },

		init: function()
		{
			var that = this;
			window.addEvent('domready',function(){
				this._submit=$(that._submit);
				this._fields = document.getElements( 'input[rel^=validate:],textarea[rel^=validate:]' );

				if( $type( this._fields )=='array' )
					this._fields.each(function(e){
						if(e.getProperty('value')) that.check(e,1);
						e.addEvent('blur',function(){ that.check(this) });
					});

				if($type(this._submit)=='element')
					this._submit.addEvent('click',function(){
						var count_of_err=0; that.result(0);
						this._fields.each(function(e){ if(!that.check(e)) count_of_err++ });
						that.result(count_of_err); !count_of_err?this._submit.getParent('form').submit():'';
					}.bind(this));
			});
		},

		check: function(obj,mode)
		{
			this._only_check=mode;
			var data=obj.getProperty( 'rel' ).replace( /^validate:/,'').split( ',' );
			var type=data[0],param=data[ 1 ], value=data[2];

			switch(type)
			{
				case 'empty':
					return this._empty(obj.getProperty('value')) ? this.valid(obj,param,value) : this.invalid(obj,type);
					break;
				case 'int':
					return this._int(obj.getProperty('value')) ? this.valid(obj,param,value) : this.invalid(obj,type);
					break;
				case 'float':
					return this._float(obj.getProperty('value')) ? this.valid(obj,param,value) : this.invalid(obj,type);
					break;
				case 'phone':
					return this._phone(obj.getProperty('value')) ? this.valid(obj,param,value) : this.invalid(obj,type);
				case 'email':
					return this._email(obj.getProperty('value')) ? this.valid(obj,param,value) : this.invalid(obj,type);
					break;
				case 'url':
					return this._url(obj.getProperty('value')) ? this.valid(obj,param,value) : this.invalid(obj,type);
					break;
				case 'confirm':
					return this._confirm(obj.getProperty('value'),obj.form.elements[param].value) ? this.valid(obj,param,value) : this.invalid(obj,type);
					break;
				default:
					this.valid(obj,param,value);
					break;
			}
		},

		valid: function(obj,param,value){
			var name=obj.getProperty('name');
			var inf=name.match( /([a-zA-Z0-0\_]+)\[([^\]]+)\]/ );
			var params='';

			if($type(inf)=='array')
				params='form='+inf[1]+'&field='+inf[2]+'&value='+obj.getProperty('value');

			this.alerter(name,1);

			if(param=='ajax' && this._empty(value))
			{
				var res=false;

				new Request({
					async:false,
					url:value+obj.getProperty('value'),
					onSuccess:function(r){ res=(r==1?this.invalid(obj,'exists'):true) }.bind(this)
				}).send(params);

				return res;
			}
			else if(param=='send' && this._empty(value) && !this._only_check)
			{
				if(obj.get('ref')!=obj.get('value'))
				{
					new Request({
						url:value+obj.get('value'),
						onSuccess:this._send_callback
					}).send(params);
					obj.set('ref',obj.get('value'));
				}
				return true;
			}

			return true;
		},

		invalid: function(obj,err){ this.alerter(obj.getProperty('name'),3,err); return false },

		alerter: function(target,type,err)
		{
			var display=$(this._objects['display'][0]+'['+target+']');
			if($type(display)=='element')
				this._objects['display'][1](display,(err?3:1));
			var message=$(this._objects['message'][0]+'['+target+']');
			if($type( message )=='element')
				this._objects['message'][1](message,this._messages[err?err:'valid']);
		},

		result: function(mode)
		{
			var msg=$(this._objects['block'][0]);
			if($type(msg)=='element')
				this._objects['block'][1](msg,(mode?'block':'none'));
		}

	}
}();
