//页面载入初始化
$(document).ready(function(){
	
	$("#replyTextArea").focus(
		function (){
			var tempContent=$("#replyTextArea").val();
			if(tempContent=="回复该话题" || tempContent=="发表新话题")
			{
				$("#replyTextArea").val("");
				$("#replyTextArea").css("color","black");
			}
	});
	$("#replyTextArea1").focus(
		function (){
			var tempContent=$("#replyTextArea1").val();
			if(tempContent=="文明上网，理性发言" || tempContent=="发表新话题")
			{
				$("#replyTextArea1").val("");
				$("#replyTextArea1").css("color","black");
			}
	});
	$("#replyTextArea2").focus(
		function (){
			var tempContent=$("#replyTextArea2").val();
			if(tempContent=="文明上网，理性发言" || tempContent=="发表新话题")
			{
				$("#replyTextArea2").val("");
				$("#replyTextArea2").css("color","black");
			}
	});
});

//验证话题或回复内容
function checkContent(content,type)
{
	var str1="发表话题失败！字数请在10-5000字内！";
	var str2="发表回复失败！字数请在2-1000字内！";
	if(content=="")
	{
		if(type==1)
		{
			alert(str1);
		}
		else
		{
			alert(str2);
		}
		return false;
	}
	if(type==1 && content=="发表新话题")
	{
		alert(str1);
		return false;
	}
	if(type==2 && content=="回复该话题")
	{
		alert(str2);
		return false;
	}
	if(type==3 && content=="文明上网，理性发言")
	{
		alert(str2);
		return false;
	}
	if(type==1)
	{
		if(content.length<10 || content.length>5000)
		{
			alert(str1);
			return false;
		}
	}
	else
	{
		if(content.length<2 || content.length>1000)
		{
			alert(str2);
			return false;
		}
	}
	return true;
}

//添加回复
function addreply(url,contenttext,cid,gameflag,checkflag,type)
{
	var value={content:contenttext,cid:cid,gameflag:gameflag,checkflag:checkflag};
	$.post(url,value,
		function (data){
			if(data=="-401")
			{
				alert("未知原因，主帖ID为空！");
			}
			else if(data=="-402")
			{
				alert("未知原因，回复内容为空！");
			}
			else if(data=="-403")
			{
				alert("游客评论已达每日最大数，若想继续评论，请登录！");
				window.location.href="/login.do";
			}
			else if(data=="-500")
			{
				alert("内容格式不规范，回复失败！");
			}
			else if(data=="-501")
			{
				alert("内容包含敏感信息，不允许发布！");
			}
			else if(data=="-502")
			{
				alert("请不要重复发表回复！");
			}
			else if(data=="-503")
			{
				alert("请不要频繁发表回复！");
			}
			else if(data=="-504")
			{
				alert("用户被加入黑名单，不能发表回复！");
			}
			else if(data=="-505")
			{
				alert("用户IP地址被加入黑名单，不能发表回复！");
			}
			else if(data=="-506")
			{
				alert("发表回复失败！字数请在2-1000字内！");
			}
			else if(data=="0")
			{
				var showText="";
				if(type==2)
				{
					showText="回复该话题";
				}
				if(type==3)
				{
					showText="文明上网，理性发言";
				}
				alert("回复成功！");
				window.location.reload();
				$("#replyTextArea").val(showText);
				$("#replyTextArea").css("color","#999999");
				$("#replyTextArea1").val(showText);
				$("#replyTextArea1").css("color","#999999");
				$("#replyTextArea2").val(showText);
				$("#replyTextArea2").css("color","#999999");
			}
			else
			{
				alert("你的禁言截止时间是:"+data);
			}
		}
	);
}

//发表话题
function addgamecomment(url,contenttext)
{
	var value={content:contenttext};
	$.post(url,value,
		function (data){
			if(data=="-401")
			{
				alert("未知原因，游戏没找到！");
			}
			else if(data=="-402")
			{
				alert("未知原因，话题内容为空！");
			}
			else if(data=="-403")
			{
				alert("游客发帖已达每日最大数，若想继续评论，请登录！");
				window.location.href="/login.do";
			}
			else if(data=="-500")
			{
				alert("内容格式不规范，发表话题失败！");
			}
			else if(data=="-501")
			{
				alert("内容包含敏感信息，不允许发表！");
			}
			else if(data=="-502")
			{
				alert("请不要重复发表话题！");
			}
			else if(data=="-503")
			{
				alert("请不要频繁发表话题！");
			}
			else if(data=="-504")
			{
				alert("用户被加入黑名单，不能发表话题！");
			}
			else if(data=="-505")
			{
				alert("用户IP地址被加入黑名单，不能发表话题！");
			}
			else if(data=="-506")
			{
				alert("发表话题失败！字数请在2-1000字内！");
			}
			else if(data=="0")
			{
				alert("发表话题成功！");
				window.location.reload();
				$("#replyTextArea").val("发表新话题");
				$("#replyTextArea").css("color","#999999");
				$("#replyTextArea1").val("发表新话题");
				$("#replyTextArea1").css("color","#999999");
				$("#replyTextArea2").val("发表新话题");
				$("#replyTextArea2").css("color","#999999");
			}
			else
			{
				alert("你的禁言截止时间是:"+data);
			}
		}
	);
}

//提交回复
function onsubmitreply(textid,type,url,cid,gameflag,checkflag)
{
	var commentContent=$("#"+textid).val();
	if(checkContent(commentContent,type))
	{
		addreply(url,commentContent,cid,gameflag,checkflag,type);
	}
}

//提交游戏话题
function onsubmitgamecomment(textid,type,url)
{
	var commentContent=$("#"+textid).val();
	if(checkContent(commentContent,type))
	{
		addgamecomment(url,commentContent);
	}
}
