<!--#include file="conn.asp"-->
<!--#include file="inc/const.asp"-->
<!--#include file="inc/ubbcode.asp"-->
<!--#include file="inc/check.asp"-->
<%
Dim Action,TopicID,TopicUseTable,CloseTopic
Dim AskTopic,classid,classname,Quserid,PostUsername
Dim Expired,Closed,DateAndTime,Reward,TopicMode,supplement
Dim allowAnswers,allowComment,islock
allowAnswers = NewAsp.ChkNumeric(NewAsp.Posts_Setting(1))
allowComment = NewAsp.ChkNumeric(NewAsp.Posts_Setting(2))
Action = NewAsp.CheckBadstr(Request("action"))
TopicID = NewAsp.ChkNumeric(Request.Form("TopicID"))
CloseTopic = 0 : islock = 0

Select Case LCase(Action)
	Case "savequestion"
		Call savequestion()
	Case "supplyquestion"
		Call savequestion()
	Case "saveanswer"
		Call saveanswer()
	Case "savecomment"
		Call savecomment()
	Case Else
		'Call showmain()
End Select

NewAsp.CloseConn()

Sub savequestion()
	
End Sub

Sub saveanswer()
	If NewAsp.FoundErr Then Exit Sub
	Dim Rs,SQL,TextContent,UserNowPoint,TextLength
	If allowAnswers = 0 Then
		NewAsp.FoundErr = True
		Response.Write "<script>alert('友情提示!\n\n本站暂时禁止回答问题!');</script>"
		Exit Sub
	End If
	TextContent = Request.Form("TextContent")
	If TopicID = 0 Then
		Response.Write "<script>alert('友情提示!\n\n错误的系统参数!');</script>"
		Exit Sub
	End If
	If Len(TextContent) < 2 Then
		Response.Write "<script>alert('友情提示!\n\n请填写正确的答案内容!');</script>"
		Exit Sub
	End If
	TextContent = UBB2Html(TextContent)
	TextLength = NewAsp.strLength(TextContent)
	If TextLength < CLng(NewAsp.Posts_Setting(16)) Then
		Response.Write "<script>alert('友情提示!\n\n问题回答不能小于 " & NewAsp.Posts_Setting(16) & " 个字节!');</script>"
		Exit Sub
	End If
	If TextLength > CLng(NewAsp.Posts_Setting(17)) Then
		Response.Write "<script>alert('友情提示!\n\n问题回答不能大于 " & NewAsp.Posts_Setting(17) & " 个字节!');</script>"
		Exit Sub
	End If
	If NewAsp.ChkRefresh Then
		Response.Write "<script>alert('友情提示!\n\n本页面起用了防刷新机制,请不要连续刷新本页面!');</script>"
		Exit Sub
	End If
	
	Call LoadTopicInfo(0)
	If TopicMode <> 0 Then
		Response.Write "<script>alert('友情提示!\n\n错误的系统参数!');</script>"
		Exit Sub
	End If
	If NewAsp.ChkBadword(TextContent) Then
		islock=0
	Else
		islock=1
	End If
	If NewAsp.FoundErr Then Exit Sub
	Set Rs = NewAsp.CreateAXObject("ADODB.Recordset")
	SQL = "SELECT * FROM NC_Ask_Answer WHERE TopicID="& TopicID &" And userid="& NewAsp.UserID
	Rs.Open SQL,Conn,1,3
	If Rs.BOF And Rs.EOF Then
		Rs.Addnew
		Rs("TopicID") = TopicID
		Rs("classid") = classid
		Rs("classname") = classname
		Rs("userid") = NewAsp.UserID
		Rs("Username") = NewAsp.UserName
		Rs("PostUserID") = Quserid
		Rs("PostUsername") = PostUsername
		Rs("title") = AskTopic
		Rs("AnswerTime") = Now()
		Rs("PostTable") = TopicUseTable
		Rs("AnswerNum") = 1
		Rs("AnswerMode") = 0
		Rs("TopicMode") = 0
		Rs.Update
	Else
		If Rs("AnswerNum") > 1 Then
			Response.Write "<script>alert('友情提示!\n\n你已经多次提交答案!');</script>"
			Exit Sub
		End If
		Rs("AnswerTime") = Now()
		Rs("AnswerNum") = Rs("AnswerNum") + 1
		Rs.Update
	End If
	Rs.Close:Set Rs = Nothing
	Set Rs = NewAsp.CreateAXObject("ADODB.Recordset")
	SQL = "SELECT * FROM " & TopicUseTable & " WHERE (postsid is null)"
	Rs.Open SQL,Conn,1,3
	Rs.Addnew
		Rs("classid") = classid
		Rs("TopicID") = TopicID
		Rs("userid") = NewAsp.UserID
		Rs("UserName") = NewAsp.UserName
		Rs("topic") = AskTopic
		Rs("content") = TextContent
		Rs("addText") = ""
		Rs("PostTime") = Now()
		Rs("DoneTime") = Now()
		Rs("length") = TextLength
		Rs("star") = 0
		Rs("satis") = 0
		Rs("LockTopic") = islock
		Rs("PostsMode") = 1
		Rs("VoteNum") = 0
		Rs("Plus") = 0
		Rs("Minus") = 0
		Rs("PostIP") = NewAsp.UserTrueIP
		Rs("Report") = 0
	Rs.Update
	Rs.Close:Set Rs = Nothing
	UserNowPoint = UserPoints + NewAsp.ChkNumeric(NewAsp.Point_Setting(8))
	'If Reward > 0 Then UserNowPoint = UserNowPoint + Reward
	NewAsp.Execute ("UPDATE NC_Ask_Users SET Points=" & UserNowPoint & ",Experience=Experience+" & NewAsp.ChkNumeric(NewAsp.Point_Setting(9)) & ",Asktotal=Asktotal+1,Answertotal=Answertotal+1 WHERE userid="& NewAsp.UserID)
	NewAsp.Execute ("UPDATE NC_Ask_Topic SET PostNum=PostNum+1,LastPostTime=" & NowString & " WHERE TopicID="& TopicID)
	Dim Direct
	Direct = NewAsp.ChkNumeric(Request.Form("direct"))
	Response.Write "<script>"
	If Direct = 0 Then Response.Write "alert('恭喜您!答案提交成功');"
	Response.Write "try{top.location.replace(document.referrer);}catch(e){}</script>"
