using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Mail;

public partial class WebMain_Member_Regvip1 : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        if (ckc.Checked != true)
        {
            Response.Redirect("~/Default.aspx");
        }

        if (IsPostBack)
        {
            this.Label1.Visible = false;
            string UserName = this.vipusername.Text;
            if (TOPdo.chkadminuser(UserName))
            {
                this.Label1.Visible = true;
                this.Label1.Text = "该用户存在。";
                this.Label1.CssClass = "ckckong2";
            }
            else
            {
                this.Label1.CssClass = "ckckong3";
                this.Label1.Visible = true;
                this.Label1.Text = "该用户不存在，您可以使用。";
            }
            if (UserName == "")
            {
                this.Label1.Visible = true;
                this.Label1.Text = "不能为空。";
                this.Label1.CssClass = "ckckong2";
            }
        }


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //取值开始
        string vipname, vipusername, vipuserpwd, PWDQuestions, PWDAnswer, vipsex, vipemail, vipBirthday, viparear, vipAddress, Post, MobilePhone, Telephone;
        //*********************
        //备用设置
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlCommand smd = new SqlCommand("select RegVIPset,IntegralSet,WEBName from SystemBasicInformation", conn);
        SqlDataReader sdr = smd.ExecuteReader();
        sdr.Read();
        string isok = sdr.GetString(0);  //  一经注册开通与否
        int Integal = sdr.GetInt32(1); //注册时默认赠送积分数量
        string company = sdr.GetString(2);//公司名字
        sdr.Close();
        conn.Close();
        int logintimes = 1;  //注册时默认登录次数
        string vipright = "1";  //登录权限

        //*********************        
        vipname = this.vipname.Text.Trim();
        vipusername = this.vipusername.Text.Trim();
        vipuserpwd = this.revipuserpwd.Text.Trim();
        PWDQuestions = this.qus.Value.ToString();
        PWDAnswer = this.PWDAnswer.Text.Trim();
        vipemail = this.vipemail.Text.Trim();
        vipBirthday = this.Birthday.Text;
        viparear = this.viparear.Text.Trim();
        vipAddress = this.vipAddress.Text.Trim();
        Post = this.Post.Text.Trim();
        MobilePhone = this.MobilePhone.Text.Trim();
        Telephone = this.Telephone.Text.Trim();
        string tjren = this.tjren.Text;
        if (tjren == "")
        {
            tjren = "无";
        }
        if (this.isn.Checked)
        {
            vipsex = "n";
        }
        else
        {
            vipsex = "v";
        }

        string userip = "";
        if (Request.ServerVariables["HTTP_VIA"] != null)
        {

            userip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();

        }
        else
        {

            userip = Request.ServerVariables["REMOTE_ADDR"].ToString();

        }
        float zk = 10.00f;
        string sqlstr = "insert into MemberVIP(isok,vipname,vipusername,vipuserpwd,PWDQuestions,PWDAnswer,vipemail,vipsex,vipBirthday,viparear,vipAddress,Post,MobilePhone,Telephone,addtime,logintimes,vipright,loginIP,Integral,enddate,tjren,intin,intout,vipzk) values ";
        sqlstr += " ('" + isok + "','" + vipname + "','" + vipusername + "','" + vipuserpwd + "','" + PWDQuestions + "','" + PWDAnswer + "','" + vipemail + "','" + vipsex + "','" + vipBirthday + "','" + viparear + "','" + vipAddress + "','" + Post + "','" + MobilePhone + "','" + Telephone + "','" + DateTime.Now.ToString() + "'," + logintimes + ",'" + vipright + "','" + userip + "'," + Integal + ",'" + DateTime.Now.ToString() + "','"+tjren+"','"+DateTime.Now.ToString()+"','"+DateTime.Now.ToString()+"',"+zk+")";
        if (Label1.Text.Trim() != "该用户存在")
        {
            if (TOPdo.Execsql(sqlstr))
            {
                this.Panel2.Visible = false;
                this.Panel1.Visible = true;
                this.username.Text = vipname;
                this.company.Text = company;
                this.uid.Text = vipusername;
                this.pwd.Text = vipuserpwd;
                SqlConnection Mconn = CDatabase.wtoconn();
                Mconn.Open();
                SqlCommand smddg = new SqlCommand("select * from  MemberVIP where vipusername='" + vipusername + "' and vipuserpwd='"+vipuserpwd+"'", Mconn);
                SqlDataReader sdrdg = smddg.ExecuteReader();
                sdrdg.Read();
                Session["username"] = sdrdg.GetString(3);
                Session["vipright"] = sdrdg.GetString(17).Trim();
                Session["userid"] = Convert.ToString(sdrdg.GetInt32(0));
                Session["times"] = "0";
                Session["times2"] = "0";
                Session["Wtouser"] = "vipadmin";
                Session["wtouserid"] = sdrdg.GetInt32(0);
                Session["vipname"] = sdrdg.GetString(2);
                Session["id"] = Convert.ToString(sdrdg.GetInt32(0));
                Session["intj"] = Integal;
                sdrdg.Close();
                SqlCommand Msmd = new SqlCommand("select * from SystemBasicInformation", Mconn);
                SqlDataReader Msdr = Msmd.ExecuteReader();
                Msdr.Read();
                string Mform, Muid, Mpwd, Mstmp, Misopen, Mcont, MCompanyName, MWEBName, MTel, MFax, MEmail, MQQ, MAddress, MMSN, TBWW, MLOGOpic, Memailto;
                Mform = Msdr.GetString(7);
                Muid = Mform;
                Mpwd = Msdr.GetString(26);
                Mstmp = Msdr.GetString(25);
                Misopen = Msdr.GetString(1).ToString().Trim();
                MCompanyName = Msdr.GetString(2);
                MWEBName = Msdr.GetString(3);
                MTel = Msdr.GetString(5);
                MFax = Msdr.GetString(6);
                MEmail = Msdr.GetString(7);
                MQQ = Msdr.GetString(8);
                MAddress = Msdr.GetString(10);
                MMSN = Msdr.GetString(13);
                TBWW = Msdr.GetString(14);
                Memailto = Msdr.GetString(20);
                MLOGOpic = "<a href=http://" + Request.ServerVariables["SERVER_NAME"].ToString() + "><img src=http://" + Request.ServerVariables["SERVER_NAME"].ToString() + "/images/" + Msdr.GetString(15) + " border=0></a>";
                Msdr.Close();
                if (Memailto == "y")
                {
                    SqlCommand Msmd2 = new SqlCommand("select content from email where lx='1'", Mconn);
                    Mcont = Server.HtmlDecode(Convert.ToString(Msmd2.ExecuteScalar()));
                    SqlCommand Msmd3 = new SqlCommand("select title from email where lx='1'", Mconn);
                    string Mti = Convert.ToString(Msmd3.ExecuteScalar());
                    MailMessage msg = new MailMessage();
                    msg.From = Mform;  //发送方地址(如test@163.com)
                    msg.To = vipemail; //接收方地址(如test@163.com) 
                    msg.BodyFormat = MailFormat.Html;   //正文内容类型 
                    msg.BodyEncoding = System.Text.Encoding.Default;    //正文内容编码 
                    //msg.Attachments.Add(new MailAttachment("d:\\d.txt"));
                    //主题 
                    msg.Subject = Mti;
                    //内容 
                    //编码定义
                    Mcont = ((Mcont.Replace("$UID$", vipusername)).Replace("$PWD$", vipuserpwd));
                    Mcont = Mcont.Replace("$CMY$", MCompanyName);
                    Mcont = Mcont.Replace("$WEBN$", MWEBName);
                    Mcont = Mcont.Replace("$TEL$", MTel);
                    Mcont = Mcont.Replace("$FAX$", MFax);
                    Mcont = Mcont.Replace("$EMA$", MEmail);
                    Mcont = Mcont.Replace("$QQ$", MQQ);
                    Mcont = Mcont.Replace("$ADD$", MAddress);
                    Mcont = Mcont.Replace("$MSN$", MMSN);
                    Mcont = Mcont.Replace("$TBW$", TBWW);
                    Mcont = Mcont.Replace("$LOGO$", MLOGOpic);
                    Mcont = Mcont.Replace("$ORN$", this.vipname.Text);
                    msg.Body = Mcont;
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //设置为需要用户验证 
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Muid); //设置验证用户名 
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Mpwd); //设置验证密码 
                    SmtpMail.SmtpServer = Mstmp;  //邮件服务器地址(如smtp.163.com) 
                    SmtpMail.Send(msg);  //发送 
                }
                Mconn.Close();
            }
            else
            {
                //Response.Write(sqlstr);
                Response.Write("<script>alert('添加不成功!');window.location.href='Regvip1.aspx'</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('存在同名用户，请重新换用户名；!');window.location.href='Regvip1.aspx'</script>");
        }

    }
}