using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Web.UI;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                    Started Date  : 12-Aug-2007
* Module          : Change Password                              Last Modified : 14-Aug-2007
* Programmer      : Janakaraj.P
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class System_Settings_System_ChangePassword : Base.BasePage
{
    #region VariableDeclaration
    string EmployeeId, strPassword;
    int count, UId, i;
    DataSet ds_CheckUserName;
    cls_Common objcommon = new cls_Common();
    byte[] inputBytes, hashBytes;
    ResourceManager rm;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    CultureInfo ci;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            objcommon.menu("Previlege", "54");
        }
        //get the culture info to set the language
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        btn_Add.OnClientClick = "if(Page_ClientValidate()) return confirm('" + rm.GetString("Alert_ConfirmSave", ci) + "');return false;";
        //Call to a function to implement the UserRights
        UserRights();
        EmployeeId = Convert.ToString(Session["EmpAutoID"]);
        if (!Page.IsPostBack)
        {
            txt_OldPassword.Focus();
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        string strScript = "";        
        i = objcommon.CheckPassword(EmployeeId, PasswordEncryption(txt_OldPassword.Text));
        if (i != 0)
        {
            objcommon.UpdatePassword(EmployeeId, PasswordEncryption(txt_NewPassword.Text));            
            strScript = "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_Passwordchangedsuccessfully", ci) + "'); ";
            strScript = strScript + " </script>";
            Page.RegisterStartupScript("Password Change", strScript);
            clearTextbox();
        }
        else
        {            
            strScript = "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_IncorrectPassword", ci) + "'); ";
            strScript = strScript + " </script>";
            Page.RegisterStartupScript("Password Change", strScript);
            clearTextbox();
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        clearTextbox();
    }
    #endregion
    #region UserDefinedFunctions    
    public string PasswordEncryption(string EPassword)
    {
        MD5 md5 = MD5.Create();
        inputBytes = Encoding.ASCII.GetBytes(EPassword);
        hashBytes = md5.ComputeHash(inputBytes);
        EPassword = Convert.ToBase64String(hashBytes);
        return EPassword;
    }
    private void clearTextbox()
    {
        txt_OldPassword.Text = "";
        txt_NewPassword.Text = "";
        txt_ConfirmPassword.Text = "";
        txt_OldPassword.Focus();
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("SystemSettings_ChangePassword_ChangePassword", ci);
        lbl_OldPass.Text = rm.GetString("ChangePassword_OldPassword", ci);
        lbl_Newpass.Text = rm.GetString("ChangePassword_NewPassword", ci);
        lbl_ConfirmPassword.Text = rm.GetString("ChangePassword_ConfirmNewPassword", ci);
        RequiredFieldValidator2.ErrorMessage = rm.GetString("ReqField_Oldpassword", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("ReqField_Newpassword", ci);
        RequiredFieldValidator3.ErrorMessage = rm.GetString("Req_confirmpassword", ci);
        CompareValidator1.ErrorMessage = rm.GetString("ReqField_Confirmpassword", ci);
        btn_Add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        lblMandatory.Text = rm.GetString("IndicatesMandatoryFields", ci);
        lblImportant.Text = rm.GetString("lblChangepassImportant", ci);
        lblChangePassCon1.Text = rm.GetString("lblChangePassCon1", ci);
        lblChangePassCon2.Text = rm.GetString("lblChangePassCon2", ci);
        lblChangePassCon3.Text = rm.GetString("lblChangePassCon3", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(54, Convert.ToInt32(Session["userGroup_Id"]), "modifyPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("SystemLandingPage.aspx");
        }
    }
    #endregion
}