<%@ Language=VBScript %>
<!--#include virtual="/inc-freehost-function/FreeHostEmail.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunction.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<%
If session("agentusername")="" Then
	ef=True
	ec="授权非法，有可能是超时造成的。"
ElseIf Request("username")=""  or RegExpTest("[^A-Za-z0-9]",Request("username")) Then
	ef=True
	ec="请输入用户名！"
'ElseIf trim(lcase(Request.Form ("username")))=trim(lcase(session("agentusername"))) Then
'	ef=True
'	ec="如果作为代理给自己开通产品，请在代理区--用户管理，用代理名作用户名切换到用户模式！"
ElseIf Request("Emailtype")="" Then
	ef=True
	ec="请选择产品！"
ElseIf Not IsNumeric(Request("Emailtype"))  Then
	ef=True
	ec="非法产品类型！"
end if

if not ef then
If Request.Form ("usermoney")<0  Then
	ef=True
	ec="金额不能小于零！"
end if
end if

if not ef then
Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
	Set ds = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_USER where  (agent1='"&session("agentusername")&"' or agent2='"&session("agentusername")&"') and username='"&Request("username")&"'"
				if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
		ds.Open Sql,conn,1,3
if ds.eof then
	ef=True
	ec="您所代理的用户中不存在这个用户!"
end if
end if

if not ef then
	Set dsemailpdlist = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_EmailProductlist where id='"&Request("Emailtype")&"'"
			if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dsemailpdlist.Open Sql,conn,1,1
if dsemailpdlist.Eof then
	ef=True
	ec="产品不存在！"
else
   if dsemailpdlist("isbyhost")="y" then
   	ef=True
	ec="赠送的只能自动开通！"
   end if
   if dsemailpdlist("isbydomain")="y" then
   	ef=True
	ec="赠送的只能自动开通！"
   end if

ServerlistID=dsemailpdlist ("ServerlistID")
if  Request.Form("ServerlistID")=cstr(dsemailpdlist ("ServerlistID2")) and dsemailpdlist ("ServerlistID2")<>0 then ServerlistID=dsemailpdlist ("ServerlistID2")
if  Request.Form("ServerlistID")=cstr(dsemailpdlist ("ServerlistID3")) and dsemailpdlist ("ServerlistID3")<>0 then ServerlistID=dsemailpdlist ("ServerlistID3")
if  Request.Form("ServerlistID")=cstr(dsemailpdlist ("ServerlistID4")) and dsemailpdlist ("ServerlistID4")<>0 then ServerlistID=dsemailpdlist ("ServerlistID4")
if  Request.Form("ServerlistID")=cstr(dsemailpdlist ("ServerlistID5")) and dsemailpdlist ("ServerlistID5")<>0 then ServerlistID=dsemailpdlist ("ServerlistID5")

	Set agnserverid = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_EmailProductlist_AGN where id='"&dsemailpdlist ("id")&"' and agent1='"&ds("agent1")&"'"
	agnserverid.Open Sql,conn,1,1
if not agnserverid.Eof then
if agnserverid ("ServerlistID")<>0 then
ServerlistID=agnserverid ("ServerlistID")
isAgnEmailSer=true
else
isAgnEmailSer=false
end if
end if
end if
end If

if not ef then
If Request.Form ("year")<>999 and Request.Form ("year")<>dsemailpdlist("PAY_Month") and Request.Form ("year")<>dsemailpdlist("PAY_Season")  and Request.Form ("year")<>dsemailpdlist("PAY_halfyear") and Request.Form ("year")<>1 and Request.Form ("year")<>dsemailpdlist("PAY_2year") and Request.Form ("year")<>dsemailpdlist("PAY_3year") and Request.Form ("year")<>dsemailpdlist("PAY_4year") and Request.Form ("year")<>dsemailpdlist("PAY_5year") Then
	ef=True
	ec="请选择时间！"
end If
end If

if not ef then
if Not IsNull(dsemailpdlist("onlyusebyagn")) Then
If session("agentusername")<>dsemailpdlist("onlyusebyagn") then
	ef=True
	ec="本产品只允许特定的代理使用！"
End if
end if
end If

if not ef then
'if not dsemailpdlist("canbyenduser")="y" and not dsemailpdlist("canbyagnenduser")="y" and not dsemailpdlist("canbyagn")="y" Then
if dsemailpdlist("canbyagn")="n" then
	ef=True
	ec="本产品不允许开通！"
end if
end if

if not ef then
if Request.Form ("year")=999 and dsemailpdlist("isbydomain")="y" and Application("Agentcantest")=false then
	ef=True
	ec="很抱歉,送域名的虚拟主机不能设为试用！"
end if
end if

if not ef then
if Request.Form ("year")=dsemailpdlist("PAY_Month") and dsemailpdlist("canmonth")="n" and Application("Agentcanunlimit")=false then
	ef=True
	ec="很抱歉,本产品不可以月付！"
end if
end if

if not ef then
if Request.Form ("year")=dsemailpdlist("PAY_Season") and dsemailpdlist("canseason")="n" and Application("Agentcanunlimit")=false then
	ef=True
	ec="很抱歉,本产品不可以季付！"
