<%@ 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("username")="" 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 not IsNumeric(Request.Form ("year"))  Then
	ef=True
	ec="时间应该是数字！"
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 username='"&session("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
if ds("password")<>session("password") 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.Form ("id")&"'"
	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("agentname")<>dsemailpdlist("onlyusebyagn") then
	ef=True
	ec="本产品只允许特定的代理使用！"
End if
end if
end If

if not ef then
if dsemailpdlist("canbyenduser")="n" And session("agentname")="n" then
	ef=True
	ec="本产品不允许最终用户开通！"
end if
end If

if not ef then
if dsemailpdlist("canbyagnenduser")="n" And session("agentname")<>"n" then
	ef=True
	ec="本产品不允许用户开通！"
end if
end If

if not ef then
if dsemailpdlist("canbyagn")="n" And session("AgnRunasUser")=true then
	ef=True
	ec="本产品不允许代理开通！"
end if
end if


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

if not ef then
if Request.Form ("year")=dsemailpdlist("PAY_Season") and dsemailpdlist("canseason")="n" then
	ef=True
	ec="很抱歉,本产品不可以季付！"
end if
end if
if not ef then
if Request.Form ("year")=dsemailpdlist("PAY_halfyear") and dsemailpdlist("canhalfyear")="n" 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
Set dsphemail = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_Email where username='"&session ("username")&"' and istest='y'"
	dsphemail.Open Sql,conn,1,3
if Request.Form ("year")=999 then
if not dsphemail.Eof then
	ef=True
	ec="一个用户只能试用一个产品！"
end if
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)
dim isfortest
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="用户区新开通邮箱产品"
pubisagnSer=isAgnEmailSer
AgnRunasUser=session("AgnRunasUser")
DoPayMoney
end if

if not ef then

dsphemail.addnew
dsphemail("username")=ds("username")
dsphemail("ServerlistID")=ServerlistID
dsphemail("Emailtype")=dsemailpdlist("Emailtype")
dsphemail("starttime")=starttime
dsphemail("endtime")=temptime
if y=999 then
dsphemail("istest")="y"
else
dsphemail("istest")="n"
end if

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

dsphemail("emailcount")=dsemailpdlist("emailcount")
dsphemail("userspacelimt")=dsemailpdlist("userspacelimt")
dsphemail("domainspacelimt")=dsemailpdlist("domainspacelimt")
dsphemail("domainmustsame")=dsemailpdlist("domainmustsame")
dsphemail("status")="正常"
if ds("agent1")<>"n" then
dsphemail("agent1")=ds("agent1")
else
dsphemail("agent1")="n"
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)

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

%>
<!--#include virtual="/inc-freehost-function/htmlwrite.asp"--><%=htmlwrite("html2")%>
　邮箱自助开通成功
<%=htmlwrite("html3")%>
<br>
<br>
<br>
<table width="60%"  border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FF0000">
  <tr align="center" bordercolor="#000000" bgcolor="#999999">
    <td height="37" colspan="2" bgcolor="#999999"><font color="#000000">　　　</font><font color="#990000"><font color="#FF0000">恭喜您的邮箱开通成功，以下是您的资料：</font></font></td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#999999">
    <td width="46%" height="37" align="right" bgcolor="#eeeeee">用户的原金额：</td>
    <td width="54%" height="37" align="left" bgcolor="#eeeeee">　<%=olduser_money%>元</td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="37" align="right" bgcolor="#eeeeee">用户金额变化：</td>
    <td height="37" align="left"><font color=red>　<%=moneych%>元</font></td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="40" align="right">用户的新金额：</td>
    <td height="40" align="left">　<%=newuser_money%>元</td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="40" align="right"><font color="#000000">邮箱到期时间：</font></td>
    <td height="40" align="left">　<%=temptime%></td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="44" align="right">详细邮箱管理：</td>
    <td height="44" align="left">　<a href="mailadm.asp"><font color="#000000">[管理邮箱]</font></a></td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#9C9A9C">
    <td height="39" colspan="2"><font color="#990000">　我　们　为　您　提　供　优　质　的　服　务！</font></td>
  </tr>
</table>
<br>
<br>
<br>
<br>

<br>
<%=htmlwrite("html4")%>
<%end if
if ef then
Response.Redirect "e.asp?e="&ec
end if%>
