using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        divPageMessage.Visible = false;

        string strReturnUrl=
            Request.Params["ReturnUrl"];

        if (string.IsNullOrEmpty(strReturnUrl)==false)
        {
            string strFileName =
                System.IO.Path.GetFileName(strReturnUrl);

            string strErrorMessage =
                string.Format("The {0} Page Needs Authentication.Please Login For . . .", strFileName);

            divPageMessage.Visible = true;
            litPageMessage.Text = strErrorMessage;
        }

        if (Page.IsPostBack==false)
        {
            Initialize();
        }
    }

    private void Initialize()
    { }

    protected virtual void DisplayErrorMessage(string message)
    {
        divPageMessage.Visible = true;
        litPageMessage.Text =
            string.Format("<div class ='error'>{0}</div>",message);
    }

    protected virtual void DisplayWarningMessage(string message)
    {
        divPageMessage.Visible = true;
        litPageMessage.Text =
            string.Format("<div class ='warning'>{0}</div>", message);
    }

    protected virtual void DisplayInformationMessage(string message)
    {
        divPageMessage.Visible = true;
        litPageMessage.Text =
            string.Format("<div class ='information'>{0}</div>", message);
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            DataSet.UsersDataTable oUserDataTable =
                new DataSet.UsersDataTable();

            DataSetTableAdapters.UsersTableAdapter oUserTableAdapter =
                new DataSetTableAdapters.UsersTableAdapter();

            oUserTableAdapter.FillUserByUserName(oUserDataTable, txtUserName.Text);

            if (oUserDataTable.Count!=1)
            {
                string strErrorMessage =
                    "UserName Or Password Is Not Correct ! Please Try Again . . . ";
                DisplayErrorMessage(strErrorMessage);
                return;
            }

            DataSet.UsersRow oUserRow = oUserDataTable[0];

            if (string.Compare(oUserRow.Password.Trim(),txtPassword.Text.Trim(),false)!=0)
            {
                 string strErrorMessage =
                    "UserName Or Password Is Not Correct ! Please Try Again . . . ";
                 DisplayErrorMessage(strErrorMessage);
                return;
            }

            if (oUserRow.IsUserActive==false)
            {
                string strInformationMessage =
                    string.Format("Dear {0} You Should Not Login At This Time , Please Contact Support",txtUserName.Text);
                DisplayInformationMessage(strInformationMessage);
                return;
            }

       

            Session["UserId"] = oUserRow.UserId;
            Session["Username"] = oUserRow.UserName;

            System.Web.Security.FormsAuthentication.RedirectFromLoginPage(oUserRow.UserName, false);

            
         

           
        }
    }
}