<!--#include file="setup.asp"-->
<%
Dim Rs_c, srtSortingList, i
Dim NewFolderPath, NewFolderPath1, objFSO
Server.ScriptTimeout = 99999
NC_Admin.AdminChk = "22"
NC_Admin.Check
DownsysClass.admin_header

Response.Write "<table width=""96%"" border=""0"" cellspacing=""1"" cellpadding=""3"" align=center class=""tableBorder"">"
Response.Write "<tr>"
Response.Write "<th width=""100%"" class=""tableHeaderText"" colspan=2 height=25>软件分类管理"
Response.Write "</th>"
Response.Write "</tr>"
Response.Write "<tr>"
Response.Write "<td class=""forumrow"" colspan=2>"
Response.Write "<p><B>注意</B>：<BR>①删除分类同时将删除该分类下所有的软件信息及下载地址！删除分类同时删除下属分类和所有软件！ 操作时请完整填写表单信息。<BR>"
Response.Write " ②如果选择<B>复位所有分类</B>，则所有分类都将作为一级分类（分类），这时您需要重新对各个分类进行归属的基本设置，<B>不要轻易使用该功能</B>，仅在做出了错误的设置而无法复原分类之间的关系和排序的时候使用 </p>"
Response.Write "</td>"
Response.Write "</tr>"
Response.Write "<tr>"
Response.Write "<td class=""forumrow"">"
Response.Write "<B>操作选项</B></td>"
Response.Write "<td class=""forumrow""><a href=""admin_sorting.asp"">分类管理首页</a>|<a href=""admin_sorting.asp?action=add"">新建下载分类</a>"
Response.Write "|<a href=""?action=orders"">一级分类排序</a>|<a href=""?action=boardorders"">N级分类排序</a>|<a href=""?action=move"">批量移动</a>"
Response.Write "|<a href=""?action=RestoreBoard"" onclick=""{if(confirm('复位所有分类将把所有分类恢复成为一级大分类，复位后要对所有分类重新进行归属的基本设置，请慎重操作，确定复位吗?')){return true;}return false;}"">复位所有分类</a>"
Response.Write "</td>"
Response.Write "</tr>"
Response.Write "</table>"
Response.Write "<p></p>"
Select Case Request("action")
	Case "add"
		Call Add
	Case "edit"
		Call edit
	Case "savenew"
		Call savenew
	Case "savedit"
		Call savedit
	Case "del"
		Call del
	Case "orders"
		Call orders
	Case "updatorders"
		Call updateorders
	Case "boardorders"
		Call boardorders
	Case "updatboardorders"
		Call updateboardorders
	Case "RestoreBoard"
		Call RestoreBoard
	Case Else
		Call SortInfo
End Select
If Founderr = True Then
	NC_Admin.Error_Msg (ErrMsg)
End If
DownsysClass.admin_footer
NothingObject

