<!--#include file = "Startup.asp"-->
<!-- #include file="Function.asp" -->
<%
call adminer()
Call Header()
Call ComeUrl()

%>

<%
dim Action,ParentID,SkinID,LayoutID,BrowsePurview,AddPurview,i,FoundErr,ErrMsg
dim SkinCount,LayoutCount
Action=trim(Request("Action"))
ParentID=trim(request("ParentID"))
if ParentID="" then
	ParentID=0
else
	ParentID=CLng(ParentID)
end if
%>
<%
if Action="Add" then
	call AddClass()
	call Main()
elseif Action="SaveAdd" then
	call SaveAdd()
elseif Action="Del" then
	call DeleteClass()
elseif Action="Modify" then
	call Modify()
	call Main()
elseif Action="SaveModify" then
	call SaveModify()
elseif Action="Clear" then
	call ClearClass()
else
	call AddClass()
	call Main()
end if
if FoundErr=True then
	call WriteErrMsg()
end if
%>





<%
Sub AddClass()
%>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="JavaScript" type="text/JavaScript">
function check()
{
  if (document.form1.ClassName.value=="")
  {
    alert("栏目名称不能为空！");
	document.form1.ClassName.focus();
	return false;
  }
}
</script>
<table width="100%" border="0" align=center cellpadding="3" cellspacing="1" bgcolor="#DEDFDE">
  <form name="form1" method="post" action="News_Class.asp" onsubmit="return check()">
    <tr bgcolor="#F7F7F7">
      <td colspan="3" align="center"><STRONG>添 加 资 讯 栏 目</STRONG></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td width="200">所属栏目分类：</td>
      <td><select name="ParentID">
          <%call NewsClass_Option(0,ParentID)%>
      </select>
        <font color="#ff6600"><strong>注意：</strong>请不要使用三级目录深度。</font> </td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td>新加栏目名称：</td>
      <td><input name="ClassName" type="text" class="input_text" size="30" maxlength="30"></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td>&nbsp;</td>
      <td><input name="Action" type="hidden" id="Action" value="SaveAdd"><input type="submit" name="Submit" value="　提　交　" class="input_submit">　　　
        <input type="reset" name="Submit" value="　重　置　" class="input_submit"></td>
    </tr>
  </form>
</table>
<%
End Sub
%>








<%
Sub Main()
	dim arrShowLine(10)
	for i=0 to ubound(arrShowLine)
		arrShowLine(i)=False
	next
	dim sqlClass,rsClass,i,iDepth
	sqlClass="select * From NewsClass order by RootID,OrderID"
	set rsClass=server.CreateObject("adodb.recordset")
	rsClass.open sqlClass,oConn,1,3

%>
<br><br>
<table width="100%"  border="0" cellpadding="0" cellspacing="1" bgcolor="#DEDFDE">
  <tr bgcolor="#F7F7F7">
    <td height="25"><div align="center"><strong>栏目名称</strong></div></td>
    <td width="100"><div align="center"><strong>添加子栏目</strong></div></td>
    <td width="100"><div align="center"><strong>修改栏目</strong></div></td>
    <td width="120"><div align="center"><strong>清空栏目下资讯</strong></div></td>
    <td width="80"><div align="center"><strong>删除</strong></div></td>
  </tr>

