<%@ Language=VBScript %>
<!--#include virtual="/inc-freehost-function/FreeHostFunction.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<%
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 Request.Form ("Emaillistid")="" Then
	ef=True
	ec="请选择产品！"
ElseIf Instr(1, Request.Form ("Emaillistid"), "'", 1)>0 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")&"'"
			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="要升级的邮箱产品不存在！"
else
   if Not IsNull(dsph("byhost")) then
   	ef=True
	ec="由于虚拟主机赠送的不能升级！"
   end if
   if Not IsNull(dsph("bydomain")) then
   	ef=True
	ec="由于域名赠送的不能升级！"
   end if
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="服务器不存在！"
else
if dsserver("agent1")="n" then
isAgnEmailSer=false
else
isAgnEmailSer=true
end if
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 trim(lcase(ds ("username")))=trim(lcase(session("agentusername"))) Then
	ef=True
	ec="如果作为代理给自己开通产品，请在代理区--用户管理，用代理名作用户名切换到用户模式！"
end if
end if

if not ef then
if not isnull(dsph("repaymoney")) then
	ef=True
	ec="由于此产品被管理员设置了单独的续费价格，因此，请您联系管理员帮您手工升级。"
end if
end if

if not ef then
	Set dshost = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_EmailProductlist where id='"&Request.Form ("Emaillistid")&"'"
			if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dshost.Open Sql,conn,1,1
if dshost.Eof then
	ef=True
	ec="产品不存在！"
end if
end if

if not ef then
  Set dsagent = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_AGENT where username='"&session("agentusername")&"' and password='"&session("agentpassword")&"'"
		if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"

Response.End

end if
		dsagent.Open Sql,conn,1,3
If dsagent.Eof Then
		ef=True
		ec="代理会员名不存在或密码出错了！"
end if
end if

if not ef then
	Set dsagnettype = Server.CreateObject("ADODB.Recordset")	
Sql="Select * from FreeHost.FreeHost_Agent_pers where agenttype='"& session("agenttype")&"'"
		dsagnettype.Open Sql,conn,1,1
if dsagnettype.Eof then
	ef=True
	ec="代理类型折扣为空，请先设置它！"
end if
end if

if not ef then
	Set dsagnpri = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_price where prtype='邮箱产品' and username='"&session ("agentusername")&"' and id="&dshost("id")&""
		dsagnpri.Open Sql,conn,1,1
if dsagnpri.eof then
myrealcost=dshost("usermoney")*dsagnettype("pers")
else
myrealcost=dsagnpri("usermoney")
end if
dsagnpri.close

	Set dsendmoney = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_user_price where prtype='邮箱产品' and username='"&session("agentusername")&"' and id="&dshost("id")&""
		dsendmoney.Open Sql,conn,1,1
if dsendmoney.eof then
usermoney=dshost("usermoney")
else
usermoney=dsendmoney("usermoney")
end if
dsendmoney.close
end if


if not ef then
if dsph("istest")="y" then
	ef=True
	ec="试用状态的产品必须延期后成为正式产品才可以升级到高级别！"
end if
end if

if not ef then
	Set dshostold = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_EmailProductlist where Emailtype='"&dsph ("Emailtype")&"'"
	dshostold.Open Sql,conn,1,1
if dshostold.Eof then
	ef=True
	ec="旧产品不存在！"
else
   if dshostold("isbyhost")="y" then
   	ef=True
	ec="赠送的不能升级！"
   end if
   if dshostold("isbydomain")="y" then
   	ef=True
	ec="赠送的不能升级！"
   end if
end if
end if


if not ef then
	Set olddsagnpri = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_price where prtype='邮箱产品' and username='"&session ("agentusername")&"' and id="&dshostold("id")&""
		olddsagnpri.Open Sql,conn,1,1
if olddsagnpri.eof then
oldmyrealcost=dshostold("usermoney")*dsagnettype("pers")
else
oldmyrealcost=olddsagnpri("usermoney")
end if
olddsagnpri.close

	Set olddsendmoney = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_user_price where prtype='邮箱产品' and username='"&session("agentusername")&"' and id="&dsph("id")&""
		olddsendmoney.Open Sql,conn,1,1
