var validators = new Hash();
var help = new function(){
    var obj = new Object();
    var i = 0;
    
	obj.bgcolor_error = '#FF5F5F';
	obj.bgcolor_ok = '#E4FFDF';
	
	obj.ok = function(obj,vg,msg){
		//raport o wywolaniu
		obj = $(obj);
		if(!obj){
			return false;
		}
		d('help.OK('+ obj.id +','+ vg + ','+msg + ')');
		//odszukuje w rejestrze
		g = validators.get(obj.id);
		var isFinded = false;
		var notify = $(obj.id + '_hint');
		if(!g){
			//nie ma nic w rejestrze - blad, cos powinno byc, zeruje
			//d('Dla helpera, obiektu: ' + obj.id + ' Powinno byc cos zarejestrowane'); 
			obj.style.background = 'transparent';
			obj.title = '';
			if(notify){
				notify.remove();
			}
			return false;
		}
		var before = g.length;
		isFinded = g.find(function(e){
				if(e == vg){
					//wyjecie z tablicy
					g = g.without(vg);
					validators.set(obj.id,g)
					return true;
				};
				return false;
		});
		//sprawdz czy tablica jest pusta jesli tak wywolaj efekt OFF.
		//d('Tablica obiektu id:' + obj.id + ', ma ' + g.length + ' elementow');
		
		if(g.length == 0){
			if(before > 0){
				new Effect.Highlight(obj, {startcolor: this.bgcolor_error, endcolor: this.bgcolor_ok, restorecolor: this.bgcolor_ok});
			}
			obj.style.background = this.bgcolor_ok;
			obj.title = msg;
			if(notify){
				notify.remove();
			}
		}	
	};
	obj.msg = function (obj,vg,msg){
		//raport o wywolaniu
		d('MSG ' + obj.id + ' : ' + msg);	
		g = validators.get(obj.id);
		var isFinded = false;
		
		if(!g){
			//dodaje do listy globalnej validators identyfikator validatora w grupie pola 
			validators.set(obj.id,[vg]);
			g = validators.get(obj.id);
			
		}else{
			//mamy obiekt, sprawdzam czy posiada juz wiezy zeby nie nadpisywac
			//upewniam sie ze element taki juz istnieje w rejestrze
			isFinded = g.find(function(e){
				if(e == vg){
					//d('[[Odszukalem obiekt');
					return true;
				};
				return false;
			});
			if(!isFinded){
				//wprowadzam na stos element
				g.push(vg);				
			}
		}
		var notify = $(obj.id + '_hint');
		if(!isFinded){
			new Effect.Highlight(obj, {endcolor: this.bgcolor_error,restorecolor: this.bgcolor_error});
			//obj.style.background = this.bgcolor_error;
		}
		if(!notify){
			notify = new Element('span');
			notify.id = obj.id + '_hint';
			
			notify.innerHTML = '!'
			var parent = obj.parentNode.appendChild(notify);
		}
		if(notify){
			notify.title = msg;
		}
		obj.title = msg;
	};
	return obj;

};
var vc = new Class.create({
	ele : null,//to jest wskaznik do aktualnie analizowanego obiektu.
	a : {},//lista parametrow w postaci obiektu.
	ok : function(msg){
		help.ok(this.e,this.type,msg);
	},	
	msg : function(msg){
		help.msg(this.e,this.type,msg);
	},
	initialize: function(obj,p) {
		
		obj = $(obj);
		this.e = obj;
		this.p = p;
		this.valid();
	}
});

var vc_int = new Class.create(vc,{
	type : 'int',
	valid : function (){
		d('[[new VC_int]]');
		this.e.value = this.e.value.replace(',','.');
		if(isNaN(Math.floor(this.e.value))){
			this.e.value = 0;
		}
		this.e.value = Math.floor(this.e.value);
		if(this.e.value < this.p.vmin){
			this.msg('Zbyt mała wartosc, dopuszczalne: '+this.p.vmin+'..'+this.p.vmax);
		}else if(this.e.value > this.p.vmax){
			this.msg('Zbyt duża wartosc, dopuszczalne: '+this.p.vmin+'..'+this.p.vmax);
		}else{
			this.ok();
		}
	}	
});
var vc_float = new Class.create(vc,{
	type : 'float',
	valid : function (){
		d('[[new VC_float]]');
		this.e.value = this.e.value.replace(',','.');
		if(isNaN(this.e.value)){
			this.e.value = 0;
		}
		if(this.p.after_dot){
			var n = new Number(this.e.value);
			this.e.value = n.toFixed(this.p.after_dot);
		}
		if(this.e.value < this.p.vmin){
			this.msg('Zbyt mała wartosc, dopuszczalne: '+this.p.vmin+'..'+this.p.vmax);
		}else if(this.e.value > this.p.vmax){
			this.msg('Zbyt duża wartosc, dopuszczalne: '+this.p.vmin+'..'+this.p.vmax);
		}else{
			this.ok();
		}	
	}	
});
var vc_time = new Class.create(vc,{
	type : 'time',
	valid : function (){
		d('[[new VC_time]]');
		this.e.value = this.e.value.replace(/\,|\.|;/,':');
		if(this.p.allow_null && this.e.value == ''){
			this.ok();
		}else{
			if(this.p.over24){
				//more than 24 hours allowed
				ss = /^[0-9]+:([0-5]?[0-9])$/;
			}else
			if(this.p.use24){
				//24 hours mode
				ss = /^(0?[0-9]|1[0-9]|2[0-3]):([0-5]?[0-9])$/;
			}else{
				//12 hours mode
				ss = /(0?[0-9]|1[0-2]):([0-5]?[0-9])\s?(pm|am)/;
			}
			if(this.e.value.search(ss) == -1){
				this.msg('Nie moge odszukać poprawnego czasu');
			}else{
				this.ok();
			}
		}
	}	
});