<%
If Not rsClass.Eof Then
Do While Not rsClass.Eof
%>  
  
  <tr bgcolor="#FFFFFF" onmouseout="this.style.backgroundColor=''" onmouseover="this.style.backgroundColor='#F7F8F8'">
    <td><% 
	iDepth=rsClass("Depth")
	if rsClass("NextID")>0 then
		arrShowLine(iDepth)=True
	else
		arrShowLine(iDepth)=False
	end if
	if iDepth>0 then
	  	for i=1 to iDepth 
			if i=iDepth then 
				if rsClass("NextID")>0 then 
					response.write "<img src='images/tree_line1.gif' width='17' height='16' valign='abvmiddle'>" 
				else 
					response.write "<img src='images/tree_line2.gif' width='17' height='16' valign='abvmiddle'>" 
				end if 
			else 
				if arrShowLine(i)=True then
					response.write "<img src='images/tree_line3.gif' width='17' height='16' valign='abvmiddle'>" 
				else
					response.write "<img src='images/tree_line4.gif' width='17' height='16' valign='abvmiddle'>" 
				end if
			end if 
	  	next 
	  end if 
	  if rsClass("Child")>0 then 
	  	response.write "<img src='Images/tree_folder4.gif' width='15' height='15' valign='abvmiddle'>" 
	  else 
	  	response.write "<img src='Images/tree_folder3.gif' width='15' height='15' valign='abvmiddle'>" 
	  end if 
	  if rsClass("Depth")=0 then 
	  	response.write "<b>" 
	  end if 
	  response.write "<a href='News_Class.asp?Action=Modify&ClassID=" & rsClass("ClassID") & "'>" & rsClass("ClassName") & "</a>"
	  if rsClass("Child")>0 then 
	  	response.write "（" & rsClass("Child") & "）" 
	  end if
	  %></td>
    <td><div align="center"><a href="News_Class.asp?Action=Add&ParentID=<%=rsClass("ClassID")%>">添加子栏目</a></div></td>
    <td><div align="center"><a href="News_Class.asp?Action=Modify&ClassID=<%=rsClass("ClassID")%>">修改栏目</a></div></td>
    <td><div align="center"><a href="News_class.asp?Action=Clear&ClassID=<%=rsClass("ClassID")%>" onClick="return ConfirmDel3();">清空栏目下资讯</a></div></td>
    <td><div align="center"><a href="News_Class.asp?Action=Del&ClassID=<%=rsClass("ClassID")%>" onClick="<%if rsClass("Child")>0 then%>return ConfirmDel1();<%else%>return ConfirmDel2();<%end if%>">删除</a></div></td>
  </tr>
  <%
  rsClass.MoveNext
  Loop
  Else
  Response.Write "<tr><td colspan='5' bgcolor='#ffffff'><div align='center'>暂无栏目分类信息！！</div></td></tr>"
  End If
  %>
</table>

<script language="JavaScript" type="text/JavaScript">
function ConfirmDel1()
{
   alert("此栏目下还有子栏目，必须先删除下属子栏目后才能删除此栏目！");
   return false;
}

function ConfirmDel2()
{
   if(confirm("删除栏目将同时删除此栏目中的所有资讯及图片，并且不能恢复！确定要删除此栏目吗？"))
     return true;
   else
     return false;
	 
}
function ConfirmDel3()
{
   if(confirm("清空栏目将把栏目的所有资讯删除！确定要清空此栏目吗？"))
     return true;
   else
     return false;
	 
}
</script>

<%
End Sub
%>


<%
Sub Modify()

	dim ClassID,sql,rsClass,i
	dim SkinID,LayoutID,BrowsePurview,AddPurview
	ClassID=trim(request("ClassID"))
	if ClassID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>参数不足！</li>"
		exit sub
	else
		ClassID=CLng(ClassID)
	end if
	
	sql="select * From NewsClass where ClassID=" & ClassID
	set rsClass=server.CreateObject ("Adodb.recordset")
	rsClass.open sql,oConn,1,3
	if rsClass.bof and rsClass.eof then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>找不到指定的栏目！</li>"
	else

