protected void ddlProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlProject.SelectedIndex > 0)
            {
                int projectId = Int32.Parse(ddlProject.SelectedValue);

                ProjectGateway projectGateway = new ProjectGateway();
                projectGateway.LoadByProjectId(projectId);

                if (projectGateway.GetFairWageApplies(projectId))
                {
                    lblIncludeAllHours.Visible = true;
                    cbxIncludeAllHours.Visible = true;
                    upnlIncludeAllHours.Update();
                    upnlIncludeAllHoursLabel.Update();
                }
                else
                {
                    lblIncludeAllHours.Visible = false;
                    cbxIncludeAllHours.Visible = false;
                    cbxIncludeAllHours.Checked = false;
                    upnlIncludeAllHours.Update();
                    upnlIncludeAllHoursLabel.Update();
                }
            }
            else
            {
                lblIncludeAllHours.Visible = false;
                cbxIncludeAllHours.Visible = false;
                cbxIncludeAllHours.Checked = false;
                upnlIncludeAllHours.Update();
                upnlIncludeAllHoursLabel.Update();
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // PUBLIC METHODS
        //
        public int LoadAll(DateTime startDate, DateTime endDate, int companyId, string month)
        {
            int rows = 0;

            PrintSummaryCostingSheetByMonthDummyInformationGateway printSummaryCostingSheetByMonthDummyInformationGateway = new PrintSummaryCostingSheetByMonthDummyInformationGateway(Data);
            printSummaryCostingSheetByMonthDummyInformationGateway.LoadProjectsWithTimesheets(startDate, endDate);

            // Load
            ProjectCostingSheetAddLabourHoursInformationGateway projectCostingSheetAddLabourHoursInformationGateway = new ProjectCostingSheetAddLabourHoursInformationGateway(Data);
            projectCostingSheetAddLabourHoursInformationGateway.ClearBeforeFill = false;
            ProjectCostingSheetAddLabourHoursInformation model = new ProjectCostingSheetAddLabourHoursInformation(Data);

            foreach (ProjectCostingSheetAddTDS.DummyInformationRow projectListRow in (ProjectCostingSheetAddTDS.DummyInformationDataTable)printSummaryCostingSheetByMonthDummyInformationGateway.Table)
            {
                int projectId = projectListRow.ProjectID;

                ProjectGateway projectGateway = new ProjectGateway();
                projectGateway.LoadByProjectId(projectId);

                ArrayList works = new ArrayList();
                works.Add("Rehab Assessment");
                works.Add("Full Length");
                works.Add("Point Lining"); works.Add("Grouting");
                works.Add("Junction Lining");
                works.Add("MH Rehab");
                works.Add("Mobilization");
                works.Add("Other");
                works.Add("Downtime");
                works.Add("Office");
                works.Add("Office / Shop");
                works.Add("R & D");
                works.Add("Special Projects");
                works.Add("Subcontractor");
                works.Add("Watermain Relining");
                works.Add("SOTA");

                if (!projectGateway.GetFairWageApplies(projectId))
                {
                    model.Load(works, projectId, startDate, endDate, companyId, month);
                }
                else
                {
                    ArrayList jobClassType = new ArrayList();
                    jobClassType.Add("Laborer Group 2");
                    jobClassType.Add("Laborer Group 6");
                    jobClassType.Add("Operator Group 1");
                    jobClassType.Add("Operator Group 2");
                    jobClassType.Add("Regular Rate");

                    model.LoadFairWageProject(works, jobClassType, projectId, startDate, endDate, companyId, month);
                }

                decimal totalCostLH = StepLabourHoursInformationProcessGrid();

                ProjectCostingSheetAddBasicInformation projectCostingSheetAddBasicInformation = new ProjectCostingSheetAddBasicInformation(Data);
                projectCostingSheetAddBasicInformation.Insert2(projectId, projectId, "", startDate, endDate, totalCostLH, totalCostLH, 0, 0, 0, 0, 0, 0, 0, 0, "", false, companyId, 0, 0, 0, 0, 0, 0, 0, 0, 0, month);

                rows = rows + projectCostingSheetAddLabourHoursInformationGateway.Table.Rows.Count;
            }

            projectCostingSheetAddLabourHoursInformationGateway.ClearBeforeFill = true;

            return rows;
        }
        protected void cvJobClassTypeFooter_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;

            int projectId = int.Parse(ddlProject.SelectedValue);

            ProjectGateway projectGateway = new ProjectGateway(new DataSet());
            projectGateway.LoadByProjectId(int.Parse(ddlProject.SelectedValue));

            if (projectGateway.GetFairWageApplies(projectId))
            {
                string jobClass = ((DropDownList)grdProjectTime.FooterRow.FindControl("ddlJobClassTypeFooter")).SelectedValue;

                if (jobClass == "(Select a Job Class Type)")
                {
                    args.IsValid = false;
                }
            }
        }
        private void GrdProjectTimeAdd()
        {
            Page.Validate("generalData");

            int projectId = int.Parse(ddlProject.SelectedValue);

            if (Page.IsValid)
            {
                Page.Validate("footerValidData");
                if (Page.IsValid)
                {
                    Page.Validate("footerData");
                    if (Page.IsValid)
                    {
                        // Validate project times
                        if (FooterValidate())
                        {
                            // Save data
                            int employeeId = Int32.Parse(hdfEmployeeID.Value);
                            int companiesId = int.Parse(ddlClient.SelectedValue);

                            if (projectId != 35 && projectId != 39 && projectId != 716)
                            {
                                DateTime date_ = tkrdpStartDate.SelectedDate.Value;
                                Int64? mealsCountry = null; if (ddlMealsCountry.SelectedValue != "-1") mealsCountry = Int64.Parse(ddlMealsCountry.SelectedValue);

                                string startTimeFooter = "";
                                string startHoursFooter = ""; if (((DropDownList)grdProjectTime.FooterRow.FindControl("ddlStartTimeHourFooter")).SelectedValue != "") startHoursFooter = ((DropDownList)grdProjectTime.FooterRow.FindControl("ddlStartTimeHourFooter")).SelectedValue.Trim();
                                string startMinutesFooter = ""; if (((DropDownList)grdProjectTime.FooterRow.FindControl("ddlStartTimeMinuteFooter")).SelectedValue != "") startMinutesFooter = ((DropDownList)grdProjectTime.FooterRow.FindControl("ddlStartTimeMinuteFooter")).SelectedValue.Trim();

                                if ((startHoursFooter != "") && (startMinutesFooter != ""))
                                {
                                    startTimeFooter = startHoursFooter + ":" + startMinutesFooter;
                                }

                                string endTimeFooter = "";
                                string endHoursFooter = ""; if (((DropDownList)grdProjectTime.FooterRow.FindControl("ddlEndTimeHourFooter")).SelectedValue != "") endHoursFooter = ((DropDownList)grdProjectTime.FooterRow.FindControl("ddlEndTimeHourFooter")).SelectedValue.Trim();
                                string endMinutesFooter = ""; if (((DropDownList)grdProjectTime.FooterRow.FindControl("ddlEndTimeMinuteFooter")).SelectedValue != "") endMinutesFooter = ((DropDownList)grdProjectTime.FooterRow.FindControl("ddlEndTimeMinuteFooter")).SelectedValue.Trim();

                                if ((endHoursFooter != "") && (endMinutesFooter != ""))
                                {
                                    endTimeFooter = endHoursFooter + ":" + endMinutesFooter;
                                }

                                decimal? offsetFooter = 0; if (((DropDownList)grdProjectTime.FooterRow.FindControl("ddlLunchFooter")).SelectedValue != "0") offsetFooter = Decimal.Round(decimal.Parse(((DropDownList)grdProjectTime.FooterRow.FindControl("ddlLunchFooter")).SelectedValue), 2);
                                double? offsetFooterFinal = null; if (offsetFooter.HasValue) offsetFooterFinal = double.Parse(((decimal)offsetFooter).ToString());
                                string typeOfWorkFooter = ((DropDownList)grdProjectTime.FooterRow.FindControl("ddlTypeOfWorkFooter")).SelectedValue;
                                int? unitIdFooter = null; if (((DropDownList)grdProjectTime.FooterRow.FindControl("ddlUnitFooter")).SelectedValue != "-1") unitIdFooter = Int32.Parse(((DropDownList)grdProjectTime.FooterRow.FindControl("ddlUnitFooter")).SelectedValue);
                                int? towedIdFooter = null; if (((DropDownList)grdProjectTime.FooterRow.FindControl("ddlTowedFooter")).SelectedValue != "-1") towedIdFooter = Int32.Parse(((DropDownList)grdProjectTime.FooterRow.FindControl("ddlTowedFooter")).SelectedValue);
                                string workingDetailsFooter = "Canada";
                                workingDetailsFooter = hdfWorkingDetails.Value;
                                bool isMealsAllowanceFooter = false;// ((CheckBox)grdProjectTime.FooterRow.FindControl("ckbxMealsAllowanceFooter")).Checked;
                                string commentsFooter = ((TextBox)grdProjectTime.FooterRow.FindControl("tbxCommentsFooter")).Text;
                                int companyId = Int32.Parse(hdfCompanyId.Value);

                                ProjectGateway projectGateway = new ProjectGateway(new DataSet());
                                projectGateway.LoadByProjectId(projectId);
                                bool fairWage = projectGateway.GetFairWageApplies(projectId);
                                string projectTimeState = "For Approval"; if ((string)ViewState["LHMode"] == "Partial") projectTimeState = "Approved";

                                string work_ = "";
                                string function_ = "";
                                if (typeOfWorkFooter != "(Select)" && typeOfWorkFooter != "")
                                {
                                    string[] workFunction = typeOfWorkFooter.ToString().Trim().Split('.');
                                    work_ = workFunction[0].Trim();
                                    function_ = workFunction[1].Trim();
                                }

                                CountryGateway countryGateway = new CountryGateway(new DataSet());
                                countryGateway.LoadByCountryId(projectGateway.GetCountryID(projectId));

                                string location = "";
                                if (countryGateway.Table.Rows.Count > 0)
                                {
                                    location = countryGateway.GetName(projectGateway.GetCountryID(projectId));
                                }

                                string mealsAllowanceType = ""; if (isMealsAllowanceFooter) mealsAllowanceType = "Full Day";
                                decimal mealsAllowance = MealsAllowance.GetMealsAllowance(mealsCountry, isMealsAllowanceFooter, "Full Day");

                                string jobClassType = ((DropDownList)grdProjectTime.FooterRow.FindControl("ddlJobClassTypeFooter")).SelectedValue;

                                // Insert Data
                                ProjectTimeTemp model = new ProjectTimeTemp(projectTimeTDS);
                                model.Insert(employeeId, companiesId, projectId, date_, startTimeFooter, endTimeFooter, offsetFooterFinal, workingDetailsFooter, location, mealsCountry, mealsAllowanceType, isMealsAllowanceFooter, mealsAllowance, unitIdFooter, towedIdFooter, projectTimeState, commentsFooter, work_, function_, typeOfWorkFooter, fairWage, jobClassType);

                                // Store Dataset
                                Session.Remove("projectTimeTempNewDummy");
                                Session["projectTimeTDS"] = projectTimeTDS;
                                Session["projectTimeTemp"] = projectTimeTDS.LFS_PROJECT_TIME_TEMP;

                                grdProjectTime.DataBind();
                                grdProjectTime.PageIndex = grdProjectTime.PageCount - 1;
                            }
                        }
                    }
                }

                hdfIsMoreThan15.Value = "false";
                upIsMoreThan15.Update();
            }
        }
        protected void grdProjectTime_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Page.Validate("generalData");
            if (Page.IsValid)
            {
                Page.Validate("editValidData");
                if (Page.IsValid)
                {
                    Page.Validate("editData");
                    if (Page.IsValid)
                    {
                        int projectTimeId = (int)e.Keys["ProjectTimeID"];
                        int employeeId = Int32.Parse(hdfEmployeeID.Value);
                        int companiesId = int.Parse(ddlClient.SelectedValue);
                        int projectId = int.Parse(ddlProject.SelectedValue);
                        DateTime date_ = tkrdpStartDate.SelectedDate.Value;
                        string workingDetails = hdfWorkingDetails.Value;
                        if (projectId == 35 || projectId == 39 || projectId == 716)
                        {
                            Page.Validate("specialData");
                        }

                        if (Page.IsValid)
                        {
                            if (projectId == 35 || projectId == 39 || projectId == 716)
                            {
                                date_ = Convert.ToDateTime(((HiddenField)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("hdfDateEdit")).Value);
                                workingDetails = ddlWorkingDetails.SelectedValue;
                            }
                            Int64? mealsCountry = null; if (ddlMealsCountry.SelectedValue != "-1") mealsCountry = Int64.Parse(ddlMealsCountry.SelectedValue);

                            string startTime = "";
                            string startHoursEdit = ((DropDownList)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ddlStartTimeHourEdit")).SelectedValue.Trim();
                            string startMinutesEdit = ((DropDownList)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ddlStartTimeMinuteEdit")).SelectedValue.Trim();

                            if ((startHoursEdit != "") && (startMinutesEdit != "") )
                            {
                                startTime = startHoursEdit + ":" + startMinutesEdit;
                            }

                            string endTime = "";
                            string endHoursEdit = ((DropDownList)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ddlEndTimeHourEdit")).SelectedValue.Trim();
                            string endMinutesEdit = ((DropDownList)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ddlEndTimeMinuteEdit")).SelectedValue.Trim();

                            if ((endHoursEdit != "") && (endMinutesEdit != ""))
                            {
                                endTime = endHoursEdit + ":" + endMinutesEdit;
                            }

                            decimal? offset = 0; if (((DropDownList)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ddlLunchEdit")).SelectedValue != "0") offset = decimal.Round(Decimal.Parse(((DropDownList)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ddlLunchEdit")).SelectedValue), 2);
                            double? offsetFinal = null; if (offset.HasValue) offsetFinal = double.Parse(((decimal)offset).ToString());
                            bool isMealsAllowance = false;// ((CheckBox)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ckbxMealsAllowanceEdit")).Checked;
                            int? unitId = null; if (((DropDownList)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ddlUnitEdit")).SelectedValue != "-1") unitId = Int32.Parse(((DropDownList)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ddlUnitEdit")).SelectedValue);
                            int? towedUnitId = null; if (((DropDownList)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ddlTowedEdit")).SelectedValue != "-1") towedUnitId = Int32.Parse(((DropDownList)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ddlTowedEdit")).SelectedValue);
                            string comments = ((TextBox)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("tbxCommentsEdit")).Text;
                            string workFunctionConcat = ((DropDownList)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ddlTypeOfWorkFunctionEdit")).SelectedValue;

                            ProjectGateway projectGateway = new ProjectGateway(new DataSet());
                            projectGateway.LoadByProjectId(projectId);
                            bool fairWage = projectGateway.GetFairWageApplies(projectId);

                            string work_ = "";
                            string function_ = "";
                            if (workFunctionConcat != "(Select)" && workFunctionConcat != "")
                            {
                                string[] workFunction = workFunctionConcat.ToString().Trim().Split('.');
                                work_ = workFunction[0].Trim();
                                function_ = workFunction[1].Trim();
                            }

                            CountryGateway countryGateway = new CountryGateway(new DataSet());
                            countryGateway.LoadByCountryId(projectGateway.GetCountryID(projectId));
                            string location = countryGateway.GetName(projectGateway.GetCountryID(projectId));

                            string mealsAllowanceType = ""; if (isMealsAllowance) mealsAllowanceType = "Full Day";
                            decimal mealsAllowance = MealsAllowance.GetMealsAllowance(mealsCountry, isMealsAllowance, "Full Day");

                            string jobClassType = ((DropDownList)grdProjectTime.Rows[e.RowIndex].Cells[1].FindControl("ddlJobClassTypeEdit")).SelectedValue;

                            // Update data
                            ProjectTimeTemp model = new ProjectTimeTemp(projectTimeTDS);
                            model.Update(projectTimeId, companiesId, projectId, date_, startTime, endTime, offsetFinal, workingDetails, location, mealsCountry, mealsAllowanceType, isMealsAllowance, mealsAllowance, unitId, towedUnitId, comments, work_, function_, workFunctionConcat, fairWage, jobClassType);

                            // Store dataset
                            Session.Remove("projectTimeTempNewDummy");
                            Session["projectTimeTDS"] = projectTimeTDS;
                            Session["projectTimeTemp"] = projectTimeTDS.LFS_PROJECT_TIME_TEMP;
                        }
                        else
                        {
                            e.Cancel = true;
                        }
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                e.Cancel = true;
            }
        }
        protected void ddlProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((ddlProject.SelectedValue != null) && (ddlProject.SelectedValue != "-1"))
            {
                int companiesId = int.Parse(ddlClient.SelectedValue);
                int projectId = int.Parse(ddlProject.SelectedValue);

                ProjectGateway projectGateway = new ProjectGateway(new DataSet());
                projectGateway.LoadByProjectId(projectId);

                CountryGateway countryGateway = new CountryGateway(new DataSet());
                countryGateway.LoadByCountryId(projectGateway.GetCountryID(projectId));

                if (projectGateway.GetFairWageApplies(projectId))
                {
                    if (projectGateway.GetCountryID(Int32.Parse(ddlProject.SelectedValue)) == 1) //Canada
                    {
                        odsJobClassType.SelectParameters.Clear();
                        odsJobClassType.SelectParameters.Add("countryId", "1");
                        odsJobClassType.SelectParameters.Add("jobClassType", "(Select a Job Class Type)");
                        odsJobClassType.SelectParameters.Add("companyId", "3");
                        odsJobClassType.Select();
                    }
                    else
                    {
                        odsJobClassType.SelectParameters.Clear();
                        odsJobClassType.SelectParameters.Add("countryId", "2");
                        odsJobClassType.SelectParameters.Add("jobClassType", "(Select a Job Class Type)");
                        odsJobClassType.SelectParameters.Add("companyId", "3");
                        odsJobClassType.Select();
                    }
                }
                else
                {
                    odsJobClassType.SelectParameters.Clear();
                    odsJobClassType.SelectParameters.Add("countryId", "-1");
                    odsJobClassType.SelectParameters.Add("jobClassType", "");
                    odsJobClassType.SelectParameters.Add("companyId", "3");
                    odsJobClassType.Select();
                }

                if (projectId != 35 && projectId != 39 && projectId != 716)
                {
                    switch (projectGateway.GetCountryID(projectId))
                    {
                        case 1:
                            ddlMealsCountry.SelectedValue = "1";
                            hdfWorkingDetails.Value = "Canada";
                            break;
                        case 2:
                            ddlMealsCountry.SelectedValue = "2";
                            hdfWorkingDetails.Value = "USA";
                            break;
                    }

                    if (tkrdpEndDate.Visible)
                    {
                        lblStartDate.Text = "Date";
                        lblLastDate.Visible = false;
                        tkrdpEndDate.Visible = false;
                    }

                    odsWorkingDetails.SelectParameters.RemoveAt(0);
                    odsWorkingDetails.SelectParameters.Add("workingDetails", "");
                    odsWorkingDetails.Select();
                    ddlWorkingDetails.DataBind();
                    ddlWorkingDetails.SelectedIndex = 0;
                    lblWorkingDetails.Visible = false;
                    ddlWorkingDetails.Visible = false;
                    lblMealsCountry.Visible = true;
                    ddlMealsCountry.Visible = true;
                    grdProjectTime.Visible = true;
                    lblCommentsVacation.Visible = false;
                    tbxCommentsVacation.Visible = false;

                    odsWorkFunctionConcat.SelectParameters.RemoveAt(0);
                    odsWorkFunctionConcat.SelectParameters.Add("workFunctionConcat", "(Select)");
                    odsWorkFunctionConcat.Select();
                }
                else
                {
                    ddlMealsCountry.SelectedIndex = 0;
                    if (!tkrdpEndDate.Visible)
                    {
                        lblStartDate.Text = "Start Date";
                        lblLastDate.Visible = true;
                        tkrdpEndDate.Visible = true;
                    }

                    odsWorkingDetails.SelectParameters.RemoveAt(0);
                    odsWorkingDetails.SelectParameters.Add("workingDetails", "(Select)");
                    odsWorkingDetails.Select();
                    ddlWorkingDetails.DataBind();
                    ddlWorkingDetails.SelectedIndex = 0;
                    lblWorkingDetails.Visible = true;
                    ddlWorkingDetails.Visible = true;
                    lblMealsCountry.Visible = false;
                    ddlMealsCountry.Visible = false;
                    grdProjectTime.Visible = false;
                    lblCommentsVacation.Visible = true;
                    tbxCommentsVacation.Visible = true;

                    odsWorkFunctionConcat.SelectParameters.RemoveAt(0);
                    odsWorkFunctionConcat.SelectParameters.Add("workFunctionConcat", "");
                    odsWorkFunctionConcat.Select();
                }
            }

            grdProjectTime.DataBind();
        }
        private void InsertProject(int projectId)
        {
            // ... Data for current project
            ProjectGateway projectGateway = new ProjectGateway(projectTDS);
            projectGateway.LoadByProjectId(projectId);

            // ... Definition of general variables
            Int64 countryId = projectGateway.GetCountryID(projectId);
            int officeId = projectGateway.GetOfficeID(projectId);
            Int64? provinceId = projectGateway.GetProvinceID(projectId);
            Int64? cityId = projectGateway.GetCityID(projectId);
            Int64? countyId = projectGateway.GetCountyID(projectId);
            int? projectLeadId = null; if (projectGateway.GetProjectLeadID(projectId).HasValue) projectLeadId = (int)projectGateway.GetProjectLeadID(projectId);
            int salesmanId = projectGateway.GetSalesmanID(projectId);
            bool deleted = false;
            int? clientPrimaryContactID = null; if (projectGateway.GetClientPrimaryContactID(projectId).HasValue) clientPrimaryContactID = (int)projectGateway.GetClientPrimaryContactID(projectId);
            int? clientSecondaryContactID = null; if (projectGateway.GetClientSecondaryContactID(projectId).HasValue) clientSecondaryContactID = (int)projectGateway.GetClientSecondaryContactID(projectId);
            int? OriginalProjectID = projectId;
            int? projectNumberCopy = null; if (projectGateway.GetLastProjectNumberCopy(projectId).HasValue) projectNumberCopy = (int)projectGateway.GetLastProjectNumberCopy(projectId) + 1; else projectNumberCopy = 1;
            string projectNumber = projectGateway.GetProjectNumber(projectId) + "-" + projectNumberCopy.ToString();
            string projectType = "Proposal";
            string projectState = projectGateway.GetProjectState(projectId);
            string name = projectGateway.GetName(projectId);
            string description = projectGateway.GetDescription(projectId);
            DateTime? proposalDate = null; if (projectGateway.GetProposalDate(projectId).HasValue) proposalDate = projectGateway.GetProposalDate(projectId);
            DateTime? startDate = null; if (projectGateway.GetStartDate(projectId).HasValue) startDate = projectGateway.GetStartDate(projectId);
            DateTime? endDate = null; if (projectGateway.GetEndDate(projectId).HasValue) endDate = projectGateway.GetEndDate(projectId);
            int clientId = projectGateway.GetClientID(projectId);
            string clientProjectNumber = projectGateway.GetClientProjectNumber(projectId);
            int? libraryCategoriesId = null; if (projectGateway.GetLibraryCategoriesId(projectId).HasValue) libraryCategoriesId = (int)projectGateway.GetLibraryCategoriesId(projectId);
            bool fairWageApplies = projectGateway.GetFairWageApplies(projectId);

            // ... Insert Project
            Project project = new Project(projectTDS);
            project.Insert(countryId, officeId, projectLeadId, salesmanId, projectNumber, projectType, projectState, name, description, proposalDate, startDate, endDate, clientId, clientPrimaryContactID, clientSecondaryContactID, clientProjectNumber, deleted, OriginalProjectID, projectNumberCopy, libraryCategoriesId, provinceId, cityId, Int32.Parse(hdfCompanyId.Value.Trim()), countyId, fairWageApplies);

            // ... Insert History
            ProjectHistory projectHistory = new ProjectHistory(projectTDS);
            projectHistory.Insert(0, 1, projectState, DateTime.Now, Convert.ToInt32(Session["loginID"]), Int32.Parse(hdfCompanyId.Value.Trim()));
        }
        protected void grdProjectTime_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                // For Start Time
                if (!string.IsNullOrEmpty(hdfStartTime.Value))
                {
                    string startTime = DateTime.Parse(hdfStartTime.Value).ToString("H:mm");
                    if (startTime != "")
                    {
                        string[] hoursMin1 = startTime.Split(':');
                        ((DropDownList)e.Row.FindControl("ddlStartTimeHourFooter")).SelectedValue = hoursMin1[0].Trim();
                        ((DropDownList)e.Row.FindControl("ddlStartTimeMinuteFooter")).SelectedValue = hoursMin1[1].Trim();
                    }
                }

                // For End Time
                if (!string.IsNullOrEmpty(hdfEndTime.Value))
                {
                    string endTime = DateTime.Parse(hdfEndTime.Value).ToString("H:mm");
                    if (endTime != "")
                    {
                        string[] endHoursMin1 = endTime.Split(':');
                        ((DropDownList)e.Row.FindControl("ddlEndTimeHourFooter")).SelectedValue = endHoursMin1[0].Trim();
                        ((DropDownList)e.Row.FindControl("ddlEndTimeMinuteFooter")).SelectedValue = endHoursMin1[1].Trim();
                    }
                }

                // For Lunch
                string lunch = hdfOffset.Value;
                if (lunch != "")
                {
                    ((DropDownList)e.Row.FindControl("ddlLunchFooter")).SelectedValue = lunch;
                }

                // For Type Of Work
                string workFunctionConcat = hdfWorkFunctionConcat.Value;
                ((DropDownList)e.Row.FindControl("ddlTypeOfWorkFooter")).SelectedValue = workFunctionConcat;

                // For Meals Allowance
                //string isMealsAllowance = hdfIsMealsAllowance.Value;
                //((CheckBox)e.Row.FindControl("ckbxMealsAllowanceFooter")).Checked = Boolean.Parse(isMealsAllowance);

                // For Job Class
                ProjectGateway projectGateway = new ProjectGateway();
                projectGateway.LoadByProjectId(Int32.Parse(ddlProject.SelectedValue));

                if (projectGateway.GetFairWageApplies(int.Parse(ddlProject.SelectedValue)))
                {
                    int employeeId = Int32.Parse(((DropDownList)e.Row.FindControl("ddlEmployeesFooter")).SelectedValue);

                    EmployeeInformationBasicInformationGateway employeeInformationBasicInformation = new EmployeeInformationBasicInformationGateway();
                    employeeInformationBasicInformation.LoadByEmployeeId(employeeId);
                    string jobClassType = employeeInformationBasicInformation.GetJobClassType(employeeId);

                    if (jobClassType != "")
                    {
                        ((DropDownList)e.Row.FindControl("ddlJobClassTypeFooter")).SelectedValue = jobClassType;
                    }
                }
            }

            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))))
            {
                int teamProjectTimeId = Int32.Parse(((Label)e.Row.FindControl("lblTeamProjectTimeIdEdit")).Text.Trim());
                int detailId = Int32.Parse(((Label)e.Row.FindControl("lblDetailIdEdit")).Text.Trim());
                TeamProjectTime2DetailGateway teamProjectTime2DetailGateway = new TeamProjectTime2DetailGateway(teamProjectTime2TDS);

                // For employee
                int employeeId = 0; if (teamProjectTime2DetailGateway.GetEmployeeId(teamProjectTimeId, detailId) != -1) employeeId = teamProjectTime2DetailGateway.GetEmployeeId(teamProjectTimeId, detailId);
                if (employeeId != -1)
                {
                    ((DropDownList)e.Row.FindControl("ddlEmployeesEdit")).SelectedValue = employeeId.ToString();
                }

                // For Start Time
                string startTime = ""; if (teamProjectTime2DetailGateway.GetStartTime(teamProjectTimeId, detailId) != "") startTime = DateTime.Parse(teamProjectTime2DetailGateway.GetStartTime(teamProjectTimeId, detailId)).ToString("H:mm"); ;
                if (startTime != "")
                {
                    string[] hoursMin1 = startTime.Split(':');
                    ((DropDownList)e.Row.FindControl("ddlStartTimeHourEdit")).SelectedValue = hoursMin1[0].Trim();
                    ((DropDownList)e.Row.FindControl("ddlStartTimeMinuteEdit")).SelectedValue = hoursMin1[1].Trim();
                }

                // For End Time
                string endTime = ""; if (teamProjectTime2DetailGateway.GetEndTime(teamProjectTimeId, detailId) != "") endTime = DateTime.Parse(teamProjectTime2DetailGateway.GetEndTime(teamProjectTimeId, detailId)).ToString("H:mm");;
                if (endTime != "")
                {
                    string[] endHoursMin1 = endTime.Split(':');
                    ((DropDownList)e.Row.FindControl("ddlEndTimeHourEdit")).SelectedValue = endHoursMin1[0].Trim();
                    ((DropDownList)e.Row.FindControl("ddlEndTimeMinuteEdit")).SelectedValue = endHoursMin1[1].Trim();
                }

                // For Type Of Work
                string work_ = teamProjectTime2DetailGateway.GetWork(teamProjectTimeId, detailId);
                string function_ = teamProjectTime2DetailGateway.GetFunction(teamProjectTimeId, detailId);
                ((DropDownList)e.Row.FindControl("ddlTypeOfWorkFunctionEdit")).SelectedValue = work_ + " . " + function_;

                string jobClass = teamProjectTime2DetailGateway.GetJobClassType(teamProjectTimeId, detailId);
                ((DropDownList)e.Row.FindControl("ddlJobClassTypeEdit")).SelectedValue = jobClass;

                // For Unit
                int? unitId = null; if (teamProjectTime2DetailGateway.GetUnitId(teamProjectTimeId, detailId).HasValue) unitId = (int)teamProjectTime2DetailGateway.GetUnitId(teamProjectTimeId, detailId);
                if (unitId.HasValue)
                {
                    ((DropDownList)e.Row.FindControl("ddlUnitEdit")).SelectedValue = ((int)unitId).ToString();
                }

                // For Towed
                int? towedId = null; if (teamProjectTime2DetailGateway.GetTowedUnitId(teamProjectTimeId, detailId).HasValue) towedId = (int)teamProjectTime2DetailGateway.GetTowedUnitId(teamProjectTimeId, detailId);
                if (towedId.HasValue)
                {
                    ((DropDownList)e.Row.FindControl("ddlTowedEdit")).SelectedValue = ((int)towedId).ToString();
                }
            }

            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Normal) || (e.Row.RowState == (DataControlRowState.Normal | DataControlRowState.Alternate))))
            {
                int teamProjectTimeId = Int32.Parse(((Label)e.Row.FindControl("lblTeamProjectTimeId")).Text.Trim());
                int detailId = Int32.Parse(((Label)e.Row.FindControl("lblDetailId")).Text.Trim());
                TeamProjectTime2DetailGateway teamProjectTime2DetailGateway = new TeamProjectTime2DetailGateway(teamProjectTime2TDS);

                // For Start Time
                string startTime = "";
                try
                {
                    if (teamProjectTime2DetailGateway.GetStartTime(teamProjectTimeId, detailId) != "") startTime = DateTime.Parse(teamProjectTime2DetailGateway.GetStartTime(teamProjectTimeId, detailId)).ToString("H:mm"); ;
                }
                catch
                {
                }
                if (startTime != "")
                {
                    ((TextBox)e.Row.FindControl("tbxStartTime")).Text = startTime;
                }

                // For End Time
                string endTime = "";
                try
                {
                    if (teamProjectTime2DetailGateway.GetEndTime(teamProjectTimeId, detailId) != "") endTime = DateTime.Parse(teamProjectTime2DetailGateway.GetEndTime(teamProjectTimeId, detailId)).ToString("H:mm"); ;
                }
                catch
                {
                }
                if (endTime != "")
                {
                    ((TextBox)e.Row.FindControl("tbxEndTime")).Text = endTime;
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // 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) && ((string)Request.QueryString["projecttime_id"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in timesheet_edit.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_EDIT"])))
                        {
                            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_EDIT"])))
                        {
                            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_EDIT"])))
                                    {
                                        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.");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Initialize viewstate variables
                ViewState["others"] = Request.QueryString["others"];
                ViewState["employee_id"] = int.Parse(Request.QueryString["employee_id"]);
                ViewState["period_id"] = int.Parse(Request.QueryString["period_id"]);
                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();
                hdfCompanyId.Value = Session["companyID"].ToString();

                // Get ProjectTime record
                ProjectTimeGateway projectTimeGateway = new ProjectTimeGateway(projectTimeTDS);
                projectTimeGateway.LoadByProjectTimeId((int)ViewState["projecttime_id"]);

                // Project time state check
                if (!Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_FULL_EDITING"]))
                {
                    if (projectTimeGateway.GetProjectTimeState((int)ViewState["projecttime_id"]) == "Approved")
                    {
                        Response.Redirect("./../../error_page.aspx?error=" + "You cannot edit an approved project time.");
                    }
                }

                if (!Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_FULL_EDITING"]))
                {
                    if (Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_MY_TIMESHEETS_MANAGEMENT"]) || Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_OTHERS_TIMESHEETS_MANAGEMENT"]))
                    {
                        bool isValidLimitPayPeriodToManagementEdit = LiquiForce.LFSLive.BL.LabourHours.ProjectTime.ProjectTime.ValidateLimitedPayPeriodToManagementEdit(projectTimeGateway.GetDate_((int)ViewState["projecttime_id"]), projectTimeGateway.GetProjectId((int)ViewState["projecttime_id"]));

                        if (!isValidLimitPayPeriodToManagementEdit)
                        {
                            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_MANAGEMENT_WED"]) || Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_OTHERS_TIMESHEETS_MANAGEMENT_WED"]))
                        {
                            bool isValidLimitPayPeriodToManagementEdit = LiquiForce.LFSLive.BL.LabourHours.ProjectTime.ProjectTime.ValidateLimitedPayPeriodToWednesdayManagementEdit(projectTimeGateway.GetDate_((int)ViewState["projecttime_id"]), projectTimeGateway.GetProjectId((int)ViewState["projecttime_id"]));

                            if (!isValidLimitPayPeriodToManagementEdit)
                            {
                                Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                            }
                        }
                    }
                }

                // Store datasets
                Session["projectTimeTDS"] = projectTimeTDS;

                // Databind
                tkrdpDate_.DataBind();
                ddlLunch.DataBind();
                tbxComments.DataBind();
                tbxState.DataBind();

                // Prepare initial data for client
                StoreNavigatorState();

                EmployeeGateway employeeGateway = new EmployeeGateway(new DataSet());
                employeeGateway.LoadByEmployeeId((int)ViewState["employee_id"]);
                tbxEmployee.Text = employeeGateway.GetFullName(int.Parse(Request.QueryString["employee_id"]));

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

                CompaniesGateway companiesGateway = new CompaniesGateway();
                companiesGateway.LoadAllByCompaniesId(Int32.Parse(DataBinder.Eval(projectTimeTDS, "Tables[LFS_PROJECT_TIME].DefaultView.[0].CompaniesID").ToString()), companyId);

                // ... If Company is active
                bool deleted = companiesGateway.GetDeleted(Int32.Parse(DataBinder.Eval(projectTimeTDS, "Tables[LFS_PROJECT_TIME].DefaultView.[0].CompaniesID").ToString()));

                if (deleted)
                {
                    ddlClient.SelectedIndex = -1;
                }
                else
                {
                    ddlClient.SelectedValue = DataBinder.Eval(projectTimeTDS, "Tables[LFS_PROJECT_TIME].DefaultView.[0].CompaniesID").ToString();
                }

                ProjectList projectList = new ProjectList();
                projectList.LoadProjectsInternalProjectsBallparkProjectsAndAddItem(-1, "(Select a project)", int.Parse(ddlClient.SelectedValue));
                ddlProject.DataSource = projectList.Table;
                ddlProject.DataValueField = "ProjectID";
                ddlProject.DataTextField = "Name";
                ddlProject.DataBind();

                if (!deleted)
                {
                    try
                    {
                        ddlProject.SelectedValue = DataBinder.Eval(projectTimeTDS, "Tables[LFS_PROJECT_TIME].DefaultView.[0].ProjectID").ToString();
                    }
                    catch
                    {
                        ddlProject.SelectedIndex = -1;
                    }
                }
                else
                {
                    ddlProject.SelectedIndex = -1;
                }

                if (ddlProject.SelectedIndex != 0)
                {
                    ProjectGateway projectGateway = new ProjectGateway();
                    projectGateway.LoadByProjectId(Int32.Parse(ddlProject.SelectedValue));

                    if (projectGateway.GetCountryID(Int32.Parse(ddlProject.SelectedValue)) == 1) //Canada
                    {
                        if (projectGateway.GetFairWageApplies(int.Parse(ddlProject.SelectedValue)))
                        {
                            ddlJobClassType.Visible = true;
                            lblJobClassType.Visible = true;

                            ProjectTimeJobClassTypeList projectTimeJobClassTypeList = new ProjectTimeJobClassTypeList();
                            projectTimeJobClassTypeList.LoadAndAddItem(1, companyId, "(Select a Job Class Type)");
                            ddlJobClassType.DataSource = projectTimeJobClassTypeList.Table;
                            ddlJobClassType.DataValueField = "JobClassType";
                            ddlJobClassType.DataTextField = "JobClassType";
                            ddlJobClassType.DataBind();

                            if (projectTimeGateway.GetJobClassType((int)ViewState["projecttime_id"]).ToString() == "")
                            {
                                ddlJobClassType.SelectedIndex = 0;
                            }
                            else
                            {
                                ddlJobClassType.SelectedValue = (string)DataBinder.Eval(projectTimeTDS, "Tables[LFS_PROJECT_TIME].DefaultView.[0].JobClassType");
                            }
                        }
                        else
                        {
                            ddlJobClassType.Visible = false;
                            lblJobClassType.Visible = false;
                        }
                    }
                    else
                    {
                        if (projectGateway.GetFairWageApplies(int.Parse(ddlProject.SelectedValue)))
                        {
                            ddlJobClassType.Visible = true;
                            lblJobClassType.Visible = true;

                            ProjectTimeJobClassTypeList projectTimeJobClassTypeList = new ProjectTimeJobClassTypeList();
                            projectTimeJobClassTypeList.LoadAndAddItem(2, companyId, "(Select a Job Class Type)");
                            ddlJobClassType.DataSource = projectTimeJobClassTypeList.Table;
                            ddlJobClassType.DataValueField = "JobClassType";
                            ddlJobClassType.DataTextField = "JobClassType";
                            ddlJobClassType.DataBind();

                            if (projectTimeGateway.GetJobClassType((int)ViewState["projecttime_id"]).ToString() == "")
                            {
                                ddlJobClassType.SelectedIndex = 0;
                            }
                            else
                            {
                                ddlJobClassType.SelectedValue = (string)DataBinder.Eval(projectTimeTDS, "Tables[LFS_PROJECT_TIME].DefaultView.[0].JobClassType");
                            }
                        }
                        else
                        {
                            ddlJobClassType.Visible = false;
                            lblJobClassType.Visible = false;
                        }
                    }
                }
                else
                {
                    ddlJobClassType.Visible = false;
                    lblJobClassType.Visible = false;
                }

                WorkingDetailsGateway workingDetailsGateway = new WorkingDetailsGateway(new DataSet());
                workingDetailsGateway.Load();
                ddlWorkingDetails.DataSource = workingDetailsGateway.Table;
                ddlWorkingDetails.DataValueField = "WorkingDetails";
                ddlWorkingDetails.DataTextField = "WorkingDetails";
                ddlWorkingDetails.DataBind();

                if (!deleted)
                {
                    ddlWorkingDetails.SelectedValue = projectTimeGateway.GetWorkingDetails((int)ViewState["projecttime_id"]);
                }
                else
                {
                    ddlWorkingDetails.SelectedIndex = -1;
                }

                ProjectTimeWorkList projectTimeWorkList = new ProjectTimeWorkList(new DataSet());

                int projectId = 0;
                try
                {
                    projectId = Int32.Parse(ddlProject.SelectedValue);
                }
                catch
                {
                }

                if (projectId == 35 || projectId == 39 || projectId == 716)
                {
                    projectTimeWorkList.LoadAndAddItem("");
                }
                else
                {
                    projectTimeWorkList.LoadAndAddItem("(Select a Type)");
                }

                // Setting start time, end time
                string startTime = DataBinder.Eval(projectTimeTDS, "Tables[LFS_PROJECT_TIME].DefaultView.[0].StartTime", "{0:H:mm}").ToString();
                if (!String.IsNullOrEmpty(startTime))
                {
                    string[] hoursMin1 = startTime.Split(':');
                    ddlStartTimeHour.SelectedValue = hoursMin1[0].Trim();
                    ddlStartTimeMinute.SelectedValue = hoursMin1[1].Trim();
                }
                else
                {
                    ddlStartTimeHour.SelectedIndex = 0;
                    ddlStartTimeMinute.SelectedIndex = 0;
                }

                string endTime = DataBinder.Eval(projectTimeTDS, "Tables[LFS_PROJECT_TIME].DefaultView.[0].EndTime", "{0:H:mm}").ToString();
                if (!String.IsNullOrEmpty(endTime))
                {
                    string[] endHoursMin1 = endTime.Split(':');
                    ddlEndTimeHour.SelectedValue = endHoursMin1[0].Trim();
                    ddlEndTimeMinute.SelectedValue = endHoursMin1[1].Trim();
                }
                else
                {
                    ddlEndTimeHour.SelectedIndex = 0;
                    ddlEndTimeMinute.SelectedIndex = 0;
                }

                // Setting type of Work
                ddlTypeOfWork.DataSource = projectTimeWorkList.Table;
                ddlTypeOfWork.DataValueField = "Work_";
                ddlTypeOfWork.DataTextField = "Work_";
                ddlTypeOfWork.DataBind();
                if (projectTimeGateway.GetWork((int)ViewState["projecttime_id"]) == "") ddlTypeOfWork.SelectedIndex = -1; else ddlTypeOfWork.SelectedValue = projectTimeGateway.GetWork((int)ViewState["projecttime_id"]);

                ProjectTimeWorkFunctionList projectTimeWorkFunctionList = new ProjectTimeWorkFunctionList(new DataSet());
                if (projectTimeGateway.GetWork((int)ViewState["projecttime_id"]) == "")
                {
                    // ... If there is no type of Work, no function should be loaded
                    projectTimeWorkFunctionList.LoadAndAddItem("", "-1");
                }
                else
                {
                    projectTimeWorkFunctionList.LoadAndAddItem("(Select a Function)", projectTimeGateway.GetWork((int)ViewState["projecttime_id"]).ToString());
                }
                ddlFunction.DataSource = projectTimeWorkFunctionList.Table;
                ddlFunction.DataValueField = "Function_";
                ddlFunction.DataTextField = "Function_";
                ddlFunction.DataBind();
                if (projectTimeGateway.GetWork((int)ViewState["projecttime_id"]).ToString() == "")
                {
                    ddlFunction.SelectedValue = "";
                }
                else
                {
                    ddlFunction.SelectedValue = (string)DataBinder.Eval(projectTimeTDS, "Tables[LFS_PROJECT_TIME].DefaultView.[0].Function_");
                }

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

                if (!deleted)
                {
                    if (projectTimeGateway.GetMealsCountry((int)ViewState["projecttime_id"]).HasValue)
                    {
                        ddlMealsCountry.SelectedValue = projectTimeGateway.GetMealsCountry((int)ViewState["projecttime_id"]).ToString();
                    }
                    else
                    {
                        ddlMealsCountry.SelectedValue = "-1";
                    }
                }
                else
                {
                    ddlMealsCountry.SelectedIndex = -1;
                }

                //if (projectTimeGateway.GetMealsAllowance((int)ViewState["projecttime_id"]) > 0)
                //{
                //    cbxMealsAllowance.Checked = true;
                //}

                string unitType = "Vehicle";
                UnitsList unitsList = new UnitsList(new DataSet());
                unitsList.LoadAndAddItem("-1", " ", Int32.Parse(Session["companyID"].ToString()), true, 0, unitType);
                ddlUnit.DataSource = unitsList.Table;
                ddlUnit.DataValueField = "UnitID";
                ddlUnit.DataTextField = "UnitCode";
                ddlUnit.DataBind();
                if (projectTimeGateway.GetUnitId((int)ViewState["projecttime_id"]).HasValue)
                {
                    ddlUnit.SelectedValue = ((int)projectTimeGateway.GetUnitId((int)ViewState["projecttime_id"])).ToString();
                }

                UnitsList towed = new UnitsList(new DataSet());
                towed.LoadAndAddItem("-1", " ", Int32.Parse(Session["companyID"].ToString()), true, 1, unitType);
                ddlTowed.DataSource = towed.Table;
                ddlTowed.DataValueField = "UnitID";
                ddlTowed.DataTextField = "UnitCode";
                ddlTowed.DataBind();
                if (projectTimeGateway.GetTowedUnitId((int)ViewState["projecttime_id"]).HasValue)
                {
                    ddlTowed.SelectedValue = ((int)projectTimeGateway.GetTowedUnitId((int)ViewState["projecttime_id"])).ToString();
                }
            }
            else
            {
                // Restore datasets
                projectTimeTDS = (ProjectTimeTDS)Session["projectTimeTDS"];
            }
        }
        protected void ddlProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((ddlProject.SelectedValue != null) && (ddlProject.SelectedValue != "-1"))
            {
                ProjectGateway projectGateway = new ProjectGateway(new DataSet());
                ProjectTimeJobClassTypeList projectTimeJobClassTypeList = new ProjectTimeJobClassTypeList();
                ProjectTimeWorkList projectTimeWorkList = new ProjectTimeWorkList(new DataSet());

                projectGateway.LoadByProjectId(int.Parse(ddlProject.SelectedValue));

                if (int.Parse(ddlProject.SelectedValue) == 35 || int.Parse(ddlProject.SelectedValue) == 39 || int.Parse(ddlProject.SelectedValue) == 716)
                {
                    projectTimeWorkList.LoadAndAddItem("");
                }
                else
                {
                    projectTimeWorkList.LoadAndAddItem("(Select a Type)");
                }

                // Setting type of Work
                ddlTypeOfWork.DataSource = projectTimeWorkList.Table;
                ddlTypeOfWork.DataValueField = "Work_";
                ddlTypeOfWork.DataTextField = "Work_";
                ddlTypeOfWork.DataBind();
                ddlTypeOfWork.SelectedIndex = 0;

                switch (projectGateway.GetCountryID(int.Parse(ddlProject.SelectedValue)))
                {
                    case 1:
                        ddlWorkingDetails.SelectedValue = "Canada";
                        ddlMealsCountry.SelectedValue = "1";
                        if (projectGateway.GetFairWageApplies(int.Parse(ddlProject.SelectedValue)))
                        {
                            ddlJobClassType.Visible = true;
                            lblJobClassType.Visible = true;

                            int companyId = Int32.Parse(hdfCompanyId.Value);
                            projectTimeJobClassTypeList.LoadAndAddItem(1, companyId, "(Select a Job Class Type)");
                            ddlJobClassType.DataSource = projectTimeJobClassTypeList.Table;
                            ddlJobClassType.DataValueField = "JobClassType";
                            ddlJobClassType.DataTextField = "JobClassType";
                            ddlJobClassType.DataBind();
                        }
                        else
                        {
                            ddlJobClassType.Visible = false;
                            lblJobClassType.Visible = false;
                        }
                        break;

                    case 2:
                        ddlWorkingDetails.SelectedValue = "USA";
                        ddlMealsCountry.SelectedValue = "2";
                        if (projectGateway.GetFairWageApplies(int.Parse(ddlProject.SelectedValue)))
                        {
                            ddlJobClassType.Visible = true;
                            lblJobClassType.Visible = true;

                            int companyId = Int32.Parse(hdfCompanyId.Value);
                            projectTimeJobClassTypeList.LoadAndAddItem(2, companyId, "(Select a Job Class Type)");
                            ddlJobClassType.DataSource = projectTimeJobClassTypeList.Table;
                            ddlJobClassType.DataValueField = "JobClassType";
                            ddlJobClassType.DataTextField = "JobClassType";
                            ddlJobClassType.DataBind();
                        }
                        else
                        {
                            ddlJobClassType.Visible = false;
                            lblJobClassType.Visible = false;
                        }
                        break;
                }
            }

            upnlWorkingDetails.Update();
            upnlMealsCountry.Update();
            upnlJobClassType.Update();
            upnlJobClassTypeLabel.Update();
            upnlTypeOfWork.Update();

            rfvProject.Validate();
        }
        // ////////////////////////////////////////////////////////////////////////
        // AUXILIAR EVENTS
        //
        protected void cvJobClassType_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;

            int projectId = int.Parse(ddlProject.SelectedValue);

            ProjectGateway projectGateway = new ProjectGateway(new DataSet());
            projectGateway.LoadByProjectId(int.Parse(ddlProject.SelectedValue));

            if (projectGateway.GetFairWageApplies(projectId))
            {
                string jobClass = ddlJobClassType.SelectedValue;

                if (jobClass == "(Select a Job Class Type)")
                {
                    args.IsValid = false;
                }
            }
        }
        private void PostPageChanges()
        {
            int projectTimeId = (int)ViewState["projecttime_id"];
            int companiesId = int.Parse(ddlClient.SelectedValue);
            int projectId = int.Parse(ddlProject.SelectedValue);
            DateTime date_ = tkrdpDate_.SelectedDate.Value;
            string workingDetails = ""; if (ddlWorkingDetails.SelectedValue != "(Select)") workingDetails = ddlWorkingDetails.SelectedValue;

            ProjectGateway projectGateway = new ProjectGateway(new DataSet());
            projectGateway.LoadByProjectId(projectId);
            CountryGateway countryGateway = new CountryGateway(new DataSet());
            countryGateway.LoadByCountryId(projectGateway.GetCountryID(projectId));
            string location = countryGateway.GetName(projectGateway.GetCountryID(projectId));

            DateTime? startTime = null;
            if ((ddlStartTimeHour.SelectedValue.Trim() != "") && (ddlStartTimeMinute.SelectedValue.Trim() != "") )
            {
                startTime = new DateTime(date_.Year, date_.Month, date_.Day, Int32.Parse(ddlStartTimeHour.SelectedValue), Int32.Parse(ddlStartTimeMinute.SelectedValue), 0);
            }

            DateTime? endTime = null;
            if ((ddlEndTimeHour.SelectedValue.Trim() != "") && (ddlEndTimeMinute.SelectedValue.Trim() != "") )
            {
                endTime = new DateTime(date_.Year, date_.Month, date_.Day, Int32.Parse(ddlEndTimeHour.SelectedValue), Int32.Parse(ddlEndTimeMinute.SelectedValue), 0);
            }

            string work_ = ""; if (ddlTypeOfWork.SelectedValue != "(Select a Type)") work_ = ddlTypeOfWork.SelectedValue;
            string function_ = ""; if (ddlFunction.SelectedValue != "(Select a Function)") function_ = ddlFunction.SelectedValue;

            double? offset = 0; if (ddlLunch.SelectedValue != "0") offset = double.Parse(ddlLunch.SelectedValue);
            Int64? mealsCountry = null; if (ddlMealsCountry.SelectedValue != "-1") mealsCountry = Int64.Parse(ddlMealsCountry.SelectedValue);
            string mealsAllowanceType = ""; //if (cbxMealsAllowance.Checked) mealsAllowanceType = "Full Day";

            decimal mealsAllowance = MealsAllowance.GetMealsAllowance(mealsCountry, false, "Full Day");

            int? unitId = null; if (ddlUnit.SelectedValue != "-1") unitId = int.Parse(ddlUnit.SelectedValue);
            int? towedUnitId = null; if (ddlTowed.SelectedValue != "-1") towedUnitId = int.Parse(ddlTowed.SelectedValue);

            string comments = tbxComments.Text.Trim();
            bool fairWage = projectGateway.GetFairWageApplies(projectId);

            string jobClassType = ""; if (ddlJobClassType.SelectedValue != "(Select a Job Class Type)") jobClassType = ddlJobClassType.SelectedValue;

            EmployeeGateway employeeGateway = new EmployeeGateway(new DataSet());
            int employeeId = employeeGateway.GetEmployeIdByLoginId(Convert.ToInt32(Session["loginID"]));

            LiquiForce.LFSLive.BL.LabourHours.ProjectTime.ProjectTime projectTime = new LiquiForce.LFSLive.BL.LabourHours.ProjectTime.ProjectTime(projectTimeTDS);
            projectTime.Update(projectTimeId, companiesId, projectId, date_, startTime, endTime, offset, workingDetails, location, mealsCountry, mealsAllowanceType, mealsAllowance, unitId, towedUnitId, comments, work_, function_, fairWage, jobClassType, employeeId);
        }
        private void UpdateState()
        {
            ProjectGateway projectGateway = new ProjectGateway(projectTDS);
            int projectId = int.Parse(hdfProjectId.Value);
            projectGateway.LoadByProjectId(projectId);

            Int64 countryId = projectGateway.GetCountryID(projectId);
            int officeId = projectGateway.GetOfficeID(projectId);
            Int64? provinceId = projectGateway.GetProvinceID(projectId);
            Int64? cityId = projectGateway.GetCityID(projectId);
            Int64? countyId = projectGateway.GetCountyID(projectId);
            int? projectLeadId = projectGateway.GetProjectLeadID(projectId);
            int salesmanId = projectGateway.GetSalesmanID(projectId);
            string projectType = projectGateway.GetProjectType(projectId);
            string projectState = null;

            switch ((string)Request.QueryString["state"])
            {
                //Proposal
                case "proposal_award":
                    projectState = "Awarded";
                    break;

                case "proposal_lost_bid":
                    projectState = "Lost Bid";
                    break;

                case "proposal_cancel":
                    projectState = "Canceled";
                    break;

                case "proposal_bidding":
                    projectState = "Bidding";
                    break;

                case "proposal_unpromote_to_ballpark":
                    projectState = "Active";
                    projectType = "Ballpark";
                    break;

                case "proposal_promote_to_project":
                    projectState = "Active";
                    projectType = "Project";
                    break;

                //Project
                case "project_waiting":
                    projectState = "Waiting";
                    break;

                case "project_activate":
                    projectState = "Active";
                    break;

                case "project_inactivate":
                    projectState = "Inactive";
                    break;

                case "project_complete":
                    projectState = "Complete";
                    break;

                case "project_cancel":
                    projectState = "Canceled";
                    break;

                case "project_unpromote_to_ballpark":
                    projectState = "Active";
                    projectType = "Ballpark";
                    break;

                case "project_unpromote_to_proposal":
                    projectState = "Awarded";
                    projectType = "Proposal";
                    break;

                case "project_tagAsInternal":
                    projectState = "Active";
                    projectType = "Internal";
                    break;

                //Internal Project
                case "internalProject_activate":
                    projectState = "Active";
                    break;

                case "internalProject_complete":
                    projectState = "Complete";
                    break;

                case "internalProject_cancel":
                    projectState = "Canceled";
                    break;

                case "internalProject_promote_to_proposal":
                    projectState = "Awarded";
                    projectType = "Proposal";
                    break;

                case "internalProject_promote_to_project":
                    projectState = "Active";
                    projectType = "Project";
                    break;

                //Ballpark
                case "ballparkProject_activate":
                    projectState = "Active";
                    break;

                case "ballparkProject_cancel":
                    projectState = "Canceled";
                    break;

                case "ballparkProject_promote_to_proposal":
                    projectState = "Bidding";
                    projectType = "Proposal";
                    break;

                case "ballparkProject_promote_to_project":
                    projectState = "Active";
                    projectType = "Project";
                    break;
            }

            string name = projectGateway.GetName(projectId);
            string description = projectGateway.GetDescription(projectId);
            DateTime? proposalDate = projectGateway.GetProposalDate(projectId);
            DateTime? startDate = projectGateway.GetStartDate(projectId);
            DateTime? endDate = projectGateway.GetEndDate(projectId);
            int clientId = projectGateway.GetClientID(projectId);
            string clientProjectNumber = projectGateway.GetClientProjectNumber(projectId);
            int? clientPrimaryContactId = projectGateway.GetClientPrimaryContactID(projectId);
            int? clientSecondaryContactId = projectGateway.GetClientSecondaryContactID(projectId);
            bool deleted = projectGateway.GetDeleted(projectId);
            int? libraryCategoriesId = projectGateway.GetLibraryCategoriesId(projectId);
            bool fairWageApplies = projectGateway.GetFairWageApplies(projectId);

            // Update project
            Project project = new Project(projectTDS);
            string projectNumber = project.UpdateProjectNumber(projectId, salesmanId);
            project.Update(projectId, countryId, officeId, projectLeadId, salesmanId, projectNumber, projectType, projectState, name, description, proposalDate, startDate, endDate, clientId, clientProjectNumber, clientPrimaryContactId, clientSecondaryContactId, deleted, libraryCategoriesId, provinceId, cityId, Int32.Parse(hdfCompanyId.Value.Trim()), countyId, fairWageApplies);

            //Insert in history
            ProjectHistory projectHistory = new ProjectHistory(projectTDS);
            int newRefId = projectHistory.GetNewRefId();
            projectHistory.Insert(projectId, newRefId, projectState, DateTime.Now, Convert.ToInt32(Session["loginID"]), Int32.Parse(hdfCompanyId.Value.Trim()));
        }
        private bool StepDataNext()
        {
            if (ValidateStepData())
            {
                PostStepDataChanges();

                if (ddlTypeOfWork.SelectedValue == "Full Length")
                {
                    switch (ddlFunction.SelectedValue)
                    {
                        case "Install":
                            SectionsInstallProcessGrid();
                            break;

                        case "Prep & Measure":
                            SectionsPrepAndMeasureProcessGrid();
                            break;

                        case "Reinstate & Post Video":
                            SectionsReinstatePostVideoProcessGrid();
                            break;
                    }
                }
                else
                {
                    if (ddlTypeOfWork.SelectedValue == "MH Rehab")
                    {
                        switch (ddlFunction.SelectedValue)
                        {
                            case "Prep":
                                ManholesRehabPrepProcessGrid();
                                break;

                            case "Spray":
                                ManholesRehabSprayProcessGrid();
                                break;
                        }
                    }
                }

                if ((ddlProject.SelectedValue != null) && (ddlProject.SelectedValue != "-1"))
                {
                    ProjectGateway projectGateway = new ProjectGateway(new DataSet());
                    projectGateway.LoadByProjectId(int.Parse(ddlProject.SelectedValue));

                    if (projectGateway.GetFairWageApplies(int.Parse(ddlProject.SelectedValue)))
                    {
                        if (projectGateway.GetCountryID(Int32.Parse(ddlProject.SelectedValue)) == 1) //Canada
                        {
                            odsJobClassType.SelectParameters.Clear();
                            odsJobClassType.SelectParameters.Add("countryId", "1");
                            odsJobClassType.SelectParameters.Add("jobClassType", "(Select a Job Class Type)");
                            odsJobClassType.SelectParameters.Add("companyId", "3");
                            odsJobClassType.Select();
                        }
                        else
                        {
                            odsJobClassType.SelectParameters.Clear();
                            odsJobClassType.SelectParameters.Add("countryId", "2");
                            odsJobClassType.SelectParameters.Add("jobClassType", "(Select a Job Class Type)");
                            odsJobClassType.SelectParameters.Add("companyId", "3");
                            odsJobClassType.Select();
                        }
                    }
                    else
                    {
                        odsJobClassType.SelectParameters.Clear();
                        odsJobClassType.SelectParameters.Add("countryId", "-1");
                        odsJobClassType.SelectParameters.Add("jobClassType", "");
                        odsJobClassType.SelectParameters.Add("companyId", "3");
                        odsJobClassType.Select();
                    }
                }

                return true;
            }
            else
            {
                return false;
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // STEP4 - LABOUR HOUR INFORMATION - METHODS
        //
        private void StepLabourHourInformationIn()
        {
            // Set instruction
            Label instruction = (Label)this.Master.FindControl("lblInstruction");
            instruction.Text = "Please verify Labour Hour information";

            // Load
            ProjectCostingSheetAddLabourHoursInformation model = new ProjectCostingSheetAddLabourHoursInformation(projectCostingSheetAddTDS);
            int projectId = Int32.Parse(hdfProjectId.Value);
            ProjectGateway projectGateway = new ProjectGateway();
            projectGateway.LoadByProjectId(projectId);

            if (projectCostingSheetAddTDS.LabourHoursInformation.Rows.Count <= 0)
            {
                ArrayList works = new ArrayList();
                if (cbxRehabAssessmentData.Checked) works.Add("Rehab Assessment");
                if (cbxFullLengthLiningData.Checked) works.Add("Full Length");
                if (cbxPointRepairData.Checked) { works.Add("Point Lining"); works.Add("Grouting"); }
                if (cbxJunctionLiningData.Checked) works.Add("Junction Lining");
                if (cbxManholeRehabData.Checked) works.Add("MH Rehab");
                if (cbxMobilizationData.Checked) works.Add("Mobilization");
                if (cbxOtherData.Checked)
                {
                    works.Add("Other");
                    works.Add("Downtime");
                    works.Add("Office");
                    works.Add("Office / Shop");
                    works.Add("R & D");
                    works.Add("Special Projects");
                    works.Add("Subcontractor");
                    works.Add("Watermain Relining");
                    works.Add("SOTA");
                }

                if (!projectGateway.GetFairWageApplies(projectId))
                {
                    model.Load(works, projectId, tkrdpFrom.SelectedDate.Value, tkrdpTo.SelectedDate.Value, int.Parse(hdfCompanyId.Value), "");
                }
                else
                {
                    ArrayList jobClassType = new ArrayList();
                    jobClassType.Add("Laborer Group 2");
                    jobClassType.Add("Laborer Group 6");
                    jobClassType.Add("Operator Group 1");
                    jobClassType.Add("Operator Group 2");
                    jobClassType.Add("Regular Rate");

                    model.LoadFairWageProject(works, jobClassType, projectId, tkrdpFrom.SelectedDate.Value, tkrdpTo.SelectedDate.Value, int.Parse(hdfCompanyId.Value), "");
                }
            }

            // Store tables
            Session.Remove("labourHoursInformationDummy");
            labourHoursInformation = (ProjectCostingSheetAddTDS.LabourHoursInformationDataTable)model.Table;
            Session["labourHoursInformation"] = labourHoursInformation;
            Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

            grdTeamMembers.Columns[10].Visible = false;
            grdTeamMembers.Columns[11].Visible = false;
            grdTeamMembers.Columns[12].Visible = false;
            grdTeamMembers.Columns[13].Visible = false;
            grdTeamMembers.Columns[15].Visible = false;
            grdTeamMembers.Columns[16].Visible = false;
            grdTeamMembers.Columns[19].Visible = false;
            grdTeamMembers.Columns[20].Visible = false;

            // Validate grid columns
            if (projectGateway.GetCountryID(projectId) == 1) //Canada
            {
                // Team Members Grid
                grdTeamMembers.Columns[14].Visible = true;
                grdTeamMembers.Columns[17].Visible = true;
                grdTeamMembers.Columns[18].Visible = false;
                grdTeamMembers.Columns[21].Visible = false;

                // Totals
                lblTeamMembersTotalCost.Text = "Total Cost (CAD) : ";
                tbxTeamMembersTotalCostCAD.Visible = true;
                tbxTeamMembersTotalCostUSD.Visible = false;
            }
            else
            {
                if (projectGateway.GetCountryID(projectId) == 2) //USA
                {
                    // Team Members Grid
                    grdTeamMembers.Columns[14].Visible = false;
                    grdTeamMembers.Columns[17].Visible = false;
                    grdTeamMembers.Columns[18].Visible = true;
                    grdTeamMembers.Columns[21].Visible = true;

                    // Totals
                    lblTeamMembersTotalCost.Text = "Total Cost (USD) : ";
                    tbxTeamMembersTotalCostCAD.Visible = false;
                    tbxTeamMembersTotalCostUSD.Visible = true;
                }
            }

            grdTeamMembers.DataBind();
            StepLabourHoursInformationProcessGrid();
        }
        // ////////////////////////////////////////////////////////////////////////
        // 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) && ((string)Request.QueryString["projecttime_id"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in timesheet_summary.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"]))
                        {
                            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"]))
                        {
                            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"]))
                                    {
                                        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.");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Initialize viewstate's variables
                ViewState["others"] = Request.QueryString["others"];
                ViewState["employee_id"] = int.Parse(Request.QueryString["employee_id"]);
                ViewState["period_id"] = int.Parse(Request.QueryString["period_id"]);
                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();

                // Get ProjectTime record
                projectTimeTDS =  new ProjectTimeTDS();

                ProjectTimeGateway projectTimeGateway = new ProjectTimeGateway(projectTimeTDS);
                projectTimeGateway.LoadByProjectTimeId((int)ViewState["projecttime_id"]);

                // Store datasets
                Session["projectTimeTDS"] = projectTimeTDS;

                // Databind
                tbxDate.DataBind();
                tbxWorkingDetails.DataBind();
                tbxStartTime.DataBind();
                tbxEndTime.DataBind();
                tbxLunch.DataBind();
                tbxComments.DataBind();
                tbxState.DataBind();

                // Prepare initial data for client
                StoreNavigatorState();

                EmployeeGateway employeeGateway = new EmployeeGateway(new DataSet());
                employeeGateway.LoadByEmployeeId((int)ViewState["employee_id"]);
                tbxEmployee.Text = employeeGateway.GetFullName((int)ViewState["employee_id"]);

                int companyId = Int32.Parse(Session["companyID"].ToString());

                CompaniesGateway companiesGateway = new CompaniesGateway(new DataSet());
                companiesGateway.LoadAllByCompaniesId(projectTimeGateway.GetCompaniesId((int)ViewState["projecttime_id"]), companyId);
                tbxClient.Text = companiesGateway.GetName(projectTimeGateway.GetCompaniesId((int)ViewState["projecttime_id"]));

                ProjectGateway projectGateway = new ProjectGateway(new DataSet());
                projectGateway.LoadByProjectId(projectTimeGateway.GetProjectId((int)ViewState["projecttime_id"]));
                tbxProject.Text = projectGateway.GetName(projectTimeGateway.GetProjectId((int)ViewState["projecttime_id"])) + "(" + projectGateway.GetProjectNumber(projectTimeGateway.GetProjectId((int)ViewState["projecttime_id"])) + ")";

                if (projectGateway.GetFairWageApplies(projectTimeGateway.GetProjectId((int)ViewState["projecttime_id"])))
                {
                    tbxJobClassType.Visible = true;
                    lblJobClassType.Visible = true;
                    tbxJobClassType.Text = projectTimeGateway.GetJobClassType((int)ViewState["projecttime_id"]);
                }
                else
                {
                    tbxJobClassType.Visible = false;
                    lblJobClassType.Visible = false;
                }

                if (projectTimeGateway.GetMealsCountry((int)ViewState["projecttime_id"]).HasValue)
                {
                    CountryGateway countryGateway = new CountryGateway(new DataSet());
                    countryGateway.LoadByCountryId((Int64)projectTimeGateway.GetMealsCountry((int)ViewState["projecttime_id"]));
                    tbxMealsCountry.Text = countryGateway.GetName((Int64)projectTimeGateway.GetMealsCountry((int)ViewState["projecttime_id"]));
                }

                //if (projectTimeGateway.GetMealsAllowance((int)ViewState["projecttime_id"]) > 0)
                //{
                //    cbxMealsAllowance.Checked = true;
                //}

                //if (projectTimeGateway.GetFairWage((int)ViewState["projecttime_id"]))
                //{
                //    cbxFairWage.Checked = true;
                //}

                if (projectTimeGateway.GetUnitId((int)ViewState["projecttime_id"]).HasValue)
                {
                    UnitsGateway unitGateway = new UnitsGateway(new DataSet());
                    unitGateway.LoadByUnitId((int)projectTimeGateway.GetUnitId((int)ViewState["projecttime_id"]), Convert.ToInt32(Session["companyID"]));
                    tbxUnit.Text = unitGateway.GetUnitCode((int)projectTimeGateway.GetUnitId((int)ViewState["projecttime_id"]));
                }

                if (projectTimeGateway.GetTowedUnitId((int)ViewState["projecttime_id"]).HasValue)
                {
                    UnitsGateway unitGateway = new UnitsGateway(new DataSet());
                    unitGateway.LoadByUnitId((int)projectTimeGateway.GetTowedUnitId((int)ViewState["projecttime_id"]), Convert.ToInt32(Session["companyID"]));
                    tbxTowed.Text = unitGateway.GetUnitCode((int)projectTimeGateway.GetTowedUnitId((int)ViewState["projecttime_id"]));
                }

                tbxTypeOfWork.Text = "";
                if (projectTimeGateway.GetWork((int)ViewState["projecttime_id"]) != "")
                {
                    tbxTypeOfWork.Text = projectTimeGateway.GetWork((int)ViewState["projecttime_id"]);
                }

                tbxFunction.Text = "";
                if (projectTimeGateway.GetFunction((int)ViewState["projecttime_id"]) != "")
                {
                    tbxFunction.Text = projectTimeGateway.GetFunction((int)ViewState["projecttime_id"]);
                }
            }
            else
            {
                // Restore datasets
                projectTimeTDS = (ProjectTimeTDS)Session["projectTimeTDS"];
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // STEP4 - EMPLOYEES - AUXILIAR EVENTS
        //
        protected void ddlEmployeesFooter_SelectedIndexChanged(object sender, EventArgs e)
        {
            ProjectGateway projectGateway = new ProjectGateway();
            projectGateway.LoadByProjectId(Int32.Parse(ddlProject.SelectedValue));

            DropDownList ddlEmployee = (DropDownList)sender;
            int employeeId = Int32.Parse(ddlEmployee.SelectedValue);

            if (projectGateway.GetFairWageApplies(int.Parse(ddlProject.SelectedValue)))
            {
                EmployeeInformationBasicInformationGateway employeeInformationBasicInformation = new EmployeeInformationBasicInformationGateway();
                employeeInformationBasicInformation.LoadByEmployeeId(employeeId);
                string jobClassType = employeeInformationBasicInformation.GetJobClassType(employeeId);

                if (jobClassType != "")
                {
                    ((DropDownList)grdProjectTime.FooterRow.FindControl("ddlJobClassTypeFooter")).SelectedValue = jobClassType;
                }
            }
        }