using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Threading;
using System.Resources;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                                          Started Date  : 18-July-2007
* Module          : Manual Time Entry                                                  Last Modified : 17-Aug-2007
* Programmer      : Kumuthini.L
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          : \\windev\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
****************************************************************************************************************************/
public partial class Transactions_Transaction_ManualTimeEntryModify : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    ManualEntryEventData objManualTimeEntry = new ManualEntryEventData();
    EntranceEventData objEntrance = new EntranceEventData();
    PunchTypesEventdata objPunchTypes = new PunchTypesEventdata();
    EmployeeEventData objEmployee = new EmployeeEventData();
    ShiftExchangeEventData objShiftExchange = new ShiftExchangeEventData();
    cls_Common objCommon = new cls_Common();
    int gintManualTimeEntryId, count;
    int gintUserId;
    string gstrDateFormat, gstrUserGroupName = string.Empty;
    bool IsOnDutyStatus = true;
    DataSet dsManualTimeEntry;
    string NewTimeFormat = string.Empty;
    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")
        {
            NewTimeFormat = "HH:mm:ss";
            hiddenTime.Value = "HH:mm";
            lbl_InTime.Text = rm.GetString("ManualEntry_InTime", ci);
            lbl_OutTime.Text = rm.GetString("ManualEntry_OutTime", ci);
        }
        else
        {
            NewTimeFormat = "hh:mm:ss tt";
            hiddenTime.Value = "LT";
            lbl_InTime.Text = rm.GetString("ManualEntry_InTime1", ci);
            lbl_OutTime.Text = rm.GetString("ManualEntry_OutTime1", ci);
        }
        Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
        Hidden2.Value = Convert.ToString(Session["ITimeFormat"]);
        gintManualTimeEntryId = Convert.ToInt32(Request.QueryString["MID"]);
        if (!Page.IsPostBack)
        {
            LoadStrings(ci);
            string strSetAlert = "";
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "','" + rm.GetString("Select", ci) + "');";
            strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_SelectInTerminal", ci) + "','" + rm.GetString("Alert_SelectPunchType", ci) + "','" + rm.GetString("Alert_FillInDate", ci) + "','" + rm.GetString("Alert_FillInTime", ci) + "','" + rm.GetString("Alert_SelectOutTerminal", ci) + "','" + rm.GetString("Alert_SelectOutPunchTypes", ci) + "','" + rm.GetString("Alert_InTimeCorrectFormat", ci) + "','" + rm.GetString("Alert_InTimeCorrectFormat", ci) + "','" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "','" + rm.GetString("Alert_OutDate", ci) + "','" + rm.GetString("Alert_OutTime", ci) + "','" + rm.GetString("Alert_EntertheEmployeeID", ci) + "','" + rm.GetString("Validator_ManualTimeEntry_Reason", ci) + "');";
            strSetAlert = strSetAlert + "SetAlertCheckDate('" + rm.GetString("Alert_Validmonthdatefourdigit", ci) + "','" + rm.GetString("Alert_Validdatemonthfourdigit", ci) + "','" + rm.GetString("Alert_Invalidyearentry", ci) + "','" + rm.GetString("Alert_Invalidmonthentry", ci) + "','" + rm.GetString("Alert_Invaliddateentry", ci) + "','" + rm.GetString("Alert_IncorrectdateformatDDMONYYYY", ci) + "','" + rm.GetString("Alert_Entertime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "','" + rm.GetString("Alert_Invalidtimeformat1", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMM", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMMTT", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "','" + rm.GetString("Alert_Norecordsfound", ci) + "','" + rm.GetString("Alert_Errorinretrivingdetails", ci) + "','" + rm.GetString("Alert_SelectEntranceID", ci) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
            UserRights();
            txt_EmployeeId.Focus();
            FillEntranceDropDown();
            GetManualEntryDetails();
            //objCommon.menu("ManualEntries", "26");
        }
        gintUserId = Int32.Parse(Session["EmpAutoID"].ToString());
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
    }
    // **********************************************************************************************************************
    // Function Name : FillEntranceDropDown
    // Purpose :
    //     The FillEntranceDropDown Method is used to fill the Entrance Names in the Entrance Name DropDown List
    // **********************************************************************************************************************
    public void FillEntranceDropDown()
    {
        DataSet ds_Entrance = new DataSet();
        ds_Entrance = objEntrance.SelectEntranceDetailsForManualTimeEntry();
        string strEntranceId = string.Empty;
        if (ds_Entrance.Tables[0].Rows.Count > 0)
        {
            strEntranceId = ds_Entrance.Tables[0].Rows[0]["Entrance_Id"].ToString();
            ddlEntranceName.DataSource = ds_Entrance;
            ddlEntranceName.DataTextField = "Entrance_Name";
            ddlEntranceName.DataValueField = "Entrance_Id";
            ddlEntranceName.DataBind();
            ddlEntranceName.SelectedValue = strEntranceId;
            FillTerminals(strEntranceId);
        }
        ds_Entrance.Dispose();
        foreach (ListItem item in ddlEntranceName.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    // **********************************************************************************************************************
    // Function Name : FillPunchTypesDropDown
    // Purpose :
    //     The FillPunchTypesDropDown Method is used to fill the In Punch Types and Out Punch Types in the 
    //     In Punch Types and Out Punch Types Dropdown Lists
    // **********************************************************************************************************************
    private void FillPunchTypesDropDown()
    {
        DataSet dsInPunchTypes = new DataSet();
        DataSet dsOutPunchTypes = new DataSet();
        int intInReaderId = 0, intOutReaderId = 0;
        if (hdn_InTerminal.Value != "")
        {
            intInReaderId = Convert.ToInt32(hdn_InTerminal.Value);
        }
        if (hdn_OutTerminal.Value != "")
        {
            intOutReaderId = Convert.ToInt32(hdn_OutTerminal.Value);
        }
        dsInPunchTypes = objPunchTypes.Select_PunchTypeDetailsByTerminalId(intInReaderId, "I");
        ddlInPunchTypes.Items.Add("-Select-");
        ddlOutPunchTypes.Items.Add("-Select-");
        if (dsInPunchTypes.Tables[0].Rows.Count > 0)
        {
            ddlInPunchTypes.DataSource = dsInPunchTypes;
            ddlInPunchTypes.DataTextField = "PunchType";
            ddlInPunchTypes.DataValueField = "PunchType";
            ddlInPunchTypes.DataBind();
            foreach (ListItem item in ddlInPunchTypes.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
        dsOutPunchTypes = objPunchTypes.Select_PunchTypeDetailsByTerminalId(intOutReaderId, "O");
        if (dsOutPunchTypes.Tables[0].Rows.Count > 0)
        {
            ddlOutPunchTypes.DataSource = dsOutPunchTypes;
            ddlOutPunchTypes.DataTextField = "PunchType";
            ddlOutPunchTypes.DataValueField = "PunchType";
            ddlOutPunchTypes.DataBind();
            foreach (ListItem item in ddlOutPunchTypes.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    public void FillTerminals(string strEntranceId)
    {
        DataSet dsInTerminals = new DataSet();
        dsInTerminals = objEntrance.SelectEntranceINOUTDetailsByIDview(Convert.ToInt32(strEntranceId), "In");
        dsInTerminals.Tables[0].TableName = "InTerminals";
        string strInTerminalId = string.Empty;
        if (dsInTerminals.Tables["InTerminals"].Rows.Count > 0)
        {
            strInTerminalId = dsInTerminals.Tables["InTerminals"].Rows[0]["Reader_Id"].ToString();
            ddlInTerminals.DataSource = dsInTerminals;
            ddlInTerminals.DataTextField = "Terminal_Name";
            ddlInTerminals.DataValueField = "Reader_Id";
            ddlInTerminals.DataBind();
            FillInPunchTypes(strInTerminalId);
        }
        hdn_InTerminal.Value = ddlInTerminals.SelectedItem.Value;
        dsInTerminals.Dispose();
        DataSet dsOutTerminals = new DataSet();
        dsOutTerminals = objEntrance.SelectEntranceINOUTDetailsByIDview(Convert.ToInt32(strEntranceId), "Out");
        dsOutTerminals.Tables[0].TableName = "OutTerminals";
        string strOutTerminalId = string.Empty;
        if (dsOutTerminals.Tables[0].Rows.Count > 0)
        {
            strOutTerminalId = dsOutTerminals.Tables["OutTerminals"].Rows[0]["Reader_Id"].ToString();
            ddlOutTerminals.DataSource = dsOutTerminals;
            ddlOutTerminals.DataTextField = "Terminal_Name";
            ddlOutTerminals.DataValueField = "Reader_Id";
            ddlOutTerminals.DataBind();
            FillOutPunchTypes(strOutTerminalId);
        }
        hdn_OutTerminal.Value = ddlOutTerminals.SelectedItem.Value;
        dsOutTerminals.Dispose();
    }
    public void FillInPunchTypes(string strEntranceId)
    {
        DataSet dsInPunchTypes = new DataSet();
        dsInPunchTypes = objPunchTypes.Select_PunchTypeDetailsByTerminalId(Convert.ToInt32(strEntranceId), "I");
        if (dsInPunchTypes.Tables.Count > 0)
        {
            if (dsInPunchTypes.Tables[0].Rows.Count > 0)
            {
                ddlInPunchTypes.DataSource = dsInPunchTypes;
                ddlInPunchTypes.DataTextField = "PunchType";
                ddlInPunchTypes.DataValueField = "PunchType_Value";
                ddlInPunchTypes.DataBind();
            }
            dsInPunchTypes.Dispose();
        }
    }
    public void FillOutPunchTypes(string strEntranceId)
    {
        DataSet dsOutPunchTypes = new DataSet();
        dsOutPunchTypes = objPunchTypes.Select_PunchTypeDetailsByTerminalId(Convert.ToInt32(strEntranceId), "O");
        if (dsOutPunchTypes.Tables.Count > 0)
        {
            if (dsOutPunchTypes.Tables[0].Rows.Count > 0)
            {
                ddlOutPunchTypes.DataSource = dsOutPunchTypes;
                ddlOutPunchTypes.DataTextField = "PunchType";
                ddlOutPunchTypes.DataValueField = "PunchType_Value";
                ddlOutPunchTypes.DataBind();
            }
        }
        ddlOutPunchTypes.Dispose();
    }
    // **********************************************************************************************************************
    // Function Name : FillTerminalsDropDown
    // Purpose :
    //     The FillTerminalsDropDown Method is used to fill the In Terminals and Out Terminals in the 
    //     In Terminals and Out Terminals Dropdown Lists
    // **********************************************************************************************************************
    private void FillTerminalsDropDown(int intEntranceId)
    {
        DataSet ds_EntranceDetails = new DataSet();
        ds_EntranceDetails = objEntrance.SelectEntranceINOUTDetailsByIDview(intEntranceId, "In");
        if (ds_EntranceDetails.Tables[0].Rows.Count > 0)
        {
            ddlInTerminals.DataSource = ds_EntranceDetails;
            ddlInTerminals.DataTextField = "Terminal_Name";
            ddlInTerminals.DataValueField = "Reader_Id";
            ddlInTerminals.DataBind();
        }
        ddlInTerminals.Items.Insert(0, "-Select-");
        foreach (ListItem item in ddlInTerminals.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
        ds_EntranceDetails = objEntrance.SelectEntranceINOUTDetailsByIDview(intEntranceId, "Out");
        if (ds_EntranceDetails.Tables[0].Rows.Count > 0)
        {
            ddlOutTerminals.DataSource = ds_EntranceDetails;
            ddlOutTerminals.DataTextField = "Terminal_Name";
            ddlOutTerminals.DataValueField = "Reader_Id";
            ddlOutTerminals.DataBind();
        }
        ddlOutTerminals.Items.Insert(0, "-Select-");
        foreach (ListItem item in ddlOutTerminals.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    // **********************************************************************************************************************
    // Function Name : GetManualEntryDetails
    // Purpose :
    //     The GetManualEntryDetails Method is used to get the Details of the Manual Time Entry for a particular 
    //     Manual Time Entry ID and the details are assigned to the appropriate Controls
    // **********************************************************************************************************************
    private void GetManualEntryDetails()
    {
        dsManualTimeEntry = new DataSet();
        DataTable dtManualTimeEntry = new DataTable();
        dsManualTimeEntry = objManualTimeEntry.SelectManualEntryById(gintManualTimeEntryId);
        if (dsManualTimeEntry.Tables.Count > 0)
        {
            dtManualTimeEntry = dsManualTimeEntry.Tables[0];
            if (dtManualTimeEntry.Rows.Count > 0)
            {
                if (dtManualTimeEntry.Rows[0]["Employee_Code_Id"] != DBNull.Value)
                {
                    txt_EmployeeId.Text = Convert.ToString(dtManualTimeEntry.Rows[0]["Employee_Code_Id"]);
                }
                if (dtManualTimeEntry.Rows[0]["Employee_Name"] != DBNull.Value)
                {
                    txt_Name.Text = Convert.ToString(dtManualTimeEntry.Rows[0]["Employee_Name"]);
                }
                if (dtManualTimeEntry.Rows[0]["Mdate"] != DBNull.Value)
                {
                    lbl_EntryDate.Text = Convert.ToString(dtManualTimeEntry.Rows[0]["Mdate"]);
                }
                if (dtManualTimeEntry.Rows[0]["Entrance_Id"] != DBNull.Value)
                {
                    ddlEntranceName.SelectedValue = dtManualTimeEntry.Rows[0]["Entrance_Id"].ToString();
                }
                if (dtManualTimeEntry.Rows[0]["ManualTimeEntry_InReaderID"] != DBNull.Value)
                {
                    ddlInTerminals.SelectedValue = dtManualTimeEntry.Rows[0]["ManualTimeEntry_InReaderID"].ToString();
                    hdn_InTerminal.Value = dtManualTimeEntry.Rows[0]["ManualTimeEntry_InReaderID"].ToString();
                }
                if (dtManualTimeEntry.Rows[0]["ManualTimeEntry_OutReaderID"] != DBNull.Value)
                {
                    ddlOutTerminals.SelectedValue = dtManualTimeEntry.Rows[0]["ManualTimeEntry_OutReaderID"].ToString();
                    hdn_OutTerminal.Value = dtManualTimeEntry.Rows[0]["ManualTimeEntry_OutReaderID"].ToString();
                }
                if (dtManualTimeEntry.Rows[0]["IsOnduty"] != DBNull.Value)
                {
                    ViewState["IsOnDuty"] = dtManualTimeEntry.Rows[0]["IsOnduty"].ToString();
                }
                if (dtManualTimeEntry.Rows[0]["InDate"] != DBNull.Value)
                {
                    txt_InDate.Text = Convert.ToString(dtManualTimeEntry.Rows[0]["InDate"]);
                    ViewState["FromDate"] = txt_InDate.Text;
                }
                if (dtManualTimeEntry.Rows[0]["ManualTimeEntry_InTime"] != DBNull.Value)
                {
                    txt_ShiftIn.Text = Convert.ToString(dtManualTimeEntry.Rows[0]["ManualTimeEntry_InTime"]);
                }
                if (dtManualTimeEntry.Rows[0]["ManualTimeEntry_InPunchType"] != DBNull.Value)
                {
                    ddlInPunchTypes.SelectedValue = dtManualTimeEntry.Rows[0]["ManualTimeEntry_InPunchType"].ToString();
                    hdn_InPunchType.Value = dtManualTimeEntry.Rows[0]["ManualTimeEntry_InPunchType"].ToString();
                }
                if (dtManualTimeEntry.Rows[0]["OutDate"] != DBNull.Value)
                {
                    txt_OutDate.Text = Convert.ToString(dtManualTimeEntry.Rows[0]["OutDate"]);
                    ViewState["ToDate"] = txt_OutDate.Text;
                }
                if (dtManualTimeEntry.Rows[0]["ManualTimeEntry_OutTime"] != DBNull.Value)
                {
                    txt_ShiftOut.Text = Convert.ToString(dtManualTimeEntry.Rows[0]["ManualTimeEntry_OutTime"]);
                }
                if (dtManualTimeEntry.Rows[0]["ManualTimeEntry_OutPunchType"] != DBNull.Value)
                {
                    ddlOutPunchTypes.SelectedValue = dtManualTimeEntry.Rows[0]["ManualTimeEntry_OutPunchType"].ToString();
                    hdn_OutPunchType.Value = dtManualTimeEntry.Rows[0]["ManualTimeEntry_OutPunchType"].ToString();
                }
                if (dtManualTimeEntry.Rows[0]["ManualTimeEntry_Reason"] != DBNull.Value)
                {
                    txt_Reason.Text = Convert.ToString(dtManualTimeEntry.Rows[0]["ManualTimeEntry_Reason"]);
                }
                if (dtManualTimeEntry.Rows[0]["ManualTimeEntry_Status"] != DBNull.Value && dtManualTimeEntry.Rows[0]["ManualTimeEntry_Status"].ToString() != string.Empty)
                {
                    lbl_Status.Text = Convert.ToString(dtManualTimeEntry.Rows[0]["ManualTimeEntry_Status"]);
                }
                if (dtManualTimeEntry.Rows[0]["IsOnDuty"].ToString() == "True")
                {
                    lbl_EntryType.Text = "On Duty";
                }
                else
                {
                    lbl_EntryType.Text = "Manual Entry";
                }
            }
        }
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        lbl_Error.Text = "";
        DataSet ds_EntranceDetails = new DataSet();
        int intEntranceId, intInReaderId = 0, intOutReaderId = 0;
        if (Page.IsValid)
        {
            int intCount = 0;
            if (Convert.ToString(Session["UserGroupName"]) != "SuperAdmin" && Convert.ToString(Session["HasSpecialRights"]) != "True")
            {
                intCount = objManualTimeEntry.CheckProcessedManualEntry(gintManualTimeEntryId);
                if (intCount > 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_RecordAlreadyProcessed", ci) + "');", true);
                    return;
                }
            }
            if ((txt_InDate.Text == "") && (txt_ShiftIn.Text == "") && (txt_OutDate.Text == "") && (txt_ShiftOut.Text == ""))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_EitherInDateTime", ci) + "');", true);
                txt_InDate.Focus();
                return;
            }
            if ((txt_InDate.Text != "") && (txt_ShiftIn.Text == ""))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_InTimeEmpty", ci) + "');", true);
                txt_ShiftIn.Focus();
                return;
            }
            if ((txt_InDate.Text == string.Empty) && (txt_ShiftIn.Text != string.Empty))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_InDate", ci) + "');", true);
                txt_InDate.Focus();
                return;
            }
            if ((txt_InDate.Text != "") && (txt_ShiftIn.Text != "") && (hdn_InPunchType.Value == "-Select-" || hdn_InPunchType.Value == ""))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_SelectPunchType", ci) + "');", true);
                ddlInPunchTypes.Focus();
                return;
            }
            if ((txt_OutDate.Text != "") && (txt_ShiftOut.Text == ""))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_OutTimeEmpty", ci) + "');", true);
                txt_ShiftOut.Focus();
                return;
            }
            if ((txt_OutDate.Text == "") && (txt_ShiftOut.Text != ""))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_OutDateEmpty", ci) + "');", true);
                txt_OutDate.Focus();
                return;
            }
            if ((txt_OutDate.Text != "") && (txt_ShiftOut.Text != "") && (hdn_OutPunchType.Value == "-Select-" || hdn_OutPunchType.Value == ""))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_SelectOutPunchType", ci) + "');", true);
                ddlOutPunchTypes.Focus();
                return;
            }
            string strInDate = "", strOutDate = "";
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strInDate = objCommon.DateConvertion(gstrDateFormat, txt_InDate.Text);
            strOutDate = objCommon.DateConvertion(gstrDateFormat, txt_OutDate.Text);
            DateTime dtmFromDate = new DateTime();
            DateTime dtmToDate = new DateTime();
            DataSet ds_Year = new DataSet();
            ds_Year = objCommon.GetYearsetting();
            if (ds_Year != null)
            {
                if (ds_Year.Tables[0].Rows.Count > 0)
                {
                    if (ds_Year.Tables[0].Rows[0]["FromDate"] != DBNull.Value)
                    {
                        dtmFromDate = DateTime.Parse(ds_Year.Tables[0].Rows[0]["FromDate"].ToString());
                    }
                    if (ds_Year.Tables[0].Rows[0]["ToDate"] != DBNull.Value)
                    {
                        dtmToDate = DateTime.Parse(ds_Year.Tables[0].Rows[0]["ToDate"].ToString());
                    }
                }
            }
            string strMaxPayrollClosureDate = objManualTimeEntry.SelectMaxPayrollClosureDate();
            if (txt_InDate.Text != "")
            {
                DateTime dtmInDate = DateTime.Parse(strInDate);
                if ((dtmFromDate <= dtmInDate) && (dtmInDate <= dtmToDate))
                {
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_ActiveYear", ci) + "');", true);
                    txt_InDate.Focus();
                    return;
                }
                if (Convert.ToString(Session["UserGroupName"]) != "SuperAdmin" && Convert.ToString(Session["HasSpecialRights"]) != "True")
                {
                    if (strMaxPayrollClosureDate != "")
                    {
                        DateTime dtmMaxPayrollClosureDate = DateTime.Parse(strMaxPayrollClosureDate);
                        if (dtmInDate <= dtmMaxPayrollClosureDate)
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_InDateProcessed", ci) + "');", true);
                            txt_InDate.Focus();
                            return;
                        }
                    }
                }
            }
            if (txt_OutDate.Text != "")
            {
                DateTime dtmOutDate = DateTime.Parse(strOutDate);
                if ((dtmFromDate <= dtmOutDate) && (dtmOutDate <= dtmToDate))
                {
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_OutdateSetting", ci) + "');", true);
                    txt_OutDate.Focus();
                    return;
                }
                if (gstrUserGroupName != "SuperAdmin")
                {
                    if (strMaxPayrollClosureDate != "")
                    {
                        DateTime dtmMaxPayrollClosureDate = DateTime.Parse(strMaxPayrollClosureDate);
                        if (dtmOutDate <= dtmMaxPayrollClosureDate)
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_InvalidPayroll", ci) + "');", true);
                            string scrt = "<script>ManualTimeEntry()</script>";
                            this.Page.RegisterStartupScript("a", scrt);
                            hdn_InPunchType.Value = "";
                            hdn_OutPunchType.Value = "";
                            hdn_InTerminal.Value = "";
                            hdn_OutTerminal.Value = "";
                            txt_OutDate.Focus();
                            return;
                        }
                    }
                }
            }
            DateTime dtmCurrentDateTime = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
            DateTime dtmIn = new DateTime();
            DateTime dtmOut = new DateTime();
            string strIn = string.Empty, strOut = string.Empty;
            int intShiftId = 0;
            string strIsINPunchPresent = "N";
            if ((txt_InDate.Text != "") && (txt_ShiftIn.Text != ""))
            {
                strIsINPunchPresent = "Y";
                strIn = strInDate + " " + txt_ShiftIn.Text;
                dtmIn = DateTime.Parse(strIn);
                if (ViewState["IsOnDuty"] == null)
                {
                    IsOnDutyStatus = false;
                }
                else
                {
                    if (ViewState["IsOnDuty"].ToString() == string.Empty && ViewState["IsOnDuty"].ToString() == "False")
                    {
                        IsOnDutyStatus = false;
                    }
                }
                if (IsOnDutyStatus == false)
                {
                    if (dtmIn > dtmCurrentDateTime)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_InDategreater", ci) + "');", true);
                        txt_ShiftIn.Focus();
                        return;
                    }
                }
                intShiftId = objShiftExchange.SelectShiftId(txt_EmployeeId.Text, dtmIn.Year.ToString(), dtmIn.Month.ToString(), dtmIn.Day.ToString());
                if (intShiftId == 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_ManualTimeEntrycannotbemodifiedasshifthasnotbeenallocatedforthisdate", ci) + "');", true);
                    txt_InDate.Focus();
                    return;
                }
            }
            if ((txt_OutDate.Text != "") && (txt_ShiftOut.Text != ""))
            {
                strOut = strOutDate + " " + txt_ShiftOut.Text;
                dtmOut = DateTime.Parse(strOut);
                if (ViewState["IsOnDuty"] == null)
                {
                    IsOnDutyStatus = false;
                }
                else
                {
                    if (ViewState["IsOnDuty"].ToString() == string.Empty && ViewState["IsOnDuty"].ToString() == "False")
                    {
                        IsOnDutyStatus = false;
                    }
                }
                if (IsOnDutyStatus == false)
                {
                    if (dtmOut > dtmCurrentDateTime)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_OutDategreater", ci) + "');", true);
                        txt_ShiftOut.Focus();
                        return;
                    }
                }
                if (strIsINPunchPresent == "N")
                {
                    intShiftId = objShiftExchange.SelectShiftId(txt_EmployeeId.Text, dtmOut.Year.ToString(), dtmOut.Month.ToString(), dtmOut.Day.ToString());
                    if (intShiftId == 0)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_ManualTimeEntrycannotbemodifiedasshifthasnotbeenallocatedforthisdate", ci) + "');", true);
                        txt_OutDate.Focus();
                        return;
                    }
                }
            }
            /* If all InDate, InTime, OutDate and OutTime are not NULL, then check whether difference between In DateTime and Out DateTime  
               should not exceed 24 Hours and also check whether In DateTime cannot be greater than Out DateTime   
            */
            if ((txt_InDate.Text != "") && (txt_ShiftIn.Text != "") && (txt_OutDate.Text != "") && (txt_ShiftOut.Text != ""))
            {
                TimeSpan tsDiff = dtmOut.Subtract(dtmIn);
                double dTotalHrs = tsDiff.TotalHours;
                if (dTotalHrs > 24)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_DiffBetInandOutTime", ci) + "');", true);
                    txt_InDate.Focus();
                    return;
                }
                else if (dTotalHrs < 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_InDatetimeGreater", ci) + "');", true);
                    txt_InDate.Focus();
                    return;
                }
            }
            string strDateOfJoining = "";
            DataSet dsEmployeeDOJ = new DataSet();
            dsEmployeeDOJ = objEmployee.GetEmployeeDateOfJoining(txt_EmployeeId.Text, "", "", "S");
            if (dsEmployeeDOJ.Tables[0].Rows.Count > 0)
            {
                if (dsEmployeeDOJ.Tables[0].Rows[0]["DateOfJoining"] != DBNull.Value)
                {
                    strDateOfJoining = dsEmployeeDOJ.Tables[0].Rows[0]["DateOfJoining"].ToString();
                }
            }
            if (strDateOfJoining != "")
            {
                DateTime dtmDateOfJoining = DateTime.Parse(strDateOfJoining);
                if (txt_InDate.Text != "")
                {
                    string strdtmInDate = objCommon.DateConvertion(gstrDateFormat, txt_InDate.Text);
                    DateTime dtmInDate = DateTime.Parse(strdtmInDate);
                    if (dtmInDate < dtmDateOfJoining)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_IndateGreaterThanEmpDOJ", ci) + "');", true);
                        txt_InDate.Focus();
                        return;
                    }
                }
                if (txt_OutDate.Text != "")
                {
                    string strdtmOutDate = objCommon.DateConvertion(gstrDateFormat, txt_OutDate.Text);
                    DateTime dtmOutDate = DateTime.Parse(strdtmOutDate);
                    if (dtmOutDate < dtmDateOfJoining)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_OutdategreaterthanEmpDOJ", ci) + "');", true);
                        txt_OutDate.Focus();
                        return;
                    }
                }
            }
            if (IsOnDutyStatus == false)
            {
                count = objManualTimeEntry.CheckManualEntryUpdate(gintManualTimeEntryId, txt_EmployeeId.Text, strInDate, txt_ShiftIn.Text, strOutDate, txt_ShiftOut.Text);
            }
            else
            {
                count = objManualTimeEntry.CheckManualODEntryUpdate(gintManualTimeEntryId, txt_EmployeeId.Text, strInDate, txt_ShiftIn.Text, strOutDate, txt_ShiftOut.Text);
            }
            if (count == 0)
            {
                if (hdn_InTerminal.Value != "")
                {
                    intInReaderId = Convert.ToInt32(hdn_InTerminal.Value);
                }
                else
                {
                    intInReaderId = 0;
                }
                if (hdn_OutTerminal.Value != "")
                {
                    intOutReaderId = Convert.ToInt32(hdn_OutTerminal.Value);
                }
                else
                {
                    intOutReaderId = 0;
                }
                string strEntryDate = objCommon.DateConvertion(gstrDateFormat, lbl_EntryDate.Text);
                if (txt_ShiftIn.Text != string.Empty)
                {
                    txt_ShiftIn.Text = Convert.ToDateTime(txt_ShiftIn.Text).ToString("hh:mm:ss tt");
                }
                if (txt_ShiftOut.Text != string.Empty)
                {
                    txt_ShiftOut.Text = Convert.ToDateTime(txt_ShiftOut.Text).ToString("hh:mm:ss tt");
                }
                objManualTimeEntry.UpdateManualEntry(gintManualTimeEntryId, txt_EmployeeId.Text, strEntryDate, strInDate, txt_ShiftIn.Text, hdn_InPunchType.Value, strOutDate, txt_ShiftOut.Text, hdn_OutPunchType.Value, intInReaderId, intOutReaderId, txt_Reason.Text.Trim().Replace("'", "''"), DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"), gintUserId);
                //The Below code added by Karthikeyan.R on 07-03-2016 once the user modify the approved manual time entry then data data reprocess the current date.
                if (lbl_Status.Text == "Approved")
                {
                    if (strInDate == "" || strInDate == string.Empty)
                    {
                        strInDate = strOutDate;
                    }
                    if (strOutDate == "" || strOutDate == string.Empty)
                    {
                        strOutDate = strInDate;
                    }
                    objManualTimeEntry.ReprocessEmployeeData(DateTime.Parse(strInDate), DateTime.Parse(strOutDate), txt_EmployeeId.Text);
                }
                //Reprocess for the modified date.
                //if (lbl_Status.Text == "Approved")
                //{
                //    if (Convert.ToString(ViewState["FromDate"]) == string.Empty || Convert.ToString(ViewState["FromDate"]) == "")
                //    {
                //        ViewState["FromDate"] = ViewState["ToDate"];
                //    }
                //    if (Convert.ToString(ViewState["ToDate"]) == string.Empty || Convert.ToString(ViewState["ToDate"]) == "")
                //    {
                //        ViewState["ToDate"] = ViewState["FromDate"];
                //    }
                //    objManualTimeEntry.ReprocessEmployeeData(DateTime.Parse(ViewState["FromDate"].ToString()), DateTime.Parse(ViewState["ToDate"].ToString()), txt_EmployeeId.Text);
                //}
                //End
                if (IsOnDutyStatus == false)
                {
                    string strScript = string.Empty;
                    strScript = strScript + "<script language='javascript'>";
                    strScript = strScript + "alert('" + rm.GetString("Msg_ManualTimeEntryModifiedSucessfully", ci) + "');";
                    strScript = strScript + "location.replace('TA_UI_Transactions_ManualTimeEntry.aspx');";
                    strScript = strScript + "</script>";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                }
                else
                {
                    string strScript = string.Empty;
                    strScript = strScript + "<script language='javascript'>";
                    strScript = strScript + "alert('" + rm.GetString("Msg_ManualTimeEntryModifiedSucessfully", ci) + "');";
                    strScript = strScript + "location.replace('TA_UI_Transactions_ManualTimeEntry.aspx');";
                    strScript = strScript + "</script>";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                }
            }
            else
            {
                if (IsOnDutyStatus == false)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_ManualEntryAlreadyhavebeenAddedforthisday", ci) + "');", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Msg_ManualEntryAlreadyhavebeenAddedforthisday", ci) + "');", true);
                }
            }
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Transactions/TA_UI_Transactions_ManualTimeEntry.aspx");
    }
    /// <summary>
    /// Compare Current Date with In Date 
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void CheckInDate(object s, ServerValidateEventArgs e)
    {
        //if (ViewState["IsOnDuty"] == null)
        //{
        //    IsOnDutyStatus = false;
        //}
        //else
        //{
        //    if (ViewState["IsOnDuty"].ToString() == string.Empty && ViewState["IsOnDuty"].ToString() == "False")
        //    {
        //        IsOnDutyStatus = false;
        //    }
        //}
        //if (IsOnDutyStatus == false)
        //{
        string strInDate = "";
        if (txt_InDate.Text != "")
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strInDate = objCommon.DateConvertion(gstrDateFormat, txt_InDate.Text);
            DateTime dtCurrent = DateTime.Now;
            DateTime dtInDate = DateTime.Parse(strInDate);
            int Days = dtCurrent.CompareTo(dtInDate);
            if (Days > 0)
            {
                e.IsValid = true;
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Validator_ManualTimeEntry_InDate", ci) + "');", true);
                txt_InDate.Focus();
                e.IsValid = false;
            }
        }
        else
        {
            e.IsValid = true;
        }
        //}
    }
    /// <summary>
    /// Compare Current Date with Out Date
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    /// 
    public void CheckOutDate(object s, ServerValidateEventArgs e)
    {
        //bool IsOnDuty = true;
        //if (ViewState["IsOnDuty"] == null)
        //{
        //    IsOnDuty = false;
        //}
        //else
        //{
        //    if (ViewState["IsOnDuty"].ToString() == string.Empty && ViewState["IsOnDuty"].ToString() == "False")
        //    {
        //        IsOnDuty = false;
        //    }
        //}
        //if (IsOnDuty == false)
        //{
        string strOutDate = "";
        if (txt_OutDate.Text != "")
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strOutDate = objCommon.DateConvertion(gstrDateFormat, txt_OutDate.Text);
            DateTime dtCurrent = DateTime.Now;
            DateTime dtOutDate = DateTime.Parse(strOutDate);
            int Days = dtCurrent.CompareTo(dtOutDate);
            if (Days > 0)
            {
                e.IsValid = true;
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Validator_ManualTimeEntry_OutDate", ci) + "');", true);
                txt_OutDate.Focus();
                e.IsValid = false;
            }
        }
        else
        {
            e.IsValid = true;
        }
        //}
    }
    /// <summary>
    /// Compare OutTime with InTime
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void CheckTime(object s, ServerValidateEventArgs e)
    {
        if ((txt_ShiftIn.Text != "") && (txt_ShiftOut.Text != ""))
        {
            DateTime dtmInTime = DateTime.Parse(txt_ShiftIn.Text);
            DateTime dtmOutTime = DateTime.Parse(txt_ShiftOut.Text);
            int day = dtmOutTime.CompareTo(dtmInTime);
            if (day > 0)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("UserGroups_ManualTimeEntry", ci);
        lbl_EmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lbl_EmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lbl_EntranceName.Text = rm.GetString("EntranceMaster_Entrance_Name", ci);
        lbl_InDate.Text = rm.GetString("ManualTimeEntry_InDate", ci);
        lblEntryType.Text = rm.GetString("ManualTimeEntryType", ci);
        lbl_InTerminal.Text = rm.GetString("Label_InTerminals", ci);
        lbl_InPunchType.Text = rm.GetString("ManualTimeEntry_InPunchType", ci);
        lbl_ManualTimeEntryDate.Text = rm.GetString("ManualTimeEntry_ManualTimeEntryDate", ci);
        lbl_OutDate.Text = rm.GetString("ManualTimeEntry_OutDate", ci);
        lbl_OutTerminal.Text = rm.GetString("Label_OutTerminals", ci);
        lbl_OutPunchType.Text = rm.GetString("ManualTimeEntry_OutPunchType", ci);
        lbl_Reason.Text = rm.GetString("email_Reason", ci);
        lbl_Status1.Text = rm.GetString("email_Status", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        btn_Add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        ddlEntranceName.Items[0].Text = rm.GetString("Select", ci);
        lblFilter.Text = rm.GetString("lblFilter", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(26, Convert.ToInt32(Session["userGroup_Id"]), "modifyPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
    }
}