if olddsendmoney.eof then
oldusermoney=dshostold("usermoney")
else
oldusermoney=olddsendmoney("usermoney")
end if
olddsendmoney.close
end if


if not ef then
if dsph("istest")<>"y" then
if dshost("Emailtype")=dsph("Emailtype") then
	ef=True
	ec="同级别不可以升级!"
end if
end if
end if

if not ef then
dim starttime,temptime,pdtype
starttime=dsph("endtime")
pdtype="原类型："&dsph ("Emailtype")&" 新类型："&dshost("Emailtype")
if dshostold("usermoney")>dshost("usermoney") then
	ef=True
	ec="不能向低别升级!"
end if
'折算的用户金额
oldmoney=DateDiff("d",NOW,dsph("endtime"))
oldmoney=Clng(dshostold("usermoney")/365*oldmoney)
if dsph("istest")="y" then  oldmoney=0
if oldmoney<0 then oldmoney=0
'折算的成本金额
oldRmoney=DateDiff("d",NOW,dsph("endtime"))
oldRmoney=Clng(oldmyrealcost/365*oldRmoney)
if dsph("istest")="y" then  oldRmoney=0
if oldRmoney<0 then oldRmoney=0
end if

if not ef then
'myrealcost=myrealcost*Request.Form ("year")
If Request.Form ("chmoney")<>"" Then
if ds("usermoney")+oldmoney-usermoney<0 then
	ef=True
	ec="此用户的余额不足！"
end if
if (usermoney-oldmoney+dsagent("usermoney"))<(myrealcost-oldmyrealcost+oldRmoney)  and isAgnHostSer=false then'成本差价
	ef=True
	ec="您的余额不足！需要金额："&(myrealcost-oldmyrealcost+oldRmoney)
end if
else
if (myrealcost-oldRmoney)>dsagent("usermoney")  and isAgnHostSer=false then
	ef=True
		ec="您的余额不足！"&(myrealcost-oldRmoney)
end if
end if
end if


if not ef then
moneych=0
mymoneych=0
olduser_money=ds("usermoney")
oldagn_money=dsagent("usermoney")
If Request.Form ("chmoney")<>"" Then
ds("usermoney")=ds("usermoney")+oldmoney-usermoney
moneych=-(oldmoney-usermoney)
dsagent("usermoney")=dsagent("usermoney")+usermoney-oldmoney
mymoneych=(usermoney-oldmoney)
ds.update
end if
if isAgnEmailSer then
mymoneych=0
dsagent("usermoney")=oldagn_money
else
dsagent("usermoney")=dsagent("usermoney")-(myrealcost-oldRmoney)
mymoneych=mymoneych-(myrealcost-oldRmoney)
dsagent.update
end if
newagn_money=dsagent("usermoney")
newuser_money=ds("usermoney")
end if

if not ef then

'开始扣费
'ds("usermoney") =ds("usermoney") -cost
'ds.update
'更新产品类别
oldtime=dsph ("endtime")
dsph ("Emailtype")=dshost("Emailtype")
dsph ("emailcount")=dshost("emailcount")
dsph ("userspacelimt")=dshost("userspacelimt")
dsph ("domainspacelimt")=dshost("domainspacelimt")
dsph ("domainmustsame")=dshost("domainmustsame")
temptime=dateadd("yyyy",1,now)
if dsph("istest")="y" then
dsph ("endtime")=temptime
dsph("istest")="n"
else
dsph ("endtime")=temptime
end if
dsph("status")="正常"
dsph.update
temptime=dsph ("endtime")

dim isAgnHostSer
isAgnHostSer=isAgnEmailSer
logfor="(AG)升级邮箱产品"
if isAgnHostSer then logfor="自动开通升级邮箱产品独立模式"

