using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using xabaraCOM;

public partial class member_user_email : System.Web.UI.Page
{
    user u = new user();
    sqlServer sql = new sqlServer();
    javasrcipt js = new javasrcipt();
    encryptDecrypt ed = new encryptDecrypt();
    email mail = new email();

    public string webTitle = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        string idTemp = Request.QueryString["id"];
        if (!string.IsNullOrEmpty(idTemp as string))    //邮箱激活
        {
            string[] strValue = ed.decryptStrUrl(idTemp).Split(new char[] { '|' });
            string uIDTemp = strValue[1];   //先赋值会员号
            strValue[1] = DateTime.Now.ToString();

            string returnStr = sql.update("xabaraUser", "@userEmail,@userEmailDate", strValue, "userCardID='" + uIDTemp + "'");
            if (returnStr.Equals("更新成功"))
            {
                js.div(false, "", "text", "邮箱验证成功！", 350, 0, "/member/", "top");
            }
            else
            {
                js.div(false, "", "text", "邮箱验证失败，请重新激活！", 350, 0, "/member/", "top");
            }
        }
        else
        {
            u.check("", "/member/user/email.aspx");

            if (!IsPostBack)
            {
                DataTable dt = sql.getDataTable("select * from xabaraUser where userCardID='" + u.userCardID + "'");
                if (dt.Rows.Count > 0)
                {
                    email.Text = dt.Rows[0]["userEmail"].ToString().Trim();
                    oldMail.Value = dt.Rows[0]["userEmail"].ToString().Trim();

                    if (!string.IsNullOrEmpty(dt.Rows[0]["userEmailDate"].ToString().Trim() as string))
                    {
                        mailCheck.Text = "您的Email已通过验证！";
                    }
                }
                dt.Dispose();

                emailCheck.Text = xml.read("xabara.config", "emailRegs", 180);

                regExpression reg = new regExpression();
                emailRegularExpressionValidator.ValidationExpression = reg.email;
                emailRegularExpressionValidator.ErrorMessage = reg.emailError;
            }
        }

        webTitle = xml.read("xabara.config", "webName", 180);
        Page.Title = "邮箱认证　" + webTitle + xml.read("xabara.config", "titleMeta", 180);
    }

    protected void postButton_Click(object sender, ImageClickEventArgs e)
    {
        string mailADD = email.Text;
        string urlClick = "http://" + url.domain + "/member/user/email.aspx?id=" + ed.encryptStrUrl(mailADD + "|" + u.userCardID);

        DataTable dt;
        dt = sql.getDataTable("select * from XabaraUser where userEmail='" + mailADD + "' and userEmailDate<>''");
        if (dt.Rows.Count > 0)
        {
            js.div(false, "", "text", "您的邮箱已验证，请不要重复认证！", 350, 0, "", "");
        }
        else
        {
            dt = sql.getDataTable("select * from XabaraUser where userEmail='" + mailADD + "' and userCardID<>'" + u.userCardID + "'");
            if (dt.Rows.Count > 0)
            {
                js.div(false, "", "text", "此邮箱已被另一个帐号使用，请重新选择一个！", 350, 0, "", "");
            }
            else
            {
                string txtName = HttpUtility.UrlDecode(Request.Cookies["XabaraUser"]["userName"].ToString().Trim());
                if (string.IsNullOrEmpty(txtName as string))
                {
                    txtName = u.userCardID;
                }

                string bodyHtml = @"<table width='100%' border='0' cellpadding='3' cellspacing='3'><tr><td style='text-align:left; vertical-align:top; line-height:180%;'>尊敬的&nbsp;<strong>" + txtName + @"</strong>，您好！<br /><br />　　请点击下面的连接来验证你的Email:<br />　　<a href='" + urlClick + @"' target='_blank'>" + urlClick + @"</a><br /><br />　　若您无法直接点击以上链接，请复制后直接粘贴至IE浏览器地址栏进行访问：" + urlClick + @"<br />　　若无法访问链接或有其他疑问，请及时回复此邮件进行咨询，以免延误邮箱验证。</td></tr></table>";

                mail.add(u.userCardID, "user", 0, mailADD, "邮箱验证", "会员邮箱验证通知信", bodyHtml, true);

                sql.log(u.userCardID, "user", "邮箱验证");
                js.div(false, "", "text", "邮箱验证信息已发送至您的邮箱，请注意查收！", 350, 0, "", "");
            }
        }
        dt.Dispose();
    }
}