<!--#include file="conn.asp"-->
<!--#include file="inc/const.asp"-->
<!--#include file="inc/ubbcode.asp"-->
<%
Dim HtmlContent,XMLDom,TopicUseTable
Dim topicid,classid,classname,child,HeadTitle,topicmode,HeadCategory,isnowuser
Dim CurrentPage,CloseTopic,CommentNum,PostNum,RemainDays,RemainHour,ExpiredTime,icons
Dim allowAnswers,allowComment,allowDeletes,PostUseridlist
allowAnswers = NewAsp.ChkNumeric(NewAsp.Posts_Setting(1))
allowComment = NewAsp.ChkNumeric(NewAsp.Posts_Setting(2))
allowDeletes = NewAsp.ChkNumeric(NewAsp.Posts_Setting(5))
CurrentPage = NewAsp.ChkNumeric(Request("page"))
If CurrentPage = 0 Then CurrentPage = 1
topicid = NewAsp.ChkNumeric(Request("topicid"))
icons = 0
isnowuser = 0
MainQuestion()

NewAsp.CloseConn()

Public Sub MainQuestion()
	If topicid = 0 Then Exit Sub
	HtmlContent = NewAsp.LoadTemplate("question")
	QuestionTopic()
	CategoryInfo()
	LoadQuestionList()
	HtmlContent = Replace(HtmlContent, "{$HeadTitle}", HeadTitle)
	'HtmlContent = Replace(HtmlContent, "{$installdir}", NewAsp.InstallDir)
	Response.Write NewAsp.ArchiveHtml(HtmlContent)
End Sub

Sub QuestionTopic()
	If topicid = 0 Then Exit Sub
	Dim SQL,Rs,Node
	CloseTopic = 0

	SQL="SELECT TopicID,classid,userid,classname,title,PostUsername,Expired,Closed,PostTable,DateAndTime,LastPostTime,ExpiredTime,LockTopic,Reward,Hits,PostNum,CommentNum,TopicMode,Broadcast,Anonymous,supplement FROM NC_Ask_Topic WHERE topicid="&topicid
	Set Rs = NewAsp.Execute(SQL)
	If Rs.BOF And Rs.EOF Then
		Set Rs = Nothing
		NewAsp.FoundErr = True
		Exit Sub
	End If
	Set XMLDom = NewAsp.RecordsetToxml(Rs,"topic","xml")
	Set Rs = Nothing
	Set Node = XMLDom.documentElement.selectSingleNode("topic")
	topicid = CLng(Node.selectSingleNode("@topicid").text)
	classid = CLng(Node.selectSingleNode("@classid").text)
	topicmode = CLng(Node.selectSingleNode("@topicmode").text)
	PostNum = CLng(Node.selectSingleNode("@postnum").text)
	ExpiredTime = CDate(Node.selectSingleNode("@expiredtime").text)
	CommentNum = CLng(Node.selectSingleNode("@commentnum").text)
	HeadTitle = Trim(Node.selectSingleNode("@title").text)
	TopicUseTable = Trim(Node.selectSingleNode("@posttable").text)
	RemainDays = DateDIff("d",Now(),ExpiredTime)
	RemainHour = DateDIff("h",Now(),ExpiredTime)
	RemainHour = RemainHour mod 24
	If RemainHour>0 Then RemainDays = RemainDays-1
	icons = topicmode
	NewAsp.classid=classid
	If CLng(Node.selectSingleNode("@closed").text) = 1 Or CLng(Node.selectSingleNode("@postnum").text) > 10 Or CLng(Node.selectSingleNode("@commentnum").text) > 100 Then
		CloseTopic = 1
		icons = 5
	Else
		CloseTopic = 0
	End If
	If topicmode = 2 Then CloseTopic = 1
	If CLng(Node.selectSingleNode("@userid").text) <> NewAsp.UserID Then
		Newasp.Execute ("UPDATE NC_Ask_Topic SET Hits=Hits+1 WHERE topicid="&topicid)
		isnowuser = 0
	Else
		isnowuser = 1
	End If
	XMLDom.documentElement.setAttribute "postuserid",Node.selectSingleNode("@userid").text
	Set Node = Nothing
	HtmlContent = Replace(HtmlContent, "{$TopicID}", topicid)
	HtmlContent = Replace(HtmlContent, "{$ClassID}", classid)

End Sub

