// ////////////////////////////////////////////////////////////////////////
        // STEP2 - TEMPLATE - PRIVATE METHODS
        //
        private void StepTemplateIn()
        {
            // Set instruction
            mWizard2 master = (mWizard2)this.Master;
            master.WizardInstruction = "Please select a template and click next.";

            // Initialize  variables
            lblMessage.Visible = false;

            // Prepare initial data
            // ... for template
            TeamProjectTime2TemplateGateway teamProjectTime2TemplateGateway = new TeamProjectTime2TemplateGateway(teamProjectTime2TDS);
            teamProjectTime2TemplateGateway.LoadByLoginId(Convert.ToInt32(Session["loginID"]));

            // Store datasets
            Session["teamProjectTime2TDS"] = teamProjectTime2TDS;
            template = teamProjectTime2TDS.Template;
            Session["template"] = teamProjectTime2TDS.Template;
        }
        private void UpdateDatabaseForTemplate()
        {
            try
            {
                TeamProjectTime2Gateway teamProjectTime2Gateway = new TeamProjectTime2Gateway(teamProjectTime2TDSToSave);
                teamProjectTime2Gateway.UpdateForTemplate();

                teamProjectTime2TDS.AcceptChanges();
                teamProjectTime2TDSToSave.AcceptChanges();

                // Store dataset
                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; ;
            }
            catch (Exception ex)
            {
                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        // /////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // STEP1 - BEGIN
        //
        // ////////////////////////////////////////////////////////////////////////
        // STEP1 - BEGIN - METHODS
        //
        private void StepBeginIn()
        {
            // Set instruction
            mWizard2 master = (mWizard2)this.Master;
            master.WizardInstruction = "What do you want to do?";

            if ((string)ViewState["StepFrom"] == "Out")
            {
                // Prepare initial data for the client
                TeamProjectTime2TemplateGateway teamProjectTime2TemplateGateway = new TeamProjectTime2TemplateGateway(teamProjectTime2TDS);
                teamProjectTime2TemplateGateway.LoadByLoginId(Convert.ToInt32(Session["loginID"]));

                // Store datasets
                Session["teamProjectTime2TDS"] = teamProjectTime2TDS;
                template = teamProjectTime2TDS.Template;
                Session["template"] = teamProjectTime2TDS.Template;

                // Template existing check
                if (teamProjectTime2TemplateGateway.Table.Rows.Count == 0)
                {
                    hdfNewOrTemplate.Value = "New";
                    wzTeam.ActiveStepIndex = 2;
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // STEP3 - DATA - METHODS
        //
        private void StepDataIn()
        {
            // Set instruction
            mWizard2 master = (mWizard2)this.Master;
            master.WizardInstruction = "Please define global data, it will be applied to your team members on the next step.";

            // Wizard type & step from check
            if ((hdfNewOrTemplate.Value == "New") && ((string)ViewState["StepFrom"] != "Employees"))
            {
                // Prepare initial data
                ddlClient.Enabled = true;
                ddlProject.Enabled = true;

                ProjectTimeWorkList projectTimeWorkList = new ProjectTimeWorkList(new DataSet());
                projectTimeWorkList.LoadAndAddItem("(Select a Type)");
                ddlTypeOfWork.DataSource = projectTimeWorkList.Table;
                ddlTypeOfWork.DataValueField = "Work_";
                ddlTypeOfWork.DataTextField = "Work_";
                ddlTypeOfWork.DataBind();
                ddlTypeOfWork.SelectedIndex = 0;

                ProjectTimeWorkFunctionList projectTimeWorkFunctionList = new ProjectTimeWorkFunctionList(new DataSet());
                projectTimeWorkFunctionList.LoadActiveForAddAndAddItem("(Select a Function)", "-1");
                ddlFunction.DataSource = projectTimeWorkFunctionList.Table;
                ddlFunction.DataValueField = "Function_";
                ddlFunction.DataTextField = "Function_";
                ddlFunction.DataBind();
                ddlFunction.SelectedIndex = 0;
            }
            else
            {
                TeamProjectTime2Gateway teamProjectTime2Gateway = new TeamProjectTime2Gateway(teamProjectTime2TDS);
                TeamProjectTime2DetailTemp teamProjectTime2DetailTemp = new TeamProjectTime2DetailTemp(teamProjectTime2TDS);

                // Prepare initial data
                if ((string)ViewState["StepFrom"] == "Template")
                {
                    ViewState["teamProjectTimeId"] = Int32.Parse(hdfSelectedIdTemplate.Value);

                    // ... for TeamProjectTime
                    teamProjectTime2Gateway.LoadByTeamProjectTimeId((int)ViewState["teamProjectTimeId"]);

                    // ... for TeamProjectTimeDetailTemp
                    teamProjectTime2DetailTemp.LoadAllByTeamProjectTimeId((int)ViewState["teamProjectTimeId"]);
                }

                // ... for Client
                string companiesId = teamProjectTime2Gateway.GetCompaniesId((int)ViewState["teamProjectTimeId"]).ToString();

                int companyId = Int32.Parse(hdfCompanyId.Value);
                CompaniesGateway companiesGateway = new CompaniesGateway();
                companiesGateway.LoadAllByCompaniesId(Int32.Parse(companiesId), companyId);

                // ... If Company is active
                bool deleted = companiesGateway.GetDeleted(Int32.Parse(companiesId));

                if (deleted)
                {
                    ddlClient.SelectedIndex = -1;
                }
                else
                {
                    ddlClient.SelectedValue = companiesId;
                }

                if (ddlClient.SelectedValue.Length == 0)
                {
                    odsClient.DataBind();
                    ddlClient.DataSourceID = "odsClient";
                    ddlClient.DataValueField = "COMPANIES_ID";
                    ddlClient.DataTextField = "Name";
                    ddlClient.DataBind();

                    if (deleted)
                    {
                        ddlClient.SelectedIndex = -1;
                    }
                    else
                    {
                        ddlClient.SelectedValue = companiesId;
                    }
                }

                if (!deleted)
                {
                    // ... for project
                    odsProjectsInternalProjectsBallparkProjects.SelectParameters.RemoveAt(2);
                    odsProjectsInternalProjectsBallparkProjects.SelectParameters.Add("ClientId", ddlClient.SelectedValue);
                    odsProjectsInternalProjectsBallparkProjects.Select();
                    odsProjectsInternalProjectsBallparkProjects.DataBind();

                    ddlProject.DataSourceID = "odsProjectsInternalProjectsBallparkProjects";
                    ddlProject.DataValueField = "ProjectID";
                    ddlProject.DataTextField = "NAME";
                    ddlProject.DataBind();

                    try
                    {
                        ddlProject.SelectedValue = teamProjectTime2Gateway.GetProjectId((int)ViewState["teamProjectTimeId"]).ToString();
                    }
                    catch
                    {
                        ddlProject.SelectedIndex = -1;
                    }
                }
                else
                {
                    ddlProject.SelectedIndex = -1;
                }

                // ... for Date
                if ((string)ViewState["StepFrom"] == "Employees")
                {
                    tkrdpDate_.SelectedDate = teamProjectTime2Gateway.GetDate_((int)ViewState["teamProjectTimeId"]);
                }

                // ... for Type of work
                ProjectTimeWorkList projectTimeWorkList = new ProjectTimeWorkList(new DataSet());
                projectTimeWorkList.LoadAndAddItem("(Select a Type)");
                ddlTypeOfWork.DataSource = projectTimeWorkList.Table;
                ddlTypeOfWork.DataValueField = "Work_";
                ddlTypeOfWork.DataTextField = "Work_";
                ddlTypeOfWork.DataBind();

                if (teamProjectTime2Gateway.GetWork((int)ViewState["teamProjectTimeId"]).ToString() == "")
                {
                    ddlTypeOfWork.SelectedValue = "(Select a Type)";
                }
                else
                {
                    ddlTypeOfWork.SelectedValue = (string)teamProjectTime2Gateway.GetWork((int)ViewState["teamProjectTimeId"]);
                }

                // ... for function
                ProjectTimeWorkFunctionList projectTimeWorkFunctionList = new ProjectTimeWorkFunctionList(new DataSet());
                if (teamProjectTime2Gateway.GetWork((int)ViewState["teamProjectTimeId"]).ToString() == "")
                {
                    // ... If there is no type of Work, no function should be loaded
                    projectTimeWorkFunctionList.LoadActiveForAddAndAddItem("(Select a Function)", "-1");
                }
                else
                {
                    projectTimeWorkFunctionList.LoadActiveForAddAndAddItem("(Select a Function)", teamProjectTime2Gateway.GetWork((int)ViewState["teamProjectTimeId"]));
                }

                ddlFunction.DataSource = projectTimeWorkFunctionList.Table;
                ddlFunction.DataValueField = "Function_";
                ddlFunction.DataTextField = "Function_";
                ddlFunction.DataBind();

                if (teamProjectTime2Gateway.GetWork((int)ViewState["teamProjectTimeId"]).ToString() == "")
                {
                    ddlFunction.SelectedValue = "(Select a Function)";
                }
                else
                {
                    ddlFunction.SelectedValue = (string)teamProjectTime2Gateway.GetFunction((int)ViewState["teamProjectTimeId"]);
                }

                // ... for working details
                if (!deleted)
                {
                    tbxWorkingDetails.Text = teamProjectTime2Gateway.GetWorkingDetails((int)ViewState["teamProjectTimeId"]);
                }
                else
                {
                    tbxWorkingDetails.Text = "";
                }

                // ... for start time
                if (teamProjectTime2Gateway.GetStartTime((int)ViewState["teamProjectTimeId"]).HasValue)
                {
                    string startTime = ((DateTime)teamProjectTime2Gateway.GetStartTime((int)ViewState["teamProjectTimeId"])).ToString("H:mm");
                    string[] hoursMin1 = startTime.Split(':');
                    ddlStartTimeHour.SelectedValue = hoursMin1[0].Trim();
                    ddlStartTimeMinute.SelectedValue = hoursMin1[1].Trim();
                }

                // ... for end time
                if (teamProjectTime2Gateway.GetEndTime((int)ViewState["teamProjectTimeId"]).HasValue)
                {
                    string endTime = ((DateTime)teamProjectTime2Gateway.GetEndTime((int)ViewState["teamProjectTimeId"])).ToString("H:mm");
                    string[] endHoursMin1 = endTime.Split(':');
                    ddlEndTimeHour.SelectedValue = endHoursMin1[0].Trim();
                    ddlEndTimeMinute.SelectedValue = endHoursMin1[1].Trim();
                }

                // ... for offset
                if (teamProjectTime2Gateway.GetOffset((int)ViewState["teamProjectTimeId"]).HasValue) ddlLunch.SelectedValue = Math.Round(teamProjectTime2Gateway.GetOffset((int)ViewState["teamProjectTimeId"]).Value, 2).ToString();

                // ... for meals country
                if (!deleted)
                {
                    if (teamProjectTime2Gateway.GetMealsCountry((int)ViewState["teamProjectTimeId"]).HasValue)
                    {
                        ddlMealsCountry.SelectedValue = teamProjectTime2Gateway.GetMealsCountry((int)ViewState["teamProjectTimeId"]).Value.ToString();
                    }
                    else
                    {
                        ddlMealsCountry.SelectedIndex = 0;
                    }
                }
                else
                {
                    ddlMealsCountry.SelectedIndex = -1;
                }

                // ... for meals allowance
                //if (teamProjectTime2Gateway.GetMealsAllowance((int)ViewState["teamProjectTimeId"]) > 0) cbxMealsAllowance.Checked = true;

                // ... for fair wage
                if (teamProjectTime2Gateway.GetFairWage((int)ViewState["teamProjectTimeId"])) hdfFairWage.Value = "True";

                // Store datasets
                Session["teamProjectTime2TDS"] = teamProjectTime2TDS;
                template = teamProjectTime2TDS.Template;
                Session["template"] = teamProjectTime2TDS.Template;
            }

            //cbxClearUnitAssigment.Checked = false;
        }
        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);
        }
        protected void SaveSelectedId()
        {
            int idForUpdate = 0;
            bool selected = false;
            hdfSelectedIdTemplate.Value = "0";

            TeamProjectTime2Template teamProjectTime2Template = new TeamProjectTime2Template(teamProjectTime2TDS);

            foreach (GridViewRow row in grdTemplate.Rows)
            {
                // ... Update all rows
                selected = ((CheckBox)row.FindControl("cbxSelected")).Checked;
                idForUpdate = Int32.Parse(((Label)row.FindControl("lblTeamProjectTimeID")).Text.Trim());
                teamProjectTime2Template.Update(idForUpdate, selected);

                // ... Save selected project
                if (selected)
                {
                    hdfSelectedIdTemplate.Value = idForUpdate.ToString();
                }
            }
            teamProjectTime2Template.Data.AcceptChanges();

            // Store datasets
            Session["teamProjectTime2TDS"] = teamProjectTime2TDS;
            template = teamProjectTime2TDS.Template;
            Session["template"] = teamProjectTime2TDS.Template;
        }
        // ////////////////////////////////////////////////////////////////////////
        // 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"];
            }
        }
        protected void AddTimesheetTemplateNewEmptyFix(GridView grdTemplate)
        {
            if (grdTemplate.Rows.Count == 0)
            {
                TeamProjectTime2TDS.TemplateDataTable dt = new TeamProjectTime2TDS.TemplateDataTable();
                dt.AddTemplateRow("", "", "", "", false, false);
                Session["templateDummy"] = dt;

                grdTemplate.DataBind();
            }

            // normally executes at all postbacks
            if (grdTemplate.Rows.Count == 1)
            {
                TeamProjectTime2TDS.TemplateDataTable dt = (TeamProjectTime2TDS.TemplateDataTable)Session["templateDummy"];
                if (dt != null)
                {
                    grdTemplate.Rows[0].Visible = false;
                    grdTemplate.Rows[0].Controls.Clear();
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        //  STEP2 - TEMPLATE - PUBLIC METHODS
        //
        public TeamProjectTime2TDS.TemplateDataTable GetTimesheetsTemplate()
        {
            template = (TeamProjectTime2TDS.TemplateDataTable)Session["templateDummy"];
            if (template == null)
            {
                template = ((TeamProjectTime2TDS.TemplateDataTable)Session["template"]);
            }

            return template;
        }