<!--#include file="conn.asp"-->
<!--#include file="check.asp"-->
<%
ClassID=clng(request.Form("ClassID"))
ClassName=trim(request.Form("ClassName"))
classtype=trim(request.Form("classtype"))
url1=trim(request.Form("url"))
	Select case classtype
	case "1"
	  url1="photo.asp"
	case "2"
	  url1="wen.asp"
	case "3"
	  url1="contents.asp"
	case "4"
	  url1=trim(request.Form("url"))
	End Select
If Request.QueryString("style")="del" Then
	DeleteClass
ElseIf Request.QueryString("style")="modify" Then
	ModifyClass
ElseIf Request.QueryString("style")="add" Then
	AddNewClass
End If

sub AddNewClass()
	dim ID
	dim sql,rs,trs
	dim RootID,MaxID,MaxRootID
	dim PrevID,NextID
	'on error resume next
	set rs = conn.execute("select Max(ID) From classname")
	MaxID=rs(0)
	if isnull(MaxID) then
		MaxID=0
	end if
	rs.close
	ID=MaxID+1
	set rs=conn.execute("select max(rootid) From classname")
	MaxRootID=rs(0)
	if isnull(MaxRootID) then
		MaxRootID=0
	end if
	rs.close
	RootID=MaxRootID+1
	
	if MaxRootID>0 then
		set trs=conn.execute("select ID from classname where RootID=" & MaxRootID & "")
		PrevID=trs(0)
		trs.close
	else
		PrevID=0
	end if
	PrevOrderID=0
	sql="Select top 1 * From classname"
	rs.open sql,conn,1,3
    rs.addnew
	rs("ID")=ID
   	rs("classname")=ClassName
   	rs("classtype")=classtype
	rs("RootID")=RootID
	rs("PrevID")=PrevID
	rs("NextID")=0
	RS("url")=url1

	rs.update

	rs.close:Set RS=nothing
    connclose	
	Response.write "<script language=javascript>alert('分类添加成功!');this.location.href='classmana.asp?style=1';</script>" 
	Response.End() 
end sub

sub DeleteClass()
	dim sql,rs,PrevID,NextID
	on error resume next
	set rs=conn.execute("select ID from imgbook where titleID=(select titleID from title where ClassID="&ClassID&")")
	if not (rs.eof or rs.bof) then
		Response.write "<script language=javascript>alert('请先把该分类的所有图片删除后再删除该分类!');this.location.href='classmana.asp?style=1';</script>" 
		Response.End() 
	end if
	sql="select * From classname where ID="&ClassID
	set rs=server.CreateObject ("Adodb.recordset")
	rs.open sql,conn,1,3

	PrevID=rs("PrevID")
	NextID=rs("NextID")

	rs.delete
	rs.update
	rs.close:Set RS=nothing
	'删除本栏目的所有信息
	conn.execute("delete from classname where ID=" & ClassID)
	
	'修改上一栏目的NextID和下一栏目的PrevID
	if PrevID>0 then
		conn.execute "update classname set NextID=" & NextID & " where ID=" & PrevID
	end if
	if NextID>0 then
		conn.execute "update classname set PrevID=" & PrevID & " where ID=" & NextID
	end if
	connclose
	Response.write "<script language=javascript>alert('分类删除成功!');this.location.href='classmana.asp?style=1';</script>" 
	Response.End() 
end sub

sub ModifyClass()
	'on error resume next
	if classname<>"" then
	conn.execute("update [classname] set classname='"&ClassName&"',classtype='"&Classtype&"',url='"&url1&"' where ID="&ClassID)
	Else
conn.execute("update [classname] set classtype='"&Classtype&"',url='"&url1&"' where ID="&ClassID)
	End if
	Response.write "<script language=javascript>alert('分类修改成功!');this.location.href='classmana.asp?style=1';</script>" 
	Response.End() 
end sub
%>