<!--#include file="../inc/config.asp" -->
<!--#include file="../inc/conn.asp" -->
<!--#include file="inc/function.asp" -->
<!-- #include file="inc/Session.asp" -->
<!-- #include file="inc/checkrole.asp" -->
<%
'''''''''''''''''''''''''''''''''''''''''''''''
call checkPrevilege(Session("Name"),"p01")
'''''''''''''''''''''''''''''''''''''''''''''''
Dim ParentID,Result,ClassID,ParentCName,ParentSaveDir,rs,iscatalog,CatalogName,AddTime,CatalogTemplate,SaveDir,LastCatalogId,domainurl,c_keywords,c_desc
act=Request.QueryString("act")
if(request("id")<>"") then
	ParentID=Clng(Request("id"))
else
	call alertstr("请选择正确的父栏目",1)
end if
if(act="add") then
	CatalogName=request.Form("CatalogName")
	AddTime=request.Form("AddTime")
	CatalogTemplate=request.Form("CatalogTemplate")
	SaveDir=request.Form("SaveFilePath")	
	SavePathType=Cint(request.Form("SavePathType"))
	c_keywords=request.Form("keywords")
	c_desc=request.Form("description")
	set rs=server.CreateObject("adodb.recordset")
	rs.open "select top 1 id from catalogs order by id desc",conn,3,1
	if not rs.eof and not rs.bof then
		LastCatalogId=rs("id")+1
	else
		LastCatalogId=1
	end if
	rs.close
	set rs=nothing
	if(SavePathType=1) then
		if not isempty(request("SaveFilePathRegix")) and len(request("SaveFilePathRegix"))>1 then
			SaveDir=request("SaveFilePathRegix")		
			SaveDir=ReplaceStaticLabel(SaveDir,LastCatalogId)
		end if	
	end if
	if(SaveDir<>"") then
		SaveDir=replace(SaveDir,"//","/")
	else
		call alertstr("栏目保存路径不能为空",1)
	end if
	'父栏目域名及保存路径
	dim psavdir,pdomain,relatepath
	set rs=server.CreateObject("adodb.recordset")
	rs.open "select savedir,domainurl from catalogs where id="&ParentID,conn,3,1
	if not rs.eof then
		psavdir=rs(0)
		pdomain=rs(1)
	end if
	rs.close
	Set rs = Nothing
	''''''''''''''''''''''''
	iscatalog=Cint(request.Form("iscatalog"))
	if(iscatalog=1) then
		domainurl=request.form("domainurl")
		if(domainurl="" or len(domainurl)<1) then
			relatepath=replace(SaveDir,psavdir,"")
			domainurl=pdomain&relatepath
			'domainurl=SiteUrl&SaveDir
			'call alertstr("域名设置错误\n不能为空",1)
		else
			if(right(domainurl,1)="/") then
				domainurl=left(domainurl,len(domainurl)-1)
			end if
		end if
	else
		'''''''''''''''''''''域名+相对路径'''''''''''''''''''''''		
		if(SaveDir<>"" and ParentID<>0) then
			relatepath=replace(SaveDir,psavdir,"")
			domainurl=pdomain&relatepath
		else
			domainurl=SaveDir
		end if
		
	end if
	if(domainurl="" or len(domainurl)<1) then
		domainurl=SiteUrl&SaveDir
	end if
	ReturnCheckInfo =AddAndModifyClass(LastCatalogId,ParentID,CatalogName,iscatalog,AddTime,CatalogTemplate,SaveDir,domainurl,c_keywords,c_desc)
	if(ReturnCheckInfo=true) then
		response.Write("<script>location.href=""tree.asp"";</script>")
		response.End()
	end if
else
	if ParentID <0 then
		ParentCName = "参数传递错误，请重新加载"
	elseif ParentID = 0 then
		ParentCName = "根栏目"
		ParentSaveDir=WebRootDir
	else
		set rs=server.CreateObject("adodb.recordset")
		rs.open "select title,savedir from catalogs where id="&ParentID,conn,3,1
		if rs.Eof or rs.Bof then
			ParentCName = "父栏目不存在，请重新加载"
		else
			ParentCName = rs(0)
			ParentSaveDir=rs(1)
		end if
		rs.close
		Set rs = Nothing
	end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>栏目添加</title>
