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 System.Resources;
using System.Globalization;
using System.Threading;
using System.IO;
public partial class Transactions_TA_UI_Transactions_GroupLeavePolicy_Popup : Base.BasePage
{
    #region Variables.
    ResourceManager rm;
    CultureInfo ci;
    LeaveEventData objLeave = new LeaveEventData();
    EmployeeEventData objEmployeeEventData = new EmployeeEventData();
    GroupPolicyAllocationEventData obj = new GroupPolicyAllocationEventData();
    string Catagory = string.Empty, LeavePolicy = string.Empty, Type = string.Empty, EmployeeName = string.Empty, EmployeeId = string.Empty;
    bool checkRecordExists, checkLevel;
    int LevelID, loginEmployeeId, count = 0;
    DataSet ds = new DataSet();
    #endregion
    #region Page_Load.
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        checkRecordExists = true;

        if (Session["Session_ActiveLanguage"] == null)
        {
            string str = "<script language=\"javascript\">";
            str = str + "opener.focus();";
            str = str + "opener.location.href = opener.location + '?fl=1&sid=" + Session.SessionID + "';";
            str = str + "self.close();";
            str = str + "</script>";
            Response.Write(str);
        }
        else
        {
            string source_Language;
            loginEmployeeId = Int32.Parse(Session["EmpAutoID"].ToString());
            source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            aspnetForm.Attributes.CssStyle.Add("Direction", rm.GetString("Direction", ci));
            LoadStrings(ci);
        }
        if (!IsPostBack)
        {
            fillLeavePolicyddl();
            FillDesignation();
            btn_done1.Visible = false;
            loadcss();
        }
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
        strSetAlert = strSetAlert + "SelectLeavePolicy('" + rm.GetString("Alert_SelectLeavePolicy", ci) + "');";
        strSetAlert = strSetAlert + "SetAlertMessages_DeleteRestore('" + rm.GetString("Alert_ConfirmDelete") + "','" + rm.GetString("Alert_SelectItemsToDelete") + "','" + rm.GetString("Alert_ConfirmDeletePermanent") + "','" + rm.GetString("Alert_Areyousuretorestore") + "','" + rm.GetString("Alert_SelectItemsToRestore") + "','" + rm.GetString("Alert_SelectItemToForwarded") + "','" + rm.GetString("Alert_SelectItemToApproved") + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
    }

    private void loadcss()
    {
        CultureInfo ci;
        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;
        //if Standard Demo
        htmlSelectpop.Attributes.Add("dir", rm.GetString("Direction", ci));
        if (source_Language == "Resources.Resource_AR")
        {
            stylebootstrap.Attributes.Add("Href", "../bootstrap/css/bootstrap-rtl.min.css");
            stylefontawesome.Attributes.Add("Href", "../bootstrap/font-awesome/css/font-awesome-rtl.css");
            styleAdminLTE.Attributes.Add("Href", "../dist/css/AdminLTE.min-rtl.css");
            stylecustom.Attributes.Add("Href", "../dist/css/custom-rtl.css");
            stylefonts.Attributes.Add("Href", "../dist/css/fonts.css");
            styleAutocomp.Attributes.Add("Href", "../bootstrap/css/autoCompleteNew-rtl.css");
            styleselect2.Attributes.Add("Href", "../plugins/select2/select2.min-rtl.css");
        }
        else
        {
            //LevelView.Attributes.Add("dir", "rtl");
            //stylebootstrap.Href = "../bootstrap/css/bootstrap-rtl.min.css";
            //stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome-rtl.css";
            //styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            //stylecustom.Href = "../dist/css/custom-rtl.css";
            //stylefonts.Href = "../dist/css/fonts.css";
            stylebootstrap.Attributes.Add("Href", "../bootstrap/css/bootstrap.min.css");
            stylefontawesome.Attributes.Add("Href", "../bootstrap/font-awesome/css/font-awesome.css");
            styleAdminLTE.Attributes.Add("Href", "../dist/css/AdminLTE.min.css");
            stylecustom.Attributes.Add("Href", "../dist/css/custom.css");
            stylefonts.Attributes.Add("Href", "../dist/css/fonts.css");
            styleAutocomp.Attributes.Add("Href", "../bootstrap/css/autoCompleteNew.css");
            styleselect2.Attributes.Add("Href", "../plugins/select2/select2.min.css");
        }
    }

    #endregion
    #region PolicyDataBind.
    public void PolicyDataBind()
    {
        LevelID = Convert.ToInt32(Session["currentSession"]);
        Rpt_Common rptcommon = new Rpt_Common();
        string Level = "";
        if (LevelID != 0)
        {
            Level = rptcommon.getNodes(LevelID, Convert.ToString(Session["LevelRights"]));
            lblError.Text = "";
        }
        else
        {
            grv_Shift.DataSourceID = "";
            grv_Shift.DataBind();
            divShift.Visible = false;
            btn_done1.Visible = false;
            Message(rm.GetString("Alert_Selectallorganizationlevels", ci));
            //lblError.Text = rm.GetString("Alert_Selectallorganizationlevels", ci);
            checkLevel = true;
            checkRecordExists = false;
            return;
        }
        if (ddlLeavePolicy.Text.Equals("0"))
        {
            LeavePolicy = null;
        }
        else
        {
            Int32 companyID = 0;
            LeavePolicy = ddlLeavePolicy.SelectedItem.Text.ToString();
            if (LeavePolicy != "")
            {
                companyID = objEmployeeEventData.ValidateSelectedLeavePolicy(LeavePolicy);
                if (companyID != LevelID)
                {
                    Message("Select Valid Leave Policy Name");
                    return;
                }
            }

        }
        string strGPLeavePolicy = null;
        if (Session["GPLeavePolicy"] != null)
        {
            strGPLeavePolicy = Session["GPLeavePolicy"].ToString();
        }
        int isNotAllocated = 0;
        if (rbtnNotAllocated.Checked) isNotAllocated = 1;
        ds = objEmployeeEventData.Select_EmployeeForGroupPolicy(Convert.ToInt32(Session["EmpAutoId"]), Level, Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), (Convert.ToInt32(ddlDesignation.SelectedValue) > 0) ? Convert.ToString(ddlDesignation.SelectedValue) : null, strGPLeavePolicy, LevelID, Convert.ToString(txtEmpCodeID.Text), Convert.ToString(txtEmployeeName.Text), isNotAllocated);
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                grv_Shift.DataSource = ds.Tables[0];
                grv_Shift.DataBind();
                Session["TotalEmployees"] = ds.Tables[0].Rows.Count;
                //grv_Shift.Visible = true;
                divShift.Visible = true;
                btn_done1.Visible = true;
            }
            else
            {
                btn_done1.Visible = false;
                checkRecordExists = false;
            }
        }
        else
        {
            checkRecordExists = false;
        }
        if (checkRecordExists == true)
        {
            CheckBox chkSelect = new CheckBox();
            chkSelect = (CheckBox)grv_Shift.HeaderRow.Cells[0].FindControl("checkall");
            chkSelect.Checked = Convert.ToBoolean(Session["SelectAllStatus"].ToString());
        }
    }
    #endregion
    #region CheckEmployeeId_CheckBox.
    public void CheckEmployeeId_CheckBox()
    {
        string strEmpId = "";
        strEmpId = (string)Session["EmployeeIds"];
        int intEmployeeIdsLength = strEmpId.Length;
        char[] Delimit = { ',' };
        string[] Empid = strEmpId.Split(Delimit);
        CheckBox deleteChkBxItem = new CheckBox();
        foreach (GridViewRow gr in grv_Shift.Rows)
        {
            string empId = ((Label)gr.FindControl("lbl_EmpId")).Text.ToString();
            foreach (string str in Empid)
            {
                if (str == empId)
                {
                    deleteChkBxItem = (CheckBox)gr.Cells[0].FindControl("deletethis");
                    deleteChkBxItem.Checked = true;
                    int intLength = empId.Length;
                    if (intEmployeeIdsLength > intLength)
                    {
                        int intIndex = strEmpId.IndexOf("," + str + ",");
                        if (intIndex != -1)
                        {
                            strEmpId = strEmpId.Remove(intIndex, (intLength + 1));
                        }
                        else
                        {
                            if (strEmpId.IndexOf(str + ",") != -1 && strEmpId.IndexOf(str + ",") == 0)
                            {
                                strEmpId = strEmpId.Remove(0, (intLength + 1));
                            }
                            else if (strEmpId.IndexOf("," + str) != -1 && (strEmpId.IndexOf("," + str) + intLength + 1) == intEmployeeIdsLength)
                            {
                                strEmpId = strEmpId.Substring(0, (intEmployeeIdsLength - (intLength + 1)));
                            }
                        }
                    }
                    else
                    {
                        strEmpId = strEmpId.Replace(empId, "");
                    }
                    intEmployeeIdsLength = strEmpId.Length;
                    Session["EmployeeIds"] = strEmpId;
                    goto D;
                }
            }
            D:;
        }
    }
    #endregion
    #region grv_Shift_PageIndexChanging.
    protected void grv_Shift_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Shift.EnableViewState = true;
        string ChkBoxIDs = "";
        CheckBox deleteChkBxItem = new CheckBox();
        foreach (GridViewRow i in grv_Shift.Rows)
        {
            deleteChkBxItem = (CheckBox)i.Cells[0].FindControl("deletethis");
            if (deleteChkBxItem.Checked)
            {
                if (ChkBoxIDs == "")
                {
                    ChkBoxIDs = ((Label)i.FindControl("lbl_EmpId")).Text.ToString();
                }
                else
                {
                    ChkBoxIDs = ChkBoxIDs + "," + ((Label)i.FindControl("lbl_EmpId")).Text.ToString();
                }
            }
        }
        if (Session["EmployeeIds"].ToString() == "")
        {
            Session["EmployeeIds"] = ChkBoxIDs;
        }
        else
        {
            if (ChkBoxIDs != "")
            {
                Session["EmployeeIds"] = Session["EmployeeIds"] + "," + ChkBoxIDs;
            }
        }
        CheckBox chkSelect = new CheckBox();
        chkSelect = (CheckBox)grv_Shift.HeaderRow.Cells[0].FindControl("checkall");
        Session["SelectAllStatus"] = chkSelect.Checked;
        grv_Shift.PageIndex = e.NewPageIndex;
        PolicyDataBind();
        CheckEmployeeId_CheckBox();
    }
    #endregion
    #region btn_done1_Click.
    protected void btn_done1_Click(object sender, EventArgs e)
    {
        if (Session["currentSession"] != null && Convert.ToString(Session["currentSession"]) != string.Empty)
        {
            int id = Convert.ToInt16(ddlLeavePolicy.SelectedItem.Value);
            Session["PolicyID"] = id;
            org_levelsEventData getOrg = new org_levelsEventData();
            getOrg.GetOrgParentLevels_FromID(Convert.ToInt32(Session["currentSession"]));
            DataSet ds_getOrg = new DataSet();
            ds_getOrg = getOrg.GetOrgParentLevels();
            if (ds_getOrg != null)
            {
                if (ds_getOrg.Tables[0].Rows.Count > 0)
                {
                    int companyId = Convert.ToInt32(ds_getOrg.Tables[0].Rows[0]["LevelID"].ToString());
                    if (Convert.ToInt32(Session["previousCompanyId"]) == companyId)
                    {
                        if (Session["GPLeavePolicy"] != string.Empty && Session["GPLeavePolicy"] != null)
                        {
                            if (Convert.ToString(Session["OldLeavePolicy"]) != Convert.ToString(Session["GPLeavePolicy"]))
                            {
                                int gintUserId = Int32.Parse(Session["EmpAutoID"].ToString());
                                obj.DeleteEmpNameGroupPolicy(gintUserId);
                            }
                        }
                        InsertEmployeeDetails();
                        Session["CheckRecordsAddInPopUP"] = "TEst";
                    }
                    else
                    {
                        if (Session["GPLeavePolicy"] != string.Empty && Session["GPLeavePolicy"] != null)
                        {
                            if (Convert.ToString(Session["OldLeavePolicy"]) != Convert.ToString(Session["GPLeavePolicy"]))
                            {
                                int gintUserId = Int32.Parse(Session["EmpAutoID"].ToString());
                                obj.DeleteEmpNameGroupPolicy(gintUserId);
                            }
                        }
                        InsertEmployeeDetails();
                        Session["CheckRecordsAddInPopUP"] = "TEst";
                    }
                    Session["OldLeavePolicy"] = Session["GPLeavePolicy"];
                }
            }
        }
        else
        {
        }
        DataSet ds = new DataSet();
        ds = obj.SelectEmployeeDetails(loginEmployeeId);
        Session["EmpDetails"] = ds;

        string str = "<script language=\"javascript\">";
        str = str + "window.top.location.href = window.top.location.href + '?fl=1&sid=" + Session.SessionID + "';";
        str = str + "parent.$('#myModal').modal('hide');";
        str = str + "</script>";
        Session["GroupLeavePolicyAvailable"] = "Allowed";
        Response.Write(str);
    }



    #endregion
    #region InsertEmployeeDetails.
    protected void InsertEmployeeDetails()
    {
        int intEmployeeId = 0;
        int intLoopCounter = 0;
        string strEmployeeCode = string.Empty;
        string strEmployeeName = string.Empty;
        string strEmployeeDateOfJoining = string.Empty;
        string strLevelID = string.Empty;
        string strEmployeeIds = Session["EmployeeIds"].ToString();
        char[] Delimit = { ',' };
        string[] EmployeeIds = strEmployeeIds.Split(Delimit);
        LevelID = Convert.ToInt32(Session["currentSession"]);
        Rpt_Common rptcommon = new Rpt_Common();
        string Level = "";
        if (LevelID != 0)
            Level = rptcommon.getNodes(LevelID, Convert.ToString(Session["LevelRights"]));
        string strLeavePolicy = null;
        if (Session["GPLeavePolicy"] != null)
        {
            strLeavePolicy = Session["GPLeavePolicy"].ToString();
        }
        int isNotAllocated = 0;
        if (rbtnNotAllocated.Checked) isNotAllocated = 1;
        ds = objEmployeeEventData.Select_EmployeeForGroupPolicy(Convert.ToInt32(Session["EmpAutoId"]), Level, Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), null, strLeavePolicy, LevelID, Convert.ToString(txtEmpCodeID.Text), Convert.ToString(txtEmployeeName.Text), isNotAllocated);
        int intCount = ds.Tables[0].Rows.Count;
        CheckBox chkSelect = new CheckBox();
        CheckBox chkSelectAll = new CheckBox();
        chkSelectAll = (CheckBox)grv_Shift.HeaderRow.Cells[0].FindControl("checkall");
        if (chkSelectAll.Checked)
        {
            if (ds.Tables[0].Rows.Count == grv_Shift.Rows.Count)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    intEmployeeId = Convert.ToInt32(dr["Employee_Id"].ToString());
                    strEmployeeCode = dr["Employee_Code_Id"].ToString();
                    strEmployeeName = dr["Employee Name"].ToString();
                    strEmployeeDateOfJoining = Convert.ToDateTime(dr["DateOfJoining"]).ToString("yyyy/MM/dd");
                    strLevelID = dr["LevelID"].ToString();
                    obj.InsertEmpDetails(intEmployeeId, strEmployeeCode, strEmployeeName.Replace("'", "''"), loginEmployeeId, strEmployeeDateOfJoining, Convert.ToInt32(strLevelID));
                    count += 1;
                }
            }
            else
            {
                foreach (GridViewRow gr in grv_Shift.Rows)
                {
                    chkSelect = (CheckBox)gr.Cells[0].FindControl("deletethis");
                    if (chkSelect.Checked)
                    {
                        string strEmpCodeId = ((Label)gr.FindControl("lbl_EmpId")).Text.ToString();
                        for (intLoopCounter = 0; intLoopCounter < intCount; intLoopCounter++)
                        {
                            if (ds.Tables[0].Rows[intLoopCounter]["Employee_Code_Id"].ToString() == strEmpCodeId)
                            {
                                intEmployeeId = Convert.ToInt32(ds.Tables[0].Rows[intLoopCounter]["Employee_Id"].ToString());
                                strEmployeeCode = ds.Tables[0].Rows[intLoopCounter]["Employee_Code_Id"].ToString();
                                strEmployeeName = ds.Tables[0].Rows[intLoopCounter]["Employee Name"].ToString();
                                strEmployeeDateOfJoining = Convert.ToDateTime(ds.Tables[0].Rows[intLoopCounter]["DateOfJoining"]).ToString("yyyy/MM/dd");
                                strLevelID = ds.Tables[0].Rows[intLoopCounter]["LevelID"].ToString();
                                obj.InsertEmpDetails(intEmployeeId, strEmployeeCode, strEmployeeName.Replace("'", "''"), loginEmployeeId, strEmployeeDateOfJoining, Convert.ToInt32(strLevelID));
                                count += 1;
                            }
                        }
                    }
                }
                foreach (string EmpCId in EmployeeIds)
                {
                    for (intLoopCounter = 0; intLoopCounter < intCount; intLoopCounter++)
                    {
                        if (ds.Tables[0].Rows[intLoopCounter]["Employee_Code_Id"].ToString() == EmpCId)
                        {
                            intEmployeeId = Convert.ToInt32(ds.Tables[0].Rows[intLoopCounter]["Employee_Id"].ToString());
                            strEmployeeCode = ds.Tables[0].Rows[intLoopCounter]["Employee_Code_Id"].ToString();
                            strEmployeeName = ds.Tables[0].Rows[intLoopCounter]["Employee Name"].ToString();
                            strEmployeeDateOfJoining = Convert.ToDateTime(ds.Tables[0].Rows[intLoopCounter]["DateOfJoining"]).ToString("yyyy/MM/dd");
                            strLevelID = ds.Tables[0].Rows[intLoopCounter]["LevelID"].ToString();
                            obj.InsertEmpDetails(intEmployeeId, strEmployeeCode, strEmployeeName.Replace("'", "''"), loginEmployeeId, strEmployeeDateOfJoining, Convert.ToInt32(strLevelID));
                            count += 1;
                        }
                    }
                }
            }
        }
        else
        {
            foreach (GridViewRow gr in grv_Shift.Rows)
            {
                chkSelect = (CheckBox)gr.Cells[0].FindControl("deletethis");
                if (chkSelect.Checked)
                {
                    string strEmpCodeId = ((Label)gr.FindControl("lbl_EmpId")).Text.ToString();
                    for (intLoopCounter = 0; intLoopCounter < intCount; intLoopCounter++)
                    {
                        if (ds.Tables[0].Rows[intLoopCounter]["Employee_Code_Id"].ToString() == strEmpCodeId)
                        {
                            intEmployeeId = Convert.ToInt32(ds.Tables[0].Rows[intLoopCounter]["Employee_Id"].ToString());
                            strEmployeeCode = ds.Tables[0].Rows[intLoopCounter]["Employee_Code_Id"].ToString();
                            strEmployeeName = ds.Tables[0].Rows[intLoopCounter]["Employee Name"].ToString();
                            strEmployeeDateOfJoining = Convert.ToDateTime(ds.Tables[0].Rows[intLoopCounter]["DateOfJoining"]).ToString("yyyy/MM/dd");
                            strLevelID = ds.Tables[0].Rows[intLoopCounter]["LevelID"].ToString();
                            obj.InsertEmpDetails(intEmployeeId, strEmployeeCode, strEmployeeName.Replace("'", "''"), loginEmployeeId, strEmployeeDateOfJoining, Convert.ToInt32(strLevelID));
                            count += 1;
                        }
                    }
                }
            }
            foreach (string EmpCId in EmployeeIds)
            {
                for (intLoopCounter = 0; intLoopCounter < intCount; intLoopCounter++)
                {
                    if (ds.Tables[0].Rows[intLoopCounter]["Employee_Code_Id"].ToString() == EmpCId)
                    {
                        intEmployeeId = Convert.ToInt32(ds.Tables[0].Rows[intLoopCounter]["Employee_Id"].ToString());
                        strEmployeeCode = ds.Tables[0].Rows[intLoopCounter]["Employee_Code_Id"].ToString();
                        strEmployeeName = ds.Tables[0].Rows[intLoopCounter]["Employee Name"].ToString();
                        strEmployeeDateOfJoining = Convert.ToDateTime(ds.Tables[0].Rows[intLoopCounter]["DateOfJoining"]).ToString("yyyy/MM/dd");
                        strLevelID = ds.Tables[0].Rows[intLoopCounter]["LevelID"].ToString();
                        obj.InsertEmpDetails(intEmployeeId, strEmployeeCode, strEmployeeName.Replace("'", "''"), loginEmployeeId, strEmployeeDateOfJoining, Convert.ToInt32(strLevelID));
                    }
                }
            }
        }
        if (Session["GPLeavePolicy"] == Session["OldLeavePolicy"])
        {
            if (count == 0)
            {
                Session["GPLeavePolicy"] = null;
            }
        }
    }
    #endregion
    #region butGo_Click.
    protected void butGo_Click(object sender, EventArgs e)
    {
        Session["SelectAllStatus"] = "False";
        Session["EmployeeIds"] = "";
        Session["currentSession"] = Session["parentLevelId"];
        if (ddlLeavePolicy.SelectedItem.Text != "-Select-")
        {
            Session["GPLeavePolicy"] = ddlLeavePolicy.SelectedItem.Text.ToString();
        }
        else
        {
            Session["GPLeavePolicy"] = null;
        }
        Session["parentLevelId"] = null;
        PolicyDataBind();
        if (checkLevel == true)
        {
            return;
        }
        if (checkRecordExists == true)
        {
            btn_done1.Visible = true;
        }
        else
        {
            btn_done1.Visible = false;
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("Display Message ShiftAllocationAdd", strMsg);
            divShift.Visible = false;
            //grv_Shift.Visible = false;
            return;
        }
    }
    #endregion
    #region LoadStrings.
    private void LoadStrings(CultureInfo ci)
    {
        butGo.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Go", ci);
        btn_done1.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Ok", ci);
        grv_Shift.Columns[1].HeaderText = rm.GetString("email_EmployeeId", ci);
        grv_Shift.Columns[2].HeaderText = rm.GetString("email_EmployeeName", ci);
        grv_Shift.Columns[3].HeaderText = rm.GetString("EmployeeMaster_DateOfJoining", ci);
        lblLeavePolicy.Text = rm.GetString("Employee_LeavePolicy", ci);
        lblDesignation.Text = rm.GetString("EmployeeMaster_Designation", ci);
        lblEmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lblEmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblFilter.Text = rm.GetString("lblFilter", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);

    }
    #endregion
    #region grv_CheckAll_CheckedChanged.
    protected void grv_CheckAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkSelect = new CheckBox();
        chkSelect = (CheckBox)grv_Shift.HeaderRow.Cells[0].FindControl("checkall");
        string ChkBoxIDs = string.Empty;
        if (chkSelect.Checked)
        {
            ChkBoxIDs = GetEmployeeIdsInOtherPagesOfGrid();
            if (Session["EmployeeIds"].ToString() == "")
            {
                Session["EmployeeIds"] = ChkBoxIDs;
            }
            else
            {
                if (ChkBoxIDs != "")
                {
                    Session["EmployeeIds"] = Session["EmployeeIds"] + "," + ChkBoxIDs;
                }
            }
        }
        else
        {
            Session["EmployeeIds"] = string.Empty;
        }
        foreach (GridViewRow gr in grv_Shift.Rows)
        {
            chkSelect = (CheckBox)gr.Cells[0].FindControl("deletethis");
            chkSelect.Checked = ((CheckBox)sender).Checked;
        }
    }
    #endregion
    #region grv_Check_CheckedChanged.
    protected void grv_Check_CheckedChanged(object sender, EventArgs e)
    {
        string ChkBoxIDs = string.Empty;
        int intCount = 0;
        int intGridPageSize = grv_Shift.PageSize;
        int intGridTotalPages = grv_Shift.PageCount;
        bool boolAllChecked = false;
        CheckBox chkSelect = new CheckBox();
        CheckBox chkSelectAll = new CheckBox();
        chkSelectAll = (CheckBox)grv_Shift.HeaderRow.Cells[0].FindControl("checkall");
        if (((CheckBox)sender).Checked == false)
        {
            chkSelectAll.Checked = false;
        }
        else
        {
            foreach (GridViewRow gr in grv_Shift.Rows)
            {
                chkSelect = (CheckBox)gr.Cells[0].FindControl("deletethis");
                if (chkSelect.Checked)
                    intCount = intCount + 1;
            }
            if (Convert.ToInt32(Session["TotalEmployees"].ToString()) == intCount)
            {
                chkSelectAll.Checked = true;
            }
            else if (intCount > 0 && Convert.ToInt32(Session["TotalEmployees"].ToString()) > intCount && Convert.ToInt32(Session["TotalEmployees"].ToString()) > intGridPageSize)
            {
                if (intCount == intGridPageSize)
                {
                    if (Session["EmployeeIds"] != null || Session["EmployeeIds"].ToString() != string.Empty)
                    {
                        ChkBoxIDs = GetEmployeeIdsInOtherPagesOfGrid();
                        char[] Delimit = { ',' };
                        string[] SessionEmployeeIds = Session["EmployeeIds"].ToString().Split(Delimit);
                        string[] ChedkedEmployeeIds = ChkBoxIDs.Split(Delimit);
                        Array.Sort(SessionEmployeeIds);
                        Array.Sort(ChedkedEmployeeIds);
                        if (SessionEmployeeIds.Length == ChedkedEmployeeIds.Length)
                        {
                            for (int iLoop = 0; iLoop < ChedkedEmployeeIds.Length; iLoop++)
                            {
                                if (SessionEmployeeIds[iLoop] != ChedkedEmployeeIds[iLoop])
                                {
                                    boolAllChecked = false;
                                    break;
                                }
                            }
                            boolAllChecked = true;
                        }
                        if (boolAllChecked == true)
                        {
                            chkSelectAll.Checked = true;
                        }
                    }
                }
                else if (grv_Shift.PageIndex == intGridTotalPages - 1)
                {
                    int intLastPageCount = Convert.ToInt32(Session["TotalEmployees"].ToString()) - (intGridPageSize * (intGridTotalPages - 1));
                    if (intCount == intLastPageCount)
                    {
                        if (Session["EmployeeIds"] != null || Session["EmployeeIds"].ToString() != string.Empty)
                        {
                            ChkBoxIDs = GetEmployeeIdsInOtherPagesOfGrid();
                            char[] Delimit = { ',' };
                            string[] SessionEmployeeIds = Session["EmployeeIds"].ToString().Split(Delimit);
                            string[] ChedkedEmployeeIds = ChkBoxIDs.Split(Delimit);
                            Array.Sort(SessionEmployeeIds);
                            Array.Sort(ChedkedEmployeeIds);
                            if (SessionEmployeeIds.Length == ChedkedEmployeeIds.Length)
                            {
                                for (int iLoop = 0; iLoop < ChedkedEmployeeIds.Length; iLoop++)
                                {
                                    if (SessionEmployeeIds[iLoop] != ChedkedEmployeeIds[iLoop])
                                    {
                                        boolAllChecked = false;
                                        break;
                                    }
                                }
                                boolAllChecked = true;
                            }
                            if (boolAllChecked == true)
                            {
                                chkSelectAll.Checked = true;
                            }
                        }
                    }
                }
            }
        }
    }
    #endregion
    #region GetEmployeeIdsInOtherPagesOfGrid.
    protected string GetEmployeeIdsInOtherPagesOfGrid()
    {
        LevelID = Convert.ToInt32(Session["currentSession"]);
        Rpt_Common rptcommon = new Rpt_Common();
        string Level = "";
        if (LevelID != 0)
            Level = rptcommon.getNodes(LevelID, Convert.ToString(Session["LevelRights"]));
        string strGPLeavePolicy = null;
        if (Session["GPLeavePolicy"] != null)
        {
            strGPLeavePolicy = Session["GPLeavePolicy"].ToString();
        }
        int isNotAllocated = 0;
        if (rbtnNotAllocated.Checked) isNotAllocated = 1;
        ds = objEmployeeEventData.Select_EmployeeForGroupPolicy(Convert.ToInt32(Session["EmpAutoId"]), Level, Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), null, strGPLeavePolicy, LevelID, Convert.ToString(txtEmpCodeID.Text), Convert.ToString(txtEmployeeName.Text), isNotAllocated);
        string strEmpId = string.Empty;
        string ChkBoxIDs = string.Empty;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            foreach (GridViewRow gr in grv_Shift.Rows)
            {
                strEmpId = ((Label)gr.FindControl("lbl_EmpId")).Text.ToString();
                if (strEmpId == dr["Employee_Code_Id"].ToString())
                {
                    goto A;
                }
            }
            if (ChkBoxIDs == "")
            {
                ChkBoxIDs = dr["Employee_Code_Id"].ToString();
            }
            else
            {
                ChkBoxIDs = ChkBoxIDs + "," + dr["Employee_Code_Id"].ToString();
            }
            A:;
        }
        return ChkBoxIDs;
    }
    #endregion
    protected void grv_Shift_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl = (Label)e.Row.Cells[3].FindControl("lbl_EmpDateOfJoining");
            lbl.Text = (Convert.ToDateTime(lbl.Text.ToString())).ToString(Session["MDateFormat"].ToString());
        }
        //if (e.Row.RowType == DataControlRowType.Header)
        //{
        //    Label lblCheckAll = (Label)e.Row.Cells[0].FindControl("lblCheckAll");
        //    lblCheckAll.Text = rm.GetString("ExportData_All", ci);
        //}
    }
    #region "function fills Leave policy ddl"
    private void FillDesignation()
    {
        DataSet ds_designation = new DataSet();
        ds_designation = objEmployeeEventData.select_CommonDesignation();
        if (ds_designation != null)
        {
            if (ds_designation.Tables[0].Rows.Count > 0)
            {
                ddlDesignation.DataSource = ds_designation;
                ddlDesignation.DataTextField = "TitleName";
                ddlDesignation.DataValueField = "TitleId";
                ddlDesignation.DataBind();
            }
            ds_designation.Dispose();
            foreach (ListItem item in ddlDesignation.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    private void fillLeavePolicyddl()
    {
        DataSet ds_leavepolicy = new DataSet();
        ds_leavepolicy = objLeave.SelectAllLeavePolicy(string.Empty);
        if (ds_leavepolicy != null)
        {
            if (ds_leavepolicy.Tables[0].Rows.Count > 0)
            {
                ddlLeavePolicy.DataSource = ds_leavepolicy;
                ddlLeavePolicy.DataTextField = "PolicyName";
                ddlLeavePolicy.DataValueField = "Policy_Id";
                ddlLeavePolicy.DataBind();
            }
            ListItem lstItem = new ListItem();
            lstItem.Value = "0";
            lstItem.Text = rm.GetString("Select", ci);
            ddlLeavePolicy.Items.Insert(0, lstItem);
            ds_leavepolicy.Dispose();
            foreach (ListItem item in ddlLeavePolicy.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    #endregion
}
