/// <summary>
        /// Reject
        /// </summary>
        /// <param name="employeeId">employeeId</param>
        /// <param name="payPeriodId">payPeriodId</param>
        public void Reject(int employeeId, int payPeriodId)
        {
            // Update timesheet's data
            TimesheetGateway timesheetGateway = new TimesheetGateway(Data);
            TimesheetTDS.LFS_TIMESHEETRow timesheetRow = (TimesheetTDS.LFS_TIMESHEETRow)timesheetGateway.GetRowByEmployeeIdPayPeriodId(employeeId, payPeriodId);
            timesheetRow.State = "Rejected";

            // Update timesheet's project time
            ProjectTimeGateway projectTimeGateway = new ProjectTimeGateway(Data);
            projectTimeGateway.LoadByEmployeeIdPayPeriodId(employeeId, payPeriodId);
            foreach (TimesheetTDS.LFS_PROJECT_TIMERow projectTimeRow in (TimesheetTDS.LFS_PROJECT_TIMEDataTable)projectTimeGateway.Table)
            {
                projectTimeRow.ProjectTimeState = "Rejected";
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // PUBLIC METHODS
        //
        /// <summary>
        /// Submit
        /// </summary>
        /// <param name="employeeId">employeeId</param>
        /// <param name="payPeriodId">payPeriodId</param>
        public void SetForApproval(int employeeId, int payPeriodId)
        {
            // Insert or update a timesheet
            // ... Search timesheet
            TimesheetTDS.LFS_TIMESHEETRow timesheetRow = null;
            if (Table.Rows.Count > 0)
            {
                TimesheetGateway timesheetGateway = new TimesheetGateway(Data);
                timesheetRow = (TimesheetTDS.LFS_TIMESHEETRow)timesheetGateway.GetRowByEmployeeIdPayPeriodId(employeeId, payPeriodId);
            }

            // ... If timesheet not exists
            bool newRow = false;
            if (timesheetRow == null)
            {
                newRow = true;
                timesheetRow = ((TimesheetTDS.LFS_TIMESHEETDataTable)Table).NewLFS_TIMESHEETRow();
            }

            // ... Update timesheet's data
            timesheetRow.EmployeeID = employeeId;
            timesheetRow.PayPeriodID = payPeriodId;
            timesheetRow.State = "For Approval";
            timesheetRow.Deleted = false;
            if (newRow)
            {
                ((TimesheetTDS.LFS_TIMESHEETDataTable)Table).AddLFS_TIMESHEETRow(timesheetRow);
            }
        }
        /// <summary>
        /// Submit
        /// </summary>
        /// <param name="employeeId">employeeId</param>
        /// <param name="payPeriodId">payPeriodId</param>
        public void Submit(int employeeId, int payPeriodId)
        {
            // Insert or update a timesheet
            // ... Search timesheet
            TimesheetTDS.LFS_TIMESHEETRow timesheetRow = null;
            if (Table.Rows.Count > 0)
            {
                TimesheetGateway timesheetGateway = new TimesheetGateway(Data);
                timesheetRow = (TimesheetTDS.LFS_TIMESHEETRow)timesheetGateway.GetRowByEmployeeIdPayPeriodId(employeeId, payPeriodId);
            }

            // ... If timesheet not exists
            bool newRow = false;
            if (timesheetRow == null)
            {
                newRow = true;
                timesheetRow = ((TimesheetTDS.LFS_TIMESHEETDataTable)Table).NewLFS_TIMESHEETRow();
            }

            // ... Update timesheet's data
            timesheetRow.EmployeeID = employeeId;
            timesheetRow.PayPeriodID = payPeriodId;
            timesheetRow.State = "Submitted";
            timesheetRow.Deleted = false;
            if (newRow)
            {
                ((TimesheetTDS.LFS_TIMESHEETDataTable)Table).AddLFS_TIMESHEETRow(timesheetRow);
            }

            // Update timesheet's project time
            ProjectTimeGateway projectTimeGateway = new ProjectTimeGateway(Data);
            projectTimeGateway.LoadByEmployeeIdPayPeriodId(employeeId, payPeriodId);
            foreach (TimesheetTDS.LFS_PROJECT_TIMERow projectTimeRow in (TimesheetTDS.LFS_PROJECT_TIMEDataTable) projectTimeGateway.Table)
            {
                projectTimeRow.ProjectTimeState = "Submitted";
            }
        }
        private void UpdateTimesheet()
        {
            int employeeId = Int32.Parse(hdfEmployeeID.Value);
            int periodId = Int32.Parse(hdfPeriodId.Value);

            TimesheetTDS timesheetTDS = new TimesheetTDS();
            TimesheetGateway timesheetGateway = new TimesheetGateway(timesheetTDS);
            timesheetGateway.LoadByEmployeeIdPayPeriodId(employeeId, periodId);
            LiquiForce.LFSLive.BL.LabourHours.Timesheet.Timesheet timesheet = new LiquiForce.LFSLive.BL.LabourHours.Timesheet.Timesheet(timesheetTDS);

            // Approve timesheet
            if (ViewState["timesheetState"].ToString() == "Approved")
            {
                EmployeeGateway employeeGateway1 = new EmployeeGateway(new DataSet());
                ViewState["approved_by_id"] = employeeGateway1.GetEmployeIdByLoginId(Convert.ToInt32(Session["loginID"]));
                timesheet.Approve(employeeId, periodId, (int)ViewState["approved_by_id"]);
            }

            // Reject timesheet
            if (ViewState["timesheetState"].ToString() == "Rejected")
            {
                timesheet.Reject(employeeId, periodId);
            }

            // Submit timesheet
            if (ViewState["timesheetState"].ToString() == "Submitted")
            {
                timesheet.Submit(employeeId, periodId);
            }

            try
            {
                timesheetGateway.Update2();
                timesheetTDS.AcceptChanges();
            }
            catch (Exception ex)
            {
                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) && ((string)Request.QueryString["others"] == null) && ((string)Request.QueryString["employee_id"] == null) && ((string)Request.QueryString["period_id"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in timesheet_add.aspx");
                }

                // Security check
                if (!Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_FULL_EDITING"]))
                {
                    if (Request.QueryString["others"] == "no")
                    {
                        if (!(Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_MY_TIMESHEETS_VIEW"]) && Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_MY_TIMESHEETS_ADD"])))
                        {
                            if (!Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_MY_TIMESHEETS_MANAGEMENT"]))
                            {
                                if (!Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_MY_TIMESHEETS_MANAGEMENT_WED"]))
                                {
                                    Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                                }
                            }
                        }
                    }
                    else
                    {
                        if (!(Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_MY_TIMESHEETS_VIEW"]) && Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_MY_TIMESHEETS_ADD"])))
                        {
                            if (!Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_MY_TIMESHEETS_MANAGEMENT"]))
                            {
                                if (!Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_MY_TIMESHEETS_MANAGEMENT_WED"]))
                                {
                                    if (!(Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_OTHERS_TIMESHEETS_VIEW"]) && Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_OTHERS_TIMESHEETS_ADD"])))
                                    {
                                        if (!Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_OTHERS_TIMESHEETS_MANAGEMENT"]))
                                        {
                                            if (!Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_OTHERS_TIMESHEETS_MANAGEMENT_WED"]))
                                            {
                                                Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Tag Page
                hdfSaveClick.Value = "false";
                hdfEmployeeID.Value = Request.QueryString["employee_id"].ToString();
                hdfPeriodId.Value = Request.QueryString["period_id"];
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfIsMoreThan15.Value = "false";
                hdfIsMoreThan15Edit.Value = "false";
                Session.Remove("projectTimeTempNewDummy");

                // Initialize viewstate variables
                ViewState["others"] = Request.QueryString["others"];
                ViewState["timesheetState"] = "For Approval";

                // Get State
                int employeeId = Int32.Parse(hdfEmployeeID.Value);
                int periodId = Int32.Parse(hdfPeriodId.Value);

                TimesheetGateway timesheet = new TimesheetGateway();
                timesheet.LoadByEmployeeIdPayPeriodId(employeeId, periodId);
                if (timesheet.Table.Rows.Count != 0)
                {
                    ViewState["timesheetState"] = timesheet.GetStateByEmployeeIdPayPeriodId(employeeId, periodId);
                }

                if (Request.QueryString["source_page"] == "timesheet_summary.aspx" || Request.QueryString["source_page"] == "timesheet_summary_from_approve_project_times.aspx")
                {
                    ViewState["projecttime_id"] = int.Parse(Request.QueryString["projecttime_id"]);
                }

                System.Configuration.AppSettingsReader appSettingReader = new System.Configuration.AppSettingsReader();
                ViewState["LHMode"] = appSettingReader.GetValue("LABOUR_HOURS_OPERATION_MODE", typeof(System.String)).ToString();

                // Timesheet state check
                if (((string)ViewState["LHMode"] == "Full") && (!Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_FULL_EDITING"])))
                {
                    TimesheetGateway timesheetGateway = new TimesheetGateway();
                    timesheetGateway.LoadByEmployeeIdPayPeriodId(employeeId, periodId);

                    if ((timesheetGateway.Table.Rows.Count != 0) && (timesheetGateway.GetStateByEmployeeIdPayPeriodId(employeeId, periodId) != "Rejected"))
                    {
                        Response.Redirect("./../../error_page.aspx?error=" + "You cannot add project time to an approved or submitted timesheet.");
                    }
                }

                // Prepare initial data for client
                EmployeeGateway employeeGateway = new EmployeeGateway(new DataSet());
                employeeGateway.LoadByEmployeeId(employeeId);
                tbxEmployee.Text = employeeGateway.GetFullName(employeeId);

                tbxState.Text = "For Approval";

                int companyId = Int32.Parse(hdfCompanyId.Value);
                CompaniesList companiesList = new CompaniesList(new DataSet());
                companiesList.LoadAndAddItem(-1, "(Select a client)", companyId);
                ddlClient.DataSource = companiesList.Table;
                ddlClient.DataValueField = "COMPANIES_ID";
                ddlClient.DataTextField = "Name";
                ddlClient.DataBind();
                ddlClient.SelectedIndex = 0;

                ProjectList projectList = new ProjectList();
                projectList.LoadActiveProjectsActiveInternalProjectsActiveBallparkProjectsAndAddItem(-1, "(Select a project)", -1);
                ddlProject.DataSource = projectList.Table;
                ddlProject.DataValueField = "ProjectID";
                ddlProject.DataTextField = "Name";
                ddlProject.DataBind();
                ddlProject.SelectedIndex = 0;

                CountryList countryList = new CountryList(new DataSet());
                countryList.LoadAndAddItem(-1, " ");
                ddlMealsCountry.DataSource = countryList.Table;
                ddlMealsCountry.DataValueField = "CountryID";
                ddlMealsCountry.DataTextField = "Name";
                ddlMealsCountry.DataBind();

                tkrdpStartDate.SelectedDate = DateTime.Now;
                tkrdpEndDate.SelectedDate = DateTime.Now;

                // Store timesheet dataset
                projectTimeTDS = new ProjectTimeTDS();
                Session["projectTimeTDS"] = projectTimeTDS;
                projectTimeTemp = projectTimeTDS.LFS_PROJECT_TIME_TEMP;
                Session["projectTimeTemp"] = projectTimeTDS.LFS_PROJECT_TIME_TEMP;
            }
            else
            {
                // Restore datasets
                projectTimeTDS = (ProjectTimeTDS)Session["projectTimeTDS"];
                projectTimeTemp = projectTimeTDS.LFS_PROJECT_TIME_TEMP;

                // Store datasets
                Session["projectTimeTemp"] = projectTimeTemp;
            }
        }
        /// <summary>
        /// MoveToProjectTime
        /// </summary>
        /// <param name="projectTimeTDS">projectTimeTDS</param>
        /// <param name="LHMode">LHMode</param>
        /// <param name="fullEditing">fullEditing</param>
        /// <param name="typeOfWork">typeOfWork</param>
        /// <param name="function">function</param>
        /// <param name="companyId">companyId</param>
        /// <param name="createdById">createdById</param>
        /// <returns></returns>
        public string MoveToProjectTime(ProjectTimeTDS projectTimeTDS, string LHMode, bool fullEditing, string typeOfWork, string function, int companyId, int createdById)
        {
            PayPeriodGateway payPeriodGateway = new PayPeriodGateway(new DataSet());
            TimesheetGateway timesheetGateway = new TimesheetGateway(new DataSet());
            EmployeeGateway employeeGateway = new EmployeeGateway(new DataSet());
            LiquiForce.LFSLive.BL.LabourHours.ProjectTime.ProjectTime projectTime = new LiquiForce.LFSLive.BL.LabourHours.ProjectTime.ProjectTime(projectTimeTDS);
            string errorMessage = "";

            // Clear before project times
            projectTime.Table.Rows.Clear();

            // Insert new project times
            foreach (TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_DETAILRow row in ((TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_DETAILDataTable)Table))
            {
                if (!row.Deleted)
                {
                    int employeeId = row.EmployeeID;
                    int companiesId = row.CompaniesID;
                    int projectId = row.ProjectID;
                    DateTime date_ = row.Date_;

                    DateTime? startTime = null;
                    if (!row.IsStartTimeNull())
                    {
                        string[] startTimeSplit = row.StartTime.Split(':');
                        int startTimeHour = Int32.Parse(startTimeSplit[0].ToString());
                        string[] startTimeMinuteSplit = startTimeSplit[1].Split(' ');
                        int startTimeMinute = Int32.Parse(startTimeMinuteSplit[0].ToString());
                        startTime = new DateTime(date_.Year, date_.Month, date_.Day, startTimeHour, startTimeMinute, 0);
                    }
                    DateTime? endTime = null;
                    if (!row.IsEndTimeNull())
                    {
                        string[] endTimeSplit = row.EndTime.Split(':');
                        int endTimeHour = Int32.Parse(endTimeSplit[0].ToString());
                        string[] endTimeMinuteSplit = endTimeSplit[1].Split(' ');
                        int endTimeMinute = Int32.Parse(endTimeMinuteSplit[0].ToString());
                        endTime = new DateTime(date_.Year, date_.Month, date_.Day, endTimeHour, endTimeMinute, 0);
                    }
                    double? offset = null; if (!row.IsOffsetNull()) offset = row.Offset;
                    double projectTimeValue = row.ProjectTime;
                    string workingDetails = ""; if (!row.IsWorkingDetailsNull()) workingDetails = row.WorkingDetails;
                    string location = ""; if (!row.IsLocationNull()) location = row.Location;
                    Int64? mealsCountry = null; if (!row.IsMealsCountryNull()) mealsCountry = row.MealsCountry;
                    string mealsAllowanceType = ""; if (!row.IsMealsAllowanceTypeNull()) mealsAllowanceType = row.MealsAllowanceType;
                    decimal mealsAllowance = row.MealsAllowance;
                    int? unitId = null; if (!row.IsUnitIDNull()) unitId = row.UnitID;
                    int? towedUnitId = null; if (!row.IsTowedUnitIDNull()) towedUnitId = row.TowedUnitID;
                    string comments = ""; if (!row.IsCommentsNull()) comments = row.Comments;
                    string work_ = row.Work_;
                    string function_ = row.Function_;
                    bool fairWage = row.FairWage;
                    string jobClass = ""; if (!row.IsJobClassTypeNull()) jobClass = row.JobClassType;

                    string projectTimeState = row.ProjectTimeState;

                   int projectTimeId = projectTime.Insert(employeeId, companiesId, projectId, date_, startTime, endTime, offset, workingDetails, location, mealsCountry, mealsAllowanceType, mealsAllowance, unitId, towedUnitId, projectTimeState, comments, work_, function_, fairWage, jobClass, createdById);

                   if (typeOfWork == "Full Length")
                   {
                       LiquiForce.LFSLive.BL.LabourHours.ProjectTime.ProjectTimeSection projectTimeSection = new LiquiForce.LFSLive.BL.LabourHours.ProjectTime.ProjectTimeSection(projectTimeTDS);

                       switch (function)
                       {
                           case "Install":
                               foreach (TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_SECTIONRow projectTimeRow in ((TeamProjectTime2TDS)Data).LFS_TEAM_PROJECT_TIME_SECTION)
                               {
                                   if (projectTimeRow.Selected)
                                   {
                                       projectTimeSection.Insert(projectTimeId, projectTimeRow.SectionID, projectTimeRow.FlowOrderID, true, projectTimeRow._Date, 0, 0, false, companyId);
                                   }
                               }
                               break;

                           case "Prep & Measure":
                               foreach (TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_SECTIONRow projectTimeRow in ((TeamProjectTime2TDS)Data).LFS_TEAM_PROJECT_TIME_SECTION)
                               {
                                   if (projectTimeRow.Selected)
                                   {
                                       projectTimeSection.Insert(projectTimeId, projectTimeRow.SectionID, projectTimeRow.FlowOrderID, projectTimeRow.Completed, projectTimeRow._Date, 0, 0, false, companyId);
                                   }
                               }
                               break;

                           case "Reinstate & Post Video":
                               foreach (TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_SECTIONRow projectTimeRow in ((TeamProjectTime2TDS)Data).LFS_TEAM_PROJECT_TIME_SECTION)
                               {
                                   if (projectTimeRow.Selected)
                                   {
                                       projectTimeSection.Insert(projectTimeId, projectTimeRow.SectionID, projectTimeRow.FlowOrderID, projectTimeRow.Completed, projectTimeRow._Date, projectTimeRow.PercentageOpened, projectTimeRow.PercentageBrushed, false, companyId);
                                   }
                               }
                               break;
                       }
                   }

                   //if (typeOfWork == "MH Rehab")//TODO MH
                   //{
                   //    LiquiForce.LFSLive.BL.LabourHours.ProjectTime.ProjectTimeSection projectTimeSection = new LiquiForce.LFSLive.BL.LabourHours.ProjectTime.ProjectTimeSection(projectTimeTDS);

                   //    switch (function)
                   //    {
                   //        case "Prep":
                   //            foreach (TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_SECTIONRow projectTimeRow in ((TeamProjectTime2TDS)Data).LFS_TEAM_PROJECT_TIME_SECTION)
                   //            {
                   //                if (projectTimeRow.Selected)
                   //                {
                   //                    projectTimeSection.Insert(projectTimeId, projectTimeRow.SectionID, projectTimeRow.FlowOrderID, true, projectTimeRow._Date, 0, 0, false, companyId);
                   //                }
                   //            }
                   //            break;

                   //        case "Spray":
                   //            foreach (TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_SECTIONRow projectTimeRow in ((TeamProjectTime2TDS)Data).LFS_TEAM_PROJECT_TIME_SECTION)
                   //            {
                   //                if (projectTimeRow.Selected)
                   //                {
                   //                    projectTimeSection.Insert(projectTimeId, projectTimeRow.SectionID, projectTimeRow.FlowOrderID, true, projectTimeRow._Date, 0, 0, false, companyId);
                   //                }
                   //            }
                   //            break;
                   //    }
                   //}
                }
            }

            return errorMessage;
        }