/// <summary>
        /// Update
        /// </summary>
        /// <param name="teamProjectTimeId">teamProjectTimeId</param>
        /// <param name="teamProjectTimeIdForReplace">teamProjectTimeIdForReplace</param>
        /// <param name="teamProjectTime2TDSToSave">teamProjectTime2TDSToSave</param>
        public void Update(int teamProjectTimeId, int teamProjectTimeIdForReplace, TeamProjectTime2TDS teamProjectTime2TDSToSave)
        {
            // Update existing row
            TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIMERow masterRow = GetRow(teamProjectTimeId);

            int companiesId = masterRow.CompaniesID;
            int projectId = masterRow.ProjectID;
            DateTime date_ = masterRow.Date_;
            DateTime? startTime = null; if (!masterRow.IsNull("StartTime")) startTime = masterRow.StartTime;
            DateTime? endTime = null; if (!masterRow.IsNull("EndTime")) endTime = masterRow.EndTime;
            double? offset = null; if (!masterRow.IsNull("Offset")) offset = masterRow.Offset;
            string workingDetails = ""; if (!masterRow.IsNull("WorkingDetails")) workingDetails = masterRow.WorkingDetails;
            string location = ""; if (!masterRow.IsNull("Location")) location = masterRow.Location;
            Int64? mealsCountry = null; if (!masterRow.IsNull("MealsCountry")) mealsCountry = masterRow.MealsCountry;
            String mealsAllowanceType = ""; if (!masterRow.IsNull("MealsAllowanceType")) mealsAllowanceType = masterRow.MealsAllowanceType;
            decimal mealsAllowance = masterRow.MealsAllowance;
            int? unitId = null; if (!masterRow.IsNull("UnitID")) unitId = masterRow.UnitID;
            int? towedUnitId = null; if (!masterRow.IsNull("TowedUnitID")) towedUnitId = masterRow.TowedUnitID;
            string comments = ""; if (!masterRow.IsNull("Comments")) comments = masterRow.Comments;
            string type = "Template";
            string state = "Done";
            int loginId = masterRow.LoginID;
            bool deleted = false;
            string work_ = masterRow.Work_;
            string function_ = masterRow.Function_;
            bool fairWage = masterRow.FairWage;

            // ... Load existing row
            TeamProjectTime2Gateway teamProjectTime2Gateway = new TeamProjectTime2Gateway(teamProjectTime2TDSToSave);
            teamProjectTime2Gateway.LoadByTeamProjectTimeId(teamProjectTimeIdForReplace);

            // ... Replace existing row
            TeamProjectTime2 teamProjectTime2ToSave = new TeamProjectTime2(teamProjectTime2TDSToSave);
            teamProjectTime2ToSave.Update(teamProjectTimeIdForReplace, teamProjectTime2Gateway.GetTemplateName(teamProjectTimeIdForReplace), companiesId, projectId, date_, startTime, endTime, offset, workingDetails, location, mealsCountry, mealsAllowanceType, mealsAllowance, unitId, towedUnitId, comments, type, state, loginId, deleted, work_, function_, fairWage);

            // Update or delete detail rows
            TeamProjectTime2Detail teamProjectTime2Detail = new TeamProjectTime2Detail(Data);
            teamProjectTime2Detail.Update(teamProjectTimeId, teamProjectTimeIdForReplace, teamProjectTime2TDSToSave);
        }
 /// <summary>
 /// InitData. 
 /// </summary>
 protected override void InitData()
 {
     _data = new TeamProjectTime2TDS();
 }
        /// <summary>
        /// Update
        /// </summary>
        /// <param name="teamProjectTimeId">teamProjectTimeId</param>
        /// <param name="teamProjectTimeIdForReplace">teamProjectTimeIdForReplace</param>
        /// <param name="teamProjectTime2TDSToSave">teamProjectTime2TDSToSave</param>
        public void Update(int teamProjectTimeId, int teamProjectTimeIdForReplace, TeamProjectTime2TDS teamProjectTime2TDSToSave)
        {
            // Load existing rows
            TeamProjectTime2DetailGateway teamProjectTime2DetailGateway = new TeamProjectTime2DetailGateway(teamProjectTime2TDSToSave);
            teamProjectTime2DetailGateway.LoadAllByTeamProjectTimeId(teamProjectTimeIdForReplace);

            // Delete existing rows
            foreach (TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_DETAILRow detailExistingRow in ((TeamProjectTime2TDS)teamProjectTime2DetailGateway.Data).LFS_TEAM_PROJECT_TIME_DETAIL)
            {
                detailExistingRow.Deleted = true;
            }

            // Insert or update detail rows
            foreach (TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_DETAILRow detailRow in ((TeamProjectTime2TDS)Data).LFS_TEAM_PROJECT_TIME_DETAIL)
            {
                int detailId = detailRow.DetailID;
                int employeeId = detailRow.EmployeeID;
                int companiesId = detailRow.CompaniesID;
                int projectId = detailRow.ProjectID;
                DateTime date_ = detailRow.Date_;
                string startTimeDetail = ""; if (!detailRow.IsNull("StartTime")) startTimeDetail = detailRow.StartTime;
                string endTimeDetail = null; if (!detailRow.IsNull("EndTime")) endTimeDetail = detailRow.EndTime;
                double? offset = null; if (!detailRow.IsNull("Offset")) offset = detailRow.Offset;
                double projectTime = detailRow.ProjectTime;
                string workingDetails = ""; if (!detailRow.IsNull("WorkingDetails")) workingDetails = detailRow.WorkingDetails;
                string location = ""; if (!detailRow.IsNull("Location")) location = detailRow.Location;
                Int64? mealsCountry = null; if (!detailRow.IsNull("MealsCountry")) mealsCountry = detailRow.MealsCountry;
                string mealsAllowanceType = ""; if (!detailRow.IsNull("MealsAllowanceType")) mealsAllowanceType = detailRow.MealsAllowanceType;
                decimal mealsAllowance = detailRow.MealsAllowance;
                int? unitId = null; if (!detailRow.IsNull("UnitID")) unitId = detailRow.UnitID;
                int? towedUnitId = null; if (!detailRow.IsNull("TowedUnitID")) towedUnitId = detailRow.TowedUnitID;
                string ProjectTimeState = detailRow.ProjectTimeState;
                string comments = ""; if (!detailRow.IsNull("Comments")) comments = detailRow.Comments;
                bool deleted = detailRow.Deleted;
                bool fairWage = detailRow.FairWage;
                string jobClassType = ""; if (!detailRow.IsNull("JobClassType")) jobClassType = detailRow.JobClassType;

                // ... Get row if exists
                TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_DETAILRow oldDetailRow = null;

                try
                {
                    oldDetailRow = (TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_DETAILRow)teamProjectTime2DetailGateway.GetRow(teamProjectTimeIdForReplace, detailId);
                }
                catch
                {
                    oldDetailRow = null;
                }

                // ... Row check
                TeamProjectTime2Detail teamProjectTime2DetailToSave = new TeamProjectTime2Detail(teamProjectTime2TDSToSave);
                if (oldDetailRow != null)
                {
                    teamProjectTime2DetailToSave.Update(teamProjectTimeIdForReplace, detailId, employeeId, companiesId, projectId, date_, startTimeDetail, endTimeDetail, offset, projectTime, workingDetails, location, mealsCountry, mealsAllowanceType, mealsAllowance, unitId, towedUnitId, ProjectTimeState, comments, deleted, fairWage, jobClassType);
                }
                else
                {
                    teamProjectTime2DetailToSave.Insert(teamProjectTimeIdForReplace, employeeId, companiesId, projectId, date_, startTimeDetail, endTimeDetail, offset, workingDetails, location, mealsCountry, mealsAllowanceType, mealsAllowance, ProjectTimeState, comments, fairWage, jobClassType);
                }
            }
        }
        /// <summary>
        /// Insert
        /// </summary>
        /// <param name="teamProjectTimeId">teamProjectTimeId</param>
        /// <param name="teamProjectTime2TDSToSave">teamProjectTime2TDSToSave</param>
        public void Insert(int teamProjectTimeId, TeamProjectTime2TDS teamProjectTime2TDSToSave)
        {
            foreach (TeamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_DETAILRow detailRow in ((TeamProjectTime2TDS)Data).LFS_TEAM_PROJECT_TIME_DETAIL)
            {
                if (!detailRow.Deleted)
                {
                    int detailId = detailRow.DetailID;
                    int employeeId = detailRow.EmployeeID;
                    int companiesId = detailRow.CompaniesID;
                    int projectId = detailRow.ProjectID;

                    DateTime date_ = detailRow.Date_;
                    string startTimeDetail = ""; if (!detailRow.IsNull("StartTime")) startTimeDetail = detailRow.StartTime;
                    string endTimeDetail = null; if (!detailRow.IsNull("EndTime")) endTimeDetail = detailRow.EndTime;
                    double? offset = null; if (!detailRow.IsNull("Offset")) offset = detailRow.Offset;
                    double projectTime = detailRow.ProjectTime;
                    string workingDetails = ""; if (!detailRow.IsNull("WorkingDetails")) workingDetails = detailRow.WorkingDetails;
                    string location = ""; if (!detailRow.IsNull("Location")) location = detailRow.Location;
                    Int64? mealsCountry = null; if (!detailRow.IsNull("MealsCountry")) mealsCountry = detailRow.MealsCountry;
                    string mealsAllowanceType = ""; if (!detailRow.IsNull("MealsAllowanceType")) mealsAllowanceType = detailRow.MealsAllowanceType;
                    decimal mealsAllowance = detailRow.MealsAllowance;
                    string ProjectTimeState = detailRow.ProjectTimeState;
                    string comments = ""; if (!detailRow.IsNull("Comments")) comments = detailRow.Comments;
                    bool fairWage = detailRow.FairWage;
                    string jobClassType = ""; if (!detailRow.IsNull("JobClassType")) jobClassType = detailRow.JobClassType;

                    TeamProjectTime2Detail teamProjectTime2DetailToSave = new TeamProjectTime2Detail(teamProjectTime2TDSToSave);
                    teamProjectTime2DetailToSave.Insert(0, employeeId, companiesId, projectId, date_, startTimeDetail, endTimeDetail, offset, workingDetails, location, mealsCountry, mealsAllowanceType, mealsAllowance, ProjectTimeState, comments, fairWage, jobClassType);
                }
            }
        }
        private bool PostStepEndChanges(int companyId)
        {
            // Update project times
            projectTime2TDS = new ProjectTimeTDS();
            if (cbxEndConfirm.Checked)
            {
                EmployeeGateway employeeGateway = new EmployeeGateway(new DataSet());
                int employeeId = employeeGateway.GetEmployeIdByLoginId(Convert.ToInt32(Session["loginID"]));

                TeamProjectTime2Detail teamProjectTime2Detail = new TeamProjectTime2Detail(teamProjectTime2TDS);
                string error = teamProjectTime2Detail.MoveToProjectTime(projectTime2TDS, (string)ViewState["LHMode"], Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_FULL_EDITING"]), ddlTypeOfWork.SelectedValue, ddlFunction.SelectedValue, companyId, employeeId);

                if (error != "")
                {
                    return false;
                }
            }

            // Update team project time
            teamProjectTime2TDSToSave = new TeamProjectTime2TDS();
            if (cbxEndSave.Checked)
            {
                TeamProjectTime2 teamProjectTime2 = new TeamProjectTime2(teamProjectTime2TDS);
                if (rbtnEndSaveNew.Checked)
                {
                    teamProjectTime2.Insert((int)ViewState["teamProjectTimeId"], tbxEndSaveNew.Text.Trim(), teamProjectTime2TDSToSave);
                }
                else
                {
                    teamProjectTime2.Update((int)ViewState["teamProjectTimeId"], int.Parse(luEndSaveTemplate.SelectedValue.ToString()), teamProjectTime2TDSToSave);
                }
            }

            return true;
        }
        private void DeleteTemplate(int teamProjectTimeId)
        {
            // Create TDS to save
            teamProjectTime2TDSToSave = new TeamProjectTime2TDS();

            // Delete Template
            TeamProjectTime2Template teamProjectTime2Template = new TeamProjectTime2Template(teamProjectTime2TDS);
            teamProjectTime2Template.Delete(teamProjectTimeId);

            // ... Store datasets for teplate
            Session.Remove("templateDummy");
            Session["teamProjectTime2TDS"] = teamProjectTime2TDS;
            template = teamProjectTime2TDS.Template;
            Session["template"] = teamProjectTime2TDS.Template;

            // Delete associated TeamProjectTime to Template
            // ... load TeamProjectTime
            TeamProjectTime2Gateway teamProjectTime2Gateway = new TeamProjectTime2Gateway(teamProjectTime2TDSToSave);
            teamProjectTime2Gateway.LoadByTeamProjectTimeId(teamProjectTimeId);

            // ... delete TeamProjectTime
            TeamProjectTime2 teamProjectTime2 = new TeamProjectTime2(teamProjectTime2TDSToSave);
            teamProjectTime2.Delete(teamProjectTimeId);

            // Delete associated TeamProjectTimeDetails to Template
            // ... load TeamProjectTimeDetails
            TeamProjectTime2DetailGateway teamProjectTime2DetailGateway = new TeamProjectTime2DetailGateway(teamProjectTime2TDSToSave);
            teamProjectTime2DetailGateway.LoadByTeamProjectTimeId(teamProjectTimeId);

            // ... delete TeamProjectTimeDetails
            TeamProjectTime2Detail teamProjectTime2Detail = new TeamProjectTime2Detail(teamProjectTime2TDSToSave);
            teamProjectTime2Detail.Delete(teamProjectTimeId);
        }
        // ////////////////////////////////////////////////////////////////////////
        // INITIAL EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_FULL_EDITING"]))
                {
                    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
                Session.Remove("teamProjectTime2TDS");
                Session.Remove("sectionsReinstatePostVideoSelect");

                Session.Remove("template");
                Session.Remove("templateDummy");

                Session.Remove("teamProjectTimeDetailTemp");
                Session.Remove("teamProjectTimeDetailTempDummy");

                Session.Remove("teamProjectTimeSection");
                Session.Remove("teamProjectTimeSectionDummy");

                Session.Remove("teamProjectTimeSectionLateral");
                Session.Remove("teamProjectTimeSectionLateralDummy");

                Session.Remove("teamProjectTimeSectionMH");
                Session.Remove("teamProjectTimeSectionMHDummy");

                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfBtnNext.Value = "False";

                // Initialize  variables
                lblMessage.Visible = false;

                // ... Initialize viewstate variables
                System.Configuration.AppSettingsReader appSettingReader = new System.Configuration.AppSettingsReader();
                ViewState["LHMode"] = appSettingReader.GetValue("LABOUR_HOURS_OPERATION_MODE", typeof(System.String)).ToString();
                ViewState["StepFrom"] = "Out";
                ViewState["teamProjectTimeId"] = 0;

                // ... Prepare initial data for template
                odsTemplate.SelectParameters.RemoveAt(0);
                odsTemplate.SelectParameters.Add("loginId", Convert.ToInt32(Session["loginID"]).ToString());
                odsTemplate.Select();

                // Store datasets
                teamProjectTime2TDS = new TeamProjectTime2TDS();
                Session["teamProjectTime2TDS"] = teamProjectTime2TDS;

                template = teamProjectTime2TDS.Template;
                Session["template"] = teamProjectTime2TDS.Template;

                teamProjectTimeDetailTemp = teamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_DETAIL_TEMP;
                Session["teamProjectTimeDetailTemp"] = teamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_DETAIL_TEMP;

                teamProjectTimeSection = teamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_SECTION;
                Session["teamProjectTimeSection"] = teamProjectTimeSection;

                teamProjectTimeSectionLateral = teamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_SECTION_LATERAL;
                Session["teamProjectTimeSectionLateral"] = teamProjectTimeSectionLateral;

                teamProjectTimeSectionMH = teamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_SECTION_MH;
                Session["teamProjectTimeSectionMH"] = teamProjectTimeSectionMH;

                Session["sectionsReinstatePostVideoSelect"] = sectionsReinstatePostVideoSelect;

                // StepSection1In
                wzTeam.ActiveStepIndex = 0;
                StepBeginIn();
            }
            else
            {
                // Restore datasets
                teamProjectTime2TDS = (TeamProjectTime2TDS)Session["teamProjectTime2TDS"];

                template = teamProjectTime2TDS.Template;
                Session["template"] = teamProjectTime2TDS.Template;

                teamProjectTimeDetailTemp = teamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_DETAIL_TEMP;
                Session["teamProjectTimeDetailTemp"] = teamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_DETAIL_TEMP;

                teamProjectTimeSection = teamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_SECTION;
                Session["teamProjectTimeSection"] = teamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_SECTION;

                teamProjectTimeSectionLateral = teamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_SECTION_LATERAL;
                Session["teamProjectTimeSectionLateral"] = teamProjectTimeSectionLateral;

                teamProjectTimeSectionMH = teamProjectTime2TDS.LFS_TEAM_PROJECT_TIME_SECTION_MH;
                Session["teamProjectTimeSectionMH"] = teamProjectTimeSectionMH;

                sectionsReinstatePostVideoSelect = (ArrayList)Session["sectionsReinstatePostVideoSelect"];
            }
        }