// ////////////////////////////////////////////////////////////////////////
        // 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"];
            }
        }
        private void LoadData(int toDoId)
        {
            ToDoListInformationBasicInformationGateway toDoListInformationBasicInformationGateway = new ToDoListInformationBasicInformationGateway(toDoListInformationTDS);
            if (toDoListInformationBasicInformationGateway.Table.Rows.Count > 0)
            {
                // Load to do Details
                int companyId = Int32.Parse(hdfCompanyId.Value);

                int createdById = toDoListInformationBasicInformationGateway.GetCreatedByID(toDoId);
                hdfCreatedById.Value = createdById.ToString();
                EmployeeGateway employeeGateway = new EmployeeGateway();
                employeeGateway.LoadByEmployeeId(createdById);
                tbxCreatedBy.Text = employeeGateway.GetFullName(createdById);

                tbxCreationDate.Text = toDoListInformationBasicInformationGateway.GetCreationDate(toDoId).ToString();
                tbxState.Text = toDoListInformationBasicInformationGateway.GetState(toDoId);

                if (toDoListInformationBasicInformationGateway.GetDueDate(toDoId).HasValue)
                {
                    DateTime dueDateValue = (DateTime)toDoListInformationBasicInformationGateway.GetDueDate(toDoId);
                    tbxDueDate.Text = dueDateValue.Month.ToString() + "/" + dueDateValue.Day.ToString() + "/" + dueDateValue.Year.ToString();
                }

                int? unitId = toDoListInformationBasicInformationGateway.GetUnitID(toDoId);
                tbxUnit.Text = "";
                if (unitId.HasValue)
                {
                    UnitsGateway unitsGateway = new UnitsGateway();
                    unitsGateway.LoadByUnitId((int)unitId, companyId);
                    tbxUnit.Text = unitsGateway.GetUnitCode((int)unitId) + " " + unitsGateway.GetDescription((int)unitId);
                }
            }
        }
        protected string GetUnitCode(object unitId)
        {
            int companyId = Int32.Parse(hdfCompanyId.Value);
            if (unitId != DBNull.Value)
            {
                UnitsGateway unitsGateway = new UnitsGateway();
                unitsGateway.LoadByUnitId((int)unitId, companyId);

                if (unitsGateway.Table.Rows.Count > 0)
                {
                    return unitsGateway.GetUnitCode((int)unitId);
                }
                else
                {
                    return "";
                }
            }
            else
            {
                return "";
            }
        }
        private void Generate()
        {
            mReport1 master = (mReport1)this.Master;

            // Get Data
            string projectTimeState = (ddlProjectTimeState.SelectedValue == "(All)") ? "%" : ddlProjectTimeState.SelectedValue;

            PrintVehicleLocationGateway printVehicleLocationGateway = new PrintVehicleLocationGateway();

            if (ddlCountry.SelectedValue == "(All)")
            {
                if (ddlUnit.SelectedValue == "-1")
                {
                    printVehicleLocationGateway.LoadByStartDateEndDateProjectTimeState(DateTime.Parse(tkrdpStartDate.SelectedDate.Value.ToShortDateString()), DateTime.Parse(tkrdpEndDate.SelectedDate.Value.ToShortDateString()), projectTimeState);
                }
                else
                {
                    printVehicleLocationGateway.LoadByStartDateEndDateUnitIdProjectTimeState(DateTime.Parse(tkrdpStartDate.SelectedDate.Value.ToShortDateString()), DateTime.Parse(tkrdpEndDate.SelectedDate.Value.ToShortDateString()), int.Parse(ddlUnit.SelectedValue), projectTimeState);
                }
            }
            else
            {
                if (ddlUnit.SelectedValue == "-1")
                {
                    printVehicleLocationGateway.LoadByStartDateEndDateProjectTimeStateCountryId(DateTime.Parse(tkrdpStartDate.SelectedDate.Value.ToShortDateString()), DateTime.Parse(tkrdpEndDate.SelectedDate.Value.ToShortDateString()), projectTimeState, int.Parse(ddlCountry.SelectedValue));
                }
                else
                {
                    printVehicleLocationGateway.LoadByStartDateEndDateUnitIdProjectTimeStateCountryId(DateTime.Parse(tkrdpStartDate.SelectedDate.Value.ToShortDateString()), DateTime.Parse(tkrdpEndDate.SelectedDate.Value.ToShortDateString()), int.Parse(ddlUnit.SelectedValue), projectTimeState, int.Parse(ddlCountry.SelectedValue));
                }
            }

            // ... set properties to master page
            master.Data = printVehicleLocationGateway.Data;
            master.Table = printVehicleLocationGateway.TableName;

            // Get report
            int companyId = Convert.ToInt32(Session["companyID"]);

            if (printVehicleLocationGateway.Table.Rows.Count > 0)
            {
                if (master.Format == "pdf")
                {
                    master.Report = new LiquiForce.LFSLive.WebUI.LabourHours.ProjectTime.PrintVehicleLocationReport();
                }
                else
                {
                    master.Report = new LiquiForce.LFSLive.WebUI.LabourHours.ProjectTime.PrintVehicleLocationReportExport();
                }

                // ... set parameters to report
                if (master.Format == "pdf")
                {
                    // ... For report
                    // ... ... project time state
                    if (ddlProjectTimeState.SelectedValue == "(All)")
                    {
                        master.SetParameter("projectTimeState", "All");
                    }
                    else
                    {
                        master.SetParameter("projectTimeState", ddlProjectTimeState.SelectedItem.Text);
                    }

                    // ... ... unit
                    if (ddlUnit.SelectedValue == "-1")
                    {
                        master.SetParameter("unitName", "All");
                    }
                    else
                    {
                        int unitId = Int32.Parse(ddlUnit.SelectedValue);
                        UnitsGateway unitsGateway = new UnitsGateway();
                        unitsGateway.LoadByUnitId(unitId, companyId);
                        string unitName = "(" + unitsGateway.GetUnitCode(unitId) + ") " + unitsGateway.GetDescription(unitId);

                        master.SetParameter("unitName", unitName);
                    }

                    // ... ...  user
                    int loginId = Convert.ToInt32(Session["loginID"]);

                    LoginGateway loginGateway = new LoginGateway();
                    loginGateway.LoadByLoginId(loginId, companyId);
                    string user = loginGateway.GetLastName(loginId, companyId) + " " + loginGateway.GetFirstName(loginId, companyId);
                    master.SetParameter("User", user.Trim());

                    // ... ...  dates
                    master.SetParameter("dateFrom", tkrdpStartDate.SelectedDate.Value.ToShortDateString());
                    master.SetParameter("dateTo", tkrdpEndDate.SelectedDate.Value.ToShortDateString());

                    // ... ... country
                    if (ddlCountry.SelectedValue == "2")
                    {
                        master.SetParameter("Country", "USA");
                    }
                    else
                    {
                        if (ddlCountry.SelectedValue == "1")
                        {
                            master.SetParameter("Country", "Canada");
                        }
                        else
                        {
                            master.SetParameter("Country", "All");
                        }
                    }
                }
            }
        }
        private void Generate()
        {
            mReport1 master = (mReport1)this.Master;
            int companyId = Convert.ToInt32(Session["companyID"]);

            // Get Data
            LiquiForce.LFSLive.BL.FleetManagement.Units.UnitInformationReport unitInformationReport = new LiquiForce.LFSLive.BL.FleetManagement.Units.UnitInformationReport();

            if (rbtnByUnit.Checked)
            {
                if (ddlUnits.SelectedValue == "-1")
                {
                    unitInformationReport.Load(companyId);
                }
                else
                {
                    int unitId = Int32.Parse(ddlUnits.SelectedValue);
                    unitInformationReport.LoadByUnitId(unitId, companyId);
                }
            }

            if (rbtnByUnitType.Checked)
            {
                unitInformationReport.LoadByUnitType(ddlUnitType.SelectedValue, companyId);
            }

            // ... set properties to master page
            master.Data = unitInformationReport.Data;
            master.Table = unitInformationReport.TableName;

            // Get report
            if (unitInformationReport.Table.Rows.Count > 0)
            {
                if (master.Format == "pdf")
                {
                    master.Report = new UnitsInformationReport();
                    int loginId = Convert.ToInt32(Session["loginID"]);

                    LoginGateway loginGateway = new LoginGateway();
                    loginGateway.LoadByLoginId(loginId, companyId);
                    string user = loginGateway.GetLastName(loginId, companyId) + " " + loginGateway.GetFirstName(loginId, companyId);
                    master.SetParameter("User", user.Trim());

                    if (rbtnByUnit.Checked)
                    {
                        if (ddlUnits.SelectedValue != "-1")
                        {
                            // For unit code
                            int unitId = Int32.Parse(ddlUnits.SelectedValue);
                            UnitsGateway unitsGateway = new UnitsGateway();
                            unitsGateway.LoadByUnitId(unitId, companyId);
                            string unitCode = unitsGateway.GetUnitCode(unitId);
                            master.SetParameter("Unit", unitCode);
                        }
                        else
                        {
                            master.SetParameter("Unit", "All");
                        }
                    }
                    else
                    {
                        master.SetParameter("Unit", "All");
                    }
                }
                else
                {
                    master.Report = new UnitsInformationReportExport();
                }
            }
        }
        private void Generate()
        {
            mReport1 master = (mReport1)this.Master;
            int companyId = Convert.ToInt32(Session["companyID"]);
            string checklistState = ddlChecklistState.SelectedValue;
            string mtoDotRules = ddlMtoDotRule.SelectedValue;
            string frequency = ddlFrequency.SelectedValue;
            string workingLocation = ddlWorkingLocation.SelectedValue;

            // Get Data
            LiquiForce.LFSLive.BL.FleetManagement.Units.UnitChecklistsReportUnitDetails unitChecklistsReportUnitDetails = new LiquiForce.LFSLive.BL.FleetManagement.Units.UnitChecklistsReportUnitDetails();

            // ... Load data by unit
            if (rbtnByUnit.Checked)
            {
                unitChecklistsReportUnitDetails.LoadByUnitId(Int32.Parse(ddlUnits.SelectedValue), mtoDotRules, frequency, checklistState, companyId);
            }

            // ... Load data by unit type
            bool allUnits = ckbxAllUnits.Checked;
            if (rbtnByUnitType.Checked)
            {
                if (ddlUnitType.SelectedValue == "All")
                {
                    if (ddlWorkingLocation.SelectedValue == "All")
                    {
                        unitChecklistsReportUnitDetails.Load(mtoDotRules, frequency, checklistState, companyId, allUnits);
                    }
                    else
                    {
                        unitChecklistsReportUnitDetails.LoadByWorkingLocation(workingLocation, mtoDotRules, frequency, checklistState, companyId, allUnits);
                    }
                }
                else
                {
                    if (ddlWorkingLocation.SelectedValue == "All")
                    {
                        unitChecklistsReportUnitDetails.LoadByUnitType(ddlUnitType.SelectedValue, mtoDotRules, frequency, checklistState, companyId, allUnits);
                    }
                    else
                    {
                        unitChecklistsReportUnitDetails.LoadByUnitTypeWorkingLocation(ddlUnitType.SelectedValue, workingLocation, mtoDotRules, frequency, checklistState, companyId, allUnits);
                    }
                }
            }

            // ... set properties to master page
            master.Data = unitChecklistsReportUnitDetails.Data;
            master.Table = unitChecklistsReportUnitDetails.TableName;

            // Get report
            if (unitChecklistsReportUnitDetails.Table.Rows.Count > 0)
            {
                if (master.Format == "pdf")
                {
                    master.Report = new UnitsChecklistsReport();

                    int loginId = Convert.ToInt32(Session["loginID"]);

                    LoginGateway loginGateway = new LoginGateway();
                    loginGateway.LoadByLoginId(loginId, companyId);
                    string user = loginGateway.GetLastName(loginId, companyId) + " " + loginGateway.GetFirstName(loginId, companyId);
                    master.SetParameter("User", user.Trim());

                    // For unit code
                    if (rbtnByUnit.Checked)
                    {
                        int unitId = Int32.Parse(ddlUnits.SelectedValue);
                        UnitsGateway unitsGateway = new UnitsGateway();
                        unitsGateway.LoadByUnitId(unitId, companyId);
                        string unitCode = unitsGateway.GetUnitCode(unitId);
                        master.SetParameter("Unit", unitCode);
                    }
                    else
                    {
                        master.SetParameter("Unit", "");
                    }

                    // For Unit Type
                    if (rbtnByUnitType.Checked)
                    {
                        if (ddlUnitType.SelectedValue == "All")
                        {
                            master.SetParameter("UnitType", "All");
                        }
                        else
                        {
                            master.SetParameter("UnitType", ddlUnitType.SelectedValue);
                        }
                    }
                    else
                    {
                        master.SetParameter("UnitType", "");
                    }

                    // For Working Location
                    if (ddlWorkingLocation.SelectedValue == "All")
                    {
                        master.SetParameter("WorkingLocation", "All");
                    }
                    else
                    {
                        if (ddlWorkingLocation.SelectedValue == "3")
                        {
                            master.SetParameter("WorkingLocation", "LFS USA");
                        }
                        else
                        {
                            master.SetParameter("WorkingLocation", "LFS Canada");
                        }
                    }

                    // For MTO/DOT Rules
                    if (ddlMtoDotRule.SelectedValue == "All")
                    {
                        master.SetParameter("MTO", "All");
                    }
                    else
                    {
                        master.SetParameter("MTO", ddlMtoDotRule.SelectedValue);
                    }

                    // For Frequency
                    if (ddlFrequency.SelectedValue == "(All)")
                    {
                        master.SetParameter("Frequency", "All");
                    }
                    else
                    {
                        master.SetParameter("Frequency", ddlFrequency.SelectedValue);
                    }

                    // For Checklist State
                    if (ddlChecklistState.SelectedValue == "All")
                    {
                        master.SetParameter("State", "All");
                    }
                    else
                    {
                        master.SetParameter("State", ddlChecklistState.SelectedValue);
                    }
                }
                else
                {
                    master.Report = new UnitsChecklistsReportExport();
                }
            }
        }