<!-- #include file=../inc/BBSsetup.asp -->
<!-- #include file=../inc/Board_Popfun.asp -->
<!-- #include file=../inc/Limit_fun.asp -->
<!-- #include file=inc/MakeAnnounceTop.asp -->
<%
DEF_BBS_HomeUrl = "../"
initDatabase
GBL_CHK_TempStr = ""
siteHead("   主题固顶或取消固顶")
%>
<script language=javascript>
	window.moveTo(window.screen.width/2-225,window.screen.height/2-18);
</script>
<%
UpdateOnlineUserAtInfo GBL_board_ID,"固帖子"
Dim Form_RootID,RootStr,Form_RootIDBak,AllTopFlag,TopID

If GBL_CHK_Flag=1 Then
	If CheckIsCanDellSure = 1 Then
		DisplayMakeGoodAnnounce
	End If
Else
	If Request("submitflag")="" Then
		Response.Write "<br><b>请先登录</b>"
	Else
		Response.Write "<br><p align=left><font color=ff0000 class=RedFont><b>" & GBL_CHK_TempStr & "</b></font>"
	End If
	Response.Write "</p>"
End If

closeDataBase
SiteBottom_Spend

Function CheckIsCanDellSure

	If GBL_CHK_LastWriteTime < DEF_WriteEventSpace Then
		Response.Write "<b><font color=Red Class=RedFont>您的操作太频，请稍候再作提交!</font></b> <br>" & VbCrLf
		CheckIsCanDellSure = 0
		Exit Function
	End If
	TopID = Left(Request("TopID"),14)
	If isNumeric(TopID) = 0 or inStr(TopID,",") > 0 or TopID = "" Then
		Response.Write "错误,要固顶或取消固顶的帖子不存在!<br>" & VbCrLf
		CheckIsCanDellSure = 0
		Exit Function
	End if

	TopID = cCur(TopID)
	Dim Rs,SQL,ParentID
	SQL = "Select top 1 BoardID,ParentID,UserID,RootID,RootIDBak from LeadBBS_Announce where id=" & TopID
	Set Rs = Con.ExeCute(SQL)
	GBL_DBNum = GBL_DBNum + 1
	If Rs.Eof Then
		Response.Write "错误,要固顶或取消固顶的帖子不存在!<br>" & VbCrLf
		Rs.Close
		Set Rs = Nothing
		CheckIsCanDellSure = 0
		Exit Function
	End if

	GBL_Board_ID = cCur(Rs(0))
	ParentID = cCur(Rs(1))
	Form_RootID = cCur(Rs(3))
	Form_RootIDBak = cCur(Rs(4))
	Rs.Close
	Set Rs = Nothing
	
	If cCur(ParentID) <> 0 Then
		Response.Write "错误,要固顶或取消固顶的帖子必须是主题编号!<br>" & VbCrLf
		CheckIsCanDellSure = 0
		Exit Function
	End if

	Dim Temp
	Temp = Application(DEF_MasterCookies & "BoardInfo" & GBL_Board_ID)
	If isArray(Temp) = False Then
		ReloadBoardInfo(GBL_Board_ID)
		Temp = Application(DEF_MasterCookies & "BoardInfo" & GBL_Board_ID)
	End If
	If isArray(Temp) = False Then
		Response.Write "社区发生错误，请联系管理员!<br>" & VbCrLf
		CheckIsCanDellSure = 0
		Set Rs = Nothing
	End If
	GBL_Board_MasterList = Temp(10,0)
	GBL_Board_BoardAssort = cCur(Temp(1,0))

	CheckisBoardMaster
	If GBL_BoardMasterFlag < 5 or GetBinarybit(GBL_CHK_UserLimit,4) = 1 Then
		CheckIsCanDellSure = 0
		Response.Write "错误,权限不足."
		Exit Function
	End If

	CheckAllTopAnnounceFlag
	
	AllTopFlag = 0
	If inStr(application(DEF_MasterCookies & "TopAncList" & GBL_Board_BoardAssort),"," & TopID & ",") Then AllTopFlag = 1
	If inStr(application(DEF_MasterCookies & "TopAncList"),"," & TopID & ",") Then AllTopFlag = 2
	If GBL_BoardMasterFlag < 7 and AllTopFlag=2 Then
		CheckIsCanDellSure = 0
		Response.Write "错误,权限不足,总固顶帖无权限取消."
		Exit Function
	End If
	If GBL_BoardMasterFlag < 6 and AllTopFlag=1 Then
		CheckIsCanDellSure = 0
		Response.Write "错误,权限不足,总固顶帖无权限取消."
		Exit Function
	End If
	
	If Form_RootID < DEF_BBS_TOPMinID Then
		RootStr = "固顶"
		If CheckMakeTopAnnounceOver = 1 then
			Response.Write "错误,置顶的帖子太多,不能再进行置顶操作.<br>"
			CheckIsCanDellSure = 0
			Exit Function
		Else
			CheckIsCanDellSure = 1
		End If
	Else
		RootStr = "取消固顶"
		CheckIsCanDellSure = 1
	End If