End Sub

Sub savecomment()
	If NewAsp.FoundErr Then Exit Sub
	Dim Rs,SQL,TextContent,UserNowPoint,TextLength
	If allowComment = 0 Then
		NewAsp.FoundErr = True
		Response.Write "<script>alert('友情提示!\n\n本站暂时禁止发表评论!');</script>"
		Exit Sub
	End If
	TextContent = Request.Form("TextContent")
	If TopicID = 0 Then
		Response.Write "<script>alert('友情提示!\n\n错误的系统参数!');</script>"
		Exit Sub
	End If
	If Len(TextContent) < 5 Then
		Response.Write "<script>alert('友情提示!\n\n请填写正确的评论内容!');</script>"
		Exit Sub
	End If
	TextContent = UBB2Html(TextContent)
	TextLength = NewAsp.strLength(TextContent)
	If TextLength > 500 Then
		Response.Write "<script>alert('友情提示!\n\n评论内容字符过长!');</script>"
		Exit Sub
	End If
	If NewAsp.ChkRefresh Then
		Response.Write "<script>alert('友情提示!\n\n本页面起用了防刷新机制,请不要连续刷新本页面!');</script>"
		Exit Sub
	End If
	Call LoadTopicInfo(1)
	If TopicMode <> 1 Then
		Response.Write "<script>alert('友情提示!\n\n错误的系统参数!');</script>"
		Exit Sub
	End If
	If NewAsp.ChkBadword(TextContent) Then
		islock=0
	Else
		islock=1
	End If
	If NewAsp.FoundErr Then Exit Sub
	Set Rs = NewAsp.CreateAXObject("ADODB.Recordset")
	SQL = "SELECT * FROM NC_Ask_Comment WHERE (CommentID is null)"
	Rs.Open SQL,Conn,1,3
	Rs.Addnew
		Rs("TopicID") = TopicID
		Rs("userid") = NewAsp.UserID
		Rs("UserName") = NewAsp.UserName
		Rs("content") = TextContent
		Rs("PostTime") = Now()
		Rs("PostIP") = NewAsp.UserTrueIP
		Rs("islock") = islock
	Rs.Update
	Rs.Close:Set Rs = Nothing
	NewAsp.Execute ("UPDATE NC_Ask_Users SET Points=Points+1,Experience=Experience+1 WHERE userid="& NewAsp.UserID)
	NewAsp.Execute ("UPDATE NC_Ask_Topic SET CommentNum=CommentNum+1 WHERE TopicID="& TopicID)
	Newasp.Execute ("UPDATE NC_Ask_Setup SET MaxCommentNum=MaxCommentNum+1")
	NewAsp.ReloadSetupCache Clng(NewAsp.MaxCommentNum)+1,11
	Response.Write "<script>alert('恭喜您!提交评论成功!');top.location.replace(document.referrer);</script>"
End Sub

Sub LoadTopicInfo(iMode)
	Dim SQL,Rs
	SQL = "SELECT TopicID,classid,classname,userid,title,PostUsername,Expired,Closed,PostTable,DateAndTime,Reward,PostNum,CommentNum,TopicMode,supplement FROM NC_Ask_Topic WHERE TopicID="&TopicID&" And TopicMode="&iMode&" And LockTopic=0"
	Set Rs = Newasp.Execute(SQL)
	If Rs.BOF And Rs.EOF Then
		NewAsp.FoundErr = True
		Response.Write "<script>alert('友情提示!\n\n问题已经处理,不能回答!');</script>"
		Exit Sub
	Else
		TopicID = Rs("TopicID")
		classid = Rs("classid")
		classname = Rs("classname")
		Quserid = Rs("userid")
		AskTopic = Rs("title")
		PostUsername = Rs("PostUsername")
		Expired = Rs("Expired")
		Closed = Rs("Closed")
		TopicUseTable = Trim(Rs("PostTable"))
		DateAndTime = Rs("DateAndTime")
		Reward = Rs("Reward")
		TopicMode = Rs("TopicMode")
		supplement = Rs("supplement")
		If iMode=0 And Rs("PostNum") > 10 Then
			CloseTopic = 1
		End If
		If iMode=1 And Rs("CommentNum") > 100 Then
			CloseTopic = 1
		End If
		If Closed = 1 Then
			NewAsp.FoundErr = True
			Response.Write "<script>alert('友情提示!\n\n问题已关闭不能提交答案!');</script>"
			Exit Sub
		End If
		If CloseTopic = 1 Then
			NewAsp.FoundErr = True
			Response.Write "<script>alert('友情提示!\n\n问题已关闭不能提交答案!');</script>"
			Exit Sub
		End If
		If iMode=0 And Quserid = NewAsp.UserID Then
			NewAsp.FoundErr = True
			Response.Write "<script>alert('友情提示!\n\n不能回答自己提出的问题!');</script>"
			Exit Sub
		End If
	End If
	Set Rs = Nothing
End Sub

%>
<script language="JavaScript">
<!--
	//parent.location.reload();
	//window.top.location.reload(true);
	//top.location.replace(document.referrer);
//-->
</script>