using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Threading;
using System.Reflection;
using System.Resources;
public partial class Transactions_TA_UI_Transaction_Discrepancy : Base.BasePage
{
    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();
    string strFromdate, gstrDateFormat, strTodate;
    CultureInfo ci;
    int intIndividualUser = 0;
    protected override void Page_Load(object sender, EventArgs e)
    {
        if (Session["Session_ActiveLanguage"] == null)
        {
            string strScript1 = "<script language='javascript' type='text/javascript'>";
            strScript1 += "alert('Session Expired...');self.close();";
            strScript1 += " </script>";
            this.RegisterStartupScript("closeScript1", strScript1);
            return;
        }
        // session checking
        //base.Page_Load(sender, e);
        //get the culture info to set the language       
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
        loadcss();
        string TimeFormat = string.Empty;
        TimeFormat = Convert.ToString(Session["ITimeFormat"]);
        if (TimeFormat == "%H:%M")
        {
            hiddenTime.Value = "%H:%M:%S";
        }
        else
        {
            hiddenTime.Value = "%I:%M:%S %p";
        }
        //Set UserRights
        UserRights();
        if (!Page.IsPostBack)
        {
            btnSave.Focus();
            hid1.Value = "";
            hid2.Value = "";
            hid3.Value = "";
            hid4.Value = "";
            hid5.Value = "";
            hid6.Value = "";
            hid7.Value = "";
            hid8.Value = "";
            hid9.Value = "";
            hid10.Value = "";
            hidTime1.Value = "";
            hidTime2.Value = "";
            hidTime3.Value = "";
            hidTime4.Value = "";
            hidTime5.Value = "";
            hidTime6.Value = "";
            hidTime7.Value = "";
            hidTime8.Value = "";
            hidTime9.Value = "";
            hidTime10.Value = "";
            // Bindings the employee category and type.
            BindGrid();
            string strSetAlert = "";
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "SetAlertMessages_DeleteRestore('" + rm.GetString("Alert_ConfirmDelete",ci) + "','" + rm.GetString("Alert_SelectItemsToDelete",ci) + "','" + rm.GetString("Alert_ConfirmDeletePermanent",ci) + "','" + rm.GetString("Alert_Areyousuretorestore",ci) + "','" + rm.GetString("Alert_SelectItemsToRestore",ci) + "','" + rm.GetString("Alert_SelectItemToForwarded") + "','" + rm.GetString("Alert_SelectItemToApproved") + "');";
            strSetAlert = strSetAlert + "SetAlertCheckDate('" + 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) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
            // Alert Message 28-1-2011
            string strrSetAlert = "";
            strrSetAlert = strrSetAlert + "<script language='javascript'>";
            strrSetAlert = strrSetAlert + "SetAlertMessages('" + rm.GetString("Alert_EntervalidTime",ci) + "');";
            strrSetAlert = strrSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strrSetAlert);
        }
        //Check whether the employee is Individual User
        intIndividualUser = objCheckUser.CheckIndividualUser(Convert.ToString(Session["UserGroupName"]));
        //Convert date time to MM/dd/YYYY format
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
    }

    private void loadcss()
    {
        CultureInfo ci;
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        //if Standard Demo
        htmlDiscrepancyPop.Attributes.Add("dir", rm.GetString("Direction", ci));
        if (source_Language == "Resources.Resource_AR")
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap-rtl.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome-rtl.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            stylecustom.Href = "../dist/css/custom-rtl.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
        else
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min.css";
            stylecustom.Href = "../dist/css/custom.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
    }
    protected void BindGrid()
    {
        string clkEmpID = "";
        string clkDate = "";
        if (Request.QueryString["clkDate"]!=null)
        {
            clkDate = Convert.ToString(Request.QueryString["clkDate"]);
        }
        if (Request.QueryString["clkEmpId"] != null)
        {
            clkEmpID = Convert.ToString(Request.QueryString["clkEmpId"]);
        }
        DiscrepancyEventData objClass = new DiscrepancyEventData();
        DataSet dsDiscrepancy = new DataSet();
        dsDiscrepancy = objClass.SelectDiscrepancyData(clkEmpID, clkDate);
        Session["parentLevelId"] = null;     
        //Check the Empty table
        if (dsDiscrepancy.Tables["Table"] == null || dsDiscrepancy.Tables["Table"].Rows.Count == 0)
        {
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_Thereisnorecord", ci) + "');window.close();";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("Display Message", strMsg);
            btnSave.Visible = false;
            grd.DataSource = "";
            grd.DataBind();
            //hid1.Value = "";
            //hid2.Value = "";
            //hid3.Value = "";
            //hid4.Value = "";
            //hid5.Value = "";
            //hid6.Value = "";
            //hid7.Value = "";
            //hid8.Value = "";
            //hid9.Value = "";
            //hid10.Value = "";
            //hidTime1.Value = "";
            //hidTime2.Value = "";
            //hidTime3.Value = "";
            //hidTime4.Value = "";
            //hidTime5.Value = "";
            //hidTime6.Value = "";
            //hidTime7.Value = "";
            //hidTime8.Value = "";
            //hidTime9.Value = "";
            //hidTime10.Value = "";
            return;
        }
        else
        {
            //hid1.Value = "";
            //hid2.Value = "";
            //hid3.Value = "";
            //hid4.Value = "";
            //hid5.Value = "";
            //hid6.Value = "";
            //hid7.Value = "";
            //hid8.Value = "";
            //hid9.Value = "";
            //hid10.Value = "";
            //hidTime1.Value = "";
            //hidTime2.Value = "";
            //hidTime3.Value = "";
            //hidTime4.Value = "";
            //hidTime5.Value = "";
            //hidTime6.Value = "";
            //hidTime7.Value = "";
            //hidTime8.Value = "";
            //hidTime9.Value = "";
            //hidTime10.Value = "";
            grd.DataSource = dsDiscrepancy;
            grd.DataBind();
            btnSave.Visible = true;
        }
    }
    /// <summary>
    /// Load the Punchality report
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_View_Click(object sender, EventArgs e)
    {
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lblDiscrepancyReport.Text = rm.GetString("Discrepancy_Entry", ci);
        btnSave.Text = rm.GetString("Caption_Close", ci);
        grd.Columns[0].HeaderText = rm.GetString("email_EmployeeId", ci);
        grd.Columns[1].HeaderText = rm.GetString("email_EmployeeName", ci);
        grd.Columns[2].HeaderText = rm.GetString("Permission_PermissionDate", ci);
        grd.Columns[3].HeaderText = rm.GetString("Shift_Time", ci);
        grd.Columns[4].HeaderText = rm.GetString("ManualTimeEntry_InDate", ci);
        grd.Columns[5].HeaderText = rm.GetString("ManualTimeEntry_In_Time", ci);
        grd.Columns[6].HeaderText = rm.GetString("ManualTimeEntry_OutDate", ci);
        grd.Columns[7].HeaderText = rm.GetString("ManualTimeEntry_Out_Time", ci);
        grd.Columns[8].HeaderText = rm.GetString("email_Reason", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(222, Convert.ToInt32(Session["userGroup_Id"]), "viewPrv");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] == DBNull.Value || Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) == 1)
            {
            }
            else
            {
            }
        }
        else
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
    }
    protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {            
            //check for null values
           // TextBox txtInDate = (TextBox)e.Row.Cells[2].FindControl("txtInDate");
           // TextBox txtInTime = (TextBox)e.Row.Cells[2].FindControl("txtInTime");
           // TextBox txtOutDate = (TextBox)e.Row.Cells[2].FindControl("txtOutDate");
           // TextBox txtOutTime = (TextBox)e.Row.Cells[2].FindControl("txtOutTime");
           // TextBox txtReason = (TextBox)e.Row.Cells[2].FindControl("txtReason");
           // Label lbl = (Label)e.Row.Cells[2].FindControl("lblClkDate");
           // if (lbl.Text != "")
           // {
           //     //lbl.Text = (Convert.ToDateTime(lbl.Text.ToString())).ToString(Session["MDateFormat"].ToString());
           //     if (Session["MDateFormat"].ToString() != "dd/MM/yyyy")
           //     {
           //         lbl.Text = (Convert.ToDateTime(lbl.Text.ToString())).ToString(Session["MDateFormat"].ToString());
           //         e.Row.Cells[2].Controls.Add(lbl);
           //     }
                
           // }
           //// txtInDate.Attributes.Add("onblur", "AssignDateValue(this," + Convert.ToInt32(e.Row.RowIndex) + ",'" + txtInDate.ClientID + "','" + txtInTime.ClientID + "','" + txtOutDate.ClientID + "','" + txtOutTime.ClientID + "')");
           // //if (txtInDate.Text == "")
           // //{
           // //    txtReason.ReadOnly = false;
           // //    txtReason.CssClass = "textbox_4";
           // //    txtInDate.ReadOnly = false;
           // //    Literal liteInDate = new Literal();
           // //    txtInDate.CssClass = "textbox_4";
           // //    liteInDate.Text = " <input type='image' src='../Images/dateicon.gif' name='butInDate" + Convert.ToInt32(e.Row.RowIndex) + "' /><script type='text/javascript'>Calendar.setup({ inputField : '" + txtInDate.ClientID + "', ifFormat : document.getElementById('Hidden1').value, showsTime : false, button : 'butInDate" + Convert.ToInt32(e.Row.RowIndex) + "', singleClick: true, step : 1});</script>";
           // //    e.Row.Cells[3].Controls.Add(liteInDate);
           // //    if (e.Row != null)
           // //    {
           // //        HiddenField hid1 = (HiddenField)Page.FindControl("hid" + (Convert.ToInt32(e.Row.RowIndex) + 1));
           // //        txtInDate.Text = hid1.Value;
           // //    }
           // //}
           // //else
           // //{
           // if (txtInDate.Text != string.Empty)
           // {
           //     if (Session["MDateFormat"].ToString() != "dd/MM/yyyy")
           //     {
           //         txtInDate.Text = (Convert.ToDateTime(txtInDate.Text.ToString())).ToString(Session["MDateFormat"].ToString());
           //         txtInDate.ReadOnly = true;
           //     }
           // }
           //// }
           // //txtInTime.Attributes.Add("onblur", "AssignTimeValue(this," + Convert.ToInt32(e.Row.RowIndex) + ",'" + txtInDate.ClientID + "','" + txtInTime.ClientID + "','" + txtOutDate.ClientID + "','" + txtOutTime.ClientID + "')");
           // //if (txtInTime.Text == "")
           // //{
           // //    txtReason.ReadOnly = false;
           // //    txtReason.CssClass = "textbox_4";
           // //    txtInTime.ReadOnly = false;
           // //    Literal liteInTime = new Literal();
           // //    txtInTime.CssClass = "textbox_4";
           // //    liteInTime.Text = " <input type='image' src='../Images/timepicker.gif' name='butInTime" + Convert.ToInt32(e.Row.RowIndex) + "' /><script type='text/javascript'>Calendar.setup({ inputField : '" + txtInTime.ClientID + "', ifFormat : document.getElementById('hiddenTime').value, showsTime : true, button : 'butInTime" + Convert.ToInt32(e.Row.RowIndex) + "', singleClick: true, step : 1});</script>";
           // //    e.Row.Cells[4].Controls.Add(liteInTime);
           // //    if (e.Row != null)
           // //    {
           // //        HiddenField hid2 = (HiddenField)Page.FindControl("hidTime" + (Convert.ToInt32(e.Row.RowIndex) + 1));
           // //        txtInTime.Text = hid2.Value;
           // //    }
           // //}
           // //else
           // //{
           //     if (txtInTime.Text != string.Empty)
           //     {
           //         txtInTime.ReadOnly = true;
           //         string NewTimeFormat = string.Empty;
           //         string TimeFormat = Convert.ToString(Session["ITimeFormat"]);
           //         if (TimeFormat == "%H:%M")
           //         {
           //             NewTimeFormat = "HH:mm:ss";
           //         }
           //         else
           //         {
           //             NewTimeFormat = "hh:mm:ss tt";
           //         }
           //         txtInTime.Text = Convert.ToDateTime(txtInTime.Text).ToString(NewTimeFormat);
           //     }
           // //}
           // //txtOutDate.Attributes.Add("onblur", "AssignDateValue(this," + Convert.ToInt32(e.Row.RowIndex) + ",'" + txtInDate.ClientID + "','" + txtInTime.ClientID + "','" + txtOutDate.ClientID + "','" + txtOutTime.ClientID + "')");
           // //if (txtOutDate.Text == "")
           // //{
           // //    txtReason.ReadOnly = false;
           // //    txtReason.CssClass = "textbox_4";
           // //    txtOutDate.ReadOnly = false;
           // //    Literal liteOutDate = new Literal();
           // //    txtOutDate.CssClass = "textbox_4";
           // //    liteOutDate.Text = " <input type='image' src='../Images/dateicon.gif' name='butOutDate" + Convert.ToInt32(e.Row.RowIndex) + "' /><script type='text/javascript'>Calendar.setup({ inputField : '" + txtOutDate.ClientID + "', ifFormat : document.getElementById('Hidden1').value, showsTime : false, button : 'butOutDate" + Convert.ToInt32(e.Row.RowIndex) + "', singleClick: true, step : 1}); document.getElementById('" + txtOutDate.ClientID + "').focus();</script>";
           // //    e.Row.Cells[5].Controls.Add(liteOutDate);
           // //    if (e.Row!=null)
           // //    {
           // //      HiddenField hid3 = (HiddenField)Page.FindControl("hid" + (Convert.ToInt32(e.Row.RowIndex) + 1));
           // //      txtOutDate.Text = hid3.Value;             
           // //    }
           // //}
           // //else
           // //{
           //     if (txtOutDate.Text !=string.Empty)
           //     {
           //         txtOutDate.Text = (Convert.ToDateTime(txtOutDate.Text.ToString())).ToString(Session["MDateFormat"].ToString());
           //         txtOutDate.ReadOnly = true;
           //     }
            //}
            //txtOutTime.Attributes.Add("onblur", "AssignTimeValue(this," + Convert.ToInt32(e.Row.RowIndex) + ",'" + txtInDate.ClientID + "','" + txtInTime.ClientID + "','" + txtOutDate.ClientID + "','" + txtOutTime.ClientID + "')");
            //if (txtOutTime.Text == "")
            //{
            //    txtReason.ReadOnly = false;
            //    txtReason.CssClass = "textbox_4";
            //    txtOutTime.ReadOnly = false;
            //    Literal liteOutTime = new Literal();
            //    txtOutTime.CssClass = "textbox_4";
            //    liteOutTime.Text = " <input type='image' src='../Images/timepicker.gif' name='butOutTime" + Convert.ToInt32(e.Row.RowIndex) + "' /><script type='text/javascript'>Calendar.setup({ inputField : '" + txtOutTime.ClientID + "', ifFormat : document.getElementById('hiddenTime').value, showsTime : true, button : 'butOutTime" + Convert.ToInt32(e.Row.RowIndex) + "', singleClick: true, step : 1});</script>";
            //    e.Row.Cells[6].Controls.Add(liteOutTime);
            //    if (e.Row != null)
            //    {
            //        HiddenField hid4 = (HiddenField)Page.FindControl("hidTime" + (Convert.ToInt32(e.Row.RowIndex) + 1));
            //        txtOutTime.Text = hid4.Value;
            //    }
            //}
            //else
            //{
                //if (txtOutTime.Text != string.Empty)
                //{
                //    txtOutTime.ReadOnly = true;
                //    string NewTimeFormat = string.Empty;
                //    string TimeFormat = Convert.ToString(Session["ITimeFormat"]);
                //    if (TimeFormat == "%H:%M")
                //    {
                //        NewTimeFormat = "HH:mm:ss";
                //    }
                //    else
                //    {
                //        NewTimeFormat = "hh:mm:ss tt";
                //    }
                //    txtOutTime.Text = Convert.ToDateTime(txtOutTime.Text).ToString(NewTimeFormat);
                //}
            //}
            // End of check for null values
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strSavAlert = "window.close();";
        this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
    }
