<!--#include file="../Inc/Conn.asp" -->
<!--#include file="../Inc/SK.Session.asp" -->
<!--#include file="../Inc/SK.Funtion.asp" -->
<%if Not (Session("Manager")="1" OR (JudgePopedomTF(Session("UserName"),"SK900000000") AND JudgePopedomTF(Session("UserName"),"SK910000001"))) then Call ReturnError1()%>
<%
dim Action,ParentID,i,FoundErr,ErrMsg
Action=F("Action")
ParentID=F("ParentID")
if ParentID="" then
ParentID=0
else
ParentID=CLng(ParentID)
end if
%>
<html>
<head>
<title>城市区域管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="../images/style.css" type=text/css rel=stylesheet>
<script type="text/javascript" src="../Inc/Check.js"></script>
<script type="text/javascript" src="../Inc/Calendar.js"></script>
<style type="text/css">
<!--
body {
margin: 5px 0px 5px 0px;
overflow-x:hidden;
overflow-y:auto;
}
-->
</style>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<TBODY>
  <tr>
    <td class="SK_Td_Top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="12" height="25"><img src="../images/tab_03.gif" width="12" height="25" /></td>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="46%" valign="middle">城市区域管理</td>
<td width="54%" align="right"><strong><a href="SK.Area.asp">区域管理首页</a> | 
<a href="?Action=Add">新建区域</a>&nbsp;|&nbsp;
<a href="?Action=Order">一级排序</a><!--&nbsp;|&nbsp;<a href="?Action=OrderN">N级排序</a>
&nbsp;|&nbsp;<a href="?Action=Reset">复位所有区域</a>&nbsp;|&nbsp;<a href="?Action=Unite">区域合并</a>--></strong></td>
</tr>
</table></td>
<td width="16"><img src="../images/tab_07.gif" width="16" height="25" /></td>
</tr>
</table></td>
</tr>
<tr>
<td>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="8" background="../images/tab_12.gif"></td>
<td>
<%
if Action="Add" then
call AddClass()
elseif Action="SaveAdd" then

dim AreaName,PrevOrderID
AreaName=F("AreaName")
dim Vote_Array,conts,i123
conts=request.Form("textaread")
if conts<>"" then
Vote_Array = split(conts,"|")
For i123=0 To Ubound(Vote_Array)
AreaName=Vote_Array(i123)
call SaveAdd()
Next
else
call SaveAdd()
end if

Response.Redirect "SK.Area.asp#end" 

elseif Action="Modify" then
call Modify()
elseif Action="SaveModify" then
call SaveModify()
elseif Action="Move" then
call MoveClass()
elseif Action="SaveMove" then
call SaveMove()
elseif Action="Del" then
call DeleteClass()
elseif Action="Clear" then
call ClearClass()
elseif Action="UpOrder" then 
call UpOrder() 
elseif Action="DownOrder" then 
call DownOrder() 
elseif Action="Order" then
call Order()
elseif Action="UpOrderN" then 
call UpOrderN() 
elseif Action="DownOrderN" then 
call DownOrderN() 
elseif Action="OrderN" then
call OrderN()
elseif Action="Reset" then
call Reset()
elseif Action="SaveReset" then
call SaveReset()
elseif Action="Unite" then
call Unite()
elseif Action="SaveUnite" then
call SaveUnite()
else
call main()
end if

sub main()

dim arrShowLine(10)
for i=0 to ubound(arrShowLine)
arrShowLine(i)=False
next
dim sqlClass,rsClass,i,iDepth
set rsClass=server.CreateObject("adodb.recordset")
sqlClass="select * From SK_SystemArea order by RootID,OrderID"
rsClass.open sqlClass,conn,1,1
%>
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
  <tr bgcolor=#efefef> 
    <td width="73%" height="22" align="center"><strong>区域名称</strong></td>
    <td width="27%" height="22" align="center"><strong>操作选项</strong></td>
  </tr>
<% 
do while not rsClass.eof 
%>
<tr onMouseOver="changeto()" onMouseOut="changeback()"> 
<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 "├" 
else 
response.write "└" 
end if 
else 
if arrShowLine(i)=True then
response.write "│" 
else
response.write "&nbsp;&nbsp;" 
end if
end if 
  next 
  end if 
  if rsClass("Child")>0 then 
  response.write "┼" 
  else 
  response.write "─" 
  end if 
  if rsClass("Depth")=0 then 
  response.write "<b>" 
  end if 
  response.write "<a href='SK.Area.asp?Action=Modify&AreaID="&rsClass("AreaID")&"'>"&rsClass("AreaName")&"</a>"
  if rsClass("Child")>0 then 
  response.write "("&rsClass("Child")&")" 
  end if
%>
</td>
<td align="center">
<a href="SK.Area.asp?Action=Add&ParentID=<%=rsClass("AreaID")%>">新建下级</a>
<a href="SK.Area.asp?Action=Del&AreaID=<%=rsClass("AreaID")%>" onClick="<%if rsClass("Child")>0 then%>return ConfirmDel1();<%else%>return ConfirmDel2();<%end if%>">删除</a>
</td>
</tr>
<% 
rsClass.movenext 
loop 
%>
</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 AddClass()
%>
<form name="thisForm" id="form1" method="post" action="SK.Area.asp" onSubmit="return Form1_Validator(this)"> 
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
    <tr bgcolor=#efefef> 
      <td height="22" colspan="2" align="center"><strong>新建区域</strong></td>
    </tr>
    <tr > 
      <td width="35%"><strong>所属上级：</strong></td>
      <td width="65%"> <select name="ParentID">
          <%call Admin_ShowClass_Option(0,ParentID)%>
        </select></td>
    </tr>
    <tr> 
      <td><strong>区域名称：</strong><br>
        可一次增加多个区域，区域之前用|做为分隔符</td>
      <td>
      <textarea name="textaread" cols="90%"  rows="7" id="textaread"></textarea></td>
    </tr>

    <tr > 
      <td height="40" colspan="2" align="center"><input name="Action" type="hidden" id="Action" value="SaveAdd"> 
<input name="Add" type="submit" value=" 添 加 " style="cursor:hand;" class="button1"> 
&nbsp; <input name="Cancel" type="button" id="Cancel" value=" 取 消 " onClick="window.location.href='SK.Area.asp'" style="cursor:hand;" class="button1"> 
      </td>
    </tr>
  </table>
</form>
<SCRIPT language=JavaScript>
function Form1_Validator(theForm)
{
if(form1.textaread.value ==""){
alert("对不起,区域名称不能为空!");
form1.textaread.focus();
return false;
}
return true;
}
</script>
<%
end sub

sub Modify()
dim AreaID,sql,rsClass,i
AreaID=trim(request("AreaID"))
if AreaID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,参数不足！');history.go(-1);</script>")
Response.end
else
AreaID=CLng(AreaID)
end if
sql="select * From SK_SystemArea where AreaID=" & AreaID
set rsClass=server.CreateObject ("Adodb.recordset")
rsClass.open sql,conn,1,3
if rsClass.bof and rsClass.eof then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,找不到指定的区域！');history.go(-1);</script>")
Response.end
else
%>
<form name="form1" method="post" action="SK.Area.asp" onSubmit="return check()">
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
    <tr  bgcolor=#efefef> 
      <td height="22" colspan="2" align="center"><strong>修改区域</strong></td>
    </tr>
    <tr > 
      <td width="35%"><strong>所属区域：</strong><br>
        如果你想改变所属区域，请<b><a href='?Action=Move&AreaID=<%=AreaID%>'>点此移动区域</a></b></td>
      <td width="65%"> <%