<style type="text/css">
<!--
.style1 {	color: #FFFFFF;
	font-weight: bold;
}
-->
</style>
</head>
<link href="Class.css" rel="stylesheet">
<script type="text/javascript">
function OpenWindowAndSetValue(Url,Width,Height,WindowObj,SetObj)
{
	var ReturnStr=showModalDialog(Url,WindowObj,'dialogWidth:'+Width+'pt;dialogHeight:'+Height+'pt;status:no;help:no;scroll:no;');
	if (ReturnStr!='') SetObj.value=ReturnStr;
	return ReturnStr;
}
function check(obj)
{	
	if(obj.CatalogName.value==""){
		alert("栏目名称不能为空!");
		obj.CatalogName.focus();
		obj.CatalogName.select();
		return false;
	}
	else if(obj.CatalogTemplate.value==""){
		alert("栏目模板不能为空!");
		obj.CatalogTemplate.focus();
		obj.CatalogTemplate.select();
		return false;
	}	
	else if(obj.SavePathType[0].checked && obj.SaveFilePath.value==""){
		alert("栏目保存路径不能为空!");		
		return false;
	}
	else if(obj.SavePathType[0].checked && obj.SaveFilePath.value=='<%=ParentSaveDir%>'){
		alert("栏目保存路径必需设置在父栏目保存路径之下!\n正确的设置如:<%=ParentSaveDir%>/aa,<%=ParentSaveDir%>/bb");		
		return false;
	}
	/*else if(obj.iscatalog.checked==true && obj.domainurl.value==""){
		alert("域名地址不能为空!");
		obj.domainurl.focus();
		obj.domainurl.select();
		return false;
	}*/
	else{
		return true;
	}
}
function $(id) {
	return document.getElementById(id);
}
function resizeup(obj) {
	var newheight = parseInt($(obj).style.height, 10) + 50;
	$(obj).style.height = newheight + 'px';
}
function resizedown(obj) {
	var newheight = parseInt($(obj).style.height, 10) - 50;
	if(newheight > 0) {
		$(obj).style.height = newheight + 'px';
	}
}
</script>
<body>
<table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><iframe src="right_top.htm" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" height="30" width="100%"></iframe></td>
  </tr>
</table>
<table width="100%" height="22"  border="0" cellpadding="0" cellspacing="0" bgcolor="#FF9900" style="border-left:1px solid #000000;border-right:1px solid #000000;border-top:1px solid #000000;">
  <tr>
    <td><div align="center" class="style1">添加栏目</div></td>
  </tr>
</table>
<div align="center">
  <table width="100%" border="0" cellspacing="5" cellpadding="0" style="border:1px solid #000000; ">
    <form action="?act=add" method="post" name="ClassForm" onSubmit="return check(this);">
      <tr> 
        <td height="5" colspan="2"></td>
      </tr>
      <tr> 
        <td width="19%"><div align="center">类别名称</div></td>
        <td width="81%"> <div align="center"> 
            <input value="" name="CatalogName" type="text" id="CatalogName" style="width:100%;">
          </div></td>
      </tr>
      <tr> 
        <td><div align="center">父 栏 目</div></td>
        <td> <div align="center"> 
            <input readonly value="<%=ParentCName %>" style="width:100%;" type="text" name="textfield3">
          </div></td>
      </tr>
      <tr> 
        <td><div align="center">栏目模板</div></td>
        <td nowrap> <div align="left"> 
            <input name="CatalogTemplate" type="text" style="width:78%;" value="<%=TempletDir%>" readonly><input type="button" name="Submit" value="选择模板" onClick="OpenWindowAndSetValue('Inc/SelectFileFrame.asp?CurrPath=<%=TempletDir%>',400,300,window,document.ClassForm.CatalogTemplate);document.ClassForm.CatalogTemplate.focus();">
          </div></td>
      </tr>
      <tr>
        <td><div align="center">保存路径设置</div></td>
        <td nowrap>手动
          <input name="SavePathType" type="radio" value="0" onClick="disp1()" checked>　
        自动
        <input name="SavePathType" type="radio" value="1" onClick="disp2()"></td>
      </tr>
      <tr id="saveFilePath"> 
        <td><div align="center">保存路径</div></td>
        <td nowrap> <div align="left"> 
            <input readonly value="<%=ParentSaveDir%>" type="text" style="width:78%;" name="SaveFilePath"><input type="button" name="Submit" value="选择路径" onClick="OpenWindowAndSetValue('Inc/SelectPathFrame.asp?CurrPath=<%=ParentSaveDir%>',400,300,window,document.ClassForm.SaveFilePath);document.ClassForm.SaveFilePath.focus();">
          </div></td>
      </tr>
	  <tr id="saveFileParse" style="display:none; "> 
        <td><div align="center">保存路径规则</div></td>
        <td nowrap> <div align="left"> 
            <input value="<%if(ParentSaveDir<>"/") then response.Write(ParentSaveDir)%>/$classid$" type="text" style="width:50%;" name="SaveFilePathRegix">(系统自动)<br>
            (标签：$SiteUrl$,$Year$等,更多请到"标签管理"->"常用标签")
          </div></td>
      </tr>
      <tr>
        <td><div align="center">添加日期</div></td>
        <td><div align="left">
            <input readonly value="<%If Request("AddTime")="" then Response.Write(Now()) else Response.Write(Request("AddTime")) %>" type="text" style="width:77%;" name="AddTime"><input name="sdaf" type="button" id="sdaf" onClick="OpenWindowAndSetValue('Inc/SelectDate.asp',280,120,window,document.ClassForm.AddTime);document.ClassForm.AddTime.focus();" value="选择日期">
        </div></td>
      </tr>
      <tr>
        <td><div align="center">作为频道</div></td>
        <td><input name="iscatalog" type="checkbox" id="iscatalog" value="1" onClick="checkDomain()"></td>
      </tr>
	  <tr id="domainset" style="display:none;">
        <td><div align="center">域名地址</div></td>
        <td><input name="domainurl" type="text" id="domainurl" size="30">
        <input type="button" name="Submit3" value="同保存路径" onClick="this.form.domainurl.value=this.form.SaveFilePath.value">
		