%>
<table width="100%" border="0" align=center cellpadding="3" cellspacing="1" bgcolor="#DEDFDE" class="border">
  <form name="form1" method="post" action="News_Class.asp">
    <tr bgcolor="#F7F7F7">
      <td colspan="3" align="center"><STRONG>修 改 资 讯 栏 目</STRONG></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td width="200">修改所属栏目分类：</td>
      <td><select name="ParentID">
          <%call NewsClass_Option(0,rsClass("ParentID"))%>
      </select></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td>当前所属栏目：</td>
      <td><%
	if rsClass("ParentID")<=0 then
	  	response.write "无（作为一级栏目）"
	else
    	dim rsParent,sqlParent
		sqlParent="Select * From NewsClass where ClassID in (" & rsClass("ParentPath") & ") order by Depth"
		set rsParent=server.CreateObject("adodb.recordset")
		rsParent.open sqlParent,oConn,1,3
		do while not rsParent.eof
			for i=1 to rsParent("Depth")
				response.write "&nbsp;&nbsp;&nbsp;"
			next
			if rsParent("Depth")>0 then
				response.write "└"
			end if
			response.write "&nbsp;" & rsParent("ClassName") & "<br>"
			rsParent.movenext
		loop
		rsParent.close
		set rsParent=nothing
	end if
	%></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td>修改栏目名称：</td>
      <td><input name="ClassName" type="text" class="input_text" size="30" maxlength="30"value="<%=rsClass("ClassName")%>"> <input name="ClassID" type="hidden" id="ClassID" value="<%=rsClass("ClassID")%>"></td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td>&nbsp;</td>
      <td><input name="Action" type="hidden" id="Action" value="SaveModify"><input type="submit" name="Submit" value="　提　交　" class="input_submit">　　　
        <input type="reset" name="Submit" value="　重　置　" class="input_submit"></td>
    </tr>
  </form>
</table>
<%
end if
rsClass.close
set rsClass=nothing
End Sub
%>










<%
sub SaveAdd()
	dim ClassID,ClassName,IsElite,ShowOnTop,Setting,Readme,ClassMaster,ClassPicUrl,LinkUrl,PrevOrderID
	dim sql,rs,trs
	dim RootID,ParentDepth,ParentPath,ParentStr,ParentName,MaxClassID,MaxRootID
	dim PrevID,NextID,Child

	ClassName=trim(request("ClassName"))'栏目名称
	Setting=trim(request("Setting"))'栏目设置
	if ClassName="" then'栏目名称
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>栏目名称不能为空！</li>"
	end if
	if FoundErr=True then
		exit sub
	end if

	set rs = oConn.execute("select Max(ClassID) From NewsClass")'ClassID栏目ID
	MaxClassID=rs(0)
	if isnull(MaxClassID) then
		MaxClassID=0
	end if
	rs.close
	ClassID=MaxClassID+1
	set rs=oConn.execute("select max(rootid) From NewsClass")'rootid根栏目ID
	MaxRootID=rs(0)
	if isnull(MaxRootID) then
		MaxRootID=0
	end if
	rs.close
	RootID=MaxRootID+1
	
	if ParentID>0 then
		sql="select * From NewsClass where ClassID=" & ParentID & ""'ParentID父栏目ID
		rs.open sql,oConn,1,3
		if rs.bof and rs.eof then
			FoundErr=True
			ErrMsg=ErrMsg & "<br><li>所属栏目已经被删除！</li>"
		end if
		if FoundErr=True then
			rs.close
			set rs=nothing
			exit sub
		else	
			RootID=rs("RootID")'根栏目ID
			ParentName=rs("ClassName")'栏目名称
			ParentDepth=rs("Depth")'栏目层数
			ParentPath=rs("ParentPath")'栏目路径
			Child=rs("Child")'子栏目数
			ParentPath=ParentPath & "," & ParentID     '得到此栏目的父级栏目路径
			PrevOrderID=rs("OrderID")'排序ID
			if Child>0 then		
				dim rsPrevOrderID
				'得到与本栏目同级的最后一个栏目的OrderID排序ID
				set rsPrevOrderID=oConn.execute("select Max(OrderID) From NewsClass where ParentID=" & ParentID)
				PrevOrderID=rsPrevOrderID(0)
				set trs=oConn.execute("select ClassID from NewsClass where ParentID=" & ParentID & " and OrderID=" & PrevOrderID)
				PrevID=trs(0)
				
				'得到同一父栏目但比本栏目级数大的子栏目的最大OrderID排序ID，如果比前一个值大，则改用这个值。
				set rsPrevOrderID=oConn.execute("select Max(OrderID) From NewsClass where ParentPath like '" & ParentPath & ",%'")
				if (not(rsPrevOrderID.bof and rsPrevOrderID.eof)) then
					if not IsNull(rsPrevOrderID(0))  then
				 		if rsPrevOrderID(0)>PrevOrderID then
							PrevOrderID=rsPrevOrderID(0)
						end if
					end if
				end if
			else
				PrevID=0
			end if

		end if
		rs.close
	else
		if MaxRootID>0 then
			set trs=oConn.execute("select ClassID from NewsClass where RootID=" & MaxRootID & " and Depth=0")
			PrevID=trs(0)
			trs.close
		else
			PrevID=0
		end if
		PrevOrderID=0
		ParentPath="0"
	end if

	sql="Select * From NewsClass Where ParentID=" & ParentID & " AND ClassName='" & ClassName & "'"
	set rs=server.CreateObject("adodb.recordset")
	rs.open sql,oConn,1,3
	if not(rs.bof and rs.eof) then
		FoundErr=True
		if ParentID=0 then
			ErrMsg=ErrMsg & "<br><li>已经存在一级栏目：" & ClassName & "</li>"
		else
			ErrMsg=ErrMsg & "<br><li>“" & ParentName & "”中已经存在子栏目“" & ClassName & "”！</li>"
		end if
		rs.close
		set rs=nothing
		exit sub
	end if
	rs.close

	sql="Select top 1 * From NewsClass"
	rs.open sql,oConn,1,3
    rs.addnew
	'rs("ClassID")=5 'ClassID'栏目ID class 自动生成。2009-5-17 
   	rs("ClassName")=ClassName'栏目名称
	rs("RootID")=RootID'根栏目ID

	rs("ParentID")=ParentID'父栏目ID
	if ParentID>0 then
		rs("Depth")=ParentDepth+1'Depth栏目层数
	else
		rs("Depth")=0
	end if
	rs("ParentPath")=ParentPath'栏目路径
	rs("OrderID")=PrevOrderID'排序ID
	rs("Child")=0'子栏目数
	rs("PrevID")=PrevID'同级的上一个栏目ID
	rs("NextID")=0'同级的下一个栏目ID
	rs.update
	rs.Close
    set rs=Nothing
	
	'更新与本栏目同一父栏目的上一个栏目的“NextID”字段值
	if PrevID>0 then'PrevID同级的上一个栏目ID
		oConn.execute("update NewsClass set NextID=" & ClassID & " where ClassID=" & PrevID)
	end if
	
	if ParentID>0 then'父栏目ID
		'更新其父类的子栏目数
		oConn.execute("update NewsClass set child=child+1 where ClassID="&ParentID)
		
		'更新该栏目排序以及大于本需要和同在本分类下的栏目排序序号
		oConn.execute("update NewsClass set OrderID=OrderID+1 where rootid=" & rootid & " and OrderID>" & PrevOrderID)
		oConn.execute("update NewsClass set OrderID=" & PrevOrderID & "+1 where ClassID=" & ClassID)
	end if
	Response.Write "<p align=center>资讯栏目添加成功，3秒后自动返回资讯栏目管理页！<script>window.setTimeout(""location.href='News_Class.asp'"",3000);</script></p>"
