/* formi validator (class) */

function validator(id){
	if(!checkAJAX()) return false;
	var d = document;
	this.init = function(){
		this.form = d.getElementById(id);
		this.c = 0; //id-ner dnelu hamar counter
		this.createIMGs();
		this.AJAX = AJAX;
		
		this.form.validator = this; //kpcnem DOMin, vor chkori :)
	};
	this.createIMGs = function(){
		var i; var arr;
		arr = this.form.getElementsByTagName("input");
		for(i = 0; i<arr.length; i++){
			if(arr[i].type=="text" ||arr[i].type=="password")
				this.createImg(arr[i]);
		}
		arr = this.form.getElementsByTagName("textarea");
		for(i = 0; i<arr.length; i++)
			this.createImg(arr[i]);
	};
	this.createImg = function(obj){
		var img = d.createElement('img');
		img.className = "status";
		
		img.id = id + "-imgStatus" + this.c;
		obj.id = id + "-input" + this.c++;
		
		img.src = "images/blank.png";
		obj.parentNode.style.position="relative";
		obj.parentNode.appendChild(img);
		/* ========EFFECTS.JS======== */
		new fader(img, 30);
		new fader(obj, 30);
		/* =========================== */
		obj.onblur = function(){d.getElementById(id).validator.stugel(this.name, this.value);};
	};
	
	//Public functions
	this.stugel = function(name, value){
		if(value=="") return; //datark vandaki hamar charji
		if(this.form[name].parentNode.hasErrorDiv)
			this.form[name].errorDiv.hide();
		this.AJAX("login/check?"+name, "post", "value="+value, this.mshakum, this.form[name], false);
	};
	this.mshakum = function(input, result){
		var img = input.parentNode.lastChild;
		if(result!="OK"){
			input.className = "error";
			img.className = "status error";
			input.form.validator.showError(input, result);
			/* ========EFFECTS.JS======== */
			img.fader.fade(1,0,2);
			input.fader.fade(1, 0, 2);
			input.errorDiv.fader.fade(1,0,2);
			/* =========================== */
		}else{
			input.className = "";
			img.className = "status ok";
		}
	};
	this.showError = function(input, text){
		var div = d.createElement('div');
		div.className = "error";
		div.innerHTML = text;
		input.parentNode.appendChild(div);
		
		div.style.position="absolute";
		div.style.zIndex = "10";
		div.style.top = (input.offsetTop + input.offsetHeight) + 'px';
		div.style.left = input.offsetLeft + 'px';
		
		new fader(div, 30);
		
		div.hide = function(){
			this.parentNode.hasErrorDiv = false;
			this.parentNode.removeChild(this);
		};
		div.onclick = div.hide;
		
		// USHADIR!
		input.parentNode.hasErrorDiv = true;
		input.errorDiv = div;
	};
	
	this.init();
}