var empty_error = "The comment should not be empty!";

$(document).ready(function()  {
   $("textarea").attr("value", "");
});

function postComment()  {
   var text = $(".blog-entry form textarea").attr("value");
   if ("undefined" == typeof(text) || 0 == text.length) {
      alert(empty_error);
      return;
   }
   
   $.post(HOME + "/serv.php?command=comment&" + Math.random(), {blog_id: blog_id, text:text}, function(xml, textStatus)   {
      try  {
         var response = $("response", xml);
         if (0 == response.length) throw {message:"invalid response"};
         if ("error" == response.attr("status")) throw {message: response.attr("message")};
         if ("ok" != response.attr("status")) throw {message: "unknown response"};

//         TODO: showing new comment
         var new_comment = $(".template-comment").clone();
         var new_text = response.attr("text");
         var new_h4 = response.attr("h4");
         var new_cnt = response.attr("cnt");

         $("p", new_comment).text(new_text);
         $("h4", new_comment).text(new_h4);
         new_comment.removeClass("none template-comment");
         $(".comments").append(new_comment);
         $(".comments").removeClass("none");
         $("#comments-count").text(new_cnt);

         $("textarea").attr("value", "");
      } catch(e)   {
         alert("An error during registration: " + e.message);
      }
      
   });
}