/******************************************************************************/
/*****************  this code-lines MUST NOT be deleted ***********************/
/******************************************************************************/
/** author    : seso media group gmbh vienna **********************************/
/** copyright :	2006-2009 seso media group vienna *****************************/
/** contact   : kl@seso.at  ***************************************************/
/** commercial and private usage is forbidden and only after consulting  ******/
/** seso media group approved *************************************************/
/******************************************************************************/


function formChecker(){
	this.checkedElement = new Array();
	this.failureText = '';
	this.failureId = '';
	this.failureTextClassName = '';
	
	this.idPrefix = '';
	this.failureClassName = '';
	this.className = '';
	this.possibleChecker = new Array('email', 'checkbox', 'radiobutton', 'number', 'empty', 'popup', 'regexp');
	
	this.hasNoFailures = true;

/** config-Function **/	
	this.setFailureText = function(msg, id){
		this.failureText = msg;
		this.failureId = id;
	}
	
	this.setFailureTextClassName = function(className){
		this.failureTextClassName = className;
	}
	
	this.setFailureClassName = function(className){
		this.failureClassName = className;
	}
	
	this.setClassName = function(className){
		this.className = className;
	}
	
	this.setIdPrefix = function(prefix){
		this.idPrefix = prefix;
	}

/** main-checking **/
	this.check = function(form){
		this.hasNoFailures = true;
		for(var i = 0; i < this.checkedElement.length; i++){
			var ch = this.checkedElement[i];
			var el = document.getElementsByName(ch.getId());
			var checker = ch.getChecker();
			switch(checker){
				case "empty":
					var val = ch.getValues();
					var minChar = val[0];
					var maxChar = val[1];
					this.checkInput(el[0], minChar, maxChar);
					break;
				case "email":
					this.checkEMail(el[0]);
					break;
				case "number":
					var val = ch.getValues();
					var minDigit = val[0];
					var maxDigit = val[1];
					this.checkNumber(el[0], minDigit, maxDigit);
					break;
				case "radiobutton":
					this.checkRadiobutton(el);
					break;
				case "checkbox":
					var val = ch.getValues();
					var minVal = val[0];
					var maxVal = val[1];
					this.checkCheckbox(el, minVal, maxVal);
					break;
				case "popup":
					var val = ch.getValues();
					var countIgnore = val[0];
					this.checkPopup(el[0], countIgnore);
					break;
				case "regexp":
					var val = ch.getValues();
					var reg = val[0];
					this.checkRegExp(el[0], reg);
					break;
			}
		}
		this.writeFailureText(!this.hasNoFailures);
		return (this.hasNoFailures) ? true : false;
	}
	
	this.writeFailureText = function(isFailure){
		var el = document.getElementById(this.failureId);
		if(isFailure){
			el.innerHTML = this.failureText;
			el.className = this.failureTextClassName;
		} else {
			el.innerHTML = "";
			el.className = "";
		}
	}

/** diffrent Checkers **/
	this.setEMailCheck = function(id){
		var ch = new Checker();
		ch.set(id, 'email', null);
		this.setChecker(ch);
	}
	
	this.checkEMail = function(el){
		var bol = false;
		var suche = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9_-]+\.[a-zA-Z]{2,}$/;
		if(!suche.test(el.value)){
			bol = true;
		}
		this.setFailure(el, bol);
	}
	
	this.setRegExpCheck = function(id, reg){
		var ch = new Checker();
		ch.set(id, 'regexp', new Array(reg, null));
		this.setChecker(ch);
	}
	
	this.checkRegExp = function(el, reg){
		var bol = false;
		var suche = new RegExp(reg);
		if(!suche.test(el.value)){
			bol = true;
		}
		this.setFailure(el, bol);
	}
	
	this.setPopupCheck = function(id, countIgnore){
		var ch = new Checker();
		ch.set(id, 'popup', new Array(countIgnore, null));
		this.setChecker(ch);
	}
	
	this.checkPopup = function(el, countIgnore){
		var le = el.options.length;
		var bol = false;
		for(var i = 0; i < le; i++){
			if(el.options[i].selected && i < countIgnore){
				bol = true;
			}
		}
		this.setFailure(el, bol);
	}
	
	this.setCheckboxCheck = function(id, minVal, maxVal){
		var ch = new Checker();
		ch.set(id, 'checkbox', new Array(minVal, maxVal));
		this.setChecker(ch);
	}
	
	this.checkCheckbox = function(el, minVal, maxVal){
		var le = el.length;
		var countSet = 0;
		var bol = false;
		for(var i = 0; i < le; i++){
			if(el[i].checked){
				countSet++;
			}
		}
		
		if(minVal >= 0) {
			if(countSet < minVal) {
				bol = true;
			}
		}			
		if(maxVal > 0) {
			if(countSet > maxVal){
				bol = true;
			}
		}
		
		this.setFailure(el[0], bol);
	}
	
	this.setRadiobuttonCheck = function(id){
		var ch = new Checker();
		ch.set(id, 'radiobutton', null);
		this.setChecker(ch);
	}
	
	this.checkRadiobutton = function(el){
		var le = el.length;
		var radioSet = false;
		for(var i = 0; i < le; i++){
			if(el[i].checked){
				radioSet = true;
			}
		}
		this.setFailure(el[0], !radioSet);
	}
	
	this.setNumberCheck = function(id, minDigit, maxDigit){
		var ch = new Checker();
		ch.set(id, 'number', new Array(minDigit, maxDigit));
		this.setChecker(ch);
	}
	
	this.checkNumber = function(el, minDigit, maxDigit){
		var bol = false; // false: kein Fehler; true: fehler
		var suche = /^[0-9]*$/;
		if(!suche.test(el.value)) { 
			bol = true;
		} else {
			if(minDigit >= 0) {
				if(el.value.length < minDigit) {
					bol = true;
				}
			}
			
			if(maxDigit > 0) {
				if(el.value.length > maxDigit){
					bol = true;
				}
			}
		}
		this.setFailure(el, bol);
	}
	
	this.setCheck = function(id, minChar, maxChar){
		var ch = new Checker();
		ch.set(id, 'empty', new Array(minChar, maxChar));
		this.setChecker(ch);
	}
	
	this.checkInput = function(el, minChar, maxChar){
		var bol = false; // false: kein Fehler; true: fehler
		if(minChar >= 0) {
			if(el.value.length < minChar) {
				bol = true;
			}
		}
			
		if(maxChar > 0) {
			if(el.value.length > maxChar){
				bol = true;
			}
		}
		this.setFailure(el, bol);
	}

/** helper-Functions **/
	this.setFailure = function(el, isFailure){
		var pel = document.getElementById(this.idPrefix+el.name);
		if(isFailure) {
			pel.className = this.failureClassName;
			this.hasNoFailures = false && this.hasNoFailures;
		} else {
			pel.className = this.className;
			this.hasNoFailures = true && this.hasNoFailures;
		}
	}
	
	this.setChecker = function(checker){
		this.checkedElement[this.checkedElement.length] = checker;
	}

/** Checker Helper-Class **/
	function Checker(){
		this.set = function(id, checker, values){
			this.id = id;
			this.checker = checker;
			this.values = values;
		}
		this.getId = function(){
			return this.id;
		}
		this.getChecker = function(){
			return this.checker;
		}
		this.getValues = function(){
			return this.values;
		}
	}
}