end sub
%>


<%
sub DeleteClass()
	dim sql,rs,PrevID,NextID,ClassID
	ClassID=trim(Request("ClassID"))
	if ClassID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>参数不足！</li>"
		exit sub
	else
		ClassID=CLng(ClassID)
	end if
	
	sql="select ClassID,RootID,Depth,ParentID,Child,PrevID,NextID From NewsClass where ClassID="&ClassID
	set rs=server.CreateObject ("Adodb.recordset")
	rs.open sql,oConn,1,3
	if rs.bof and rs.eof then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>栏目不存在，或者已经被删除</li>"
	else
		if rs("Child")>0 then
			FoundErr=True
			ErrMsg=ErrMsg & "<br><li>该栏目含有子栏目，请删除其子栏目后再进行删除本栏目的操作</li>"
		end if
	end if
	if FoundErr=True then
		rs.close
		set rs=nothing
		exit sub
	end if
	PrevID=rs("PrevID")
	NextID=rs("NextID")
	if rs("Depth")>0 then
		oConn.execute("update NewsClass set child=child-1 where ClassID=" & rs("ParentID"))'child子栏目数,Depth栏目层数
	end if
	rs.delete
	rs.update
	rs.close
	set rs=nothing
	
	'删除栏目分类下所有新闻中的图片
	Dim sSavePathFileName,aSavePathFileName,i
	sSql = "SELECT D_SavePathFileName,D_ClassID FROM NewsData WHERE D_ClassID ="& ClassID
	oRs.Open sSql, oConn, 0, 1
		Do While Not oRs.Eof
		sSavePathFileName = oRs("D_SavePathFileName")
		' 把带"|"的字符串转为数组
		aSavePathFileName = Split(sSavePathFileName, "|")

		' 删除新闻相关的文件，从文件夹中
		For i = 0 To UBound(aSavePathFileName)
			' 按路径文件名删除文件
			Call DoDelFile(aSavePathFileName(i))
		Next
		oRs.MoveNext
		Loop
	oRs.Close
	
	
	'删除本栏目的所有新闻
	oConn.execute("delete from NewsData where D_ClassID=" & ClassID)
	
	'修改上一栏目的NextID和下一栏目的PrevID
	if PrevID>0 then
		oConn.execute "update NewsClass set NextID=" & NextID & " where ClassID=" & PrevID
	end if
	if NextID>0 then
		oConn.execute "update NewsClass set PrevID=" & PrevID & " where ClassID=" & NextID
	end if
	Response.Write "<p align=center>资讯栏目删除成功，3秒后自动返回资讯栏目管理页！<script>window.setTimeout(""location.href='News_Class.asp'"",3000);</script></p>"
	'response.redirect "News_Class.asp"
		
