using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Transactions_TA_UI_Transactions_DiscrepancyList : Base.BasePage
{
    #region VariableDeclaration
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    HeaderInformation hdrTable = new HeaderInformation();
    GetReports rptEmp = new GetReports();
    Rpt_Common rptcommon = new Rpt_Common();
    ResourceManager rm;
    cls_Common objCommon = new cls_Common();
    EmployeeEventData objEmployee = new EmployeeEventData();
    DiscrepancyEventData objDis = new DiscrepancyEventData();
    ShiftPatternEventData obj = new ShiftPatternEventData();
    LeaveApplicationEventData objLeave = new LeaveApplicationEventData();
    ManualEntryEventData objManualEntry = new ManualEntryEventData();
    string strFromdate, gstrDateFormat, strTodate;
    CultureInfo ci;
    int intIndividualUser = 0;
    int intMaxClkId = 0;
    DataSet dsDiscrepancy = new DataSet();
    DataSet dsDiscrepancyList = new DataSet();
    DataSet ds = new DataSet();
    PunchTypesEventdata objPunchType = new PunchTypesEventdata();
    private const string CHECKED_ITEMS = "CheckedItems";
    string strWithPunchType = string.Empty;
    string strWithOutPunchType = string.Empty;
    string strDateFormat, strTimeFormat;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        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"]);
        string TimeFormat = string.Empty;
        TimeFormat = Convert.ToString(Session["ITimeFormat"]);
        if (TimeFormat == "HH:mm")
        {
            hiddenTime.Value = "HH:mm";
        }
        else
        {
            hiddenTime.Value = "LT";
        }
        Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
        Hidden2.Value = Convert.ToString(Session["ITimeFormat"]);
        if (!Page.IsPostBack)
        {
            base.CheckPages(222);
            LoadStrings(ci);
            string strSetAlert = "";
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
            strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_EntervalidTime") + "','" + rm.GetString("Msg_ChooseOption") + "');";
            strSetAlert = strSetAlert + "SetAlertCheckDate('" + rm.GetString("Alert_Validdatemonthfourdigit") + "','" + rm.GetString("Alert_Invalidyearentry") + "','" + rm.GetString("Alert_Invalidmonthentry") + "','" + rm.GetString("Alert_Invaliddateentry") + "','" + rm.GetString("Alert_IncorrectdateformatDDMONYYYY") + "','" + rm.GetString("Alert_Entertime") + "','" + rm.GetString("Alert_Invalidvalueforhours") + "','" + rm.GetString("Alert_Invalidvalueforminites") + "','" + rm.GetString("Alert_Invalidtimeformat1") + "','" + rm.GetString("Alert_PleaseentervalidDateandTime") + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMM") + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMMTT") + "','" + rm.GetString("Alert_PleaseentervalidDateandTime") + "','" + rm.GetString("Alert_Invalidvalueforhours") + "','" + rm.GetString("Alert_Invalidvalueforminites") + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
            UserRights();
            objCommon.menu("ManualTransaction", "222");
            BindingEmployeeCategoryandType();
            fillShift();
            txtFromDate.Text = (Convert.ToDateTime(System.DateTime.Now.ToShortDateString())).ToString(Session["MDateFormat"].ToString());
            txtToDate.Text = (Convert.ToDateTime(System.DateTime.Now.ToShortDateString())).ToString(Session["MDateFormat"].ToString());
        }
        intIndividualUser = objCheckUser.CheckIndividualUser(Convert.ToString(Session["UserGroupName"]));
        if (intIndividualUser >= 1)
        {
            PlaceHolderDiscrepancy.Controls.Clear();
            divEmployeeDetails.Visible = false;
        }
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        strFromdate = txtFromDate.Text;
        strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
        strTodate = txtToDate.Text;
        strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
    }
    #endregion
    #region UserDefinedFunctions
    private void BindingEmployeeCategoryandType()
    {
        DataSet dsCategory = new DataSet();
        dsCategory = objEmployee.SelectCategoryDetailsEmployee("");
        if (dsCategory != null)
        {
            if (dsCategory.Tables[0].Rows.Count > 0)
            {
                ddlEmpCatagory.DataSource = dsCategory;
                ddlEmpCatagory.DataTextField = "CategoryName";
                ddlEmpCatagory.DataValueField = "CategoryId";
                ddlEmpCatagory.DataBind();
            }
            dsCategory.Dispose();
            foreach (ListItem item in ddlEmpCatagory.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
        DataSet dsType = new DataSet();
        dsType = objEmployee.SelectTypeDetailsEmployee("");
        if (dsType != null)
        {
            if (dsType.Tables[0].Rows.Count > 0)
            {
                ddlEmpType.DataSource = dsType;
                ddlEmpType.DataTextField = "TypeName";
                ddlEmpType.DataValueField = "TypeId";
                ddlEmpType.DataBind();
            }
            dsType.Dispose();
            foreach (ListItem item in ddlEmpType.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    public void ValidateFromDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            strFromdate = txtFromDate.Text;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day <= 0)
            {
                e.IsValid = true;
                grd.Visible = true;
            }
            else
            {
                grd.Visible = false;
                btnSave.Visible = false;
                base.Message(rm.GetString("Alert_SelectvalidFromDate", ci));
                txtFromDate.Focus();
                e.IsValid = false;
            }
        }
        catch (Exception ex)
        {
            Message(ex.ToString());
        }
    }
    public void ValidateToDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strTodate = txtToDate.Text;
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strTodate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day <= 0)
            {
                e.IsValid = true;
                grd.Visible = true;
            }
            else
            {
                grd.Visible = false;
                btnSave.Visible = false;
                base.Message(rm.GetString("Alert_SelectvalidToDate", ci));
                txtToDate.Focus();
                e.IsValid = false;
            }
        }
        catch (Exception ex)
        {
            Message(ex.ToString());
        }
    }
    public void CompareDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            strFromdate = txtFromDate.Text;
            strTodate = txtToDate.Text;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
            DateTime dtToDate = DateTime.Parse(strTodate);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtToDate.Subtract(dtFromDate);
            int day = diff.Days;
            if (day >= 0)
            {
                e.IsValid = true;
                grd.Visible = true;
            }
            else
            {
                grd.Visible = false;
                btnSave.Visible = false;
                base.Message(rm.GetString("Msg_GrearterThanFrmDate", ci));
                e.IsValid = false;
            }
        }
        catch (Exception ex)
        {
            Message(ex.ToString());
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        btn_View.Text = "&nbsp;&nbsp;" + rm.GetString("btn_View", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        btnSave.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        lblDiscrepancyReport.Text = rm.GetString("Discrepancy_Entry", ci);
        lblEmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lblEmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblEmpCategory.Text = rm.GetString("Master_Category", ci);
        lblEmpType.Text = rm.GetString("Master_Type", ci);
        lblTimePeriod.Text = rm.GetString("Reports_TimePeriod", ci);
        lblFrom.Text = rm.GetString("email_FromDate", ci);
        lblTo.Text = rm.GetString("email_ToDate", ci);
        reqFromDate.ErrorMessage = rm.GetString("Alert_PleaseselectFromDate", ci);
        reqToDate.ErrorMessage = rm.GetString("Alert_PleaseselectToDate", ci);
        cus_ToDate.ErrorMessage = rm.GetString("Alert_SelectvalidToDate", ci);
        cus_ComToDate.ErrorMessage = rm.GetString("Custom_TodateShouldbegreaterthanFromdate", ci);
        cus_ToDate.ErrorMessage = rm.GetString("Alert_SelectvalidToDate", ci);
        grd.Columns[1].HeaderText = rm.GetString("email_EmployeeId", ci);
        grd.Columns[2].HeaderText = rm.GetString("email_EmployeeName", ci);
        grd.Columns[3].HeaderText = rm.GetString("ShiftDate", ci);
        grd.Columns[4].HeaderText = rm.GetString("Shift_Time", ci);
        grd.Columns[5].HeaderText = rm.GetString("ManualTimeEntry_InDate", ci);
        grd.Columns[6].HeaderText = rm.GetString("ManualTimeEntry_In_Time", ci);
        grd.Columns[7].HeaderText = rm.GetString("ManualTimeEntry_InPunchType", ci);
        grd.Columns[8].HeaderText = rm.GetString("ManualTimeEntry_OutDate", ci);
        grd.Columns[9].HeaderText = rm.GetString("ManualTimeEntry_Out_Time", ci);
        grd.Columns[10].HeaderText = rm.GetString("ManualTimeEntry_OutPunchType", ci);
        grd.Columns[11].HeaderText = rm.GetString("email_Reason", ci);
        grd.Columns[12].HeaderText = rm.GetString("Alert_CLK_ID", ci);
        grd.Columns[13].HeaderText = rm.GetString("Alert_DiscrepancyType", ci);
        grd.Columns[14].HeaderText = rm.GetString("Alert_ShiftStartTime", ci);
        grd.Columns[15].HeaderText = rm.GetString("Alert_ShiftEndTime", ci);
        grd.Columns[16].HeaderText = rm.GetString("Alert_ShiftInDateTime", ci);
        grd.Columns[17].HeaderText = rm.GetString("Alert_MaxHoursBeforeShift", ci);
        ddlEmpCatagory.Items[0].Text = rm.GetString("Select", ci);
        ddlEmpType.Items[0].Text = rm.GetString("Select", ci);
        lblEmpSwipe.Text = rm.GetString("TerminalDataType_Card_No", ci);
        cbDiscrepancy.Text = rm.GetString("Dash_Discrepancy", ci);
        cbAbsent.Text = rm.GetString("Report_Absent", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        lblShift.Text = rm.GetString("Menu_Reports_Shift", ci);
        lblLoading.Text = rm.GetString("ProgressBarText", ci);
        lblDataView.Text = rm.GetString("lblDataView", ci);
        lblFilter.Text = rm.GetString("lblFilter", ci);
        rbtn_WithPunchtype.Text = rm.GetString("lbl_WithPunchType", ci);
        rbtn_WithoutPunchtype.Text = rm.GetString("lbl_WithoutPunchType", ci);

    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(222, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
    }
    public void fillShift()
    {
        ShiftEvenData getShift = new ShiftEvenData();
        DataSet ds = new DataSet();
        string strusergroup = string.Empty;
        ds = getShift.SelectShift(Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["LevelRights"]), 0);
        foreach (DataRow drRow in ds.Tables[0].Rows)
        {
            switch (drRow["Shift_Name"].ToString())
            {
                case "No Shift":
                    drRow["Shift_Name"] = rm.GetString("Caption_NoShift", ci);
                    break;
            }
        }
        if (ds.Tables[0].Rows.Count > 0)
        {
            this.ddlShift.DataSource = ds;
            this.ddlShift.DataTextField = "Shift_Name";
            this.ddlShift.DataValueField = "Shift_Id";
            this.ddlShift.DataBind();
        }
        ListItem lstItem = new ListItem();
        lstItem.Value = "0";
        lstItem.Text = rm.GetString("ExportData_All", ci);
        ddlShift.Items.Insert(0, lstItem);
        foreach (ListItem item in ddlShift.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    public int getParentCompany(int iEmployeeLevelID)
    {
        org_levelsEventData getOrg = new org_levelsEventData();
        getOrg.GetOrgParentLevels_FromID(iEmployeeLevelID);
        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());
            }
        }
        return iCompanyID;
    }
    public bool CheckTimeFormat(string strValue, string strTimeFormat)
    {
        bool boolStatus = true;
        if (strTimeFormat != "HH:mm")
        {
            if (Regex.IsMatch(strValue, @"^(([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))$"))
                boolStatus = true;
            else
                boolStatus = false;
        }
        else
        {
            if (Regex.IsMatch(strValue, @"^(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?)$"))
                boolStatus = true;
            else
                boolStatus = false;
        }
        return boolStatus;
    }
    public bool CheckDateFormat(string Format, string strDateTime)
    {
        bool boolStatus = true;
        try
        {
            if (strDateTime == "")
            {
                return false;
            }
            int indexMonth = -2;
            int intMonthNoOfChars = 0;
            string strMonth = "";
            int indexMMM = Format.IndexOf(@"MMM");
            if (indexMMM == -1)
            {
                int indexMM = Format.IndexOf(@"MM");
                if (indexMM >= 0)
                {
                    indexMonth = indexMM;
                    intMonthNoOfChars = 2;
                }
            }
            else
            {
                indexMonth = indexMMM;
                intMonthNoOfChars = 3;
            }
            if (indexMonth != -2)
            {
                strMonth = strDateTime.Substring(indexMonth, intMonthNoOfChars);
            }
            int indexDate = -2;
            int intDateNoOfChars = 0;
            string strDate = "";
            int indexDD = Format.IndexOf(@"dd");
            if (indexDD >= 0)
            {
                indexDate = indexDD;
                intDateNoOfChars = 2;
            }
            if (indexDate != -2)
            {
                strDate = strDateTime.Substring(indexDate, intDateNoOfChars);
            }
            int indexYear = -2;
            int intYearNoOfChars = 0;
            string strYear = "";
            int indexYYYY = Format.IndexOf(@"yyyy");
            if (indexYYYY == -1)
            {
                int indexYY = Format.IndexOf(@"yy");
                if (indexYY >= 0)
                {
                    indexYear = indexYY;
                    intYearNoOfChars = 2;
                }
            }
            else
            {
                indexYear = indexYYYY;
                intYearNoOfChars = 4;
            }
            if (indexYear != -2)
            {
                strYear = strDateTime.Substring(indexYear, (strDateTime.Length - indexYear));
            }
            string strFinalDate = "";
            if (indexMMM >= 0)
            {
                strFinalDate = strDate + "/" + strMonth + "/" + strYear;
            }
            else
            {
                strFinalDate = strYear + "/" + strMonth + "/" + strDate;
            }
            DateTime dtmFinalDate = new DateTime();
            dtmFinalDate = DateTime.Parse(strFinalDate);
        }
        catch (Exception ex)
        {
            boolStatus = false;
        }
        return (boolStatus);
    }
    private void RePopulateValues()
    {
        ArrayList CheckedClockIdList = (ArrayList)Session[CHECKED_ITEMS];
        if (CheckedClockIdList != null && CheckedClockIdList.Count > 0)
        {
            foreach (GridViewRow gvRow in grd.Rows)
            {
                int intIndex = (int)grd.DataKeys[gvRow.RowIndex].Value;
                if (CheckedClockIdList.Contains(intIndex))
                {
                    CheckBox chkBxSelect = (CheckBox)gvRow.FindControl("deletethis");
                    chkBxSelect.Checked = true;
                }
            }
        }
    }
    private void RememberOldValues()
    {
        ArrayList CheckedClockIdList = new ArrayList();
        int intIndex = -1;
        foreach (GridViewRow gvRow in grd.Rows)
        {
            intIndex = (int)grd.DataKeys[gvRow.RowIndex].Value;
            bool bResult = ((CheckBox)gvRow.FindControl("deletethis")).Checked;
            if (Session[CHECKED_ITEMS] != null)
                CheckedClockIdList = (ArrayList)Session[CHECKED_ITEMS];
            if (bResult)
            {
                if (!CheckedClockIdList.Contains(intIndex))
                    CheckedClockIdList.Add(intIndex);
            }
            else
                CheckedClockIdList.Remove(intIndex);
        }
        if (CheckedClockIdList != null && CheckedClockIdList.Count > 0)
            Session[CHECKED_ITEMS] = CheckedClockIdList;
    }
    public bool FillDataset(int intpageIndex)
    {
        bool blnCheck = true;
        try
        {
            string NewTimeFormat = string.Empty;
            string TimeFormat = Convert.ToString(Session["ITimeFormat"]);
            dsDiscrepancyList = (DataSet)Session["DiscrepancyList"];
            foreach (GridViewRow i in grd.Rows)
            {
                Label lblClkId = (Label)i.FindControl("lblClkID");
                Label lblDiscrepancyType = (Label)i.FindControl("lblDiscrepancyType");
                TextBox txtInDate = (TextBox)i.FindControl("txtInDate");
                TextBox txtInTime = (TextBox)i.FindControl("txtInTime");
                DropDownList ddlInPunch = (DropDownList)i.FindControl("ddlInPunchType");
                TextBox txtOutDate = (TextBox)i.FindControl("txtOutDate");
                TextBox txtOutTime = (TextBox)i.FindControl("txtOutTime");
                DropDownList ddlOutPunch = (DropDownList)i.FindControl("ddlOutPunchType");
                TextBox txtReason = (TextBox)i.FindControl("txtReason");
                Label lblShiftStartTime = (Label)i.FindControl("lblShiftStartTime");
                Label lblShiftEndTime = (Label)i.FindControl("lblShiftEndTime");
                dsDiscrepancyList.Tables[0].DefaultView.RowFilter = "CLK_ID = '" + Convert.ToInt32(lblClkId.Text) + "'";
                if (dsDiscrepancyList.Tables[0].DefaultView.Count > 0)
                {
                    if (lblDiscrepancyType.Text == "I")
                    {
                        string strInDate = string.Empty;
                        string strInTime = string.Empty;
                        string strInPunchType = string.Empty;
                        if (txtInDate.Text != null && txtInDate.Text.Trim() != string.Empty)
                        {
                            bool blnCheckDate = true;
                            blnCheckDate = CheckDateFormat(gstrDateFormat, txtInDate.Text.Trim());
                            if (blnCheckDate == false)
                            {
                                strInDate = txtInDate.Text.Trim();
                                strInTime = txtInTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidDate", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtInDate");
                                        txtDateNew.Text = strInDate;
                                        txtDateNew.Focus();
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtInTime");
                                        txtTimeNew.Text = strInTime;
                                    }
                                }
                                blnCheck = false;
                                return blnCheck;
                            }
                            if (Convert.ToString(Session["MDateFormat"]) != "MM/dd/yyyy")
                            {
                                strInDate = objCommon.DateConvertion(gstrDateFormat, txtInDate.Text.Trim());
                                strInDate = (Convert.ToDateTime(strInDate)).ToString("MM/dd/yyyy");
                            }
                            else
                            {
                                strInDate = txtInDate.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["InDate"] = strInDate;
                        if ((txtInTime.Text != null) && (txtInTime.Text != string.Empty))
                        {
                            bool blnCheckTime = true;
                            blnCheckTime = CheckTimeFormat(txtInTime.Text.Trim(), Session["ITimeFormat"].ToString());
                            if (blnCheckTime == false)
                            {
                                strInDate = txtInDate.Text.Trim();
                                strInTime = txtInTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidTime", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtInDate");
                                        txtDateNew.Text = strInDate;
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtInTime");
                                        txtTimeNew.Text = strInTime;
                                        txtTimeNew.Focus();
                                    }
                                }
                                blnCheck = false;
                                return blnCheck;
                            }

                            if (Convert.ToString(Session["ITimeFormat"]) != "HH:mm:ss")
                            {
                                strInTime = (Convert.ToDateTime(txtInTime.Text.Trim().ToString())).ToString("HH:mm:ss");
                            }
                            else
                            {
                                strInTime = txtInDate.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["InTime"] = strInTime;
                        strInPunchType = ddlInPunch.SelectedItem.Text;
                        dsDiscrepancyList.Tables[0].DefaultView[0]["InPunchType"] = strInPunchType;
                    }
                    else if (lblDiscrepancyType.Text == "O")
                    {
                        string strOutDate = string.Empty;
                        string strOutTime = string.Empty;
                        string strOutPunchType = string.Empty;
                        if (txtOutDate.Text != null && txtOutDate.Text.Trim() != string.Empty)
                        {
                            bool blnCheckDate = true;
                            blnCheckDate = CheckDateFormat(gstrDateFormat, txtOutDate.Text.Trim());
                            if (blnCheckDate == false)
                            {
                                strOutDate = txtOutDate.Text.Trim();
                                strOutTime = txtOutTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidDate", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtOutDate");
                                        txtDateNew.Text = strOutDate;
                                        txtDateNew.Focus();
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtOutTime");
                                        txtTimeNew.Text = strOutTime;
                                    }
                                }
                                blnCheck = false;
                                return blnCheck;
                            }
                            if (Convert.ToString(Session["MDateFormat"]) != "MM/dd/yyyy")
                            {
                                strOutDate = objCommon.DateConvertion(gstrDateFormat, txtOutDate.Text.Trim());
                                strOutDate = (Convert.ToDateTime(strOutDate)).ToString("MM/dd/yyyy");
                            }
                            else
                            {
                                strOutDate = txtOutDate.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["OutDate"] = strOutDate;
                        if ((txtOutTime.Text != null) && (txtOutTime.Text != string.Empty))
                        {
                            bool blnCheckTime = true;
                            blnCheckTime = CheckTimeFormat(txtOutTime.Text.Trim(), Session["ITimeFormat"].ToString());
                            if (blnCheckTime == false)
                            {
                                strOutDate = txtOutDate.Text.Trim();
                                strOutTime = txtOutTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidTime", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtOutDate");
                                        txtDateNew.Text = strOutDate;
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtOutTime");
                                        txtTimeNew.Text = strOutTime;
                                        txtTimeNew.Focus();
                                    }
                                }
                                blnCheck = false;
                                return blnCheck;
                            }
                            if (Convert.ToString(Session["ITimeFormat"]) != "HH:mm:ss")
                            {
                                strOutTime = (Convert.ToDateTime(txtOutTime.Text.Trim().ToString())).ToString("HH:mm:ss");
                            }
                            else
                            {
                                strOutTime = txtOutTime.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["OutTime"] = strOutTime;
                        strOutPunchType = ddlOutPunch.SelectedItem.Text;
                        dsDiscrepancyList.Tables[0].DefaultView[0]["OutPunchType"] = strOutPunchType;
                    }
                    else if (lblDiscrepancyType.Text == "A")
                    {
                        string strInDate = string.Empty;
                        string strInTime = string.Empty;
                        string strInPunch = string.Empty;
                        if (txtInDate.Text != null && txtInDate.Text.Trim() != string.Empty)
                        {
                            bool blnCheckDate = true;
                            blnCheckDate = CheckDateFormat(gstrDateFormat, txtInDate.Text.Trim());
                            if (blnCheckDate == false)
                            {
                                strInDate = txtInDate.Text.Trim();
                                strInTime = txtInTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidDate", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtInDate");
                                        txtDateNew.Text = strInDate;
                                        txtDateNew.Focus();
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtInTime");
                                        txtTimeNew.Text = strInTime;
                                    }
                                }
                                blnCheck = false;
                                return blnCheck;
                            }
                            if (Convert.ToString(Session["MDateFormat"]) != "MM/dd/yyyy")
                            {
                                strInDate = objCommon.DateConvertion(gstrDateFormat, txtInDate.Text.Trim());
                                strInDate = (Convert.ToDateTime(strInDate)).ToString("MM/dd/yyyy");
                            }
                            else
                            {
                                strInDate = txtInDate.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["InDate"] = strInDate;
                        if ((txtInTime.Text != null) && (txtInTime.Text != string.Empty))
                        {
                            bool blnCheckTime = true;
                            blnCheckTime = CheckTimeFormat(txtInTime.Text.Trim(), Session["ITimeFormat"].ToString());
                            if (blnCheckTime == false)
                            {
                                strInDate = txtInDate.Text.Trim();
                                strInTime = txtInTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidTime", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtInDate");
                                        txtDateNew.Text = strInDate;
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtInTime");
                                        txtTimeNew.Text = strInTime;
                                        txtTimeNew.Focus();
                                    }
                                }
                                blnCheck = false;
                                return blnCheck;
                            }
                            if (Convert.ToString(Session["ITimeFormat"]) != "HH:mm:ss")
                            {
                                strInTime = (Convert.ToDateTime(txtInTime.Text.Trim().ToString())).ToString("HH:mm:ss");
                            }
                            else
                            {
                                strInTime = txtInDate.Text;
                            }
                        }
                        strInPunch = ddlInPunch.SelectedItem.Text;
                        dsDiscrepancyList.Tables[0].DefaultView[0]["InTime"] = strInTime;
                        dsDiscrepancyList.Tables[0].DefaultView[0]["InPunchType"] = strInPunch;
                        string strOutDate = string.Empty;
                        string strOutTime = string.Empty;
                        string strOutPunch = string.Empty;
                        if (txtOutDate.Text != null && txtOutDate.Text.Trim() != string.Empty)
                        {
                            bool blnCheckDate = true;
                            blnCheckDate = CheckDateFormat(gstrDateFormat, txtOutDate.Text.Trim());
                            if (blnCheckDate == false)
                            {
                                strOutDate = txtOutDate.Text.Trim();
                                strOutTime = txtOutTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidDate", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtOutDate");
                                        txtDateNew.Text = strOutDate;
                                        txtDateNew.Focus();
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtOutTime");
                                        txtTimeNew.Text = strOutTime;
                                    }
                                }
                                blnCheck = false;
                                return blnCheck;
                            }
                            if (Convert.ToString(Session["MDateFormat"]) != "MM/dd/yyyy")
                            {
                                strOutDate = objCommon.DateConvertion(gstrDateFormat, txtOutDate.Text.Trim());
                                strOutDate = (Convert.ToDateTime(strOutDate)).ToString("MM/dd/yyyy");
                            }
                            else
                            {
                                strOutDate = txtOutDate.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["OutDate"] = strOutDate;
                        if ((txtOutTime.Text != null) && (txtOutTime.Text != string.Empty))
                        {
                            bool blnCheckTime = true;
                            blnCheckTime = CheckTimeFormat(txtOutTime.Text.Trim(), Session["ITimeFormat"].ToString());
                            if (blnCheckTime == false)
                            {
                                strOutDate = txtOutDate.Text.Trim();
                                strOutTime = txtOutTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidTime", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtOutDate");
                                        txtDateNew.Text = strOutDate;
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtOutTime");
                                        txtTimeNew.Text = strOutTime;
                                        txtTimeNew.Focus();
                                    }
                                }
                                blnCheck = false;
                                return blnCheck;
                            }
                            if (Convert.ToString(Session["ITimeFormat"]) != "HH:mm:ss")
                            {
                                strOutTime = (Convert.ToDateTime(txtOutTime.Text.Trim().ToString())).ToString("HH:mm:ss");
                            }
                            else
                            {
                                strOutTime = txtOutTime.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["OutTime"] = strOutTime;
                        strOutPunch = ddlOutPunch.SelectedItem.Text;
                        dsDiscrepancyList.Tables[0].DefaultView[0]["OutPunchType"] = strOutPunch;
                    }
                    if ((txtReason.Text != null) && (txtReason.Text != string.Empty))
                    {
                        dsDiscrepancyList.Tables[0].DefaultView[0]["Reason"] = txtReason.Text;
                    }
                }
            }
            Session["DiscrepancyList"] = dsDiscrepancyList;
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
        return blnCheck;
    }
    private string GetManualEntryStatus(int EmployeeAutoId, string EmployeeCodeId)
    {
        string strManualTimeEntryStatus = "Pending";
        Int64 intReportTo = 0, intLeave_Sanc_Authority = 0;
        int manualEntryExists = 0;
        int approvalSettingsPageExists = 0;
        manualEntryExists = objCommon.CheckPageExistsInLicense(98);
        //Checking grade module is available or not and changed the id value from 110 to 228
        approvalSettingsPageExists = objCommon.CheckPageExistsInLicense(228);
        if (manualEntryExists == 1)
        {
            if (approvalSettingsPageExists == 1)
            {
                int intCompanyId = base.GetCompanyId(EmployeeAutoId);
                DataSet dsApprovalSettings = new DataSet();
                ShiftEvenData objShift = new ShiftEvenData();
                dsApprovalSettings = objShift.SelectApprovalSettings(intCompanyId);
                if (dsApprovalSettings != null)
                {
                    if (dsApprovalSettings.Tables.Count > 0)
                    {
                        if (dsApprovalSettings.Tables[0].Rows.Count > 0)
                        {
                            if (Convert.ToBoolean(dsApprovalSettings.Tables[0].Rows[0]["ManualEntry"]) == false)
                            {
                                strManualTimeEntryStatus = "Approved";
                            }
                            else
                            {
                                DataSet dsReportingPersons = new DataSet();
                                dsReportingPersons = objEmployee.GetReportingToSanctionAuthorityByCodeID(EmployeeCodeId);
                                if (dsReportingPersons.Tables[0].Rows.Count > 0)
                                {
                                    if (dsReportingPersons.Tables[0].Rows[0]["Reporting_To"] != DBNull.Value && dsReportingPersons.Tables[0].Rows[0]["Reporting_To"].ToString() != string.Empty)
                                    {
                                        intReportTo = Int64.Parse(dsReportingPersons.Tables[0].Rows[0]["Reporting_To"].ToString());
                                    }
                                    if (dsReportingPersons.Tables[0].Rows[0]["Leave_Sanc_Authority"] != DBNull.Value && dsReportingPersons.Tables[0].Rows[0]["Leave_Sanc_Authority"].ToString() != string.Empty)
                                    {
                                        intLeave_Sanc_Authority = Int64.Parse(dsReportingPersons.Tables[0].Rows[0]["Leave_Sanc_Authority"].ToString());
                                    }
                                }
                                if (intReportTo != 0 && intLeave_Sanc_Authority != 0 && intReportTo == intLeave_Sanc_Authority)
                                {
                                    strManualTimeEntryStatus = "Forwarded";
                                }
                                else if (intReportTo == 0 && intLeave_Sanc_Authority != 0)
                                {
                                    strManualTimeEntryStatus = "Forwarded";
                                }
                                else if (intReportTo != 0 && intLeave_Sanc_Authority == 0)
                                {
                                    strManualTimeEntryStatus = "Forwarded";
                                }
                                else if (intReportTo == 0 && intLeave_Sanc_Authority == 0)
                                {
                                    strManualTimeEntryStatus = "Forwarded";
                                }
                                else
                                {
                                    strManualTimeEntryStatus = "Pending";
                                }
                            }
                        }
                        else
                        {
                            return "NotDefined";
                        }
                    }
                }
            }
        }
        else
        {
            strManualTimeEntryStatus = "Approved";
        }
        return strManualTimeEntryStatus;
    }
    #endregion
    #region ButtonEvents
    protected void btn_View_Click(object sender, EventArgs e)
    {
        HiddenField1.Value = "true";
        Session[CHECKED_ITEMS] = null;
        try
        {
            if (Page.IsValid == true)
            {
                string dtFrom = "", dtTo = "";
                if (txtFromDate.Text != "")
                    dtFrom = strFromdate;
                if (txtToDate.Text != "")
                    dtTo = strTodate;
                string employeeName = string.Empty;
                string empName = string.Empty;
                string empId = string.Empty;
                if (intIndividualUser >= 1)
                {
                    empName = string.Empty;
                    empId = Convert.ToString(Session["EmployeeCodeId"]);
                }
                else
                {
                    empName = txtEmployeeName.Text.ToString();
                    empId = txtEmployeeId.Text.Trim().ToString();
                    employeeName = this.txtEmployeeName.Text.Replace("'", "''");
                    employeeName = employeeName.Replace(' ', '%');
                }
                int intCompanyId = 0;
                intCompanyId = getParentCompany(Convert.ToInt16(Session["parentLevelId"]));
                bool blnParentExists = true;
                if (Session["parentLevelId"] == null)
                {
                    blnParentExists = false;
                }
                else if (Convert.ToInt32(Session["parentLevelId"]) == 0)
                {
                    blnParentExists = false;
                }
                if (intIndividualUser <= 0)
                {
                    if (blnParentExists == false)
                    {
                        Message(rm.GetString("Alert_SelectCompany", ci));
                        btnSave.Visible = false;
                        grd.DataSource = null;
                        grd.DataBind();
                        return;
                    }
                }
                if (cbDiscrepancy.Checked == true)
                {
                    DataSet dsShiftDetails_Before = new DataSet();
                    DataSet dsShiftDetails_After = new DataSet();
                    DataSet dsShiftAfterDetails = new DataSet();
                    dsDiscrepancy = objDis.SelectDiscrepancyList(dtFrom, dtTo, empId, Convert.ToInt32(ddlShift.SelectedValue), employeeName, "0", Convert.ToInt32(ddlEmpCatagory.SelectedValue), Convert.ToInt32(ddlEmpType.SelectedValue), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), txtSwipeCard.Text.ToString().Trim().Replace("'", "''"), intCompanyId, Convert.ToInt16(Session["parentLevelId"]), Convert.ToInt32(Session["EmpAutoID"].ToString()));
                    if (dsDiscrepancy.Tables[0].Rows.Count > 0)
                    {
                        int ShiftId_Before = 0;
                        int ShiftId_After = 0;
                        DateTime dtShiftStartTime_Before = new DateTime();
                        TimeSpan tsWeeklyOffStartHours = new TimeSpan();
                        DateTime dtShiftStartTime_After = new DateTime();
                        TimeSpan tsMaxHoursBeforeShift = new TimeSpan();
                        DateTime dtClockDate = new DateTime();
                        foreach (DataRow drRow in dsDiscrepancy.Tables[0].Rows)
                        {
                            ShiftId_Before = 0;
                            ShiftId_After = 0;
                            if (drRow["Shift_Id"].ToString() == "-1")
                            {
                                dtClockDate = Convert.ToDateTime(drRow["ClockDate"]);
                                do
                                {
                                    ShiftId_Before = objDis.GetShiftIdBeforeWeekOff(Convert.ToInt32(drRow["EmployeeAutoId"]), Convert.ToString(dtClockDate));
                                    dtClockDate = dtClockDate.AddDays(-1);
                                }
                                while (ShiftId_Before == -1);
                                dtClockDate = Convert.ToDateTime(drRow["ClockDate"]);
                                do
                                {
                                    dsShiftAfterDetails = objLeave.GetShiftIdBeforeAfterWeekOff(Convert.ToInt32(drRow["EmployeeAutoId"]), dtClockDate, dtClockDate, "A");
                                    ShiftId_After = Convert.ToInt32(dsShiftAfterDetails.Tables[0].Rows[0][0]);
                                    dtClockDate = dtClockDate.AddDays(1);
                                }
                                while (ShiftId_After == -1);
                                dsShiftDetails_Before = obj.ShiftName(ShiftId_Before);
                                dsShiftDetails_After = obj.ShiftName(ShiftId_After);
                                dtShiftStartTime_Before = Convert.ToDateTime(Convert.ToDateTime(drRow["ClockDate"].ToString()).AddDays(-1).ToShortDateString() + " " + dsShiftDetails_Before.Tables[0].Rows[0]["Shift_StartTime"].ToString());
                                tsWeeklyOffStartHours = Convert.ToDateTime(drRow["WeeklyOffStartHours"]).TimeOfDay;
                                tsMaxHoursBeforeShift = Convert.ToDateTime(drRow["MaxHoursBeforeShift"]).TimeOfDay;
                                dtShiftStartTime_After = Convert.ToDateTime(Convert.ToDateTime(drRow["ClockDate"].ToString()).AddDays(1).ToShortDateString() + " " + dsShiftDetails_After.Tables[0].Rows[0]["Shift_StartTime"].ToString());
                                drRow["Shift_StartTime"] = Convert.ToDateTime(dtShiftStartTime_Before.Add(tsWeeklyOffStartHours).ToString()).ToString("hh:mm tt");
                                drRow["Shift_EndTime"] = Convert.ToDateTime(dtShiftStartTime_After.Subtract(tsMaxHoursBeforeShift).ToString()).ToString("hh:mm tt");
                                drRow["ShiftInDateTime"] = Convert.ToDateTime(dtShiftStartTime_Before.Add(tsWeeklyOffStartHours).ToString());
                            }
                        }
                    }
                    if (dsDiscrepancy.Tables[0].Rows.Count > 0)
                    {
                        intMaxClkId = Convert.ToInt32(dsDiscrepancy.Tables[0].Compute("Max(CLK_ID)", string.Empty));
                    }
                    else
                    {
                        intMaxClkId = 0;
                    }
                }
                if (cbAbsent.Checked == true)
                {
                    DataSet dsAbsentRecords = new DataSet();
                    dsAbsentRecords = objDis.SelectAbsentRecords(dtFrom, dtTo, empId, Convert.ToInt32(ddlShift.SelectedValue), employeeName, "0", Convert.ToInt32(ddlEmpCatagory.SelectedValue), Convert.ToInt32(ddlEmpType.SelectedValue), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]), txtSwipeCard.Text.ToString().Trim().Replace("'", "''"), intCompanyId, Convert.ToInt16(Session["parentLevelId"]));
                    string strShiftStartTime = string.Empty;
                    string strShiftEndTime = string.Empty;
                    string timePart1 = string.Empty;
                    string strInDateTime = string.Empty;
                    string[] strShiftInDateTime;
                    foreach (DataRow drShiftTime in dsAbsentRecords.Tables[0].Rows)
                    {
                        strShiftStartTime = Convert.ToDateTime(drShiftTime["Shift_StartTime"].ToString()).ToString("HH:mm:ss");
                        strShiftEndTime = Convert.ToDateTime(drShiftTime["Shift_EndTime"].ToString()).ToString("HH:mm:ss");
                        strShiftInDateTime = drShiftTime["ShiftInDateTime"].ToString().Split(" ".ToCharArray());
                        timePart1 = Convert.ToDateTime(strShiftInDateTime[1].ToString() + " " + strShiftInDateTime[2].ToString()).ToString("HH:mm:ss");
                        strInDateTime = strShiftInDateTime[0].ToString() + " " + timePart1;
                        drShiftTime["ShiftInDateTime"] = strInDateTime;
                        drShiftTime["InDate"] = drShiftTime["ClockDate"].ToString();
                        drShiftTime["InTime"] = drShiftTime["Shift_StartTime"].ToString();
                        drShiftTime["OutTime"] = drShiftTime["Shift_EndTime"].ToString();
                        if (Convert.ToDateTime(strShiftStartTime) > Convert.ToDateTime(strShiftEndTime))
                        {
                            drShiftTime["OutDate"] = Convert.ToDateTime(drShiftTime["ClockDate"].ToString()).AddDays(1);
                        }
                        else
                        {
                            drShiftTime["OutDate"] = drShiftTime["ClockDate"].ToString();
                        }
                    }
                    if (dsAbsentRecords.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow drAbsentRecords in dsAbsentRecords.Tables[0].Rows)
                        {
                            intMaxClkId = intMaxClkId + 1;
                            drAbsentRecords["CLK_ID"] = intMaxClkId;
                        }
                        dsDiscrepancy.Merge(dsAbsentRecords);
                    }
                }
                if (dsDiscrepancy.Tables["Table"] == null || dsDiscrepancy.Tables["Table"].Rows.Count == 0)
                {
                    btnSave.Visible = false;
                    Message(rm.GetString("Alert_Thereisnorecord", ci));
                    grd.DataSource = null;
                    grd.DataBind();
                    return;
                }
                else
                {
                    if (intIndividualUser >= 1)
                    {
                    }
                    else
                    {
                        btnSave.Visible = true;
                    }
                    Session["DiscrepancyList"] = dsDiscrepancy;
                    grd.PageIndex = 0;
                    grd.DataSource = dsDiscrepancy;
                    grd.DataBind();
                    if (rbtn_WithPunchtype.Checked == true)
                    {
                        grd.Columns[7].Visible = true;
                        grd.Columns[10].Visible = true;
                    }
                    else if (rbtn_WithoutPunchtype.Checked == true)
                    {
                        grd.Columns[7].Visible = false;
                        grd.Columns[10].Visible = false;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int intPageIndex = grd.PageIndex;
        bool blnCheck = true;
        blnCheck = FillDataset(intPageIndex);
        if (blnCheck == false)
        {
            return;
        }
        string gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        cls_Common objCommon = new cls_Common();
        int iSuccessFlag = 0;
        dsDiscrepancyList = (DataSet)Session["DiscrepancyList"];
        string NewTimeFormat = string.Empty;
        string TimeFormat = Convert.ToString(Session["ITimeFormat"]);
        if (TimeFormat == "HH:mm")
        {
            NewTimeFormat = "HH:mm:ss";
        }
        else
        {
            NewTimeFormat = "hh:mm:ss tt";
        }
        if (dsDiscrepancyList.Tables[0].Rows.Count > 0)
        {
            int intGridCount = 0;
            RememberOldValues();
            ArrayList CheckedClockIdList = (ArrayList)Session[CHECKED_ITEMS];
            foreach (DataRow drDiscrepancyList in dsDiscrepancyList.Tables[0].Rows)
            {
                if (CheckedClockIdList != null)
                {
                    if (CheckedClockIdList.Contains(Convert.ToInt32(drDiscrepancyList["CLK_ID"].ToString())) == true)
                    {
                        if (Convert.ToString(HiddenField1.Value) == "true")
                        {
                            if (drDiscrepancyList["DiscrepancyType"].ToString() == "I")
                            {
                                if (rbtn_WithPunchtype.Checked == true)
                                {
                                    if (drDiscrepancyList["InDate"].ToString() != "" && drDiscrepancyList["InTime"].ToString() != "" && drDiscrepancyList["InpunchType"].ToString() != "Select")
                                    {
                                        if (drDiscrepancyList["InpunchType"].ToString() != "Select")
                                        {
                                            string EmployeeCodeID = "";
                                            string InDate = "";
                                            string InTime = "";
                                            string InPunchType = drDiscrepancyList["InpunchType"].ToString();
                                            String[] InPunchTypes = InPunchType.Split('-');
                                            InPunchType = InPunchTypes[1].ToString();
                                            int InReaderID = 0;
                                            string OutDate = "";
                                            string OutTime = "";
                                            string OutPunchType = "";
                                            int OutReaderID = 0;
                                            int EmpID = 0;
                                            EmployeeCodeID = drDiscrepancyList["Employee_Code_Id"].ToString();
                                            InDate = drDiscrepancyList["InDate"].ToString();
                                            InTime = drDiscrepancyList["InTime"].ToString();
                                            string strOutDt = "";
                                            string strOutTi = "";
                                            strOutDt = drDiscrepancyList["OutDate"].ToString();
                                            strOutTi = drDiscrepancyList["OutTime"].ToString();
                                            string strDateFormatLocal = "MM/dd/yyyy";
                                            InDate = Convert.ToDateTime(InDate).ToString(strDateFormatLocal);
                                            DateTime dtInTime = DateTime.Parse(InDate + " " + InTime);
                                            DateTime dtOutTime = DateTime.Parse(objCommon.DateConvertion(strDateFormatLocal, strOutDt) + " " + strOutTi);
                                            string strInDateTimeForAlert = Convert.ToDateTime(InDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(InTime).ToString(NewTimeFormat);
                                            string strOutDateTimeForAlert = Convert.ToDateTime(strOutDt).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(strOutTi).ToString(NewTimeFormat);
                                            if (DateTime.Compare(dtInTime, dtOutTime) > 0)
                                            {
                                                iSuccessFlag = 0;
                                                Message(rm.GetString("Alert_InDateandTimeisgreaterthanOutDateandTimefor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            else
                                            {
                                                TimeSpan tsMaxHoursBeforeShift = TimeSpan.Parse(drDiscrepancyList["MaxHoursBeforeShift"].ToString());
                                                DateTime dtmShiftInDateTime = Convert.ToDateTime(drDiscrepancyList["ShiftInDateTime"].ToString()).Subtract(tsMaxHoursBeforeShift);
                                                string strActualShiftInDateTime = drDiscrepancyList["InDate"].ToString() + " " + drDiscrepancyList["InTime"].ToString();
                                                if (Convert.ToDateTime(strActualShiftInDateTime) < dtmShiftInDateTime)
                                                {
                                                    iSuccessFlag = 0;
                                                    Message(rm.GetString("Alert_InvalidInDateandTimeforEmpID", ci) + " " + drDiscrepancyList["Employee_Code_Id"].ToString() + " Shift Date: " + (Convert.ToDateTime(drDiscrepancyList["ClockDate"].ToString())).ToString(Session["MDateFormat"].ToString()));
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
                                                double TimeDiff = tsDiff.TotalHours;
                                                if (TimeDiff > 24)
                                                {
                                                    iSuccessFlag = 0;
                                                    Message(rm.GetString("Alert_DifferencebetweenInDateTimeandOutDateTimeshouldnotExceed24Hoursfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                                if (count > 0)
                                                {
                                                    iSuccessFlag = 0;
                                                    Message(rm.GetString("Alert_ManualEntryAlreadyhavebeenAddedforthisdurationfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                int intIsPermission = 0;
                                                intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                                if (intIsPermission > 0)
                                                {
                                                    iSuccessFlag = 0;
                                                    Message(rm.GetString("Alert_PermissionEntryispresentforthisdurationfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                            }
                                            iSuccessFlag = 1;
                                        }
                                    }
                                }
                                else
                                {
                                    if (drDiscrepancyList["InDate"].ToString() != "" && drDiscrepancyList["InTime"].ToString() != "")
                                    {
                                        string EmployeeCodeID = "";
                                        string InDate = "";
                                        string InTime = "";
                                        string InPunchType = string.Empty;
                                        int InReaderID = 0;
                                        string OutDate = "";
                                        string OutTime = "";
                                        string OutPunchType = "";
                                        int OutReaderID = 0;
                                        int EmpID = 0;
                                        EmployeeCodeID = drDiscrepancyList["Employee_Code_Id"].ToString();
                                        InDate = drDiscrepancyList["InDate"].ToString();
                                        InTime = drDiscrepancyList["InTime"].ToString();
                                        string strOutDt = "";
                                        string strOutTi = "";
                                        strOutDt = drDiscrepancyList["OutDate"].ToString();
                                        strOutTi = drDiscrepancyList["OutTime"].ToString();
                                        string strDateFormatLocal = "MM/dd/yyyy";
                                        InDate = Convert.ToDateTime(InDate).ToString(strDateFormatLocal);
                                        DateTime dtInTime = DateTime.Parse(InDate + " " + InTime);
                                        DateTime dtOutTime = DateTime.Parse(objCommon.DateConvertion(strDateFormatLocal, strOutDt) + " " + strOutTi);
                                        string strInDateTimeForAlert = Convert.ToDateTime(InDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(InTime).ToString(NewTimeFormat);
                                        string strOutDateTimeForAlert = Convert.ToDateTime(strOutDt).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(strOutTi).ToString(NewTimeFormat);
                                        if (DateTime.Compare(dtInTime, dtOutTime) > 0)
                                        {
                                            iSuccessFlag = 0;
                                            string strAlert = "alert('" + rm.GetString("Alert_InDateandTimeisgreaterthanOutDateandTimefor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                            BindGrid(0);
                                            btnSave.Focus();
                                            return;
                                        }
                                        else
                                        {
                                            TimeSpan tsMaxHoursBeforeShift = TimeSpan.Parse(drDiscrepancyList["MaxHoursBeforeShift"].ToString());
                                            DateTime dtmShiftInDateTime = Convert.ToDateTime(drDiscrepancyList["ShiftInDateTime"].ToString()).Subtract(tsMaxHoursBeforeShift);
                                            string strActualShiftInDateTime = drDiscrepancyList["InDate"].ToString() + " " + drDiscrepancyList["InTime"].ToString();
                                            if (Convert.ToDateTime(strActualShiftInDateTime) < dtmShiftInDateTime)
                                            {
                                                iSuccessFlag = 0;
                                                string strAlert = "alert('" + rm.GetString("Alert_InvalidInDateandTimeforEmpID", ci) + " " + drDiscrepancyList["Employee_Code_Id"].ToString() + " Shift Date: " + (Convert.ToDateTime(drDiscrepancyList["ClockDate"].ToString())).ToString(Session["MDateFormat"].ToString()) + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
                                            double TimeDiff = tsDiff.TotalHours;
                                            if (TimeDiff > 24)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl = "alert('" + rm.GetString("Alert_DifferencebetweenInDateTimeandOutDateTimeshouldnotExceed24Hoursfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                            if (count > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl = "alert('" + rm.GetString("Alert_ManualEntryAlreadyhavebeenAddedforthisdurationfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            int intIsPermission = 0;
                                            intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                            if (intIsPermission > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl2 = "alert('" + rm.GetString("Alert_PermissionEntryispresentforthisdurationfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                        }
                                        iSuccessFlag = 1;
                                    }
                                }
                            }
                            else if (drDiscrepancyList["DiscrepancyType"].ToString() == "O")
                            {
                                if (rbtn_WithPunchtype.Checked == true)
                                {
                                    if (drDiscrepancyList["OutTime"].ToString() != "" && drDiscrepancyList["OutDate"].ToString() != "" && drDiscrepancyList["OutPunchtype"].ToString() != "Select")
                                    {
                                        if (drDiscrepancyList["OutPunchtype"].ToString() != "Select")
                                        {
                                            string EmployeeCodeID = "";
                                            string InDate = "";
                                            string InTime = "";
                                            string InPunchType = "";
                                            int InReaderID = 0;
                                            string OutDate = "";
                                            string OutTime = "";
                                            string OutPunchType = drDiscrepancyList["OutPunchType"].ToString();
                                            string[] OutPunchTypes = OutPunchType.Split('-');
                                            OutPunchType = OutPunchTypes[1].ToString();
                                            int OutReaderID = 0;
                                            int EmpID = 0;
                                            EmployeeCodeID = drDiscrepancyList["Employee_Code_Id"].ToString();
                                            OutDate = drDiscrepancyList["OutDate"].ToString();
                                            OutTime = drDiscrepancyList["OutTime"].ToString();
                                            string strInDt = "";
                                            string strInTi = "";
                                            strInDt = drDiscrepancyList["InDate"].ToString();
                                            strInTi = drDiscrepancyList["InTime"].ToString();
                                            string strDateFormatLocal = "MM/dd/yyyy";
                                            OutDate = Convert.ToDateTime(OutDate).ToString(strDateFormatLocal);
                                            string strInDateTimeForAlert = Convert.ToDateTime(strInDt).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(strInTi).ToString(NewTimeFormat);
                                            string strOutDateTimeForAlert = Convert.ToDateTime(OutDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(OutTime).ToString(NewTimeFormat);
                                            DateTime dtOutTime = DateTime.Parse(OutDate + " " + OutTime);
                                            DateTime dtInTime = DateTime.Parse(objCommon.DateConvertion(strDateFormatLocal, strInDt) + " " + strInTi);
                                            if (DateTime.Compare(dtInTime, dtOutTime) > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAlert = "alert('" + rm.GetString("Alert_InDateandTimeisgreaterthanOutDateandTimefor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            else
                                            {
                                                TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
                                                double TimeDiff = tsDiff.TotalHours;
                                                if (TimeDiff > 24)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl = "alert('" + rm.GetString("Alert_DifferencebetweenInDateTimeandOutDateTimeshouldnotExceed24Hoursfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                                if (count > 0)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl = "alert('" + rm.GetString("Alert_ManualEntryAlreadyhavebeenAddedforthisdurationfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                int intIsPermission = 0;
                                                intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                                if (intIsPermission > 0)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl2 = "alert('" + rm.GetString("Alert_PermissionEntryispresentforthisdurationfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                            }
                                            iSuccessFlag = 1;
                                        }
                                    }
                                }
                                else
                                {
                                    if (drDiscrepancyList["OutDate"].ToString() != "" && drDiscrepancyList["OutTime"].ToString() != "")
                                    {
                                        string EmployeeCodeID = "";
                                        string InDate = "";
                                        string InTime = "";
                                        int InReaderID = 0;
                                        string OutDate = "";
                                        string OutTime = "";
                                        int OutReaderID = 0;
                                        int EmpID = 0;
                                        EmployeeCodeID = drDiscrepancyList["Employee_Code_Id"].ToString();
                                        OutDate = drDiscrepancyList["OutDate"].ToString();
                                        OutTime = drDiscrepancyList["OutTime"].ToString();
                                        string strInDt = "";
                                        string strInTi = "";
                                        strInDt = drDiscrepancyList["InDate"].ToString();
                                        strInTi = drDiscrepancyList["InTime"].ToString();
                                        string strDateFormatLocal = "MM/dd/yyyy";
                                        OutDate = Convert.ToDateTime(OutDate).ToString(strDateFormatLocal);
                                        string strInDateTimeForAlert = Convert.ToDateTime(strInDt).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(strInTi).ToString(NewTimeFormat);
                                        string strOutDateTimeForAlert = Convert.ToDateTime(OutDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(OutTime).ToString(NewTimeFormat);
                                        DateTime dtOutTime = DateTime.Parse(OutDate + " " + OutTime);
                                        DateTime dtInTime = DateTime.Parse(objCommon.DateConvertion(strDateFormatLocal, strInDt) + " " + strInTi);
                                        //   string IsNightShift = string.Empty;
                                        //  IsNightShift = FindIsNightShift(Convert.ToString(drDiscrepancyList["Employee_Code_Id"]), Convert.ToDateTime(drDiscrepancyList["ClockDate"].ToString()).Year.ToString(), Convert.ToDateTime(drDiscrepancyList["ClockDate"].ToString()).Month.ToString(), Convert.ToDateTime(drDiscrepancyList["ClockDate"].ToString()).Day.ToString());
                                        if (DateTime.Compare(dtInTime, dtOutTime) > 0)
                                        {
                                            //iSuccessFlag = 0;
                                            //string strAlert = "alert('" + rm.GetString("Alert_InDateandTimeisgreaterthanOutDateandTimefor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                            //ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                            //BindGrid(0);
                                            //btnSave.Focus();
                                            //return;
                                        }
                                        else
                                        {
                                            TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
                                            double TimeDiff = tsDiff.TotalHours;
                                            if (TimeDiff > 24)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl = "alert('" + rm.GetString("Alert_DifferencebetweenInDateTimeandOutDateTimeshouldnotExceed24Hoursfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                            if (count > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl = "alert('" + rm.GetString("Alert_ManualEntryAlreadyhavebeenAddedforthisdurationfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            int intIsPermission = 0;
                                            intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                            if (intIsPermission > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl2 = "alert('" + rm.GetString("Alert_PermissionEntryispresentforthisdurationfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                        }
                                        iSuccessFlag = 1;
                                    }
                                }
                            }
                            else if (drDiscrepancyList["DiscrepancyType"].ToString() == "A")
                            {
                                if (drDiscrepancyList["InDate"].ToString() != "" && drDiscrepancyList["InTime"].ToString() != "" && drDiscrepancyList["OutDate"].ToString() != "" && drDiscrepancyList["OutTime"].ToString() != "")
                                {
                                    if (rbtn_WithPunchtype.Checked == true)
                                    {
                                        if (drDiscrepancyList["InpunchType"].ToString() != "Select" && drDiscrepancyList["OutPunchtype"].ToString() != "Select")
                                        {
                                            string EmployeeCodeID = "";
                                            string InDate = "";
                                            string InTime = "";
                                            string InPunchType = drDiscrepancyList["InPunchType"].ToString();
                                            string[] InPunchTypes = InPunchType.Split('-');
                                            InPunchType = InPunchTypes[1].ToString();
                                            int InReaderID = 0;
                                            string OutDate = "";
                                            string OutTime = "";
                                            string OutPunchType = drDiscrepancyList["OutPunchType"].ToString();
                                            string[] OutPunchTypes = OutPunchType.Split('-');
                                            OutPunchType = OutPunchTypes[1].ToString();
                                            int OutReaderID = 0;
                                            int EmpID = 0;
                                            EmployeeCodeID = drDiscrepancyList["Employee_Code_Id"].ToString();
                                            InDate = drDiscrepancyList["InDate"].ToString();
                                            InTime = drDiscrepancyList["InTime"].ToString();
                                            OutDate = drDiscrepancyList["OutDate"].ToString();
                                            OutTime = drDiscrepancyList["OutTime"].ToString();
                                            TimeSpan tsMaxHoursBeforeShift = TimeSpan.Parse(drDiscrepancyList["MaxHoursBeforeShift"].ToString());
                                            DateTime dtmShiftInDateTime = Convert.ToDateTime(drDiscrepancyList["ShiftInDateTime"].ToString()).Subtract(tsMaxHoursBeforeShift);
                                            string strActualShiftInDateTime = drDiscrepancyList["InDate"].ToString() + " " + drDiscrepancyList["InTime"].ToString();
                                            if (Convert.ToDateTime(strActualShiftInDateTime) < dtmShiftInDateTime)
                                            {
                                                iSuccessFlag = 0;
                                                string strAlert = "alert('" + rm.GetString("Alert_InvalidInDateandTimeforEmpID", ci) + " " + drDiscrepancyList["Employee_Code_Id"].ToString() + " Shift Date: " + (Convert.ToDateTime(drDiscrepancyList["ClockDate"].ToString())).ToString(Session["MDateFormat"].ToString()) + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            string strDateFormatLocal = "MM/dd/yyyy";
                                            InDate = Convert.ToDateTime(InDate).ToString(strDateFormatLocal);
                                            OutDate = Convert.ToDateTime(OutDate).ToString(strDateFormatLocal);
                                            DateTime dtInTime = DateTime.Parse(InDate + " " + InTime);
                                            DateTime dtOutTime = DateTime.Parse(OutDate + " " + OutTime);
                                            string strInDateTimeForAlert = Convert.ToDateTime(InDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(InTime).ToString(NewTimeFormat);
                                            string strOutDateTimeForAlert = Convert.ToDateTime(OutDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(OutTime).ToString(NewTimeFormat);
                                            if (DateTime.Compare(dtInTime, dtOutTime) > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAlert = "alert('" + rm.GetString("Alert_InDateandTimeisgreaterthanOutDateandTimefor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            else
                                            {
                                                TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
                                                double TimeDiff = tsDiff.TotalHours;
                                                if (TimeDiff > 24)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl = "alert('" + rm.GetString("Alert_DifferencebetweenInDateTimeandOutDateTimeshouldnotExceed24Hoursfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                                if (count > 0)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl = "alert('" + rm.GetString("Alert_ManualEntryAlreadyhavebeenAddedforthisdurationfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                int intIsPermission = 0;
                                                intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                                if (intIsPermission > 0)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl2 = "alert('" + rm.GetString("Alert_PermissionEntryispresentforthisdurationfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                            }
                                            iSuccessFlag = 1;
                                        }
                                    }
                                    else
                                    {
                                        string EmployeeCodeID = "";
                                        string InDate = "";
                                        string InTime = "";
                                        int InReaderID = 0;
                                        string OutDate = "";
                                        string OutTime = "";
                                        int OutReaderID = 0;
                                        int EmpID = 0;
                                        EmployeeCodeID = drDiscrepancyList["Employee_Code_Id"].ToString();
                                        InDate = drDiscrepancyList["InDate"].ToString();
                                        InTime = drDiscrepancyList["InTime"].ToString();
                                        OutDate = drDiscrepancyList["OutDate"].ToString();
                                        OutTime = drDiscrepancyList["OutTime"].ToString();
                                        TimeSpan tsMaxHoursBeforeShift = TimeSpan.Parse(drDiscrepancyList["MaxHoursBeforeShift"].ToString());
                                        DateTime dtmShiftInDateTime = Convert.ToDateTime(drDiscrepancyList["ShiftInDateTime"].ToString()).Subtract(tsMaxHoursBeforeShift);
                                        string strActualShiftInDateTime = drDiscrepancyList["InDate"].ToString() + " " + drDiscrepancyList["InTime"].ToString();
                                        if (Convert.ToDateTime(strActualShiftInDateTime) < dtmShiftInDateTime)
                                        {
                                            iSuccessFlag = 0;
                                            string strAlert = "alert('" + rm.GetString("Alert_InvalidInDateandTimeforEmpID", ci) + " " + drDiscrepancyList["Employee_Code_Id"].ToString() + " Shift Date: " + (Convert.ToDateTime(drDiscrepancyList["ClockDate"].ToString())).ToString(Session["MDateFormat"].ToString()) + "');";
                                            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                            BindGrid(0);
                                            btnSave.Focus();
                                            return;
                                        }
                                        string strDateFormatLocal = "MM/dd/yyyy";
                                        InDate = Convert.ToDateTime(InDate).ToString(strDateFormatLocal);
                                        OutDate = Convert.ToDateTime(OutDate).ToString(strDateFormatLocal);
                                        DateTime dtInTime = DateTime.Parse(InDate + " " + InTime);
                                        DateTime dtOutTime = DateTime.Parse(OutDate + " " + OutTime);
                                        string strInDateTimeForAlert = Convert.ToDateTime(InDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(InTime).ToString(NewTimeFormat);
                                        string strOutDateTimeForAlert = Convert.ToDateTime(OutDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(OutTime).ToString(NewTimeFormat);
                                        if (DateTime.Compare(dtInTime, dtOutTime) > 0)
                                        {
                                            //iSuccessFlag = 0;
                                            //string strAlert = "alert('" + rm.GetString("Alert_InDateandTimeisgreaterthanOutDateandTimefor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                            //ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                            //BindGrid(0);
                                            //btnSave.Focus();
                                            //return;
                                        }
                                        else
                                        {
                                            TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
                                            double TimeDiff = tsDiff.TotalHours;
                                            if (TimeDiff > 24)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl = "alert('" + rm.GetString("Alert_DifferencebetweenInDateTimeandOutDateTimeshouldnotExceed24Hoursfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                            if (count > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl = "alert('" + rm.GetString("Alert_ManualEntryAlreadyhavebeenAddedforthisdurationfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            int intIsPermission = 0;
                                            intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                            if (intIsPermission > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl2 = "alert('" + rm.GetString("Alert_PermissionEntryispresentforthisdurationfor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                        }
                                        iSuccessFlag = 1;
                                    }
                                }
                                else if (drDiscrepancyList["InDate"].ToString() != "" || drDiscrepancyList["InTime"].ToString() != "" || drDiscrepancyList["OutDate"].ToString() != "" || drDiscrepancyList["OutTime"].ToString() != "" || drDiscrepancyList["InpunchType"].ToString() != "Select" || drDiscrepancyList["OutPunchtype"].ToString() != "Select")
                                {
                                    if (drDiscrepancyList["InDate"].ToString() == "" || drDiscrepancyList["InTime"].ToString() == "" || drDiscrepancyList["OutDate"].ToString() == "" || drDiscrepancyList["OutTime"].ToString() == "" || drDiscrepancyList["InpunchType"].ToString() == "Select" || drDiscrepancyList["OutPunchtype"].ToString() == "Select")
                                    {
                                        iSuccessFlag = 0;
                                        string strAl2 = "alert('" + rm.GetString("Alert_Enterthemissingfields", ci) + "');";
                                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                                        BindGrid(0);
                                        btnSave.Focus();
                                        return;
                                    }
                                }
                            }
                            else
                            {
                                int k = 1;
                            }
                        }
                        else
                        {
                            iSuccessFlag = 0;
                            string strAl2 = "alert('" + rm.GetString("Alert_ClickViewAfterChangingPunchType", ci) + "');";
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                            BindGrid(0);
                            btnSave.Focus();
                            return;
                        }
                    }
                }
                intGridCount = intGridCount + 1;
            }
        }
        string strManaulEntryStatus = string.Empty;
        if (iSuccessFlag != 0)
        {
            int intCheckApproval = 0;
            if (dsDiscrepancyList.Tables[0].Rows.Count > 0)
            {
                int intGridCount = 0;
                ArrayList CheckedClockIdList = (ArrayList)Session[CHECKED_ITEMS];
                foreach (DataRow drDiscrepancyList in dsDiscrepancyList.Tables[0].Rows)
                {
                    if (CheckedClockIdList != null)
                    {
                        if (CheckedClockIdList.Contains(Convert.ToInt32(drDiscrepancyList["CLK_ID"].ToString())) == true)
                        {
                            if (intCheckApproval == 0)
                            {
                                string strEmployeeAutoId = drDiscrepancyList["EmployeeAutoId"].ToString();
                                string strEmployeeCodeId = drDiscrepancyList["Employee_Code_Id"].ToString();
                                strManaulEntryStatus = GetManualEntryStatus(Convert.ToInt32(strEmployeeAutoId), strEmployeeCodeId);
                                intCheckApproval = 1;
                                if (strManaulEntryStatus == "NotDefined")
                                {
                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_ApprovalSettings", ci) + "');", true);
                                    return;
                                }
                            }
                            string strInDate = drDiscrepancyList["InDate"].ToString();
                            if (drDiscrepancyList["DiscrepancyType"].ToString() == "I")
                            {
                                if (drDiscrepancyList["InDate"].ToString() != "" && drDiscrepancyList["InTime"].ToString() != "")
                                {
                                    if (rbtn_WithPunchtype.Checked == true)
                                    {
                                        if (drDiscrepancyList["InpunchType"].ToString() != "Select")
                                        {
                                            string EmployeeCodeID = "";
                                            string InDate = "";
                                            string InTime = "";
                                            string InPunchType = drDiscrepancyList["InPunchType"].ToString();
                                            string[] InPunchTypes = InPunchType.Split('-');
                                            InPunchType = InPunchTypes[1].ToString();
                                            int InReaderID = 0;
                                            string OutDate = "";
                                            string OutTime = "";
                                            string OutPunchType = "";
                                            int OutReaderID = 0;
                                            int EmpID = 0;
                                            EmployeeCodeID = drDiscrepancyList["Employee_Code_Id"].ToString();
                                            InDate = drDiscrepancyList["InDate"].ToString();
                                            InTime = drDiscrepancyList["InTime"].ToString();
                                            string strOutDt = "";
                                            string strOutTi = "";
                                            strOutDt = drDiscrepancyList["OutDate"].ToString();
                                            strOutTi = drDiscrepancyList["OutTime"].ToString();
                                            string strDateFormatLocal = "MM/dd/yyyy";
                                            InDate = Convert.ToDateTime(InDate).ToString(strDateFormatLocal);
                                            DateTime dtInTime = DateTime.Parse(InDate + " " + InTime);
                                            DateTime dtOutTime = DateTime.Parse(objCommon.DateConvertion(strDateFormatLocal, strOutDt) + " " + strOutTi);
                                            string strInDateTimeForAlert = Convert.ToDateTime(InDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(InTime).ToString(NewTimeFormat);
                                            string strOutDateTimeForAlert = Convert.ToDateTime(strOutDt).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(strOutTi).ToString(NewTimeFormat);
                                            if (DateTime.Compare(dtInTime, dtOutTime) > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAlert = "alert('" + rm.GetString("Alert_InDateandTimeisgreaterthanOutDateandTimefor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            else
                                            {
                                                TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
                                                double TimeDiff = tsDiff.TotalHours;
                                                if (TimeDiff > 24)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl = "alert('" + rm.GetString("Alert_DifferencebetweenInDateTimeandOutDateTimeshouldnotExceed24Hoursfor", ci) + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                                if (count > 0)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl = "alert('" + rm.GetString("Alert_ManualEntryAlreadyhavebeenAddedforthisdurationfor", ci) + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                int intIsPermission = 0;
                                                intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                                if (intIsPermission > 0)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl2 = "alert('" + rm.GetString("Alert_PermissionEntryispresentforthisdurationfor", ci) + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                            }
                                            DataSet ds = new DataSet();
                                            ds = objDis.SelectMatchingDevice(Convert.ToString(drDiscrepancyList["OutReaderID"]), "In");
                                            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["Reader_Id"] != DBNull.Value)
                                            {
                                                InReaderID = Convert.ToInt32(ds.Tables[0].Rows[0]["Reader_Id"]);
                                            }
                                            EmpID = Convert.ToInt32(Session["EmpAutoID"]);
                                            if (InTime != string.Empty)
                                            {
                                                if (Session["ITimeFormat"].ToString() != "hh:mm:ss tt")
                                                {
                                                    InTime = Convert.ToDateTime(InTime).ToString("hh:mm:ss tt");
                                                }
                                            }
                                            if (OutTime != string.Empty)
                                            {
                                                if (Session["ITimeFormat"].ToString() != "hh:mm:ss tt")
                                                {
                                                    OutTime = Convert.ToDateTime(OutTime).ToString("hh:mm:ss tt");
                                                }
                                            }
                                            string strReason = drDiscrepancyList["Reason"].ToString();
                                            objDis.InsertDiscrepancy(EmployeeCodeID, InDate, InTime, InPunchType, InReaderID, OutDate, OutTime, OutPunchType, OutReaderID, EmpID, strManaulEntryStatus, strReason.Trim().Replace("'", "''"));
                                        }
                                        else
                                        {
                                            string strAlert = "alert('" + rm.GetString("Alert_SelectInPunchType", ci) + "');";
                                            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAlert, true);
                                            BindGrid(0);
                                            btnSave.Focus();
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        string strInReaderId1 = drDiscrepancyList["InReaderID"].ToString();
                                        string strOutReaderId1 = drDiscrepancyList["OutReaderID"].ToString();
                                        string strPunchTypeExists = string.Empty;
                                        string OutPunchType = drDiscrepancyList["OutPunchType"].ToString();
                                        if (strOutReaderId1 != "")
                                        {
                                            strPunchTypeExists = objDis.CheckPunchTypeForDiscrepancy(strOutReaderId1, OutPunchType);
                                        }
                                        if (strPunchTypeExists == string.Empty)
                                        {
                                            string strAlert = "alert('" + rm.GetString("Alert_INpunchtypeshouldbecreatedinpunchtypemaster", ci) + "');";
                                            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAlert, true);
                                            BindGrid(0);
                                            btnSave.Focus();
                                            return;
                                        }
                                        string EmployeeCodeID = "";
                                        string InDate = "";
                                        string InTime = "";
                                        string InPunchType = string.Empty;
                                        int InReaderID = 0;
                                        string OutDate = "";
                                        string OutTime = "";
                                        OutPunchType = null;
                                        int OutReaderID = 0;
                                        int EmpID = 0;
                                        EmployeeCodeID = drDiscrepancyList["Employee_Code_Id"].ToString();
                                        InDate = drDiscrepancyList["InDate"].ToString();
                                        InTime = drDiscrepancyList["InTime"].ToString();
                                        string strOutDt = "";
                                        string strOutTi = "";
                                        strOutDt = drDiscrepancyList["OutDate"].ToString();
                                        strOutTi = drDiscrepancyList["OutTime"].ToString();
                                        string strDateFormatLocal = "MM/dd/yyyy";
                                        InDate = Convert.ToDateTime(InDate).ToString(strDateFormatLocal);
                                        DateTime dtInTime = DateTime.Parse(InDate + " " + InTime);
                                        DateTime dtOutTime = DateTime.Parse(objCommon.DateConvertion(strDateFormatLocal, strOutDt) + " " + strOutTi);
                                        string strInDateTimeForAlert = Convert.ToDateTime(InDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(InTime).ToString(NewTimeFormat);
                                        string strOutDateTimeForAlert = Convert.ToDateTime(strOutDt).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(strOutTi).ToString(NewTimeFormat);
                                        if (DateTime.Compare(dtInTime, dtOutTime) > 0)
                                        {
                                            //iSuccessFlag = 0;
                                            //string strAlert = "alert('" + rm.GetString("Alert_InDateandTimeisgreaterthanOutDateandTimefor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                            //ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                            //BindGrid(0);
                                            //btnSave.Focus();
                                            //return;
                                        }
                                        else
                                        {
                                            TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
                                            double TimeDiff = tsDiff.TotalHours;
                                            if (TimeDiff > 24)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl = "alert('" + rm.GetString("Alert_DifferencebetweenInDateTimeandOutDateTimeshouldnotExceed24Hoursfor", ci) + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                            if (count > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl = "alert('" + rm.GetString("Alert_ManualEntryAlreadyhavebeenAddedforthisdurationfor", ci) + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            int intIsPermission = 0;
                                            intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                            if (intIsPermission > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl2 = "alert('" + rm.GetString("Alert_PermissionEntryispresentforthisdurationfor", ci) + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                        }
                                        DataSet ds = new DataSet();
                                        ds = objDis.SelectMatchingDevice(Convert.ToString(drDiscrepancyList["OutReaderID"]), "In");
                                        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["Reader_Id"] != DBNull.Value)
                                        {
                                            InReaderID = Convert.ToInt32(ds.Tables[0].Rows[0]["Reader_Id"]);
                                        }
                                        EmpID = Convert.ToInt32(Session["EmpAutoID"]);
                                        if (InTime != string.Empty)
                                        {
                                            if (Session["ITimeFormat"].ToString() != "hh:mm:ss tt")
                                            {
                                                InTime = Convert.ToDateTime(InTime).ToString("hh:mm:ss tt");
                                            }
                                        }
                                        if (OutTime != string.Empty)
                                        {
                                            if (Session["ITimeFormat"].ToString() != "hh:mm:ss tt")
                                            {
                                                OutTime = Convert.ToDateTime(OutTime).ToString("hh:mm:ss tt");
                                            }
                                        }
                                        if (rbtn_WithoutPunchtype.Checked == true)
                                        {
                                            InPunchType = strPunchTypeExists;
                                        }
                                        string strReason = drDiscrepancyList["Reason"].ToString();
                                        objDis.InsertDiscrepancy(EmployeeCodeID, InDate, InTime, InPunchType, InReaderID, OutDate, OutTime, OutPunchType, OutReaderID, EmpID, strManaulEntryStatus, strReason.Trim().Replace("'", "''"));
                                    }
                                    iSuccessFlag = 1;
                                }
                            }
                            else if (drDiscrepancyList["DiscrepancyType"].ToString() == "O")
                            {
                                if (drDiscrepancyList["OutDate"].ToString() != "" && drDiscrepancyList["OutTime"].ToString() != "")
                                {
                                    if (rbtn_WithPunchtype.Checked == true)
                                    {
                                        if (drDiscrepancyList["OutPunchtype"].ToString() != "Select")
                                        {
                                            string EmployeeCodeID = "";
                                            string InDate = "";
                                            string InTime = "";
                                            string InPunchType = "";
                                            int InReaderID = 0;
                                            string OutDate = "";
                                            string OutTime = "";
                                            string OutPunchType = drDiscrepancyList["OutPunchType"].ToString();
                                            string[] OutPunchTypes = OutPunchType.Split('-');
                                            OutPunchType = OutPunchTypes[1].ToString();
                                            int OutReaderID = 0;
                                            int EmpID = 0;
                                            EmployeeCodeID = drDiscrepancyList["Employee_Code_Id"].ToString();
                                            OutDate = drDiscrepancyList["OutDate"].ToString();
                                            OutTime = drDiscrepancyList["OutTime"].ToString();
                                            string strInDt = "";
                                            string strInTi = "";
                                            strInDt = drDiscrepancyList["InDate"].ToString();
                                            strInTi = drDiscrepancyList["InTime"].ToString();
                                            string strDateFormatLocal = "MM/dd/yyyy";
                                            OutDate = Convert.ToDateTime(OutDate).ToString(strDateFormatLocal);
                                            DateTime dtOutTime = DateTime.Parse(OutDate + " " + OutTime);
                                            DateTime dtInTime = DateTime.Parse(objCommon.DateConvertion(strDateFormatLocal, strInDt) + " " + strInTi);
                                            string strInDateTimeForAlert = Convert.ToDateTime(strInDt).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(strInTi).ToString(NewTimeFormat);
                                            string strOutDateTimeForAlert = Convert.ToDateTime(OutDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(OutTime).ToString(NewTimeFormat);
                                            if (DateTime.Compare(dtInTime, dtOutTime) > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAlert = "alert('" + rm.GetString("Alert_InDateandTimeisgreaterthanOutDateandTimefor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            else
                                            {
                                                TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
                                                double TimeDiff = tsDiff.TotalHours;
                                                if (TimeDiff > 24)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl = "alert('" + rm.GetString("Alert_DifferencebetweenInDateTimeandOutDateTimeshouldnotExceed24Hoursfor", ci) + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                                if (count > 0)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl = "alert('" + rm.GetString("Alert_ManualEntryAlreadyhavebeenAddedforthisdurationfor", ci) + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                int intIsPermission = 0;
                                                intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                                if (intIsPermission > 0)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl2 = "alert('" + rm.GetString("Alert_PermissionEntryispresentforthisdurationfor", ci) + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                            }
                                            DataSet ds = new DataSet();
                                            ds = objDis.SelectMatchingDevice(Convert.ToString(drDiscrepancyList["InReaderID"]), "Out");
                                            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["Reader_Id"] != DBNull.Value)
                                            {
                                                OutReaderID = Convert.ToInt32(ds.Tables[0].Rows[0]["Reader_Id"]);
                                            }
                                            EmpID = Convert.ToInt32(Session["EmpAutoID"]);
                                            if (InTime != string.Empty)
                                            {
                                                if (Session["ITimeFormat"].ToString() != "hh:mm:ss tt")
                                                {
                                                    InTime = Convert.ToDateTime(InTime).ToString("hh:mm:ss tt");
                                                }
                                            }
                                            if (OutTime != string.Empty)
                                            {
                                                if (Session["ITimeFormat"].ToString() != "hh:mm:ss tt")
                                                {
                                                    OutTime = Convert.ToDateTime(OutTime).ToString("hh:mm:ss tt");
                                                }
                                            }
                                            string strReason = drDiscrepancyList["Reason"].ToString();
                                            objDis.InsertDiscrepancy(EmployeeCodeID, InDate, InTime, InPunchType, InReaderID, OutDate, OutTime, OutPunchType, OutReaderID, EmpID, strManaulEntryStatus, strReason.Trim().Replace("'", "''"));
                                        }
                                        else
                                        {
                                            string strAlert = "alert('" + rm.GetString("Alert_SelectOutPunchType1", ci) + "');";
                                            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAlert, true);
                                            BindGrid(0);
                                            btnSave.Focus();
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        string strInReaderId1 = drDiscrepancyList["InReaderID"].ToString();
                                        string strOutReaderId1 = drDiscrepancyList["OutReaderID"].ToString();
                                        string InPunchType = drDiscrepancyList["InPunchType"].ToString();
                                        string strPunchTypeExists = string.Empty;
                                        if (strInReaderId1 != "")
                                        {
                                            strPunchTypeExists = objDis.CheckPunchTypeForDiscrepancy(strInReaderId1, InPunchType);
                                        }
                                        if (strPunchTypeExists == string.Empty)
                                        {
                                            string strAlert = "alert('" + rm.GetString("Alert_OUTpunchtypeshouldbecreatedinpunchtypemaster", ci) + "');";
                                            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAlert, true);
                                            BindGrid(0);
                                            btnSave.Focus();
                                            return;
                                        }
                                        string EmployeeCodeID = "";
                                        string InDate = "";
                                        string InTime = "";
                                        InPunchType = null;
                                        int InReaderID = 0;
                                        string OutDate = "";
                                        string OutTime = "";
                                        string OutPunchType = string.Empty;
                                        int OutReaderID = 0;
                                        int EmpID = 0;
                                        EmployeeCodeID = drDiscrepancyList["Employee_Code_Id"].ToString();
                                        OutDate = drDiscrepancyList["OutDate"].ToString();
                                        OutTime = drDiscrepancyList["OutTime"].ToString();
                                        string strInDt = "";
                                        string strInTi = "";
                                        strInDt = drDiscrepancyList["InDate"].ToString();
                                        strInTi = drDiscrepancyList["InTime"].ToString();
                                        string strDateFormatLocal = "MM/dd/yyyy";
                                        OutDate = Convert.ToDateTime(OutDate).ToString(strDateFormatLocal);
                                        DateTime dtOutTime = DateTime.Parse(OutDate + " " + OutTime);
                                        DateTime dtInTime = DateTime.Parse(objCommon.DateConvertion(strDateFormatLocal, strInDt) + " " + strInTi);
                                        string strInDateTimeForAlert = Convert.ToDateTime(strInDt).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(strInTi).ToString(NewTimeFormat);
                                        string strOutDateTimeForAlert = Convert.ToDateTime(OutDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(OutTime).ToString(NewTimeFormat);
                                        if (DateTime.Compare(dtInTime, dtOutTime) > 0)
                                        {
                                            //iSuccessFlag = 0;
                                            //string strAlert = "alert('" + rm.GetString("Alert_InDateandTimeisgreaterthanOutDateandTimefor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                            //ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                            //BindGrid(0);
                                            //btnSave.Focus();
                                            //return;
                                        }
                                        else
                                        {
                                            TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
                                            double TimeDiff = tsDiff.TotalHours;
                                            if (TimeDiff > 24)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl = "alert('" + rm.GetString("Alert_DifferencebetweenInDateTimeandOutDateTimeshouldnotExceed24Hoursfor", ci) + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                            if (count > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl = "alert('" + rm.GetString("Alert_ManualEntryAlreadyhavebeenAddedforthisdurationfor", ci) + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            int intIsPermission = 0;
                                            intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                            if (intIsPermission > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl2 = "alert('" + rm.GetString("Alert_PermissionEntryispresentforthisdurationfor", ci) + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                        }
                                        DataSet ds = new DataSet();
                                        ds = objDis.SelectMatchingDevice(Convert.ToString(drDiscrepancyList["InReaderID"]), "Out");
                                        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["Reader_Id"] != DBNull.Value)
                                        {
                                            OutReaderID = Convert.ToInt32(ds.Tables[0].Rows[0]["Reader_Id"]);
                                        }
                                        EmpID = Convert.ToInt32(Session["EmpAutoID"]);
                                        if (InTime != string.Empty)
                                        {
                                            if (Session["ITimeFormat"].ToString() != "hh:mm:ss tt")
                                            {
                                                InTime = Convert.ToDateTime(InTime).ToString("hh:mm:ss tt");
                                            }
                                        }
                                        if (OutTime != string.Empty)
                                        {
                                            if (Session["ITimeFormat"].ToString() != "hh:mm:ss tt")
                                            {
                                                OutTime = Convert.ToDateTime(OutTime).ToString("hh:mm:ss tt");
                                            }
                                        }
                                        if (rbtn_WithoutPunchtype.Checked == true)
                                        {
                                            OutPunchType = strPunchTypeExists;
                                        }
                                        string strReason = drDiscrepancyList["Reason"].ToString();
                                        objDis.InsertDiscrepancy(EmployeeCodeID, InDate, InTime, InPunchType, InReaderID, OutDate, OutTime, OutPunchType, OutReaderID, EmpID, strManaulEntryStatus, strReason.Trim().Replace("'", "''"));
                                    }
                                    iSuccessFlag = 1;
                                }
                            }
                            else if (drDiscrepancyList["DiscrepancyType"].ToString() == "A")
                            {
                                if (drDiscrepancyList["InDate"].ToString() != "" && drDiscrepancyList["InTime"].ToString() != "" && drDiscrepancyList["OutDate"].ToString() != "" && drDiscrepancyList["OutTime"].ToString() != "")
                                {
                                    if (rbtn_WithPunchtype.Checked == true)
                                    {
                                        if (drDiscrepancyList["InpunchType"].ToString() != "Select" && drDiscrepancyList["OutPunchtype"].ToString() != "Select")
                                        {
                                            string EmployeeCodeID = "";
                                            string InDate = "";
                                            string InTime = "";
                                            string InPunchType = drDiscrepancyList["InPunchType"].ToString();
                                            string[] InPunchTypes = InPunchType.Split('-');
                                            InPunchType = InPunchTypes[1].ToString();
                                            int InReaderID = 0;
                                            string OutDate = "";
                                            string OutTime = "";
                                            string OutPunchType = drDiscrepancyList["OutPunchType"].ToString();
                                            string[] OutPunchTypes = OutPunchType.Split('-');
                                            OutPunchType = OutPunchTypes[1].ToString();
                                            int OutReaderID = 0;
                                            int EmpID = 0;
                                            EmployeeCodeID = drDiscrepancyList["Employee_Code_Id"].ToString();
                                            OutDate = drDiscrepancyList["OutDate"].ToString();
                                            OutTime = drDiscrepancyList["OutTime"].ToString();
                                            InDate = drDiscrepancyList["InDate"].ToString();
                                            InTime = drDiscrepancyList["InTime"].ToString();
                                            string strDateFormatLocal = "MM/dd/yyyy";
                                            OutDate = Convert.ToDateTime(OutDate).ToString(strDateFormatLocal);
                                            InDate = Convert.ToDateTime(InDate).ToString(strDateFormatLocal);
                                            DateTime dtOutTime = DateTime.Parse(OutDate + " " + OutTime);
                                            DateTime dtInTime = DateTime.Parse(InDate + " " + InTime);
                                            string strInDateTimeForAlert = Convert.ToDateTime(InDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(InTime).ToString(NewTimeFormat);
                                            string strOutDateTimeForAlert = Convert.ToDateTime(OutDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(OutTime).ToString(NewTimeFormat);
                                            if (DateTime.Compare(dtInTime, dtOutTime) > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAlert = "alert('" + rm.GetString("Alert_InDateandTimeisgreaterthanOutDateandTimefor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            else
                                            {
                                                TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
                                                double TimeDiff = tsDiff.TotalHours;
                                                if (TimeDiff > 24)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl = "alert('" + rm.GetString("Alert_DifferencebetweenInDateTimeandOutDateTimeshouldnotExceed24Hoursfor", ci) + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                                if (count > 0)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl = "alert('" + rm.GetString("Alert_ManualEntryAlreadyhavebeenAddedforthisdurationfor", ci) + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                                int intIsPermission = 0;
                                                intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                                if (intIsPermission > 0)
                                                {
                                                    iSuccessFlag = 0;
                                                    string strAl2 = "alert('" + rm.GetString("Alert_PermissionEntryispresentforthisdurationfor", ci) + "');";
                                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                                                    BindGrid(0);
                                                    btnSave.Focus();
                                                    return;
                                                }
                                            }
                                            DataSet ds = new DataSet();
                                            string[] strInReaderId;
                                            strInReaderId = drDiscrepancyList["InReaderID"].ToString().Split(',');
                                            ds = objDis.SelectMatchingDevice(Convert.ToString(strInReaderId[0].ToString()), "Out");
                                            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["Reader_Id"] != DBNull.Value)
                                            {
                                                OutReaderID = Convert.ToInt32(ds.Tables[0].Rows[0]["Reader_Id"]);
                                            }
                                            EmpID = Convert.ToInt32(Session["EmpAutoID"]);
                                            if (InTime != string.Empty)
                                            {
                                                if (Session["ITimeFormat"].ToString() != "hh:mm:ss tt")
                                                {
                                                    InTime = Convert.ToDateTime(InTime).ToString("hh:mm:ss tt");
                                                }
                                            }
                                            if (OutTime != string.Empty)
                                            {
                                                if (Session["ITimeFormat"].ToString() != "hh:mm:ss tt")
                                                {
                                                    OutTime = Convert.ToDateTime(OutTime).ToString("hh:mm:ss tt");
                                                }
                                            }
                                            string strReason = drDiscrepancyList["Reason"].ToString();
                                            InReaderID = Convert.ToInt32(strInReaderId[1]);
                                            if (rbtn_WithoutPunchtype.Checked == true)
                                            {
                                                InPunchType = "In";
                                                OutPunchType = "Out";
                                            }
                                            objDis.InsertDiscrepancy(EmployeeCodeID, InDate, InTime, InPunchType, InReaderID, OutDate, OutTime, OutPunchType, OutReaderID, EmpID, strManaulEntryStatus, strReason.Trim().Replace("'", "''"));
                                        }
                                        else
                                        {
                                            string strAlert = "alert('" + rm.GetString("Alert_SelectInPunchorOutPunchType", ci) + "');";
                                            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAlert, true);
                                            BindGrid(0);
                                            btnSave.Focus();
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        string strInReaderId1 = drDiscrepancyList["InReaderID"].ToString();
                                        string strOutReaderId1 = drDiscrepancyList["OutReaderID"].ToString();
                                        string strPunchTypeExists = string.Empty;
                                        string strOutPunchTypeExists = string.Empty;
                                        if (strInReaderId1 != "")
                                        {
                                            strPunchTypeExists = objDis.CheckPunchTypeForDiscrepancy(strInReaderId1, "In");
                                        }
                                        if (strOutReaderId1 != "")
                                        {
                                            strOutPunchTypeExists = objDis.CheckPunchTypeForDiscrepancy(strOutReaderId1, "Out");
                                        }
                                        if (strOutPunchTypeExists == "")
                                        {
                                            string strAlert = "alert('" + rm.GetString("Alert_INpunchtypeshouldbecreatedinpunchtypemaster", ci) + "');";
                                            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAlert, true);
                                            BindGrid(0);
                                            btnSave.Focus();
                                            return;
                                        }
                                        else if (strPunchTypeExists == "")
                                        {
                                            string strAlert = "alert('" + rm.GetString("Alert_OUTpunchtypeshouldbecreatedinpunchtypemaster", ci) + "');";
                                            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAlert, true);
                                            BindGrid(0);
                                            btnSave.Focus();
                                            return;
                                        }
                                        string EmployeeCodeID = "";
                                        string InDate = "";
                                        string InTime = "";
                                        string InPunchType = string.Empty;
                                        int InReaderID = 0;
                                        string OutDate = "";
                                        string OutTime = "";
                                        string OutPunchType = string.Empty;
                                        int OutReaderID = 0;
                                        int EmpID = 0;
                                        EmployeeCodeID = drDiscrepancyList["Employee_Code_Id"].ToString();
                                        OutDate = drDiscrepancyList["OutDate"].ToString();
                                        OutTime = drDiscrepancyList["OutTime"].ToString();
                                        InDate = drDiscrepancyList["InDate"].ToString();
                                        InTime = drDiscrepancyList["InTime"].ToString();
                                        string strDateFormatLocal = "MM/dd/yyyy";
                                        OutDate = Convert.ToDateTime(OutDate).ToString(strDateFormatLocal);
                                        InDate = Convert.ToDateTime(InDate).ToString(strDateFormatLocal);
                                        DateTime dtOutTime = DateTime.Parse(OutDate + " " + OutTime);
                                        DateTime dtInTime = DateTime.Parse(InDate + " " + InTime);
                                        string strInDateTimeForAlert = Convert.ToDateTime(InDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(InTime).ToString(NewTimeFormat);
                                        string strOutDateTimeForAlert = Convert.ToDateTime(OutDate).ToString(Session["MDateFormat"].ToString()) + " " + Convert.ToDateTime(OutTime).ToString(NewTimeFormat);
                                        if (DateTime.Compare(dtInTime, dtOutTime) > 0)
                                        {
                                            //iSuccessFlag = 0;
                                            //string strAlert = "alert('" + rm.GetString("Alert_InDateandTimeisgreaterthanOutDateandTimefor", ci) + " " + strInDateTimeForAlert + " And " + strOutDateTimeForAlert + "');";
                                            //ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAlert, true);
                                            //BindGrid(0);
                                            //btnSave.Focus();
                                            //return;
                                        }
                                        else
                                        {
                                            TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
                                            double TimeDiff = tsDiff.TotalHours;
                                            if (TimeDiff > 24)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl = "alert('" + rm.GetString("Alert_DifferencebetweenInDateTimeandOutDateTimeshouldnotExceed24Hoursfor", ci) + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                            if (count > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl = "alert('" + rm.GetString("Alert_ManualEntryAlreadyhavebeenAddedforthisdurationfor", ci) + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                            int intIsPermission = 0;
                                            intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
                                            if (intIsPermission > 0)
                                            {
                                                iSuccessFlag = 0;
                                                string strAl2 = "alert('" + rm.GetString("Alert_PermissionEntryispresentforthisdurationfor", ci) + "');";
                                                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strAl2, true);
                                                BindGrid(0);
                                                btnSave.Focus();
                                                return;
                                            }
                                        }
                                        DataSet ds = new DataSet();
                                        string[] strInReaderId;
                                        strInReaderId = drDiscrepancyList["InReaderID"].ToString().Split(',');
                                        ds = objDis.SelectMatchingDevice(Convert.ToString(strInReaderId[0].ToString()), "Out");
                                        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["Reader_Id"] != DBNull.Value)
                                        {
                                            OutReaderID = Convert.ToInt32(ds.Tables[0].Rows[0]["Reader_Id"]);
                                        }
                                        EmpID = Convert.ToInt32(Session["EmpAutoID"]);
                                        if (InTime != string.Empty)
                                        {
                                            if (Session["ITimeFormat"].ToString() != "hh:mm:ss tt")
                                            {
                                                InTime = Convert.ToDateTime(InTime).ToString("hh:mm:ss tt");
                                            }
                                        }
                                        if (OutTime != string.Empty)
                                        {
                                            if (Session["ITimeFormat"].ToString() != "hh:mm:ss tt")
                                            {
                                                OutTime = Convert.ToDateTime(OutTime).ToString("hh:mm:ss tt");
                                            }
                                        }
                                        string strReason = drDiscrepancyList["Reason"].ToString();
                                        InReaderID = Convert.ToInt32(strInReaderId[0]);
                                        if (strOutPunchTypeExists == "" || strPunchTypeExists == "")
                                        {
                                            InPunchType = "In";
                                            OutPunchType = "Out";
                                        }
                                        else if (strOutPunchTypeExists != "" && strPunchTypeExists != "")
                                        {
                                            InPunchType = strOutPunchTypeExists;
                                            OutPunchType = strPunchTypeExists;
                                        }
                                        else if (strOutPunchTypeExists != "" && strPunchTypeExists == "")
                                        {
                                            InPunchType = strOutPunchTypeExists;
                                            OutPunchType = "Out";
                                        }
                                        else if (strOutPunchTypeExists == "" && strPunchTypeExists != "")
                                        {
                                            InPunchType = "In";
                                            OutPunchType = strPunchTypeExists;
                                        }
                                        objDis.InsertDiscrepancy(EmployeeCodeID, InDate, InTime, InPunchType, InReaderID, OutDate, OutTime, OutPunchType, OutReaderID, EmpID, strManaulEntryStatus, strReason.Trim().Replace("'", "''"));
                                    }
                                    iSuccessFlag = 1;
                                }
                            }
                        }
                    }
                    intGridCount = intGridCount + 1;
                }
            }
            else
            {
                int k = 1;
            }
        }
        BindGrid(0);
        if (iSuccessFlag == 1)
        {
            grd.DataSource = null;
            grd.DataBind();
            Session["DiscrepancyList"] = null;
            btnSave.Visible = false;
            string strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("Alert_DiscrepancyEntrySavedSuccessfully", ci) + "');";
            strScript = strScript + "location.replace('TA_UI_Transactions_DiscrepancyList.aspx');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else
        {
            string strSavAlert = "alert('" + rm.GetString("Alert_Enterthemissingfields", ci) + "');";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strSavAlert, true);
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Transactions_ManualTimeEntryList.aspx");
    }
    #endregion
    #region Gridview Events
    protected void grd_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        RememberOldValues();
        int intpageIndex = grd.PageIndex;
        grd.PageIndex = e.NewPageIndex;
        BindGrid(intpageIndex);
        RePopulateValues();
    }
    protected void BindGrid(int intpageIndex)
    {
        try
        {
            string NewTimeFormat = string.Empty;
            string TimeFormat = Convert.ToString(Session["ITimeFormat"]);
            dsDiscrepancyList = (DataSet)Session["DiscrepancyList"];
            string strInDate = string.Empty;
            string strInTime = string.Empty;
            bool blnCheckDate = true;
            bool blnCheckTime = true;
            string strOutDate = string.Empty;
            string strOutTime = string.Empty;
            string strInPunchType = string.Empty;
            string strOutPunchType = string.Empty;
            foreach (GridViewRow i in grd.Rows)
            {
                Label lblClkId = (Label)i.FindControl("lblClkID");
                Label lblDiscrepancyType = (Label)i.FindControl("lblDiscrepancyType");
                TextBox txtInDate = (TextBox)i.FindControl("txtInDate");
                TextBox txtInTime = (TextBox)i.FindControl("txtInTime");
                DropDownList ddlInPunchType = (DropDownList)i.FindControl("ddlInPunchType");
                DropDownList ddlOutPunchType = (DropDownList)i.FindControl("ddlOutPunchType");
                TextBox txtOutDate = (TextBox)i.FindControl("txtOutDate");
                TextBox txtOutTime = (TextBox)i.FindControl("txtOutTime");
                TextBox txtReason = (TextBox)i.FindControl("txtReason");
                Label lblShiftStartTime = (Label)i.FindControl("lblShiftStartTime");
                Label lblShiftEndTime = (Label)i.FindControl("lblShiftEndTime");
                dsDiscrepancyList.Tables[0].DefaultView.RowFilter = "CLK_ID = '" + Convert.ToInt32(lblClkId.Text) + "'";
                if (dsDiscrepancyList.Tables[0].DefaultView.Count > 0)
                {
                    if (lblDiscrepancyType.Text == "I")
                    {
                        strInDate = string.Empty;
                        strInTime = string.Empty;
                        strInPunchType = string.Empty;
                        if (txtInDate.Text != null && txtInDate.Text.Trim() != string.Empty)
                        {
                            blnCheckDate = CheckDateFormat(gstrDateFormat, txtInDate.Text.Trim());
                            if (blnCheckDate == false)
                            {
                                strInDate = txtInDate.Text.Trim();
                                strInTime = txtInTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidDate", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtInDate");
                                        txtDateNew.Text = strInDate;
                                        txtDateNew.Focus();
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtInTime");
                                        txtTimeNew.Text = strInTime;
                                    }
                                }
                                return;
                            }
                            if (Session["MDateFormat"].ToString() != "MM/dd/yyyy")
                            {
                                strInDate = objCommon.DateConvertion(gstrDateFormat, txtInDate.Text.Trim());
                                strInDate = (Convert.ToDateTime(strInDate)).ToString("MM/dd/yyyy");
                            }
                            else
                            {
                                strInDate = txtInDate.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["InDate"] = strInDate;
                        if ((txtInTime.Text != null) && (txtInTime.Text != string.Empty))
                        {
                            blnCheckTime = CheckTimeFormat(txtInTime.Text.Trim(), Session["ITimeFormat"].ToString());
                            if (blnCheckTime == false)
                            {
                                strInDate = txtInDate.Text.Trim();
                                strInTime = txtInTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidTime", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtInDate");
                                        txtDateNew.Text = strInDate;
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtInTime");
                                        txtTimeNew.Text = strInTime;
                                        txtTimeNew.Focus();
                                    }
                                }
                                return;
                            }
                            if (Session["ITimeFormat"].ToString() != "HH:mm:ss")
                            {
                                strInTime = (Convert.ToDateTime(txtInTime.Text.Trim().ToString())).ToString("HH:mm:ss");
                            }
                            else
                            {
                                strInTime = txtInDate.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["InTime"] = strInTime;
                        strInPunchType = ddlInPunchType.SelectedItem.Text;
                        dsDiscrepancyList.Tables[0].DefaultView[0]["InPunchType"] = strInPunchType;
                    }
                    else if (lblDiscrepancyType.Text == "O")
                    {
                        strOutDate = string.Empty;
                        strOutTime = string.Empty;
                        strOutPunchType = string.Empty;
                        if (txtOutDate.Text != null && txtOutDate.Text.Trim() != string.Empty)
                        {
                            blnCheckDate = CheckDateFormat(gstrDateFormat, txtOutDate.Text.Trim());
                            if (blnCheckDate == false)
                            {
                                strOutDate = txtOutDate.Text.Trim();
                                strOutTime = txtOutTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidDate", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtOutDate");
                                        txtDateNew.Text = strOutDate;
                                        txtDateNew.Focus();
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtOutTime");
                                        txtTimeNew.Text = strOutTime;
                                    }
                                }
                                return;
                            }
                            if (Session["MDateFormat"].ToString() != "MM/dd/yyyy")
                            {
                                strOutDate = objCommon.DateConvertion(gstrDateFormat, txtOutDate.Text.Trim());
                                strOutDate = (Convert.ToDateTime(strOutDate)).ToString("MM/dd/yyyy");
                            }
                            else
                            {
                                strOutDate = txtOutDate.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["OutDate"] = strOutDate;
                        if ((txtOutTime.Text != null) && (txtOutTime.Text != string.Empty))
                        {
                            blnCheckTime = CheckTimeFormat(txtOutTime.Text.Trim(), Session["ITimeFormat"].ToString());
                            if (blnCheckTime == false)
                            {
                                strOutDate = txtOutDate.Text.Trim();
                                strOutTime = txtOutTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidTime", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtOutDate");
                                        txtDateNew.Text = strOutDate;
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtOutTime");
                                        txtTimeNew.Text = strOutTime;
                                        txtTimeNew.Focus();
                                    }
                                }
                                return;
                            }
                            if (Session["ITimeFormat"].ToString() != "HH:mm:ss")
                            {
                                strOutTime = (Convert.ToDateTime(txtOutTime.Text.Trim().ToString())).ToString("HH:mm:ss");
                            }
                            else
                            {
                                strOutTime = txtOutTime.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["OutTime"] = strOutTime;
                        strOutPunchType = ddlOutPunchType.SelectedItem.Text;
                        dsDiscrepancyList.Tables[0].DefaultView[0]["OutPunchType"] = strOutPunchType;
                    }
                    else if (lblDiscrepancyType.Text == "A")
                    {
                        strInDate = string.Empty;
                        strInTime = string.Empty;
                        strInPunchType = string.Empty;
                        if (txtInDate.Text != null && txtInDate.Text.Trim() != string.Empty)
                        {
                            blnCheckDate = CheckDateFormat(gstrDateFormat, txtInDate.Text.Trim());
                            if (blnCheckDate == false)
                            {
                                strInDate = txtInDate.Text.Trim();
                                strInTime = txtInTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidDate", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtInDate");
                                        txtDateNew.Text = strInDate;
                                        txtDateNew.Focus();
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtInTime");
                                        txtTimeNew.Text = strInTime;
                                    }
                                }
                                return;
                            }
                            if (Session["MDateFormat"].ToString() != "MM/dd/yyyy")
                            {
                                strInDate = objCommon.DateConvertion(gstrDateFormat, txtInDate.Text.Trim());
                                strInDate = (Convert.ToDateTime(strInDate)).ToString("MM/dd/yyyy");
                            }
                            else
                            {
                                strInDate = txtInDate.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["InDate"] = strInDate;
                        if ((txtInTime.Text != null) && (txtInTime.Text != string.Empty))
                        {
                            blnCheckTime = CheckTimeFormat(txtInTime.Text.Trim(), Session["ITimeFormat"].ToString());
                            if (blnCheckTime == false)
                            {
                                strInDate = txtInDate.Text.Trim();
                                strInTime = txtInTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidTime", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtInDate");
                                        txtDateNew.Text = strInDate;
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtInTime");
                                        txtTimeNew.Text = strInTime;
                                        txtTimeNew.Focus();
                                    }
                                }
                                return;
                            }
                            if (Session["ITimeFormat"].ToString() != "HH:mm:ss")
                            {
                                strInTime = (Convert.ToDateTime(txtInTime.Text.Trim().ToString())).ToString("HH:mm:ss");
                            }
                            else
                            {
                                strInTime = txtInDate.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["InTime"] = strInTime;
                        strInPunchType = ddlInPunchType.SelectedItem.Text;
                        dsDiscrepancyList.Tables[0].DefaultView[0]["InPunchType"] = strInPunchType;
                        strOutDate = string.Empty;
                        strOutTime = string.Empty;
                        strOutPunchType = string.Empty;
                        if (txtOutDate.Text != null && txtOutDate.Text.Trim() != string.Empty)
                        {
                            blnCheckDate = CheckDateFormat(gstrDateFormat, txtOutDate.Text.Trim());
                            if (blnCheckDate == false)
                            {
                                strOutDate = txtOutDate.Text.Trim();
                                strOutTime = txtOutTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidDate", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtOutDate");
                                        txtDateNew.Text = strOutDate;
                                        txtDateNew.Focus();
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtOutTime");
                                        txtTimeNew.Text = strOutTime;
                                    }
                                }
                                return;
                            }
                            if (Session["MDateFormat"].ToString() != "MM/dd/yyyy")
                            {
                                strOutDate = objCommon.DateConvertion(gstrDateFormat, txtOutDate.Text.Trim());
                                strOutDate = (Convert.ToDateTime(strOutDate)).ToString("MM/dd/yyyy");
                            }
                            else
                            {
                                strOutDate = txtOutDate.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["OutDate"] = strOutDate;
                        if ((txtOutTime.Text != null) && (txtOutTime.Text != string.Empty))
                        {
                            blnCheckTime = CheckTimeFormat(txtOutTime.Text.Trim(), Session["ITimeFormat"].ToString());
                            if (blnCheckTime == false)
                            {
                                strOutDate = txtOutDate.Text.Trim();
                                strOutTime = txtOutTime.Text.Trim();
                                string strSavAlert = "alert('" + rm.GetString("Alert_InValidTime", ci) + "');";
                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
                                grd.DataSource = dsDiscrepancyList;
                                grd.PageIndex = intpageIndex;
                                grd.DataBind();
                                foreach (GridViewRow j in grd.Rows)
                                {
                                    if (j.DataItemIndex == i.DataItemIndex)
                                    {
                                        TextBox txtDateNew = (TextBox)j.FindControl("txtOutDate");
                                        txtDateNew.Text = strOutDate;
                                        TextBox txtTimeNew = (TextBox)j.FindControl("txtOutTime");
                                        txtTimeNew.Text = strOutTime;
                                        txtTimeNew.Focus();
                                    }
                                }
                                return;
                            }
                            if (Session["ITimeFormat"].ToString() != "HH:mm:ss")
                            {
                                strOutTime = (Convert.ToDateTime(txtOutTime.Text.Trim().ToString())).ToString("HH:mm:ss");
                            }
                            else
                            {
                                strOutTime = txtOutTime.Text;
                            }
                        }
                        dsDiscrepancyList.Tables[0].DefaultView[0]["OutTime"] = strOutTime;
                        strOutPunchType = ddlOutPunchType.SelectedItem.Text;
                        dsDiscrepancyList.Tables[0].DefaultView[0]["OutPunchType"] = strOutPunchType;
                    }
                    if ((txtReason.Text != null) && (txtReason.Text != string.Empty))
                    {
                        dsDiscrepancyList.Tables[0].DefaultView[0]["Reason"] = txtReason.Text;
                    }
                }
            }
            Session["DiscrepancyList"] = dsDiscrepancyList;
            grd.DataSource = dsDiscrepancyList;
            grd.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
    protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox txtInDate = (TextBox)e.Row.FindControl("txtInDate");
            TextBox txtInTime = (TextBox)e.Row.FindControl("txtInTime");
            TextBox txtOutDate = (TextBox)e.Row.FindControl("txtOutDate");
            TextBox txtOutTime = (TextBox)e.Row.FindControl("txtOutTime");
            DropDownList ddlInPunch = (DropDownList)e.Row.FindControl("ddlInPunchType");
            DropDownList ddlOutPunch = (DropDownList)e.Row.FindControl("ddlOutPunchType");
            Label lblInPunchtype = (Label)e.Row.FindControl("lblInPunchtype");
            Label lblOutPunchType = (Label)e.Row.FindControl("lblOutPunchtype");
            Label lblInReader = (Label)e.Row.FindControl("lblInReader");
            Label lblOutReader = (Label)e.Row.FindControl("lblOutReader");
            int InReaderId = 0;
            int OutReaderId = 0;
            string strInreaderType = string.Empty;
            string strOutReaderType = string.Empty;
            Label lblDiscrepancyType = (Label)e.Row.FindControl("lblDiscrepancyType");
            Label lblShiftStartTime = (Label)e.Row.FindControl("lblShiftStartTime");
            Label lblShiftEndTime = (Label)e.Row.FindControl("lblShiftEndTime");
            Label lblClkDate = (Label)e.Row.FindControl("lblClkDate");
            TextBox txtReason = (TextBox)e.Row.FindControl("txtReason");
            HiddenField hdnManualEntryAlreadyExists = (HiddenField)e.Row.FindControl("hdnManualEntryAlreadyExists");
            if (hdnManualEntryAlreadyExists.Value == "Yes")
            {
                CheckBox cbSelect = (CheckBox)e.Row.FindControl("deletethis");
                cbSelect.Enabled = false;
                cbSelect.ToolTip = "Un processed data exists for this date";
            }
            Label lbl = (Label)e.Row.FindControl("lblClkDate");
            lbl.Style.Add("Width", "60px");
            if (lbl.Text != "")
            {
                lbl.Text = (Convert.ToDateTime(lbl.Text.ToString())).ToString(Session["MDateFormat"].ToString());
                e.Row.Cells[3].Controls.Add(lbl);
            }
            string NewTimeFormat = string.Empty;
            string TimeFormat = Convert.ToString(Session["ITimeFormat"]);
            if (TimeFormat == "HH:mm")
            {
                NewTimeFormat = "HH:mm:ss";
            }
            else
            {
                NewTimeFormat = "hh:mm:ss tt";
            }

            //if (Convert.ToString(Session["MDateFormat"]) == "dd/MM/yyyy")
            //{
            //    strDateFormat = "dd/mm/yyyy";
            //}
            //else if (Convert.ToString(Session["MDateFormat"]) == "MM/dd/yyyy")
            //{
            //    strDateFormat = "mm/dd/yyyy";
            //}
            //else if (Convert.ToString(Session["MDateFormat"]) == "dd-MMM-yyyy")
            //{
            //    strDateFormat = "dd-mmm-yyyy";
            //}

            //if (Convert.ToString(Session["ITimeFormat"]) == "HH:mm")
            //{
            //    strTimeFormat = "hh:mm";
            //}
            //else if (Convert.ToString(Session["ITimeFormat"]) == "LT")
            //{
            //    strTimeFormat = "hh:mm t";
            //}

            if (lblDiscrepancyType.Text != "A")
            {
                if (lblDiscrepancyType.Text == "I")
                {
                    txtReason.ReadOnly = false;
                    txtReason.CssClass = "form-control";
                    txtInDate.ReadOnly = false;
                    txtInDate.CssClass = "form-control";
                    //txtInDate.Attributes.Add("data-inputmask", "'alias':'" + strDateFormat + "'");
                    txtInDate.Attributes.Add("onmouseover", "javascript:return DynamicDatePicker('" + Hidden1.Value + "','" + txtInDate.ClientID + "')");
                    if (e.Row != null)
                    {
                        if ((txtInDate.Text != null) && (txtInDate.Text != string.Empty))
                        {
                            txtInDate.Text = (Convert.ToDateTime(txtInDate.Text.ToString())).ToString(Session["MDateFormat"].ToString());
                        }
                    }
                }
                else
                {
                    txtInDate.Text = (Convert.ToDateTime(txtInDate.Text.ToString())).ToString(Session["MDateFormat"].ToString());

                }
                if (lblDiscrepancyType.Text == "I")
                {
                    txtReason.ReadOnly = false;
                    txtReason.CssClass = "form-control";
                    txtInTime.ReadOnly = false;
                    txtInTime.CssClass = "form-control";
                    txtInTime.Attributes.Add("onmouseover", "javascript:return DynamicDatePickerForTime('" + hiddenTime.Value + "','" + txtInTime.ClientID + "')");
                    //txtInTime.Attributes.Add("data-inputmask", "'alias':'" + strTimeFormat + "'");
                    if (e.Row != null)
                    {
                        if ((txtInTime.Text != null) && (txtInTime.Text != string.Empty))
                        {
                            txtInTime.Text = Convert.ToDateTime(txtInTime.Text).ToString(NewTimeFormat);
                        }
                    }
                }
                else
                {
                    txtInTime.ReadOnly = true;
                    txtInTime.Attributes.Add("data-mask", "");
                    txtInTime.Attributes.Add("data-inputmask", "'alias':'" + strTimeFormat + "'");
                    txtInTime.Text = Convert.ToDateTime(txtInTime.Text).ToString(NewTimeFormat);
                    txtInTime.Style.Add("height", "23px");
                }
                if (lblDiscrepancyType.Text == "I")
                {
                    txtReason.ReadOnly = false;
                    txtReason.CssClass = "form-control";
                    ddlInPunch.Visible = true;
                    ddlInPunch.CssClass = "form-control select2";
                    ddlInPunch.Style.Add("min-width", "100px");
                    //InReaderId = Convert.ToInt32(lblOutReader.Text.ToString());
                    strOutReaderType = "In";
                    DataSet ds = objDis.LoadingPunchTypes(lblOutReader.Text, strOutReaderType);
                    ddlInPunch.DataTextField = "PunchType";
                    ddlInPunch.DataValueField = "PunchType_Id";
                    ddlInPunch.DataSource = ds;
                    ddlInPunch.DataBind();
                    ddlInPunch.Items.Insert(0, "Select");
                    ddlInPunch.SelectedIndex = 0;
                    foreach (ListItem item in ddlInPunch.Items)
                    {
                        item.Attributes.Add("title", item.Text);
                    }
                }
                else
                {
                    lblInPunchtype.Visible = true;
                }
                if (lblDiscrepancyType.Text == "O")
                {
                    txtReason.ReadOnly = false;
                    txtReason.CssClass = "form-control";
                    txtOutDate.ReadOnly = false;
                    txtOutDate.CssClass = "form-control";
                    //txtOutDate.Attributes.Add("data-inputmask", "'alias':'" + strDateFormat + "'");
                    txtOutDate.Attributes.Add("onmouseover", "javascript:return DynamicDatePicker('" + Hidden1.Value + "','" + txtOutDate.ClientID + "')");
                    if (e.Row != null)
                    {
                        if ((txtOutDate.Text != null) && (txtOutDate.Text != string.Empty))
                        {
                            txtOutDate.Text = (Convert.ToDateTime(txtOutDate.Text.ToString())).ToString(Session["MDateFormat"].ToString());
                        }
                    }
                }
                else
                {
                    //txtOutDate.Attributes.Add("data-inputmask", "'alias':'" + strDateFormat + "'");
                    txtOutDate.Text = (Convert.ToDateTime(txtOutDate.Text.ToString())).ToString(Session["MDateFormat"].ToString());
                    //txtOutDate.Style.Add("height", "20px");
                }
                if (lblDiscrepancyType.Text == "O")
                {
                    txtReason.ReadOnly = false;
                    txtReason.CssClass = "form-control";
                    txtOutTime.ReadOnly = false;
                    txtOutTime.CssClass = "form-control";
                    //txtOutTime.Attributes.Add("data-inputmask", "'alias':'" + strTimeFormat + "'");
                    txtOutTime.Attributes.Add("onmouseover", "javascript:return DynamicDatePickerForTime('" + hiddenTime.Value + "','" + txtOutTime.ClientID + "')");
                    if (e.Row != null)
                    {
                        if ((txtOutTime.Text != null) && (txtOutTime.Text != string.Empty))
                        {
                            txtOutTime.Text = Convert.ToDateTime(txtOutTime.Text).ToString(NewTimeFormat);
                        }
                    }
                }
                else
                {
                    txtOutTime.ReadOnly = true;
                    txtOutTime.Text = Convert.ToDateTime(txtOutTime.Text).ToString(NewTimeFormat);

                }
                if (lblDiscrepancyType.Text == "O")
                {
                    ddlOutPunch.Visible = true;
                    ddlOutPunch.CssClass = "form-control select2";
                    ddlOutPunch.Style.Add("min-width", "100px");
                    txtReason.ReadOnly = false;
                    txtReason.CssClass = "form-control";
                    //OutReaderId = Convert.ToInt32(lblInReader.Text.ToString());
                    strOutReaderType = "Out";
                    DataSet ds = objDis.LoadingPunchTypes(lblInReader.Text, strOutReaderType);
                    ddlOutPunch.DataTextField = "PunchType";
                    ddlOutPunch.DataValueField = "PunchType_Id";
                    ddlOutPunch.DataSource = ds;
                    ddlOutPunch.DataBind();
                    ddlOutPunch.Items.Insert(0, "Select");
                    ddlOutPunch.SelectedIndex = 0;
                    foreach (ListItem item in ddlOutPunch.Items)
                    {
                        item.Attributes.Add("title", item.Text);
                    }
                }
                else
                {
                    lblOutPunchType.Visible = true;
                }
            }
            else if (lblDiscrepancyType.Text == "A")
            {
                txtReason.ReadOnly = false;
                txtReason.CssClass = "form-control";

                txtInDate.ReadOnly = false;

                txtInDate.CssClass = "form-control";
                txtInDate.Attributes.Add("onmouseover", "javascript:return DynamicDatePicker('" + Hidden1.Value + "','" + txtInDate.ClientID + "')");
                //txtInDate.Attributes.Add("data-inputmask", "'alias':'" + strDateFormat + "'");
                if (e.Row != null)
                {
                    if ((txtInDate.Text != null) && (txtInDate.Text != string.Empty))
                    {
                        txtInDate.Text = (Convert.ToDateTime(txtInDate.Text.ToString())).ToString(Session["MDateFormat"].ToString());
                    }

                }

                txtInTime.ReadOnly = false;

                txtInTime.CssClass = "form-control";
                //txtInTime.Attributes.Add("data-inputmask", "'alias':'" + strTimeFormat + "'");
                txtInTime.Attributes.Add("onmouseover", "javascript:return DynamicDatePickerForTime('" + hiddenTime.Value + "','" + txtInTime.ClientID + "')");

                if (e.Row != null)
                {
                    if ((txtInTime.Text != null) && (txtInTime.Text != string.Empty))
                    {
                        txtInTime.Text = Convert.ToDateTime(txtInTime.Text).ToString(NewTimeFormat);
                        //txtInTime.Style.Add("height", "20px");
                    }

                }
                txtOutDate.ReadOnly = false;
                //Literal liteOutDate = new Literal();
                txtOutDate.CssClass = "form-control";
                //txtOutDate.Attributes.Add("data-inputmask", "'alias':'" + strDateFormat + "'");
                txtOutDate.Attributes.Add("onmouseover", "javascript:return DynamicDatePicker('" + Hidden1.Value + "','" + txtOutDate.ClientID + "')");
                //liteOutDate.Text = " <input type='image' src='../Images/dateicon.gif' id='butOutDate" + Convert.ToInt32(e.Row.RowIndex) + "' /><script type='text/javascript'>Calendar.setup({ inputField : '" + txtOutDate.ClientID + "', ifFormat : document.getElementById('ctl00_BodyContentPH_Hidden1').value, showsTime : false, button : 'butOutDate" + Convert.ToInt32(e.Row.RowIndex) + "', singleClick: true, step : 1}); document.getElementById('" + txtOutDate.ClientID + "').focus();</script>";
                // e.Row.Cells[8].Controls.Add(liteOutDate);
                if (e.Row != null)
                {
                    if ((txtOutDate.Text != null) && (txtOutDate.Text != string.Empty))
                    {
                        txtOutDate.Text = (Convert.ToDateTime(txtOutDate.Text.ToString())).ToString(Session["MDateFormat"].ToString());
                    }

                }
                txtOutTime.ReadOnly = false;
                //txtOutTime.Attributes.Add("data-inputmask", "'alias':'" + strTimeFormat + "'");
                txtOutTime.CssClass = "form-control";
                txtOutTime.Attributes.Add("onmouseover", "javascript:return DynamicDatePickerForTime('" + hiddenTime.Value + "','" + txtOutTime.ClientID + "')");

                if (e.Row != null)
                {
                    if ((txtOutTime.Text != null) && (txtOutTime.Text != string.Empty))
                    {
                        txtOutTime.Text = Convert.ToDateTime(txtOutTime.Text).ToString(NewTimeFormat);
                        //txtOutTime.Style.Add("height", "20px");
                    }

                }
                //OutPunch
                ddlOutPunch.Visible = true;
                ddlOutPunch.CssClass = "form-control select2";
                ddlOutPunch.Style.Add("min-width", "100px");
                //OutReaderId = Convert.ToInt32(lblInReader.Text.ToString());
                strOutReaderType = "Out";
                DataSet ds1 = objDis.LoadingPunchTypes("18000000", strOutReaderType);
                ddlOutPunch.DataTextField = "PunchType";
                ddlOutPunch.DataValueField = "PunchType_Id";
                ddlOutPunch.DataSource = ds1;
                ddlOutPunch.DataBind();
                ddlOutPunch.Items.Insert(0, "Select");
                ddlOutPunch.SelectedIndex = 0;
                foreach (ListItem item in ddlOutPunch.Items)
                {
                    item.Attributes.Add("title", item.Text);
                }
                //InPunch
                ddlInPunch.Visible = true;
                ddlOutPunch.CssClass = "form-control select2";
                ddlOutPunch.Style.Add("min-width", "100px");
                // InReaderId = Convert.ToInt32(lblOutReader.Text.ToString());
                strOutReaderType = "In";
                DataSet ds2 = objDis.LoadingPunchTypes("18000000", strOutReaderType);
                ddlInPunch.DataTextField = "PunchType";
                ddlInPunch.DataValueField = "PunchType_Id";
                ddlInPunch.DataSource = ds2;
                ddlInPunch.DataBind();
                ddlInPunch.Items.Insert(0, "Select");
                ddlInPunch.SelectedIndex = 0;
                foreach (ListItem item in ddlInPunch.Items)
                {
                    item.Attributes.Add("title", item.Text);
                }
            }
        }
    }
    public Control FindControlRecursive(Control Root, string Id)
    {
        if (Root.ID == Id)
            return Root;
        foreach (Control Ctl in Root.Controls)
        {
            Control FoundCtl = FindControlRecursive(Ctl, Id);
            if (FoundCtl != null)
                return FoundCtl;
        }
        return null;
    }
    protected void grd_OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Pager)
        {
        }
        if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
        {
            CheckBox chkBxSelect = (CheckBox)e.Row.Cells[0].FindControl("deletethis");
            CheckBox chkBxHeader = (CheckBox)this.grd.HeaderRow.FindControl("checkall");
            chkBxSelect.Attributes["onclick"] = string.Format("javascript:ChildClick(this,'{0}');", chkBxHeader.ClientID);
        }
    }
    #endregion
    protected void rbtn_WithoutPunchtype_CheckedChanged(object sender, EventArgs e)
    {
        grd.DataSource = null;
        grd.DataBind();
        btnSave.Visible = false;
    }
    protected void rbtn_WithPunchtype_CheckedChanged(object sender, EventArgs e)
    {
        grd.DataSource = null;
        grd.DataBind();
        btnSave.Visible = false;
    }
    protected string FindIsNightShift(string Employee_Code_Id, string Year, string month, string day)
    {
        DataSet dsActualShift = new DataSet();
        DataTable dtActualShift = new DataTable();
        string result = string.Empty;
        //IsNightShift = FindIsNightShift(Convert.ToString(drDiscrepancyList["Employee_Code_Id"]), Convert.ToDateTime(drDiscrepancyList["ClockDate"].ToString()).Year.ToString(), Convert.ToDateTime(drDiscrepancyList["ClockDate"].ToString()).Month.ToString(), Convert.ToDateTime(drDiscrepancyList["ClockDate"].ToString()).Day.ToString());
        dsActualShift = objManualEntry.SelectShiftIdforOnduty(Employee_Code_Id, Year, month, day);
        if (dsActualShift.Tables.Count > 0)
        {
            dtActualShift = dsActualShift.Tables[0];
            if (dtActualShift.Rows.Count > 0 && Convert.ToInt64(dtActualShift.Rows[0]["ShiftId"]) != -1)
            {
                string strActualShiftIn = (dtActualShift.Rows[0]["Shift_StartTime"].ToString());
                string strActualShiftOut = (dtActualShift.Rows[0]["Shift_EndTime"].ToString());
                if (strActualShiftIn.Contains("PM") == true)
                {
                    if (strActualShiftOut.Contains("AM"))
                    {
                        result = "YES";
                    }
                    else
                    {
                        result = "NO";
                    }
                }
            }
        }
        return result;
    }
}
