using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Threading;
using System.Web.UI.WebControls;
public partial class Organization_Settings_Organization_EmployeeFlexiPopup : System.Web.UI.Page
{
    #region Global variable Declaration
    ResourceManager rm;
    CultureInfo ci;
    int intCount;
    bool CheckGrid = false;
    cls_Common objClassCommon = new cls_Common();
    string gstrDateFormat;
    int isFlexible = 0;
    #endregion

    #region Page Load Event
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Session_ActiveLanguage"] == null)
        {
            lblErr.Text = rm.GetString("Message_SessionExpired", ci);
            btnFlexi.Visible = true;
            btnNonFlexi.Visible = true;
        }
        else
        {
            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;
            aspnetForm.Attributes.CssStyle.Add("Direction", rm.GetString("Direction", ci));
            LoadStrings(ci);
            loadcss();
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            if (!IsPostBack)
            {
                Session["SearchBut"] = 0;
                Session["TotalSwipes"] = 0;
                btnFlexi.OnClientClick = "return confirm('" + rm.GetString("Alert_AreyousuretoSetFlexible", ci) + "');";
                btnNonFlexi.OnClientClick = "return confirm('" + rm.GetString("Alert_AreyousuretoSetNonFlexible", ci) + "');";
                string EmpId = "";
                string EmpName = "";
                int EmpCategory = 0;
                int EmpType = 0;
                string FromDate = "";
                string ToDate = "";
                string Level = "";
                Rpt_Common rptcommon = new Rpt_Common();
                Level = rptcommon.getNodes(Convert.ToInt16(Request.QueryString["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
                org_levelsEventData getOrg = new org_levelsEventData();
                getOrg.GetOrgParentLevels_FromID(Convert.ToInt16(Request.QueryString["parentLevelId"]));
                DataSet ds_getOrg = new DataSet();
                ds_getOrg = getOrg.GetOrgParentLevels();
                int iCompanyId = 0;
                if (ds_getOrg != null)
                {
                    if (ds_getOrg.Tables[0].Rows.Count > 0)
                    {
                        iCompanyId = Convert.ToInt32(ds_getOrg.Tables[0].Rows[0]["LevelID"].ToString());
                    }
                }
                int iFlagFlexi = 0;
                int iFlagFlexiPattern = 0; // 1 - Organization, 2 - Individual Employee , 0 - no flexi
                ShiftSettingsEventData getShift = new ShiftSettingsEventData();
                DataSet dsShift = new DataSet();
                dsShift = getShift.GetShiftSettingsByComId(iCompanyId);
                if (dsShift != null && dsShift.Tables.Count > 0 && dsShift.Tables[0].Rows.Count > 0)
                {
                    if (dsShift.Tables[0].Rows[0]["IsFlexible"] != DBNull.Value)
                    {
                        if (Convert.ToString(dsShift.Tables[0].Rows[0]["IsFlexible"]) == "True")
                        {
                            iFlagFlexi = 1;
                            if (dsShift.Tables[0].Rows[0]["FlexibleFlag"] != DBNull.Value)
                            {
                                if (Convert.ToString(dsShift.Tables[0].Rows[0]["FlexibleFlag"]) == "1")
                                {
                                    iFlagFlexiPattern = 1; // organization wise
                                }
                                if (Convert.ToString(dsShift.Tables[0].Rows[0]["FlexibleFlag"]) == "2")
                                {
                                    iFlagFlexiPattern = 2; // employee wise
                                }
                            }
                        }
                    }
                }
                if (iFlagFlexi == 0 || iFlagFlexiPattern == 1)
                {
                    btnFlexi.Visible = true;
                    btnNonFlexi.Visible = true;
                }
                if (Request.QueryString["EmpId"] != null && Convert.ToString(Request.QueryString["EmpId"]) != "")
                {
                    EmpId = Convert.ToString(Request.QueryString["EmpId"]);
                }
                if (Request.QueryString["isFlexible"] != null && Convert.ToString(Request.QueryString["isFlexible"]) != "")
                {
                    isFlexible = Convert.ToInt16(Request.QueryString["isFlexible"]);
                }
                if (Request.QueryString["EmpName"] != null && Convert.ToString(Request.QueryString["EmpName"]) != "")
                {
                    EmpName = Convert.ToString(Request.QueryString["EmpName"]);
                }
                if (Request.QueryString["EmpCategory"] != null && Convert.ToString(Request.QueryString["EmpCategory"]) != "")
                {
                    EmpCategory = Convert.ToInt16(Request.QueryString["EmpCategory"]);
                }
                if (Request.QueryString["EmpType"] != null && Convert.ToString(Request.QueryString["EmpType"]) != "")
                {
                    EmpType = Convert.ToInt16(Request.QueryString["EmpType"]);
                }
                if (Request.QueryString["FromDate"] != null && Convert.ToString(Request.QueryString["FromDate"]) != "")
                {
                    FromDate = Convert.ToString(Request.QueryString["FromDate"]);
                    FromDate = objClassCommon.DateConvertion(gstrDateFormat, FromDate);
                }
                if (Request.QueryString["ToDate"] != null && Convert.ToString(Request.QueryString["ToDate"]) != "")
                {
                    ToDate = Convert.ToString(Request.QueryString["ToDate"]);
                    ToDate = objClassCommon.DateConvertion(gstrDateFormat, ToDate);
                }
                GetReports rptEmp = new GetReports();
                DataSet ds = new DataSet();
                FlexiEventData objClass = new FlexiEventData();
                ds = objClass.SelectEmployees(FromDate, ToDate, EmpId, EmpName, Level, EmpCategory, EmpType, 3, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Request.QueryString["parentLevelId"]), isFlexible, Convert.ToInt32(Session["EmpAutoID"]));
                if (ds != null && ds.Tables.Count > 0)
                {
                    grd.DataSource = ds;
                    grd.DataBind();
                    ViewState["dtEmployee"] = ds.Tables[0];
                    Session["TotalEmployees"] = Convert.ToInt32(ds.Tables[0].Rows.Count);
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        string strScript = "<script language='javascript' type='text/javascript'>";
                        strScript += "alert('" + rm.GetString("Alert_No_Employees_Found", ci) + "');self.close();";
                        strScript += " </script>";
                        this.RegisterStartupScript("closeScript", strScript);
                        // added by gopinath for the mantis bug no.27800
                        divFlexiPopup.Style.Add("Display", "none");
                        divModuleHeading.Style.Add("Display", "none");
                    }
                }
                if (iFlagFlexiPattern == 1) // organization wise
                {
                    btnNonFlexi.Enabled = true;
                    btnFlexi.Enabled = true;
                    btnSearch.Enabled = false;
                    ddlChoice.SelectedIndex = 0;
                    ddlChoice.Enabled = false;
                    grd.Columns[5].Visible = false;
                }
                if (iFlagFlexiPattern == 2)// employee wise
                {
                    ddlChoice.SelectedIndex = 0;
                    ddlChoice.Enabled = true;
                    btnNonFlexi.Enabled = true;
                    btnFlexi.Enabled = true;
                    btnSearch.Enabled = true;
                    grd.Columns[6].Visible = true;
                }
                Session["EmpIDs"] = "";
            }
        }
    }
    #endregion