End Function

Function CheckMakeTopAnnounceOver

	Dim Rs,SQL
	SQL = "Select count(*) from LeadBBS_Topic Where BoardID=" & GBL_board_ID & " and RootID>=" & DEF_BBS_TOPMinID
	Set Rs = Con.ExeCute(SQL)
	GBL_DBNum = GBL_DBNum + 1
	If Not Rs.Eof Then
		SQL = Rs(0)
		If isNull(SQL) Then SQL = 0
		SQL = cCur(SQL)
	Else
		SQL = 0
	End If
	Rs.Close
	Set Rs = Nothing

	If SQL > DEF_BBS_MaxTopAnnounce Then
		CheckMakeTopAnnounceOver = 1
	Else
		CheckMakeTopAnnounceOver = 0
	End If

End Function

Function CheckAllTopAnnounceFlag

	Dim Rs,SQL,Num

	SQL = "Select top 1 ID from LeadBBS_TopAnnounce where RootID=" & Form_RootIDBak
	Set Rs = Con.ExeCute(SQL)
	GBL_DBNum = GBL_DBNum + 1
	If Rs.Eof Then
		CheckAllTopAnnounceFlag = 0
	Else
		CheckAllTopAnnounceFlag = 1
	End If
	Rs.Close
	Set Rs = Nothing

End Function

Function DisplayMakeGoodAnnounce

	TopID = Left(Request("TopID"),14)
	If isNumeric(TopID) = 0 or inStr(TopID,",") > 0 or TopID = "" Then
		Response.Write "错误,请提供要" & RootStr & "的帖子的ID!<br>" & VbCrLf
		Exit Function
	End if
	If Request.Form("DeleteSureFlag")="dk9@dl9s92lw_SWxl" Then
		TopID = cCur(TopID)
		MakeTopAnc(TopID)
		Response.Write "成功" & RootStr & "社区帖子，请按ESC键退出。"
	Else
		%>
		<form name=DellClientForm action=MakeTopAnc.asp method=post>
			<input type=hidden name=DeleteSureFlag value="dk9@dl9s92lw_SWxl">
			<input type=hidden name=TopID value="<%=TopID%>">
			<input type=hidden name=BoardID value="<%=GBL_Board_ID%>">
			<b>确认要<%=RootStr%>编号为<font color=ff0000 class=RedFont><%=TopID%></font>的帖子吗？</b>
			<p><input type=submit value=确定 class=fmbtn>
			<input type=button value=取消此操作 onclick="javascript:window.close();" class=fmbtn>
		</form>
		<%
	End If

End Function