if rsClass("ParentID")<=0 then
  response.write "无(作为一级区域)"
else
    dim rsParentClass,sqlParentClass
sqlParentClass="Select * From SK_SystemArea where AreaID in (" & rsClass("ParentPath") & ") order by Depth"
set rsParentClass=server.CreateObject("adodb.recordset")
rsParentClass.open sqlParentClass,conn,1,1
do while not rsParentClass.eof
for i=1 to rsParentClass("Depth")
response.write "&nbsp;&nbsp;&nbsp;"
next
if rsParentClass("Depth")>0 then
response.write "└"
end if
response.write "&nbsp;" & rsParentClass("AreaName") & "<br>"
rsParentClass.movenext
loop
rsParentClass.close
set rsParentClass=Nothing
end if
%> </select></td>
    </tr>
    <tr > 
      <td><strong>区域名称：</strong></td>
      <td><input name="AreaName" type="text" value="<%=rsClass("AreaName")%>" size="37" maxlength="20"> 
        <input name="AreaID" type="hidden" id="AreaID" value="<%=rsClass("AreaID")%>"></td>
    </tr>

    <tr > 
      <td height="40" colspan="2" align="center"><input name="Action" type="hidden" id="Action" value="SaveModify"> 
<input name="Submit" type="submit" value=" 保存 " style="cursor:hand;" class="button1"> 
&nbsp; <input name="Cancel" type="button" id="Cancel" value=" 取 消 " onClick="window.location.href='SK.Area.asp'" style="cursor:hand;" class="button1"> 
</td>
</tr>
</table>
</form>
<script language="JavaScript" type="text/JavaScript">
function check()
{
  if (document.form1.AreaName.value=="")
  {
    alert("区域名称不能为空！");
document.form1.AreaName.focus();
return false;
  }
}
</script>
<%
end if
rsClass.close
set rsClass=Nothing
end sub

sub MoveClass()
dim AreaID,sql,rsClass,i
AreaID=trim(request("AreaID"))
if AreaID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,参数不足！');history.go(-1);</script>")
Response.end
exit sub
else
AreaID=CLng(AreaID)
end if

sql="select * From SK_SystemArea where AreaID=" & AreaID
set rsClass=server.CreateObject ("Adodb.recordset")
rsClass.open sql,conn,1,3
if rsClass.bof and rsClass.eof then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,找不到指定的区域！');history.go(-1);</script>")
Response.end
else
%>
<form name="form1" method="post" action="SK.Area.asp">
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
    <tr  bgcolor=#efefef> 
      <td height="22" colspan="2" align="center"><strong>移动区域</strong></td>
    </tr>
    <tr > 
      <td width="20%"><strong>区域名称：</strong></td>
      <td width="80%"><%=rsClass("AreaName")%> <input name="AreaID" type="hidden" id="AreaID" value="<%=rsClass("AreaID")%>"></td>
    </tr>
    <tr >
      <td><strong>当前所属区域：</strong></td>
      <td>
        <%
if rsClass("ParentID")<=0 then
  response.write "无(作为一级区域)"
else
    dim rsParent,sqlParent
sqlParent="Select * From SK_SystemArea where AreaID in (" & rsClass("ParentPath") & ") order by Depth"
set rsParent=server.CreateObject("adodb.recordset")
rsParent.open sqlParent,conn,1,1
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("AreaName")&"<br>"
rsParent.movenext
loop
rsParent.close
set rsParent=Nothing
end if
%>
      </td>
    </tr>
    <tr > 
      <td><strong>移动到：</strong><br>
        不能指定为当前区域的下属子区域<br>
        不能指定为外部区域</td>
<td><select name="ParentID" size="2" style="height:300px;width:500px;"><%call Admin_ShowClass_Option(0,rsClass("ParentID"))%></select></td>
    </tr>
    <tr > 
      <td height="40" colspan="2" align="center"><input name="Action" type="hidden" id="Action" value="SaveMove"> 
<input name="Submit" type="submit" value=" 保存 " style="cursor:hand;" class="button1">
&nbsp; 
<input name="Cancel" type="button" id="Cancel" value=" 取 消 " onClick="window.location.href='SK.Area.asp'" style="cursor:hand;" class="button1"></td></tr>
  </table>
</form>
<%
end if
rsClass.close
set rsClass=Nothing
end sub

sub Order() 
dim sqlClass,rsClass,i,iCount,j 
sqlClass="select * From SK_SystemArea where ParentID=0 order by RootID" 
set rsClass=server.CreateObject("adodb.recordset") 
rsClass.open sqlClass,conn,1,1 
iCount=rsClass.recordcount 
%>
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
<tr bgcolor=#efefef>  
<td height="22" colspan="4" align="center"><strong>一级区域排序</strong></td> 
</tr> 
<% 
j=1 
do while not rsClass.eof 
%> 
<tr onMouseOut="this.style.backgroundColor=''" onMouseOver="this.style.backgroundColor='#BFDFFF'">  
<td width="20%">&nbsp;<%=rsClass("AreaName")%></td> 
<% 
if j>1 then 
  response.write "<form action='SK.Area.asp?Action=UpOrder' method='post'><td width='150'>" 
response.write "<select name=MoveNum size=1><option value=0>向上移动</option>" 
for i=1 to j-1 
response.write "<option value="&i&">"&i&"</option>" 
next 
response.write "</select>" 
response.write "<input type=hidden name=AreaID value="&rsClass("AreaID")&">"
response.write "<input type=hidden name=cRootID value="&rsClass("RootID")&">&nbsp;<input type=submit name=Submit value=修改>" 
response.write "</td></form>" 
else 
response.write "<td width='150'>&nbsp;</td>" 
end if 
if iCount>j then 
  response.write "<form action='SK.Area.asp?Action=DownOrder' method='post'><td width='150'>" 
response.write "<select name=MoveNum size=1><option value=0>向下移动</option>" 
for i=1 to iCount-j 
response.write "<option value="&i&">"&i&"</option>" 
next 
response.write "</select>" 
response.write "<input type=hidden name=AreaID value="&rsClass("AreaID")&">"
response.write "<input type=hidden name=cRootID value="&rsClass("RootID")&">&nbsp;<input type=submit name=Submit value=修改>" 
response.write "</td></form>" 
else 
response.write "<td width='150'>&nbsp;</td>" 
end if 
%> 
<td width="80%">&nbsp;</td>
</tr> 
<% 
j=j+1 
rsClass.movenext 
loop 
%> 
</table>
<% 
rsClass.close 
set rsClass=Nothing 
end sub 

sub OrderN() 
dim sqlClass,rsClass,i,iCount,trs,UpMoveNum,DownMoveNum 
sqlClass="select * From SK_SystemArea order by RootID,OrderID" 
set rsClass=server.CreateObject("adodb.recordset") 
rsClass.open sqlClass,conn,1,1 
%>
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
<tr bgcolor=#efefef>  
<td height="22" colspan="4" align="center"><strong>N级区域排序</strong></td> 
</tr> 
<% 
do while not rsClass.eof 
%> 
<tr onMouseOut="this.style.backgroundColor=''" onMouseOver="this.style.backgroundColor='#BFDFFF'">  
<td width="20%"> 
<% 
for i=1 to rsClass("Depth") 
  response.write "&nbsp;&nbsp;&nbsp;" 
