var emoticons_max_number = 3;

function max_emoticons(current_post, emoticons_max) {

var emoticons_number = 0;

//counting to see if more than max emoticons in post
var emoticons_results = current_post.match(/\[confused\]|\[crying\]|\[happy\]|\[kidding\]|\[mad\]|\[sad\]|\[scared\]|\[serious\]|\[shy\]|\[skeptical\]|\[smile\]|\[surprised\]|\[wink\]|\[laugh\]|\[love\]/g);

if (emoticons_results != null) emoticons_number = emoticons_results.length;
else emoticons_number = 0;

if (emoticons_number > emoticons_max ) {
	alert('The maximum number of emoticons in a post is ' + emoticons_max_number);
	return true;
}
else return false;
}


function add_emoticon(emoticon) {

var current_post = document.comments.comment.value;

if (!max_emoticons(current_post, emoticons_max_number - 1)) {
	current_post = current_post + " " + emoticon + " ";
	document.comments.comment.value = current_post;
}
}

function validateComment() {

if (document.comments.thename) var thename=document.comments.thename.value;
var comment=document.comments.comment.value;
illegal_chars = new RegExp("[^ \.A-Za-z0-9-_]");

document.comments.antibot.value = "passedJStest";

if (findLeetSpeak(comment)) return false;
if (findCurseWords(comment)) return false;
if (findTextAbuse(comment)) return false;
if (max_emoticons(comment, emoticons_max_number)) return false;

//g = perform a global match
//cleaning empty space and emoticons code
var comment_cleaned = comment.replace(/\s+/g, '');
var comment_cleaned = comment_cleaned.replace(/\[[a-z]+\]/g, '');
var real_comment_length = comment_cleaned.length;

if (document.comments.thename) {
	
	if ((thename == "") || (thename.length > 30)) {
	alert("Your guest name is empty or over the maximum of 30 characters");
	document.comments.thename.focus();
	return false;
	}
	else if (illegal_chars.test(thename)) {
	alert(thename.match(illegal_chars) + " is an illegal character in a name. You can only use alphanumerical characters, spaces, point, dash and underscore.");
	document.comments.thename.focus();
	return false;
	}
}

if (comment == "") {
	alert("Please insert your comment");
	document.comments.comment.focus();
	return false;
}
else if ((real_comment_length < 50) || (comment.length < 50)) {
	alert("Your comment has " + real_comment_length + " characters, the minimum is 50.\nPlease compose a more complete sentence.\nPosts stuffed with repetitive characters to avoid this message will be deleted.");
	document.comments.comment.focus();
	return false;
}
else if (comment == comment.toUpperCase()) { //avoiding all upper case posts

	alert("Please don't post in all uppercase, it is considered shouting");
	document.comments.comment.focus();
	return false;	
}

return true;
}


