var check;
var reply;
var comid;
var annonymous;
function comment(checkOccupation,r,c,a)
{
	check=checkOccupation;
	reply=r;
	comid=c;
	annonymous=a;
	var email=document.getElementById("email");
	var pass=document.getElementById("pass");
	var code=document.getElementById("code");
	var words=document.getElementById("words");
	if(!annonymous)
	{
		if(email!=null)
		{
			if(email.value.trim().length==0)
			{
				alert("请输入注册邮箱");
				email.focus();
				return false;
			}
			if(email.value.trim().indexOf(" ")!=-1||email.value.trim().indexOf("\u3000")!=-1)
			{
				alert("注册邮箱不能含有空格");
				email.focus();
				return false;
			}
			if(pass.value.trim().length==0)
			{
				alert("请输入密码");
				pass.focus();
				return false;
			}
			if(pass.value.trim().indexOf(" ")!=-1||pass.value.trim().indexOf("\u3000")!=-1)
			{
				alert("密码不能含有空格");
				pass.focus();
				return false;
			}
		}
	}
	if(words.value.trim().length==0)
	{
		alert("请输入留言");
		words.focus();
		return false;
	}
	if(words.value.trim().length<5)
	{
		alert("留言最小长度限制为5个字符");
		words.focus();
		return false;
	}
	if(words.value.trim().length>400)
	{
		alert("留言最大长度限制为400个字符");
		words.focus();
		return false;
	}
	var re=/((http|https):\/\/)?(www.)?(\w|@)+\.(com|cn|net|org|us|biz|tv|jp)/gi;
	var addr=words.value.trim().match(re);
	if(addr!=null)
	{
		for(var i=0;i<addr.length;i++)
		{
			if(addr[i].indexOf("fwbao.com")==-1)
			{
				if(addr[i].indexOf("@")==-1)
				{
					alert("留言内容中不能包含外部链接");
					words.focus();
					return false;
				}
			}
		}
	}	
	if(code.value.trim().length==0)
	{
		alert("请输入验证码");
		if(document.getElementById("codeSpan").style.display=="")
		{
			code.focus();	
		}
		return false;
	}
	if(code.value.trim().length!=5)
	{
		alert("验证码长度为5");
		code.focus();
		return false;
	}
	document.getElementById("loading").style.display="";
	document.getElementById("commentButton").disabled=true;
	if(email!=null)
	{
		pass.value=hex_md5(pass.value.trim());
		checkLogin(email.value.trim(),pass.value,code.value.trim());	
	}
	else
	{
		checkLogin("<","<",code.value.trim());
	}
	return false;
}

var xmlhttp=null;

function checkLogin(email,pass,code)
{
	xmlhttp=ajax();
	xmlhttp.onreadystatechange=handleCheckLogin;
	xmlhttp.open("POST","/checkLogin.aspx",true);
	if(check)
	{
		if(reply||annonymous)
		{
			xmlhttp.send(code);
		}
		else
		{
			xmlhttp.send(email+" "+pass+" "+code+" y");	
		}
	}
	else
	{
		if(reply||annonymous)
		{
			xmlhttp.send(code);
		}
		else
		{
			xmlhttp.send(email+" "+pass+" "+code+" n");	
		}
	}
}

function handleCheckLogin()
{
	if(xmlhttp.readyState==4)
	{
		if(xmlhttp.status==200)
		{
			if(reply||annonymous)
			{
				if(xmlhttp.responseText!="0")
				{
					enableControl();
					alert("验证码错误");
					document.getElementById("code").focus();
				}
				else
				{
					filter();
					if(annonymous)
					{
						document.commentForm.action=document.commentForm.action+"&annonymous=";	
					}
					if(reply)
					{
						document.commentForm.action=document.commentForm.action+"&comid="+comid;	
					}
					document.commentForm.submit();
				}
			}
			else
			{
				if(xmlhttp.responseText=="1")
				{
					enableControl();
					alert("验证码错误");
					document.getElementById("code").focus();
				}
				else if(xmlhttp.responseText=="2")
				{
					enableControl();
					alert("登录失败,请检查您的注册邮箱和登录密码");
					document.getElementById("email").focus();
				}
				else if(xmlhttp.responseText=="5")
				{
					enableControl();
					alert("连接服务器失败");
				}
				else if(xmlhttp.responseText=="4")
				{
					enableControl();
					parent.promptMsg();
				}
				else
				{
					filter();
					document.commentForm.action=document.commentForm.action;
					document.commentForm.submit();
				}
			}
		}
		else
		{
			enableControl();
			alert("连接服务器失败");
		}
	}
}

function enableControl()
{
	document.getElementById("loading").style.display="none";
	document.getElementById("commentButton").disabled=false;
}

function filter()
{
	var w=document.getElementById("words");
	w.value=w.value.replace(/</g,"&lt;");
	w.value=(w.value.replace(/>/g,"&gt;")).trim();
}

function resize()
{
	var iframe=parent.document.getElementById("commentiFrame");
	iframe.height=iframe.contentWindow.document.documentElement.scrollHeight;
}

function createCode()
{
	document.getElementById("promptSpan").style.display="none";
	document.getElementById("codeImg").src="/vc_small.aspx";
	document.getElementById("codeSpan").style.display="";
	document.getElementById("code").focus();
}

function replyComment(comid,to)
{
	document.getElementById("reply").innerHTML="回复："+to;
	window.scrollTo(0,0);
	document.getElementById("words").focus();
	document.getElementById("commentButton").style.display="none";
	document.getElementById("ann").style.display="none";
	document.getElementById("replySpan").innerHTML="<input type='button' onClick=comment(false,true,'"+comid+"',false) value='回复留言'>";
}

function replyComment2(comid,to)
{
	document.getElementById("reply").innerHTML="回复："+to;
	document.getElementById("words").focus();
	document.getElementById("commentButton").style.display="none";
	document.getElementById("ann").style.display="none";
	document.getElementById("replySpan").innerHTML="<input type='button' onClick=comment(false,true,'"+comid+"',false) value='回复留言'>";
}

function deleteComment(comid,domain)
{
	if(confirm("您确定要删除该留言吗?"))
	{
		if(domain)
		{
			location.href="comments.aspx?del="+comid+"&domain=";
		}
		else
		{
			location.href="comments.aspx?del="+comid;
		}
	}
}

function deleteComment2(tid,comid,domain)
{
	if(confirm("您确定要删除该留言吗?"))
	{
		if(domain)
		{
			location.href="comments.aspx?del="+comid+"&domain=";
		}
		else
		{
			location.href="comments.aspx?tid="+tid+"&del="+comid;
		}
	}
}

function deleteComment3(cid,comid,domain)
{
	if(confirm("您确定要删除该留言吗?"))
	{
		if(domain)
		{
			location.href="caseDetail.aspx?cid="+cid+"&del="+comid+"&domain=";	
		}
		else
		{
			location.href="caseDetail.aspx?cid="+cid+"&del="+comid;
		}
	}
}

function preComment()
{
	if(document.getElementById("annonymous").checked)
	{
		comment(false,false,null,true);
	}
	else
	{
		comment(false,false,null,false);
	}
}

function shiftBox(box)
{
	var loginP=document.getElementById("loginP");
	if(box.checked)
	{
		if(loginP!=null)
		{
			loginP.style.display="none";	
		}
	}
	else
	{
		if(loginP!=null)
		{
			loginP.style.display="";	
		}
	}
}