<!--#include file="conn.asp"-->
<!--#include file="chkadmin.asp"-->
<!--#include file="config.asp"-->
<!--#include file="inc/char.asp"-->
<%dim id,ClassName,Sequence,ParentStr,sql,rs,sql2,rs2
id=CInt(Trim(Request.Form("id")))
ClassName=Trim(Request.Form("ClassName"))

dim errmsg,founderr
founderr=false

if chkadmin=false or power(5)="0" then
	founderr=true
	errmsg=errmsg+"<li>您没有添加分类的权限</li>"
end if

if ClassName="" then
	founderr=true
	errmsg=errmsg+"<li>类别名称不能为空</li>"
elseif strlen(ClassName)>20 then
	founderr=true
	errmsg=errmsg+"<li>类别名称应在20个字符内</li>"
end if

if founderr=false then

	sql="select * from Class where ParentID="&id
	set rs=conn.execute(sql)
	if rs.eof and rs.bof then
		Sequence=1
	else
		sql2="select Max(Sequence) as Sequence from Class where ParentID="&id
		set rs2=conn.execute(sql2)
		Sequence=rs2("Sequence")+1
		set rs2=nothing
	end if

	if id>0 then
		sql="select ParentStr from Class where ID="&id
		set rs=conn.execute(sql)
		if rs(0)="0" then
			ParentStr=CStr(id)
		else
			ParentStr=rs(0)&","&CStr(id)
		end if
	else
		ParentStr="0"
	end if

	conn.execute("insert into Class (ClassName,ParentID,ParentStr,Sequence) values ('"&ClassName&"',"&id&",'"&ParentStr&"',"&Sequence&")")

	dim newid
	sql="select top 1 ID from Class order by ID desc"
	set rs=conn.execute(sql)
	newid=rs(0)

	if id>0 then
		sql="select ChildID from Class where ID="&id
		set rs=conn.execute(sql)
		if isnull(rs(0)) or rs(0)="" then
			conn.execute("update Class set ChildID='"&CStr(newid)&"' where ID="&id)
		else
			conn.execute("update Class set ChildID=ChildID&','&'"&CStr(newid)&"' where ID="&id)
		end if
	end if

	set rs=nothing
	dim gourl,msg
	gourl=request.ServerVariables("HTTP_REFERER")
	msg="分类添加成功！"
	call ok()
else
	call error()
end if

conn.close
set conn=nothing
%>