<%@ Language=VBScript %>
<!--#include virtual="/inc-freehost-function/FreeHostFunction.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<%
domain=trim(LCASE(request.Form ("domain")))
emailname=Application("admmailname")&"@"&domain
If session("agentusername")="" Then
	ef=True
	ec="授权非法，有可能是超时造成的。"
ElseIf Request.Form ("id")="" Then
	ef=True
	ec="请选择产品！"
ElseIf Instr(1, Request.Form ("id"), "'", 1)>0 then
	ef=True
	ec="非法产品ID！"
ElseIf not IsNumeric(Request.Form ("id"))  Then
	ef=True
	ec="产品ID应该是数字！"
elseif IsValidEmail(emailname)=false then
	ef=True
	ec="存在无效邮箱名称！"
end if

if not ef then
Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
	Set dsph = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_Email where id='"&Request.Form ("id")&"' and (agent1='"&session("agentusername")&"' or agent2='"&session("agentusername")&"') "
	if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dsph.Open Sql,conn,1,3
if dsph.Eof then
	ef=True
	ec="邮箱不存在！"
end if
end if


if not ef then
	Set ds = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_USER where  (agent1='"&session("agentusername")&"' or agent2='"&session("agentusername")&"') and username='"&dsph("username")&"'"
		ds.Open Sql,conn,1,3
if ds.eof then
	ef=True
	ec="用户不存在!"
end if
end if

if not ef then
if Application("notusedomain")<>"" then
emailarry=split(Application("notusedomain"),"@")
for i=0 to ubound(emailarry)
if len(emailarry(i))>3 then
If Instr(emailname, "@"&emailarry(i))>0 then
	ef=True
	ec="不允许设置这个域名！"
end if
end if
next
end if
end if

if not ef then
	Set dshost = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_EmailProductlist where Emailtype='"&dsph ("Emailtype")&"'"
	dshost.Open Sql,conn,1,1
if dshost.Eof then
	ef=True
	ec="产品类型不存在！"
end if
end if

if not ef then
	Set dsserver = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_ServerEmaillist where id='"&dsph ("ServerlistID")&"'"
	dsserver.Open Sql,conn,1,1
if dsserver.Eof then
	ef=True
	ec="服务器不存在！"
end if
end if

if not ef then
email=trim(emailname)
emailarry=split(email,"@")
username=emailarry(0)
domain=emailarry(1)
Set dspdallemail = Server.CreateObject("ADODB.Recordset")
		Sql="Select * from FreeHost.FreeHost_Product_ALLEmail where PdEID<>'"&dsph ("id")&"' and emailname like '%@"&domain&"'"
		dspdallemail.Open Sql,conn,1,1
if not dspdallemail.Eof then
	ef=True
	ec="此域已被使用！已存在的域管理员："&dspdallemail("emailname")&" 但要绑定："&domain
end if
end if

if not ef then
Set dspdallemail = Server.CreateObject("ADODB.Recordset")	
		Sql="Select top 1 * from FreeHost.FreeHost_Product_ALLEmail where username='"&ds ("username")&"' and PdEID='"&dsph ("id")&"'"
		dspdallemail.Open Sql,conn,1,3
'if dspdallemail.Eof then
'	ef=True
'	ec="邮箱不存在！"
'end if
'end if

'if not ef then

if not dspdallemail.Eof then
email=trim(dspdallemail("emailname"))
emailarry=split(email,"@")
name1=emailarry(0)
olddomain=lcase(trim(emailarry(1)))
if (domain<>olddomain) then
starttime=dspdallemail("starttime")
'如果旧域存在，并且与新输入的不同，就删除旧的域
sql="delete from FreeHost.FreeHost_Product_ALLEmail where username='"&ds ("username")&"' and PdEID='"&dsph ("id")&"'"
    conn.Execute Sql
ret=DelAdmEmail(dsph ("ServerlistID"),olddomain)
FreeHost_clientlog conn,dsph ("username"),session("admusername"),"超级管理员操作","邮箱产品",dsph ("id"),"删除","","",ret
end if
end if

if dspdallemail.Eof or (domain<>olddomain) then
 dspdallemail.addnew
dspdallemail("emailname")=emailname
dspdallemail("username")=ds ("username")
dspdallemail("PdEID")=dsph ("id")
dspdallemail("Emailtype")=dsph ("Emailtype")
if starttime<>"" then
dspdallemail("starttime")=starttime
else
dspdallemail("starttime")=now
end if
dspdallemail("endtime")=dsph ("endtime")
dspdallemail("istest")=dsph ("istest")
dspdallemail("status")=dsph ("status")
dspdallemail("userspacelimt")=dsph ("userspacelimt")
dspdallemail("domainspacelimt")=dsph ("domainspacelimt")
dspdallemail.update
end if
tem2=SetAdmEmail(dsph ("ServerlistID"),emailname,domain,dsph ("emailcount"),dsph ("domainspacelimt")*1024)'maxuser
if dsserver("softtype")="Imail" then
URL=dsserver("site")
FreeHostsharekey=dsserver("FreeHostsharekey")

regpath=domain&"\users\"&emailname
key="MaxSize"
keyvalue=dshost ("userspacelimt")*1024*1024
retvlue=replace(Regimailop(URL,FreeHostsharekey,regpath,key,keyvalue,2),"操作成功，数值为：","")
end if
%>
<!--#include virtual="/inc-freehost-function/htmlwrite.asp"--><%=htmlwrite("html5")%>
 　邮箱管理
 <%=htmlwrite("html6")%>
<br><%=ret%><%=tem2%>
<br><br><br>
成功设置，请返回上一页设置登陆密码。
<%=htmlwrite("html7")%>
<%end if
if ef then
Response.Redirect "e.asp?e="&ec
end if%>
