using System;
using System.Data;
using System.Configuration;
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 UserInfo;
using Product;
using Base;
//实现接口
public partial class _register : BasePage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (CheckCookies())
        {
            //ShowMessage("您已处于登录状态,请退出后注册!","../Member/Index.aspx");
            Response.Redirect("../Error.aspx?err=1");
            return;
        }
        if (!IsPostBack)
        {
            if (bind())
            {
                IndustryBind();
                isShow();
            }
        }
    }



    private void isShow()
    {
        Solely.UserRegistr set = Solely.UserRegistr.GateItem();

        //是否启用验证码
        if (set.isCode)
        {
            this.trCode.Visible = true;
            this.ViewState["isCode"] = "1";
        }
        else
        {
            this.trCode.Visible = false;
            this.ViewState["isCode"] = "0";
        }
        //是否启用验证问题
        if (set.isQuestion)
        {
            this.ViewState["isQuestion"] = "1";
            string[] strQuestrion;
            string[] strAnswer;
            if (set.Question.Contains("!"))
            {

                strQuestrion = set.Question.Split('!');  //分解问题存入数组
                strAnswer = set.Answer.Split('!');
                Random randObj = new Random();
                int intRandom = randObj.Next(0, 50);
                this.divAnswer.InnerText = strQuestrion[intRandom % strQuestrion.Length] + " " + strAnswer[intRandom % strQuestrion.Length];
                this.ViewState["Answer"] = strAnswer[intRandom % strQuestrion.Length];
            }
            else
            {
                this.divAnswer.InnerText = set.Question + " ? " + set.Answer;
                this.ViewState["Answer"] = set.Answer;
                return;
            }
        }
        else
        {
            this.ViewState["isQuestion"] = "0";
            this.trQuestion.Visible = false;
        }
    }


    private bool bind()
    {
        try
        {
            string IP = Base.GetRequest.GetIP();
            int outReturn = 0;
            int intIP = Solely.UserRegistr.CheckIP(IP);
            Solely.UserRegistr set = Solely.UserRegistr.GateItem();
            this.ViewState["intGradeID"] = set.VipGrade;
            if (!set.isRegister)   //是否允许注册
            {
                //ShowMessage("没有开放会员注册功能", Config.WebURL);
                Response.Redirect("../Error.aspx?err=0");
                return false;
            }
            else
            {
                if (outReturn == -1)  //IP是否允许注册
                {
                    ShowMessage("您的IP在此段时间已经注册过请过会儿在注册", Config.WebURL);
                    return false;
                }
                else
                {
                    ViewState.Add("BannedUser", set.BannedUser);
                    ViewState.Add("BreakTime", set.MinTimeRegister);
                    ViewState.Add("Minimum", set.Minimum);
                    ViewState.Add("Longest", set.Longest);
                    this.HidminName.Value = set.Minimum.ToString();
                    this.HidmaxName.Value = set.Longest.ToString();
                    return true;
                }
            }
        }
        catch (Exception e)
        {
            ShowMessage(e.Message);
        }
        return true;
    }

    private void IndustryBind()
    {
        IIndustryType II = new RIndustryType();
        DataTable db = II.GetDataTable(0);
        dropIndustry.DataSource = db.DefaultView;
        dropIndustry.DataTextField = "Type";
        dropIndustry.DataValueField = "ID";
        dropIndustry.DataBind();

    }

    protected void btnServer_Click(object sender, EventArgs e)
    {
        string msgText = "";
        string code = this.yangzheng.Text;
        string testString = this.txtLoginName.Text;
        //验证码
        if (this.ViewState["isCode"].ToString() == "1")
        {
            if (!CheckCode(code))   //验证码检测
            {
                ShowMessage("验证码错误！");
                return;
            }
        }

        //验证答案
        if (this.ViewState["isQuestion"].ToString() == "1")
        {
            if (this.txtQuestion.Text != this.ViewState["Answer"])
            {
                ShowMessage("验证问题不正确！");
                return;
            }
        }
        int min = 4;
        int max = 12;
        min = int.Parse(this.ViewState["Minimum"].ToString());
        max = int.Parse(this.ViewState["Longest"].ToString());
        if ( (testString.Length < min) || (testString.Length > max))  //(Base.Common.IsIncludeChineseCode(this.txtLoginName.Text.Trim()) ||
        {
            msgText =  min + "～" + max + "位之间";
        }
        if (!this.txtPassword.Text.Trim().Equals(this.txtOldPassword.Text))
        {
            msgText = msgText + "<br/>两次密码输入不一致";
        }
        string noUser = "";
        noUser = this.ViewState["BannedUser"].ToString();
        if (noUser.IndexOf(this.txtLoginName.Text) > 0)    //会员名检测
        {
            ShowMessage("不允许注册的会员名");
            return;
        }
        if (msgText != "")
        {
            ShowMessage(msgText, Config.WebURL);
        }
        else
        {
            SaveUserInfo();
        }

    }

    private void SaveUserInfo()
    {
        try
        {
            TUserInfo TUser = new TUserInfo();
            TUser.LoginName = this.txtLoginName.Text;
            string Md5Pawd = Common.Encryption(this.txtPassword.Text);
            TUser.Password = Md5Pawd;
            TUser.Email = this.txtEmail.Text;
            TUser.Question = this.dropQuestion.SelectedValue;
            TUser.Answer = this.txtAnswer.Text;
            TUser.LinkName = this.txtLinkName.Text;
            TUser.Sex = this.radlSex.SelectedValue.ToString();
            TUser.Department = this.txtDepartment.Text;
            TUser.Position = this.txtPosition.Text;
            TUser.Tel_International = this.txtTel_International.Text;
            TUser.Tel_DistrictNumber = this.txtTel_DistrictNumber.Text;
            TUser.Tel_Telephone = this.txtTel_Telephone.Text;
            TUser.Tel_Ext = this.txtTel_Ext.Text;
            TUser.Fax_International = this.txtFax_International.Text;
            TUser.Fax_DistrictNumber = this.txtFax_DistrictNumber.Text;
            TUser.Fax_Telephone = this.txtFax_Telephone.Text;
            TUser.Fax_Ext = this.txtFax_Ext.Text;
            TUser.MobilePhone = this.txtMobilePhone.Text;
            TUser.QQ = this.txtQQ.Text;
            TUser.MSN = this.txtMSN.Text;
            TUser.CompanyNature = this.radlCompanyNature.SelectedValue;
            TUser.CompanyName = this.txtCompanyName.Text;
            TUser.DealinAdd = this.txtDealinAdd.Text;
            TUser.Industry = this.dropIndustry.SelectedValue;
            TUser.RegDate = Convert.ToDateTime(DateTime.Now.Date);
            TUser.Country = Request.Form["country"];
            TUser.Province = Request.Form["province"];
            TUser.City = Request.Form["city"];
            TUser.Area = Request.Form["county"];
            TUser.GradeID = 1;
            IUserInfo RUser = new RUserInfo();
            RUser.Insert(TUser);
            //读取指定节点下的value值
            string strFlag = Base.XMLProcess.Read("xml/EmailFlag.xml", "/Item/flag/mode[@type='userRegister']", "value");
            if (strFlag == "")
                strFlag = "us_register";
            if (Base.Common.isMail(txtEmail.Text.Trim()))
                nspBase.Core.clsEmail.SendMail(strFlag, txtEmail.Text.Trim());
            DataSet tableset = RUser.GetDataTable(TUser.LoginName, TUser.Password);
            GetRequest.SetCookie("LoginName", tableset.Tables[0].Rows[0]["LoginName"].ToString());
            GetRequest.SetCookie("CheckInfo", tableset.Tables[0].Rows[0]["Password"].ToString());
            GetRequest.SetCookie("UserID", tableset.Tables[0].Rows[0]["UserID"].ToString());
            GetRequest.SetCookie("GradeID", tableset.Tables[0].Rows[0]["GradeID"].ToString());
            GetRequest.SetCookie("CompanyName", tableset.Tables[0].Rows[0]["CompanyName"].ToString());
            //ShowMessage("注册成功！", "../Default.aspx");
            Response.Redirect("Success.aspx");
        }
        catch (Exception ex)
        {
            ShowMessage("注册失败！");
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/member/Index.aspx");
    }


}