end if
end if
if not ef then
if Request.Form ("year")=dsemailpdlist("PAY_halfyear") and dsemailpdlist("canhalfyear")="n" and Application("Agentcanunlimit")=false 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='"&ServerlistID&"'"
	dsserver.Open Sql,conn,1,1
if dsserver.Eof then
	ef=True
	ec="服务器不存在！"
end if
end if

if not ef then

dim starttime,temptime,pdtype,isAgnHostSer
isAgnHostSer=isAgnEmailSer
pdtype=dsemailpdlist("Emailtype")
starttime=Now
'结束时间
y=Request.Form ("year")
if y=1 then temptime=dateadd("yyyy",1,now)
if y=dsemailpdlist("PAY_2year") then temptime=dateadd("yyyy",2,now)
if y=dsemailpdlist("PAY_3year") then temptime=dateadd("yyyy",3,now)
if y=dsemailpdlist("PAY_4year") then temptime=dateadd("yyyy",4,now)
if y=dsemailpdlist("PAY_5year") then temptime=dateadd("yyyy",5,now)
if y=dsemailpdlist("PAY_Month") then temptime=dateadd("y",31,now)
if y=dsemailpdlist("PAY_Season") then temptime=dateadd("y",92,now)
if y=dsemailpdlist("PAY_halfyear") then temptime=dateadd("y",182,now)
if y=999 then
temptime=dateadd("y",Application("testtime"),now)
isfortest=true
end if

dim ef,ec,Userpaymoney,forpaytype,olduser_money,moneych,newuser_money,oldagn_money,mymoneych,newagn_money,Useragentname,agent_UP,agent_thisgree_cost,agent_this_name,logfor,pubisagnSer,AgnRunasUser
Userpaymoney=dsemailpdlist("usermoney")
forpayyear=Request.Form ("year")
set RsUserds=ds
Useragentname=ds("agent1")
forpaytype="邮箱产品"
forpayid=dsemailpdlist("id")
logfor="开通新建(AG)"
pubisagnSer=isAgnEmailSer
AgnRunasUser=false
DoPayMoney
end if

if not ef then
	Set dsphemail = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_Email"
	dsphemail.Open Sql,conn,1,3
if request.Form ("endtime")<>"" then temptime=request.Form ("endtime")
dsphemail.addnew
dsphemail("username")=ds("username")
dsphemail("ServerlistID")=dsserver("id")
dsphemail("Emailtype")=dsemailpdlist("Emailtype")
dsphemail("starttime")=starttime
dsphemail("endtime")=temptime
if y=999 then
dsphemail("istest")="y"
else
dsphemail("istest")="n"
end if

dsphemail("emailcount")=dsemailpdlist("emailcount")
dsphemail("userspacelimt")=dsemailpdlist("userspacelimt")
dsphemail("domainspacelimt")=dsemailpdlist("domainspacelimt")
if Request.Form("emailcount")<>"" then dsphemail("emailcount")=Request.Form("emailcount")
if Request.Form("userspacelimt")<>"" then dsphemail("userspacelimt")=Request.Form("userspacelimt")
if Request.Form("domainspacelimt")<>"" then dsphemail("domainspacelimt")=Request.Form("domainspacelimt")
dsphemail("domainmustsame")=dsemailpdlist("domainmustsame")

dsphemail("status")="正常"

dsphemail("kefu")=ds("kefunow")

if ds("agent1")<>"n" then
dsphemail("agent1")=ds("agent1")
else
dsphemail("agent1")="n"
end If

If Application("smsemailopen")=True And (ds("agent1")="n" Or Application("agncangetsms")=True) Then
Dim username
username=ds("username")
mobi=Trim(ds("mobi"))
smsContent=Application("smsemailopencon")
'恭喜！您的邮箱产品已开通了！用户名 ：#用户名#产品类型 ：#产品类型#到期时间是 ：#到期时间#您的帐户余款是 ：#金额# 元
smsContent=Replace(smsContent,"#用户名#",ds("username"))
smsContent=Replace(smsContent,"#产品类型#",dsphemail("Emailtype"))
smsContent=Replace(smsContent,"#到期时间#",formatdatetime(dsphemail("endtime"),1))
smsContent=Replace(smsContent,"#金额#",newuser_money)
ret=SendSms(mobi,smsContent,0,Now,"")
End if


dsphemail.update
'dsphemail.close

'log
	Set dsphemail = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_Email where starttime='"&starttime&"' and username='"&ds("username")&"'"
	dsphemail.Open Sql,conn,1,1
Sql="UPDATE [FreeHost].[FreeHost_Moneylog] set pdid='"&dsphemail("id")&"' where pdid='"&session("moneylog_pdid")&"'"
    conn.Execute Sql

dim Subject,body
emailopen
ret=Send2Mail(conn,ds("username"),Subject,body)
%>
<!--#include virtual="/inc-freehost-function/htmlwrite.asp"--><%=htmlwrite("html5")%>　开通邮箱
<%=htmlwrite("html6")%>
<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><%=mymoneych%></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 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>
<%=htmlwrite("html7")%>
<%end if
if ef then
Response.Redirect "e.asp?e="&ec
end if%>
