protected void gridViewTime_Command(object sender, GridViewCommandEventArgs e)
 {
     if (e.CommandName == "Select")
     {
         GetMyAccessRights();
         modalLabelError.Text = "";
         modalLabelError.Visible = false;
         modalBtnSubmit.Visible = Convert.ToBoolean(myAccessRights.CanUpdate);
         modalBtnSubmit.CommandArgument = "Update";
         TimeSetting timesetting = new TimeSetting();
         timesetting = timesetting.GetTimeSetting();
         modalTxtBoxInterval.Text = timesetting.Interval.ToString();
         this.programmaticModalPopup.Show();
     }
 }
        private void GenerateEndMin(int shour, int smin,string ehour,string selectedTime = "")
        {
            Dictionary<string, string> mins = new Dictionary<string, string>();
            if (modalDropDownEndTimeHour.SelectedItem.Text.Trim() == "--")
            {
                mins.Add("--", "00");
            }
            if (ehour != "--")
            {
                DateTime selectedDate = DateTime.Parse(txtBoxDate.Text);
                int userid = Convert.ToInt32(Session["UserId"]);
                bool isCurrentDate = false;

                int hour = Convert.ToInt32(ehour);
                JobTracker jobtracker = new JobTracker();
                TimeSetting timeSetting = new TimeSetting();
                timeSetting = timeSetting.GetTimeSetting();
                TimeSpan selTime = new TimeSpan(shour, smin, 0);
                List<TimeSpan> availableTime = new List<TimeSpan>();
                User user = new User();
                user = user.GetUser(userid, selectedDate);
                TimeSpan cutOffTime = user.GetMyCutOfTime();

                if (hour != 24)
                {
                    for (int j = 0; j < 60; j += timeSetting.Interval)
                    {
                        if (selTime < cutOffTime)
                        {
                            if (new TimeSpan(hour, j, 0) <= cutOffTime)
                            {
                                availableTime.Add(new TimeSpan(hour, j, 0));
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            availableTime.Add(new TimeSpan(hour, j, 0));
                        }
                    }
                }
                else
                {
                    if (selTime < cutOffTime)
                    {
                        if (new TimeSpan(hour, 0, 0) <= cutOffTime)
                        {
                            availableTime.Add(new TimeSpan(hour, 0, 0));
                        }
                    }
                    else
                    {
                        availableTime.Add(new TimeSpan(hour, 0, 0));
                    }
                }

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

                jobtracker = jobtracker.GetNextUsedTime(userid, DateTime.Parse(selectedDate.Year + "-" + selectedDate.Month + "-" + selectedDate.Day + " " + selTime.ToString()), selectedDate);
                TimeSpan stime = jobtracker == null ? new TimeSpan(23, 59, 59) : Convert.ToDateTime(jobtracker.StartTime).TimeOfDay;
                if (Session["StartTime"] != null)
                {
                    TimeSpan ts = TimeSpan.Parse(Session["StartTime"].ToString());
                    if (ts == stime)
                    {
                        jobtracker = jobtracker.GetNextUsedTime(userid, DateTime.Parse(selectedDate.Year + "-" + selectedDate.Month + "-" + selectedDate.Day + " " + Session["StartTime"].ToString()), selectedDate);
                        stime = jobtracker == null ? new TimeSpan(23, 59, 59) : Convert.ToDateTime(jobtracker.StartTime).TimeOfDay;
                    }
                }

                for (int j = 0; j < availableTime.Count; j++)
                {
                    if (availableTime[j] > stime)
                    {
                        availableTime.RemoveAt(j);
                        --j;
                    }
                    else if (isCurrentDate == true && DateTime.Now.TimeOfDay < availableTime[j])
                    {
                        availableTime.RemoveAt(j);
                        --j;
                    }
                    else if (selTime >= availableTime[j])
                    {
                        availableTime.RemoveAt(j);
                        --j;
                    }
                }

                int curtime = 60;

                for (int i = 0; i < availableTime.Count; i++)
                {
                    if (curtime != availableTime[i].Minutes)
                    {
                        curtime = availableTime[i].Minutes;
                        mins.Add(curtime > 9 ? curtime.ToString() : "0" + curtime.ToString(), curtime > 9 ? curtime.ToString() : "0" + curtime.ToString());
                    }
                }
            }

            modalDropDownEndTimeMin.DataSource = mins;
            modalDropDownEndTimeMin.DataTextField = "Key";
            modalDropDownEndTimeMin.DataValueField = "Value";
            modalDropDownEndTimeMin.DataBind();

            if (selectedTime.Trim() != "")
            {
                string[] s = selectedTime.Split(':');
                foreach (ListItem i in modalDropDownEndTimeMin.Items)
                {
                    if (i.Text.Trim() == s[1].Trim())
                        i.Selected = true;
                }
            }
        }
        private void GenerateEndHour(int shour, int smin,bool addBlank, string selectedTime = "")
        {
            DateTime selectedDate = DateTime.Parse(txtBoxDate.Text);
            bool isCurrentDate = false;
            int userid = Convert.ToInt32(Session["UserId"]);

            JobTracker jobtracker = new JobTracker();
            TimeSetting timeSetting = new TimeSetting();
            timeSetting = timeSetting.GetTimeSetting();
            TimeSpan selTime = new TimeSpan(shour, smin, 0);
            User user = new User();
            user = user.GetUser(userid, selectedDate);
            TimeSpan cutOffTime = user.GetMyCutOfTime();

            List<TimeSpan> availableTime = new List<TimeSpan>();

            for (int i = shour; i < 25; i++)
            {
                if (i != 24)
                {
                    for (int j = 0; j < 60; j += timeSetting.Interval)
                    {
                        if (selTime < cutOffTime)
                        {
                            if (new TimeSpan(i, j, 0) <= cutOffTime)
                            {
                                availableTime.Add(new TimeSpan(i, j, 0));
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            availableTime.Add(new TimeSpan(i, j, 0));
                        }
                    }
                }
                else //adding 24:00
                {
                    if (selTime < cutOffTime)
                    {
                        if (new TimeSpan(i, 0, 0) <= cutOffTime)
                        {
                            availableTime.Add(new TimeSpan(i, 0, 0));
                        }
                    }
                    else
                    {
                        availableTime.Add(new TimeSpan(i, 0, 0));
                    }
                }
            }

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

            Dictionary<string, string> hours = new Dictionary<string, string>();
            jobtracker = jobtracker.GetNextUsedTime(userid, DateTime.Parse(selectedDate.Year + "-" + selectedDate.Month + "-" + selectedDate.Day + " " + selTime.ToString()), selectedDate);

            TimeSpan stime = jobtracker == null ? new TimeSpan(24,0,0) : Convert.ToDateTime(jobtracker.StartTime).TimeOfDay;
            if (Session["StartTime"] != null)
            {
                TimeSpan ts = TimeSpan.Parse(Session["StartTime"].ToString());
                if (ts == stime)
                {
                    jobtracker = jobtracker.GetNextUsedTime(userid, DateTime.Parse(selectedDate.Year + "-" + selectedDate.Month + "-" + selectedDate.Day + " " + Session["StartTime"].ToString()), selectedDate);
                    stime = jobtracker == null ? new TimeSpan(24, 0, 0) : Convert.ToDateTime(jobtracker.StartTime).TimeOfDay;
                }
            }

            for (int j = 0; j < availableTime.Count; j++)
            {
                if (availableTime[j] > stime)
                {
                    availableTime.RemoveAt(j);
                    --j;
                }
                else if (isCurrentDate == true && DateTime.Now.TimeOfDay < availableTime[j])
                {
                    availableTime.RemoveAt(j);
                    --j;
                }
                else if (selTime >= availableTime[j])
                {
                    availableTime.RemoveAt(j);
                    --j;
                }
            }

            int curtime = 24;
            if (addBlank == true)
            {
                hours.Add("--", "00");
            }

            for (int i = 0; i < availableTime.Count; i++)
            {
                if (curtime != Convert.ToInt32(Math.Floor(availableTime[i].TotalHours)))
                {
                    curtime = Convert.ToInt32(Math.Floor(availableTime[i].TotalHours));
                    hours.Add(curtime > 9 ? curtime.ToString() : "0" + curtime.ToString(), curtime > 9 ? curtime.ToString() : "0" + curtime.ToString());
                }
            }
            modalDropDownEndTimeHour.DataSource = hours;
            modalDropDownEndTimeHour.DataTextField = "Key";
            modalDropDownEndTimeHour.DataValueField = "Value";
            modalDropDownEndTimeHour.DataBind();

            if (selectedTime.Trim() != "")
            {
                string seletime = "";
                if (selectedTime.Length > 10)
                {
                    DateTime selEndDate = Convert.ToDateTime(selectedTime);

                    if (selectedDate.Date < selEndDate.Date)
                        seletime = "24:00:00";
                    else
                        seletime = selEndDate.TimeOfDay.ToString();
                }
                else
                {
                    seletime = selectedTime;
                }
                string[] s = seletime.Split(':');
                foreach (ListItem i in modalDropDownEndTimeHour.Items)
                {
                    if (i.Text.Trim() == s[0].Trim())
                        i.Selected = true;
                }
            }
        }
        private void RunBreakButtonActionForShifting()
        {
            int userid = Convert.ToInt32(Session["UserId"]);
            JobType jobtype = new JobType();
            jobtype = jobtype.GetDefaultBreak();
            DateTime selectedDate = DateTime.Parse(txtBoxDate.Text);
            JobTracker jobtracker = new JobTracker();
            int jobtrackid = 0;
            bool noError = true;
            User user = new User();
            user = user.GetUser(userid, selectedDate);
            TimeSpan cutOffTime = user.GetMyCutOfTime();

            if (BtnBreak.Text == "Take a break")
            {
                DateTime startTime = new DateTime();
                if (gridJobTrack.Rows.Count > 0)
                {
                    jobtrackid = Convert.ToInt32(((Label)gridJobTrack.Rows[gridJobTrack.Rows.Count - 1].FindControl("labelJobTrackId")).Text);
                    jobtracker = jobtracker.GetJobTracker(jobtrackid, false);
                    JobType jtype = jobtype.GetJobType(Convert.ToInt32(jobtracker.JobTypeId));
                    if (Convert.ToDateTime(jobtracker.StartTime).TimeOfDay < cutOffTime && DateTime.Now.TimeOfDay >= cutOffTime)
                    {
                        if (jobtracker.EndTime == null)
                        {
                            noError = false;
                            TimeSetting tsetting = new TimeSetting();
                            tsetting = tsetting.GetTimeSetting();
                            panelAlertHeader2.CssClass = "modalAlertHeader";
                            alertModalBtnOK2.CssClass = "buttonalert";
                            labelAlertHeader2.Text = "Error";
                            labelAlertMessage2.Text = "Please close all task first.";
                        }
                        else
                        {
                            GenerateStartHour();
                            GenerateStartMin(Convert.ToInt32(modalDropDownStartTimeHour.SelectedItem.Value));
                            startTime = DateTime.Parse(selectedDate.Year + "-" + selectedDate.Month + "-" + selectedDate.Day + " " + modalDropDownStartTimeHour.SelectedValue + ":" + modalDropDownStartTimeMin.SelectedValue + ":00");
                        }
                    }
                    else
                    {
                        if (jobtracker.EndTime == null)
                        {
                            DateTime stime = Convert.ToDateTime(jobtracker.StartTime);
                            GenerateEndHour(stime.Hour, stime.Minute, true);
                            GenerateEndMin(stime.Hour, stime.Minute, "--");
                            modalDropDownEndTimeHour.SelectedIndex = (modalDropDownEndTimeHour.Items.Count - 1);
                            GenerateEndMin(stime.Hour, stime.Minute, modalDropDownEndTimeHour.SelectedItem.Text.Trim(), modalDropDownEndTimeMin.Items.Count == 0 ? "" : modalDropDownEndTimeHour.Items[modalDropDownEndTimeHour.Items.Count - 1].Text.Trim() + ":" + modalDropDownEndTimeMin.SelectedItem.Text.Trim());
                            if (modalDropDownEndTimeHour.Items[modalDropDownEndTimeHour.Items.Count - 1].Text != "--")
                            {
                                if (modalDropDownEndTimeHour.SelectedValue == "24") //end date selected is 12:00 AM next day
                                {
                                    jobtracker.EndTime = DateTime.Parse((selectedDate.AddDays(1)).Year + "-" + (selectedDate.AddDays(1)).Month + "-" + (selectedDate.AddDays(1)).Day + " 00:00:00");
                                }
                                else
                                {
                                    jobtracker.EndTime = DateTime.Parse(selectedDate.Year + "-" + selectedDate.Month + "-" + selectedDate.Day + " " + modalDropDownEndTimeHour.Items[modalDropDownEndTimeHour.Items.Count - 1].Value + ":" + modalDropDownEndTimeMin.Items[modalDropDownEndTimeMin.Items.Count - 1].Value + ":00");
                                }
                                jobtracker.Status = "Approved";
                                jobtracker.ApprovedBy = userid;
                                jobtracker.ActionRequest = "Update";
                                jobtracker.Update(jobtracker);
                                startTime = Convert.ToDateTime(jobtracker.EndTime);
                                InitializeGrid();
                            }
                            else
                            {
                                noError = false;
                                TimeSetting tsetting = new TimeSetting();
                                tsetting = tsetting.GetTimeSetting();
                                panelAlertHeader2.CssClass = "modalAlertHeader";
                                alertModalBtnOK2.CssClass = "buttonalert";
                                labelAlertHeader2.Text = "Error";
                                labelAlertMessage2.Text = "Cannot take a break without spending at least " + tsetting + " minutes on the last task.";
                            }
                        }
                        else
                        {
                            startTime = Convert.ToDateTime(jobtracker.EndTime);
                        }
                    }
                }
                else
                {
                    GenerateStartHour();
                    GenerateStartMin(Convert.ToInt32(modalDropDownStartTimeHour.SelectedItem.Value));
                    startTime = DateTime.Parse(selectedDate.Year + "-" + selectedDate.Month + "-" + selectedDate.Day + " " + modalDropDownStartTimeHour.SelectedValue + ":" + modalDropDownStartTimeMin.SelectedValue + ":00");
                }
                if (noError)
                {
                    jobtracker = new JobTracker();
                    jobtracker.StartTime = startTime;
                    jobtracker.Status = "Pending";
                    jobtracker.JobStatus = "";
                    jobtracker.ApprovedBy = userid;
                    jobtracker.ActionRequest = "Add";
                    jobtracker.UserId = userid;
                    jobtracker.CreateDate = DateTime.Now;
                    jobtracker.CreatedBy = userid;
                    jobtracker.LastUpdateDate = DateTime.Now;
                    jobtracker.LastUpdatedBy = userid;
                    jobtracker.Description = jobtype.Description;
                    jobtracker.JobTypeId = jobtype.Id;
                    jobtracker.ScheduleDate = selectedDate;
                    jobtracker.Remarks = "";
                    jobtracker.Insert(jobtracker);
                    InitializeGrid();
                }
                else
                {
                    programmaticAlertModalPopup2.Show();
                }
            }
            else if (BtnBreak.Text == "End break")
            {
                jobtrackid = Convert.ToInt32(((Label)gridJobTrack.Rows[gridJobTrack.Rows.Count - 1].FindControl("labelJobTrackId")).Text);
                jobtracker = jobtracker.GetJobTracker(jobtrackid, false);
                jobtracker.LastUpdateDate = DateTime.Now;
                jobtracker.LastUpdatedBy = userid;
                if (jobtracker.JobTypeId == jobtype.Id)
                {
                    DateTime stime = Convert.ToDateTime(jobtracker.StartTime);
                    GenerateEndHour(stime.Hour, stime.Minute, true);
                    GenerateEndMin(stime.Hour, stime.Minute, "--");
                    modalDropDownEndTimeHour.SelectedIndex = (modalDropDownEndTimeHour.Items.Count - 1);
                    GenerateEndMin(stime.Hour, stime.Minute, modalDropDownEndTimeHour.SelectedItem.Text.Trim(), modalDropDownEndTimeMin.Items.Count == 0 ? "" : modalDropDownEndTimeHour.Items[modalDropDownEndTimeHour.Items.Count - 1].Text.Trim() + ":" + modalDropDownEndTimeMin.SelectedItem.Text.Trim());
                    if (modalDropDownEndTimeHour.Items[modalDropDownEndTimeHour.Items.Count - 1].Text != "--")
                    {
                        if (modalDropDownEndTimeHour.SelectedValue == "24") //end date selected is 12:00 AM next day
                        {
                            jobtracker.EndTime = DateTime.Parse((selectedDate.AddDays(1)).Year + "-" + (selectedDate.AddDays(1)).Month + "-" + (selectedDate.AddDays(1)).Day + " 00:00:00");
                        }
                        else
                        {
                            jobtracker.EndTime = DateTime.Parse(selectedDate.Year + "-" + selectedDate.Month + "-" + selectedDate.Day + " " + modalDropDownEndTimeHour.Items[modalDropDownEndTimeHour.Items.Count - 1].Value + ":" + modalDropDownEndTimeMin.Items[modalDropDownEndTimeMin.Items.Count - 1].Value + ":00");
                        }
                        jobtracker.Status = "Approved";
                        jobtracker.ApprovedBy = userid;
                        jobtracker.ActionRequest = "Update";
                        jobtracker.Update(jobtracker);
                        InitializeGrid();
                    }
                    else//delete break if start time and end time are the same
                    {
                        jobtracker.Delete(jobtracker.Id);
                        InitializeGrid();
                    }
                }
                if (gridJobTrack.Rows.Count > 1) // continue the last created job
                {
                    for (int i = gridJobTrack.Rows.Count - 1; i >= 0; i--)
                    {
                        jobtrackid = Convert.ToInt32(((Label)gridJobTrack.Rows[i].FindControl("labelJobTrackId")).Text);
                        JobTracker jtracker = jobtracker.GetJobTracker(jobtrackid, false);
                        JobType jtype = jobtype.GetJobType(Convert.ToInt32(jtracker.JobTypeId));
                        if (jtype.ComputeTime == true)
                        {
                            jtracker.StartTime = jobtracker.EndTime;
                            jtracker.UserId = userid;
                            jtracker.CreateDate = DateTime.Now;
                            jtracker.CreatedBy = userid;
                            jtracker.LastUpdateDate = DateTime.Now;
                            jtracker.LastUpdatedBy = userid;
                            jtracker.EndTime = null;
                            jtracker.ActionRequest = "Add";
                            jtracker.Status = "Approved";
                            if(Convert.ToDateTime(jobtracker.EndTime).TimeOfDay != new TimeSpan(0,0,0)) //only insert if endtime is not equal to 24:00 (next day)
                                jtracker.Insert(jtracker);
                            InitializeGrid();
                            break;
                        }
                    }
                }
            }
        }
        private void GenerateStartMin(int hour, string selectedTime = "")
        {
            DateTime selectedDate = DateTime.Parse(txtBoxDate.Text);
            int userid = Convert.ToInt32(Session["UserId"]);
            bool isCurrentDate = false;

            JobTracker jobtracker = new JobTracker();
            TimeSetting timeSetting = new TimeSetting();
            timeSetting = timeSetting.GetTimeSetting();
            TimeSpan startTime = new TimeSpan();
            List<TimeSpan> availableTime = new List<TimeSpan>();
            List<String> deleteMarker = new List<string>();
            User user = new User();
            user = user.GetUser(userid, selectedDate);
            TimeSpan cutOffTime = user.GetMyCutOfTime();
            bool isComplete = Convert.ToBoolean(Session["Is2ndPartComplete"]);
            if (Session["StartTime"] != null)
            {
                string[] s = Session["StartTime"].ToString().Split(':');
                startTime = new TimeSpan(Convert.ToInt32(s[0]), Convert.ToInt32(s[1]), 0);
            }

            for (int j = 0; j < 60; j += timeSetting.Interval)
            {
                if (isComplete == false && hour == cutOffTime.Hours)
                {
                    if (new TimeSpan(hour, j, 0) < cutOffTime)
                    {
                        availableTime.Add(new TimeSpan(hour, j, 0));
                        deleteMarker.Add(availableTime.LastOrDefault().ToString());
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    availableTime.Add(new TimeSpan(hour, j, 0));
                    deleteMarker.Add(availableTime.LastOrDefault().ToString());
                }
            }

            if (selectedDate.CompareTo(DateTime.Today) == 0)
            {
                isCurrentDate = true;
            }
            Dictionary<string, string> mins = 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])
                    {
                        deleteMarker[deleteMarker.LastIndexOf(availableTime[j].ToString())] = "deleted";
                        availableTime.RemoveAt(j);
                        --j;
                    }
                    else if (usedTime[i].EndTime != null)
                    {
                        TimeSpan stime = Convert.ToDateTime(usedTime[i].StartTime).TimeOfDay;
                        TimeSpan 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
                            {
                                deleteMarker[deleteMarker.LastIndexOf(availableTime[j].ToString())] = "deleted";
                                availableTime.RemoveAt(j);
                                --j;
                            }
                        }
                    }
                }
            }

            if (usedTime.Count < 1)
            {
                for (int j = 0; j < availableTime.Count; j++)
                {
                    if (isCurrentDate == true && DateTime.Now.TimeOfDay < availableTime[j])
                    {
                        deleteMarker[deleteMarker.LastIndexOf(availableTime[j].ToString())] = "deleted";
                        availableTime.RemoveAt(j);
                        --j;
                    }
                }
            }

            //make sure that there is no gap between time
            if (selectedTime.Trim() == "")
            {
                if (Convert.ToInt32(modalDropDownStartTimeHour.Items[modalDropDownStartTimeHour.Items.Count - 1].Text) == hour && availableTime.Count > 1 && usedTime.Count > 0)
                {
                    TimeSpan etime = new TimeSpan(Convert.ToDateTime(usedTime[0].EndTime).Hour, Convert.ToDateTime(usedTime[0].EndTime).Minute, 0);
                    int index = -1;
                    int markerindex = -1;
                    bool deleteDetected = false;
                    for (int i = 0; i < deleteMarker.Count; i++)
                    {
                        if (deleteDetected && deleteMarker[i] != "deleted")
                        {
                            markerindex = i;
                            break;
                        }
                        if (deleteMarker[i] == "deleted")
                            deleteDetected = true;
                    }
                    if (markerindex > -1)
                    {
                        for (int i = 0; i < availableTime.Count; i++)
                        {
                            if (availableTime[i].ToString() == deleteMarker[markerindex])
                            {
                                index = i;
                                break;
                            }
                        }
                        if (index > -1 && (index + 1) < availableTime.Count)
                        {
                            availableTime.RemoveRange(index + 1, availableTime.Count - (index + 1));
                            //if (index > 0)
                            //    availableTime.RemoveRange(0, index);
                        }
                    }
                }
            }

            if (!isCurrentDate)
            {
                if (availableTime.Count > 0 && hour == Convert.ToInt32(modalDropDownStartTimeHour.Items[modalDropDownStartTimeHour.Items.Count - 1].Value))
                {

                    GenerateEndHour(availableTime[availableTime.Count - 1].Hours, availableTime[availableTime.Count - 1].Minutes, false);
                    if (modalDropDownEndTimeHour.Items.Count == 0)
                        availableTime.RemoveAt(availableTime.Count - 1);
                }
            }

            int curtime = 60;

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

            if (selectedTime.Trim() != "")
            {
                string[] s = selectedTime.Split(':');
                foreach (ListItem i in modalDropDownStartTimeMin.Items)
                {
                    if (i.Text.Trim() == s[1].Trim())
                        i.Selected = true;
                }
            }
            else
            {
                TimeSpan s = DateTime.Now.TimeOfDay;
                double gap = 100000;
                int index = 0;
                for (int i = 0; i < modalDropDownStartTimeMin.Items.Count; i++)
                {

                    System.TimeSpan sp = new System.TimeSpan(Convert.ToInt32(modalDropDownStartTimeHour.SelectedItem.Value), Convert.ToInt32(modalDropDownStartTimeMin.Items[i].Value), 0);
                    double cgap = Math.Abs((sp.Subtract(s)).TotalMinutes);
                    if (cgap == 0)
                    {
                        gap = cgap;
                        index = i;
                        break;
                    }
                    else if (cgap < gap)
                    {
                        gap = cgap;
                        index = i;
                    }
                }
                modalDropDownStartTimeMin.Items[index].Selected = true;
            }
        }
        //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;
            }
        }