next 
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("ParentID")=0 then 
response.write "<b>" 
end if 
response.write rsClass("AreaName") 
if rsClass("Child")>0 then 
response.write "(" & rsClass("Child") & ")" 
end if 
%></td> 
<% 
if rsClass("ParentID")>0 then   '如果不是一级区域，则算出相同深度的区域数目，得到该区域在相同深度的区域中所处位置(之上或者之下的区域数) 
'所能提升最大幅度应为For i=1 to 该版之上的版面数 
set trs=conn.execute("select count(AreaID) From SK_SystemArea where ParentID="&rsClass("ParentID")&" and OrderID<"&rsClass("OrderID")&"") 
UpMoveNum=trs(0) 
if isnull(UpMoveNum) then UpMoveNum=0 
if UpMoveNum>0 then 
  response.write "<form action='SK.Area.asp?Action=UpOrderN' method='post'><td width='150'>" 
response.write "<select name=MoveNum size=1><option value=0>向上移动</option>" 
for i=1 to UpMoveNum 
response.write "<option value="&i&">"&i&"</option>" 
next 
response.write "</select>" 
response.write "<input type=hidden name=AreaID value="&rsClass("AreaID")&">&nbsp;<input type=submit name=Submit value=修改>" 
response.write "</td></form>" 
else 
response.write "<td width='150'>&nbsp;</td>" 
end if 
trs.close 
'所能降低最大幅度应为For i=1 to 该版之下的版面数 
set trs=conn.execute("select count(AreaID) From SK_SystemArea where ParentID="&rsClass("ParentID")&" and orderID>"&rsClass("orderID")&"") 
DownMoveNum=trs(0) 
if isnull(DownMoveNum) then DownMoveNum=0 
if DownMoveNum>0 then 
  response.write "<form action='SK.Area.asp?Action=DownOrderN' method='post'><td width='150'>" 
response.write "<select name=MoveNum size=1><option value=0>向下移动</option>" 
for i=1 to DownMoveNum 
response.write "<option value="&i&">"&i&"</option>" 
next 
response.write "</select>" 
response.write "<input type=hidden name=AreaID value="&rsClass("AreaID")&">&nbsp;<input type=submit name=Submit value=修改>" 
response.write "</td></form>" 
else 
response.write "<td width='150'>&nbsp;</td>" 
end if 
trs.close 
else 
response.write "<td colspan=2>&nbsp;</td>" 
end if 
%> 
<td width="80%">&nbsp;</td>
</tr> 
<% 
UpMoveNum=0 
DownMoveNum=0 
rsClass.movenext 
loop 
%> 
</table>
<% 
rsClass.close 
set rsClass=Nothing 
end sub 
sub Reset() 
%>
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
  <tr bgcolor=#efefef>  
    <td height="22" colspan="3" align="center"><strong>复位所有区域</strong></td> 
  </tr> 
    <tr >  
    <td align="center">  
      <form name="form1" method="post" action="?Action=SaveReset"> 
        <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
          <tr>  
            <td height="150"><font color="#FF0000"><strong>注意：</strong></font><br> 
              &nbsp;&nbsp;如果选择复位所有区域，则所有区域都将作为一级区域，这时您需要重新对各个区域进行归属的基本设置。不要轻易使用该功能，仅在做出了错误的设置而无法复原区域之间的关系和排序的时候使用。  
            </td> 
          </tr> 
        </table> 
        <input type="submit" name="Submit" value="复位" class="button1"> &nbsp; <input name="Cancel" type="button" id="Cancel" value=" 取 消 " onClick="window.location.href='SK.Area.asp'" style="cursor:hand;" class="button1">
      </form></td>
    </tr>
</table>
<%
end sub

sub Unite()
%>
<br>
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
  <tr  bgcolor=#efefef> 
    <td height="22" colspan="3" align="center"><strong>区域合并</strong></td>
  </tr>
  <tr > 
    <td height="100"><form name="myform" method="post" action="" onSubmit="return ConfirmUnite();">
        &nbsp;&nbsp;将区域 
        <select name="AreaID" id="AreaID">
        <%call Admin_ShowClass_Option(1,0)%>
        </select>
        合并到
        <select name="TargetAreaID" id="TargetAreaID">
        <%call Admin_ShowClass_Option(1,0)%>
        </select>
        <br> <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        <input name="Action" type="hidden" id="Action" value="SaveUnite">
        <input type="submit" name="Submit" value=" 合并 " style="cursor:hand;" class="button1">
        &nbsp;&nbsp; 
        <input name="Cancel" type="button" id="Cancel" value=" 取 消 " onClick="window.location.href='SK.Area.asp'" style="cursor:hand;" class="button1">
      </form>
</td>
  </tr>
  <tr > 
    <td height="60"><strong>注意事项：</strong><br>
      &nbsp;&nbsp;所有操作不可逆，请慎重操作！！！<br>
      &nbsp;&nbsp;不能在同一个区域内进行操作，不能将一个区域合并到其下属区域中。目标区域中不能含有子区域。<br>
      &nbsp;&nbsp;合并后您所指定的区域(或者包括其下属区域)将被删除，所有将转移到目标区域中。</td>
  </tr>
</table> 
<script language="JavaScript" type="text/JavaScript">
function ConfirmUnite()
{
  if (document.myform.AreaID.value==document.myform.TargetAreaID.value)
  {
    alert("请不要在相同区域内进行操作！");
document.myform.TargetAreaID.focus();
return false;
  }
  if (document.myform.TargetAreaID.value=="")
  {
    alert("目标区域不能指定为含有子区域的区域！");
document.myform.TargetAreaID.focus();
return false;
  }
}
</script>
<% 
end sub 
%> 
<a name="end"></a>
</td>
<td width="8" background="../images/tab_15.gif"></td>
</tr>
</table></td>
</tr>
<tr>
<td class="SK_Td_Foot">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="1%" height="20"><img src="../images/tab_18.gif" width="12" height="20" /></td>
<td width="98%" class="SK_Td_Page"></td>
<td width="1%"><img src="../images/tab_20.gif" width="16" height="20" /></td>
</tr>
</table>

</td>
</tr>
</TBODY>
</table>
</body>
<%Call CloseDB()%>
</html>
<% 
sub SaveAdd()
dim sql,rs,trs
dim RootID,ParentDepth,ParentPath,ParentStr,ParentName,MaxAreaID,MaxRootID
dim PrevID,NextID,Child

if AreaName="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,区域名称不能为空！');history.go(-1);</script>")
response.End()

end if
set rs = conn.execute("select Max(AreaID) From SK_SystemArea")
MaxAreaID=rs(0)
if isnull(MaxAreaID) then
MaxAreaID=0
end if
rs.close
AreaID=MaxAreaID+1
set rs=conn.execute("select max(rootid) From SK_SystemArea")
MaxRootID=rs(0)
if isnull(MaxRootID) then
MaxRootID=0
end if
rs.close
RootID=MaxRootID+1

