mmw.parseUrl = function(url) {
	var fragSplit = url.split('#');
	var queryTopSplit = fragSplit[0].split('?');
	var querySplit = (typeof queryTopSplit[1] == 'undefined' ? null : queryTopSplit[1].split('='));
	var urlSplit = queryTopSplit[0].split('/');
	return {
		scheme: urlSplit[0],
		authority: urlSplit[2].split(':'),
		path: (typeof urlSplit[3] == 'undefined' ? [] : urlSplit.slice(3)),
		query: querySplit,
		fragment: (typeof fragSplit[1] == 'undefined' ? '' : fragSplit[1])
	};
}

if(typeof mmw.article == 'undefined') {
	mmw.article = {};
}

mmw.article.commentForm = {
	errorId: 'commentErrorMessage',
	formId: 'article_comment_form',
	validate: function(useForm)
	{
		var articleCommentFormElem = $(useForm);
		if(typeof articleCommentFormElem == 'undefined' || articleCommentFormElem.length < 1)
		{
			articleCommentFormElem = $('#' + this.formId);
		}
		var commentNameElem = articleCommentFormElem.find('input, textarea');
		var captchaValue = $.trim(articleCommentFormElem.find('.capt > .display').text());
		var inputElemVal = null;
		
		// Check CAPTCHA
		if(captchaValue != '') {
			inputElemVal = commentNameElem.filter('[name="brackets"]').first().val();
			if($.trim(inputElemVal) == '') {
				this.setErrorMessage('Please enter the numbers within the backets [ ].');
				return false;
			}
			else if(inputElemVal != captchaValue) {
				this.setErrorMessage('Please enter the correct numbers within the backets [ ].');
				return false;
			}
		}

		// Check Name field
		inputElemVal = commentNameElem.filter('[name="commentName"]').first().val();
		if($.trim(inputElemVal) == '') {
			mmw.article.commentForm.setErrorMessage('Please enter your name.');
			return false;
		}

		// Check email field
		inputElemVal = commentNameElem.filter('[name="commentMail"]').first().val();
		if($.trim(inputElemVal) == '') {
			mmw.article.commentForm.setErrorMessage('Please enter your email address.');
			return false;
		}

		// Check comment / message field
		inputElemVal = commentNameElem.filter('[name="commentText"]').first().val();
		if($.trim(inputElemVal) == '') {
			mmw.article.commentForm.setErrorMessage('Please enter a comment.');
			return false;
		}

		return true;
	},
	setErrorMessage: function(message) {
		var commentErrorElem = $('#' + this.errorId);
		commentErrorElem.text(message);
		commentErrorElem.show();
		var currentLoc = window.location.toString().split('#');
		window.location = currentLoc[0] + '#commentForm';
	},
	onInit: function()
	{
		$('#' + this.formId).bind('submit', this.validate);
		if(typeof commentFormErrorMessage != 'undefined' && $.trim(commentFormErrorMessage) != '') {
			this.setErrorMessage(commentFormErrorMessage);
			return false;
		}
		return true;
	}
}
