protected void gridViewUser_Command(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                GetMyAccessRights();
                modalLabelError.Text = "";
                modalLabelError.Visible = false;
                int index = Convert.ToInt32(e.CommandArgument);
                int userid = Convert.ToInt32(((Label)gridViewUser.Rows[index].FindControl("labelUserId")).Text);
                User user = new User();
                user = user.GetUser(userid);
                InitializeModalDropDownDepartment(user.DepartmentId.ToString());
                InitializeModalDropDownRole(user.RoleId.ToString());
                InitializeModalDropDownStatus(user.Status);
                modalBtnSubmit.Visible = Convert.ToBoolean(myAccessRights.CanUpdate);
                modalLabelUserId.Text = userid.ToString();
                modalBtnSubmit.CommandArgument = "Update";
                modalTxtBoxEmployeeNo.Text = user.EmployeeNumber.ToString();
                modalTxtBoxFirstname.Text = user.Firstname.Trim();
                modalTxtBoxLastname.Text = user.Lastname;
                modalTxtBoxUsername.Text = user.Username;
                modalTxtBoxPassword.Attributes.Add("value", user.Password);
                modalTxtBoxPhone.Text = user.Phone;
                modalTxtBoxMobile.Text = user.Mobile;
                modalTxtBoxEmail.Text = user.Email;
                modalTxtBoxFax.Text = user.Fax;
                //InitializeModalRadBtnShift(user.Shift);

                modalTxtBoxStartTime.Text = user.startTime;
                modalTxtBoxEndTime.Text = user.endTime;
                modalTxtBoxSalary.Text = user.currentSalary.ToString();
                modalTxtBoxBreakTimeMin.Text = user.currentMinBreak.ToString();

                InitializeModalDropDownOffDay(user.currentOffDay.ToString());
                InitializeModalDropDownSpecialOffDay(user.currentSpecialOffDay.ToString());
                modalChckBoxNoOTPay.Checked = user.noOTpay;
                modalChckBoxOfficeWorker.Checked =  user.isOfficeWorker;

                TimeSpan cutOfTime = user.GetMyCutOfTime();

                modalChkBoxUpdateRate.Checked = false;
                cpeSalaryRate.ClientState = "true";
                ToggleReqField(false);

                this.programmaticModalPopup.Show();
            }
        }
        //Use to get newly created jobtracker
        public JobTracker GetJobTracker(int createdby, int lastupdatedby, DateTime starttime, int jobtypeid, string actionrequest, string status, bool computetime)
        {
            TimeTrackerEntities db = new TimeTrackerEntities();

            var data = (from j in db.T_JobTracker
                        where j.CreatedBy == createdby
                        && j.LastUpdatedBy == lastupdatedby
                        && j.StartTime == starttime
                        && j.JobTypeId == jobtypeid
                        && j.ActionRequest == actionrequest
                        && j.Status == status
                        select new JobTracker()
                        {
                            Id = j.Id,
                            UserId = j.UserId,
                            StartTime = j.StartTime,
                            EndTime = j.EndTime,
                            Description = j.Description,
                            JobTypeId = j.JobTypeId,
                            JobIdNumber = j.JobIdNumber,
                            jobtype = j.M_JobType.Description,
                            Remarks = j.Remarks,
                            ApprovedBy = j.ApprovedBy,
                            CreateDate = j.CreateDate,
                            LastUpdateDate = j.LastUpdateDate,
                            CreatedBy = j.CreatedBy,
                            LastUpdatedBy = j.LastUpdatedBy,
                            Status = j.Status,
                            SupervisorRemarks = j.SupervisorRemarks,
                            ActionRequest = j.ActionRequest,
                            ScheduleDate = j.ScheduleDate,
                            SWNo = j.SWNo,
                            HWNo = j.HWNo,
                            JobStatus = j.JobStatus,
                            fullname = j.M_User.Firstname + " " + j.M_User.Lastname,
                            Customer = j.Customer,
                            EvalNo = j.EvalNo
                        }).FirstOrDefault();

            db.Dispose();

            //if (data.JobIdNumber != null && data.JobIdNumber != "")
            //{
            //    GetCustomer(data);
            //}
            if (computetime == true)
            {
                User user = new User();
                user = user.GetUser(Convert.ToInt32(data.UserId), Convert.ToDateTime(data.StartTime));
                if (data.EndTime != null)
                {
                    TimeSpan stime = Convert.ToDateTime(data.StartTime).TimeOfDay;
                    TimeSpan etime = Convert.ToDateTime(data.EndTime).TimeOfDay;
                    double nmins = 0;
                    double omins = 0;
                    otcost = 0;
                    normalcost = 0;

                    double time = Convert.ToDateTime(data.EndTime).Subtract(Convert.ToDateTime(data.StartTime)).TotalMinutes;
                    if (user.shifting == false)
                    {
                        if (TimeSpan.Parse(user.endTime) <= stime) //entry is OT
                        {
                            omins = time;
                        }
                        else if ((etime == new TimeSpan(0, 0, 0) ? new TimeSpan(1, 0, 0, 0) : etime) > TimeSpan.Parse(user.endTime)) //entry is from normal time to OT
                        {
                            nmins = TimeSpan.Parse(user.endTime).Subtract(stime).TotalMinutes;
                            omins = (etime == new TimeSpan(0, 0, 0) ? new TimeSpan(1, 0, 0, 0) : etime).Subtract(TimeSpan.Parse(user.endTime)).TotalMinutes;
                        }
                        else //entry is within normal time
                        {
                            nmins = time;
                        }
                    }
                    else
                    {
                        TimeSpan cutoff = user.GetMyCutOfTime();
                        if (stime >= cutoff)
                        {
                            nmins = time;
                        }
                        else
                        {
                            if (TimeSpan.Parse(user.endTime) <= stime) //entry is OT
                            {
                                omins = time;
                            }
                            else if ((etime == new TimeSpan(0, 0, 0) ? new TimeSpan(1, 0, 0, 0) : etime) > TimeSpan.Parse(user.endTime))
                            {
                                nmins = TimeSpan.Parse(user.endTime).Subtract(stime).TotalMinutes;
                                omins = (etime == new TimeSpan(0, 0, 0) ? new TimeSpan(1, 0, 0, 0) : etime).Subtract(TimeSpan.Parse(user.endTime)).TotalMinutes;
                            }
                        }
                    }
                    double hr = Math.Truncate(time / 60);
                    double min = time % 60;
                    data.totalhours = hr == 0 && min == 0 ? "0 min" : (hr > 0 ? hr > 1 ? hr + " hrs" : hr + " hr" : "") + (hr > 0 && min > 0 ? ", " : "") + (min > 0 ? min > 1 ? min + " mins" : min + " min" : "");

                    hr = Math.Truncate(nmins / 60);
                    min = nmins % 60;
                    data.normalhours = hr == 0 && min == 0 ? "0 min" : (hr > 0 ? hr > 1 ? hr + " hrs" : hr + " hr" : "") + (hr > 0 && min > 0 ? ", " : "") + (min > 0 ? min > 1 ? min + " mins" : min + " min" : "");

                    hr = Math.Truncate(omins / 60);
                    min = omins % 60;
                    data.othours = hr == 0 && min == 0 ? "0 min" : (hr > 0 ? hr > 1 ? hr + " hrs" : hr + " hr" : "") + (hr > 0 && min > 0 ? ", " : "") + (min > 0 ? min > 1 ? min + " mins" : min + " min" : "");
                }
            }

            return data;
        }
        private bool HasTimeGap(int userid, DateTime selecteddate)
        {
            bool result = false;

            var data = GetJobTrackerListWithEndTimeAndNotRejected(userid, selecteddate);

            User user = new User();
            user = user.GetUser(userid,selecteddate);
            if (TimeSpan.Parse(user.startTime) > TimeSpan.Parse(user.endTime)) //for shifting hours check two time (12 midnight - cutofftime and cutooftime to 12 midnight)
            {
                int loop = 0;
                TimeSpan cutOfTime = user.GetMyCutOfTime();
                DateTime stime1 = new DateTime();
                stime1 = DateTime.Parse(selecteddate.Year + "-" + selecteddate.Month + "-" + selecteddate.Day + " 00:00:00");
                DateTime stime2 = new DateTime();
                for (int i = 0; i < data.Count; i++)
                {
                    if (Convert.ToDateTime(data[i].StartTime).TimeOfDay < cutOfTime)
                    {
                        if (stime1 != data[i].StartTime)
                        {
                            result = true;
                            break;
                        }
                        else
                        {
                            stime1 = Convert.ToDateTime(data[i].EndTime);
                        }
                    }
                    else
                    {
                        if (loop == 0)
                        {
                            stime2 = Convert.ToDateTime(data[i].EndTime);
                        }
                        else if (stime2 != data[i].StartTime)
                        {
                            result = true;
                            break;
                        }
                        else
                        {
                            stime2 = Convert.ToDateTime(data[i].EndTime);
                        }
                    }
                }
            }
            else
            {
                DateTime sTime = new DateTime();
                for (int i = 0; i < data.Count; i++)
                {
                    if (i == 0)
                    {
                        sTime = Convert.ToDateTime(data[i].EndTime);
                    }
                    else if (sTime != data[i].StartTime)
                    {
                        result = true;
                        break;
                    }
                    else
                    {
                        sTime = Convert.ToDateTime(data[i].EndTime);
                    }
                }
            }

            return result;
        }
        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 ComputeHours()
        {
            User user = new User();
            user = user.GetUser(Convert.ToInt32(UserId), Convert.ToDateTime(StartTime));
            if (EndTime != null)
            {
                DateTime startdatetime = Convert.ToDateTime(StartTime);
                TimeSpan stime = startdatetime.TimeOfDay;
                TimeSpan etime = Convert.ToDateTime(EndTime).TimeOfDay;
                int curday = (int)startdatetime.DayOfWeek;
                Holiday holiday = new Holiday();

                otcost = 0;
                normalcost = 0;

                double time = Convert.ToDateTime(EndTime).Subtract(Convert.ToDateTime(StartTime)).TotalMinutes;
                if (user.shifting == false)
                {
                    if (user.usePattern == true)
                    {
                        string[] pattern = user.offPattern.Split(',');
                        int patterndays = 0;
                        List<bool> isOffdays = new List<bool>();
                        for (int i = 0; i < pattern.Length; i++)
                        {
                            patterndays += Convert.ToInt32(pattern[i]);
                            for (int j = 0; j < Convert.ToInt32(pattern[i]); j++)
                            {
                                if (i % 2 == 0)
                                    isOffdays.Add(true);
                                else
                                    isOffdays.Add(false);
                            }
                        }
                        int offdayindex = 0;
                        offdayindex = Convert.ToInt32(Math.Floor((startdatetime.Date - user.patternStartDate.Date).TotalDays)) % patterndays;
                        if (TimeSpan.Parse(user.endTime) <= stime || holiday.IsHoliday(startdatetime.Date) || isOffdays[offdayindex] == true) //entry is OT
                        {
                            otmins = time;
                        }
                        else if ((etime == new TimeSpan(0, 0, 0) ? new TimeSpan(1, 0, 0, 0) : etime) > TimeSpan.Parse(user.endTime)) //entry is from normal time to OT
                        {
                            normalmins = TimeSpan.Parse(user.endTime).Subtract(stime).TotalMinutes;
                            otmins = (etime == new TimeSpan(0, 0, 0) ? new TimeSpan(1, 0, 0, 0) : etime).Subtract(TimeSpan.Parse(user.endTime)).TotalMinutes;
                        }
                        else //entry is within normal time
                        {
                            normalmins = time;
                        }
                    }
                    else
                    {
                        if (TimeSpan.Parse(user.endTime) <= stime || holiday.IsHoliday(startdatetime.Date) || curday == user.currentOffDay || curday == user.currentSpecialOffDay || curday == user.currentOptOffDay1 || curday == user.currentOptOffDay2 || curday == user.currentOptOffDay3 || curday == user.currentOptOffDay4) //entry is OT
                        {
                            otmins = time;
                        }
                        else if ((etime == new TimeSpan(0, 0, 0) ? new TimeSpan(1, 0, 0, 0) : etime) > TimeSpan.Parse(user.endTime)) //entry is from normal time to OT
                        {
                            normalmins = TimeSpan.Parse(user.endTime).Subtract(stime).TotalMinutes;
                            otmins = (etime == new TimeSpan(0, 0, 0) ? new TimeSpan(1, 0, 0, 0) : etime).Subtract(TimeSpan.Parse(user.endTime)).TotalMinutes;
                        }
                        else //entry is within normal time
                        {
                            normalmins = time;
                        }
                    }
                }
                else
                {
                    TimeSpan cutoff = user.GetMyCutOfTime();
                    if (user.usePattern == true)
                    {
                        string[] pattern = user.offPattern.Split(',');
                        int patterndays = 0;
                        List<bool> isOffdays = new List<bool>();
                        for (int i = 0; i < pattern.Length; i++)
                        {
                            patterndays += Convert.ToInt32(pattern[i]);
                            for (int j = 0; j < Convert.ToInt32(pattern[i]); j++)
                            {
                                if (i % 2 == 0)
                                    isOffdays.Add(true);
                                else
                                    isOffdays.Add(false);
                            }
                        }
                        int offdayindex = 0;

                        if (stime >= cutoff)
                        {
                            offdayindex = Convert.ToInt32(Math.Floor((startdatetime.Date - user.patternStartDate.Date).TotalDays)) % patterndays;
                            if (holiday.IsHoliday(startdatetime.Date) || isOffdays[offdayindex] == true)
                                otmins = time;
                            else
                                normalmins = time;
                        }
                        else
                        {
                            offdayindex = Convert.ToInt32(Math.Floor((startdatetime.AddDays(-1).Date - user.patternStartDate.Date).TotalDays)) % patterndays;
                            if (TimeSpan.Parse(user.endTime) <= stime || holiday.IsHoliday(startdatetime.AddDays(-1).Date) || isOffdays[offdayindex] == true)
                            {
                                otmins = time;
                            }
                            else if ((etime == new TimeSpan(0, 0, 0) ? new TimeSpan(1, 0, 0, 0) : etime) > TimeSpan.Parse(user.endTime))
                            {
                                normalmins = TimeSpan.Parse(user.endTime).Subtract(stime).TotalMinutes;
                                otmins = (etime == new TimeSpan(0, 0, 0) ? new TimeSpan(1, 0, 0, 0) : etime).Subtract(TimeSpan.Parse(user.endTime)).TotalMinutes;
                            }
                        }
                    }
                    else
                    {
                        if (stime >= cutoff)
                        {
                            if (holiday.IsHoliday(startdatetime.Date) || curday == user.currentOffDay || curday == user.currentSpecialOffDay || curday == user.currentOptOffDay1 || curday == user.currentOptOffDay2 || curday == user.currentOptOffDay3 || curday == user.currentOptOffDay4)
                                otmins = time;
                            else
                                normalmins = time;
                        }
                        else
                        {
                            curday = (int)startdatetime.AddDays(-1).DayOfWeek;
                            if (TimeSpan.Parse(user.endTime) <= stime || holiday.IsHoliday(startdatetime.AddDays(-1).Date) || curday == user.currentOffDay || curday == user.currentSpecialOffDay || curday == user.currentOptOffDay1 || curday == user.currentOptOffDay2 || curday == user.currentOptOffDay3 || curday == user.currentOptOffDay4) //entry is OT
                            {
                                otmins = time;
                            }
                            else if ((etime == new TimeSpan(0, 0, 0) ? new TimeSpan(1, 0, 0, 0) : etime) > TimeSpan.Parse(user.endTime))
                            {
                                normalmins = TimeSpan.Parse(user.endTime).Subtract(stime).TotalMinutes;
                                otmins = (etime == new TimeSpan(0, 0, 0) ? new TimeSpan(1, 0, 0, 0) : etime).Subtract(TimeSpan.Parse(user.endTime)).TotalMinutes;
                            }
                        }
                    }
                }
                double hr = Math.Truncate(time / 60);
                double min = time % 60;
                totalhours = hr == 0 && min == 0 ? "0 min" : (hr > 0 ? hr > 1 ? hr + " hrs" : hr + " hr" : "") + (hr > 0 && min > 0 ? ", " : "") + (min > 0 ? min > 1 ? min + " mins" : min + " min" : "");

                hr = Math.Truncate(normalmins / 60);
                min = normalmins % 60;
                normalhours = hr == 0 && min == 0 ? "0 min" : (hr > 0 ? hr > 1 ? hr + " hrs" : hr + " hr" : "") + (hr > 0 && min > 0 ? ", " : "") + (min > 0 ? min > 1 ? min + " mins" : min + " min" : "");

                hr = Math.Truncate(otmins / 60);
                min = otmins % 60;
                othours = hr == 0 && min == 0 ? "0 min" : (hr > 0 ? hr > 1 ? hr + " hrs" : hr + " hr" : "") + (hr > 0 && min > 0 ? ", " : "") + (min > 0 ? min > 1 ? min + " mins" : min + " min" : "");

            }
        }
        private void ComputeCost()
        {
            if (EndTime != null)
            {
                User user = new User();
                user = user.GetUser(Convert.ToInt32(UserId), Convert.ToDateTime(StartTime));
                Holiday holiday = new Holiday();
                OTRateSetting otRateSetting = new OTRateSetting();
                DateTime startdatetime = Convert.ToDateTime(StartTime);
                //int workingdays = holiday.GetWorkingDaysInMonth(user.Id, startdatetime);
                double normalRatePerMin = 0;
                //double normalRatePerMin = Convert.ToDouble((user.currentSalary/workingdays) / user.minsWorkPerDay);
                if (user.minsWorkPerDay < 490)
                    normalRatePerMin = Convert.ToDouble((user.currentSalary * 12) / (2080 * 60));
                else
                    normalRatePerMin = Convert.ToDouble((user.currentSalary * 12) / (2184 * 60));
                normalcost = normalmins * normalRatePerMin;
                otRateSetting = otRateSetting.GetOTRateSettingByDate(startdatetime);

                #region OT COMPUTATION
                otcost = 0;
                if (!user.noOTpay && otRateSetting != null)
                {
                    if (user.currentSalary >= otRateSetting.OTExemptedSalary) //User is Exempted from Salary
                    {
                        otcost = otmins * Convert.ToDouble((otRateSetting.ExemptedSalaryIncentive / 60));
                    }
                    else
                    {
                        if (user.shifting == false)
                        {
                            if (user.usePattern == true)
                            {
                                if (holiday.IsHoliday(startdatetime.Date))
                                {
                                    otcost = otmins * (normalRatePerMin * otRateSetting.OTSpecialRate);
                                }
                                else
                                {
                                    otcost = otmins * (normalRatePerMin * otRateSetting.OTNormalRate);
                                }
                            }
                            else
                            {
                                if (holiday.IsHoliday(startdatetime.Date) || user.isOfficeWorker && user.currentSpecialOffDay == (int)startdatetime.DayOfWeek)
                                {
                                    otcost = otmins * (normalRatePerMin * otRateSetting.OTSpecialRate);
                                }
                                else
                                {
                                    otcost = otmins * (normalRatePerMin * otRateSetting.OTNormalRate);
                                }
                            }
                        }
                        else //For Shifting Hours
                        {
                            TimeSpan stime = startdatetime.TimeOfDay;
                            TimeSpan etime = Convert.ToDateTime(EndTime).TimeOfDay;
                            TimeSpan cutoff = user.GetMyCutOfTime();

                            if (user.usePattern == true)
                            {
                                if (user.offPattern.Trim() != "")
                                {

                                    if (stime >= cutoff)
                                    {
                                        if (holiday.IsHoliday(startdatetime.Date))
                                        {
                                            otcost = otmins * (normalRatePerMin * otRateSetting.OTSpecialRate);
                                        }
                                        else
                                        {
                                            otcost = otmins * (normalRatePerMin * otRateSetting.OTNormalRate);
                                        }
                                    }
                                    else
                                    {
                                        if (holiday.IsHoliday(startdatetime.AddDays(-1).Date))
                                        {
                                            otcost = otmins * (normalRatePerMin * otRateSetting.OTSpecialRate);
                                        }
                                        else
                                        {
                                            otcost = otmins * (normalRatePerMin * otRateSetting.OTNormalRate);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (stime >= cutoff)
                                {
                                    if (holiday.IsHoliday(startdatetime.Date) || (user.isOfficeWorker && user.currentSpecialOffDay == (int)startdatetime.DayOfWeek))
                                    {
                                        otcost = otmins * (normalRatePerMin * otRateSetting.OTSpecialRate);
                                    }
                                    else
                                    {
                                        otcost = otmins * (normalRatePerMin * otRateSetting.OTNormalRate);
                                    }
                                }
                                else
                                {
                                    if (holiday.IsHoliday(startdatetime.AddDays(-1).Date) || user.isOfficeWorker && user.currentSpecialOffDay == (int)startdatetime.AddDays(-1).DayOfWeek)
                                    {
                                        otcost = otmins * (normalRatePerMin * otRateSetting.OTSpecialRate);
                                    }
                                    else
                                    {
                                        otcost = otmins * (normalRatePerMin * otRateSetting.OTNormalRate);
                                    }
                                }
                            }
                        }
                    }
                    normalcost = Math.Round(normalcost, 2, MidpointRounding.AwayFromZero);
                    otcost = Math.Round(otcost, 2, MidpointRounding.AwayFromZero);
                }
                #endregion
            }
        }
        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 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 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;
            }
        }