if ParentID>0 then
sql="select * From SK_SystemArea where AreaID="&ParentID&""
rs.open sql,conn,1,1
if rs.bof and rs.eof then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,所属区域已经被删除！');history.go(-1);</script>")
Response.end
end if
if FoundErr=True then
rs.close
set rs=Nothing
exit sub
else
RootID=rs("RootID")
ParentName=rs("AreaName")
ParentDepth=rs("Depth")
ParentPath=rs("ParentPath")
Child=rs("Child")
ParentPath=ParentPath & "," & ParentID     '得到此区域的父级区域路径
PrevOrderID=rs("OrderID")
if Child>0 then
dim rsPrevOrderID
'得到与本区域同级的最后一个区域的OrderID
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_SystemArea where ParentID="&ParentID)
PrevOrderID=rsPrevOrderID(0)
set trs=conn.execute("select AreaID From SK_SystemArea where ParentID="&ParentID&" and OrderID="&PrevOrderID)
PrevID=trs(0)

'得到同一父区域但比本区域级数大的子区域的最大OrderID，如果比前一个值大，则改用这个值。
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_SystemArea 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=conn.execute("select AreaID From SK_SystemArea 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 SK_SystemArea Where ParentID="&ParentID&" AND AreaName='"&AreaName&"'"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
if not(rs.bof and rs.eof) then
FoundErr=True
if ParentID=0 then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,已经存在一级区域："&AreaName&"！');history.go(-1);</script>")
Response.end
else
Response.Write("<script LANGUAGE='javascript'>alert('对不起," & ParentName & "中已经存在"&AreaName&"！');history.go(-1);</script>")
Response.end
end if
rs.close
set rs=Nothing
exit sub
end if
rs.close

sql="Select top 1 * From SK_SystemArea"
rs.open sql,conn,1,3
    rs.addnew
rs("AreaID")=AreaID
   rs("AreaName")=AreaName
rs("RootID")=RootID
rs("ParentID")=ParentID
if ParentID>0 then
rs("Depth")=ParentDepth+1
else
rs("Depth")=0
end if
rs("ParentPath")=ParentPath
rs("OrderID")=PrevOrderID
rs("Child")=0
rs("PrevID")=PrevID
rs("NextID")=0
rs.update
rs.Close
if PrevID>0 then
conn.execute("update SK_SystemArea set NextID=" & AreaID & " where AreaID=" & PrevID)
end if

if ParentID>0 then
'更新其父类的子区域数
conn.execute("update SK_SystemArea set child=child+1 where AreaID="&ParentID)

'更新该区域排序以及大于本需要和同在本区域下的区域排序序号
conn.execute("update SK_SystemArea set OrderID=OrderID+1 where rootid=" & rootid & " and OrderID>" & PrevOrderID)
conn.execute("update SK_SystemArea set OrderID=" & PrevOrderID & "+1 where AreaID=" & AreaID)
end if
end sub

sub SaveModify()
dim AreaName
dim trs,rs
dim AreaID,sql,rsClass,i
dim SkinCount,LayoutCount
AreaID=trim(request("AreaID"))
if AreaID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,参数不足！');history.go(-1);</script>")
Response.end
else
AreaID=CLng(AreaID)
end if
AreaName=trim(request("AreaName"))
if AreaName="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,区域名称不能为空！');history.go(-1);</script>")
Response.end
end if

if FoundErr=True then
exit sub
end if

sql="select * From SK_SystemArea where AreaID=" & AreaID
set rsClass=server.CreateObject ("Adodb.recordset")
rsClass.open sql,conn,1,3
if rsClass.bof and rsClass.eof then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,找不到指定的区域！');history.go(-1);</script>")
Response.end
rsClass.close
set rsClass=Nothing
exit sub
end if
rsClass("AreaName")=AreaName
rsClass.update
rsClass.close
set rsClass=Nothing

set rs=Nothing
set trs=Nothing
Response.Redirect "SK.Area.asp"  
end sub


sub DeleteClass()
dim sql,rs,PrevID,NextID,AreaID
AreaID=F("AreaID")
if AreaID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,参数不足！');history.go(-1);</script>")
Response.end
exit sub
else
AreaID=CLng(AreaID)
end if

sql="select AreaID,RootID,Depth,ParentID,Child,PrevID,NextID From SK_SystemArea where AreaID="&AreaID
set rs=server.CreateObject ("Adodb.recordset")
rs.open sql,conn,1,3
if rs.bof and rs.eof then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,区域不存在，或者已经被删除！');history.go(-1);</script>")
Response.end
else
if rs("Child")>0 then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,该区域含有子区域，请删除其子区域后再进行删除本区域的操作！');history.go(-1);</script>")
Response.end

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
conn.execute("update SK_SystemArea set child=child-1 where AreaID="&rs("ParentID"))
end if
rs.delete
rs.update
rs.close
set rs=Nothing

'删除本区域的所有内容
'conn.execute("delete from product where AreaID=" & AreaID)
'conn.execute("delete from productComment where AreaID=" & AreaID)
Response.Write("<script LANGUAGE='javascript'>alert('恭喜你，删除成功！');history.go(-1);</script>")
Response.end

'修改上一区域的NextID和下一区域的PrevID
if PrevID>0 then
conn.execute "update SK_SystemArea set NextID="&NextID&" where AreaID="&PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemArea set PrevID="&PrevID&" where AreaID="&NextID
end if
call CloseConn()
response.redirect "SK.Area.asp"

end sub

sub ClearClass()
dim strAreaID,rs,trs,SuccessMsg,AreaID
AreaID=trim(Request("AreaID"))
if AreaID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,参数不足！');history.go(-1);</script>")
Response.end
exit sub
else
AreaID=CLng(AreaID)
end if
strAreaID=cstr(AreaID)
set rs=conn.execute("select AreaID,Child,ParentPath From SK_SystemArea where AreaID="&AreaID)
if rs.bof and rs.eof then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,区域不存在，或者已经被删除！');history.go(-1);</script>")
Response.end
exit sub
end if
if rs(1)>0 then
set trs=conn.execute("select AreaID From SK_SystemArea where ParentID="&rs(0))
do while not trs.eof
strAreaID=strAreaID&","&trs(0)
trs.movenext
loop
trs.close
set trs=conn.execute("select AreaID From SK_SystemArea where ParentPath like '"&rs(2)&","&rs(0)&",%'")
do while not trs.eof
strAreaID=strAreaID&","&trs(0)
trs.movenext
loop
trs.close
set trs=Nothing
end if
rs.close
set rs=Nothing
'conn.execute("update SK_Recycle set Deleted=True where ID in (" & strAreaID & ")")
'conn.execute("delete from SK_Recycle where ID in (" & strAreaID & ")")
SuccessMsg="此区域(包括子区域)的所有文章已经被移到回收站中！"
call WriteSuccessMsg(SuccessMsg)
end sub

sub SaveMove()
dim AreaID,sql,rsClass,i
dim rParentID
dim trs,rs
dim ParentID,RootID,Depth,Child,ParentPath,ParentName,iParentID,iParentPath,PrevOrderID,PrevID,NextID
AreaID=trim(request("AreaID"))
if AreaID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,参数不足除！');history.go(-1);</script>")
Response.end
exit sub
else
AreaID=CLng(AreaID)
end if

sql="select * From SK_SystemArea where AreaID="&AreaID
set rsClass=server.CreateObject ("Adodb.recordset")
rsClass.open sql,conn,1,3
if rsClass.bof and rsClass.eof then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,找不到指定的区域！');history.go(-1);</script>")
Response.end
rsClass.close
set rsClass=Nothing
exit sub
end if

