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

$(document).ready(function(){ 
    $("#btnCommentSubmit").click(function(){

	  id = $("#comment-submit-form #nodeID").val();
	  commentid = $("#comment-submit-form #commentID").val();

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

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

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

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

	    commanage_send_login();
	}
    });
});

function add_comment()
{
    var comment_content = $("#comment-submit-form #comment").val();
    var identifiying_code_txt = $("#comment-submit-form #identifiying-code-txt").val();

    if( comment_content == '' || comment_content == "请在此发表评论，限1000字内。" )
    {
	alert("抱歉!评论文字不能为空，感谢您的支持！");
        $("#comment-submit-form #comment").focus();
    }
    else if( comment_content.length > 1000 )
    {
	alert("字数过多，建议您分段发表。谢谢支持！");
        $("#comment-submit-form #comment").focus();
    }
    else if( identifiying_code_txt == '' )
    {
        alert("抱歉!验证码不能为空，感谢您的支持！");
        $("#comment-submit-form #identifiying-code-txt").focus();
    }
    else if( !check_identifiying_code( identifiying_code_txt ) )
    {
        alert("抱歉!输入的验证码不正确，请重新输入感谢您的支持！");
        $("#comment-submit-form #identifiying-code-txt").focus();
    }
    else{
        $.ajax({ 
      	     type: "POST", 
	     url: "/commanage/add_comment/" + id + "/" + commentid, 
	     data: "isanonymous=" + isanonymous + "&add=add&comment=" + encodeURIComponent($("#comment-submit-form #comment").val()), 
	     success: function( responseText ){
	         alert("感谢您的留言！留言需经过审核，请稍后查看。");
	         reload_comments( responseText );
	     } 
        });
    }
}

function check_identifiying_code( code )
{
    var	txtcaptcha=code.toLocaleLowerCase();
    var res = "";
    $.ajax({
              type: "GET",
              url: "/ajaxext/validatecaptcha/captcha/"+txtcaptcha,
	      async: false,
              success: function( ajax ){
	          res=eval("("+ajax+")");
	      }
           });
    return res.res;
}

function reload_comments( responseText )
{
	var res = eval('('+responseText+')');
	if(res.res)
	{
	    if( $("#comment-submit-form").find(".url_redirect").val() != "" )
	    {
	        location.href = $("#comment-submit-form").find(".url_redirect").val();
	    }
	    else
	    {
	        location.reload();
	    }
	}
}

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

    var formstring = $("#comment-submit-form").serialize();

    $.ajax({ 
      type: "POST", 
      url: "/action/login", 
      async: true,
      data: formstring,
      success: function( responseText ){
          commanage_after_login(responseText);
      },
      error: function(){
          alert("Error loading");
      }
   });
}

function commanage_after_login(responseText)
{
    var res = eval('('+responseText+')');

    if(res.res)
    {
        add_comment();
    }
    else
    {
	alert("会员名称或密码错误！");
    }
}

