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

public partial class controls_Login : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null)
        {
            ILogin login = new digioz.Login();
            string lsMode;

            // Checks Url Referrer Session Variable
            login.RefUrlValidation(Session, Page);

            try
            {
                lsMode = Request.QueryString["mode"];

                if (lsMode == "logout")
                {
                    login.Logout(Session, Page);
                }
            }
            catch
            {
                lsMode = String.Empty;
            }

            // Check for Existing Cookie      
            bool existingCookie = login.CheckForCookie(Session, Page);

            if (existingCookie)
            {
                HttpCookie cookie = login.GetCookie(Session, Page);

                // Set Remember Me Checkbox Selection Based on Cookie Info
                string remember = cookie.Values["rememberMe"];
                Login1.RememberMeSet = Convert.ToBoolean(remember);

                // User Authentication and Login
                bool lbSuccess = login.Authentication(Session, Page, cookie);

                if (lbSuccess == true)
                {
                    login.Redirection(Session, Page);
                }
                else
                {
                    // Show error message here
                    // Currently Using System Messaging
                }
            }
        }
    }

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        string
           lsUserNameFilter = "LoginFilters" + Setting.DELIMITER + "UserName" + Setting.DELIMITER + Login1.UserName,
           lsEmailFilter = "LoginFilters" + Setting.DELIMITER + "Email" + Setting.DELIMITER + UserManagement.GetUserByUsername(Login1.UserName).Email,
           lsIPFilter = "LoginFilters" + Setting.DELIMITER + "IP" + Setting.DELIMITER + Utility.GetVisitor().IP;
        Setting
            loUserNameFilter = SettingsManager.GetSettingByName(lsUserNameFilter),
            loEmailFilter = SettingsManager.GetSettingByName(lsEmailFilter),
            loIPFilter = SettingsManager.GetSettingByName(lsIPFilter);

        if ((loUserNameFilter == null || !loUserNameFilter.Enabled) &&
            (loEmailFilter == null || !loEmailFilter.Enabled) &&
            (loIPFilter == null || !loIPFilter.Enabled))
        {
            ILogin login = new digioz.Login();

            // Retrieve Remember Me Checkbox Selection
            CheckBox cb = (CheckBox)Login1.FindControl("RememberMe");
            bool rememberMe = cb.Checked;

            // User Authentication and Login
            bool lbSuccess = login.Authentication(Session, Page, rememberMe, Login1.UserName, Login1.Password);

            if (lbSuccess == true)
            {
                UserLoggedIn();
                login.Redirection(Session, Page);
            }
            else
            {
                // Show error message here
                // Currently Using System Messaging
            }
        }
    }

    /// <summary>
    /// Logic to perform after a user has logged in
    /// </summary>
    private void UserLoggedIn()
    {
        if (Session["PromptShown"] == null)
        {
            // Signal that we need to prompt the user
            Session["PromptShown"] = "NO";
        }
    }

}
