	
	var forum = Class.create({
		
		initialize:function(){
			this.instID = 'forum.' + parseInt(Math.random()*1000000);
		},
		
		showReactions : function(id){
			if ($(id + '_reactions').visible()){
				new Effect.BlindUp($(id + '_reactions'), {duration:'0.3'});
			}else{
				new Effect.BlindDown($(id + '_reactions'), {duration:'0.3'});
			}
			
		},
		
		openForm : function(element){
			if (element.visible()){
				new Effect.BlindUp(element, {duration:'0.3'});
			}else{
				new Effect.BlindDown(element, {duration:'0.3'});
			}
			parentWidth = $(element.parentNode).getWidth() - 40;
			element.setStyle('width:' + parentWidth + 'px');
		},
		
		validate: function(parentID){
			this.ok = true;
			this.parentID = parentID;
			this.disableFields(parentID);
			//check values:
			if (! (this.voornaam = this.hasValue($(parentID + '_voornaam'))) ) {
				this.setError(parentID+"_voornaam")
				this.ok = false;
			}else{
				this.voornaam = encodeURIComponent(this.voornaam);
				this.unSetError(parentID+"_voornaam")
			}
			if (! (this.body = this.hasValue($(parentID + '_body'))) ) {
				this.setError(parentID+"_body")
				this.ok = false;
			}else{
				this.body = encodeURIComponent(this.body);
				this.unSetError(parentID+"_body")
			}
			if (! (this.email = this.hasValue($(parentID + '_email'))) ) {
				this.setError(parentID+"_email");
				this.ok = false;
			}else if(!this.isEmail(this.email)){
				this.setError(parentID+"_email");
				$(parentID + '_email_error').update(this.email + " is geen geldig e-mail adres.");
				this.ok = false;
			}else{
				$(parentID + '_email_error').update("e-mail adres is verplicht");
				this.email = encodeURIComponent(this.email);
				this.unSetError(parentID+"_email")
			}
			this.achternaam = '';
			if ($(parentID+"_achternaam").getValue()) this.achternaam = $(parentID+"_achternaam").getValue();
			this.achternaam = encodeURIComponent(this.achternaam);
			if (this.ok){
				this.requestPost();
			}else{
				this.enableFields(parentID);
			}
		},
		
		requestPost : function(){
			var wpsRPC = new wps.rpc;
			wpsRPC.debug = true;
			wpsRPC.createCall('forum', this.doPost, this);
			wpsRPC.call('requestPost');
		},
		
		doPost : function(req){
			var authID = req.responseJSON.id;
			var wpsRPC = new wps.rpc;
			wpsRPC.debug = true;
			wpsRPC.createCall('forum', this.reload, this);
			wpsRPC.call('doPost', 'authID=' + authID, 'nickname='+this.voornaam + ' '+ this.achternaam, 'email='+this.email, 'body=<![CDATA[' + this.body +"]]>", 'parentID=' + this.parentID );
		},
		
		reload : function(req){
			var newID = req.responseJSON.id;
			var newLocation = 'http://' + window.location.hostname + "/Forum/&rand=" + parseInt(Math.random()*1000000) + "#message" + newID  
			window.location.href = newLocation;
		},
		
		hasValue : function(field){
			var value = field.getValue();
			value = value.stripTags().stripScripts().replace(' ', '');
			if (!value) return false;
			return value;
		},
		
		disableFields : function(parentID){
			$(parentID + '_voornaam').disabled = true;
			$(parentID + '_achternaam').disabled = true;
			$(parentID + '_email').disabled = true;
			$(parentID + '_body').disabled = true;
			$(parentID + '_submit').disabled = true;
		},
		
		enableFields : function(parentID){
			$(parentID + '_voornaam').disabled = false;
			$(parentID + '_achternaam').disabled = false;
			$(parentID + '_email').disabled = false;
			$(parentID + '_body').disabled = false;
			$(parentID + '_submit').disabled = false;
		},
		
		setError : function(id){
			$(id+'_error').show();
		},

		unSetError : function(id){
			$(id+'_error').hide();
		},
		
		isEmail : function(emailStr) {
			var checkTLD=1;
			var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
			var emailPat=/^(.+)@(.+)$/;
			var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
			var validChars="\[^\\s" + specialChars + "\]";
			var quotedUser="(\"[^\"]*\")";
			var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
			var atom=validChars + '+';
			var word="(" + atom + "|" + quotedUser + ")";
			var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
			var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
			var matchArray=emailStr.match(emailPat);

			if (matchArray==null) {
				return false;
			}

			var user=matchArray[1];
			var domain=matchArray[2];

			for (i=0; i<user.length; i++) {
				if (user.charCodeAt(i)>127) {
					return false;
				}
			}

			for (i=0; i<domain.length; i++) {
				if (domain.charCodeAt(i)>127) {
					return false;
				}
			}
			
			if (user.match(userPat)==null) {
				return false;
			}

			var IPArray=domain.match(ipDomainPat);
			
			if (IPArray!=null) {
				for (var i=1;i<=4;i++) {
					if (IPArray[i]>255) {
						return false;
			   		}
				}
				return true;
			}
			
			 
			var atomPat=new RegExp("^" + atom + "$");
			var domArr=domain.split(".");
			var len=domArr.length;

			for (i=0;i<len;i++) {
				if (domArr[i].search(atomPat)==-1) {
					return false;
			   }
			}

			if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
				return false;
			}

			if (len<2) {
				return false;
			}
			
			return true;
		}
		
	})