end sub


' 删除指定的文件
Sub DoDelFile(sPathFile)
	On Error Resume Next
	Dim oFSO
	Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
	oFSO.DeleteFile(Server.MapPath(sPathFile))
	Set oFSO = Nothing
End Sub
%>




<%
sub SaveModify()
	
	dim ClassID,ClassName,sql,rsClass,i
	dim rParentID
	dim trs,rs
	dim ParentID,RootID,Depth,Child,ParentPath,ParentName,iParentID,iParentPath,PrevOrderID,PrevID,NextID
	ClassID=trim(request("ClassID"))
	if ClassID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>参数不足！</li>"
		exit sub
	else
		ClassID=CLng(ClassID)
	end if
	
	sql="select * From NewsClass where ClassID=" & ClassID
	set rsClass=server.CreateObject ("Adodb.recordset")
	rsClass.open sql,oConn,1,3
	if rsClass.bof and rsClass.eof then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>找不到指定的栏目！</li>"
		rsClass.close
		set rsClass=nothing
		exit sub
	end if
	'更新栏目名称
	ClassName=trim(request("ClassName"))
   	rsClass("ClassName")=ClassName
	rsClass.update
	
	rParentID=trim(request("ParentID"))'父栏目ID
	if rParentID="" then
		rParentID=0
	else
		rParentID=CLng(rParentID)
	end if
	
	if rsClass("ParentID")<>rParentID then   '更改了所属栏目，则要做一系列检查
		if rParentID=rsClass("ClassID") then
			FoundErr=True
			ErrMsg=ErrMsg & "<br><li>所属栏目不能为自己！</li>"
		end if
		'判断所指定的栏目是否为外部栏目或本栏目的下属栏目
		if rsClass("ParentID")=0 then
			if rParentID>0 then
				set trs=oConn.execute("select rootid From NewsClass where ClassID="&rParentID)'rootid根栏目ID
				if trs.bof and trs.eof then
					FoundErr=True
					ErrMsg=ErrMsg & "<br><li>不能指定外部栏目为所属栏目</li>"
				else
					if rsClass("rootid")=trs(0) then
						FoundErr=True
						ErrMsg=ErrMsg & "<br><li>不能指定该栏目的下属栏目作为所属栏目</li>"
					end if
				end if
				trs.close
				set trs=nothing
			end if
		else
			set trs=oConn.execute("select ClassID From NewsClass where ParentPath like '"&rsClass("ParentPath")&"," & rsClass("ClassID") & "%' and ClassID="&rParentID)
			if not (trs.eof and trs.bof) then
				FoundErr=True
				ErrMsg=ErrMsg & "<br><li>您不能指定该栏目的下属栏目作为所属栏目</li>"
			end if
			trs.close
			set trs=nothing
		end if
		
	end if

	if FoundErr=True then
		rsClass.close
		set rsClass=nothing
		exit sub
	end if
	
	if rsClass("ParentID")=0 then
		ParentID=rsClass("ClassID")
		iParentID=0
	else
		ParentID=rsClass("ParentID")
		iParentID=rsClass("ParentID")
	end if
	Depth=rsClass("Depth")'栏目层数
	Child=rsClass("Child")'子栏目数
	RootID=rsClass("RootID")'根栏目ID
	ParentPath=rsClass("ParentPath")'栏目路径
	PrevID=rsClass("PrevID")'同级的上一个栏目ID
	NextID=rsClass("NextID")'同级的下一个栏目ID
	rsClass.close
	set rsClass=nothing
	
	
  '假如更改了所属栏目
  '需要更新其原来所属栏目信息，包括深度、父级ID、栏目数、排序、继承版主等数据
  '需要更新当前所属栏目信息
  '继承版主数据需要另写函数进行更新--取消，在前台可用ClassID in ParentPath来获得
  dim mrs,MaxRootID
  set mrs=oConn.execute("select max(rootid) From NewsClass")
  MaxRootID=mrs(0)
  set mrs=nothing
  if isnull(MaxRootID) then
	MaxRootID=0
  end if
  dim k,nParentPath,mParentPath
  dim ParentSql,ClassCount
  dim rsPrevOrderID
  if clng(parentid)<>rParentID and not (iParentID=0 and rParentID=0) then  '假如更改了所属栏目
	'更新原来同一父栏目的上一个栏目的NextID和下一个栏目的PrevID
	if PrevID>0 then
		oConn.execute "update NewsClass set NextID=" & NextID & " where ClassID=" & PrevID
	end if
	if NextID>0 then
		oConn.execute "update NewsClass set PrevID=" & PrevID & " where ClassID=" & NextID
	end if
	
	if iParentID>0 and rParentID=0 then  	'如果原来不是一级分类改成一级分类
		'得到上一个一级分类栏目
		sql="select ClassID,NextID from NewsClass where RootID=" & MaxRootID & " and Depth=0"
		set rs=server.CreateObject("Adodb.recordset")
		rs.open sql,oConn,1,3
		PrevID=rs(0)      '得到新的PrevID
		rs(1)=ClassID     '更新上一个一级分类栏目的NextID的值
		rs.update
		rs.close
		set rs=nothing
		
		MaxRootID=MaxRootID+1
		'更新当前栏目数据
		oConn.execute("update NewsClass set depth=0,OrderID=0,rootid="&maxrootid&",parentid=0,ParentPath='0',PrevID=" & PrevID & ",NextID=0 where ClassID="&ClassID)
		'如果有下属栏目，则更新其下属栏目数据。下属栏目的排序不需考虑，只需更新下属栏目深度和一级排序ID(rootid)数据
		if child>0 then
			i=0
			ParentPath=ParentPath & ","
			set rs=oConn.execute("select * From NewsClass where ParentPath like '%"&ParentPath & ClassID&"%'")
			do while not rs.eof
				i=i+1
				mParentPath=replace(rs("ParentPath"),ParentPath,"")
				oConn.execute("update NewsClass set depth=depth-"&depth&",rootid="&maxrootid&",ParentPath='"&mParentPath&"' where ClassID="&rs("ClassID"))
				rs.movenext
			loop
			rs.close
			set rs=nothing
		end if
		
		'更新其原来所属栏目的栏目数，排序相当于剪枝而不需考虑
		oConn.execute("update NewsClass set child=child-1 where ClassID="&iParentID)
		
	elseif iParentID>0 and rParentID>0 then    '如果是将一个分栏目移动到其他分栏目下
		'得到当前栏目的下属子栏目数
		ParentPath=ParentPath & ","
		set rs=oConn.execute("select count(*) From NewsClass where ParentPath like '%"&ParentPath & ClassID&"%'")
		ClassCount=rs(0)
		if isnull(ClassCount) then
			ClassCount=1
		end if
		rs.close
		set rs=nothing
		
		'获得目标栏目的相关信息		
		set trs=oConn.execute("select * From NewsClass where ClassID="&rParentID)
		if trs("Child")>0 then		
			'得到与本栏目同级的最后一个栏目的OrderID
			set rsPrevOrderID=oConn.execute("select Max(OrderID) From NewsClass where ParentID=" & trs("ClassID"))
			PrevOrderID=rsPrevOrderID(0)
			'得到与本栏目同级的最后一个栏目的ClassID
			sql="select ClassID,NextID from NewsClass where ParentID=" & trs("ClassID") & " and OrderID=" & PrevOrderID
			set rs=server.createobject("adodb.recordset")
			rs.open sql,oConn,1,3
			PrevID=rs(0)    '得到新的PrevID
			rs(1)=ClassID     '更新上一个栏目的NextID的值
			rs.update
			rs.close
			set rs=nothing
			
			'得到同一父栏目但比本栏目级数大的子栏目的最大OrderID，如果比前一个值大，则改用这个值。
			set rsPrevOrderID=oConn.execute("select Max(OrderID) From NewsClass where ParentPath like '" & trs("ParentPath") & "," & trs("ClassID") & ",%'")
			if (not(rsPrevOrderID.bof and rsPrevOrderID.eof)) then
				if not IsNull(rsPrevOrderID(0))  then
			 		if rsPrevOrderID(0)>PrevOrderID then
						PrevOrderID=rsPrevOrderID(0)
					end if
				end if
			end if
		else
			PrevID=0
			PrevOrderID=trs("OrderID")
		end if
		
		'在获得移动过来的栏目数后更新排序在指定栏目之后的栏目排序数据
		oConn.execute("update NewsClass set OrderID=OrderID+" & ClassCount & "+1 where rootid=" & trs("rootid") & " and OrderID>" & PrevOrderID)
		
		'更新当前栏目数据
		oConn.execute("update NewsClass set depth="&trs("depth")&"+1,OrderID="&PrevOrderID&"+1,rootid="&trs("rootid")&",ParentID="&rParentID&",ParentPath='" & trs("ParentPath") & "," & trs("ClassID") & "',PrevID=" & PrevID & ",NextID=0 where ClassID="&ClassID)
		
		'如果有子栏目则更新子栏目数据，深度为原来的相对深度加上当前所属栏目的深度
		set rs=oConn.execute("select * From NewsClass where ParentPath like '%"&ParentPath&ClassID&"%' order by OrderID")
		i=1
		do while not rs.eof
			i=i+1
			iParentPath=trs("ParentPath") & "," & trs("ClassID") & "," & replace(rs("ParentPath"),ParentPath,"")
			oConn.execute("update NewsClass set depth=depth-"&depth&"+"&trs("depth")&"+1,OrderID="&PrevOrderID&"+"&i&",rootid="&trs("rootid")&",ParentPath='"&iParentPath&"' where ClassID="&rs("ClassID"))
			rs.movenext
		loop
		rs.close
		set rs=nothing
		trs.close
		set trs=nothing
		
		'更新所指向的上级栏目的子栏目数
		oConn.execute("update NewsClass set child=child+1 where ClassID="&rParentID)
		
		'更新其原父类的子栏目数			
		oConn.execute("update NewsClass set child=child-1 where ClassID="&iParentID)
	else    '如果原来是一级栏目改成其他栏目的下属栏目
		'得到移动的栏目总数
		set rs=oConn.execute("select count(*) From NewsClass where rootid="&rootid)
		ClassCount=rs(0)
		rs.close
		set rs=nothing
		
		'获得目标栏目的相关信息		
		set trs=oConn.execute("select * From NewsClass where ClassID="&rParentID)
		if trs("Child")>0 then		
			'得到与本栏目同级的最后一个栏目的OrderID
			set rsPrevOrderID=oConn.execute("select Max(OrderID) From NewsClass where ParentID=" & trs("ClassID"))
			PrevOrderID=rsPrevOrderID(0)
			sql="select ClassID,NextID from NewsClass where ParentID=" & trs("ClassID") & " and OrderID=" & PrevOrderID
			set rs=server.createobject("adodb.recordset")
			rs.open sql,oConn,1,3
			PrevID=rs(0)
			rs(1)=ClassID
			rs.update
			set rs=nothing
			
			'得到同一父栏目但比本栏目级数大的子栏目的最大OrderID，如果比前一个值大，则改用这个值。
			set rsPrevOrderID=oConn.execute("select Max(OrderID) From NewsClass where ParentPath like '" & trs("ParentPath") & "," & trs("ClassID") & ",%'")
			if (not(rsPrevOrderID.bof and rsPrevOrderID.eof)) then
				if not IsNull(rsPrevOrderID(0))  then
			 		if rsPrevOrderID(0)>PrevOrderID then
						PrevOrderID=rsPrevOrderID(0)
					end if
				end if
			end if
		else
			PrevID=0
			PrevOrderID=trs("OrderID")
		end if
	
		'在获得移动过来的栏目数后更新排序在指定栏目之后的栏目排序数据
		oConn.execute("update NewsClass set OrderID=OrderID+" & ClassCount &"+1 where rootid=" & trs("rootid") & " and OrderID>" & PrevOrderID)
		
		oConn.execute("update NewsClass set PrevID=" & PrevID & ",NextID=0 where ClassID=" & ClassID)
		set rs=oConn.execute("select * From NewsClass where rootid="&rootid&" order by OrderID")
		i=0
		do while not rs.eof
			i=i+1
			if rs("parentid")=0 then
				ParentPath=trs("ParentPath") & "," & trs("ClassID")
				oConn.execute("update NewsClass set depth=depth+"&trs("depth")&"+1,OrderID="&PrevOrderID&"+"&i&",rootid="&trs("rootid")&",ParentPath='"&ParentPath&"',parentid="&rParentID&" where ClassID="&rs("ClassID"))
			else
				ParentPath=trs("ParentPath") & "," & trs("ClassID") & "," & replace(rs("ParentPath"),"0,","")
				oConn.execute("update NewsClass set depth=depth+"&trs("depth")&"+1,OrderID="&PrevOrderID&"+"&i&",rootid="&trs("rootid")&",ParentPath='"&ParentPath&"' where ClassID="&rs("ClassID"))
			end if
			rs.movenext
		loop
		rs.close
		set rs=nothing
		trs.close
		set trs=nothing
		'更新所指向的上级栏目栏目数		
		oConn.execute("update NewsClass set child=child+1 where ClassID="&rParentID)

	end if
  end if
	
	Response.Write "<p align=center>资讯栏目修改成功，3秒后自动返回资讯栏目管理页！<script>window.setTimeout(""location.href='News_Class.asp'"",3000);</script></p>"

