function postFilter() {
	theForm = document.getElementById('filterForm');
	if (theForm != null) 
		theForm.submit();
}

function imposeMaxLengthComment(Object, MaxLen) {
	if(Object.value.length > MaxLen) { 
          alert('Only ' + MaxLen + ' characters are permitted for comments in the forum.'); 
          Object.value = Object.value.substr(0, MaxLen); 
          return false; 
    }
}

function validateTopicForm() {
	var errs = "";
	if (document.getElementById("topic").value.length == 0) {
		errs += "A topic name is required.\n";
	}
	if (document.getElementById("newTopicCommentTextArea").value.length == 0) {
		errs += "Comments are required.";
	}
	if (errs.length > 0) {
		alert(errs);
		return false;
	}
	return true;
}

var commentTextAreaFocused = false;
function removeTextOnFirstFocus(object) {
	if (!commentTextAreaFocused) {
		object.value = '';
		commentTextAreaFocused = true;
		jQuery('#addCommentButton').fadeIn('slow', function() {
	        // Animation complete
	      });
	}
}

function addDefaultTextOnEmptyTextAreaUnFocus(object) {
	if (commentTextAreaFocused) {
		object.value = jQuery.trim(object.value);
		if (object.value == '') {
			object.value= 'Add a comment';
			jQuery('#addCommentButton').fadeOut('fast', function() {
		        // Animation complete
		      });
			commentTextAreaFocused = false;
		}
	}
}

function resetCommentText() {
	jQuery("#topicAddCommentTextArea").val('Add a comment');
	commentTextAreaFocused = false;
}

function addTopicComment() {
		jQuery('#topicAddCommentTextArea').val(jQuery.trim(jQuery('#topicAddCommentTextArea').val()));
		var comment = jQuery('#topicAddCommentTextArea').val();
		if (comment.length == 0 ||
				comment == "Add a comment") {
			alert("You must specify a comment.");
		} else {
			//serialize the form
			try {
				var data = jQuery("#addCommentForm").serialize();	
				jQuery.ajax({  
					url : "/ajax.php?action=addcomment",
				    type: "post", 
					data: data,
					success: function(responseData) { 
						if (responseData == "false" || responseData == "") {
							alert("Sorry, there was an error posting your comment. Please try again later.");
						} else {
							var addedId = responseData;
							
							//now try and update the id="commentWrapper" div to contain the new comments
							jQuery.ajax({
								url : "/ajax.php?action=getaddedcomment&commentId=" + addedId,
								success: function(responseDataDiv) {
									jQuery("#commentWrapper").append(responseDataDiv);
									resetCommentText();
								}
							})
						}				
					},
					error: function() { 
						alert("Sorry, there was an error posting your comment. Please try again later.");
			        }
				});
			} catch (e) {
				alert('Oops, sorry there was a problem posting your comment. Please try again later.' );
			}
		}
		return false;
}