//    protected void btnSave_Click(object sender, EventArgs e)
//    {
//        string gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
//        cls_Common objCommon = new cls_Common();   
//        int iSuccessFlag = 0;
//        ManualEntryEventData objManualEntry = new ManualEntryEventData();
//        DiscrepancyEventData objClass = new DiscrepancyEventData();
//        foreach (GridViewRow i in grd.Rows)
//        {
//             TextBox txtInDate = (TextBox)i.FindControl("txtInDate");
//             if (txtInDate != null && txtInDate.CssClass == "textbox_4")
//             {
//                 //
//                 HiddenField hidDate = (HiddenField) Page.FindControl("hid" + (i.DataItemIndex+1) );
//                 HiddenField hidTime = (HiddenField)Page.FindControl("hidTime" + (i.DataItemIndex + 1));
//                 if (hidDate.Value != "" && hidTime.Value !="")
//                 {
//                     string EmployeeCodeID=""; 
//                     string InDate=""; 
//                     string InTime=""; 
//                     string InPunchType="In"; 
//                     int InReaderID=0; 
//                     string OutDate=""; 
//                     string OutTime=""; 
//                     string OutPunchType="";
//                     int OutReaderID=0;
//                     int EmpID =0;
//                     Label lblEmpID = (Label)i.FindControl("lblEmpID");
//                     EmployeeCodeID = Convert.ToString(lblEmpID.Text);
//                     InDate = hidDate.Value.ToString();
//                     InTime = hidTime.Value.ToString();
//                     //check the conditions
//                     string strOutDt = "";
//                     string strOutTi = "";
//                     TextBox txtOutDt = (TextBox)i.FindControl("txtOutDate");
//                     TextBox txtOutTi = (TextBox)i.FindControl("txtOutTime");
//                     strOutDt = txtOutDt.Text;
//                     //txtOutTi.Text = "";
//                     InDate = objCommon.DateConvertion(gstrDateFormat, InDate);
//                     DateTime dtInTime = DateTime.Parse(InDate + " " + InTime);
//                     DateTime dtOutTime = DateTime.Parse(objCommon.DateConvertion(gstrDateFormat, txtOutDt.Text) + " " + txtOutTi.Text);
//                     if (DateTime.Compare(dtInTime, dtOutTime) > 0)
//                     {
//                         iSuccessFlag = 0;
//                         string strAlert = "alert('InDate and Time is greater than OutDate and Time for " + Convert.ToString(dtInTime) + " And " + Convert.ToString(dtOutTime) + "');";
//                         this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAlert, true);
//                         BindGrid();
//                         btnSave.Focus();
//                         return;
//                     }
//                     else
//                     {
//                         //check for 24 hours
//                         TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
//                         double TimeDiff = tsDiff.TotalHours;
//                         if (TimeDiff > 24)
//                         {
//                             iSuccessFlag = 0;
//                             string strAl = "alert('Difference between In DateTime and Out DateTime should not Exceed 24 Hours for " + Convert.ToString(dtInTime) + " And " + Convert.ToString(dtOutTime) + "');";
//                             this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAl, true);
//                             BindGrid();
//                             btnSave.Focus();
//                             return;
//                         }
//                         //check manual timeEntry Already Exits
//                         int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
//                         if (count > 0)
//                         {
//                             iSuccessFlag = 0;
//                             string strAl = "alert('Manual Entry Already have been Added for this duration for " + Convert.ToString(dtInTime) + " And " + Convert.ToString(dtOutTime) + "');";
//                             this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAl, true);
//                             BindGrid();
//                             btnSave.Focus();
//                             return;
//                         }
//                         //Check for permission
//                         int intIsPermission = 0;
//                         // Check whether this duration is present in the Permission
//                         intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
//                         if (intIsPermission > 0)
//                         {
//                             iSuccessFlag = 0;
//                             string strAl2 = "alert('Permission Entry is present for this duration for " + Convert.ToString(dtInTime) + " And " + Convert.ToString(dtOutTime) + "');";
//                             this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAl2, true);
//                             BindGrid();
//                             btnSave.Focus();
//                             return;
//                         }
//                     }
//                     // End of check the conditions
//                     //DataSet ds = new DataSet();
//                     //HiddenField hidInReader = (HiddenField)i.FindControl("hidOutReader");
//                     //ds = objClass.SelectMatchingDevice(Convert.ToString(hidInReader.Value), "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"]);
//                     //objClass.InsertDiscrepancy(EmployeeCodeID, InDate, InTime, InPunchType, InReaderID, OutDate, OutTime, OutPunchType, OutReaderID, EmpID);
//                     iSuccessFlag = 1;
//                 }                
//             }
//             else
//             {
//                 TextBox txtOutDate = (TextBox)i.FindControl("txtOutDate");
//                 if (txtOutDate != null && txtOutDate.CssClass == "textbox_4")
//                 {
//                     //
//                     HiddenField hidDate = (HiddenField)Page.FindControl("hid" + (i.DataItemIndex + 1));
//                     HiddenField hidTime = (HiddenField)Page.FindControl("hidTime" + (i.DataItemIndex + 1));
//                     if (hidDate.Value != "" && hidTime.Value != "")
//                     {
//                         string EmployeeCodeID = "";
//                         string InDate = "";
//                         string InTime = "";
//                         string InPunchType = "";
//                         int InReaderID = 0;
//                         string OutDate = "";
//                         string OutTime = "";
//                         string OutPunchType = "Out";
//                         int OutReaderID = 0;
//                         int EmpID = 0;
//                         Label lblEmpID = (Label)i.FindControl("lblEmpID");
//                         EmployeeCodeID = Convert.ToString(lblEmpID.Text);
//                         OutDate = hidDate.Value.ToString();
//                         OutTime = hidTime.Value.ToString();
//                         //check the conditions
//                         string strInDt = "";
//                         string strInTi = "";
//                         TextBox txtInDt = (TextBox)i.FindControl("txtInDate");
//                         TextBox txtInTi = (TextBox)i.FindControl("txtInTime");
//                         strInDt = txtInDt.Text;
//                         //txtInTi.Text = "";
//                         OutDate = objCommon.DateConvertion(gstrDateFormat, OutDate);
//                         DateTime dtOutTime = DateTime.Parse(OutDate + " " + OutTime);
//                         DateTime dtInTime = DateTime.Parse(objCommon.DateConvertion(gstrDateFormat, txtInDt.Text) + " " + txtInTi.Text);
//                         if (DateTime.Compare(dtInTime, dtOutTime) > 0)
//                         {
//                             iSuccessFlag = 0;
//                             string strAlert = "alert('InDate and Time is greater than OutDate and Time for " + Convert.ToString(dtInTime) + " And " + Convert.ToString(dtOutTime) + "');";
//                             this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAlert, true);
//                             BindGrid();
//                             btnSave.Focus();
//                             return;
//                         }
//                         else
//                         {
//                             //check for 24 hours
//                             TimeSpan tsDiff =dtOutTime.Subtract(dtInTime);
//                             double TimeDiff = tsDiff.TotalHours;
//                             if (TimeDiff>24)
//                             {
//                                 iSuccessFlag = 0;
//                                 string strAl = "alert('Difference between In DateTime and Out DateTime should not Exceed 24 Hours for " + Convert.ToString(dtInTime) + " And " + Convert.ToString(dtOutTime) + "');";
//                                 this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAl, true);
//                                 BindGrid();
//                                 btnSave.Focus();                                
//                                 return;
//                             }
//                             //check manual timeEntry Already Exits
//                             int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
//                             if (count>0)
//                             {
//                                 iSuccessFlag = 0;
//                                 string strAl = "alert('Manual Entry Already have been Added for this duration for " + Convert.ToString(dtInTime) + " And " + Convert.ToString(dtOutTime) + "');";
//                                 this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAl, true);
//                                 BindGrid();
//                                 btnSave.Focus();
//                                 return;
//                             }
//                             //Check for permission
//                             int intIsPermission = 0;
//                            // Check whether this duration is present in the Permission
//                             intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
//                            if (intIsPermission> 0)
//                            {
//                                iSuccessFlag = 0;
//                                string strAl2 = "alert('Permission Entry is present for this duration for " + Convert.ToString(dtInTime) + " And " + Convert.ToString(dtOutTime) + "');";
//                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAl2, true);
//                                BindGrid();
//                                btnSave.Focus();
//                                return;
//                            }
//                         }
//                         // End of check the conditions
//                         //HiddenField hidOutReader = (HiddenField)i.FindControl("hidInReader");
//                         //DataSet ds = new DataSet();
//                         //ds = objClass.SelectMatchingDevice(Convert.ToString(hidOutReader.Value), "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"]);
//                         //objClass.InsertDiscrepancy(EmployeeCodeID, InDate, InTime, InPunchType, InReaderID, OutDate, OutTime, OutPunchType, OutReaderID, EmpID);
//                         iSuccessFlag = 1;
//                     }   
//                 }
//                 else
//                 {
//                     //
//                     int k = 1;
//                 }
//             }
//        }
///////save discrepancy
//        string strManaulEntryStatus = string.Empty;
//        if (iSuccessFlag != 0)
//        {
//            int intCheckApproval = 0;
//            foreach (GridViewRow i in grd.Rows)
//            {
//                if (intCheckApproval == 0)
//                {
//                    HiddenField hidEmployeeAutoId = (HiddenField)i.FindControl("hidEmployeeAutoId");
//                     strManaulEntryStatus = GetManualEntryStatus(Convert.ToInt32(hidEmployeeAutoId.Value));
//                    intCheckApproval = 1;
//                    if(strManaulEntryStatus=="NotDefined")
//                    {
//                        base.Message("Approval Settings yet to defined for this company");
//                        return;
//                    }
//                }
//                TextBox txtInDate = (TextBox)i.FindControl("txtInDate");
//                if (txtInDate != null && txtInDate.CssClass == "textbox_4")
//                {
//                    //
//                    HiddenField hidDate = (HiddenField)Page.FindControl("hid" + (i.DataItemIndex + 1));
//                    HiddenField hidTime = (HiddenField)Page.FindControl("hidTime" + (i.DataItemIndex + 1));
//                    if (hidDate.Value != "" && hidTime.Value != "")
//                    {
//                        string EmployeeCodeID = "";
//                        string InDate = "";
//                        string InTime = "";
//                        string InPunchType = "In";
//                        int InReaderID = 0;
//                        string OutDate = "";
//                        string OutTime = "";
//                        string OutPunchType = "";
//                        int OutReaderID = 0;
//                        int EmpID = 0;
//                        Label lblEmpID = (Label)i.FindControl("lblEmpID");
//                        EmployeeCodeID = Convert.ToString(lblEmpID.Text);
//                        InDate = hidDate.Value.ToString();
//                        InTime = hidTime.Value.ToString();
//                        //check the conditions
//                        string strOutDt = "";
//                        string strOutTi = "";
//                        TextBox txtOutDt = (TextBox)i.FindControl("txtOutDate");
//                        TextBox txtOutTi = (TextBox)i.FindControl("txtOutTime");
//                        strOutDt = txtOutDt.Text;
//                        //txtOutTi.Text = "";
//                        InDate = objCommon.DateConvertion(gstrDateFormat, InDate);
//                        DateTime dtInTime = DateTime.Parse(InDate + " " + InTime);
//                        DateTime dtOutTime = DateTime.Parse(objCommon.DateConvertion(gstrDateFormat, txtOutDt.Text) + " " + txtOutTi.Text);
//                        if (DateTime.Compare(dtInTime, dtOutTime) > 0)
//                        {
//                            iSuccessFlag = 0;
//                            string strAlert = "alert('InDate and Time is greater than OutDate and Time for " + Convert.ToString(dtInTime) + " And " + Convert.ToString(dtOutTime) + "');";
//                            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAlert, true);
//                            BindGrid();
//                            btnSave.Focus();
//                            return;
//                        }
//                        else
//                        {
//                            //check for 24 hours
//                            TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
//                            double TimeDiff = tsDiff.TotalHours;
//                            if (TimeDiff > 24)
//                            {
//                                iSuccessFlag = 0;
//                                string strAl = "alert('Difference between In DateTime and Out DateTime should not Exceed 24 Hours');";
//                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAl, true);
//                                BindGrid();
//                                btnSave.Focus();
//                                return;
//                            }
//                            //check manual timeEntry Already Exits
//                            int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
//                            if (count > 0)
//                            {
//                                iSuccessFlag = 0;
//                                string strAl = "alert('Manual Entry Already have been Added for this duration');";
//                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAl, true);
//                                BindGrid();
//                                btnSave.Focus();
//                                return;
//                            }
//                            //Check for permission
//                            int intIsPermission = 0;
//                            // Check whether this duration is present in the Permission
//                            intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
//                            if (intIsPermission > 0)
//                            {
//                                iSuccessFlag = 0;
//                                string strAl2 = "alert('Permission Entry is present for this duration');";
//                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAl2, true);
//                                BindGrid();
//                                btnSave.Focus();
//                                return;
//                            }
//                        }
//                        // End of check the conditions
//                        DataSet ds = new DataSet();
//                        HiddenField hidInReader = (HiddenField)i.FindControl("hidOutReader");
//                        ds = objClass.SelectMatchingDevice(Convert.ToString(hidInReader.Value), "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)
//                        {
//                            InTime = Convert.ToDateTime(InTime).ToString("hh:mm:ss tt");
//                        }
//                        if (OutTime != string.Empty)
//                        {
//                            OutTime = Convert.ToDateTime(OutTime).ToString("hh:mm:ss tt");
//                        }
//                        TextBox txtReason = (TextBox)i.FindControl("txtReason");
//                        objClass.InsertDiscrepancy(EmployeeCodeID, InDate, InTime, InPunchType, InReaderID, OutDate, OutTime, OutPunchType, OutReaderID, EmpID,strManaulEntryStatus,txtReason.Text.Trim().Replace("'","''"));
//                        iSuccessFlag = 1;
//                    }
//                }
//                else
//                {
//                    TextBox txtOutDate = (TextBox)i.FindControl("txtOutDate");
//                    if (txtOutDate != null && txtOutDate.CssClass == "textbox_4")
//                    {
//                        //
//                        HiddenField hidDate = (HiddenField)Page.FindControl("hid" + (i.DataItemIndex + 1));
//                        HiddenField hidTime = (HiddenField)Page.FindControl("hidTime" + (i.DataItemIndex + 1));
//                        if (hidDate.Value != "" && hidTime.Value != "")
//                        {
//                            string EmployeeCodeID = "";
//                            string InDate = "";
//                            string InTime = "";
//                            string InPunchType = "";
//                            int InReaderID = 0;
//                            string OutDate = "";
//                            string OutTime = "";
//                            string OutPunchType = "Out";
//                            int OutReaderID = 0;
//                            int EmpID = 0;
//                            Label lblEmpID = (Label)i.FindControl("lblEmpID");
//                            EmployeeCodeID = Convert.ToString(lblEmpID.Text);
//                            OutDate = hidDate.Value.ToString();
//                            OutTime = hidTime.Value.ToString();
//                            //check the conditions
//                            string strInDt = "";
//                            string strInTi = "";
//                            TextBox txtInDt = (TextBox)i.FindControl("txtInDate");
//                            TextBox txtInTi = (TextBox)i.FindControl("txtInTime");
//                            strInDt = txtInDt.Text;
//                            //txtInTi.Text = "";
//                            OutDate = objCommon.DateConvertion(gstrDateFormat, OutDate);
//                            DateTime dtOutTime = DateTime.Parse(OutDate + " " + OutTime);
//                            DateTime dtInTime = DateTime.Parse(objCommon.DateConvertion(gstrDateFormat, txtInDt.Text) + " " + txtInTi.Text);
//                            if (DateTime.Compare(dtInTime, dtOutTime) > 0)
//                            {
//                                iSuccessFlag = 0;
//                                string strAlert = "alert('InDate and Time is greater than OutDate and Time for " + Convert.ToString(dtInTime) + " And " + Convert.ToString(dtOutTime) + "');";
//                                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAlert, true);
//                                BindGrid();
//                                btnSave.Focus();
//                                return;
//                            }
//                            else
//                            {
//                                //check for 24 hours
//                                TimeSpan tsDiff = dtOutTime.Subtract(dtInTime);
//                                double TimeDiff = tsDiff.TotalHours;
//                                if (TimeDiff > 24)
//                                {
//                                    iSuccessFlag = 0;
//                                    string strAl = "alert('Difference between In DateTime and Out DateTime should not Exceed 24 Hours');";
//                                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAl, true);
//                                    BindGrid();
//                                    btnSave.Focus();
//                                    return;
//                                }
//                                //check manual timeEntry Already Exits
//                                int count = objManualEntry.CheckManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
//                                if (count > 0)
//                                {
//                                    iSuccessFlag = 0;
//                                    string strAl = "alert('Manual Entry Already have been Added for this duration');";
//                                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAl, true);
//                                    BindGrid();
//                                    btnSave.Focus();
//                                    return;
//                                }
//                                //Check for permission
//                                int intIsPermission = 0;
//                                // Check whether this duration is present in the Permission
//                                intIsPermission = objManualEntry.CheckPermissionForManualEntry(EmployeeCodeID, InDate, InTime, OutDate, OutTime);
//                                if (intIsPermission > 0)
//                                {
//                                    iSuccessFlag = 0;
//                                    string strAl2 = "alert('Permission Entry is present for this duration');";
//                                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strAl2, true);
//                                    BindGrid();
//                                    btnSave.Focus();
//                                    return;
//                                }
//                            }
//                            // End of check the conditions
//                            HiddenField hidOutReader = (HiddenField)i.FindControl("hidInReader");
//                            DataSet ds = new DataSet();
//                            ds = objClass.SelectMatchingDevice(Convert.ToString(hidOutReader.Value), "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)
//                            {
//                                InTime = Convert.ToDateTime(InTime).ToString("hh:mm:ss tt");
//                            }
//                            if (OutTime != string.Empty)
//                            {
//                                OutTime = Convert.ToDateTime(OutTime).ToString("hh:mm:ss tt");
//                            }
//                            TextBox txtReason = (TextBox)i.FindControl("txtReason");
//                            objClass.InsertDiscrepancy(EmployeeCodeID, InDate, InTime, InPunchType, InReaderID, OutDate, OutTime, OutPunchType, OutReaderID, EmpID,strManaulEntryStatus,txtReason.Text.Trim().Replace("'","''"));
//                            iSuccessFlag = 1;
//                        }
//                    }
//                    else
//                    {
//                        //
//                        int k = 1;
//                    }
//                }
//            }
//        }
////End of discrepancy
//        BindGrid();
//        if (iSuccessFlag == 1)
//        {
//            string strSavAlert = "alert('Discrepancy Entry Saved Successfully. The Status is " + strManaulEntryStatus + "');window.close();";
//            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
//        }
//        else
//        {
//            string strSavAlert = "alert('Enter the Missing Fields');";
//            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", strSavAlert, true);
//        }
//    }
    private string GetManualEntryStatus(int EmployeeAutoId)
    {
        //Check Approval Settings for EnterPrise Edition and Demo Version
        //Declare manual Entry Status
        string strManualTimeEntryStatus = "Pending";
        // Demo Edition - 0
        //Standard Edition -  1
        //Profession Edition - 2
        //Enterprise Edition - 3
        int manualEntryExists = 0;
        manualEntryExists = objCommon.CheckPageExistsInLicense(98);
       // if (Session["Edition"].ToString() == "0" || Session["Edition"].ToString() == "3")
        if(manualEntryExists == 1)
        {
            //Get Compnay Id of the Employeee
            int intCompanyId = base.GetCompanyId(EmployeeAutoId);
            //Get Approval Settings Based on Company Id
            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
                    {
                        //base.Message("Approval Settings yet to defined for this company");
                        return "NotDefined";
                    }
                }
            }
        }
        else
        {
            strManualTimeEntryStatus = "Approved";
        }
        return strManualTimeEntryStatus;
    }
}