    #region User Defined Functions
    private void LoadStrings(CultureInfo ci)
    {
        btnFlexi.Text = rm.GetString("Flexible", ci);
        btnNonFlexi.Text = rm.GetString("NonFlexible", ci);
        btnSearch.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Search", ci);
        grd.Columns[2].HeaderText = rm.GetString("email_EmployeeName", ci);
        grd.Columns[3].HeaderText = rm.GetString("email_EmployeeId", ci);
        grd.Columns[4].HeaderText = rm.GetString("EmployeeMaster_Designation", ci);
        grd.Columns[5].HeaderText = rm.GetString("Level", ci);
        grd.Columns[6].HeaderText = rm.GetString("FlexibleHours", ci);
        lblFlexibleHoursApplicability.Text = rm.GetString("Caption_FlexibleHoursApplicability", ci);
        ddlChoice.Items[0].Text = rm.GetString("ExportData_All", ci);
        ddlChoice.Items[1].Text = rm.GetString("Flexible", ci);
        ddlChoice.Items[2].Text = rm.GetString("NonFlexible", ci);
        lblSelect.Text = rm.GetString("Caption_Select", ci);
    }
    protected string GetEmployeeIdsInOtherPagesOfGrid()
    {
        DataTable dtSwipes = new DataTable();
        dtSwipes = (DataTable)ViewState["dtEmployee"];
        string strEmpId = string.Empty;
        string ChkBoxIDs = string.Empty;
        foreach (DataRow dr in dtSwipes.Rows)
        {
            foreach (GridViewRow gr in grd.Rows)
            {
                strEmpId = ((Label)gr.FindControl("lblEID")).Text.ToString();
                CheckBox chkEnabled = (CheckBox)gr.FindControl("deletethis");
                if (chkEnabled.Enabled == false)
                {
                    goto A;
                }
                if (strEmpId == dr["empAutoID"].ToString())
                {
                    goto A;
                }
            }
            if (ChkBoxIDs == "")
            {
                ChkBoxIDs = dr["empAutoID"].ToString();
            }
            else
            {
                ChkBoxIDs = ChkBoxIDs + "," + dr["empAutoID"].ToString();
            }
        A: ;
        }
        return ChkBoxIDs;
    }
    public void BindGrid()
    {
        string EmpId = "";
        string EmpName = "";
        int EmpCategory = 0;
        int EmpType = 0;
        string FromDate = "";
        string ToDate = "";
        string Level = "";
        Rpt_Common rptcommon = new Rpt_Common();
        Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
        if (Request.QueryString["EmpId"] != null && Convert.ToString(Request.QueryString["EmpId"]) != "")
        {
            EmpId = Convert.ToString(Request.QueryString["EmpId"]);
        }
        if (Request.QueryString["EmpName"] != null && Convert.ToString(Request.QueryString["EmpName"]) != "")
        {
            EmpName = Convert.ToString(Request.QueryString["EmpName"]);
        }
        if (Request.QueryString["EmpCategory"] != null && Convert.ToString(Request.QueryString["EmpCategory"]) != "")
        {
            EmpCategory = Convert.ToInt16(Request.QueryString["EmpCategory"]);
        }
        if (Request.QueryString["EmpType"] != null && Convert.ToString(Request.QueryString["EmpType"]) != "")
        {
            EmpType = Convert.ToInt16(Request.QueryString["EmpType"]);
        }
        if (Request.QueryString["FromDate"] != null && Convert.ToString(Request.QueryString["FromDate"]) != "")
        {
            FromDate = Convert.ToString(Request.QueryString["FromDate"]);
            FromDate = objClassCommon.DateConvertion(gstrDateFormat, FromDate);
        }
        if (Request.QueryString["ToDate"] != null && Convert.ToString(Request.QueryString["ToDate"]) != "")
        {
            ToDate = Convert.ToString(Request.QueryString["ToDate"]);
            ToDate = objClassCommon.DateConvertion(gstrDateFormat, ToDate);
        }
        GetReports rptEmp = new GetReports();
        DataSet ds = new DataSet();
        FlexiEventData objClass = new FlexiEventData();
        ds = objClass.SelectEmployees(FromDate, ToDate, EmpId, EmpName, Level, EmpCategory, EmpType, 3, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]), isFlexible, Convert.ToInt32(Session["EmpAutoID"]));
        if (ds != null && ds.Tables.Count > 0)
        {
            ViewState["dtEmployee"] = ds.Tables[0];
            grd.DataSource = ds;
            grd.DataBind();
            CheckBox chkSelect = new CheckBox();
            chkSelect = (CheckBox)grd.HeaderRow.Cells[0].FindControl("checkall");
            chkSelect.Checked = Convert.ToBoolean(Session["SelectAllStatus"].ToString());
        }
    }
    public void CheckSwipes_CheckBox()
    {
        string strEmpId = "";
        strEmpId = (string)Session["EmpIDs"];
        int intEmployeeIdsLength = strEmpId.Length;
        char[] Delimit = { ',' };
        string[] Empid = strEmpId.Split(Delimit);
        CheckBox deleteChkBxItem = new CheckBox();
        foreach (GridViewRow gr in grd.Rows)
        {
            string empId = ((Label)gr.FindControl("lblEID")).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["EmpIDs"] = strEmpId;
                    goto D;
                }
            }
        D: ;
        }
    }
    private void Message(string messageContent)
    {
        string strScript = "<script language='javascript' type='text/javascript'>";
        strScript += "alert('" + messageContent + "');";
        strScript += " </script>";
        this.RegisterStartupScript("setScript", strScript);
    }
    protected void SearchBindGrid()
    {
        string EmpId = "";
        string EmpName = "";
        int EmpCategory = 0;
        int EmpType = 0;
        string FromDate = "";
        string ToDate = "";
        string Level = "";
        Rpt_Common rptcommon = new Rpt_Common();
        Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
        if (Request.QueryString["EmpId"] != null && Convert.ToString(Request.QueryString["EmpId"]) != "")
        {
            EmpId = Convert.ToString(Request.QueryString["EmpId"]);
        }
        if (Request.QueryString["EmpName"] != null && Convert.ToString(Request.QueryString["EmpName"]) != "")
        {
            EmpName = Convert.ToString(Request.QueryString["EmpName"]);
        }
        if (Request.QueryString["EmpCategory"] != null && Convert.ToString(Request.QueryString["EmpCategory"]) != "")
        {
            EmpCategory = Convert.ToInt16(Request.QueryString["EmpCategory"]);
        }
        if (Request.QueryString["EmpType"] != null && Convert.ToString(Request.QueryString["EmpType"]) != "")
        {
            EmpType = Convert.ToInt16(Request.QueryString["EmpType"]);
        }
        if (Request.QueryString["FromDate"] != null && Convert.ToString(Request.QueryString["FromDate"]) != "")
        {
            FromDate = Convert.ToString(Request.QueryString["FromDate"]);
            FromDate = objClassCommon.DateConvertion(gstrDateFormat, FromDate);
        }
        if (Request.QueryString["ToDate"] != null && Convert.ToString(Request.QueryString["ToDate"]) != "")
        {
            ToDate = Convert.ToString(Request.QueryString["ToDate"]);
            ToDate = objClassCommon.DateConvertion(gstrDateFormat, ToDate);
        }
        GetReports rptEmp = new GetReports();
        DataSet ds = new DataSet();
        FlexiEventData objClass = new FlexiEventData();
        int iOpt = 0;
        if (ddlChoice.SelectedIndex == 0) // select All
        {
            iOpt = 3;
        }
        else if (ddlChoice.SelectedIndex == 1) // select flexible
        {
            iOpt = 1;
        }
        else // select non flexible
        {
            iOpt = 0;
        }
        ds = objClass.SelectEmployees(FromDate, ToDate, EmpId, EmpName, Level, EmpCategory, EmpType, iOpt, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]), isFlexible, Convert.ToInt32(Session["EmpAutoID"]));
        if (ds != null && ds.Tables.Count > 0)
        {
            ViewState["dtEmployee"] = ds.Tables[0];
            grd.DataSource = ds;
            grd.DataBind();
            if (ds.Tables[0].Rows.Count > 0)
            {
                btnNonFlexi.Enabled = true;
                btnFlexi.Enabled = true;
                grd.PageIndex = 0;
            }
            else
            {
                btnNonFlexi.Enabled = false;
                btnFlexi.Enabled = false;
                string strScript = "<script language='javascript' type='text/javascript'>";
                strScript += "alert('" + rm.GetString("Alert_No_Employees_Found", ci) + "');";
                strScript += " </script>";
                this.RegisterStartupScript("closeScript", strScript);
            }
        }
    }

    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
        htmlFlexi.Attributes.Add("dir", rm.GetString("Direction", ci));
        if (source_Language == "Resources.Resource_AR")
        {
            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";
        }
        else
        {
            //htmlFlexi.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.Href = "../bootstrap/css/bootstrap.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min.css";
            stylecustom.Href = "../dist/css/custom.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
    }

    #endregion

    #region Gridview Events
    protected void grv_CheckAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkSelect = new CheckBox();
        chkSelect = (CheckBox)grd.HeaderRow.Cells[0].FindControl("checkall");
        string ChkBoxIDs = string.Empty;
        if (chkSelect.Checked)
        {
            ChkBoxIDs = GetEmployeeIdsInOtherPagesOfGrid();
            if (Convert.ToString(Session["EmpIDs"]) == "")
            {
                Session["EmpIDs"] = ChkBoxIDs;
            }
            else
            {
                if (ChkBoxIDs != "")
                {
                    Session["EmpIDs"] = Session["EmpIDs"] + "," + ChkBoxIDs;
                }
            }
        }
        else
        {
            Session["EmpIDs"] = string.Empty;
        }
        foreach (GridViewRow gr in grd.Rows)
        {
            chkSelect = (CheckBox)gr.Cells[0].FindControl("deletethis");
            chkSelect.Checked = ((CheckBox)sender).Checked;
        }
    }
    protected void grv_Check_CheckedChanged(object sender, EventArgs e)
    {
        string ChkBoxIDs = string.Empty;
        int intCount = 0;
        int intGridPageSize = grd.PageSize;
        int intGridTotalPages = grd.PageCount;
        bool boolAllChecked = false;
        CheckBox chkSelect = new CheckBox();
        CheckBox chkSelectAll = new CheckBox();
        chkSelectAll = (CheckBox)grd.HeaderRow.Cells[0].FindControl("checkall");
        if (((CheckBox)sender).Checked == false)
        {
            chkSelectAll.Checked = false;
        }
        else
        {
            foreach (GridViewRow gr in grd.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["TotalSwipes"].ToString()) > intGridPageSize)
            {
                if (intCount == intGridPageSize)
                {   //All checkbox are checked in the current page
                    if (Session["EmpIDs"] != null || Session["EmpIDs"].ToString() != string.Empty)
                    {   //some checkbox in other pages is also checked
                        ChkBoxIDs = GetEmployeeIdsInOtherPagesOfGrid();
                        char[] Delimit = { ',' };
                        string[] SessionEmployeeIds = Session["EmpIDs"].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)
                        {   //All checkBox Checked
                            chkSelectAll.Checked = true;
                        }
                    }
                }
                else if (grd.PageIndex == intGridTotalPages - 1)
                {   //Last Page
                    int intLastPageCount = Convert.ToInt32(Session["TotalEmployees"].ToString()) - (intGridPageSize * (intGridTotalPages - 1));
                    if (intCount == intLastPageCount)
                    {
                        if (Session["EmpIDs"] != null || Session["EmpIDs"].ToString() != string.Empty)
                        {   //some checkbox in other pages is also checked
                            ChkBoxIDs = GetEmployeeIdsInOtherPagesOfGrid();
                            char[] Delimit = { ',' };
                            string[] SessionEmployeeIds = Session["EmpIDs"].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)
                            {   //All checkBox Checked
                                chkSelectAll.Checked = true;
                            }
                        }
                    }
                }
            }
        }
    }
    protected void grd_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grd.EnableViewState = true;
        string ChkBoxIDs = "";
        CheckBox deleteChkBxItem = new CheckBox();
        foreach (GridViewRow i in grd.Rows)
        {
            deleteChkBxItem = (CheckBox)i.Cells[0].FindControl("deletethis");
            if (deleteChkBxItem.Checked)
            {
                if (ChkBoxIDs == "")
                {
                    ChkBoxIDs = ((Label)i.FindControl("lblEID")).Text.ToString();
                }
                else
                {
                    ChkBoxIDs = ChkBoxIDs + "," + ((Label)i.FindControl("lblEID")).Text.ToString();
                }
            }
        }
        if (Session["EmpIDs"].ToString() == "")
        {
            Session["EmpIDs"] = ChkBoxIDs;
        }
        else
        {
            if (ChkBoxIDs != "")
            {
                Session["EmpIDs"] = Session["EmpIDs"] + "," + ChkBoxIDs;
            }
        }
        CheckBox chkSelect = new CheckBox();
        chkSelect = (CheckBox)grd.HeaderRow.Cells[0].FindControl("checkall");
        Session["SelectAllStatus"] = chkSelect.Checked;
        grd.PageIndex = e.NewPageIndex;
        if (Convert.ToInt16(Session["SearchBut"]) == 1)
        {
            SearchBindGrid();
        }
        else
        {
            BindGrid();
        }
        CheckSwipes_CheckBox();
    }
    protected void grd_PageIndexChanged(object sender, EventArgs e)
    {
    }
    protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblFlex = (Label)e.Row.Cells[1].FindControl("lblFlexi");
            if (lblFlex.Text == "True")
            {
                lblFlex.ForeColor = System.Drawing.Color.Blue;
                lblFlex.Text = rm.GetString("ShiftSetting_Yes", ci);
            }
            else
            {
                lblFlex.Text = rm.GetString("ShiftSetting_No", ci);
            }
        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //Label lblAll = (Label)e.Row.Cells[1].FindControl("Label1");
            //lblAll.Text = rm.GetString("ExportData_All", ci);
        }
    }
    #endregion

    #region Button Click Events
    protected void btnFlexi_Click(object sender, EventArgs e)
    {
        FlexiEventData objClass = new FlexiEventData();
        int intLoopCounter = 0;
        int intClkID = 0;
        string strEmployeeName = string.Empty;
        string strRemarks = string.Empty;
        DataTable dtSwipes = new DataTable();
        dtSwipes = (DataTable)ViewState["dtEmployee"];
        string strSwipeIDs = Convert.ToString(Session["EmpIDs"]);
        char[] Delimit = { ',' };
        string[] SwipeIDs = strSwipeIDs.Split(Delimit);
        CheckBox chkSelect = new CheckBox();
        CheckBox chkSelectAll = new CheckBox();
        intCount = dtSwipes.Rows.Count;
        chkSelectAll = (CheckBox)grd.HeaderRow.Cells[0].FindControl("checkall");
        if (chkSelectAll.Checked)
        {
            StringWriter strWriteXML = new StringWriter();
            dtSwipes.WriteXml(strWriteXML);
            objClass.UpdateFlexi_All(1, strWriteXML.ToString());
            CheckGrid = true;
        }
        else
        {
            foreach (GridViewRow gr in grd.Rows)
            {
                chkSelect = (CheckBox)gr.Cells[0].FindControl("deletethis");
                if (chkSelect.Checked)
                {
                    string lblClkID = ((Label)gr.FindControl("lblEID")).Text.ToString();
                    for (intLoopCounter = 0; intLoopCounter < intCount; intLoopCounter++)
                    {
                        if (dtSwipes.Rows[intLoopCounter]["empAutoID"].ToString() == lblClkID)
                        {
                            intClkID = Convert.ToInt32(dtSwipes.Rows[intLoopCounter]["empAutoID"]);
                            objClass.UpdateFlexi(intClkID, 1);
                            CheckGrid = true;
                        }
                    }
                }
            }
            foreach (string EmpCId in SwipeIDs)
            {
                for (intLoopCounter = 0; intLoopCounter < intCount; intLoopCounter++)
                {
                    if (dtSwipes.Rows[intLoopCounter]["empAutoID"].ToString() == EmpCId)
                    {
                        intClkID = Convert.ToInt32(dtSwipes.Rows[intLoopCounter]["empAutoID"].ToString());
                        objClass.UpdateFlexi(intClkID, 1);
                        CheckGrid = true;
                    }
                }
            }
        }
        if (CheckGrid == false)
        {
            Message(rm.GetString("Alert_SelectAnyRecord", ci));
        }
        else
        {
            string strScript = "<script language='javascript' type='text/javascript'>";
            strScript += "alert('" + rm.GetString("Alert_Flexible_flag_Updated_Successfully", ci) + "');self.close();";
            strScript += " </script>";
            this.RegisterStartupScript("closeScript", strScript);
        }
    }
    protected void btnNonFlexi_Click(object sender, EventArgs e)
    {
        FlexiEventData objClass = new FlexiEventData();
        bool CheckGrid = false;
        int intLoopCounter = 0;
        int intClkID = 0;
        string strEmployeeName = string.Empty;
        string strRemarks = string.Empty;
        DataTable dtSwipes = new DataTable();
        dtSwipes = (DataTable)ViewState["dtEmployee"];
        string strSwipeIDs = Convert.ToString(Session["EmpIDs"]);
        char[] Delimit = { ',' };
        string[] SwipeIDs = strSwipeIDs.Split(Delimit);
        CheckBox chkSelect = new CheckBox();
        CheckBox chkSelectAll = new CheckBox();
        intCount = dtSwipes.Rows.Count;
        chkSelectAll = (CheckBox)grd.HeaderRow.Cells[0].FindControl("checkall");
        if (chkSelectAll.Checked)
        {
            StringWriter strWriteXML = new StringWriter();
            dtSwipes.WriteXml(strWriteXML);
            string strXMLData = strWriteXML.ToString();
            objClass.UpdateFlexi_All(0, strXMLData);
            CheckGrid = true;
        }
        else
        {
            foreach (GridViewRow gr in grd.Rows)
            {
                chkSelect = (CheckBox)gr.Cells[0].FindControl("deletethis");
                if (chkSelect.Checked)
                {
                    string lblClkID = ((Label)gr.FindControl("lblEID")).Text.ToString();
                    for (intLoopCounter = 0; intLoopCounter < intCount; intLoopCounter++)
                    {
                        if (dtSwipes.Rows[intLoopCounter]["empAutoID"].ToString() == lblClkID)
                        {
                            intClkID = Convert.ToInt32(dtSwipes.Rows[intLoopCounter]["empAutoID"]);
                            objClass.UpdateFlexi(intClkID, 0);
                            CheckGrid = true;
                        }
                    }
                }
            }
            foreach (string EmpCId in SwipeIDs)
            {
                for (intLoopCounter = 0; intLoopCounter < intCount; intLoopCounter++)
                {
                    if (dtSwipes.Rows[intLoopCounter]["empAutoID"].ToString() == EmpCId)
                    {
                        intClkID = Convert.ToInt32(dtSwipes.Rows[intLoopCounter]["empAutoID"].ToString());
                        objClass.UpdateFlexi(intClkID, 0);
                        CheckGrid = true;
                    }
                }
            }
        }
        if (CheckGrid == false)
        {
            Message(rm.GetString("Alert_SelectAnyRecord", ci));
        }
        else
        {
            string strScript = "<script language='javascript' type='text/javascript'>";
            strScript += "alert('" + rm.GetString("Alert_Flexible_flag_Updated_Successfully", ci) + "');self.close();";
            strScript += " </script>";
            this.RegisterStartupScript("closeScript", strScript);
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["SearchBut"] = 1;
        SearchBindGrid();
    }
    #endregion
}