rParentID=trim(request("ParentID"))
if rParentID="" then
rParentID=0
else
rParentID=CLng(rParentID)
end if

if rsClass("ParentID")<>rParentID then   '更改了所属区域，则要做一系列检查
if rParentID=rsClass("AreaID") then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,所属区域不能为自己！');history.go(-1);</script>")
Response.end
end if
'判断所指定的区域是否为外部区域或本区域的下属区域
if rsClass("ParentID")=0 then
if rParentID>0 then
set trs=conn.execute("select rootid From SK_SystemArea where AreaID="&rParentID)
if trs.bof and trs.eof then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,不能指定外部区域为所属区域！');history.go(-1);</script>")
Response.end
else
if rsClass("rootid")=trs(0) then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,不能指定该区域的下属区域作为所属区域！');history.go(-1);</script>")
Response.end
end if
end if
trs.close
set trs=Nothing
end if
else
set trs=conn.execute("select AreaID From SK_SystemArea where ParentPath like '"&rsClass("ParentPath")&","&rsClass("AreaID")&"%' and AreaID="&rParentID)
if not (trs.eof and trs.bof) then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,不能指定该区域的下属区域作为所属区域！');history.go(-1);</script>")
Response.end
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("AreaID")
iParentID=0
else
ParentID=rsClass("ParentID")
iParentID=rsClass("ParentID")
end if
Depth=rsClass("Depth")
Child=rsClass("Child")
RootID=rsClass("RootID")
ParentPath=rsClass("ParentPath")
PrevID=rsClass("PrevID")
NextID=rsClass("NextID")
rsClass.close
set rsClass=Nothing

'假如更改了所属区域
'需要更新其原来所属区域信息，包括深度、父级ID、区域数、排序、继承版主等数据
'需要更新当前所属区域信息
'继承版主数据需要另写函数进行更新--取消，在前台可用AreaID in ParentPath来获得

dim mrs,MaxRootID
set mrs=conn.execute("select max(rootid) From SK_SystemArea")
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
conn.execute "update SK_SystemArea set NextID=" & NextID & " where AreaID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemArea set PrevID=" & PrevID & " where AreaID=" & NextID
end if

if iParentID>0 and rParentID=0 then  '如果原来不是一级区域改成一级区域
'得到上一个一级区域区域
sql="select AreaID,NextID From SK_SystemArea where RootID=" & MaxRootID & " and Depth=0"
set rs=server.CreateObject("Adodb.recordset")
rs.open sql,conn,1,3
PrevID=rs(0)      '得到新的PrevID
rs(1)=AreaID     '更新上一个一级区域区域的NextID的值
rs.update
rs.close
set rs=Nothing

MaxRootID=MaxRootID+1
'更新当前区域数据
conn.execute("update SK_SystemArea set depth=0,OrderID=0,rootid="&maxrootid&",parentid=0,ParentPath='0',PrevID=" & PrevID & ",NextID=0 where AreaID="&AreaID)
'如果有下属区域，则更新其下属区域数据。下属区域的排序不需考虑，只需更新下属区域深度和一级排序ID(rootid)数据
if child>0 then
i=0
ParentPath=ParentPath & ","
set rs=conn.execute("select * From SK_SystemArea where ParentPath like '%"&ParentPath & AreaID&"%'")
do while not rs.eof
i=i+1
mParentPath=replace(rs("ParentPath"),ParentPath,"")
conn.execute("update SK_SystemArea set depth=depth-"&depth&",rootid="&maxrootid&",ParentPath='"&mParentPath&"' where AreaID="&rs("AreaID"))
rs.movenext
loop
rs.close
set rs=Nothing
end if

'更新其原来所属区域的区域数，排序相当于剪枝而不需考虑
conn.execute("update SK_SystemArea set child=child-1 where AreaID="&iParentID)

elseif iParentID>0 and rParentID>0 then    '如果是将一个分区域移动到其他分区域下
'得到当前区域的下属子区域数
ParentPath=ParentPath & ","
set rs=conn.execute("select count(*) From SK_SystemArea where ParentPath like '%"&ParentPath & AreaID&"%'")
ClassCount=rs(0)
if isnull(ClassCount) then
ClassCount=1
end if
rs.close
set rs=Nothing

'获得目标区域的相关信息
set trs=conn.execute("select * From SK_SystemArea where AreaID="&rParentID)
if trs("Child")>0 then
'得到与本区域同级的最后一个区域的OrderID
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_SystemArea where ParentID=" & trs("AreaID"))
PrevOrderID=rsPrevOrderID(0)
'得到与本区域同级的最后一个区域的AreaID
sql="select AreaID,NextID From SK_SystemArea where ParentID=" & trs("AreaID") & " and OrderID=" & PrevOrderID
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,3
PrevID=rs(0)    '得到新的PrevID
rs(1)=AreaID     '更新上一个区域的NextID的值
rs.update
rs.close
set rs=Nothing

'得到同一父区域但比本区域级数大的子区域的最大OrderID，如果比前一个值大，则改用这个值。
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_SystemArea where ParentPath like '" & trs("ParentPath") & "," & trs("AreaID") & ",%'")
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

'在获得移动过来的区域数后更新排序在指定区域之后的区域排序数据
conn.execute("update SK_SystemArea set OrderID=OrderID+" & ClassCount & "+1 where rootid=" & trs("rootid") & " and OrderID>" & PrevOrderID)

'更新当前区域数据
conn.execute("update SK_SystemArea set depth="&trs("depth")&"+1,OrderID="&PrevOrderID&"+1,rootid="&trs("rootid")&",ParentID="&rParentID&",ParentPath='" & trs("ParentPath") & "," & trs("AreaID") & "',PrevID=" & PrevID & ",NextID=0 where AreaID="&AreaID)

'如果有子区域则更新子区域数据，深度为原来的相对深度加上当前所属区域的深度
set rs=conn.execute("select * From SK_SystemArea where ParentPath like '%"&ParentPath&AreaID&"%' order by OrderID")
i=1
do while not rs.eof
i=i+1
iParentPath=trs("ParentPath") & "," & trs("AreaID") & "," & replace(rs("ParentPath"),ParentPath,"")
conn.execute("update SK_SystemArea set depth=depth-"&depth&"+"&trs("depth")&"+1,OrderID="&PrevOrderID&"+"&i&",rootid="&trs("rootid")&",ParentPath='"&iParentPath&"' where AreaID="&rs("AreaID"))
rs.movenext
loop
rs.close
set rs=Nothing
trs.close
set trs=Nothing

'更新所指向的上级区域的子区域数
conn.execute("update SK_SystemArea set child=child+1 where AreaID="&rParentID)

'更新其原父类的子区域数
conn.execute("update SK_SystemArea set child=child-1 where AreaID="&iParentID)
else    '如果原来是一级区域改成其他区域的下属区域
'得到移动的区域总数
set rs=conn.execute("select count(*) From SK_SystemArea where rootid="&rootid)
ClassCount=rs(0)
rs.close
set rs=Nothing

'获得目标区域的相关信息
set trs=conn.execute("select * From SK_SystemArea where AreaID="&rParentID)
if trs("Child")>0 then
'得到与本区域同级的最后一个区域的OrderID
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_SystemArea where ParentID=" & trs("AreaID"))
PrevOrderID=rsPrevOrderID(0)
sql="select AreaID,NextID From SK_SystemArea where ParentID=" & trs("AreaID") & " and OrderID=" & PrevOrderID
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,3
PrevID=rs(0)
rs(1)=AreaID
rs.update
set rs=Nothing