Private Sub SortInfo()
	Response.Write " <table width=""96%"" class=""tableBorder"" cellspacing=""1"" cellpadding=""2"" align=center>"
	Response.Write " <tr>"
	Response.Write " <th width=""35%""><strong>软件分类</strong> </th>"
	Response.Write " <th width=""35%""><strong>操作</strong> </th>"
	Response.Write "</tr>"
	SQL = "select * from NC_SoftSort order by rootid,orders"
	Set Rs = Server.CreateObject("adodb.recordset")
	Rs.Open SQL, Conn, 1, 1
	DownsysClass.SqlQueryNum = DownsysClass.SqlQueryNum + 1
	i = 1
	Do While Not Rs.EOF
		Response.Write " <tr class=""forumrow"">"
		Response.Write " <td width=35% class=""forumrow"">"
		Response.Write " "
		If Rs("depth") = 1 Then Response.Write "&nbsp;&nbsp;<font color=""#666666"">├</font>"
		If Rs("depth") > 1 Then
			For i = 2 To Rs("depth")
				Response.Write "&nbsp;&nbsp;<font color=""#666666"">│</font>"
			Next
			Response.Write "&nbsp;&nbsp;<font color=""#666666"">├</font> "
		End If
		If Rs("parentid") = 0 Then Response.Write ("<b>")
		Response.Write Rs("sortname")
		If Rs("child") > 0 Then Response.Write "(" & Rs("child") & ")"
		Response.Write " </td>"
		Response.Write " <td width=65% class=""forumrow"" align=right><a href=""admin_sorting.asp?action=add&editid="
		Response.Write Rs("sortid")
		Response.Write """>添加分类</a>"
		Response.Write " | <a href=""admin_sorting.asp?action=edit&editid="
		Response.Write Rs("sortid")
		Response.Write """>分类设置</a>"
		Response.Write " |"
		Response.Write " "
		If Rs("child") = 0 Then
			Response.Write " <a href=""admin_sorting.asp?action=del&editid="
			Response.Write Rs("sortid")
			Response.Write """ onclick=""{if(confirm('删除将包括该分类的所有软件信息，确定删除吗?')){return true;}return false;}"">删除"
			Response.Write " "
		Else
			Response.Write "<a href=""#"" onclick=""{if(confirm('该分类含有下属分类，必须先删除其下属分类方能删除本分类！')){return true;}return false;}"">"
			Response.Write " 删除</a>"
			Response.Write " "
		End If
		Response.Write " </td>"
		Response.Write "</tr>"
		Rs.movenext
		i = i + 1
	Loop
	Rs.Close
	Set Rs = Nothing
	Response.Write "</table>"
End Sub

Private Sub Add()
	Dim SortNum
	Set Rs = CreateObject("Adodb.recordset")
	SQL = "select Max(sortid) from NC_SoftSort"
	Rs.Open SQL, Conn, 1, 1
	If Rs.bof And Rs.EOF Then
		SortNum = 1
	Else
		SortNum = Rs(0) + 1
	End If
	If IsNull(SortNum) Then SortNum = 1
	Rs.Close
	Response.Write "<form action =""admin_sorting.asp?action=savenew"" method=post>"
	Response.Write "<input type=""hidden"" name=""newsortid"" value="""
	Response.Write SortNum
	Response.Write """>"
	Response.Write " <table width=""96%"" border=""0"" cellspacing=""1"" cellpadding=""3"" align=center class=""tableBorder"">"
	Response.Write " <tr>"
	Response.Write " <th colspan=2>添加新分类</th>"
	Response.Write "</tr>"
	Response.Write " <tr class=""forumrow"">"
	Response.Write " <td width=""30%"" class=""forumrow"" height=30><U>分类名称</U></td>"
	Response.Write " <td width=""70%"" class=""forumrow"">"
	Response.Write " <input type=""text"" name=""sortname"" size=""35"">"
	Response.Write "</td>"
	Response.Write "</tr>"
	Response.Write " <tr>"
	Response.Write " <td height=24 class=""forumrow""><U>分类说明</U></td>"
	Response.Write " <td class=""forumrow"">"
	Response.Write " <textarea name=""Readme"" cols=""50"" rows=""3""></textarea>"
	Response.Write "</td>"
	Response.Write "</tr>"
	Response.Write "<tr>"
	Response.Write "<td height=30 class=""forumrow""><U>使用设置模板样式</U></td>"
	Response.Write "<td class=""forumrow"">"
	Response.Write "<select name=skinid>"
	Response.Write "<option value=""0"">≡使用默认模板≡</option>"
	Set Rs_c = CreateObject("adodb.recordset")
	SQL = "select id,TempName from [NC_Template] order by id desc"
	Rs_c.Open SQL, Conn, 1, 1
	If Rs_c.bof And Rs_c.EOF Then
		Response.Write "<option value=>请先添加模板</option>"
	Else
		Do While Not Rs_c.EOF
			Response.Write "<option value="""
			Response.Write Rs_c("id")
			Response.Write """>"
			Response.Write Rs_c("tempname")
			Rs_c.movenext
		Loop
	End If
	Rs_c.Close
	Response.Write "</select>"
	Response.Write "</td>"
	Response.Write "</tr>"
	Response.Write " <tr>"
	Response.Write " <td height=30 class=""forumrow""><U>所属类别</U></td>"
	Response.Write " <td class=""forumrow"">"
	Response.Write " <select name=""class"">"
	Response.Write "<option value=""0"">做为软件分类</option>"
	SQL = "select * from NC_SoftSort order by rootid,orders"
	Rs.Open SQL, Conn, 1, 1
	Do While Not Rs.EOF
		Response.Write "<option value=""" & Rs("sortid") & """ "
		If Request("editid") <> "" And CLng(Request("editid")) = Rs("sortid") Then Response.Write "selected"
		Response.Write ">"
		If Rs("depth") = 1 Then Response.Write "&nbsp;&nbsp;├ "
		If Rs("depth") > 1 Then
			For i = 2 To Rs("depth")
				Response.Write "&nbsp;&nbsp;│"
			Next
			Response.Write "&nbsp;&nbsp;├ "
		End If
		Response.Write Rs("sortname") & "</option>" & vbCrLf
		Rs.movenext
	Loop
	Rs.Close
	Response.Write "</select>"
	Response.Write "</td>"
	Response.Write " <tr>"
	Response.Write " <td height=24 class=""forumrow"">&nbsp;</td>"
	Response.Write " <td class=""forumrow"">"
	Response.Write " <input type=""submit"" name=""Submit"" class=button value=""添加分类"">"
	Response.Write "</td>"
	Response.Write "</tr>"
	Response.Write "</table>"
	Response.Write "</form>"
	Set Rs = Nothing
End Sub


Private Sub edit()
	Dim Rs_e
	Set Rs = CreateObject("Adodb.recordset")
	SQL = "select * from NC_SoftSort where sortid=" & Request("editid")
	Set Rs_e = DownsysClass.Execute(SQL)
	Response.Write "<form action =""admin_sorting.asp?action=savedit"" method=post>"
	Response.Write "<input type=""hidden"" name=editid value="""
	Response.Write Request("editid")
	Response.Write """>"
	Response.Write " <table width=""96%"" border=""0"" cellspacing=""1"" cellpadding=""3"" align=center class=""tableBorder"">"
	Response.Write " <tr>"
	Response.Write " <th height=24 colspan=2>编辑分类："
	Response.Write Rs_e("sortname")
	Response.Write "</th>"
	Response.Write " </tr>"
	Response.Write " <tr class=""forumrow"">"
	Response.Write " <td width=""30%"" height=30 class=""forumrow""><U>分类名称</U></td>"
	Response.Write " <td width=""70%"" class=""forumrow"">"
	Response.Write " <input type=""text"" name=""sortname"" size=""35"" value="""
	Response.Write Rs_e("sortname")
	Response.Write """>"
	Response.Write " </td>"
	Response.Write " </tr>"
	Response.Write " <tr>"
	Response.Write " <td class=""forumrow""height=24><U>分类说明</U><BR>"
	Response.Write " 可以使用HTML代码</td>"
	Response.Write " <td class=""forumrow"">"
	Response.Write " <textarea name=""Readme"" cols=""50"" rows=""3"">"
	Response.Write Rs_e("readme")
	Response.Write "</textarea>"
	Response.Write " </td>"
	Response.Write " </tr>"
	Response.Write "<tr>"
	Response.Write "<td height=30 class=""forumrow""><U>使用设置模板样式</U></td>"
	Response.Write "<td class=""forumrow"">"
	Response.Write "<select name=skinid>"
	Response.Write "<option value=""0"">≡使用默认模板≡</option>"
	Set Rs_c = CreateObject("Adodb.recordset")
	SQL = "select id,TempName from [NC_Template] order by id desc"
	Rs_c.Open SQL, Conn, 1, 1
	If Rs_c.EOF And Rs_c.bof Then
		Response.Write "<option value=>请先添加模板</option>"
	Else
		Do While Not Rs_c.EOF
			If Rs_e("skinid") = Rs_c("id") Then
				Response.Write "<option value="""
				Response.Write Rs_c("id")
				Response.Write """ selected>"
				Response.Write Rs_c("TempName")
			Else
				Response.Write "<option value="""
				Response.Write Rs_c("id")
				Response.Write """>"
				Response.Write Rs_c("TempName")
			End If
			Rs_c.movenext
		Loop
	End If
	Rs_c.Close
	Response.Write "</select>"
	Response.Write "</td>"
	Response.Write "</tr>"
	Response.Write " <tr>"
	Response.Write " <td height=30 class=""forumrow""><U>所属类别</U></td>"
	Response.Write " <td class=""forumrow"">"
	Response.Write " <select name=""class"">"
	Response.Write " <option value=""0"">做为软件分类</option>"
	Response.Write " "
	SQL = "select * from NC_SoftSort order by rootid,orders"
	Rs.Open SQL, Conn, 1, 1
	Do While Not Rs.EOF
		Response.Write "<option value=""" & Rs("sortid") & """ "
		If Rs_e("parentid") = Rs("sortid") Then Response.Write "selected"
		Response.Write ">"
		If Rs("depth") = 1 Then Response.Write "&nbsp;&nbsp;├ "
		If Rs("depth") > 1 Then
			For i = 2 To Rs("depth")
				Response.Write "&nbsp;&nbsp;│"
			Next
			Response.Write "&nbsp;&nbsp;├ "
		End If
		Response.Write Rs("sortname") & "</option>" & vbCrLf
		Rs.movenext
	Loop
	Rs.Close
	Response.Write " </select> </td>"
	Response.Write " </tr>"
	Response.Write " <tr>"
	Response.Write " <td class=""forumrow""height=24>&nbsp;</td>"
	Response.Write " <td class=""forumrow"">"
	Response.Write " <input type=""submit"" name=""Submit"" class=button value=""保存修改"">"
	Response.Write " </td>"
	Response.Write " </tr>"
	Response.Write " </table>"
	Response.Write "</form>"
	Set Rs = Nothing
	Set Rs_e = Nothing
End Sub

Private Sub savenew()
	Dim sortid
	Dim rootid
	Dim ParentID
	Dim depth
	Dim orders
	Dim Maxrootid
	Dim strParent
	Dim neworders
	On Error Resume Next
	If Request("sortname") = "" Then
		ErrMsg = ErrMsg + "<br>" + "<li>请输入分类名称。"
		Founderr = True
		Exit Sub
	End If
	If Request("class") = "" Then
		ErrMsg = ErrMsg + "<br>" + "<li>请选择软件分类。"
		Founderr = True
		Exit Sub
	End If
	If Request("readme") = "" Then
		ErrMsg = ErrMsg + "<br>" + "<li>请输入分类说明。"
		Founderr = True
		Exit Sub
	End If
	Set Rs = CreateObject("adodb.recordset")
	If Request("class") <> "0" Then
		SQL = "select rootid,sortid,depth,orders,strparent from NC_SoftSort where sortid=" & Request("class")
		Rs.Open SQL, Conn, 1, 1
		rootid = Rs(0)
		ParentID = Rs(1)
		depth = Rs(2)
		orders = Rs(3)
		If depth + 1 > 20 Then
			ErrMsg = "<li>本系统限制最多只能有20级子分类</li>"
			Founderr = True
			Exit Sub
		End If
		strParent = Rs(4)
		Rs.Close
		neworders = orders
		SQL = "select max(orders) from NC_SoftSort where ParentID=" & Request("class")
		Rs.Open SQL, Conn, 1, 1
		If Not (Rs.bof And Rs.EOF) Then
			neworders = Rs(0)
		End If
		If IsNull(neworders) Then neworders = orders
		Rs.Close
		DownsysClass.Execute ("update NC_SoftSort set orders=orders+1 where orders>" & CInt(neworders) & "")
	Else
		SQL = "select max(rootid) from NC_SoftSort"
		Rs.Open SQL, Conn, 1, 1
		Maxrootid = Rs(0) + 1
		If IsNull(Maxrootid) Then Maxrootid = 1
		Rs.Close
	End If
	SQL = "select sortid from NC_SoftSort where sortid=" & Request("newsortid")
	Rs.Open SQL, Conn, 1, 1
	If Not (Rs.EOF And Rs.bof) Then
		ErrMsg = "<li>您不能指定和别的分类一样的序号。</li>"
		Founderr = True
		Exit Sub
	Else
		sortid = Request("newsortid")
	End If
	Rs.Close
	SQL = "select * from NC_SoftSort"
	Rs.Open SQL, Conn, 1, 3
	Rs.addnew
	If Request("class") <> "0" Then
		Rs("depth") = depth + 1
		Rs("rootid") = rootid
		Rs("orders") = neworders + 1
		Rs("parentid") = Request.Form("class")
		If strParent = "0" Then
			Rs("strparent") = Request.Form("class")
		Else
			Rs("strparent") = strParent & "," & Request.Form("class")
		End If
	Else
		Rs("depth") = 0
		Rs("rootid") = Maxrootid
		Rs("orders") = 0
		Rs("parentid") = 0
		Rs("strparent") = 0
	End If
	Rs("child") = 0
	Rs("sortid") = Request.Form("newsortid")
	Rs("sortname") = Server.HTMLEncode(Request.Form("sortname"))
	Rs("readme") = Request.Form("readme")
	Rs("skinid") = Request.Form("skinid")
	Rs("isUpdate") = 1
	Rs.Update
	Rs.Close
	If Request("class") <> "0" Then
		If depth > 0 Then DownsysClass.Execute ("update NC_SoftSort set child=child+1 where sortid in (" & strParent & ")")
		DownsysClass.Execute ("update NC_SoftSort set child=child+1 where sortid=" & Request("class"))
	End If
	Set Rs = DownsysClass.Execute("select ID from NC_Template")
	Do While Not Rs.EOF
		DownsysClass.DelCahe ("SortingList" & Rs(0))
		DownsysClass.DelCahe ("SortingMenu" & Rs(0))
		Rs.movenext
	Loop
	DownsysClass.DelCahe "SortingJumpMenu"
	DownsysClass.DelCahe "SortingJumpList"
	SucMsg = "<li>分类添加成功！</li>"
	NC_Admin.Succeed_Msg (SucMsg)
	Set Rs = Nothing
End Sub

Private Sub savedit()
	Dim newsortid
	Dim Maxrootid
	Dim ParentID
	Dim depth
	Dim Child
	Dim strParent
	Dim rootid
	Dim iparentid
	Dim istrparent
	Dim trs
	Dim brs
	Dim mrs
	Dim k
	Dim nstrparent
	Dim mstrparent
	Dim ParentSql
	Dim boardcount
	If CLng(Request("editid")) = CLng(Request("class")) Then
		ErrMsg = "<li>所属分类不能指定自己</li>"
		Founderr = True
		Exit Sub
	End If
	Set Rs = CreateObject("adodb.recordset")
	SQL = "select * from NC_SoftSort where sortid=" & Request("editid")
	Rs.Open SQL, Conn, 1, 3
	newsortid = Rs("sortid")
	ParentID = Rs("parentid")
	iparentid = Rs("parentid")
	strParent = Rs("strparent")
	depth = Rs("depth")
	Child = Rs("child")
	rootid = Rs("rootid")

	If ParentID = 0 Then
		If CLng(Request("class")) <> 0 Then
			Set trs = DownsysClass.Execute("select rootid from NC_SoftSort where sortid=" & Request("class"))
			If rootid = trs(0) Then
				ErrMsg = "<li>您不能指定该分类的下属分类作为所属分类</li>"
				Founderr = True
				Exit Sub
				Response.End
			End If
		End If
	Else
		Set trs = DownsysClass.Execute("select sortid from NC_SoftSort where strparent like '%" & strParent & "%' and sortid=" & Request("class"))
		If Not (trs.EOF And trs.bof) Then
			ErrMsg = "<li>您不能指定该分类的下属分类作为所属分类</li>"
			Founderr = True
			Exit Sub
			Response.End
		End If
	End If
	If ParentID = 0 Then
		ParentID = Rs("sortid")
		iparentid = 0
	End If
	Rs("sortname") = Server.HTMLEncode(Request.Form("sortname"))

	Rs("readme") = Request("readme")
	Rs("skinid") = Request.Form("skinid")
	Rs("isUpdate") = 1
	Rs.Update
	Rs.Close
	Set Rs = Nothing
	Set mrs = DownsysClass.Execute("select max(rootid) from NC_SoftSort")
	Maxrootid = mrs(0) + 1

	If CLng(ParentID) <> CLng(Request("class")) And Not (iparentid = 0 And CInt(Request("class")) = 0) Then
		If iparentid > 0 And CInt(Request("class")) = 0 Then
			DownsysClass.Execute ("update NC_SoftSort set depth=0,orders=0,rootid=" & Maxrootid & ",parentid=0,strparent='0' where sortid=" & newsortid)
			strParent = strParent & ","
			Set Rs = DownsysClass.Execute("select count(*) from NC_SoftSort where strparent like '%" & strParent & "%'")
			boardcount = Rs(0)
			If IsNull(boardcount) Then
				boardcount = 1
			Else
				boardcount = boardcount + 1
			End If
			DownsysClass.Execute ("update NC_SoftSort set child=child-" & boardcount & " where sortid=" & iparentid)
			For i = 1 To depth
				Set Rs = DownsysClass.Execute("select parentid from NC_SoftSort where sortid=" & iparentid)
				If Not (Rs.EOF And Rs.bof) Then
					iparentid = Rs(0)
					DownsysClass.Execute ("update NC_SoftSort set child=child-" & boardcount & " where sortid=" & iparentid)
				End If
			Next
			If Child > 0 Then
				i = 0
				Set Rs = DownsysClass.Execute("select * from NC_SoftSort where strparent like '%" & strParent & "%'")
				Do While Not Rs.EOF
					i = i + 1
					mstrparent = Replace(Rs("strparent"), strParent, "")
					DownsysClass.Execute ("update NC_SoftSort set depth=depth-" & depth & ",rootid=" & Maxrootid & ",strparent='" & mstrparent & "' where sortid=" & Rs("sortid"))
					Rs.movenext
				Loop
			End If
		ElseIf iparentid > 0 And CInt(Request("class")) > 0 Then
			Set trs = DownsysClass.Execute("select * from NC_SoftSort where sortid=" & Request("class"))
			strParent = strParent & ","
			Set Rs = DownsysClass.Execute("select count(*) from NC_SoftSort where strparent like '%" & strParent & "%'")
			boardcount = Rs(0)
			If IsNull(boardcount) Then boardcount = 1
			DownsysClass.Execute ("update NC_SoftSort set orders=orders + " & boardcount & " + 1 where rootid=" & trs("rootid") & " and orders>" & trs("orders") & "")
			DownsysClass.Execute ("update NC_SoftSort set depth=" & trs("depth") & "+1,orders=" & trs("orders") & "+1,rootid=" & trs("rootid") & ",ParentID=" & Request("class") & ",strparent='" & trs("strparent") & "," & trs("sortid") & "' where sortid=" & newsortid)
			i = 1
			Set Rs = DownsysClass.Execute("select * from NC_SoftSort where strparent like '%" & strParent & "%' order by orders")
			Do While Not Rs.EOF
				i = i + 1
				istrparent = trs("strparent") & "," & trs("sortid") & "," & Replace(Rs("strparent"), strParent, "")
				DownsysClass.Execute ("update NC_SoftSort set depth=depth+" & trs("depth") & "-" & depth & "+1,orders=" & trs("orders") & "+" & i & ",rootid=" & trs("rootid") & ",strparent='" & istrparent & "' where sortid=" & Rs("sortid"))
				Rs.movenext
			Loop
			ParentID = Request("class")
			If rootid = trs("rootid") Then

				DownsysClass.Execute ("update NC_SoftSort set child=child+" & i & " where (not ParentID=0) and sortid=" & ParentID)
				For k = 1 To trs("depth")
					Set Rs = DownsysClass.Execute("select parentid from NC_SoftSort where (not ParentID=0) and sortid=" & ParentID)
					If Not (Rs.EOF And Rs.bof) Then
						ParentID = Rs(0)
						DownsysClass.Execute ("update NC_SoftSort set child=child+" & i & " where (not ParentID=0) and  sortid=" & ParentID)
					End If
				Next
				DownsysClass.Execute ("update NC_SoftSort set child=child-" & i & " where (not ParentID=0) and sortid=" & iparentid)
				For k = 1 To depth
					Set Rs = DownsysClass.Execute("select parentid from NC_SoftSort where (not ParentID=0) and sortid=" & iparentid)
					If Not (Rs.EOF And Rs.bof) Then
						iparentid = Rs(0)
						DownsysClass.Execute ("update NC_SoftSort set child=child-" & i & " where (not ParentID=0) and  sortid=" & iparentid)
					End If
				Next
			Else
				DownsysClass.Execute ("update NC_SoftSort set child=child+" & i & " where sortid=" & ParentID)
				For k = 1 To trs("depth")
					Set Rs = DownsysClass.Execute("select parentid from NC_SoftSort where sortid=" & ParentID)
					If Not (Rs.EOF And Rs.bof) Then
						ParentID = Rs(0)
						DownsysClass.Execute ("update NC_SoftSort set child=child+" & i & " where sortid=" & ParentID)
					End If
				Next
				DownsysClass.Execute ("update NC_SoftSort set child=child-" & i & " where sortid=" & iparentid)
				For k = 1 To depth
					Set Rs = DownsysClass.Execute("select parentid from NC_SoftSort where sortid=" & iparentid)
					If Not (Rs.EOF And Rs.bof) Then
						iparentid = Rs(0)
						DownsysClass.Execute ("update NC_SoftSort set child=child-" & i & " where sortid=" & iparentid)
					End If
				Next
			End If
		Else

			Set trs = DownsysClass.Execute("select * from NC_SoftSort where sortid=" & Request("class"))
			Set Rs = DownsysClass.Execute("select count(*) from NC_SoftSort where rootid=" & rootid)
			boardcount = Rs(0)
			ParentID = Request("class")
			DownsysClass.Execute ("update NC_SoftSort set child=child+" & boardcount & " where sortid=" & ParentID)
			For k = 1 To trs("depth")
				Set Rs = DownsysClass.Execute("select parentid from NC_SoftSort where sortid=" & ParentID)
				If Not (Rs.EOF And Rs.bof) Then
					ParentID = Rs(0)
					DownsysClass.Execute ("update NC_SoftSort set child=child+" & boardcount & " where sortid=" & ParentID)
				End If
			Next
			DownsysClass.Execute ("update NC_SoftSort set orders=orders + " & boardcount & " + 1 where rootid=" & trs("rootid") & " and orders>" & trs("orders") & "")
			i = 0
			Set Rs = DownsysClass.Execute("select * from NC_SoftSort where rootid=" & rootid & " order by orders")
			Do While Not Rs.EOF
				i = i + 1
				If Rs("parentid") = 0 Then
					If trs("strparent") = "0" Then
						strParent = trs("sortid")
					Else
						strParent = trs("strparent") & "," & trs("sortid")
					End If
					DownsysClass.Execute ("update NC_SoftSort set depth=depth+" & trs("depth") & "+1,orders=" & trs("orders") & "+" & i & ",rootid=" & trs("rootid") & ",strparent='" & strParent & "',parentid=" & Request("class") & " where sortid=" & Rs("sortid"))
				Else
					If trs("strparent") = "0" Then
						strParent = trs("sortid") & "," & Rs("strparent")
					Else
						strParent = trs("strparent") & "," & trs("sortid") & "," & Rs("strparent")
					End If
					DownsysClass.Execute ("update NC_SoftSort set depth=depth+" & trs("depth") & "+1,orders=" & trs("orders") & "+" & i & ",rootid=" & trs("rootid") & ",strparent='" & strParent & "' where sortid=" & Rs("sortid"))
				End If
				Rs.movenext
			Loop
		End If
	End If
	Set Rs = DownsysClass.Execute("select ID from NC_Template")
	Do While Not Rs.EOF
		DownsysClass.DelCahe ("SortingList" & Rs(0))
		DownsysClass.DelCahe ("SortingMenu" & Rs(0))
		Rs.movenext
	Loop
	DownsysClass.DelCahe "SortingJumpMenu"
	DownsysClass.DelCahe "SortingJumpList"
	SucMsg = "<li>分类修改成功！</li>"
	NC_Admin.Succeed_Msg (SucMsg)
	Set Rs = Nothing
	Set mrs = Nothing
	Set trs = Nothing
End Sub

Private Sub del()
	On Error Resume Next
	Set Rs = DownsysClass.Execute("select strparent,child,depth from NC_SoftSort where sortid=" & Request("editid"))
	If Not (Rs.EOF And Rs.bof) Then
		If Rs(1) > 0 Then
			Response.Write "该分类含有下属分类，请删除其下属分类后再进行删除本分类的操作"
			Exit Sub
		End If
		If Rs(2) > 0 Then
			DownsysClass.Execute ("update NC_SoftSort set child=child-1 where sortid in (" & Rs(0) & ")")
		End If
		SQL = "delete from NC_SoftSort where sortid=" & Request("editid")
		DownsysClass.Execute (SQL)
		DownsysClass.Execute ("delete from NC_SoftInfo where sortid=" & Request("editid"))
		DownsysClass.Execute ("delete from NC_SoftSite where sortid=" & Request("editid"))
	End If

	If CInt(DownsysClass.Setting(5)) = 0 Then
		NewFolderPath = Server.MapPath(DownsysClass.SetupDir & "Software/Catalog" & Request("editid"))
		NewFolderPath1 = Server.MapPath(DownsysClass.SetupDir & "Sorting/Catalog" & Request("editid"))
		Set objFSO = Server.CreateObject(DownsysClass.Script_FSO)
		If objFSO.FolderExists(NewFolderPath) Then
			objFSO.DeleteFile(NewFolderPath)
		End If
		If objFSO.FolderExists(NewFolderPath1) Then
			objFSO.DeleteFile(NewFolderPath1)
		End If
		objFSO.Close
		Set objFSO = Nothing
	End If

	Set Rs = DownsysClass.Execute("select ID from NC_Template")
	Do While Not Rs.EOF
		DownsysClass.DelCahe ("SortingList" & Rs(0))
		DownsysClass.DelCahe ("SortingMenu" & Rs(0))
		Rs.movenext
	Loop
	Set Rs = Nothing
	DownsysClass.DelCahe "SortingJumpMenu"
	DownsysClass.DelCahe "SortingJumpList"
	NC_Admin.Succeed_Msg ("分类删除成功！")

End Sub

Private Sub orders()
	Response.Write " <table width=""96%"" border=""0"" cellspacing=""1"" cellpadding=""3"" align=center class=""tableBorder"">"
	Response.Write " <tr>"
	Response.Write " <th>分类一级分类重新排序修改(请在相应软件分类的排序表单内输入相应的排列序号) </th>"
	Response.Write " </tr>"
	Response.Write " <tr>"
	Response.Write " <td class=forumrow>"
	Response.Write "<table width=""50%"">"
	Set Rs = CreateObject("Adodb.recordset")
	SQL = "select * from NC_SoftSort where ParentID=0 order by RootID"
	Rs.Open SQL, Conn, 1, 1
	If Rs.bof And Rs.EOF Then
		Response.Write "还没有相应的软件分类。"
	Else
		Do While Not Rs.EOF
			Response.Write "<form action=admin_sorting.asp?action=updatorders method=post><tr><td width=""50%"">" & Rs("sortname") & "</td>"
			Response.Write "<td width=""50%""><input type=text name=""OrderID"" size=4 value=""" & Rs("rootid") & """><input type=hidden name=""cID"" value=""" & Rs("rootid") & """>&nbsp;&nbsp;<input type=submit name=Submit class=button value='修 改'></td></tr></form>"
			Rs.movenext
		Loop
		Response.Write "</table>"
		Response.Write "<BR>&nbsp;<font color=red>请注意，这里一定<B>不能填写相同的序号</B>，否则非常难修复！</font>"
	End If
	Rs.Close
	Set Rs = Nothing
	Response.Write " </td>"
	Response.Write " </tr>"
	Response.Write "</table>"
End Sub

Private Sub updateorders()
	Dim cID
	Dim OrderID
	Dim ClassName
	cID = Replace(Request.Form("cID"), "'", "")
	OrderID = Replace(Request.Form("OrderID"), "'", "")
	Set Rs = DownsysClass.Execute("select sortid from NC_SoftSort where rootid=" & OrderID)
	If Rs.bof And Rs.EOF Then
		NC_Admin.Succeed_Msg ("设置成功，请返回。")
		DownsysClass.Execute ("update NC_SoftSort set rootid=" & OrderID & " where rootid=" & cID)
		DownsysClass.Execute ("update NC_SoftInfo set rootid=" & OrderID & " where rootid=" & cID)
	Else
		NC_Admin.Error_Msg ("请不要和其他分类设置相同的序号")
	End If
	Set Rs = DownsysClass.Execute("select ID from NC_Template")
	Do While Not Rs.EOF
		DownsysClass.DelCahe ("SortingList" & Rs(0))
		DownsysClass.DelCahe ("SortingMenu" & Rs(0))
		Rs.movenext
	Loop
	DownsysClass.DelCahe "SortingJumpMenu"
	DownsysClass.DelCahe "SortingJumpList"
	Set Rs = Nothing
End Sub

Private Sub boardorders()
	Dim trs
	Dim uporders
	Dim doorders
	Response.Write " <table width=""96%"" border=""0"" cellspacing=""1"" cellpadding=""2"" class=""tableBorder"" align=center>"
	Response.Write " <tr>"
	Response.Write " <th colspan=2 class=""forumrow"">分类N级分类重新排序修改(请在相应软件分类的排序表单内输入相应的排列序号)"
	Response.Write " </th>"
	Response.Write " </tr>"
	Set Rs = Server.CreateObject("Adodb.recordset")
	SQL = "select * from NC_SoftSort order by RootID,orders"
	Rs.Open SQL, Conn, 1, 1
	If Rs.bof And Rs.EOF Then
		Response.Write "还没有相应的软件分类。"
	Else
		Do While Not Rs.EOF
			Response.Write "<form action=admin_sorting.asp?action=updatboardorders method=post><tr><td width=""50%"" class=forumrow>"
			If Rs("depth") = 1 Then Response.Write "&nbsp;&nbsp;<font color=""#666666"">├</font>"
			If Rs("depth") > 1 Then
				For i = 2 To Rs("depth")
					Response.Write "&nbsp;&nbsp;<font color=""#666666"">│</font>"
				Next
				Response.Write "&nbsp;&nbsp;<font color=""#666666"">├</font> "
			End If
			If Rs("parentid") = 0 Then Response.Write ("<b>")
			Response.Write Rs("sortname")
			If Rs("child") > 0 Then Response.Write "(" & Rs("child") & ")"
			Response.Write "</td><td width=""50%"" class=forumrow>"
			If Rs("ParentID") > 0 Then
				Set trs = DownsysClass.Execute("select count(*) from NC_SoftSort where ParentID=" & Rs("ParentID") & " and orders<" & Rs("orders") & "")
				uporders = trs(0)
				If IsNull(uporders) Then uporders = 0
				Set trs = DownsysClass.Execute("select count(*) from NC_SoftSort where ParentID=" & Rs("ParentID") & " and orders>" & Rs("orders") & "")
				doorders = trs(0)
				If IsNull(doorders) Then doorders = 0
				If uporders > 0 Then
					Response.Write "<select name=uporders size=1><option value=0>↑</option>"
					For i = 1 To uporders
						Response.Write "<option value=" & i & ">↑" & i & "</option>"
					Next
					Response.Write "</select>"
				End If
				If doorders > 0 Then
					If uporders > 0 Then Response.Write "&nbsp;"
					Response.Write "<select name=doorders size=1><option value=0>↓</option>"
					For i = 1 To doorders
						Response.Write "<option value=" & i & ">↓" & i & "</option>"
					Next
					Response.Write "</select>"
				End If
				If doorders > 0 Or uporders > 0 Then
					Response.Write "<input type=hidden name=""editID"" value=""" & Rs("sortid") & """>&nbsp;<input type=submit name=Submit class=button value='修 改'>"
				End If
			End If
			Response.Write "</td></tr></form>"
			uporders = 0
			doorders = 0
			Rs.movenext
		Loop
	End If
	Rs.Close
	Set Rs = Nothing
	Response.Write "</table>"

End Sub

Private Sub updateboardorders()
	Dim ParentID
	Dim orders
	Dim strParent
	Dim Child
	Dim uporders
	Dim doorders
	Dim oldorders
	Dim trs
	Dim ii
	If Not IsNumeric(Request("editID")) Then
		NC_Admin.Error_Msg ("非法的参数！")
		Exit Sub
	End If
	If Request("uporders") <> "" And Not CInt(Request("uporders")) = 0 Then
		If Not IsNumeric(Request("uporders")) Then
			NC_Admin.Error_Msg ("非法的参数！")
			Exit Sub
		ElseIf CInt(Request("uporders")) = 0 Then
			NC_Admin.Error_Msg ("请选择要提升的数字！")
			Exit Sub
		End If
		Set Rs = DownsysClass.Execute("select ParentID,orders,strparent,child from NC_SoftSort where sortid=" & Request("editID"))
		ParentID = Rs(0)
		orders = Rs(1)
		strParent = Rs(2) & "," & Request("editID")
		Child = Rs(3)
		i = 0
		If Child > 0 Then
			Set Rs = DownsysClass.Execute("select count(*) from NC_SoftSort where strparent like '%" & strParent & "%'")
			oldorders = Rs(0)
		Else
			oldorders = 0
		End If
		Set Rs = DownsysClass.Execute("select sortid,orders,child,strparent from NC_SoftSort where ParentID=" & ParentID & " and orders<" & orders & " order by orders desc")
		Do While Not Rs.EOF
			i = i + 1
			If CInt(Request("uporders")) >= i Then
				If Rs(2) > 0 Then
					ii = 0
					Set trs = DownsysClass.Execute("select sortid,orders from NC_SoftSort where strparent like '%" & Rs(3) & "," & Rs(0) & "%' order by orders")
					If Not (trs.EOF And trs.bof) Then
						Do While Not trs.EOF
							ii = ii + 1
							DownsysClass.Execute ("update NC_SoftSort set orders=" & orders & "+" & oldorders & "+" & ii & " where sortid=" & trs(0))
							trs.movenext
						Loop
					End If
				End If
				DownsysClass.Execute ("update NC_SoftSort set orders=" & orders & "+" & oldorders & " where sortid=" & Rs(0))
				If CInt(Request("uporders")) = i Then uporders = Rs(1)
			End If
			orders = Rs(1)
			Rs.movenext
		Loop
		DownsysClass.Execute ("update NC_SoftSort set orders=" & uporders & " where sortid=" & Request("editID"))
		If Child > 0 Then
			i = uporders
			Set Rs = DownsysClass.Execute("select sortid from NC_SoftSort where strparent like '%" & strParent & "%' order by orders")
			Do While Not Rs.EOF
				i = i + 1
				DownsysClass.Execute ("update NC_SoftSort set orders=" & i & " where sortid=" & Rs(0))
				Rs.movenext
			Loop
		End If
		Set Rs = Nothing
		Set trs = Nothing
	ElseIf Request("doorders") <> "" Then
		If Not IsNumeric(Request("doorders")) Then
			NC_Admin.Error_Msg ("非法的参数！")
			Exit Sub
		ElseIf CInt(Request("doorders")) = 0 Then
			NC_Admin.Error_Msg ("请选择要下降的数字！")
			Exit Sub
		End If
		Set Rs = DownsysClass.Execute("select ParentID,orders,strparent,child from NC_SoftSort where sortid=" & Request("editID"))
		ParentID = Rs(0)
		orders = Rs(1)
		strParent = Rs(2) & "," & Request("editID")
		Child = Rs(3)
		i = 0
		If Child > 0 Then
			Set Rs = DownsysClass.Execute("select count(*) from NC_SoftSort where strparent like '%" & strParent & "%'")
			oldorders = Rs(0)
		Else
			oldorders = 0
		End If
		Set Rs = DownsysClass.Execute("select sortid,orders,child,strparent from NC_SoftSort where ParentID=" & ParentID & " and orders>" & orders & " order by orders")
		Do While Not Rs.EOF
			i = i + 1
			If CInt(Request("doorders")) >= i Then
				If Rs(2) > 0 Then
					ii = 0
					Set trs = DownsysClass.Execute("select sortid,orders from NC_SoftSort where strparent like '%" & Rs(3) & "," & Rs(0) & "%' order by orders")
					If Not (trs.EOF And trs.bof) Then
						Do While Not trs.EOF
							ii = ii + 1
							DownsysClass.Execute ("update NC_SoftSort set orders=" & orders & "+" & ii & " where sortid=" & trs(0))
							trs.movenext
						Loop
					End If
				End If
				DownsysClass.Execute ("update NC_SoftSort set orders=" & orders & " where sortid=" & Rs(0))
				If CInt(Request("doorders")) = i Then doorders = Rs(1)
			End If
			orders = Rs(1)
			Rs.movenext
		Loop
		DownsysClass.Execute ("update NC_SoftSort set orders=" & doorders & " where sortid=" & Request("editID"))
		If Child > 0 Then
			i = doorders
			Set Rs = DownsysClass.Execute("select sortid from NC_SoftSort where strparent like '%" & strParent & "%' order by orders")
			Do While Not Rs.EOF
				i = i + 1
				DownsysClass.Execute ("update NC_SoftSort set orders=" & i & " where sortid=" & Rs(0))
				Rs.movenext
			Loop
		End If

	End If
	Set Rs = DownsysClass.Execute("select ID from NC_Template")
	Do While Not Rs.EOF
		DownsysClass.DelCahe ("SortingList" & Rs(0))
		DownsysClass.DelCahe ("SortingMenu" & Rs(0))
		Rs.movenext
	Loop
	Set Rs = Nothing
	Set trs = Nothing
	DownsysClass.DelCahe "SortingJumpMenu"
	DownsysClass.DelCahe "SortingJumpList"
	Response.redirect "admin_sorting.asp?action=boardorders"
End Sub

Private Sub RestoreBoard()
	i = 0
	Set Rs = DownsysClass.Execute("select sortid from NC_SoftSort order by rootid,orders")
	Do While Not Rs.EOF
		i = i + 1
		DownsysClass.Execute ("update NC_SoftSort set rootid=" & i & ",depth=0,orders=0,ParentID=0,strparent='0',child=0 where sortid=" & Rs(0))
		Rs.movenext
	Loop
	Set Rs = DownsysClass.Execute("select ID from NC_Template")
	Do While Not Rs.EOF
		DownsysClass.DelCahe ("SortingList" & Rs(0))
		DownsysClass.DelCahe ("SortingMenu" & Rs(0))
		Rs.movenext
	Loop
	Set Rs = Nothing
	DownsysClass.DelCahe "SortingJumpMenu"
	DownsysClass.DelCahe "SortingJumpList"
	NC_Admin.Succeed_Msg ("复位成功，请返回做分类归属设置。")

End Sub
%>