Sub LoadQuestionList()
	If NewAsp.FoundErr Then Exit Sub
	XMLDom.documentElement.setAttribute "classname",classname
	XMLDom.documentElement.setAttribute "userid",NewAsp.UserID
	XMLDom.documentElement.setAttribute "topicmode",topicmode
	XMLDom.documentElement.setAttribute "closetopic",CloseTopic
	XMLDom.documentElement.setAttribute "remaindays",RemainDays
	XMLDom.documentElement.setAttribute "remainhour",RemainHour
	XMLDom.documentElement.setAttribute "isnowuser",isnowuser
	XMLDom.documentElement.setAttribute "icons",icons
	XMLDom.documentElement.setAttribute "allowanswers",allowAnswers
	XMLDom.documentElement.setAttribute "allowcomment",allowComment
	XMLDom.documentElement.setAttribute "allowdeletes",allowDeletes
	XMLDom.documentElement.setAttribute "answerpoint",NewAsp.ChkNumeric(NewAsp.Point_Setting(8))
	XMLDom.documentElement.setAttribute "acceptpoint",NewAsp.ChkNumeric(NewAsp.Point_Setting(10))
	Dim SQL,Rs,Postlist,Node,i
	Dim satisanswer,otheranswer
	i=0
	SQL="SELECT postsid,classid,TopicID,userid,UserName,topic,content,addText,PostTime,DoneTime,star,satis,LockTopic,PostsMode,VoteNum,Plus,Minus,PostIP,Report FROM ["&TopicUseTable&"] WHERE topicid="&topicid&" and LockTopic=0 ORDER BY postsid ASC"
	Set Rs = NewAsp.Execute(SQL)
	If Not Rs.EOF Then
		SQL=Rs.GetRows(-1)
		Set Postlist=NewAsp.ArrayToxml(SQL,Rs,"row","postlist")
	Else
		Set Postlist=Nothing
	End If
	Set Rs=Nothing
	SQL=Empty
	If Not Postlist Is Nothing Then
		satisanswer = Postlist.documentElement.SelectNodes("row[@postsmode=1 and @satis=1]").Length
		otheranswer = Postlist.documentElement.SelectNodes("row[@postsmode=1 and @satis=0]").Length
		XMLDom.documentElement.setAttribute "satisanswer",satisanswer
		XMLDom.documentElement.setAttribute "otheranswer",otheranswer
		For Each Node in Postlist.documentElement.SelectNodes("row")
			Node.selectSingleNode("@topic").text=NewAsp.Checkstr(Node.selectSingleNode("@topic").text)
			Node.selectSingleNode("@posttime").text=NewAsp.FormatDate(Node.selectSingleNode("@posttime").text,1)
			Node.selectSingleNode("@content").text=UBBCode(Node.selectSingleNode("@content").text)
			If Not IsNull(Node.selectSingleNode("@addtext").text) And Node.selectSingleNode("@addtext").text <> "" Then
				Node.selectSingleNode("@addtext").text=UBBCode(Node.selectSingleNode("@addtext").text)
			Else
				Node.selectSingleNode("@addtext").text=""
			End If
			If i = 0 Then
				PostUseridlist=Node.selectSingleNode("@userid").text
			Else
				PostUseridlist=PostUseridlist&","&Node.selectSingleNode("@userid").text
			End If
			Node.attributes.setNamedItem(Postlist.createNode(2,"i","")).text = i
			i = i + 1
		Next
		XMLDom.documentElement.appendChild(Postlist.documentElement)
		Set Postlist=Nothing
	Else
		satisanswer = 0
		otheranswer = 0
		XMLDom.documentElement.setAttribute "satisanswer",satisanswer
		XMLDom.documentElement.setAttribute "otheranswer",otheranswer
	End If
	'--?????????????,??????????
	Dim NowPostNum
	NowPostNum = satisanswer + otheranswer
	XMLDom.documentElement.setAttribute "nowpostnum",NowPostNum
	If PostNum <> NowPostNum Then
		NewAsp.Execute ("UPDATE NC_Ask_Topic SET PostNum=" & NowPostNum & " WHERE topicid="&topicid)
	End If
	LoadCommentList()
	Dim Postuserlist
	PostUseridlist = NewAsp.CheckIDlist(PostUseridlist)
	Set Rs=NewAsp.Execute("SELECT userid,Username,Nickname,UserClass,UserTitle,Useremail,qq,msn,Usersex,UserFace,Photo,Homepage,addtime,lastime,Points,Experience,Asktotal,Askpend,Askdone,Askvote,Askshare,Askstop,Askoverdue,Answertotal,Adopted FROM NC_Ask_Users WHERE userid IN ("& PostUseridlist &")")
	If Not Rs.EOF Or PostUseridlist<>"" Then
		Set Postuserlist=NewAsp.RecordsetToxml(Rs,"user","userlist")
		XMLDom.documentElement.appendChild(Postuserlist.documentElement)
		'Response.Write XMLDom.xml
	End If
	transform_QuestionList()
End Sub

Sub transform_QuestionList()
	Dim proc,XMLStyle,node,cnode,XSLTemplate
	Set XSLTemplate=NewAsp.CreateAXObject("Msxml2.XSLTemplate" & MsxmlVersion )
	Set XMLStyle=NewAsp.CreateXmlDoc("msxml2.FreeThreadedDOMDocument"& MsxmlVersion )
	XMLStyle.load Server.MapPath(NewAsp.TemplatePath & "xslt/question.xslt")
	Set Node=XMLStyle.createNode(1,"xsl:variable","http://www.w3.org/1999/XSL/Transform")
	Set CNode=XMLStyle.createNode(2,"name","")
	CNode.text="installdir"
	Node.attributes.setNamedItem(CNode)
	Node.text=NewAsp.InstallDir
	XMLStyle.documentElement.appendChild(node)
	Set Node=XMLStyle.createNode(1,"xsl:variable","http://www.w3.org/1999/XSL/Transform")
	Set CNode=XMLStyle.createNode(2,"name","")
	CNode.text="skinurl"
	Node.attributes.setNamedItem(CNode)
	node.text=NewAsp.AskedSkinUrl
	XMLStyle.documentElement.appendChild(node)
	XSLTemplate.stylesheet=XMLStyle
	Set proc = XSLTemplate.createProcessor()
	proc.input = XMLDom
	proc.transform()
	Dim procstr
	procstr = proc.output
	HtmlContent = Replace(HtmlContent, "{$QuestionList}", procstr)
	'Response.Write XMLDom.xml
	Set XMLDom=Nothing
	Set proc=Nothing
	Set XMLStyle=Nothing
	Set XSLTemplate=Nothing
