function getVerificationCode()
{
		if(!$("#validateImage").attr("src")){
			changeVerificationCode();
		}
		$("#show_message").html("");
}

function changeVerificationCode()
{
	$("#img_span").show();
	var url = "/comment/validateCode.action?id=" + __vars__.commentUniqueResourceId + "&timeStamp=" + (new Date().getTime());
	//用timeout防止在傲游这堆稀奇古怪的浏览器里显示不出来
	setTimeout(function(){
		$("#validateImage").attr("src",url);
	},1);
}
function publishComment(releaseUrl,releaseData)
{
	if(!validate(releaseData))
	{
		return;
	}

	jQuery.ajax({
			type:"POST",
			url:releaseUrl,
			data:releaseData,
			dataType:"json",
			success:function(data){
				if(!data.hasError)
				{
					$("#show_message").html(data.actionMessage || "评论发表成功，待系统刷新后可见...");
					$("textarea[name]").val("");
					$("input[name='validateCode']").val("");
					$("label[name='commentCount']").each(function(){
						$(this).html(data.totalCount);
					});
					changeVerificationCode();
				}
				else if(data.hasError == true)
				{
					$("#show_message").html(data.errorMessage);
				}
				else
				{
					$("#show_message").html("评论发表失败");
				}
				changeVerificationCode();
				//window.location.reload();
			}
	});
}


function validate(data)
{
	if($.trim(data.content)=='')
	{
		//alert("发表评论的内容不能为空!");
		alert("请输入评论的内容.");
		return false;
	}
	if(data.content.length > 2000){
		alert("输入的字符不能超过2000");
		return false;
	}
	if($.trim($("input[name='validateCode']").val())=='')
	{
		alert("请输入验证码.");
		return false;
	}
	return true;
}

function onAgreeClick(id){
	var url = "/comment/agree.action";
	var data = "commentId=" + id;
	jQuery.ajax({
		type:"POST",
		url:url,
		data:data,
		dataType:"json",
		success:function(data){
			if(data == null){
				//alert("您已经对该评论投过票了.");
			}else{
				$("label[name='"+data.commentId+"']").each(function(){
					$(this).html(data.count);
				});
			}
		}
	});
}
 function onDisagreeClick(id){
	var url = "/comment/disagree.action";
	var data = "commentId=" + id;
	jQuery.ajax({
		type:"POST",
		url:url,
		data:data,
		dataType:"json",
		success:function (data){
			if(data == null){
				//alert("您已经对该评论投过票了.");
			}else{
				$("em[name='"+data.commentId+"']").each(function(){
					$(this).html(data.discount);
				});
			}
		}
	});
}



jQuery(function($){

	$("#commentForm").submit(function(){
		return false;
	});

	$("#btnAddComment").click(function(){
		$("#commentContent").focus();
	});

	$(".replyContent").click(function(){
		$("#commentContent").focus();
		var replyId = $(this).attr("_value");
		$("#replyId").val(replyId);
	});

	$(".agree").click(function(){
		$(this).text("已支持").unbind();
	});

	$(".disagree").click(function(){
		$(this).text("已反对").unbind();
	});

	$("#commentContent").blur(function(){
		if($.trim($("#commentContent").val())=='')
		{
			$("#replyId").val("0");
		}
	});
	
	$("#txtValidateCode").focus(function(){
		getVerificationCode();
	});
	$("#commentContent").focus(function(){
		getVerificationCode();
	});

	$("#changeVerificationCode").click(function(){
		changeVerificationCode();
	});

	$("#txtValidateCode").keypress(function(evt){
		if(evt.keyCode == 13){
			submitComment();
		}
	});

});