var is_max = false;
var tmp = false;

var myrules = {
	'#maximize' : function(el) {
		el.onclick = function() {
			var content = document.getElementById("content");
			var left = document.getElementById("left");
			//var right = document.getElementById("right");
			
			if( ! is_max ) {
				left.visualEffect('fade',{duration:1, afterFinish: function(obj) {content.className='clayout1';}});
				//right.visualEffect('fade',{duration:1, afterFinish: function(obj) {content.className='clayout1';}});
				
				is_max = true;
			}
			else {			
				left.visualEffect('appear',{duration:1, beforeStart: function(obj) {content.className='clayout0';}});
				//right.visualEffect('appear',{duration:1});
				
				is_max = false;
			}
			
			return true;
		}
	},
	
	'input.overwrite' : function(el) {
		el.onchange = function() {
			if( ! el.checked )
			{				
				disable( el.form.elements[ el.name + "_new"] );
			}
			else 
			{
				enable( el.form.elements[ el.name + "_new"] );
			}
			return true;
		}
	},
	
	'input.switch' : function(el) {
		el.onchange = function() {
			var targets = el.getAttribute('rel').split(',');
			for (var i = 0; i < targets.length; i++) {
				if( targets[ i].substring(0,3) == 'on_') {
					if( el.checked )
					{				
						enable( el.form.elements[ targets[ i].substr(3)] );
					}
				}
				else if( targets[ i].substring(0,4) == 'off_') {
					if( el.checked )
					{				
						disable( el.form.elements[ targets[ i].substr(4)] );
					}
				}
			}
			return true;
		}
	},
	
	'select.chooseOrNew' : function(el) {
		el.onchange = function() {
			if( el.selectedIndex != 0 )
			{				
				disable( el.form.elements[ el.name + "_new"] );
			}
			else 
			{
				enable( el.form.elements[ el.name + "_new"] );
			}
			return true;
		}
	},
	
	'.submitter' : function(el) {
		el.onclick = function() {
			if( el.parentNode.nodeName == "FORM" ) {
				el.parentNode.submit();
			}	
			else
				alert(el.parentNode.nodeName);
		}
	},
	
	'.onChangeSubmit' : function(el) {
		el.onchange = function() {
			el.form.submit();
		}
	},
	
	'.unhideNextDiv' : function(el) {
		el.onclick = function() {
			div = el.nextSibling.nextSibling;
			if( div.nodeName == 'DIV' ) {
				if ( Element.visible( div ) ) {
					new Effect.BlindUp( div, {duration:1});
				}
				else {
					new Effect.BlindDown( div, {duration:1});
				}
			}
			else
				alert(div);
		}
		return true;
	},
	
	'select.divUnhider' : function(el) {
		el.onchange = function() {
			div = document.getElementById(el.value);
			if( div.nodeName == 'DIV' ) {
				if ( tmp != false ) {
					new Effect.Shrink( tmp, {duration:1});
				}
				new Effect.Grow( div, {duration:1});
				tmp = div;
			}
			else
				alert(div.nodeName);
		}
		return true;
	},
	
	'input.confirm_deletion' : function(el) {
		el.onclick = function() {
			return confirm(unescape('Wollen sie den Eintrag wirklich l%F6schen?'));
		}
	},
	
	'input.validate_submit' : function(el) {
		el.onclick = function() {
			form = el.form;
			inputs = form.getElementsByTagName("input");
			error_inputs = new Array();
			var err_msg = new Array('Folgende Fehler sind aufgetreten:\n');
			var error = false;
			for (var i = 0; i < inputs.length; i++) {
				jscss('remove', inputs[ i], 'validationError');
				
				if( jscss('check', inputs[ i], 'notEmpty') ) {
					if (!inputs[ i].value && !inputs[ i].disabled) {      
						error = true;
				    	err_msg.push(inputs[ i].title + unescape(' muss ausgef%FCllt sein'));
				    	error_inputs.push(inputs[ i]);
				    }
				}
				
				if( jscss('check', inputs[ i], 'email') ) {
					if ( inputs[ i].value && !inputs[ i].disabled) {      
					      var usr = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
					      var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
					      var regex = "^"+usr+"\@"+domain+"$";
					      var myrxp = new RegExp(regex);
					      var check = (myrxp.test(inputs[ i].value));
					        if (check != true) {
					          error=true;
					          err_msg.push(inputs[ i].title+' ist keine Email Adresse');
					          error_inputs.push(inputs[ i]);
					        }
					      }
				    }
				    
				   if( jscss('check', inputs[ i], 'integer') ) {
					if ( inputs[ i].value && !inputs[ i].disabled) {
					      var regex = "^[0-9]*$";
					      var myrxp = new RegExp(regex);
					      var check = (myrxp.test(inputs[ i].value));
					        if (check != true) {
					          error=true;
					          err_msg.push(inputs[ i].title+' ist keine Zahl');
					          error_inputs.push(inputs[ i]);
					        }
					      }
				    }
			}
			
			// add error class to input fields
			for (var i = 0; i < error_inputs.length; i++) {
				jscss('add', error_inputs[ i], 'validationError');
			}
			
			// print error message
			if (error) {
			    err_msg = err_msg.join('\n\xB7 ');
				alert(err_msg);
				return false;
			  }
			  else {
			  	//alert('found no errors');
			    return true;
			  }
		}
	}
}

Behaviour.register(myrules);

