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 wormstrdo;

public partial class cusreg : System.Web.UI.Page
{
    protected string cnames;
    protected string cpws;
    protected string csexs;
    protected string cqqs;
    protected string cmails;
    protected string caddresss;
    protected string cheads;
    protected string cinstroduces;
    protected string cdates;
    protected string cleis;
    protected int cjis;
    protected int cdengs;
    protected int csends;
    protected string sql;

    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = "SkinFile";
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        csame.Text = "";
    }

    protected void send_Click(object sender, EventArgs e)      //单击提交按钮，写入用户信息
    {
        if (Page.IsValid)
        {
            if (this.tbck.Text == Request.Cookies["vCode"].Value)
            {
                if (csame.Text == "可以注册！" && CustomValidator1.IsValid == true)
                {
                    SqlConnection one = conn.sqlconn();
                    try
                    {
                        one.Open();
                        cnames = this.cname.Text;
                        cpws = this.cpw.Text;
                        csexs = this.csex.Text;
                        cqqs = this.cqq.Text;
                        cmails = this.cmail.Text;
                        caddresss = this.caddress.Text;
                        cheads = this.headlist.Text;
                        cinstroduces = this.cinstroduce.Text;
                        cdates = DateTime.Now.ToString("d");
                        cleis = "普通会员";
                        cjis = 1;
                        cdengs = 0;
                        csends = 0;

                        //过滤字符串
                        strreplace str = new strreplace();
                        cnames = str.txtstr(cnames);
                        cpws = str.txtstr(cpws);
                        cqqs = str.txtstr(cqqs);
                        cmails = str.txtstr(cmails);
                        caddresss = str.msgstr(caddresss);
                        cinstroduces = str.msgstr(cinstroduces);
                        //End

                        sql = "insert into customer(cname,cpw,csex,cqq,cmail,caddress,chead,cinstroduce,cdate,clei,cji,cdeng,csend) values('" + cnames.Trim() + "','" + cpws.Trim() + "','" + csexs + "','" + cqqs + "','" + cmails + "','" + caddresss + "','" + cheads + "','" + cinstroduces + "','" + cdates + "','" + cleis + "'," + cjis + "," + cdengs + "," + csends + ")";
                        SqlCommand cmd = new SqlCommand(sql, one);  //建立SQL命令
                        int ok = cmd.ExecuteNonQuery();
                        if (ok != 0)
                        {

                            Session["ad"] = cnames;
                            Session["pw"] = cpws;
                            Session["profile"] = cleis;


                            //用户登录一次，cdeng段值加一
                            string Sql = "update customer set cdeng=cdeng+1 where cname='" + cnames + "'";
                            cmd = new SqlCommand(Sql, one);
                            cmd.ExecuteNonQuery();
                            //End

                            //这为用户注册，更改Application["EveryIP"]的用户名
                            string newad;
                            string oldad;
                            if (Application["EveryIP"].ToString().IndexOf(Request.UserHostAddress.ToString()) > 0)
                            {
                                if (Session["ad"].ToString() == "")
                                { newad = "游客"; }
                                else
                                { newad = Session["ad"].ToString(); }
                                if (Session["oldad"].ToString() == "")
                                { oldad = "游客"; }
                                else
                                { oldad = Session["oldad"].ToString(); }
                                string tempip = oldad + "[" + Session["ip"].ToString() + "]&nbsp;&nbsp;|&nbsp;&nbsp;";    //登录前用户
                                string tempip2 = newad + "[" + Session["ip"].ToString() + "]&nbsp;&nbsp;|&nbsp;&nbsp;";      //登录后用户
                                Application.Lock();
                                Application["EveryIP"] = Application["EveryIP"].ToString().Replace(tempip, tempip2);
                                Application.UnLock();//解锁
                            }
                            else
                            {
                                if (Session["ad"].ToString() == "")
                                { newad = "游客"; }
                                else
                                { newad = Session["oldad"].ToString(); }
                                string tempip = newad + "[" + Session["ip"].ToString() + "]&nbsp;&nbsp;|&nbsp;&nbsp;";      //登录后用户
                                Application.Lock();
                                Application["EveryIP"] = Application["EveryIP"] + tempip;
                                Application.UnLock();//解锁
                            }
                            //End
                            Session["oldad"] = Session["ad"].ToString();
                            Response.Write("<script>window.alert('注册成功!');window.parent.location='default.aspx';</script>");
                        }
                        cmd.Dispose();
                    }
                    catch (System.Exception ee)
                    {
                        Response.Write(ee.Message.ToString());
                    }
                    finally
                    {
                        one.Close();
                    }
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('验证码错误,请重新输入！');</script>");
            }
        }
    }

    protected void samename(object source, ServerValidateEventArgs arguments)   //同名用户排除【用于自定义验证控件】
    {
        SqlConnection one = conn.sqlconn();
        try
        {
            one.Open();
            cnames = this.cname.Text;
            if (cnames == "admin" || cnames=="administrator" || cnames=="游客")
            {
                csame.Text = "系统内定名称不能注册！";
            }
            else
            {
                sql = "select * from customer where cname='" + cnames + "'";
                SqlCommand cmd = new SqlCommand(sql, one);
                SqlDataReader same = cmd.ExecuteReader();
                if (same.HasRows)
                {
                    //csame.Text = "用户名已存在！";
                    arguments.IsValid = false;
                }
                else
                {
                    csame.Text = "可以注册！";
                    arguments.IsValid = true;
                }
                same.Dispose();
                cmd.Dispose();
            }
        }
        catch (System.Exception ee)
        {
            Response.Write(ee.Message.ToString());
        }
        finally
        {
            one.Close();
        }
    }
    
    protected void reset_Click(object sender, EventArgs e)
    {
        cname.Text = "";
        cpw.Text = "";
        cpw2.Text = "";
        cqq.Text = "";
        cmail.Text = "";
        caddress.Text = "";
        cinstroduce.Text = "";
        csex.SelectedIndex = 0;
        headlist.SelectedIndex = 0;
    }
    protected void pwcom(object sender, EventArgs e)
    {
        compw.Enabled = true;
    }
}