Function MakeTopAnc(AnnounceID)

	Dim Rs,SQL,BoardID,RootID,MaxRootID,RootIDBak
	
	SQL = "Select top 1 ID,RootID,BoardID,RootIDBak from LeadBBS_Announce where ID=" & AnnounceID
	Set Rs = Con.ExeCute(SQL)
	GBL_DBNum = GBL_DBNum + 1
	If Not Rs.Eof Then
		RootID = cCur(Rs(1))
		BoardID = cCur(Rs(2))
		RootIDBak = cCur(Rs(3))
		Rs.Close
		Set Rs = Nothing
	Else
		Rs.Close
		Set Rs = Nothing
		Exit function
	End If
	
	If RootID<DEF_BBS_TOPMinID Then
		Set Rs = Con.ExeCute("Select Max(RootID) from LeadBBS_Topic where BoardID=" & BoardID)
		GBL_DBNum = GBL_DBNum + 1
		If Rs.Eof Then
			MaxRootID = DEF_BBS_TOPMinID
		Else
			MaxRootID = Rs(0)
			If isNull(MaxRootID) or MaxRootID="" Then
				MaxRootID=DEF_BBS_TOPMinID
			End If
			MaxRootID = cCur(MaxRootID)
		End If
		Rs.Close
		Set Rs = Nothing
		If MaxRootID < DEF_BBS_TOPMinID Then MaxRootID = DEF_BBS_TOPMinID
		Con.ExeCute(" Update LeadBBS_Announce Set RootID=" & MaxRootID+1 & " where ID=" & RootIDBak)
		Con.ExeCute(" Update LeadBBS_Topic Set RootID=" & MaxRootID+1 & " where ID=" & RootIDBak)
		GBL_DBNum = GBL_DBNum + 1
	Else
		Set Rs = Con.ExeCute("Select Max(RootID) from LeadBBS_Topic where BoardID=" & BoardID & " and RootID<" & DEF_BBS_TOPMinID)
		GBL_DBNum = GBL_DBNum + 1
		If Rs.Eof Then
			MaxRootID = 0
		Else
			MaxRootID = Rs(0)
			If isNull(MaxRootID) or MaxRootID="" Then
				MaxRootID=0
			End If
			MaxRootID = cCur(MaxRootID)
		End If
		Rs.Close
		Set Rs = Nothing
		Con.ExeCute(" Update LeadBBS_Announce Set RootID=" & MaxRootID+1 & " where ID=" & RootIDBak)
		Con.ExeCute(" Update LeadBBS_Topic Set RootID=" & MaxRootID+1 & " where ID=" & RootIDBak)
		If AllTopFlag = 1 or AllTopFlag = 2 Then
			Con.ExeCute(" delete from LeadBBS_TopAnnounce where RootID=" & Form_RootIDBak)
			GBL_DBNum = GBL_DBNum + 1
		End If
		GBL_DBNum = GBL_DBNum + 1
	End If
	Set Rs = Nothing

	If CheckSupervisorUserName = 0 Then
		Con.ExeCute("Update LeadBBS_User Set LastWriteTime=" & GetTimeValue(DEF_Now) & " where ID=" & GBL_UserID)
		GBL_DBNum = GBL_DBNum + 1
		UpdateSessionValue 13,GetTimeValue(DEF_Now),0
	End If
	UpdateBoardValue(BoardID)
	If inStr(application(DEF_MasterCookies & "TopAncList"),"," & AnnounceID & ",") Then ReloadTopAnnounceInfo(0)
	If inStr(application(DEF_MasterCookies & "TopAncList" & GBL_Board_BoardAssort),"," & AnnounceID & ",") Then
		ReloadTopAnnounceInfo(GBL_Board_BoardAssort)
	End If

End Function

Sub ReloadTopAnnounceInfo(TID)

	If isNumeric(TID) = 0 Then Exit Sub
	TID = Fix(cCur(TID))
	Dim Rs,GetDataTop,TIDStr
	If TID = 0 Then
		TIDStr = ""
	Else
		TIDStr = TID
	End If
	Set Rs = Con.ExeCute("Select RootID,BoardID from LeadBBS_TopAnnounce where TopType=" & TID)
	GBL_DBNum = GBL_DBNum + 1
	If Rs.Eof Then
		Application.Lock
		application(DEF_MasterCookies & "TopAnc" & TIDStr) = "yes"
		application(DEF_MasterCookies & "TopAncList" & TIDStr) = ""
		Application.UnLock
		Rs.Close
		Set Rs = Nothing
		Exit Sub
	Else
		GetDataTop = Rs.GetRows(-1)
		Rs.close
		Set Rs = Nothing
	End If
	
	Dim Temp,N
	Temp = ""
	If cCur(GetDataTop(0,0)) > 0 Then Temp = GetDataTop(0,0)
	For N = 1 to Ubound(GetDataTop,2)
		If cCur(GetDataTop(0,N)) > 0 Then Temp = Temp & "," & GetDataTop(0,N)
	Next
	If Left(Temp,1) = "," Then Temp = Mid(Temp,2)
	If cStr(Temp) <> "" Then
		Set Rs = Con.ExeCute("select top " & Ubound(GetDataTop,2)+1 & " id,ChildNum,Title,FaceIcon,LastTime,Hits,Length,UserName,UserID,RootID,LastUser,NotReplay,GoodFlag,BoardID,TopicType,PollNum,TitleStyle,LastInfo,ndatetime,GoodAssort from LeadBBS_Topic where ID in(" & Temp & ")")
		GBL_DBNum = GBL_DBNum + 1
		If Not Rs.Eof Then
			GetDataTop = Rs.GetRows(-1)
			Rs.Close
			Set Rs = Nothing
			Application.Lock
			application(DEF_MasterCookies & "TopAnc" & TIDStr) = GetDataTop
			Application.UnLock
			Application.Lock
			application(DEF_MasterCookies & "TopAncList" & TIDStr) = "," & Temp & ","
			Application.UnLock
		Else
			Rs.Close
			Set Rs = Nothing
			Application.Lock
			application(DEF_MasterCookies & "TopAnc" & TIDStr) = "yes"
			Application.UnLock
		End If
	Else
		Application.Lock
		application(DEF_MasterCookies & "TopAnc" & TIDStr) = "yes"
		Application.UnLock
	End If

End Sub
%>