var FormCheck = new Class({
	options : {
		alreadyChecked: false,
		
		tips_class: 'tipsbox',			//tips error class
		error_class: 'error_f',			//div error class
		
		display : {		  
			showErrors : 1,				//0 : onSubmit, 1 : onSubmit & onBlur
			errorsLocation : 2,			//1 : tips, 2 : before, 3 : after, 4 : ganz oben
			indicateErrors : 2,			//0 :  none, 1 : one, 2 : all
			tipsOffsetX : -20,			//Left position of the tips box (margin-left)
			tipsOffsetY : 190,			//Top position of the tips box (margin-bottom)
			tipsPosition : 'relative',	//If you want to set the tips position with relative or absolute value (page not centered)
			tipsContainer : 'undef',	//Container of fields, to get right positions.
			listErrorsAtTop : false,	//list all errors at the top of the form
			scrollToFirst : true,		//Smooth scroll the page to first error
			fadeDuration : 300			//Transition duration
		},
		
		alerts : {
			required: "The field %s is required!",
			alpha: "The field %s accepts alphabetic characters only!",
			alphanum: "The field %s accepts alphanumeric characters only!",
			nodigit: "No digits are accepted in field %s!",
			digit: "Please enter a valid integer in field %s!",
			digitmin: "The number must be at least %0 in field %s!",
			digitltd: "The value must be between %0 and %1 in field %s!",
			number: "Please enter a valid number in field %s!",
			email: "Please enter a valid E-Mail address in field %s!",
			phone: "Please enter a valid phone in field %s!",
			url: "Please enter a valid url in field %s!",
			
			confirm: "The field %s is different from %0!",
			differs: "The value in field %s must be different of %0!",
			length_str: "The length in field %s is incorrect, it must be between %0 and %1!",
			lengthmax: "The length in field %s is incorrect, it must be at max %0!",
			lengthmin: "The length in field %s is incorrect, it must be at least %0!",
			checkbox: "Please check the box in field %s!",
			radios: "Please choose a radio button in the %s!",
			select: "Please choose a value in field %s!"
		},
		
		regexp : {
			required : /[^.*]/,
			alpha : /^[a-züöä ._-]+$/i,
			alphanum : /^[a-züöä0-9 ._-]+$/i,
			digit : /^[-+]?[0-9]+$/,
			nodigit : /^[^0-9]+$/,
			number : /^[-+]?\d*\.?\d+$/,
			email : /^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i,
			phone : /^[\d\s ().-]+$/,
			url : /^(http|https|ftp)\:\/\/[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*$/i
		}
	},
	
	/*
	Constructor: initialize
		Constructor
	
		Add event on formular and perform some stuff, you now, like settings, ...
	*/
	initialize : function(form, options) {	  	  
		if (this.form = $(form)) {
			this.form.isValid = true;
			this.regex = ['length'];
			this.setOptions(options);
			
			this.options.display.indicateErrors = 2;						
			
			//internalization
			//if (typeof(formcheckLanguage) != 'undefined') this.options.alerts = formcheckLanguage;			
			
			this.validations = [];
			this.alreadyIndicated = false;
			this.firstError = false;
			
			var regex = new Hash(this.options.regexp);
			regex.each(function(el, key) {
				this.regex.push(key);
			}, this)
	
			this.form.getElements("*[class*=validate]").each(function(el) {
				el.validation = [];
				var classes = el.getProperty("class").split(' ');
				classes.each(function(classX) {
					if(classX.match(/^validate(\[.+\])$/)) {
						var validators = eval(classX.match(/^validate(\[.+\])$/)[1]);
						for(var i = 0; i < validators.length; i++) {
							el.validation.push(validators[i]);
						}
						this._register(el);
					}
				}, this);
			}, this);
			
			
			this.form.addEvents({
				"submit": this._onSubmit.bind(this)
			});
		}
	},
	
	/*
	Function: _register
		Private method
		
		Add listener on fields
	*/
	_register : function(el) {
		this.validations.push(el);
		el.errors = [];
		if (this._isChildType(el) == false && this.options.display.showErrors == 1) el.addEvent('blur', function() {
			this._manageError(el, 'blur');
		}.bind(this));
	},
	
	/*
	Function: _validate
		Private method
		
		Dispatch check to other methods
	*/
	_validate : function(el) {
		el.errors = [];
		el.isOk = true;
		
		//On valide l'élément qui n'est pas un radio ni checkbox
		
		el.validation.each(function(rule) {
			if ($(el.getProperty('id')).getProperty('class').indexOf('validate[]') >= 0){
				// dann mach nix
				// bei nachträglicher Änderung via Ajax kriegt er das sonst nicht mit, weil vorher initalisiert wurde
			}
			else if (el.getProperty('id').indexOf('issue_') > 0){
				if (el.getProperty('id').indexOf('issue_de_7') < 0){
					return;
				}
				
				if (this._validateIssues(el) == false){
					el.isOk = false;
				}
			} else if (el.getProperty('id').indexOf('avago2012opto_sample_') > 0) {
				if (el.getProperty('id').indexOf('avago2012opto_sample_ULP1') < 0) {
					return;
				}
				if (this._validateAvago2012OptoSamples(el, 'avago2012opto_sample') == false){
					el.isOk = false;
				}
			} else if (el.getProperty('id').indexOf('avago2011segment_') > 0) {
                                if (el.getProperty('id').indexOf('avago2011segment_LED') < 0) {
                                        return;
                                }
                                if (this._validateAvagoSegments(el) == false){
                                        el.isOk = false;
                                }
                        } else if (el.getProperty('id').indexOf('avago2011sample_3w_') > 0) {
				if (el.getProperty('id').indexOf('avago2011sample_3w_COO') < 0) {
					return;
				}
				if( $('tx_ebvregistration_pi1[avago_sample_box_avago_sample_box_mini]').checked == true ) {
					if (this._validateAvagoSamples( el, 'avago2011sample_3w' ) == false){
						el.isOk = false;
					}
				}
			} else if (el.getProperty('id').indexOf('avago2011sample_05w_') > 0) {
				if (el.getProperty('id').indexOf('avago2011sample_05w_COO') < 0) {
					return;
				}
				if( $('tx_ebvregistration_pi1[avago_sample_box_avago_sample_box_mid]').checked == true ) {
					if (this._validateAvagoSamples( el, 'avago2011sample_05w' ) == false){
						el.isOk = false;
					}
				}        
			} else if (el.getProperty('id').indexOf('avago2011sample_oneled_') > 0) {
				if (el.getProperty('id').indexOf('avago2011sample_oneled_ONE1') < 0) {
					return;
				}
				if( $('tx_ebvregistration_pi1[avago_sample_box_avago_sample_box_oneled]').checked == true ) {
					if (this._validateAvagoSamples( el, 'avago2011sample_oneled' ) == false){
						el.isOk = false;
					}
				}
			} else if (el.getProperty('id').indexOf('dc2010segment_') > 0) {
				if (el.getProperty('id').indexOf('dc2010segment_AUT') < 0) {
					return;
				}
				if (this._validateSegments(el) == false){
					el.isOk = false;
				}
			} else if(this._isChildType(el)) {	
				// TGA: damit die Fehlermeldung nur beim ersten der Radio-Buttons ausgegeben wird
				// und nicht x-mal							
				var first = el.getParent('styled').getFirst('radio');
				var previous = el.getPrevious('radio');
				if (first == previous){
					if (this._validateGroup(el) == false) {
						el.isOk = false;
					}
				}
			} else {
				var ruleArgs = [];
				
				if(rule.match(/^.+\[/)) {
					var ruleMethod = rule.split('[')[0];
					var ruleArgs = eval(rule.match(/^.+(\[.+\])$/)[1].replace(/([A-Z_]+)/i, "'$1'"));
				} else var ruleMethod = rule;
				
				if (this.regex.contains(ruleMethod)) {
					if (this._validateRegex(el, ruleMethod, ruleArgs) == false) {
						el.isOk = false;
					}
				}
				if (ruleMethod == 'confirm') {
					if (this._validateConfirm(el, ruleArgs) == false) {
						el.isOk = false;
					}
				}
				if (ruleMethod == 'differs') {
					if (this._validateDiffers(el, ruleArgs) == false) {
						el.isOk = false;
					}
				}
				if (el.getTag() == "select" || el.type == "checkbox") {
					if (this._simpleValidate(el) == false) {
						el.isOk = false;
					}
				}
			}				
		}, this);
		
		if (el.isOk) return true;
		else return false;
	},
	
	_validateIssues : function (el) {
		var inputelements = $('registration').getElements('input');
		var checkedIssue = false;
		inputelements.each(function(e){					
			if (e.getProperty('type') != 'checkbox')
				return;

			var elementid = e.getProperty('id');

			if (elementid) {
				if (elementid.indexOf('issue_') > 0 && e.getProperty('checked')) {
					checkedIssue = true;
				}
			}
		});
		
		if (!checkedIssue){
			el.errors.push('Please select at least one issue!');
			
			inputelements.each(function(e){					
				if (e.getProperty('type') != 'checkbox')
					return;

				var elementid = e.getProperty('id');

				if (elementid) {
					if (elementid.indexOf('issue_') > 0) {
						$(elementid).getPrevious().setStyle('background-position', '0pt -100px');
					}
				}
			});
			
			return false;
		}
		
		return true;
	},
	
	_validateAvago2012OptoSamples : function ( el, sample_type ) {
		
		var inputelements = $('registration').getElements('input');
		var checkedSamples = false;
		inputelements.each(function(e){					
			if (e.getProperty('type') != 'checkbox')
				return;

			var elementid = e.getProperty('id');

			if (elementid) {
				if (elementid.indexOf( sample_type ) > 0 && e.getProperty('checked')) {
					checkedSamples = true;
				}
			}
		});
		
		if ( !checkedSamples ){
			el.errors.push('Please select at least one sample in the section FREE evaluation board!');
			
			inputelements.each(function(e){					
				if (e.getProperty('type') != 'checkbox')
					return;

				var elementid = e.getProperty('id');

				if (elementid) {
					if (elementid.indexOf( sample_type ) > 0) {
						$(elementid).getPrevious().setStyle('background-position', '0pt -100px');
					}
				}
			});
			
			return false;
		}
		
		return true;
	},
	
	_validateAvagoSegments : function (el) {
                var inputelements = $('registration').getElements('input');
                var checkedSegment = false;
                inputelements.each(function(e){
                        if (e.getProperty('type') != 'checkbox')
                                return;

                        var elementid = e.getProperty('id');

                        if (elementid) {
                                if (elementid.indexOf('avago2011segment_') > 0 && e.getProperty('checked')) {
                                        checkedSegment = true;
                                }
                        }
                });

                if (!checkedSegment){
                        el.errors.push('Please select at least one associated product!');

                        inputelements.each(function(e){
                                if (e.getProperty('type') != 'checkbox')
                                        return;

                                var elementid = e.getProperty('id');

                                if (elementid) {
                                        if (elementid.indexOf('avago2011segment_') > 0) {
                                                $(elementid).getPrevious().setStyle('background-position', '0pt -100px');
                                        }
                                }
                        });

                        return false;
                }

                return true;
        },
        _validateAvagoSamples : function ( el, sample_type ) {
		
		var inputelements = $('registration').getElements('input');
		var checkedSamples = false;
		inputelements.each(function(e){					
			if (e.getProperty('type') != 'checkbox')
				return;

			var elementid = e.getProperty('id');

			if (elementid) {
				if (elementid.indexOf( sample_type ) > 0 && e.getProperty('checked')) {
					checkedSamples = true;
				}
			}
		});
		
		if ( !checkedSamples ){
			el.errors.push('Please select at least one sample in the section FREE LED samples!');
			
			inputelements.each(function(e){					
				if (e.getProperty('type') != 'checkbox')
					return;

				var elementid = e.getProperty('id');

				if (elementid) {
					if (elementid.indexOf( sample_type ) > 0) {
						$(elementid).getPrevious().setStyle('background-position', '0pt -100px');
					}
				}
			});
			
			return false;
		}
		
		return true;
	},
	_validateSegments : function (el) {
		var inputelements = $('registration').getElements('input');
		var checkedSegment = false;
		inputelements.each(function(e){					
			if (e.getProperty('type') != 'checkbox')
				return;

			var elementid = e.getProperty('id');

			if (elementid) {
				if (elementid.indexOf('dc2010segment_') > 0 && e.getProperty('checked')) {
					checkedSegment = true;
				}
			}
		});
		
		if (!checkedSegment){
			el.errors.push('Please select at least one segment!');
			
			inputelements.each(function(e){					
				if (e.getProperty('type') != 'checkbox')
					return;

				var elementid = e.getProperty('id');

				if (elementid) {
					if (elementid.indexOf('dc2010segment_') > 0) {
						$(elementid).getPrevious().setStyle('background-position', '0pt -100px');
					}
				}
			});
			
			return false;
		}
		
		return true;
	},
	
	/*
	Function: _simpleValidate
		Private method
		
		Perform simple check for select fields and checkboxes
	*/
	_simpleValidate : function(el) {
                if (el.getTag() == 'select' && (el.value == el.options[0].value)) {
                        el.errors.push(this.options.alerts.select);
                        return false;
                } else if (el.type == "checkbox" && el.checked == false) {
                        if( el.getProperty('id') == 'tx_ebvregistration_pi1[legal]' ) {
                            el.errors.push('Please accept the Privacy Statement of EBV!');
                        } else if( el.getProperty('id') == 'tx_ebvregistration_pi1[terms_conditions]' ) {
                        	el.errors.push('Please accept the the Terms & Conditions!');
                        } else {
                            el.errors.push(this.options.alerts.checkbox);
                        }
                        return false;
                }
                return true;
  },
	
	/*
	Function: _validateRegex
		Private method
		
		Perform regex validations
	*/
	_validateRegex : function(el, ruleMethod, ruleArgs) {
		var msg = "";						
		
		label = getLabel(el.getProperty("name"));				
		
		if (!label || label == 'undefined'){		  		  
		  name = el.getProperty("name");		  
		  name = name.replace('_input', '');
		  label = getLabel(name);		  	
		  
		  if (!label || label == 'undefined')
		    label = '';	  
		}
		
		help = this.options.alerts[ruleMethod];		
		
		this.options.alerts[ruleMethod] = this.options.alerts[ruleMethod].replace("%s", label);				
		
		if (ruleArgs[1] && ruleMethod == 'length') {
			if (ruleArgs[1] == -1) {
				this.options.regexp.length = new RegExp("^.{"+ ruleArgs[0] +",}$");
				msg = this.options.alerts.lengthmin.replace("%0",ruleArgs[0]);				
			} else {
				this.options.regexp.length = new RegExp("^.{"+ ruleArgs[0] +","+ ruleArgs[1] +"}$");
				msg = this.options.alerts.length_str.replace("%0",ruleArgs[0]).replace("%1",ruleArgs[1]);				
			}
		} else if (ruleArgs[0]) {
			this.options.regexp.length = new RegExp("^.{0,"+ ruleArgs[0] +"}$");
			msg = this.options.alerts.lengthmax.replace("%0",ruleArgs[0]);			
		} else {
			msg = this.options.alerts[ruleMethod];			
		}
		if (ruleArgs[1] && ruleMethod == 'digit') {
			var regres = true;
			if (!this.options.regexp.digit.test(el.value)) {
				el.errors.push(this.options.alerts[ruleMethod]);				
				regres = false;
			}
			if (ruleArgs[1] == -1) {
				if (el.value >= ruleArgs[0]) var valueres = true; else var valueres = false;
				msg = this.options.alerts.digitmin.replace("%0",ruleArgs[0]);
			} else {
				if (el.value >= ruleArgs[0] && el.value <= ruleArgs[1]) var valueres = true; else var valueres = false;
				msg = this.options.alerts.digitltd.replace("%0",ruleArgs[0]).replace("%1",ruleArgs[1]);
			}
			if (regres == false || valueres == false) {
				el.errors.push(msg);
				this.options.alerts[ruleMethod] = help;
				return false;
			}
		} else if (this.options.regexp[ruleMethod].test($(el.getProperty('id')).getProperty('value')) == false)  { 
			el.errors.push(msg);
			this.options.alerts[ruleMethod] = help;
			return false;
		}
		
		this.options.alerts[ruleMethod] = help;		
		return true;
	},
	
	/*
	Function: _validateConfirm
		Private method
		
		Perform confirm validations
	*/
	_validateConfirm: function(el,ruleArgs) {
		if (el.validation.contains('required') == false) {
			//el.validation.push('required');
		}
		var confirm = ruleArgs[0];
		if(el.value != this.form[confirm].value){
			msg = this.options.alerts.confirm.replace("%0",ruleArgs[0]);
			el.errors.push(msg);
			return false;
		}
		return true;
	},
	
	/*
	Function: _validateDiffers
		Private method
		
		Perform differs validations
	*/
	_validateDiffers: function(el,ruleArgs) {
		var confirm = ruleArgs[0];
		if(el.value == this.form[confirm].value){
			msg = this.options.alerts.differs.replace("%0",ruleArgs[0]);
			el.errors.push(msg);
			return false;
		}
		return true;
	},
	
	/*
	Function: _isChildType
		Private method
		
		Determine if the field is a group of radio or not.
	*/
	_isChildType: function(el) {
		var elType = el.type.toLowerCase();
		if((elType == "radio")) return true;
		return false;
	},
	
	/*
	Function: _validateGroup
		Private method
		
		Perform radios validations
	*/
	_validateGroup : function(el) {
		el.errors = [];
		var nlButtonGroup = this.form[el.getProperty("name")];
		el.group = nlButtonGroup;
		var cbCheckeds = false;
		
		for(var i = 0; i < nlButtonGroup.length; i++) {			
			if(nlButtonGroup[i].checked) {
				cbCheckeds = true;
			}
		}
		if(cbCheckeds == false){		  
			label = getLabel(el.getProperty("name"));				
		
			if (!label || label == 'undefined'){		  		  
		  		name = el.getProperty("name");		  
		  		name = name.replace('_input', '');
		  		label = getLabel(name);		  	
		  	
		  		if (!label || label == 'undefined')
		    		label = '';	  
				}		
		
		var help = this.options.alerts.radios.replace("%s", label);
		
			el.errors.push(help);
			return false;
		} else {
			return true;	
		}
	},
	
	/*
	Function: _listErrorsAtTop
		Private method
		
		Display errors
	*/
	_listErrorsAtTop : function(obj) {
		if(!this.form.element) {
			 this.form.element = new Element('div', {'id' : 'errorlist', 'class' : this.options.error_class}).injectTop(this.form);
		}
		if ($type(obj) == 'collection') {
			new Element('p').setHTML("<span>" + obj[0].name + " : </span>" + obj[0].errors[0]).injectInside(this.form.element);
		} else {
			if ((obj.validation.contains('required') && obj.errors.length > 0) || (obj.errors.length > 0 && obj.value && obj.validation.contains('required') == false)) {
				obj.errors.each(function(error) {
					new Element('p').setHTML("<span>" + obj.name + " : </span>" + error).injectInside(this.form.element);
				}, this);
			}
		}
	},
	
	/*
	Function: _manageError
		Private method
		
		Manage display of errors boxes
	*/
	_manageError : function(el, method){
		var isValid = this._validate(el);
		
		if (((!isValid && el.validation.contains('required')) || (!el.validation.contains('required') && el.value && !isValid))) {
			if(this.options.display.listErrorsAtTop == true && method == 'submit')
				this._listErrorsAtTop(el, method);
			if (this.options.display.indicateErrors == 2 ||this.alreadyIndicated == false || el.name == this.alreadyIndicated.name)
				{
					this._addError(el);
					return false;
				}
		} else if ((isValid || (!el.validation.contains('required') && !el.value)) && el.element) {
			this._removeError(el);
			return true;
		}
		return true;
	},
	
	/*
	Function: _addError
		Private method
		
		Add error message
	*/
	_addError : function(obj) {
		this.alreadyIndicated = obj;
		if(!this.firstError) this.firstError = obj;																
		
		if(!obj.element) {
			if (this.options.display.errorsLocation == 1) {
				if (this.options.display.tipsPosition == 'relative') {
					var marginLeft = this.options.display.tipsOffsetX;
					if (this.options.display.tipsContainer = 'undef')
						var displacement = this.form.getCoordinates().left;
					else
						var displacement = $(this.options.display.tipsContainer).getCoordinates().left;
					var options = {
						'opacity' : 0,
						'position' : 'absolute',
						'margin-left' : obj.getCoordinates().right - displacement + this.options.display.tipsOffsetX
					}
				} else if (this.options.display.tipsPosition == 'absolute') {
					var options = {
						'opacity' : 0,
						'position' : 'absolute',
						'margin-left' : this.options.display.tipsOffsetX,
						'left' : obj.getCoordinates().right,
						'bottom' : obj.getCoordinates().top
					}
				}

					obj.element = new Element('div', {'id' : 'diverror' + obj.name, 'class' : this.options.tips_class, 'styles' : options});
					
					obj.element.injectInside(this.form);
				
			} else if (this.options.display.errorsLocation == 2){
				obj.element = new Element('div', {'id' : 'diverror' + obj.name, 'class' : this.options.error_class, 'styles' : {'opacity' : 0}});
				obj.element.injectBefore(obj);								
			} else if (this.options.display.errorsLocation == 3){
				obj.element = new Element('div', {'id' : 'diverror' + obj.name, 'class' : this.options.error_class, 'styles' : {'opacity' : 0}});
				
				if ($type(obj.group) == 'object' || $type(obj.group) == 'collection')
					obj.element.injectAfter(obj.group[obj.group.length-1]);
				else
					obj.element.injectAfter(obj);
			} else if (this.options.display.errorsLocation == 4){			  
			  obj.element = new Element('div', {'id' : 'diverror' + obj.name, 'class' : this.options.error_class, 'styles' : {'opacity' : 0}});				
			  
			  /* DAS IST NUR FÜR EBV begin */			  			  
			  
			  // Hinter den letzten Fehler hängen
			  var firstError = this.form.getFirst('error_f');
			  
			  if (firstError.getProperty('class') != 'error_f'){
			    obj.element.inject(this.form, 'top');			    
			  } else {
			    var lastError = this.form.getLast('error_f');			    
			    obj.element.inject(lastError, 'before');
			  }			  			  
			  
			  // bei EBV sind das die Select-Boxen, die per JS gepimpt werden und durch ein Hidden-Input-Feld repräsentiert werden
			   if (obj.getProperty('type') == 'hidden'){	
          var previous = $(obj.getProperty('id')).getPrevious('myelSelect');
		  if (previous != null) {
		  	var child = previous.getFirst('elSelect');
		  	child.setStyle('background-image', 'url(fileadmin/templates/images/content/form/error-select.gif)');
		  }
        }
			  
        if (obj.getProperty('type') == 'text' || obj.getProperty('type') == 'password'){			  
        	var width = obj.getStyle('width');        	        	        	
        	
        	if (width == '200px'){
          	var parent = $(obj.getProperty('id')).getParent('styled_input');
			    	parent.setStyle(
          	  'background-image', 'url(fileadmin/templates/images/content/form/error-input.gif)'
          	);
          	$(obj.getProperty('id')).addEvent('keydown', function() {				
							parent.setStyle(
          	  	'background-image', 'url(fileadmin/templates/images/content/form/input-no.jpg)'
          		);
          		/*          		
          		$('diverror' + obj.getProperty('id')).setStyle(
          			'display', 'none'
          		);
          		*/
        		});
          } else {
          	var parent = obj.getParent('styled_input');          
			    	parent.setStyle(
          	  'background-image', 'url(fileadmin/templates/images/content/form/error-input-wide.gif)'
          	);
          	obj.addEvent('keydown', function() {
							parent.setStyle(
          	  	'background-image', 'url(fileadmin/templates/images/content/form/input-no-wide.gif)'
          		);
          		/*          		
          		$('diverror' + obj.getProperty('id')).setStyle(
          			'display', 'none'
          		);
          		*/
        		});
          }          	         
        }
        
        if (obj.getProperty('type') == 'checkbox'){			          	
          var span = obj.getPrevious('styled');                    
			    span.setStyle(
            'background-position', '0 -100px'
          );
          
          // Das Ausblenden der Fehlermeldung funktioniert nicht, weil er auf kein Event der checkbox reagiert
          // daher: in formelements.js ausgelagert
        }
        
        if (obj.getProperty('type') == 'radio'){			          	
          var parent = obj.getParent('styled');                              
          var children = parent.getChildren('radio');
          
          children.each(function(child) {
						child.setStyle(
            	'background-position', '0 -76px'
          	);             
					});      
					
					// Das Ausblenden der Fehlermeldung funktioniert nicht, weil er auf kein Event der checkbox reagiert
          // daher: in formelements.js ausgelagert
        }                       
        
        if (obj.type == 'textarea'){

          var parent = obj.getParent('styled_textarea');          
			    parent.setStyle(
            'background-image', 'url(fileadmin/templates/images/content/form/error-textarea.gif)'
          );             
          parent.setStyle(
            'background-position', 'top left'
          );             
          var before = parent.getPrevious('styled_textarea_top');                    
			    before.setStyle(
            'background-image', 'url(fileadmin/templates/images/content/form/error-textarea-top.gif)'
          );             
          before.setStyle(
            'background-position', 'top left'
          );             
          var after = parent.getNext('styled_textarea_bottom');                    
          after.setStyle(
            'background-position', '0 -1px'
          );             
			    after.setStyle(
            'background-image', 'url(fileadmin/templates/images/content/form/error-textarea-bottom.gif)'
          );         
          
          
          obj.addEvent('keydown', function() {						
			    	parent.setStyle(
            	'background-image', 'url(fileadmin/templates/images/content/form/textarea.gif)'
          	);             
          	parent.setStyle(
            	'background-position', '-5px 0'
          	);                       
			    	before.setStyle(
            	'background-image', 'url(fileadmin/templates/images/content/form/textarea-top.gif)'
          	);             
          	before.setStyle(
            	'background-position', '-5px 0'
          	);                       
          	after.setStyle(
            	'background-position', '-5px 0'
          	);             
			    	after.setStyle(
            	'background-image', 'url(fileadmin/templates/images/content/form/textarea-bot.gif)'
          	);     
          	
          	/*          		
          		$('diverror' + obj.getProperty('id')).setStyle(
          			'display', 'none'
          		);
          		*/
        	});                   
        }        
        /* DAS IST NUR FÜR EBV end */
			}
		}						
		if (obj.element) {
			obj.element.empty();
			if (this.options.display.errorsLocation == 1) {
				var errors = [];
				obj.errors.each(function(error) {				  
					errors.push(new Element('p').setHTML(error));
				});
				var tips = this._makeTips(errors).injectInside(obj.element);
				obj.element.setStyle('top', obj.getCoordinates().top - tips.getCoordinates().height - this.options.display.tipsOffsetY);
			} else {
				obj.errors.each(function(error) {
					new Element('p').setHTML(error).injectInside(obj.element);
				});
			}
			
			if (!window.ie7 && obj.element.getStyle('opacity') == 0)
				new Fx.Styles(obj.element, {'duration' : this.options.display.fadeDuration}).start({'opacity':[1]});
			else
				obj.element.setStyle('opacity', 1);
		}
	},
	
	/*
	Function: _removeError
		Private method
		
		Remove the error display
	*/
	_removeError : function(obj) {
		this.firstError = false;
		this.alreadyIndicated = false;
		obj.errors = [];
		obj.isOK = true;
		if (this.options.display.errorsLocation == 2)
			new Fx.Styles(obj.element, {'duration' : this.options.display.fadeDuration}).start({ 'height':[0] });
		if (!window.ie7) {
			new Fx.Styles(obj.element, {
				'duration' : this.options.display.fadeDuration,
				'onComplete' : function() {
					if (obj.element) {
						obj.element.remove();
						obj.element = false;
					}
				}.bind(this)
			}).start({ 'opacity':[1,0] });
		} else {
			obj.element.remove();
			obj.element = false;
		}
	},
	
	/*
	Function: _focusOnError
		Private method
		
		Create set the focus to the first field with an error if needed
	*/
	_focusOnError : function (obj) {
		if (this.options.display.scrollToFirst && !this.alreadyFocused && this.alreadyIndicated.element && !this.isScrolling) {
		
		//alert(obj.element.getCoordinates().top);
		new Fx.Scroll(window, {onComplete : function() {this.isScrolling = false;}.bind(this)}).scrollTo(0,obj.element.getCoordinates().top-20);
				//new Fx.Scroll(window, {onComplete : function() {this.isScrolling = false;}.bind(this)}).scrollTo(0,obj.getCoordinates().top-20);
			
			if (this.options.display.errorsLocation == 1) new Fx.Scroll(window, {onComplete : function() {this.isScrolling = false;}.bind(this)}).scrollTo(0,obj.element.getCoordinates().top);
			else if (this.options.display.errorsLocation == 2) new Fx.Scroll(window, {onComplete : function() {this.isScrolling = false;}.bind(this)}).scrollTo(0,obj.getCoordinates().top-30);
			this.isScrolling = true;
			if (obj.getProperty('type') != 'radio' && obj.getProperty('type') != 'checkbox') {
				obj.focus();
				this.alreadyFocused = true;
			}
		} else if (this.options.display.scrollToFirst && !this.isScrolling) {
			new Fx.Scroll(window, {onComplete : function() {this.isScrolling = false;}.bind(this)}).scrollTo(0,obj.getCoordinates().top-30);
			this.isScrolling = true;
			if (obj.getProperty('type') != 'radio' && obj.getProperty('type') != 'checkbox') {
				obj.focus();
				this.alreadyFocused = true;
			}
		}
	},
	
	/*
	Function: _makeTips
		Private method
		
		Create tips boxes
	*/
	_makeTips : function(txt) {
		var table = new Element('table', {'class' : 'tipsbox'});
			table.cellPadding ='0';
			table.cellSpacing ='0';
			table.border ='0';
			
			var tbody = new Element('tbody').injectInside(table);
				var tr1 = new Element('tr').injectInside(tbody);
					new Element('td', {'class' : 'tipsbox_top_left'}).injectInside(tr1);
					new Element('td', {'class' : 'tipsbox_top'}).injectInside(tr1);
					new Element('td', {'class' : 'tipsbox_top_right'}).injectInside(tr1);
				var tr2 = new Element('tr').injectInside(tbody);
					new Element('td', {'class' : 'tipsbox_left'}).injectInside(tr2);
					var errors = new Element('td', {'class' : 'tipsbox_inner'}).injectInside(tr2);
					txt.each(function(error) {
						error.injectInside(errors);
					});
					new Element('td', {'class' : 'tipsbox_right'}).injectInside(tr2);
				var tr3 = new Element('tr').injectInside(tbody);
					new Element('td', {'class' : 'tipsbox_bottom_left'}).injectInside(tr3);
					new Element('td', {'class' : 'tipsbox_mark'}).injectInside(tr3);
					new Element('td', {'class' : 'tipsbox_bottom_right'}).injectInside(tr3);		
		return table;
	},
	
	/*
	Function: _reinitialize
		Private method		
		
		Reinitialize form before submit check
	*/
	_reinitialize: function() {
		this.validations.each(function(el) {
			if (el.element) {
				el.errors = [];
				el.isOK = true;
				el.element.remove();
				el.element = false;
			}
		});
		if (this.form.element) this.form.element.empty();
		this.alreadyFocused = false;
		this.firstError = false;
		this.alreadyIndicated = false;
		this.form.isValid = true;
	},
	
	/*
	Function: _onSubmit
		Private method		
		
		Perform check on submit action
	*/
	_onSubmit: function(event) {
		this._reinitialize();
		
		//RESET Selector BG to normal otherwise former elements are still highlighted
		this.form.getElements("*[class*=elSelect]").setStyle('background-image', 'url(fileadmin/templates/images/nav/news/select-bg.gif)');		
		
		this.validations.each(function(el) {						
			if(!this._manageError(el,'submit'))
				this.form.isValid = false;
		}, this);
		
		if(!this.form.isValid) { 
			new Event(event).stop();
			if (this.firstError) this._focusOnError(this.firstError);
		}
	}
});
FormCheck.implement(new Options());