FreeHost_moneyNewlog conn,"y","n","n","n","n",isAgnHostSer,moneych,dsph ("username"),session("agentusername"),dsph ("username"),olduser_money,newuser_money,agnusername,agnStmoney,agnEndmoney,supusername,logfor,logfor,"邮箱产品",dsph("id"),pingzheng,type1,bank,haveget,havecheck
if ds("agent1")<>"n" then
if newagn_money-oldagn_money>=0  then
FreeHost_moneyNewlog conn,"n","y","n","n","y",isAgnHostSer,newagn_money-oldagn_money,session("agentusername"),dsph ("username"),norusername,norStmoney,norEndmoney,session("agentusername"),oldagn_money,newagn_money,supusername,logfor&"代理收入",logfor&"代理收入","邮箱产品",dsph("id"),pingzheng,type1,bank,haveget,havecheck
else
FreeHost_moneyNewlog conn,"n","y","n","n","n",isAgnHostSer,-(newagn_money-oldagn_money),session("agentusername"),dsph ("username"),norusername,norStmoney,norEndmoney,session("agentusername"),oldagn_money,newagn_money,supusername,logfor&"扣除代理资金",logfor&"扣除代理资金","邮箱产品",dsph("id"),pingzheng,type1,bank,haveget,havecheck
end if
end if

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 not dspdallemail.Eof then
emailname=trim(dspdallemail("emailname"))
emailarry=split(emailname,"@")
name1=emailarry(0)
domain=lcase(trim(emailarry(1)))
op=0
temp2=PauseEmail(dsph ("ServerlistID"),dspdallemail("emailname"),op)
tem=ModAdmEmail(dsph ("ServerlistID"),trim(dspdallemail("emailname")),domain,dsph ("emailcount"),dsph ("domainspacelimt")*1024)'maxuser
end if
%>
<!--#include virtual="/inc-freehost-function/htmlwrite.asp"--><%=htmlwrite("html5")%>　邮箱管理
<%=htmlwrite("html6")%>
<br>
<br>
<br>
<table width="84%"  border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FF0000">
  <tr align="center" bordercolor="#000000" bgcolor="#999999">
    <td height="39" colspan="4"><font color="#000000">　　　</font><font color="#990000"><font color="#FF0000">邮箱升级成功，以下是相关的资料：</font></font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td width="50%" height="30"><font color="#000000"> 　　　 用户的原金额：</font></td>
    <td width="50%" colspan="3"><font color="#000000"><%=olduser_money%>元</font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td height="41"><font color="#000000">　　　 用户的金额变化：</font></td>
    <td colspan="3"><font color=red><%=moneych%></font><font color=#FF0000> 元</font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td height="39"><font color="#000000">　　　 用户的新金额：</font></td>
    <td colspan="3"><font color="#000000"><%=newuser_money%> 元</font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td height="39"><font color="#000000">　　　 我的原金额：</font></td>
    <td colspan="3"><font color="#000000"><%=oldagn_money%> 元</font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td height="39"><font color="#000000">　　　 我的金额变化：</font></td>
    <td colspan="3"><font color=red><%=newagn_money-oldagn_money%></font><font color="#000000"> 元</font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td height="39"><font color="#000000">　　　 我的新金额：</font></td>
    <td colspan="3"><font color="#000000"><%=newagn_money%> 元</font></td>
  </tr>
  <tr bordercolor="#000000">
    <td height="37" bgcolor="#eeeeee"><font color="#000000"> 　　　 原来的到期时间是:</font></td>
    <td colspan="3" bgcolor="#eeeeee"><%=oldtime%></td>
  </tr>
  <tr bordercolor="#000000">
    <td height="37" bgcolor="#eeeeee"><font color="#000000"> 　　　 <font color="#FF0000">现在的到期时间是:</font></font></td>
    <td colspan="3" bgcolor="#eeeeee"><%=temptime%></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#eeeeee">
    <td colspan="4"><font color="#000000">　　　</font><font color="#000000">　　　</font><font color="#000000">　</font><font color="#000000">　　　</font><font color="#000000">　　　</font><font color="#000000"> 　　　</font> 　</td>
  </tr>
  <tr align="center" bordercolor="#000000">
    <td height="34" colspan="4" bgcolor="#999999"><font color="#990000">　我　们　为　您　提　供　优　质　的　服　务！</font></td>
  </tr>
</table>
<br>
<br>
<%=htmlwrite("html7")%>
<%end if
if ef then
Response.Redirect "e.asp?e="&ec
end if%>
