using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Web.Security;
using NikaiDSR;
using BusinessLayer;
using DTO;

namespace NikaiDSR
{
    public partial class ChangePassword : PageBase
    {

        #region code declaration
        UserManager UM = new UserManager();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            PageTitle = "NIKAI - CHANGE PASSWORD";

        }

        #region code btnSubmit_Command
        protected void btnSubmit_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "save")
            {

                if (ValidateInput())
                {
                   
                    if (UM.ChangePassword(base.UserID, txtNewPass.Text))
                    {
                        lblMsg.Text = "";
                        base.ShowMessage(lblMsg, "Password changed successfully.", Helper.Common.ERROR_MESSAGE_TYPE.SUCCESS);
                        btnSubmit.Visible = false;
                        Session.Abandon(); // Remove session and redirect to login page
                        FormsAuthentication.SignOut();
                        string redirectionScript = "<script language='javascript'>Delayer()</script>";
                        String csname = "Startup";
                        Type cstype = this.GetType();
                        ClientScript.RegisterStartupScript(cstype, csname, redirectionScript);

                    }
                    else
                        base.ShowMessage(lblMsg, "Cannot change Password", Helper.Common.ERROR_MESSAGE_TYPE.ERROR); // Error occured updation 
                }
            }
        }
        #endregion

        #region Validate Details
        private bool ValidateInput()
        {
            bool Flag = true;
            string Msg = "";
            int msgId = 0;

            //if (!objCU.ValidateControlsAtServer(this, "MainContent_txtOldpass,MainContent_txtNewPass,MainContent_txtConPass", ref Msg)) // check for all text boxes have values 
            //{                    
            //    msgId = 3;              
            //    Flag = false;
            //}
            if (txtNewPass.Text.Trim() != txtConPass.Text.Trim())
            {
                Msg = "Password and Confirm password does not match";  // // Password & Confirm Password not matching 
                Flag = false;
            }

            if (Password != txtOldpass.Text.Trim())
            {
                Msg = "Old Passowrd does not match"; // Existing password entered is wrong 
                Flag = false;
            }

            if (Password == txtNewPass.Text)
            {
                Msg = "Old password and New password cannot be same"; // // Existing password and new password is same 
                Flag = false;
            }

            if (Flag == false)
                base.ShowMessage(lblMsg, Msg, Helper.Common.ERROR_MESSAGE_TYPE.ERROR);
            return Flag;
        }

        #endregion
    }
}