var Func = new Class({
	name: 'spec_func',
	Implements: [Chain, Events, Options],
	initialize: function(options){
		this.setOptions(options);
		this.fireEvent('onStart');
		this.callChain();
	}
});
	
validation = new Class({
	Implements:[Hash],
	name: 'validace',
	uniqueRequest : false,
	moduls : [],
	json: new Hash({}),
	initialize:function(){
		new Asset.css('/js/validation/validation.css');
	},
	clear:function(){
		this.json = {};
	},
	valideAll:function(form){
		var form = $(form);
		if (form.getElements('.validation_error').length ==0 && form.getElements('.validation_require').length==0){
				return true;
		} else {
			alert('Nejsou vyplněny všechny povinné údaje');
			return false;
		}		
	},
	
	valide: function(el,field){
		//var event = new Event(e);
		//var el = event.target;
		
		var arr = field.split('/');
		var field = this.exist(arr);
		if (!field) return false;
		for (index in field){
			p = eval(field[index]);
			if(p.method.substring(0,1) != '/'){
				/* pokud se jedna o funkci */
				fnc = eval('this.' + p.method).bind(this);
				if (!fnc(el, p['message']))
					return false;
			} else {
				/* pokud se jedna o regular */
				reg = p.method.substring(1,p.method.length -1);
				if (el.value.test(reg)){
					this.valRes(el,true);
					//return true;
				} else {
					this.valRes(el,false,p.message);
					return false;
				}
			}
		};
		
	},
	valRes: function(el,result,message){
		if (!result){
			$(el.id + '_span').setProperties({
				'class' :	'validation_error',
				'title'		:	message
			});
		} else {
			$(el.id + '_span').setProperties({
				'class' :	'validation_ok',
				'title'		:	''
			});
		}
	},
	exist: function(arr){
		var json = this.json;
		for (i=0;i<arr.length;i++){
			if (!json[arr[i]]){
				return false;
			} else {
				json = json[arr[i]];
			}
		}
		return json;
	},
	load: function(moduls){
		var loadJSON = new Request.JSON({
			url: "/admin/menu_items/validace/" + moduls, 
			onComplete: (function(json){
				this.json = $H(this.json).extend(json);
			}).bind(this)
		}).send();
	},
	unload: function(moduls){
	
	},
	isConfirm: function(el,message){
		
		var parObject = $(el.id.substring(0,el.id.length - 7));
		if (parObject.value != el.value){
			//alert(parObject.id + 'Confirm_span');
			$(parObject.id + 'Confirm_span').setProperties({
				'class' :	'validation_error',
				'title'		:	message
			});
			return false;
		} else {
			$(parObject.id + 'Confirm_span').setProperties({
				'class' :	'validation_ok',
				'title'		:	''
			});
			return true;
		}
	},
	isUnique:function(el,message){
		if (el.value.length > 3) 
		var req = new Request.HTML({
			url			:	'/system/isUnique/',
			evalScripts	:	true,	
			autoCancel: true,
			onLoading	:	function(){
				document.getElementById(el.id + '_span').className = 'load';
			},
			onComplete	:	(function(a,b,request, json){
				if (request == 1){
					document.getElementById(el.id + '_span').className = 'validation_error';
					document.getElementById(el.id + '_span').title = message;
					//object.value = error;
				} else {
					span = document.getElementById(el.id + '_span');
					if (span){
						span.className = 'validation_ok';	
						span.title = '';
					}
					
				}
			}).bind(this)
		}).post({value:el.value,name:el.name});
	}
});

if (!document.validation)
	document.validation = new validation();