end sub




sub ClearClass()
	dim strClassID,rs,trs,SuccessMsg,ClassID
	ClassID=trim(Request("ClassID"))
	if ClassID="" then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>参数不足！</li>"
		exit sub
	else
		ClassID=CLng(ClassID)
	end if
	strClassID=cstr(ClassID)
	set rs=oConn.execute("select ClassID,Child,ParentPath from NewsClass where ClassID=" & ClassID)
	if rs.bof and rs.eof then
		FoundErr=True
		ErrMsg=ErrMsg & "<br><li>栏目不存在，或者已经被删除</li>"
		exit sub
	end if
	if rs(1)>0 then
		set trs=oConn.execute("select ClassID from NewsClass where ParentID=" & rs(0))
		do while not trs.eof
			strClassID=strClassID & "," & trs(0)
			trs.movenext
		loop
		trs.close
		set trs=oConn.execute("select ClassID from NewsClass where ParentPath like '" & rs(2) & "," & rs(0) & ",%'")
		do while not trs.eof
			strClassID=strClassID & "," & trs(0)
			trs.movenext
		loop
		trs.close
		set trs=nothing
	end if
	rs.close
	set rs=nothing
	oConn.execute("update NewsData set d_recycle=0,D_RecycleTime='"&Now()&"' where D_ClassID in (" & strClassID & ")")
	Response.Write "<p align=center>栏目清空成功，3秒后自动返回资讯栏目管理页！<script>window.setTimeout(""location.href='News_Class.asp'"",3000);</script></p>"
end sub

%>