using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Resources;
using System.Globalization;
using System.Threading;
using System.IO;
public partial class TimeCheck : System.Web.UI.MasterPage
{
    #region "Variable Declaration"
    ResourceManager rm;
    String punchTypes = "";
    ManualEntryEventData objManualEntry = new ManualEntryEventData();
    EmployeeEventData objEmployee = new EmployeeEventData();
    ShiftExchangeEventData objShiftExchange = new ShiftExchangeEventData();
    cls_Common objCommon = new cls_Common();
    LoginListEventData loginList = new LoginListEventData();
    EmployeeEventData ObjEmployeeEventData = new EmployeeEventData();
    DataSet ds_Image = new DataSet();
    BrandingEventdata obj_Branding = new BrandingEventdata();
    string strDelarLogo;
    DataSet ds_Logo = new DataSet();
    DateTime d;
    #endregion
    string strFormat = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        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;
        // LoadStrings Function fills the Label from the Resource File        
        //if (Session["EmployeeId"] != null)
        //{
        //    string sUser = Convert.ToString(Cache[Session["EmployeeId"].ToString().ToUpper()]);
        //    if (sUser == null || sUser == String.Empty)
        //    {
        //        Response.Redirect("index.aspx");
        //    }
        //    else
        //    {
        //        string cValues = Convert.ToString(Cache[Session["EmployeeId"].ToString().ToUpper()]);
        //        string[] cData = cValues.Split(',');
        //        string cSessionId = cData[2].ToString();
        //        if (Session.SessionID != cSessionId)
        //        {
        //            string currentPageFileName = new FileInfo(this.Request.Url.LocalPath).Name;
        //            RedirectToLoginPage(currentPageFileName);
        //        }
        //    }
        //}
       


