protected void modalBtnSubmit_Command(object sender, CommandEventArgs e)
        {
            if (modalLabelError.Visible == true)
                this.programmaticModalPopup.Show();
            else
            {
                int userid = Convert.ToInt32(Session["UserId"]);
                User user = new User();
                if (modalLabelUserId.Text.Trim() != "") //Update
                {
                    user = user.GetUser(Convert.ToInt32(modalLabelUserId.Text));
                }

                UserRateSchedule userRateSchedule = new UserRateSchedule();
                UserRateSchedule prevUserRateSchedule = userRateSchedule.GetPreviousUserScheduleRateByUserIdLastUpdateDate(user.Id, DateTime.Today);
                userRateSchedule = userRateSchedule.GetUserScheduleRateByUserIdCreateDate(user.Id, DateTime.Today); //Checks if there is already a schedule date with the same start date

                if (userRateSchedule != null)
                {
                    userRateSchedule.StartTime = modalTxtBoxStartTime.Text.Trim();
                    userRateSchedule.EndTime = modalTxtBoxEndTime.Text.Trim();
                    userRateSchedule.MonthlySalary = userRateSchedule.EncryptSalary(modalTxtBoxSalary.Text.Trim());
                    userRateSchedule.OffDay = Convert.ToInt32(modalDropDownOffDay.SelectedItem.Value);
                    userRateSchedule.SpecialOffDay = Convert.ToInt32(modalDropDownSpecialOffDay.SelectedItem.Value);
                    userRateSchedule.OptionalOffDay1 = Convert.ToInt32(modalDropDownOptionalOffDay1.SelectedItem.Value);
                    userRateSchedule.OptionalOffDay2 = Convert.ToInt32(modalDropDownOptionalOffDay2.SelectedItem.Value);
                    userRateSchedule.OptionalOffDay3 = Convert.ToInt32(modalDropDownOptionalOffDay3.SelectedItem.Value);
                    userRateSchedule.OptionalOffDay4 = Convert.ToInt32(modalDropDownOptionalOffDay4.SelectedItem.Value);
                    userRateSchedule.NoOTPay = modalChckBoxNoOTPay.Checked;
                    userRateSchedule.IsOfficeWorker = modalChckBoxOfficeWorker.Checked;
                    userRateSchedule.MinsBreak = Convert.ToInt32(modalTxtBoxBreakTimeMin.Text);
                    userRateSchedule.StartDate = Convert.ToDateTime(modalTxtBoxStartDate.Text.Trim());
                    userRateSchedule.LastUpdatedDate = DateTime.Today;
                    userRateSchedule.IsCurrentRate = true;
                    userRateSchedule.UsePattern = modalChckBoxUsePattern.Checked;
                    userRateSchedule.OffPattern = modalTxtBoxPattern.Text.Trim();
                    userRateSchedule.PatternStartDate = Convert.ToDateTime(modalTxtBoxPatternStartDate.Text.Trim());
                    userRateSchedule.Update(userRateSchedule);
                }
                else
                {
                    userRateSchedule = new UserRateSchedule();
                    userRateSchedule.StartTime = modalTxtBoxStartTime.Text.Trim();
                    userRateSchedule.EndTime = modalTxtBoxEndTime.Text.Trim();
                    userRateSchedule.UserId = user.Id;
                    userRateSchedule.MonthlySalary = userRateSchedule.EncryptSalary(modalTxtBoxSalary.Text.Trim());
                    userRateSchedule.OffDay = Convert.ToInt32(modalDropDownOffDay.SelectedItem.Value);
                    userRateSchedule.SpecialOffDay = Convert.ToInt32(modalDropDownSpecialOffDay.SelectedItem.Value);
                    userRateSchedule.OptionalOffDay1 = Convert.ToInt32(modalDropDownOptionalOffDay1.SelectedItem.Value);
                    userRateSchedule.OptionalOffDay2 = Convert.ToInt32(modalDropDownOptionalOffDay2.SelectedItem.Value);
                    userRateSchedule.OptionalOffDay3 = Convert.ToInt32(modalDropDownOptionalOffDay3.SelectedItem.Value);
                    userRateSchedule.OptionalOffDay4 = Convert.ToInt32(modalDropDownOptionalOffDay4.SelectedItem.Value);
                    userRateSchedule.NoOTPay = modalChckBoxNoOTPay.Checked;
                    userRateSchedule.IsOfficeWorker = modalChckBoxOfficeWorker.Checked;
                    userRateSchedule.MinsBreak = Convert.ToInt32(modalTxtBoxBreakTimeMin.Text);
                    userRateSchedule.StartDate = Convert.ToDateTime(modalTxtBoxStartDate.Text.Trim());
                    userRateSchedule.IsCurrentRate = true;
                    userRateSchedule.CreatedDate = DateTime.Today;
                    userRateSchedule.LastUpdatedDate = DateTime.Today;
                    userRateSchedule.UsePattern = modalChckBoxUsePattern.Checked;
                    userRateSchedule.OffPattern = modalTxtBoxPattern.Text.Trim();
                    userRateSchedule.PatternStartDate = Convert.ToDateTime(modalTxtBoxPatternStartDate.Text.Trim());
                    userRateSchedule.Insert(userRateSchedule);
                }
                if (prevUserRateSchedule != null)
                {
                    prevUserRateSchedule.EndDate = Convert.ToDateTime(modalTxtBoxStartDate.Text.Trim()+" 23:59:59");
                    prevUserRateSchedule.IsCurrentRate = false;
                    prevUserRateSchedule.LastUpdatedDate = DateTime.Today;

                    prevUserRateSchedule.Update(prevUserRateSchedule);
                }
            }
        }
        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);
                modalBtnSubmit.Visible = Convert.ToBoolean(myAccessRights.CanUpdate);
                modalLabelUserId.Text = userid.ToString();
                modalLabelEmployeeNo.Text = user.EmployeeNumber.ToString();
                modalLabelFirstName.Text = user.Firstname.Trim();
                modalLableLastName.Text = user.Lastname;
                modalTxtBoxStartTime.Text = user.startTime;
                modalTxtBoxEndTime.Text = user.endTime;
                modalTxtBoxSalary.Text = user.currentSalary.ToString();
                modalTxtBoxBreakTimeMin.Text = user.currentMinBreak.ToString();

                InitializeOffDayDropDown(modalDropDownOffDay,user.currentOffDay.ToString());
                InitializeOffDayDropDown(modalDropDownSpecialOffDay, user.currentSpecialOffDay.ToString());
                InitializeOffDayDropDown(modalDropDownOptionalOffDay1,user.currentOptOffDay1.ToString());
                InitializeOffDayDropDown(modalDropDownOptionalOffDay2,user.currentOptOffDay2.ToString());
                InitializeOffDayDropDown(modalDropDownOptionalOffDay3,user.currentOptOffDay3.ToString());
                InitializeOffDayDropDown(modalDropDownOptionalOffDay4,user.currentOptOffDay4.ToString());
                modalChckBoxNoOTPay.Checked = user.noOTpay;
                modalChckBoxOfficeWorker.Checked = user.isOfficeWorker;
                if (user.currentRateCreateDate == DateTime.Today)
                {
                    modalTxtBoxStartDate.Text = user.currentRateStartDate.ToString("dd MMM yyyy");
                    calendarExtenderStartDate.SelectedDate = user.currentRateStartDate;
                }
                else
                {
                    modalTxtBoxStartDate.Text = DateTime.Today.ToString("dd MMM yyyy");
                    calendarExtenderStartDate.SelectedDate = DateTime.Today;
                }
                UserRateSchedule prevUserRateSchedule= new UserRateSchedule();
                prevUserRateSchedule = prevUserRateSchedule.GetPreviousUserScheduleRateByUserIdLastUpdateDate(user.Id, DateTime.Today);
                if (prevUserRateSchedule != null)
                {
                    calendarExtenderStartDate.StartDate = prevUserRateSchedule.StartDate.AddDays(1);
                }
                else
                {
                    calendarExtenderStartDate.StartDate = null;
                }

                modalChckBoxUsePattern.Checked = user.usePattern;
                ToggleReqField(user.usePattern);
                modalTxtBoxPattern.Text = user.offPattern.Trim();
                modalTxtBoxPatternStartDate.Text = user.patternStartDate.ToString("dd MMM yyyy");
                calendarExtenderPatternStartDate.SelectedDate = user.patternStartDate;
                if (modalChckBoxUsePattern.Checked == true)
                {
                    cpeNormalUse.ClientState = "true";
                    cpePatternUse.ClientState = "false";
                }
                else
                {
                    cpePatternUse.ClientState = "true";
                    cpeNormalUse.ClientState = "false";
                }

                this.programmaticModalPopup.Show();
            }
        }
        protected void modalBtnSubmit_Command(object sender, CommandEventArgs e)
        {
            //if (modalChkBoxUpdateRate.Checked == true) //Checks if starttime and endtime is in correct format
            //{
            //    TimeSpan starttime = new TimeSpan();
            //    TimeSpan endtime = new TimeSpan();
            //    if (TimeSpan.TryParse(modalTxtBoxStartTime.Text, out starttime))
            //    {

            //    }
            //    else
            //    {
            //        modalLabelError.Text = "Start Time not in valid format.";
            //        modalLabelError.Visible = true;
            //    }
            //    if (TimeSpan.TryParse(modalTxtBoxEndTime.Text, out endtime))
            //    {

            //    }
            //    else
            //    {
            //        modalLabelError.Text = "End Time not in valid format.";
            //        modalLabelError.Visible = true;
            //    }
            //}
            if (modalLabelError.Visible == true)
                this.programmaticModalPopup.Show();
            else
            {
                int userid = Convert.ToInt32(Session["UserId"]);
                User user = new User();
                if (modalLabelUserId.Text.Trim() != "") //Update
                {
                    user = user.GetUser(Convert.ToInt32(modalLabelUserId.Text));
                }
                user.Firstname = modalTxtBoxFirstname.Text.Trim();
                user.Lastname = modalTxtBoxLastname.Text.Trim();
                user.RoleId = Convert.ToInt32(modalDropDownRole.SelectedItem.Value);
                user.DepartmentId = Convert.ToInt32(modalDropDownDepartment.SelectedItem.Value);
                user.Username = modalTxtBoxUsername.Text.Trim();
                user.Password = modalTxtBoxPassword.Text.Trim();
                user.Email = modalTxtBoxEmail.Text.Trim();
                user.Phone = modalTxtBoxPhone.Text.Trim();
                user.Fax = modalTxtBoxFax.Text.Trim();
                user.Mobile = modalTxtBoxMobile.Text.Trim();
                user.LastUpdatedBy = userid;
                user.LastUpdateDate = DateTime.Now;
                user.EmployeeNumber = Convert.ToInt32(modalTxtBoxEmployeeNo.Text);
                user.Status = modalDropDownStatus.SelectedItem.Value;
                //user.Shift = modalRadBtnListShift.SelectedItem.Value.Trim();
                if (e.CommandArgument.ToString().Trim() == "Add")
                {
                    user.CreateDate = DateTime.Now;
                    user.CreatedBy = userid;
                    user.Insert(user);
                    user = user.GetLastInsertedUser(); //assign newly created user to variable user
                }
                else if (e.CommandArgument.ToString().Trim() == "Update")
                {
                    user.Update(user);
                }
                if (modalChkBoxUpdateRate.Checked == true) //User intends to update salary rate
                {
                    UserRateSchedule userRateSchedule = new UserRateSchedule();
                    userRateSchedule = userRateSchedule.GetUserScheduleRateByUserIdStartDate(user.Id, DateTime.Today); //Checks if there is already a schedule date with the same start date
                    if (userRateSchedule != null)
                    {
                        userRateSchedule.StartTime = modalTxtBoxStartTime.Text.Trim();
                        userRateSchedule.EndTime = modalTxtBoxEndTime.Text.Trim();
                        userRateSchedule.OffDay = Convert.ToInt32(modalDropDownOffDay.SelectedItem.Value);
                        userRateSchedule.SpecialOffDay = Convert.ToInt32(modalDropDownSpecialOffDay.SelectedItem.Value);
                        userRateSchedule.NoOTPay = modalChckBoxNoOTPay.Checked;
                        userRateSchedule.Update(userRateSchedule);
                        userRateSchedule.IsOfficeWorker = modalChckBoxOfficeWorker.Checked;
                        userRateSchedule.MinsBreak = Convert.ToInt32(modalTxtBoxBreakTimeMin.Text);
                    }
                    else
                    {
                        userRateSchedule = new UserRateSchedule();
                        UserRateSchedule prevUserRateSchedule = userRateSchedule.GetPreviousUserScheduleRateByUserIdLastUpdateDate(user.Id, DateTime.Today);
                        userRateSchedule.StartTime = modalTxtBoxStartTime.Text.Trim();
                        userRateSchedule.EndTime = modalTxtBoxEndTime.Text.Trim();
                        userRateSchedule.UserId = user.Id;
                        userRateSchedule.OffDay = Convert.ToInt32(modalDropDownOffDay.SelectedItem.Value);
                        userRateSchedule.SpecialOffDay = Convert.ToInt32(modalDropDownSpecialOffDay.SelectedItem.Value);
                        userRateSchedule.NoOTPay = modalChckBoxNoOTPay.Checked;
                        userRateSchedule.IsOfficeWorker = modalChckBoxOfficeWorker.Checked;
                        userRateSchedule.MinsBreak = Convert.ToInt32(modalTxtBoxBreakTimeMin.Text);
                        userRateSchedule.StartDate = DateTime.Today;
                        userRateSchedule.IsCurrentRate = true;

                        if (prevUserRateSchedule != null)
                        {
                            prevUserRateSchedule.EndDate = Convert.ToDateTime(DateTime.Today.AddDays(-1).ToString("dd MMM yyyy") + " 23:59:59");

                            prevUserRateSchedule.IsCurrentRate = false;
                            prevUserRateSchedule.Update(prevUserRateSchedule);
                        }

                        userRateSchedule.Insert(userRateSchedule);
                    }
                }
                InitializeGridUser();
                this.programmaticModalPopup.Hide();
            }
        }