/**
 *
 *
 */

var Monktools = {};
Monktools.version = 0.10;
Monktools.Form = {};
Monktools.Form.Checker = new Class(
{
	elForm:false,
	strWrapperElement:false,
	strErrorElement:false,
	strErrorTriggerClass:false,
	funcSubmitHandler:false,
	
	initialize:function()
	{
		this.Error._parent = this;
	},
	
	setFormWrapper:function( argElForm )
	{
		this.elForm = $(argElForm);
		this.elForm._parent = this;
		this.elForm.onsubmit = function(){
			
			return this._parent.Send();
		};
	},
	
	setWrapperElement:function( argStrWrapperElement )
	{
		if( $type(argStrWrapperElement) != 'string' ){alert('setWrapperElement argument is not a string (xpath)')}
		this.strWrapperElement = argStrWrapperElement;
	},
	
	setErrorElement:function( strErrorElement )
	{
		if( $type(strErrorElement) != 'string' ){ alert('setErrorElement argument is not a string (xpath)') }
		this.strErrorElement = strErrorElement;
		return this.Error;
	},
	
	setSubmitHandler:function(argFunc){
		this.funcSubmitHandler = argFunc;
	},
	
	Error:{
		setTriggerClass:function( strClass )
		{
			if( $type(strClass) != 'string' ){ alert('setTriggerClass argument is not a string') }

			this._parent.strErrorTriggerClass = strClass;
		},
		
		setError:function( bln, el )
		{
			if( bln )
			{
				if( this._parent.strErrorTriggerClass )
				{
					el.addClass( this._parent.strErrorTriggerClass );
				}
				else
				{
					el.setStyle('display','block');
				}
			}
			else
			{
				if( this._parent.strErrorTriggerClass )
				{
					el.removeClass( this._parent.strErrorTriggerClass );
				}
				else
				{
					el.setStyle('display','none');
				}				
			}
		}
	},
	
	Send:function()
	{
		if( !this.__hasErrorCheck() )
		{
			return true;
		}
		else
		{
			return false;
		}
		
		return false;
	},
	
	checkMail:function(argStrMail)
	{
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(argStrMail))
		{
			return true;
		}
		else
		{
			return false;
		}
	},

	checkPostcode:function(argStrPostcode)
	{
		
		var filter = /[0-9]{4} ?[A-Z]{2}/i;
		if (filter.test(argStrPostcode))
		{
			return true;
		}
		else
		{
			return false;
		}
	},

	__hasErrorCheck:function()
	{
		var blnHasError = false;
		
		this.elForm.getElements( this.strWrapperElement ).each(function(elNode,intI)
		{
			
			var arrElError = elNode.getElements( this.strErrorElement );
										
			if( arrElError.length > 0 )
			{
				arrElInput = elNode.getElements('input');	
				arrElTextarea = elNode.getElements('textarea');	
				arrElSelect = elNode.getElements('select');	
				
				if( arrElInput.length > 0 )
				{
					var blnCurrentError = false;
					var blnNeedOne = false;
					
					arrElInput.each(function(elNode, intI)
					{
						if( elNode.name.indexOf('email')>=0  )
						{
							if( !this.checkMail(elNode.value) )
							{
								blnHasError = true;
								
								this.Error.setError(true, arrElError[0] );
							}
							else if( !blnCurrentError )
							{
								this.Error.setError(false, arrElError[0] );
							}
						}
						else if( elNode.name.indexOf('geslacht')>=0  )
						{
							if( elNode.checked == false && intI == 1 && !blnNeedOne )
							{
								blnHasError = true;
								this.Error.setError( true, arrElError[0] );
							}
							else if( !blnCurrentError && elNode.checked != false )
							{
								blnNeedOne = true;
								this.Error.setError(false, arrElError[0] );
							}
						}
						else if( elNode.name.indexOf('postcode')>=0  )
						{										
							if( !this.checkPostcode(elNode.value) )
							{
								blnHasError = true;
								blnCurrentError = true;
								this.Error.setError( true, arrElError[0] );
							}
							else if( !blnCurrentError )
							{
								this.Error.setError(false, arrElError[0] );
							}
						}
						else if( elNode.type.indexOf('checkbox')>=0  )
						{
							if( elNode.checked == false )
							{
								blnHasError = true;
								
								this.Error.setError(true, arrElError[0] );
							}
							else if( !blnCurrentError )
							{
								this.Error.setError(false, arrElError[0] );
							}
						}
						else
						{
							if( elNode.value == '' )
							{
								blnHasError = true;
								this.Error.setError( true, arrElError[0] );
							}
							else if( !blnCurrentError )
							{
								this.Error.setError(false, arrElError[0] );
							}
						
						}
					}.bind(this));
				}

				if( arrElSelect.length > 0 )
				{
					var blnCurrentError = false;
					var blnNeedOne = false;
					
					arrElSelect.each(function(elNode, intI)
					{
						
						if( elNode.value == '' )
						{
							blnHasError = true;
							this.Error.setError( true, arrElError[0] );
						}
						else if( !blnCurrentError )
						{
							this.Error.setError(false, arrElError[0] );
						}
						
					}.bind(this));
				}

				if( arrElTextarea.length > 0 )
				{
					if( arrElTextarea[0].value == '' )
					{
						blnHasError = true;
						this.Error.setError( true, arrElError[0] );
					}
					else
					{
						this.Error.setError( false, arrElError[0] );
					}
				}
				
			}
		}.bind(this));
		
		return blnHasError;
	}

});


Monktools.Html = {};
Monktools.Html.Tabber = new Class({
	initialize:function(tabs,content,activate){
		this.tabs = $A(tabs);
		this.content = $A(content);
		
		this.tabs.each(function(el,i){
			el._parent = this;
			el.i = i;
			el.addEvent('click',function(){
				this._parent.activate(this.i);
				return false;
			});
		}.bind(this));
		
		var cookie_activate = Cookie.read('Monktools.Html.Tabber.Index');
		
		if( cookie_activate != null )
		{
			this.activate( cookie_activate );
		}
		else if(activate != undefined)
		{
			this.activate( activate );
		}
	},
	
	activate:function(argI){
		Cookie.write('Monktools.Html.Tabber.Index', argI, {duration: 1});
		
		this.tabs.each(function(el,i){
			el.removeClass('active');
			
			if(i == argI)
			{
				el.addClass('active');
			}
		}.bind(this));
		
		this.content.each(function(el,i){
			el.setStyle('display','none');
			
			if(i == argI)
			{
				el.setStyle('display','block');
			}
		}.bind(this));
	}
});
