Behaviour.register({
	'.ReplyForm_Holder form': {
		validate : function(fromAnOnBlur) {
			initialiseForm(this, fromAnOnBlur);
			if(typeof fromAnOnBlur != 'undefined'){
				if(fromAnOnBlur.name == 'Content')
					require(fromAnOnBlur);
			}else{
				require('Content');
			}
			
			var error = hasHadFormError();
			if(!error && fromAnOnBlur) clearErrorMessage(fromAnOnBlur);
			
			return !error;
		},
		onsubmit : function() {
			return this.validate();
		}
	}
});
// TODO Performance-issue: Behaviour is possibly applied twice
Behaviour.apply('.ReplyForm_Holder form');	

Behaviour.register({
	".ajaxreplylink": {
		onclick: function(e) {
			var el = Event.element(e);
			
			if(el.nodeName != "a") {
				var el = Event.findElement(e,"a");
			}
			
			var matches = el.href.match(/.+\/([0-9]+)/);

			new Ajax.Updater(
				{success: 'ReplyForm_Holder_'+matches[1]},
				el.href, 
				{
					method: 'get',
					onFailure: function(response) {alert(response.responseText)},
					onComplete: function() {Behaviour.apply()}
				}
			);

			Event.stop(e);
			return false;
		}
	},
	
	"#startatopic": {
		onclick: function(e){
			if(this.innerHTML.match(/Start/)){
				$('StartATopicForm_Holder').style.display="block";
				$('itemlist').style.display="none";
				$('thanksMessage').style.display="none";
				this.innerHTML = " &#62&#62 Back to listed topics";
			}else{
				$('StartATopicForm_Holder').style.display="none";
				$('itemlist').style.display="block";
				$('thanksMessage').style.display="none";
				this.innerHTML = " &#62&#62 Start a topic";
			}
			Event.stop(e);
			return false;
		}
	},
	
	"#Form_ReplyForm_action_ajaxsubmit": {
		onclick: function(e){
			var form = findForm(this);
			var validate = form.onsubmit()
			if(validate){
				Ajax.SubmitForm(form, 'action_ajaxsubmit', {
					onSuccess : Ajax.Evaluator,
					onFailure : function(response) {
						alert(response.responseText);
					}
				});
				
				form.parentNode.innerHTML = "<h2>Your post has been sent for moderation, Thanks!</h2>";
			}
			Event.stop(e);
			return false;
		}
	},
	
	"#Form_StartATopicForm_action_submittopic": {
		onclick: function(e){
			var form = findForm(this);
			$('Form_StartATopicForm_Title').onfocus();
			$('Form_StartATopicForm_Content').onfocus();
			var validate = form.onsubmit();
			if(validate){
				Ajax.SubmitForm(form, 'action_submittopic', {
					onSuccess : Ajax.Evaluator,
					onFailure : function(response) {
						alert(response.responseText);
					}
				});
				$('thanksMessage').style.display="block";
				$('StartATopicForm_Holder').style.display="none";
				$('itemlist').style.display="none";
			}
			Event.stop(e);
			return false;
		}
	},
	"#Form_ReplyForm_action_ajaxcancel":{
		onclick: function(e){
			var form = findForm(this);
			form.parentNode.innerHTML = "";
			Event.stop(e);
			return false;
		}
	},
	
	"#Form_StartATopicForm_Title": {
		onfocus: function(e){
			if(this.value == "Enter a topic here!"){
				this.value = "";
			}
		}
	},
	
	"#Form_StartATopicForm_Content": {
		onfocus: function(e){
			if(this.value == "Place a comment here!"){
				this.value = "";
			}
		}
	}
});

function findForm(el){
	var f = el.parentNode;
	while(f && f.tagName.toLowerCase() != 'form') f = f.parentNode;
	return f;
}