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

namespace TTMobileCSVS
{
	/// <summary>
	/// Summary description for SignIn.
	/// </summary>
	public class SignIn : System.Web.UI.MobileControls.MobilePage
	{
		protected System.Web.UI.MobileControls.Label LabelSignInTitle;
		protected System.Web.UI.MobileControls.Label Label5;
		protected System.Web.UI.MobileControls.TextBox eMailText;
		protected System.Web.UI.MobileControls.RequiredFieldValidator RequiredFieldValidatorEmail;
		protected System.Web.UI.MobileControls.RegularExpressionValidator RegularExpressionValidator1;
		protected System.Web.UI.MobileControls.Label password;
		protected System.Web.UI.MobileControls.TextBox passwordText;
		protected System.Web.UI.MobileControls.RequiredFieldValidator RequiredfieldvalidatorPassword;
		protected System.Web.UI.MobileControls.Label signInMsg;
		protected System.Web.UI.MobileControls.Form FormSignIn;
		protected System.Web.UI.MobileControls.Command Submit;

		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.Submit.Click += new System.EventHandler(this.Submit_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Submit_Click(object sender, System.EventArgs e)
		{
			TTUser accountSystem = new TTUser();
			string userId = accountSystem.Login(eMailText.Text, TTSecurity.Encrypt(passwordText.Text));

			if (userId != "" && userId != string.Empty)
			{
				// Use security system to set the UserID within a client-side Cookie
				FormsAuthentication.SetAuthCookie(eMailText.Text, false);
				// Redirect browser back to originating page
				RedirectToMobilePage("TimeSheet.aspx");
			}
			else
				signInMsg.Text = "Login Failed!";
		}
	}
}
