private void InitializeLabelTimeClock(DateTime seltime)
        {
            TimeClock timeclock = new TimeClock();
            int userid = Convert.ToInt32(Session["UserId"]);
            LabelTimeClock.Text = "";

            User user = new User();
            user = user.GetUser(userid,seltime);
            if (user != null)
            {
                if (TimeSpan.Parse(user.startTime) > TimeSpan.Parse(user.endTime)) //user with shifting hour
                {
                    TimeSpan cutOfTime = user.GetMyCutOfTime();
                    string cot = cutOfTime.ToString("hh\\:mm");
                    timeclock = timeclock.GetStartEndTimeForShifting(Convert.ToInt32(user.EmployeeNumber), Convert.ToDateTime(seltime.AddDays(-1).ToString("yyyy-MM-dd") + " " + cutOfTime.ToString("hh\\:mm")), Convert.ToDateTime(seltime.ToString("yyyy-MM-dd") + " " + cutOfTime.ToString("hh\\:mm")), false);
                    if (timeclock != null)
                    {
                        LabelTimeClock.Text = "Time-In: " + timeclock.starttime.ToString("hh:mm tt") + ", Time-Out: " + timeclock.endtime.ToString("hh:mm tt");
                    }
                    timeclock = new TimeClock();
                    timeclock = timeclock.GetStartEndTimeForShifting(Convert.ToInt32(user.EmployeeNumber), Convert.ToDateTime(seltime.ToString("yyyy-MM-dd") + " " + cutOfTime.ToString("hh\\:mm\\:ss")), Convert.ToDateTime(seltime.AddDays(1).ToString("yyyy-MM-dd") + " " + cutOfTime.ToString("hh\\:mm\\:ss")), true);
                    if (timeclock != null)
                    {
                        if (LabelTimeClock.Text != "")
                            LabelTimeClock.Text += " | ";
                        LabelTimeClock.Text += "Time-In: " + timeclock.starttime.ToString("hh:mm tt") + ", Time-Out: " + timeclock.endtime.ToString("hh:mm tt");
                    }
                }
                else
                {
                    DateTime sdate = Convert.ToDateTime(seltime.ToString("yyyy-MM-dd") + " 00:00");
                    DateTime edate = Convert.ToDateTime(seltime.ToString("yyyy-MM-dd") + " 23:59");
                    timeclock = timeclock.GetStartEndTime(Convert.ToInt32(user.EmployeeNumber), sdate, edate);
                    if (timeclock != null)
                    {
                        LabelTimeClock.Text = "Time-In: " + timeclock.starttime.ToString("hh:mm tt") + ", Time-Out: " + timeclock.endtime.ToString("hh:mm tt");
                    }
                }
            }
        }
        private bool HasTimeClockGap(int userid, DateTime selecteddate)
        {
            //Checks if the user has a gap between his facetime time and the time key in, in jobtrack
            bool result = false;
            User user = new User();
            user = user.GetUser(userid,selecteddate);
            if (user.EmployeeNumber != 0)
            {
                if (TimeSpan.Parse(user.startTime) > TimeSpan.Parse(user.endTime)) //user with shifting hour
                {
                    TimeClock timeclock1 = new TimeClock();
                    TimeClock timeclock2 = new TimeClock();
                    TimeSpan cutOffTime = user.GetMyCutOfTime();

                    timeclock2 = timeclock2.GetStartEndTimeForShifting(Convert.ToInt32(user.EmployeeNumber), Convert.ToDateTime(selecteddate.AddDays(-1).ToString("yyyy-MM-dd") + " " + cutOffTime.ToString("hh\\:mm\\:ss")), Convert.ToDateTime(selecteddate.ToString("yyyy-MM-dd") + " " + cutOffTime.ToString("hh\\:mm\\:ss")), false);
                    if (timeclock2 != null)
                    {
                        var data = GetJobTrackerListWithEndTimeAndNotRejected(userid, Convert.ToDateTime(selecteddate.ToString("yyyy-MM-dd") + " 00:00:00"), Convert.ToDateTime(selecteddate.ToString("yyyy-MM-dd") + " " + cutOffTime.ToString("hh\\:mm\\:ss")));
                        if (data.Count > 0)
                        {
                            if (timeclock2.starttime < Convert.ToDateTime(data[0].StartTime))
                                result = true;
                            if ((timeclock2.endtime.TimeOfDay < new TimeSpan(0,30,0) ? timeclock1.endtime : timeclock2.endtime.AddMinutes(-30)) > Convert.ToDateTime(data[data.Count - 1].EndTime))
                                result = true;
                        }
                        else
                        {
                            result = true;
                        }
                    }
                    if (result == false)
                    {
                        timeclock1 = timeclock1.GetStartEndTimeForShifting(Convert.ToInt32(user.EmployeeNumber), Convert.ToDateTime(selecteddate.ToString("yyyy-MM-dd") + " " + cutOffTime.ToString("hh\\:mm\\:ss")), Convert.ToDateTime(selecteddate.AddDays(1).ToString("yyyy-MM-dd") + " " + cutOffTime.ToString("hh\\:mm\\:ss")), true);
                        if (timeclock1 != null)
                        {
                            var data = GetJobTrackerListWithEndTimeAndNotRejected(userid, Convert.ToDateTime(selecteddate.ToString("yyyy-MM-dd") + " " + cutOffTime.ToString("hh\\:mm\\:ss")), Convert.ToDateTime(selecteddate.AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00"));
                            if (data.Count > 0)
                            {
                                if (timeclock1.starttime.AddMinutes(30) < Convert.ToDateTime(data[0].StartTime))
                                    result = true;
                                if ((timeclock1.endtime.TimeOfDay < new TimeSpan(0, 30, 0) ? timeclock1.endtime : timeclock1.endtime.AddMinutes(-30)) > Convert.ToDateTime(data[data.Count - 1].EndTime))
                                    result = true;
                            }
                            else
                            {
                                result = true;
                            }
                        }
                    }

                }
                else
                {
                    TimeClock timeclock = new TimeClock();
                    timeclock = timeclock.GetStartEndTime(Convert.ToInt32(user.EmployeeNumber), Convert.ToDateTime(selecteddate.ToString("dd MMM yyyy") + " 00:00:00"), Convert.ToDateTime(selecteddate.ToString("dd MMM yyyy") + " 23:59:59"));

                    if (timeclock != null)
                    {
                        var data = GetJobTrackerListWithEndTimeAndNotRejected(userid,selecteddate);
                        if (data.Count > 0)
                        {
                            //TimeSetting timesetting = new TimeSetting();
                            //timesetting = timesetting.GetTimeSetting();
                            //if (timeclock.starttime.AddMinutes(timesetting.Interval) < Convert.ToDateTime(data[0].StartTime))
                            if (timeclock.starttime.AddMinutes(30) < Convert.ToDateTime(data[0].StartTime))
                                result = true;
                            //if (timeclock.endtime.AddMinutes(-1 * timesetting.Interval) > Convert.ToDateTime(data[data.Count - 1].EndTime))
                            if (timeclock.endtime.AddMinutes(-30) > Convert.ToDateTime(data[data.Count - 1].EndTime))
                                result = true;
                        }
                        else
                        {
                            result = true;
                        }
                    }
                }
            }
            return result;
        }
        //private void InitializeModalStartTime(string value = "")
        //{
        //    //GenerateStartHour();
        //    //GenerateStartMin(Convert.ToInt32(modalDropDownStartTimeHour.SelectedItem.Value));
        //    Time time = new Time();
        //    var timelist = time.GetTimeList();
        //    DateTime date = Convert.ToDateTime(txtBoxDate.Text);
        //    List<Time> starttime = new List<Time>();
        //    TimeSpan curtime = TimeSpan.Parse(DateTime.Now.Hour + ":" + DateTime.Now.Minute);
        //    if (date.CompareTo(DateTime.Today) == 0)
        //    {
        //        foreach (Time t in timelist)
        //        {
        //            TimeSpan selectedTime = TimeSpan.Parse(t.C24hrConversion);
        //            if (selectedTime >= curtime)
        //            {
        //                time.Position = t.Position > 1 ? (t.Position-1) : t.Position;
        //                break;
        //            }
        //        }
        //        starttime = time.GetStartTimeList(time.Position);
        //    }
        //    else
        //    {
        //        starttime = time.GetStartTimeList();
        //    }
        //    //remove used time
        //    JobTracker jobtracker = new JobTracker();
        //    var joblist = jobtracker.GetJobTrackerList(Convert.ToInt32(Session["UserId"]), date);
        //    for (int i = 0; i < joblist.Count; i++)
        //    {
        //        if (value.Trim() != "")
        //        {
        //            if (joblist[i].StartTime == DateTime.Parse(value))
        //                continue;
        //        }
        //        if (joblist[i].EndTime != null)
        //        {
        //            Time stime = time.GetTime(Convert.ToDateTime(joblist[i].StartTime));
        //            Time etime = time.GetTime(Convert.ToDateTime(joblist[i].EndTime));
        //            RemoveUsedStartTime(starttime, stime.Position, etime.Position);
        //        }
        //    }
        //    //
        //    modalDropDownStartTime.DataSource = starttime;
        //    modalDropDownStartTime.DataTextField = "Description";
        //    modalDropDownStartTime.DataValueField = "C24hrConversion";
        //    modalDropDownStartTime.DataBind();
        //    if (value.Trim() == "")
        //    {
        //        bool hasSelected = false;
        //        int maxindex = 0;
        //        TimeSpan maxTime = new TimeSpan(23, 59, 0);
        //        for (int i = 0; i < modalDropDownStartTime.Items.Count;i++)
        //        {
        //            TimeSpan selectedTime = TimeSpan.Parse(modalDropDownStartTime.Items[i].Value);
        //            if (selectedTime < curtime && maxTime < selectedTime)
        //            {
        //                maxindex = i;
        //                maxTime = selectedTime;
        //            }
        //            if (selectedTime >= curtime && hasSelected == false)
        //            {
        //                modalDropDownStartTime.Items[i].Selected = true;
        //                hasSelected = true;
        //            }
        //            else
        //                modalDropDownStartTime.Items[i].Selected = false;
        //        }
        //        if (hasSelected == false && modalDropDownStartTime.Items.Count > 0)
        //        {
        //            if (maxindex > 0)
        //                modalDropDownStartTime.Items[maxindex].Selected = true;
        //            else
        //                modalDropDownStartTime.Items[modalDropDownStartTime.Items.Count - 1].Selected = true;
        //        }
        //    }
        //    else
        //    {
        //        DateTime stime = DateTime.Parse(value);
        //        string selectedTime = stime.Hour + ":" + stime.Minute;
        //        foreach (ListItem i in modalDropDownStartTime.Items)
        //        {
        //            if (TimeSpan.Parse(i.Value.Trim()) == TimeSpan.Parse(selectedTime.Trim()))
        //            {
        //                i.Selected = true;
        //                break;
        //            }
        //        }
        //    }
        //}
        private void GenerateStartHour(string selectedTime = "")
        {
            Session["Is2ndPartComplete"] = true;
            DateTime selectedDate = DateTime.Parse(txtBoxDate.Text); //Get selected Time
            bool isCurrentDate = false;
            int userid = Convert.ToInt32(Session["UserId"]);

            JobTracker jobtracker = new JobTracker();
            TimeSetting timeSetting = new TimeSetting();
            timeSetting = timeSetting.GetTimeSetting();
            TimeSpan startTime = new TimeSpan();
            List<TimeSpan> availableTime = new List<TimeSpan>();
            List<TimeSpan> timeMarker = new List<TimeSpan>();
            List<TimeSpan> cutOffMarker = new List<TimeSpan>();
            User user = new User();
            user = user.GetUser(userid, selectedDate);
            TimeSpan cutOffTime = user.GetMyCutOfTime();
            //generates the timelist
            if (Session["StartTime"] != null)
            {
                string[] s = Session["StartTime"].ToString().Split(':') ;
                startTime = new TimeSpan(Convert.ToInt32(s[0]), Convert.ToInt32(s[1]),0);
            }

            for (int i = 0; i < 24; i++)
            {
                for (int j = 0; j < 60; j += timeSetting.Interval)
                {
                    availableTime.Add(new TimeSpan(i, j, 0));
                    timeMarker.Add(new TimeSpan(i, j, 0));
                    if (cutOffTime <= new TimeSpan(i, j, 0))
                    {
                        cutOffMarker.Add(new TimeSpan(i,j,0));
                    }
                }
            }

            if (selectedDate.CompareTo(DateTime.Today) == 0)
            {
                isCurrentDate = true;
            }

            Dictionary<string, string> hour = new Dictionary<string, string>();
            var usedTime = jobtracker.GetJobTrackerList(userid, selectedDate,false);

            for (int i = 0; i < usedTime.Count; i++)
            {
                for (int j = 0; j < availableTime.Count; j++)
                {
                    if (isCurrentDate == true && DateTime.Now.TimeOfDay < availableTime[j])
                    {
                        availableTime.RemoveAt(j);
                        --j;
                    }
                    else if (usedTime[i].EndTime != null)
                    {
                        TimeSpan stime = Convert.ToDateTime(usedTime[i].StartTime).TimeOfDay;
                        TimeSpan etime;
                        if (Convert.ToDateTime(usedTime[i].EndTime).Date > Convert.ToDateTime(usedTime[i].StartTime).Date) //Convert 00:00:00 next day to 1.00:00:00 Timespan
                        {
                            etime = new TimeSpan(24,0,0);
                        }
                        else
                        {
                            etime = new TimeSpan(Convert.ToDateTime(usedTime[i].EndTime).Hour, Convert.ToDateTime(usedTime[i].EndTime).Minute, 0);
                        }
                        if (availableTime[j] >= stime && availableTime[j] < etime)
                        {
                            if (Session["StartTime"] != null && (startTime >= stime && startTime < etime))
                                continue;
                            else
                            {
                                availableTime.RemoveAt(j);
                                --j;
                            }
                        }
                    }
                }
            }
            if (usedTime.Count < 1)
            {
                for (int j = 0; j < availableTime.Count; j++)
                {
                    if (isCurrentDate == true && DateTime.Now.TimeOfDay < availableTime[j])
                    {
                        availableTime.RemoveAt(j);
                        --j;
                    }
                }
            }

            if (TimeSpan.Parse(user.startTime) > TimeSpan.Parse(user.endTime)) //user with shifting hour
            {

                TimeClock timeclock = new TimeClock();
                timeclock = timeclock.GetStartEndTimeForShifting(Convert.ToInt32(user.EmployeeNumber), Convert.ToDateTime(selectedDate.AddDays(-1).ToString("yyyy-MM-dd") + " " + cutOffTime.ToString("hh\\:mm\\:ss")), Convert.ToDateTime(selectedDate.ToString("yyyy-MM-dd") + " " + cutOffTime.ToString("hh\\:mm\\:ss")), false);
                bool notComplete = false;
                if (timeclock != null)
                {
                    for (int i = 0; i < availableTime.Count; i++)
                    {
                        if (availableTime[i] >= timeclock.starttime.TimeOfDay && availableTime[i] < timeclock.endtime.TimeOfDay) //Check if the 2nd part of the previous day is completed
                        {
                            notComplete = true;
                            Session["Is2ndPartComplete"] = false;
                            break;
                        }
                    }
                }
                if (notComplete)
                {
                    //make sure that there is no gap between time
                    for (int i = 0; i < availableTime.Count; i++)
                    {
                        if (availableTime[i] >= cutOffTime)
                        {
                            if (i < availableTime.Count - 1)
                            {
                                availableTime.RemoveRange(i, availableTime.Count - i);
                                break;
                            }
                        }
                    }
                    if (!isCurrentDate)
                    {
                        if (availableTime.Count > 0 && (availableTime[availableTime.Count - 1].Hours != availableTime[availableTime.Count - 2].Hours)) //remove last hour
                            availableTime.RemoveAt(availableTime.Count - 1);
                    }

                    if (usedTime.Count > 0)
                    {
                        for (int i = 0; i < availableTime.Count; i++)
                        {
                            if (availableTime[i] != timeMarker[i])
                            {
                                if (i < availableTime.Count - 1)
                                {
                                    availableTime.RemoveRange(i + 1, availableTime.Count - i - 1);
                                    break;
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (!isCurrentDate)
                    {
                        if (availableTime.Count > 0 && (availableTime[availableTime.Count - 1].Hours != availableTime[availableTime.Count - 2].Hours)) //remove last hour
                            availableTime.RemoveAt(availableTime.Count - 1);
                    }
                    if (usedTime.Count > 0)
                    {
                        int loop = 0;
                        for (int i = 0; i < availableTime.Count; i++)
                        {
                            if (availableTime[i] >= cutOffTime)
                            {
                                if (availableTime[i] != cutOffMarker[loop])
                                {
                                    if (i < availableTime.Count - 1)
                                    {
                                        availableTime.RemoveRange(i + 1, availableTime.Count - i - 1);
                                        break;
                                    }
                                }
                                loop++;
                            }
                        }
                    }
                }
            }
            else
            {
                if (!isCurrentDate)
                {
                    if (availableTime.Count > 0 && (availableTime[availableTime.Count - 1].Hours != availableTime[availableTime.Count - 2].Hours)) //remove last hour
                        availableTime.RemoveAt(availableTime.Count - 1);
                }

                //make sure that there is no gap between time
                if (usedTime.Count > 0)
                {
                    for (int i = 0; i < availableTime.Count; i++)
                    {
                        if (availableTime[i] != timeMarker[i])
                        {
                            if (i < availableTime.Count - 1)
                            {
                                availableTime.RemoveRange(i + 1, availableTime.Count - i - 1);
                                break;
                            }
                        }
                    }
                }
            }

            int curtime = 24;

            for (int i = 0; i < availableTime.Count; i++)
            {
                if (curtime != availableTime[i].Hours)
                {
                    curtime = availableTime[i].Hours;
                    hour.Add(curtime > 9 ? curtime.ToString() : "0" + curtime.ToString(), curtime > 9 ? curtime.ToString() : "0" + curtime.ToString());
                }
            }
            modalDropDownStartTimeHour.DataSource = hour;
            modalDropDownStartTimeHour.DataTextField = "Key";
            modalDropDownStartTimeHour.DataValueField = "Value";
            modalDropDownStartTimeHour.DataBind();

            if (selectedTime.Trim() != "")
            {
                string[] s = selectedTime.Split(':');
                foreach (ListItem i in modalDropDownStartTimeHour.Items)
                {
                    if (i.Text.Trim() == s[0].Trim())
                        i.Selected = true;
                }
            }
            else if (isCurrentDate == true && usedTime.Count < 1) //select the time closest to current time
            {
                int s = DateTime.Now.Hour;
                decimal gap = 1000;
                int index = 0;
                for (int i = 0; i < modalDropDownStartTimeHour.Items.Count; i++)
                {
                    decimal cgap = Math.Abs(Convert.ToInt32(modalDropDownStartTimeHour.Items[i].Value) - s);
                    if (cgap == 0)
                    {
                        gap = cgap;
                        index = i;
                        break;
                    }
                    else if (cgap < gap)
                    {
                        gap = cgap;
                        index = i;
                    }
                }
                modalDropDownStartTimeHour.Items[index].Selected = true;
            }
            else if (hour.Count < 1)
            {
            }
            else
            {
                modalDropDownStartTimeHour.Items[modalDropDownStartTimeHour.Items.Count - 1].Selected = true;
            }
        }