'得到同一父区域但比本区域级数大的子区域的最大OrderID，如果比前一个值大，则改用这个值。
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_SystemArea where ParentPath like '" & trs("ParentPath") & "," & trs("AreaID") & ",%'")
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

'在获得移动过来的区域数后更新排序在指定区域之后的区域排序数据
conn.execute("update SK_SystemArea set OrderID=OrderID+" & ClassCount &"+1 where rootid=" & trs("rootid") & " and OrderID>" & PrevOrderID)

conn.execute("update SK_SystemArea set PrevID=" & PrevID & ",NextID=0 where AreaID=" & AreaID)
set rs=conn.execute("select * From SK_SystemArea 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("AreaID")
conn.execute("update SK_SystemArea set depth=depth+"&trs("depth")&"+1,OrderID="&PrevOrderID&"+"&i&",rootid="&trs("rootid")&",ParentPath='"&ParentPath&"',parentid="&rParentID&" where AreaID="&rs("AreaID"))
else
ParentPath=trs("ParentPath") & "," & trs("AreaID") & "," & replace(rs("ParentPath"),"0,","")
conn.execute("update SK_SystemArea set depth=depth+"&trs("depth")&"+1,OrderID="&PrevOrderID&"+"&i&",rootid="&trs("rootid")&",ParentPath='"&ParentPath&"' where AreaID="&rs("AreaID"))
end if
rs.movenext
loop
rs.close
set rs=Nothing
trs.close
set trs=Nothing
'更新所指向的上级区域区域数
conn.execute("update SK_SystemArea set child=child+1 where AreaID="&rParentID)

end if
  end if

  call CloseConn()
  Response.Redirect "SK.Area.asp"  
end sub

sub UpOrder()
dim AreaID,sqlOrder,rsOrder,MoveNum,cRootID,tRootID,i,rs,PrevID,NextID
AreaID=trim(request("AreaID"))
cRootID=Trim(request("cRootID"))
MoveNum=trim(request("MoveNum"))
if AreaID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,参数不足！');history.go(-1);</script>")
Response.end
else
AreaID=CLng(AreaID)
end if
if cRootID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,错误参数！');history.go(-1);</script>")
Response.end
else
cRootID=Cint(cRootID)
end if
if MoveNum="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,错误参数！');history.go(-1);</script>")
Response.end
else
MoveNum=Cint(MoveNum)
if MoveNum=0 then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,请选择要提升的数字！');history.go(-1);</script>")
Response.end
end if
end if
if FoundErr=True then
exit sub
end if

'得到本区域的PrevID,NextID
set rs=conn.execute("select PrevID,NextID From SK_SystemArea where AreaID="&AreaID)
PrevID=rs(0)
NextID=rs(1)
rs.close
set rs=Nothing
'先修改上一区域的NextID和下一区域的PrevID
if PrevID>0 then
conn.execute "update SK_SystemArea set NextID="&NextID&" where AreaID="&PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemArea set PrevID="&PrevID&" where AreaID="&NextID
end if

dim mrs,MaxRootID
set mrs=conn.execute("select max(rootid) From SK_SystemArea")
MaxRootID=mrs(0)+1
'先将当前区域移至最后，包括子区域
conn.execute("update SK_SystemArea set RootID="&MaxRootID&" where RootID=" & cRootID)

'然后将位于当前区域以上的区域的RootID依次加一，为要提升的数字
sqlOrder="select * From SK_SystemArea where ParentID=0 and RootID<"&cRootID&" order by RootID desc"
set rsOrder=server.CreateObject("adodb.recordset")
rsOrder.open sqlOrder,conn,1,3
if rsOrder.bof and rsOrder.eof then
exit sub        '如果当前区域已经在最上面，则无需移动
end if
i=1
do while not rsOrder.eof
tRootID=rsOrder("RootID")       '得到要提升位置的RootID，包括子区域
conn.execute("update SK_SystemArea set RootID=RootID+1 where RootID="&tRootID)
i=i+1
if i>MoveNum then
rsOrder("PrevID")=AreaID
rsOrder.update
conn.execute("update SK_SystemArea set NextID="&rsOrder("AreaID")&" where AreaID="&AreaID)
exit do
end if
rsOrder.movenext
loop
rsOrder.movenext
if rsOrder.eof then
conn.execute("update SK_SystemArea set PrevID=0 where AreaID="&AreaID)
else
rsOrder("NextID")=AreaID
rsOrder.update
conn.execute("update SK_SystemArea set PrevID="&rsOrder("AreaID")&" where AreaID="&AreaID)
end if
rsOrder.close
set rsOrder=Nothing

'然后再将当前区域从最后移到相应位置，包括子区域
conn.execute("update SK_SystemArea set RootID="&tRootID&" where RootID="&MaxRootID)
call CloseConn()
response.Redirect "SK.Area.asp?Action=Order"
end sub

sub DownOrder()
dim AreaID,sqlOrder,rsOrder,MoveNum,cRootID,tRootID,i,rs,PrevID,NextID
AreaID=trim(request("AreaID"))
cRootID=Trim(request("cRootID"))
MoveNum=trim(request("MoveNum"))
if AreaID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,参数不足！');history.go(-1);</script>")
Response.end
else
AreaID=CLng(AreaID)
end if
if cRootID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,错误参数！');history.go(-1);</script>")
Response.end
else
cRootID=Cint(cRootID)
end if
if MoveNum="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,错误参数！');history.go(-1);</script>")
Response.end
else
MoveNum=Cint(MoveNum)
if MoveNum=0 then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,请选择要提升的数字！');history.go(-1);</script>")
Response.end
end if
end if
if FoundErr=True then
exit sub
end if

'得到本区域的PrevID,NextID
set rs=conn.execute("select PrevID,NextID From SK_SystemArea where AreaID="&AreaID)
PrevID=rs(0)
NextID=rs(1)
rs.close
set rs=Nothing
'先修改上一区域的NextID和下一区域的PrevID
if PrevID>0 then
conn.execute "update SK_SystemArea set NextID="&NextID&" where AreaID="&PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemArea set PrevID="&PrevID&" where AreaID="&NextID
end if

dim mrs,MaxRootID
set mrs=conn.execute("select max(rootid) From SK_SystemArea")
MaxRootID=mrs(0)+1
'先将当前区域移至最后，包括子区域
conn.execute("update SK_SystemArea set RootID="&MaxRootID&" where RootID="&cRootID)

'然后将位于当前区域以下的区域的RootID依次减一，为要下降的数字
sqlOrder="select * From SK_SystemArea where ParentID=0 and RootID>"&cRootID&" order by RootID"
set rsOrder=server.CreateObject("adodb.recordset")
rsOrder.open sqlOrder,conn,1,3
if rsOrder.bof and rsOrder.eof then
exit sub        '如果当前区域已经在最下面，则无需移动
end if
i=1
do while not rsOrder.eof
tRootID=rsOrder("RootID")       '得到要提升位置的RootID，包括子区域
conn.execute("update SK_SystemArea set RootID=RootID-1 where RootID="&tRootID)
i=i+1
if i>MoveNum then
rsOrder("NextID")=AreaID
rsOrder.update
conn.execute("update SK_SystemArea set PrevID="&rsOrder("AreaID")&" where AreaID="&AreaID)
exit do
end if
rsOrder.movenext
loop
rsOrder.movenext
if rsOrder.eof then
conn.execute("update SK_SystemArea set NextID=0 where AreaID="&AreaID)
else
rsOrder("PrevID")=AreaID
rsOrder.update
conn.execute("update SK_SystemArea set NextID="&rsOrder("AreaID")&" where AreaID="&AreaID)
end if
rsOrder.close
set rsOrder=Nothing