</td>
      </tr>
	  <tr>
        <td><div align="center">栏目关键词</div></td>
        <td><input name="keywords" style="width:100%;" type="text" id="keywords"></td>
      </tr>
	  
	  <tr>
        <td><div align="center">栏目描述</div></td>
        <td><textarea style="width:100%; height:50px;" name="description" id="description"></textarea>
          <B><A onClick="resizeup('description');" href="###">[+]</A> 
<A onClick="resizedown('description');" href="###">[-]</A></B></td>
      </tr>
      <tr>
        <td><div align="center">栏目模型</div></td>
        <td>
        <select name="ModuleId" id="ModuleId">
        <%
				set rs=server.createobject("adodb.recordset")
				rs.open "select id,Title from t_module",conn,3,1
				while not rs.eof
					response.Write("<option value="""&rs(0)&""""&">"&rs(1)&"</option>")
					rs.movenext
				wend
				rs.close
				set rs=nothing
				%>
        </select>
        </td>
      </tr>
      <tr>
        <td><div align="center">内容页模板</div></td>
        <td>
        <select name="TempletId" id="TempletId">
        <%
				set rs=server.createobject("adodb.recordset")
				rs.open "select id,templetname from templet",conn,3,1
				while not rs.eof
					response.Write("<option value="""&rs(0)&""""&">"&rs(1)&"</option>")
					rs.movenext
				wend
				rs.close
				set rs=nothing
				%>
        </select>
        </td>
      </tr>
      <tr> 
        <td colspan="2">
		<div align="center"> 
                  <input type="submit" name="Submitgsdfg" value=" 确 定 ">                  
                  <input name="ClassID" value="<%=ClassID %>" type="hidden" id="ClassID">
                  <input name="id" value="<%=ParentID %>" type="hidden" id="ParentID">
                  <input type="button" name="Submit2" onClick="window.close();" value=" 取 消 ">
        </div></td>
      </tr>
    </form>
  </table>
</div>
<%
end if
%>
<script language="JavaScript">
function disp1(){
saveFilePath.style.display='';
saveFileParse.style.display='none';
}
function disp2(){
saveFileParse.style.display='';
saveFilePath.style.display='none';
}
function checkDomain(){
	if(document.ClassForm.iscatalog.checked==true){
		document.getElementById('domainset').style.display='';
	}else{
		document.ClassForm.domainurl.value='';
		document.getElementById('domainset').style.display='none';
	}
}
</script>
</body>
</html>

<%
call connclose()
'=======================================================================
'函数：AddAndModifyClass(ParentID,ClassCName,Contr,AddTime,ClassTemp,SaveDir)
'作用：添加分类
'参数：ParentID     ---父节点ID号
'      ClassCName        ---分类名称
'      Contr        ---是否允许投稿
'      AddTime        ---添加时间
'      ClassTemp        ---分类模板
'      SaveDir        ---保存路径
'=====================================================================

Function AddAndModifyClass(Catalogid,ParentID,ClassCName,IsCatalog,AddTime,ClassTemp,SaveDir,DomainUrl,KeyWords,Desc)
	on error resume next
	dim rs
	set rs=server.CreateObject("adodb.recordset")
	rs.open "select top 1 * from catalogs where 1=0 order by id desc",conn,3,3
	rs.addnew
	rs("id")=Catalogid
	rs("parentid")=ParentID
	rs("title")=ClassCName
	rs("flag")=IsCatalog
	rs("childnum")=0
	rs("sortid")=Catalogid
	rs("moditime")=AddTime
	rs("templet")=ClassTemp
	rs("savedir")=SaveDir
	rs("domainurl")=DomainUrl
	rs("keywords")=KeyWords
	rs("description")=Desc
	rs("ModuleId")=Cint(request.Form("ModuleId"))
	rs("TempletId")=Cint(request.Form("TempletId"))
	rs.update
	rs.close
	set rs=nothing
	conn.execute("update catalogs set childnum=childnum+1 where id="&ParentID)
	if Err.Number<>0 then
		AddAndModifyClass=false
	else
		AddAndModifyClass=true
	end if
end Function
%>