        if (Session["user"] == null || Session["user"].ToString() == "")
        {
            Response.Redirect("Index.aspx");
        }
        else
        {
            if (Convert.ToString(Session["EmployeeId"]).ToUpper() != "ADMIN")
            {
                lblEmployeeID.Text = Convert.ToString(Session["EmployeeId"]);
            }
            else
            {
                lblID.Style.Add("display", "none");
                lblEmployeeID.Style.Add("display", "none");
            }
            lbl_User2.ToolTip = lbl_user.ToolTip = lbl_User2.Text = lbl_user.Text = (Convert.ToString(Session["user"]).Length > 15) ?
            ci.TextInfo.ToTitleCase(Convert.ToString(Session["user"]).Substring(0, 15)) + "..." : ci.TextInfo.ToTitleCase(Convert.ToString(Session["user"]));
            //Convert.ToString(Session["user"]).Substring(0, 15).ToUpper() + "..." : Convert.ToString(Session["user"]).ToUpper();
        }
        if (!Page.IsPostBack)
        {
            LoadStrings(ci);
            if (Convert.ToString(Session["TopMenu"]) != string.Empty)
            {
                this.MainMenu.InnerHtml = Convert.ToString(Session["TopMenu"]);
                LoadYear();
            }
            else
            {
                loadMenu();
            }
            lbtn_logout.OnClientClick = "javascript:return confirm('" + rm.GetString("Alert_Logout", ci) + "');";
            //LoadNews();          
            cls_master_levels class1 = new cls_master_levels();
            DataSet ds = new DataSet();
            if (Session["UserGroupName"] != null)
            {
                ds = class1.getOrgLevelUpdated(0, Convert.ToString(Session["LevelRights"]), 0, Convert.ToString(Session["UserGroupName"]));
            }
            DisplayProfileOption();
            Session["OrgLevels"] = ds;
        }
        loadcss();
        GetImagePath();
        //get the culture info to set the language
        //LDAP
        if (Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["LDAPrequired"]) == "1")
        {
            if (Convert.ToString(Session["isDomainLoginRequiredflag"]) != "")
                if (Convert.ToBoolean(Session["isDomainLoginRequiredflag"]) == true)
                    A1.Style.Add("display", "none");
        }
        UTCDatetimeProjectLoad();
        UTCLoad();

        if (Convert.ToString(Session["EmployeeId"]).ToUpper() != "ADMIN")
        {
            ManualTimeEntryIndividuals.Style.Add("display", "block");
        }
        else
        {
            ManualTimeEntryIndividuals.Style.Add("display", "none");
        }

        LoadTodayPunchTime();
    }

    private void LoadTodayPunchTime()
    {
        DataSet ds_ManualTimeLoad = new DataSet();
        DataSet ds_ManualTimeOut = new DataSet();
        DataSet ds_ManualTimeBreakIn = new DataSet();
        DataSet ds_ManualTimeBreakOut = new DataSet();
        org_levelsEventData ds_ManualTimeLoads = new org_levelsEventData();
        String EmployeeID = Convert.ToString(Session["EmployeeId"]);
        ds_ManualTimeLoad = ds_ManualTimeLoads.GetManualTimeLoadToday(EmployeeID, "Shift In");
        ds_ManualTimeBreakIn = ds_ManualTimeLoads.GetManualTimeLoadToday(EmployeeID, "Break In");
        ds_ManualTimeBreakOut = ds_ManualTimeLoads.GetManualTimeLoadToday(EmployeeID, "Break Out");
        if (ds_ManualTimeLoad.Tables["Table"].Rows.Count > 0)
        {
            String IN = Convert.ToString(ds_ManualTimeLoad.Tables["Table"].Rows[0]["InTime"].ToString());
            if (IN == "NO")
            {
                div1.Style.Add("display", "block");
            }
            else
            {
                ShiftIN.Text = Convert.ToString(ds_ManualTimeLoad.Tables["Table"].Rows[0]["InTime"].ToString());
                div1.Style.Add("display", "none");

            }
            String Out = Convert.ToString(ds_ManualTimeLoad.Tables["Table"].Rows[0]["OutTime"].ToString());
            if (Out == "NO")
            {
                div3.Style.Add("display", "block");
            }
            else
            {
                div3.Style.Add("display", "none");
                ShiftOut.Text = Convert.ToString(ds_ManualTimeLoad.Tables["Table"].Rows[0]["OutTime"].ToString());
            }

        }
        if (ds_ManualTimeBreakIn.Tables["Table"].Rows.Count > 0)
        {
            String BreakIns = Convert.ToString(ds_ManualTimeBreakIn.Tables["Table"].Rows[0]["BreakIN"].ToString());
            if (BreakIns == "NO")
            {
                div6.Style.Add("display", "block");
            }
            else
            {
                div6.Style.Add("display", "none");
                BreakIn.Text = Convert.ToString(ds_ManualTimeBreakIn.Tables["Table"].Rows[0]["BreakIN"].ToString());
            }
        }
        if (ds_ManualTimeBreakOut.Tables["Table"].Rows.Count > 0)
        {
            String Breakouts = Convert.ToString(ds_ManualTimeBreakOut.Tables["Table"].Rows[0]["BreakOut"].ToString());
            if (Breakouts == "NO")
            {
                div5.Style.Add("display", "block");
            }
            else
            {
                BreakOut.Text = Convert.ToString(ds_ManualTimeBreakOut.Tables["Table"].Rows[0]["BreakOut"].ToString());
                div5.Style.Add("display", "none");
            }

        }
    }

    //Login based Timezone Loading Function Created by Hariharan.J on 06-Dec-2023
    private void UTCDatetimeProjectLoad()
   {
        String EmployeeID = Convert.ToString(Session["EmployeeId"]);
        org_levelsEventData getUtcEmployeeCheck = new org_levelsEventData();
        DataSet ds_UTC = new DataSet();
        DataSet ds_UTCServer = new DataSet();
        org_levelsEventData getUtcServer = new org_levelsEventData();
        if (EmployeeID == "1" || EmployeeID.ToUpper() == "ADMIN")
        {
            //Session["TimeZoneName"] = "India";
            ds_UTCServer = getUtcServer.GetUTCTimeZoneServerUTC(EmployeeID);
            Session["TimezoneId"] = ds_UTCServer.Tables[0].Rows[0]["TimezoneId"].ToString();
            Session["TimeZoneName"] = ds_UTCServer.Tables[0].Rows[0]["TimeZoneName"].ToString();
            Session["TimeZoneDesc"] = ds_UTCServer.Tables[0].Rows[0]["TimeZoneValue"].ToString();
            Session["HoursToBeAdded"] = ds_UTCServer.Tables[0].Rows[0]["HoursToBeAdded"].ToString();
            Session["MinToBeAdded"] = ds_UTCServer.Tables[0].Rows[0]["MinToBeAdded"].ToString();
            DateTime ConvertUTCTime = new DateTime();
            int hours = Convert.ToInt32(ds_UTCServer.Tables[0].Rows[0]["HoursToBeAdded"]);
            int Minutes = Convert.ToInt32(ds_UTCServer.Tables[0].Rows[0]["MinToBeAdded"]);
            ConvertUTCTime = objCommon.TimezoneGetDateTime(hours, Minutes);

            
            strFormat = Convert.ToString(Session["ITimeFormat"]);
            if (strFormat == "HH:mm" && Convert.ToString(Session["MDateFormat"]) != "dd/MM/yyyy" && Convert.ToString(Session["MDateFormat"]) != "dd-MMM-yyyy" && Convert.ToString(Session["MDateFormat"]) != "dd-MM-yyyy")
            {
                Session["CurrentCountryDatetime"] = ConvertUTCTime.ToString(Convert.ToString(Convert.ToString(Session["MDateFormat"]) + " HH:mm"));
            }
            else
            {
                Session["CurrentCountryDatetime"] = ConvertUTCTime.ToString(Convert.ToString("MM/dd/yyyy") + " hh:mm tt");
                
            }

        }
        else
        {
            ds_UTC = getUtcEmployeeCheck.GetUTCTimeZoneEmployeeCheck(EmployeeID);
            int Counts = ds_UTC.Tables[0].Rows.Count;
            //String UTC = Convert.ToString(Session["TimezoneId"]);
            //TimeZoneName,TimeZoneDesc,HoursToBeAdded,MinToBeAdded TimezoneId
            if (Counts != 0)
            {
                Session["TimezoneId"] = ds_UTC.Tables[0].Rows[0]["TimezoneId"].ToString();
                Session["TimeZoneName"] = ds_UTC.Tables[0].Rows[0]["TimeZoneName"].ToString();
                Session["TimeZoneDesc"] = ds_UTC.Tables[0].Rows[0]["TimeZoneValue"].ToString();
                Session["HoursToBeAdded"] = ds_UTC.Tables[0].Rows[0]["HoursToBeAdded"].ToString();
                Session["MinToBeAdded"] = ds_UTC.Tables[0].Rows[0]["MinToBeAdded"].ToString();

                //var dt = DateTime.UtcNow;
                //var tz = TimeZoneInfo.FindSystemTimeZoneById(Convert.ToString(Session["TimeZoneName"]) + " Standard Time");
                //var DateTimeForCountry = TimeZoneInfo.ConvertTimeFromUtc(dt, tz);

                DateTime ConvertUTCTime = new DateTime();
                int hours = Convert.ToInt32(ds_UTC.Tables[0].Rows[0]["HoursToBeAdded"]);
                int Minutes = Convert.ToInt32(ds_UTC.Tables[0].Rows[0]["MinToBeAdded"]);
                ConvertUTCTime = objCommon.TimezoneGetDateTime(hours, Minutes);

                //lbl_EntryDate.Text = DateTimeForCountry.ToString(Convert.ToString(Session["MDateFormat"]) + " HH:mm:ss tt");
                strFormat = Convert.ToString(Session["ITimeFormat"]);
                if (strFormat == "HH:mm" && Convert.ToString(Session["MDateFormat"])!="dd/MM/yyyy" && Convert.ToString(Session["MDateFormat"]) != "dd-MMM-yyyy" && Convert.ToString(Session["MDateFormat"]) != "dd-MM-yyyy")
                {
                    Session["CurrentCountryDatetime"] = ConvertUTCTime.ToString(Convert.ToString(Convert.ToString(Session["MDateFormat"]) + " HH:mm"));
                }
                else
                {
                    Session["CurrentCountryDatetime"] = ConvertUTCTime.ToString(Convert.ToString("MM/dd/yyyy") + " hh:mm tt");
                }

            }
            else
            {

                strFormat = Convert.ToString(Session["ITimeFormat"]);

                if (strFormat == "HH:mm")
                {
                    Session["CurrentCountryDatetime"] = DateTime.Now.ToString(Convert.ToString(Convert.ToString(Session["MDateFormat"]) + " HH:mm"));
                }
                else
                {
                    Session["CurrentCountryDatetime"] = DateTime.Now.ToString(Convert.ToString(Session["MDateFormat"]) + " hh:mm tt");
                }

            }
        }
        
    }
    
    
    // Sign Out Layout CUrrent UTC Timezone Loading Created by Hariharan on 21-Dec-2023

    private void UTCLoad()
    {
        if(Convert.ToString(Session["CurrentCountryDatetime"]) != "")
        {
            if(Convert.ToString(Session["DashBoard"]) == String.Empty)
            {
                if (Convert.ToString( Session["DashBoard"]) != "DashBoardChart")
                {
                    // Assuming "02/29/2024 6:58:21 PM" is stored in the session as a string
                    string sessionValue = (string)Session["CurrentCountryDatetime"];
                    try
                    {
                        d = Convert.ToDateTime(sessionValue);
                    }
                    catch (Exception ex)
                    {
                        d = DateTime.UtcNow;
                    }
                   
                    sessionValue = ((string)Session["CurrentCountryDatetime"]).Trim();
                    try
                    {

                        // Attempt to parse the session value using the specified format
                        //d = DateTime.ParseExact(sessionValue, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
                        d.ToString(Convert.ToString(Convert.ToString("dd/MM/yyyy") + " HH:mm"));
                        // Parsing successful, use the DateTime object 'd' as needed
                        DateTime parsedDateTime;

                        string strUTCountryName = Convert.ToString(Session["TimeZoneName"]);

                        string strformattedDate = d.ToString("dddd, MMMM d, yyyy");

                        string strGMT = Convert.ToString(Session["TimeZoneDesc"]);

                        string strFromattedUTCTime = d.ToString("hh:mm tt", CultureInfo.InvariantCulture);
                        if (Convert.ToString(Session["TimeZoneDesc"]) == String.Empty)
                        {
                            Session["TimeZoneDesc"] = "GMT+05:30";
                        }
                        //lblCountryName.Text=strUTCountryName+" "+strGMT;
                        lblCountryName.Text = strUTCountryName;
                        lblUTCTime.Text = strFromattedUTCTime;
                        lblUTCDatetime.Text = strformattedDate + " " + strGMT;
                    }
                    catch (FormatException ex)
                    {
                        // Handle the exception, perhaps log the error or show a user-friendly message
                        //Console.WriteLine("Error parsing DateTime: " + ex.Message);

                    }
                }
                else
                {

                }

            }
            
            

            
        }
    }

    private void DisplayProfileOption()
    {
        if (Session["IsMyProfileRqrd"] != null)
        {
            if (Convert.ToString(Session["IsMyProfileRqrd"]).ToUpper() == "1")
            {
                divMyprofile.Style.Add("display", "");
            }
            else
            {
                divMyprofile.Style.Add("display", "none");
            }
        }
        else
        {
            divMyprofile.Style.Add("display", "none");
        }
    }

    private static void RedirectToLoginPage(string currentPageFileName)
    {
        switch (currentPageFileName.ToUpper())
        {
            case "DASHBOARDCHART.ASPX":
            case "DASHBOARD.ASPX":
            case "LANDING.ASPX":
            case "MYCALENDAR.ASPX":
            case "DASHBOARD_SAMSUNG.ASPX":
                HttpContext.Current.Response.Redirect("Index.aspx");
                break;
            default:
                HttpContext.Current.Response.Redirect("../Index.aspx");
                break;
        }
    }
    protected void GetImagePath()
    {
        try
        {
            if (System.Configuration.ConfigurationManager.AppSettings["DelarLogo"].ToString() != "")
            {
                strDelarLogo = "~/" + System.Configuration.ConfigurationManager.AppSettings["DelarLogo"].ToString();
                IMG2.Visible = true;
                IMG2.Src = strDelarLogo;
            }
            else
            {
                IMG2.Visible = false;
            }
            ds_Logo = obj_Branding.SelectCusLogo();
            if (ds_Logo != null && ds_Logo.Tables.Count > 0)
            {
                if (ds_Logo.Tables[0].Rows.Count > 0)
                {
                    if (ds_Logo.Tables[0].Rows[0]["Cus_Image"] != System.DBNull.Value)
                    {
                        byte[] img = (byte[])ds_Logo.Tables[0].Rows[0]["Cus_Image"];
                        GetImage(img);
                        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        Random rnd = new Random();
                        this.IMG1.Src = "~/Customer_Logo.jpg?Rnd=" + rnd.NextDouble();
                    }
                    else
                    {
                        IMG1.Visible = false;
                    }
                }
                else
                {
                    IMG1.Visible = false;
                }
            }
            ds_Logo.Dispose();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    public void GetImage(byte[] Logo)
    {
        string strPath = AppDomain.CurrentDomain.BaseDirectory + "Customer_Logo.jpg";
        if (File.Exists(strPath))
        {
            File.Delete(strPath);
        }
        MemoryStream stream = new MemoryStream(Logo);
        System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
        img.Save(strPath);
        stream.Dispose();
        stream.Close();
    }
    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
        MasHtml.Attributes.Add("dir", rm.GetString("Direction", ci));
        Session["Edit"] = rm.GetString("Grid_Edit", ci);
        Session["Delete"] = rm.GetString("btn_Delete", ci);
        Session["View"] = rm.GetString("btn_View", 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";
            stylebuild.Href = "../bootstrap/font-awesome/css/build-rtl.css";
            stylejvectormap.Href = "../plugins/jvectormap/jquery-jvectormap-1.2.2-rtl.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            styleallskins.Href = "../dist/css/_all-skins.min-rtl.css";
            stylecustom.Href = "../dist/css/custom-rtl.css";
            stylefonts.Href = "../dist/css/fonts.css";
            styledatepicker3.Href = "../plugins/datepicker/datepicker3-rtl.css";
            styledaterangepicker.Href = "../plugins/daterangepicker/daterangepicker-bs3-rtl.css";
            styleProgressBar.Href = "../css/ProgressBar-rtl.css";
            styleselect2.Href = "../plugins/select2/select2.min-rtl.css";
            styleAutocomp.Href = "../bootstrap/css/autoCompleteNew-rtl.css";
            Session["GridCSS"] = "../bootstrap/css/GridView-rtl.css";
            Session["SiteMapProvider"] = "Arabic";
        }
        else
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome.css";
            stylebuild.Href = "../bootstrap/font-awesome/css/build.css";
            stylejvectormap.Href = "../plugins/jvectormap/jquery-jvectormap-1.2.2.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min.css";
            styleallskins.Href = "../dist/css/_all-skins.min.css";
            stylecustom.Href = "../dist/css/custom.css";
            stylefonts.Href = "../dist/css/fonts.css";
            styledatepicker3.Href = "../plugins/datepicker/datepicker3.css";
            styledaterangepicker.Href = "../plugins/daterangepicker/daterangepicker-bs3.css";
            styleProgressBar.Href = "../css/ProgressBar.css";
            styleselect2.Href = "../plugins/select2/select2.min.css";
            styleAutocomp.Href = "../bootstrap/css/autoCompleteNew.css";
            Session["GridCSS"] = "../bootstrap/css/GridView.css";
            Session["SiteMapProvider"] = "English";
        }
    }
    private void loadMenu()
    {
        //Session["MenuCreate"] = "1";
        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
        string strType = string.Empty;
        if (Session["Demo"] != null)
        {
            switch (Convert.ToString(Session["Demo"]))
            {
                case "Standard":
                    strType = "Standard";
                    break;
                case "Express":
                    strType = "Express";
                    break;
                default:
                    break;
            }
        }
        cls_Common getmenu = new cls_Common();
        DataSet ds = new DataSet();
        DataSet dsChildMenus = new DataSet();
        DataSet dsReportsMenu = new DataSet();
        DataSet dsReportsSubMenu = new DataSet();
        int intReportsSubMenu = 0;
        int MenuNo = 1;
        if (Convert.ToInt32(Session["userGroup_Id"]) != 0)
        {
            ds = getmenu.getUserMenu(0, Convert.ToInt32(Session["userGroup_Id"]), strType);
            for (int iloop = 0; iloop <= ds.Tables[0].Rows.Count - 1; iloop++)
            {
                String txt = Convert.ToString(ds.Tables[0].Rows[iloop]["displayName"]);
                string title = txt + "_Title";
                string Icon = txt + "_Icon";
                dsChildMenus = getmenu.getUserMenu(Convert.ToInt32(ds.Tables[0].Rows[iloop]["pageid"]), Convert.ToInt32(Session["userGroup_Id"]), strType);
                if (dsChildMenus.Tables[0].Rows.Count > 0)
                {
                    this.MainMenu.InnerHtml += "<li id='" + Convert.ToString(ds.Tables[0].Rows[iloop]["displayName"]) + "'><a class='menu-hed' href='" + ds.Tables[0].Rows[iloop]["path"] + "' id='" + Convert.ToString(ds.Tables[0].Rows[iloop]["pageid"]) + "' title='" + rm.GetString(title, ci) + " '><img class='icon' src='dist/img/" + rm.GetString(Icon, ci) + "'><span>" + rm.GetString(txt, ci) + "</span><i class='fa fa-angle-right pull-right'></i></a>";
                    this.MainMenu.InnerHtml += "<ul class='treeview-menu'>";
                }
                else
                {
                    this.MainMenu.InnerHtml += "<li id='" + Convert.ToString(ds.Tables[0].Rows[iloop]["displayName"]) + "'><a class='menu-hed' href='" + ds.Tables[0].Rows[iloop]["path"] + "' id='" + Convert.ToString(ds.Tables[0].Rows[iloop]["pageid"]) + "' title='" + rm.GetString(title, ci) + " '><img class='icon' src='dist/img/" + rm.GetString(Icon, ci) + "'><span>" + rm.GetString(txt, ci) + "</span></a>";
                }
                for (int iloopChild = 0; iloopChild <= dsChildMenus.Tables[0].Rows.Count - 1; iloopChild++)
                {
                    string txtChild = Convert.ToString(dsChildMenus.Tables[0].Rows[iloopChild]["displayName"]);
                    string titlechild = txtChild + "_Title";
                    string[] splittedPath = Convert.ToString(ds.Tables[0].Rows[iloop]["path"]).Split("/".ToCharArray());
                    if (dsChildMenus.Tables[0].Rows[iloopChild]["path"].ToString() != string.Empty)
                    {
                        this.MainMenu.InnerHtml += "<li id='" + Convert.ToString(dsChildMenus.Tables[0].Rows[iloopChild]["displayName"]) + "'><a href='" + dsChildMenus.Tables[0].Rows[iloopChild]["path"] + "' id='" + Convert.ToString(dsChildMenus.Tables[0].Rows[iloopChild]["pageid"]) + "' title='" + rm.GetString(titlechild, ci) + " '><i class='fa fa-circle-o'></i>" + rm.GetString(txtChild, ci) + " </a>";
                    }
                    else
                    {
                        this.MainMenu.InnerHtml += "<li id='" + Convert.ToString(dsChildMenus.Tables[0].Rows[iloopChild]["displayName"]) + "'><a id='" + Convert.ToString(dsChildMenus.Tables[0].Rows[iloopChild]["pageid"]) + "' title='" + rm.GetString(titlechild, ci) + " '><i class='fa fa-circle-o'></i>" + rm.GetString(txtChild, ci) + " <i class='fa fa-angle-right pull-right MenuPadding' ></i></a>";
                    }
                    dsReportsSubMenu = getmenu.getUserMenu(Convert.ToInt32(dsChildMenus.Tables[0].Rows[iloopChild]["pageId"]), Convert.ToInt32(Session["userGroup_Id"]), strType);
                    if (dsReportsSubMenu.Tables[0].Rows.Count > 0)
                    {
                        this.MainMenu.InnerHtml += "<ul class='treeview-menu' id='" + dsChildMenus.Tables[0].Rows[iloopChild]["displayname"].ToString() + "' style='padding:10px'>";
                        for (intReportsSubMenu = 0; intReportsSubMenu <= dsReportsSubMenu.Tables[0].Rows.Count - 1; intReportsSubMenu++)
                        {
                            string txtChild1 = Convert.ToString(dsReportsSubMenu.Tables[0].Rows[intReportsSubMenu]["displayName"]);
                            string titlechild1 = txtChild1 + "_Title";
                            this.MainMenu.InnerHtml += "<li class='active MenuPadding1' id='" + Convert.ToString(dsReportsSubMenu.Tables[0].Rows[intReportsSubMenu]["displayName"]) + "'><a href='" + dsReportsSubMenu.Tables[0].Rows[intReportsSubMenu]["path"] + "' id='" + Convert.ToString(dsReportsSubMenu.Tables[0].Rows[intReportsSubMenu]["pageid"]) + "' title='" + rm.GetString(titlechild1, ci) + " '><i class='fa fa-circle-o'></i>" + rm.GetString(txtChild1, ci) + "</a></li>";
                        }
                        if (dsReportsSubMenu.Tables[0].Rows.Count == 2)
                        {
                            this.MainMenu.InnerHtml += "<li class='line-spec'></li>";
                        }
                        this.MainMenu.InnerHtml += "</ul>";
                    }
                    this.MainMenu.InnerHtml += "</li>";
                }
                if (dsChildMenus.Tables[0].Rows.Count > 0)
                {
                    this.MainMenu.InnerHtml += "</ul>";
                }
                this.MainMenu.InnerHtml += "</li>";
                dsChildMenus.Dispose();
                MenuNo = MenuNo + 1;
            }
        }
        ds.Clear();
        ds.Dispose();
        Session["TopMenu"] = this.MainMenu.InnerHtml;
        Session["TopMenuPrevious"] = this.MainMenu.InnerHtml;
        LoadYear();
    }
    private void LoadYear()
    {
        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;
        //Year Setting Display in Main Menu
        string sYearValue = Convert.ToString(Cache["ActiveYearSetting"]);
        if (sYearValue != null && sYearValue != "")
        {
            lblYear.Text = rm.GetString("Year", ci);
            lblYearData.Text = sYearValue;
            if (lblYearData.Text.Contains("January"))
            {
                lblYearData.Text = lblYearData.Text.Replace("January", "Jan");
            }
            if (lblYearData.Text.Contains("February"))
            {
                lblYearData.Text = lblYearData.Text.Replace("February", "Feb");
            }
            if (lblYearData.Text.Contains("March"))
            {
                lblYearData.Text = lblYearData.Text.Replace("March", "Mar");
            }
            if (lblYearData.Text.Contains("April"))
            {
                lblYearData.Text = lblYearData.Text.Replace("April", "Apr");
            }
            if (lblYearData.Text.Contains("May"))
            {
                lblYearData.Text = lblYearData.Text.Replace("May", "May");
            }
            if (lblYearData.Text.Contains("June"))
            {
                lblYearData.Text = lblYearData.Text.Replace("June", "Jun");
            }
            if (lblYearData.Text.Contains("July"))
            {
                lblYearData.Text = lblYearData.Text.Replace("July", "Jul");
            }
            if (lblYearData.Text.Contains("August"))
            {
                lblYearData.Text = lblYearData.Text.Replace("August", "Aug");
            }
            if (lblYearData.Text.Contains("September"))
            {
                lblYearData.Text = lblYearData.Text.Replace("September", "Sep");
            }
            if (lblYearData.Text.Contains("October"))
            {
                lblYearData.Text = lblYearData.Text.Replace("October", "Oct");
            }
            if (lblYearData.Text.Contains("November"))
            {
                lblYearData.Text = lblYearData.Text.Replace("November", "Nov");
            }
            if (lblYearData.Text.Contains("December"))
            {
                lblYearData.Text = lblYearData.Text.Replace("December", "Dec");
            }
        }
        else
        {
            lblYearData.Text = string.Empty;
        }
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Login.Text = rm.GetString("Login", ci);
        lbtn_logout.Text = rm.GetString("btn_Logout", ci);
        lbl_OT_Comp.Text = rm.GetString("lbl_NewsEvents", ci);
    }
    protected void lbtn_logout_Click(object sender, EventArgs e)
    {
        if (Session["EmployeeId"] != null)
        {
            string userId = Convert.ToString(Session["EmployeeId"]);
            string sUser = Convert.ToString(Cache[Session["EmployeeId"].ToString().ToUpper()]);
            if (sUser != null && sUser != String.Empty)
            {
                HttpContext.Current.Cache.Remove(Session["EmployeeId"].ToString().ToUpper());
                Session.Clear();
            }
            ObjEmployeeEventData.InsertUpdateLastLogout(userId);
        }
        string currentPageFileName = new FileInfo(this.Request.Url.LocalPath).Name;
        RedirectToLoginPage(currentPageFileName);
    }

    protected void ShiftINManualTimeEntry_ServerClick(object sender, EventArgs e)
    {
        punchTypes = "Shift In";
        ManualTimeEntryInsertandValidation();
        LoadTodayPunchTime();
    }
    protected void ShiftOutManualTimeEntry_ServerClick(object sender, EventArgs e)
    {
        punchTypes = "Shift Out";
        ManualTimeEntryInsertandValidation();
        LoadTodayPunchTime();
    }
    protected void BreakOutManualTimeEntry_ServerClick(object sender, EventArgs e)
    {
        punchTypes = "Break Out";
        ManualTimeEntryInsertandValidation();
        LoadTodayPunchTime();
    }
    protected void BrakINtManualTimeEntry_ServerClick(object sender, EventArgs e)
    {
        punchTypes = "Break In";
        ManualTimeEntryInsertandValidation();
        LoadTodayPunchTime();
    }

    private void ManualTimeEntryInsertandValidation()
    {
        try
        {
            DataSet DSManualEntry = new DataSet(); Int32 intEmployeeID = 0;
            if (Convert.ToString(Session["EmployeeId"]).ToUpper() != "ADMIN")
            {
                //string path = HttpContext.Current.Request.Url.AbsolutePath.ToUpper();
                string EmployeeId = Convert.ToString(Session["EmployeeId"]);
                String strEntryDate = Convert.ToString(DateTime.Now.ToShortDateString());
                String ShiftTime = Convert.ToString(DateTime.Now.ToString("hh:mm:ss tt"));
                string strInDate = string.Empty, strOutDate = string.Empty, strManualTimeEntryStatus = string.Empty;

                var intShiftId = objShiftExchange.SelectShiftId(EmployeeId, DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString());
                if (intShiftId == 0)
                {
                    // Message(rm.GetString("Msg_ManualTimeEntry", ci));
                    return;
                }
                else
                {
                    string punchType = punchTypes;

                    if (Convert.ToString(EmployeeId) != "")
                    {
                        int intCompanyId = GetCompanyId(Convert.ToInt32(Session["EmpAutoId"]));
                        if (Convert.ToInt32(Session["EmpAutoId"]) == 1)
                        {
                            intCompanyId = 1;
                        }
                        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]["webClockInOut"]) == false)
                                        strManualTimeEntryStatus = "Approved";
                                    else
                                        strManualTimeEntryStatus = GetManualTimeEntryStatus(Convert.ToString(EmployeeId));
                                }
                                else
                                    strManualTimeEntryStatus = GetManualTimeEntryStatus(Convert.ToString(EmployeeId));
                            }
                        }
                        else
                            strManualTimeEntryStatus = "Approved";


                        if (punchType == "Shift In" || punchType == "Break In")
                        {
                            objManualEntry.InsertManualEntry
                            (
                                Convert.ToString(EmployeeId),
                                DateTime.Parse(DateTime.Now.ToString("MM/dd/yyyy")),
                                DateTime.Now.ToString("MM/dd/yyyy"),
                                DateTime.Now.ToString("HH:mm:ss tt"),
                                punchType,
                                "",
                                "",
                                "",
                                787,
                                0,
                                "Web " + punchType + " from" + "ManualPunch",
                                0,
                                strManualTimeEntryStatus,
                                DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"),
                                Convert.ToInt32(Session["EmpAutoID"]),
                               Convert.ToString(ViewState["YearSettings_Id"]), 0, 0
                               );

                            string strScript = "";
                            strScript = strScript + "<script language='javascript'>";

                            if (punchType == "Shift In")
                            {
                                strScript = strScript + "alert('" + rm.GetString("Msg_ManualClockInPunchSuccessfull") + "');";
                            }
                            else if (punchType == "Break In")
                            {
                                strScript = strScript + "alert('" + rm.GetString("Msg_ManualBreakInPunchSuccessfull") + "');";
                            }

                           // strScript = strScript + "location.replace('TA_UI_Transactions_ManualTimeEntry.aspx');";
                            strScript = strScript + "</script>";
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
                        }
                        else
                        {
                            objManualEntry.InsertManualEntry
                             (
                                 Convert.ToString(EmployeeId),
                                 DateTime.Parse(DateTime.Now.ToString("MM/dd/yyyy")),
                                 "",
                                 "",
                                 "",
                                 DateTime.Now.ToString("MM/dd/yyyy"),
                                 DateTime.Now.ToString("HH:mm:ss tt"),
                                 punchType,
                                 0,
                                 787,
                                 "Web " + punchType + " from" + "ManualPunch",
                                 0,
                                 strManualTimeEntryStatus,
                                 DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"),
                                 Convert.ToInt32(Session["EmpAutoID"]),
                                 Convert.ToString(ViewState["YearSettings_Id"]), 0, 0
                                );

                            string strScript = "";
                            strScript = strScript + "<script language='javascript'>";


                            if (punchType == "Shift Out")
                            {
                                strScript = strScript + "alert('" + rm.GetString("Msg_ManualClockOutPunchSuccessfull") + "');";
                            }
                            else if (punchType == "Break Out")
                            {
                                strScript = strScript + "alert('" + rm.GetString("Msg_ManualBreakOutPunchSuccessfull") + "');";
                            }

                            //strScript = strScript + "location.replace('TA_UI_Transactions_ManualTimeEntry.aspx');";
                            strScript = strScript + "</script>";
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);

                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {

        }
    }

    public string GetManualTimeEntryStatus(string inEmployeeCodeId)
    {
        DataSet dsReportingPersons = new DataSet();
        DataTable dtReportingPersons = new DataTable();
        string strManualTimeEntryStatus = string.Empty;
        Int64 intReportTo = 0, intLeave_Sanc_Authority = 0;
        dsReportingPersons = objEmployee.GetReportingToSanctionAuthorityByCodeID(inEmployeeCodeId);
        dtReportingPersons = dsReportingPersons.Tables[0];
        if (dtReportingPersons.Rows.Count > 0)
        {
            if (dtReportingPersons.Rows[0]["Reporting_To"] != DBNull.Value && dtReportingPersons.Rows[0]["Reporting_To"].ToString() != string.Empty)
                intReportTo = Int64.Parse(dtReportingPersons.Rows[0]["Reporting_To"].ToString());
            if (dtReportingPersons.Rows[0]["Leave_Sanc_Authority"] != DBNull.Value && dtReportingPersons.Rows[0]["Leave_Sanc_Authority"].ToString() != string.Empty)
                intLeave_Sanc_Authority = Int64.Parse(dtReportingPersons.Rows[0]["Leave_Sanc_Authority"].ToString());
        }
        if (intReportTo != 0 && intLeave_Sanc_Authority != 0 && intReportTo != intLeave_Sanc_Authority)
            strManualTimeEntryStatus = "Pending";
        else
            strManualTimeEntryStatus = "Forwarded";
        return (strManualTimeEntryStatus);
    }
    public int GetCompanyId(int intEmployeeId)
    {
        DataSet dsEmp = new DataSet();
        LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
        dsEmp = objLeaveApplicationEventData.GetEmployeeLevel(Convert.ToInt32(intEmployeeId));
        int iEmployeeLevelID = 0;
        if (dsEmp != null && dsEmp.Tables.Count != 0 && dsEmp.Tables[0].Rows.Count != 0)
        {
            if (dsEmp.Tables[0].Rows[0]["levelID"] != DBNull.Value)
            {
                iEmployeeLevelID = Convert.ToInt32(dsEmp.Tables[0].Rows[0]["levelID"]);
            }
        }
        //Select Company
        org_levelsEventData getOrg = new org_levelsEventData();
        getOrg.GetOrgParentLevels_FromID(iEmployeeLevelID);
        DataSet ds_getOrg = new DataSet();
        ds_getOrg = getOrg.GetOrgParentLevels();
        int iCompanyID = 0;
        if (ds_getOrg != null)
        {
            if (ds_getOrg.Tables[0].Rows.Count > 0)
            {
                iCompanyID = Convert.ToInt32(ds_getOrg.Tables[0].Rows[0]["LevelID"].ToString());
            }
        }
        return iCompanyID;
    }

    protected void MyProfile_ServerClick(object sender, EventArgs e)
    {
        try
        {
            DataSet dsMyProfile = new DataSet(); Int32 intEmployeeID = 0;
            if (Convert.ToString(Session["EmployeeId"]).ToUpper() != "ADMIN")
            {
                dsMyProfile = ObjEmployeeEventData.GetMyProfileDetails(Session["EmployeeId"].ToString());
                Session["MyProfileURL"] = HttpContext.Current.Request.Url.AbsolutePath;
                string path = HttpContext.Current.Request.Url.AbsolutePath.ToUpper();
                if (dsMyProfile.Tables.Count > 0)
                {
                    if (dsMyProfile.Tables[0].Rows.Count > 0)
                    {
                        intEmployeeID = Convert.ToInt32(dsMyProfile.Tables[0].Rows[0][0].ToString());
                        // Response.Redirect("/Masters/TA_UI_Master_EmployeeModify.aspx?Emp_Id=" + intEmployeeID.ToString() + "&IsFrom=MyProfile", false);
                        if (path.Contains("/MASTERS/"))
                        {
                            Response.Redirect("TA_UI_Master_MyProfileView.aspx?Emp_Id=" + intEmployeeID.ToString() + "&IsFrom=MyProfile");
                        }
                        else
                        {
                            string currentPageFileName = new FileInfo(this.Request.Url.LocalPath).Name;
                            if (currentPageFileName == "MYCALENDAR.ASPX" || currentPageFileName == "DASHBOARD.ASPX" || currentPageFileName == "DASHBOARD_SAMSUNG.ASPX")
                            {
                                Response.Redirect("Masters/TA_UI_Master_MyProfileView.aspx?Emp_Id=" + intEmployeeID.ToString() + "&IsFrom=MyProfile");
                            }
                            else
                            {
                                Response.Redirect("~/Masters/TA_UI_Master_MyProfileView.aspx?Emp_Id=" + intEmployeeID.ToString() + "&IsFrom=MyProfile");
                            }

                        }
                    }
                }

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
