// $Id: view_article.js 350 2008-12-12 19:00:00Z rfludwick $

// Posts a comment
function postComment(event)
{
	// Check that there is a comment
	var comment = $F("comment");

	if (!comment)
	{
		alert("You cannot post an empty comment.");
	}
	else
	{
		new Ajax.Request(WEB_ROOT + "ajax/article_post_comment",
		{
			method : "post",
			parameters :
			{
				"comment" : comment,
				"article_id" : $F("article_id")
			},
			onFailure : AjaxBase.alertError,
			onSuccess : function(request)
			{
				// Check for some sort of error
				if (!request.responseText.isJSON())
				{
					AjaxBase.alertError();
				}
				else
				{
					var results = request.responseText.evalJSON(true);

					if (results["error"])
					{
						alert(results["error"]);
					}
					else
					{
						// Show the new comment at the top nad clear the form
						$("article_comments_title").insert({ "after" : "<div class=\"article_comment\">" +
								"<div class=\"article_comment_author\">" + results["user"] + "</div>" +
								"<div class=\"article_comment_date\">" + results["date"] + "</div>" +
								"<p>" + results["comment"] + "</p>" +
							"</div>" });

						$("comment").clear();

						// Oh, remove any "first to post" text there may be
						var article_comment_first_post = $("article_comment_first_post");

						if (article_comment_first_post)
						{
							article_comment_first_post.remove();
						}
					}
				}
			}
		});
	}

	// Stop the event
	Event.stop(event);
}