End Sub

Sub LoadCommentList()
	If NewAsp.FoundErr Then Exit Sub
	Dim SQL,Rs,essaylist,Node,NowCommentNum,i
	i=0
	SQL="SELECT CommentID,TopicID,userid,UserName,content,PostTime,PostIP,islock FROM [NC_Ask_Comment] WHERE topicid="&topicid&" and islock=0 ORDER BY CommentID DESC"
	Set Rs = NewAsp.Execute(SQL)
	If Not Rs.EOF Then
		SQL=Rs.GetRows(-1)
		Set essaylist=NewAsp.ArrayToxml(SQL,Rs,"row","commentlist")
	Else
		Set essaylist=Nothing
	End If
	Set Rs=Nothing
	SQL=Empty
	If Not essaylist Is Nothing Then
		NowCommentNum = CLng(essaylist.documentElement.SelectNodes("row").Length)
		XMLDom.documentElement.setAttribute "nowcommentnum",NowCommentNum
		For Each Node in essaylist.documentElement.SelectNodes("row")
			'Node.selectSingleNode("@topic").text=NewAsp.Checkstr(Node.selectSingleNode("@topic").text)
			Node.selectSingleNode("@posttime").text=NewAsp.FormatDate(Node.selectSingleNode("@posttime").text,1)
			Node.selectSingleNode("@content").text=UBBCode(Node.selectSingleNode("@content").text)
			PostUseridlist=PostUseridlist&","&Node.selectSingleNode("@userid").text
			i=i+1
			Node.attributes.setNamedItem(essaylist.createNode(2,"i","")).text = i
		Next
		XMLDom.documentElement.appendChild(essaylist.documentElement)
		Set essaylist=Nothing
	Else
		NowCommentNum = 0
		XMLDom.documentElement.setAttribute "nowcommentnum",NowCommentNum
	End If
	'--??????????????,??????????
	If CommentNum <> NowCommentNum Then
		NewAsp.Execute ("UPDATE NC_Ask_Topic SET CommentNum=" & NowCommentNum & " WHERE topicid="&topicid)
	End If
End Sub

Sub CategoryInfo()
	If NewAsp.FoundErr Then Exit Sub
	Dim iAskCount,cid,cNameText
	Dim Catelist,Childlist,Node,parentid,Parentstr
	cid = classid
	child = 0
	Set Catelist = Application(NewAsp.CacheName&"_classlist")
	If Not Catelist Is Nothing Then
		For Each Node in Catelist.documentElement.SelectNodes("row[@classid="&classid&"]")
			classname=Node.selectSingleNode("@classname").text
			child=CLng(Node.selectSingleNode("@child").text)
			parentid=CLng(Node.selectSingleNode("@parentid").text)
			Parentstr=Node.selectSingleNode("@parentstr").text
			If child>0 Then
				cid=classid
			Else
				cid=CLng(Node.selectSingleNode("@parentid").text)
			End If
		Next
		cNameText = ""
		If parentid>0 Then
			Dim classType,cateid
			Dim ArrayParent,i,n,DocNodes
			ArrayParent = Split(Parentstr, ",")
			n=0
			For i=0 To UBound(ArrayParent)
				If NewAsp.ChkNumeric(ArrayParent(i))>0 Then
					If n=0 Then
						DocNodes="row[@classid="&ArrayParent(i)&""
					Else
						DocNodes=DocNodes&" or @classid="&ArrayParent(i)&""
					End If
					n=n+1
				End If
			Next
			If DocNodes<>"" Then
				DocNodes=DocNodes&"]"
				For Each Node in Catelist.documentElement.SelectNodes(DocNodes)
					classType=Node.selectSingleNode("@classname").text
					cateid=Node.selectSingleNode("@classid").text
					cNameText=cNameText&"<a href="""&NewAsp.InstallDir&"showlist.asp?classid="&cateid&""">"&classType&"</a> &gt; "
					HeadCategory=HeadCategory&classType&"-"
				Next
			End If
		End If
		cNameText=cNameText&"<a href="""&NewAsp.InstallDir&"showlist.asp?classid="&classid&""">"&classname&"</a>"
		HeadCategory=HeadCategory&classname
		Set Catelist = Nothing
	End If
	HtmlContent = Replace(HtmlContent, "{$Current}", cNameText)
	HtmlContent = Replace(HtmlContent, "{$ClassName}", classname)
End Sub

%>