/**
 * Stellt Formlar-Validators zur Verfügung. Das Benutzen dieses Namespaces wird
 * durch frm_helper.php vereifacht.
 *
 * @namespace jaex.frm
 * @requires jaex >= 0.7.x
 */

/**
 * @private
 */
jaex.frm = function() {};

/**
 * Neues Formular
 */
jaex.frm.Form = function() {
	this.inputs = new Array();
	
	this.addInput = function(input) {
		this.inputs[this.inputs.length] = input;	
	}
	
	this.isValid = function() {
		var valid = true;
		
		for(var ic = 0; ic < this.inputs.length; ic++) {
			if(!this.inputs[ic].callIsValidAT(++this.inputs[ic].onChange)) valid = false;
		}
		
		return valid;
	}
}

/**
 * Input Validator 
 *
 * @abstract
 */
jaex.frm.InputValidator = function() {
	this.blockElem;
	this.onChange = 0;
	this.onStart = false;
	
	this.prepareToCheck = function() {
		this.block();
		if(this.isBlockedByMe()) {
			this.removeError();
		}
		
		this.onStart = false;
		var _obj = this;
		var onChange = ++this.onChange;
		setTimeout(function() { _obj.callIsValidAT(onChange) }, 750);
	};
	
	this.callIsValidAT = function(onChange) {
		this.block();
		
		if(onChange == this.onChange) {
			return this.isValid();
		}
	};
	
	this.isValid = function() {
		return true;
	};
	
	this.showError = function() { };
	
	this.removeError = function() {	};
	
	this.setBlockElem = function(elem) { 
		this.blockElem = elem;	
	};
	
	this.block = function() {
		if(!jaex.util.isObject(this.blockElem._jaexfrmvalblocked)) {
			this.blockElem._jaexfrmvalblocked = this;
			return true;
		} else {
			return false;
		}
	};
	
	this.unblock = function() { 
		if(this.isBlockedByMe()) {
			this.blockElem._jaexfrmvalblocked = 0;
			return true;
		} else {
			return false;
		}
	};
	
	this.isBlockedByMe = function() { 
		if(this.blockElem._jaexfrmvalblocked == this) {
			return true;
		} else {
			return false;
		}
	};
}

/**
 * Neuer TextField-Validator
 *
 * @extends InputValidator
 */
jaex.frm.TextFieldValidator = function(inputIds, msgId, validator) {
	
	this.validator = "jaex.frm.validators." + validator;
	this.inputIds = inputIds;
	this.numInputs = this.inputIds.length;
	this.msg = $(msgId);
		
	this.inputs = new Array();
	this.classNames = new Array();
	this.onError = new Array();
	
	for(var ic = 0; ic < this.numInputs; ic++) {
		this.inputs[ic] = $(this.inputIds[ic]);	
		this.classNames[ic] = this.inputs[ic].className.replace(/ ?error/, ""); 
		
		this.inputs[ic]._jaexfrmobj = this;
		jaex.registerEvent(this.inputIds[ic], this.inputs[ic], 'onkeydown', this, 'prepareKeyDownToCheck');
		jaex.registerEvent(this.inputIds[ic], this.inputs[ic], 'onkeyup', this, 'prepareKeyUpToCheck');
		jaex.registerEvent(this.inputIds[ic], this.inputs[ic], 'onmouseup', this, 'prepareOnClickToCheck');
		
		jaex.registerEvent(this.inputIds[ic], this.inputs[ic], 'onfocus', this, 'start');
		jaex.registerEvent(this.inputIds[ic], this.inputs[ic], 'onblur', this, 'stop');
	}
	
	this.onfocus = false;
	this.setBlockElem(this.inputs[0]);
	this.started = false;
		
	this.start = function() {
		this.onfocus = true;
		if(this.started) {
			this.onStart = true;
			this.callIsValidAT(++this.onChange);
		} else {
			this.started = true;
			this.prepareToCheck();
		}
	};
	
	this.stop = function() {
		this.onStart = false;
		this.onfocus = false;
		this.callIsValidAT(++this.onChange);
	};
	
	this.prepareKeyUpToCheck = function() {
		if(!this.onStart) {
			this.prepareToCheck();
		}
	};
	
	this.prepareKeyDownToCheck = function() {
		this.onStart = false;
		this.prepareToCheck();		
	};
	
	this.prepareOnClickToCheck = function(e) {
		if(!this.onStart) {
			this.prepareToCheck();
		} else {
			this.onStart = false;
		}
	};
}

jaex.frm.TextFieldValidator.prototype = new jaex.frm.InputValidator();

/*
 * @overwirte
 */
jaex.frm.TextFieldValidator.prototype.isValid = function() {
	var anyErrors = false;
		
	if(eval(this.validator + "(this);")) {
		if(this.isBlockedByMe()) {
			this.removeError();
			this.unblock();
		}
		return true;
	} else {
		if(this.isBlockedByMe()) {
			this.showError();
		}
		return false;
	}		
}

/*
 * @overwirte
 */
jaex.frm.TextFieldValidator.prototype.showError = function() {
	if(this.onfocus) {
		this.msg.style.display = 'block';
		
		for(var ic = 0; ic < this.numInputs; ic++) {
			this.inputs[ic].className = this.classNames[ic];
		}
	} else {
		this.msg.style.display = 'none';
		
		for(var ic = 0; ic < this.numInputs; ic++) {
			if(this.onError[ic]) {
				this.inputs[ic].className = this.classNames[ic] + " error";
			} else {
				this.inputs[ic].className = this.classNames[ic];
			}
		}
	}
}

/*
 * @overwirte
 */
jaex.frm.TextFieldValidator.prototype.removeError = function() {
	this.msg.style.display = 'none';
	
	for(var ic = 0; ic < this.numInputs; ic++) {
		this.inputs[ic].className = this.classNames[ic];
	}
	
	this.inputs[0].blocked = 0;
}


