using ASPNET.StarterKit.TimeTracker.BusinessLogicLayer;
using System.Configuration;
using System.Web.Security;
using System;

namespace TTWebCSVS
{
	/// <summary>
	/// Summary description for Register.
	/// </summary>
	public class Register : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox Email;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
		protected System.Web.UI.WebControls.TextBox DisplayName;
		protected System.Web.UI.WebControls.TextBox Password;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
		protected System.Web.UI.WebControls.TextBox ConfirmPassword;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator4;
		protected System.Web.UI.WebControls.CompareValidator CompareValidator1;
		protected System.Web.UI.WebControls.Label Message;
		protected System.Web.UI.WebControls.LinkButton RegisterBtn;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.RegisterBtn.Click += new System.EventHandler(this.RegisterBtn_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void RegisterBtn_Click(object sender, System.EventArgs e)
		{
			// Only attempt a login if all form fields on the page are valid
			if (Page.IsValid)
			{
				//Add New User to Portal User Database
				TTUser accountSystem = new TTUser(0, Email.Text, TTSecurity.CleanStringRegex(DisplayName.Text), ConfigurationSettings.AppSettings["DefaultRoleForNewUser"]);
				accountSystem.Password = TTSecurity.Encrypt(Password.Text);

				if (accountSystem.Save())
				{
					// Set the user's authentication name to the userId
					FormsAuthentication.SetAuthCookie(Email.Text, false);

					// Redirect browser back to home page
					Response.Redirect("TimeEntry.aspx");
				}
				else
					Message.Text = "注册失败  <" + "u" + ">" + Email.Text + "<" + "/u" + "> 已存在。" + "<" + "br" + ">" + "请更换一个邮件地址来注册";
			}
		}
	}
}
