var id="";
var commentid="";
var isanonymous="";

(function($){
     $(document).ready(function(){
	 $("#comment-submit-btn").click(function(){
	      var comment_content = $("div.comments-submit #comment").val();
	      var identifiying_code_txt = $("#identifiying-code-txt").val();

	      if( comment_content == '' || comment_content == "请在此发表评论，限1000字内。" )
              {
		  $("div.comments-submit #submit-warning").html( "抱歉!评论文字不能为空，感谢您的支持！" ); 

 	          $("div.comments-submit #submit-warning").show();

		  $("div.comments-submit #comment").focus();

		  return false;
              }
              else if( comment_content.length > 1000 )
              {
		  $("div.comments-submit #submit-warning").html( "字数过多，建议您分段发表。谢谢支持！" ); 

 	          $("div.comments-submit #submit-warning").show();

		  $("div.comments-submit #comment").focus();

		  return false;
    	      }
	      else if( identifiying_code_txt == '' )
	      {
		  $("div.comments-submit #submit-warning").html( "抱歉!验证码不能为空，感谢您的支持！" ); 

 	          $("div.comments-submit #submit-warning").show();

		  $("#identifiying-code-txt").focus();

		  return false;
	      }
	      else
	      {
	          $("div.comments-submit #submit-warning").hide();
	      }

	      if( $("div.comments-submit #ckbAnonymity").attr("checked") == true )
	      {
	          isanonymous = "checked";
	      }

	      if( identifiying_code_txt != '' )
	      {
		   if( !check_identifiying_code( identifiying_code_txt ))
		   {
			   return false;
		   }
	      }

	      if( $("#comment-login").length > 0 && $("#comment-password").length > 0 )
	      {
	          if( $("#comment-login").val() != ""  && $("#comment-password").val() != "" )
	          {
	              isanonymous = "";

		      commanage_send_login();
	          }
    	          else
	          {
		      add_comment();
	          }
    	      }
	      else
	      {
		  add_comment();
	      }
	 });

	 $("#comment-password").keydown(function(evt){
	      if(evt.keyCode == 13)
	      {
	          isanonymous = "";

	          commanage_send_login();
	      }
         });
     });

     function add_comment()
     {
	 alert("感谢您的留言！留言需经过审核，请稍后查看。");

	 reload_comments();
     }

     function reload_comments()
     {
	 location.href = '/';
     }

     function commanage_send_login()
     {
         var userlogin = $("#comment-login").val();
         var userpassword = $("#comment-password").val();

	 if( userlogin == 'admin' && userpassword == 'publish' )
	 {
	     commanage_after_login();
	 }
	 else
	 {
	     $("div.comments-submit #submit-warning").show();
	     $("div.comments-submit #submit-warning").html( "用户名或密码错误！用户名admin，密码publish" );
	     return false;
	 }
     }

     function commanage_after_login()
     {
         add_comment();
     }

     function check_identifiying_code( code )
     {
	 if( code == 'HJDE' || code == 'hjde' )
	 {
	     return true;
	 }
	 else
	 {
	     $("div.comments-submit #submit-warning").show();
	     $("div.comments-submit #submit-warning").html( "验证码错误,请重新填写！" );
	     $("#identifiying-code-txt").focus();
	     return false;             
	 }
     }

})(jQuery);

