$(document).ready(function() {
	// Submit Comment
	$("#submitComment").bind("click", function (e) {
		// Chances are we have multiple hidden fields with the same values i.e. imageID
		// Restrict by form class name to get the values
		var page = $("form.commentsForm input[name=commentPage]").val();
		var module = $("form.commentsForm input[name=commentModule]").val();
		var primaryKey = $("form.commentsForm input[name='pKey']").val();
		var imageID = $("form.commentsForm input[name='imageID']").val();
		var comments = FCKeditorAPI.GetInstance('comments').GetXHTML(); /* Use FCKEditorAPI to get contents - won't work with jQuery */

		if (comments != "") {
			$("#comments_error").remove();
			$.post(
				"ajax/ajax.Comments.php",
				{
					action: 'addComment',
					page: page,
					module: module,
					primaryKey: primaryKey,
					imageID: imageID,
					comments: comments
				},
				function (data) {
					FCKeditorAPI.GetInstance('comments').SetHTML(""); // Clear comments box
					
					// Reset image
					$("#commentImageUpload img.image").attr("src", "ajax/ajax.ImageResize.php?imageID=0&width=115&quality=90");
					$("form.commentsForm input[name='imageID']").val("");
					$("form.commentsForm input[name='imageIDs']").val("");
					$("form.commentsForm input[name='commentImageUpload']").val("");
					
					$("#commentArea").html(data);
				}
			);
		} else {
			// Content exists - remove error message
			if ($("#comments_error").length == 0) {					  
				$("#comments___Frame").after("<label id=\"comments_error\" for=\"comments\" class=\"fckValidateError\" style=\"display: block;\">Please complete this field.</label>");	
				FCKeditorAPI.GetInstance("comments").Events.AttachEvent( 'OnSelectionChange', CheckCommentsFCKEditor ) ;
			}
		}
	});
	
	function CheckCommentsFCKEditor(editorInstance) {
		var contentHTML = editorInstance.GetXHTML();
		if (contentHTML != "") {
			// Content exists - remove error message
			if ($("#comments_error").length > 0) {					  
				$("#comments_error").remove();
			}
		}
	}
});