'然后再将当前区域从最后移到相应位置，包括子区域
conn.execute("update SK_SystemArea set RootID="&tRootID&" where RootID="&MaxRootID)
call CloseConn()
response.Redirect "SK.Area.asp?Action=Order"
end sub

sub UpOrderN()
dim sqlOrder,rsOrder,MoveNum,AreaID,i
dim ParentID,OrderID,ParentPath,Child,PrevID,NextID
AreaID=Trim(request("AreaID"))
MoveNum=trim(request("MoveNum"))
if AreaID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,错误参数！');history.go(-1);</script>")
Response.end
else
AreaID=CLng(AreaID)
end if
if MoveNum="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,错误参数！');history.go(-1);</script>")
Response.end
else
MoveNum=Cint(MoveNum)
if MoveNum=0 then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,请选择要提升的数字！');history.go(-1);</script>")
Response.end
end if
end if
if FoundErr=True then
exit sub
end if

dim sql,rs,oldorders,ii,trs,tOrderID
'要移动的区域信息
set rs=conn.execute("select ParentID,OrderID,ParentPath,child,PrevID,NextID From SK_SystemArea where AreaID="&AreaID)
ParentID=rs(0)
OrderID=rs(1)
ParentPath=rs(2) & "," & AreaID
child=rs(3)
PrevID=rs(4)
NextID=rs(5)
rs.close
set rs=Nothing
if child>0 then
set rs=conn.execute("select count(*) From SK_SystemArea where ParentPath like '%"&ParentPath&"%'")
oldorders=rs(0)
rs.close
set rs=Nothing
else
oldorders=0
end if
'先修改上一区域的NextID和下一区域的PrevID
if PrevID>0 then
conn.execute "update SK_SystemArea set NextID="&NextID&" where AreaID="&PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemArea set PrevID="&PrevID&" where AreaID="&NextID
end if

'和该区域同级且排序在其之上的区域------更新其排序，为要提升的数字
sql="select AreaID,OrderID,child,ParentPath,PrevID,NextID From SK_SystemArea where ParentID="&ParentID&" and OrderID<"&OrderID&" order by OrderID desc"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,3
i=1
do while not rs.eof
tOrderID=rs(1)
conn.execute("update SK_SystemArea set OrderID="&tOrderID+oldorders+i&" where AreaID="&rs(0))
if rs(2)>0 then
ii=i+1
set trs=conn.execute("select AreaID,OrderID From SK_SystemArea where ParentPath like '%"&rs(3)&","&rs(0)&"%' order by OrderID")
if not (trs.eof and trs.bof) then
do while not trs.eof
conn.execute("update SK_SystemArea set OrderID="&tOrderID+oldorders+ii&" where AreaID="&trs(0))
ii=ii+1
trs.movenext
loop
end if
trs.close
set trs=Nothing
end if
i=i+1
if i>MoveNum then
rs(4)=AreaID
rs.update
conn.execute("update SK_SystemArea set NextID="&rs(0)& " where AreaID="&AreaID)
exit do
end if
rs.movenext
loop
rs.movenext
if rs.eof then
conn.execute("update SK_SystemArea set PrevID=0 where AreaID="&AreaID)
else
rs(5)=AreaID
rs.update
conn.execute("update SK_SystemArea set PrevID="&rs(0)&" where AreaID="&AreaID)
end if
rs.close
set rs=Nothing

'更新所要排序的区域的序号
conn.execute("update SK_SystemArea set OrderID="&tOrderID&" where AreaID="&AreaID)
'如果有下属区域，则更新其下属区域排序
if child>0 then
i=1
set rs=conn.execute("select AreaID From SK_SystemArea where ParentPath like '%"&ParentPath&"%' order by OrderID")
do while not rs.eof
conn.execute("update SK_SystemArea set OrderID="&tOrderID+i&" where AreaID="&rs(0))
i=i+1
rs.movenext
loop
rs.close
set rs=Nothing
end if
call CloseConn()
response.Redirect "SK.Area.asp?Action=OrderN"
end sub

sub DownOrderN()
dim sqlOrder,rsOrder,MoveNum,AreaID,i
dim ParentID,OrderID,ParentPath,Child,PrevID,NextID
AreaID=Trim(request("AreaID"))
MoveNum=trim(request("MoveNum"))
if AreaID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,错误参数！');history.go(-1);</script>")
Response.end
exit sub
else
AreaID=Cint(AreaID)
end if
if MoveNum="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,错误参数！');history.go(-1);</script>")
Response.end
exit sub
else
MoveNum=Cint(MoveNum)
if MoveNum=0 then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,请选择要下降的数字！');history.go(-1);</script>")
Response.end
exit sub
end if
end if

dim sql,rs,oldorders,ii,trs,tOrderID
'要移动的区域信息
set rs=conn.execute("select ParentID,OrderID,ParentPath,child,PrevID,NextID From SK_SystemArea where AreaID="&AreaID)
ParentID=rs(0)
OrderID=rs(1)
ParentPath=rs(2) &","&AreaID
child=rs(3)
PrevID=rs(4)
NextID=rs(5)
rs.close
set rs=Nothing

'先修改上一区域的NextID和下一区域的PrevID
if PrevID>0 then
conn.execute "update SK_SystemArea set NextID="&NextID&" where AreaID="&PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemArea set PrevID="&PrevID&" where AreaID="&NextID
end if

'和该区域同级且排序在其之下的区域------更新其排序，为要下降的数字
sql="select AreaID,OrderID,child,ParentPath,PrevID,NextID From SK_SystemArea where ParentID="&ParentID&" and OrderID>"&OrderID&" order by OrderID"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,3
i=0      '同级区域
ii=0     '同级区域和子区域
do while not rs.eof
conn.execute("update SK_SystemArea set OrderID="&OrderID+ii&" where AreaID="&rs(0))
if rs(2)>0 then
set trs=conn.execute("select AreaID,OrderID From SK_SystemArea where ParentPath like '%"&rs(3)&","&rs(0)&"%' order by OrderID")
if not (trs.eof and trs.bof) then
do while not trs.eof
ii=ii+1
conn.execute("update SK_SystemArea set OrderID="&OrderID+ii&" where AreaID="&trs(0))
trs.movenext
loop
end if
trs.close
set trs=Nothing
end if
ii=ii+1
i=i+1
if i>=MoveNum then
rs(5)=AreaID
rs.update
conn.execute("update SK_SystemArea set PrevID="&rs(0)&" where AreaID="&AreaID)
exit do
end if
rs.movenext
loop
rs.movenext
if rs.eof then
conn.execute("update SK_SystemArea set NextID=0 where AreaID="&AreaID)
else
rs(4)=AreaID
rs.update
conn.execute("update SK_SystemArea set NextID="&rs(0)&" where AreaID=" & AreaID)
end if
rs.close
set rs=Nothing

'更新所要排序的区域的序号
conn.execute("update SK_SystemArea set OrderID="&OrderID+ii&" where AreaID="&AreaID)
'如果有下属区域，则更新其下属区域排序
if child>0 then
i=1
set rs=conn.execute("select AreaID From SK_SystemArea where ParentPath like '%"&ParentPath&"%' order by OrderID")
do while not rs.eof
conn.execute("update SK_SystemArea set OrderID="&OrderID+ii+i&" where AreaID="&rs(0))
i=i+1
rs.movenext
loop
rs.close
set rs=Nothing
end if
call CloseConn()
response.Redirect "SK.Area.asp?Action=OrderN"
end sub

sub SaveReset()
dim i,sql,rs,SuccessMsg,iCount,PrevID,NextID
sql="select AreaID From SK_SystemArea order by RootID,OrderID"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
iCount=rs.recordcount
i=1
PrevID=0
do while not rs.eof
rs.movenext
if rs.eof then
NextID=0
else
NextID=rs(0)
end if
rs.moveprevious
conn.execute("update SK_SystemArea set RootID="&i&",OrderID=0,ParentID=0,Child=0,ParentPath='0',Depth=0,PrevID="&PrevID&",NextID="&NextID&" where AreaID="&rs(0))
PrevID=rs(0)
i=i+1
rs.movenext
loop
rs.close
set rs=Nothing

SuccessMsg="复位成功！请返回<a href=''>区域管理首页</a>做区域的归属设置。"
call WriteSuccessMsg(SuccessMsg)
end sub

sub SaveUnite()
dim AreaID,TargetAreaID,ParentPath,iParentPath,Depth,iParentID,Child,PrevID,NextID
dim rs,trs,i,SuccessMsg
AreaID=trim(request("AreaID"))
TargetAreaID=trim(request("TargetAreaID"))
if AreaID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,请指定要合并的区域！');history.go(-1);</script>")
Response.end
else
AreaID=CLng(AreaID)
end if
if TargetAreaID="" then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,请指定目标区域！');history.go(-1);</script>")
Response.end
else
TargetAreaID=CLng(TargetAreaID)
end if
if AreaID=TargetAreaID then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,请不要在相同区域内进行操作！');history.go(-1);</script>")
Response.end
end if
if FoundErr=True then
exit sub
end if
'判断目标区域是否有子区域，如果有，则报错。
set rs=conn.execute("select Child From SK_SystemArea where AreaID=" & TargetAreaID)
if rs.bof and rs.eof then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,请指定目标区域！');history.go(-1);</script>")
Response.end
else
if rs(0)>0 then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,目标区域中含有子区域，不能合并！');history.go(-1);</script>")
Response.end
end if
end if
if FoundErr=True then
exit sub
end if

'得到当前区域信息
set rs=conn.execute("select AreaID,ParentID,ParentPath,PrevID,NextID,Depth From SK_SystemArea where AreaID="&AreaID)
iParentID=rs(1)
Depth=rs(5)
if iParentID=0 then
ParentPath=rs(0)
else
ParentPath=rs(2) & "," & rs(0)
end if
iParentPath=rs(0)
PrevID=rs(3)
NextID=rs(4)

'判断是否是合并到其下属区域中
set rs=conn.execute("select AreaID From SK_SystemArea where AreaID="&TargetAreaID&" and ParentPath like '"&ParentPath&"%'")
if not (rs.eof and rs.bof) then
Response.Write("<script LANGUAGE='javascript'>alert('对不起,不能将一个区域合并到其下属子区域中！');history.go(-1);</script>")
Response.end
exit sub
end if

'得到当前区域的下属区域ID
set rs=conn.execute("select AreaID From SK_SystemArea where ParentPath like '"&ParentPath&"%'")
i=0
if not (rs.eof and rs.bof) then
do while not rs.eof
iParentPath=iParentPath & "," & rs(0)
i=i+1
rs.movenext
loop
end if
if i>0 then
ParentPath=iParentPath
else
ParentPath=AreaID
end if

'先修改上一区域的NextID和下一区域的PrevID
if PrevID>0 then
conn.execute "update SK_SystemArea set NextID="&NextID&" where AreaID="&PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemArea set PrevID="&PrevID&" where AreaID="&NextID
end if

'更新文章及评论所属区域
'conn.execute("update Article set AreaID="&TargetAreaID&" where AreaID in ("&ParentPath&")")
'conn.execute("update ArticleComment set AreaID="&TargetAreaID&" where AreaID in ("&ParentPath&")")

'删除被合并区域及其下属区域
'conn.execute("delete From SK_SystemArea where AreaID in ("&ParentPath&")")

'更新其原来所属区域的子区域数，排序相当于剪枝而不需考虑
if Depth>0 then
conn.execute("update SK_SystemArea set Child=Child-1 where AreaID="&iParentID)
end if

SuccessMsg="区域合并成功！已经将被合并区域及其下属子区域的所有数据转入目标区域中。<br><br>同时删除了被合并的区域及其子区域。"
call WriteSuccessMsg(SuccessMsg)
set rs=Nothing
set trs=Nothing
end sub

sub Admin_ShowClass_Option(ShowType,CurrentID)
if ShowType=0 then
    response.write "<option value='0'"
if CurrentID=0 then response.write " selected"
response.write ">无(作为一级区域)</option>"
end if
dim rsClass,sqlClass,strTemp,tmpDepth,i
dim arrShowLine(20)
for i=0 to ubound(arrShowLine)
arrShowLine(i)=False
next
sqlClass="Select * From SK_SystemArea order by RootID,OrderID"
set rsClass=server.CreateObject("adodb.recordset")
rsClass.open sqlClass,conn,1,1
if rsClass.bof and rsClass.bof then
response.write "<option value=''>请先新建区域</option>"
else
dim UserLevel
if UserLevel="" then
UserLevel=5000
else
UserLevel=Cint(UserLevel)
end if
do while not rsClass.eof
tmpDepth=rsClass("Depth")
if rsClass("NextID")>0 then
arrShowLine(tmpDepth)=True
else
arrShowLine(tmpDepth)=False
end if
if ShowType=1 then

elseif ShowType=2 then

elseif ShowType=3 then
if rsClass("Child")>0 then
strTemp="<option value=''"
strTemp="<option value='0'"
else
strTemp="<option value='"&rsClass("AreaID")&"'"
end if
elseif ShowType=4 then
if rsClass("Child")>0 then
strTemp="<option value=''"
strTemp="<option value='0'"
else
strTemp="<option value='"&rsClass("AreaID")&"'"
end if
else
strTemp="<option value='"&rsClass("AreaID")&"'"
end if
if CurrentID>0 and rsClass("AreaID")=CurrentID then
 strTemp=strTemp & " selected"
end if
strTemp=strTemp & ">"

if tmpDepth>0 then
for i=1 to tmpDepth
strTemp=strTemp & "&nbsp;&nbsp;"
if i=tmpDepth then
if rsClass("NextID")>0 then
strTemp=strTemp & "├&nbsp;"
else
strTemp=strTemp & "└&nbsp;"
end if
else
if arrShowLine(i)=True then
strTemp=strTemp & "│"
else
strTemp=strTemp & "&nbsp;"
end if
end if
next
end if
strTemp=strTemp & rsClass("AreaName")
strTemp=strTemp & "</option>"
response.write strTemp
rsClass.movenext
loop
end if
rsClass.close
set rsClass=Nothing
end sub
Call CloseDB()
%>