/// <summary>
        /// Save all comments to database (direct)
        /// </summary>
        /// <param name="companyId">companyId</param>        
        public void Save(int companyId)
        {
            SelectProjectTDS lastUsedProjectsChanges = (SelectProjectTDS)Data.GetChanges();

            if (lastUsedProjectsChanges.LastUsedProjects.Rows.Count > 0)
            {
                SelectProjectLastUsedProjectsGateway selectProjectLastUsedProjectsGateway = new SelectProjectLastUsedProjectsGateway(lastUsedProjectsChanges);

                foreach (SelectProjectTDS.LastUsedProjectsRow row in (SelectProjectTDS.LastUsedProjectsDataTable)lastUsedProjectsChanges.LastUsedProjects)
                {
                    // Insert project login
                    if ((!row.Deleted)&& (!row.InDataBase))
                    {
                        WorkProjectLoginProjectLastLogin workProjectLoginProjectLastLogin = new WorkProjectLoginProjectLastLogin(null);
                        workProjectLoginProjectLastLogin.InsertDirect(row.ClientID, row.ProjectID, row.UserID, row.LastLoggedInDate, row.COMPANY_ID, row.Deleted, row.WorkType);
                    }

                    // Update project login
                    if ((!row.Deleted) && (row.InDataBase))
                    {
                        int projectId = row.ProjectID;
                        int clientId = row.ClientID;
                        int userId = row.UserID;
                        string workType = row.WorkType;

                        // Original Values
                        int originalClientId = selectProjectLastUsedProjectsGateway.GetClientIdOriginal(projectId, clientId, userId, companyId, workType);
                        int originalProjectId = selectProjectLastUsedProjectsGateway.GetProjectIdOriginal(projectId, clientId, userId, companyId, workType);
                        int originalUserId = selectProjectLastUsedProjectsGateway.GetUserIdOriginal(projectId, clientId, userId, companyId, workType);
                        DateTime originalLastLoggedInDate = selectProjectLastUsedProjectsGateway.GetLastLoggedInDateOriginal(projectId, clientId, userId, companyId, workType);
                        int originalCompanyId = selectProjectLastUsedProjectsGateway.GetCompanyIdOriginal(projectId, clientId, userId, companyId, workType);
                        string originalWorkType = selectProjectLastUsedProjectsGateway.GetWorkTypeOriginal(projectId, clientId, userId, companyId, workType);

                        // New Values
                        int newClientId = selectProjectLastUsedProjectsGateway.GetClientId(projectId, clientId, userId, companyId, workType);
                        int newProjectId = selectProjectLastUsedProjectsGateway.GetProjectId(projectId, clientId, userId, companyId, workType);
                        int newUserId = selectProjectLastUsedProjectsGateway.GetUserId(projectId, clientId, userId, companyId, workType);
                        DateTime newLastLoggedInDate = selectProjectLastUsedProjectsGateway.GetLastLoggedInDate(projectId, clientId, userId, companyId, workType);
                        int newCompanyId = selectProjectLastUsedProjectsGateway.GetCompanyId(projectId, clientId, userId, companyId, workType);
                        string newWorkType = selectProjectLastUsedProjectsGateway.GetWorkType(projectId, clientId, userId, companyId, workType);

                        WorkProjectLoginProjectLastLogin workProjectLoginProjectLastLogin = new WorkProjectLoginProjectLastLogin(null);
                        workProjectLoginProjectLastLogin.UpdateDirect(originalClientId, originalProjectId, originalUserId, originalLastLoggedInDate, originalCompanyId, false, originalWorkType, newClientId, newProjectId, newUserId, newLastLoggedInDate, newCompanyId, false, newWorkType);
                    }
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // METHODS
        //
        private void Save()
        {
            bool existsAtGrid = false;

            // Validate page
            if (Page.IsValid)
            {
                SelectProjectLastUsedProjectsGateway selectProjectLastUsedProjectsVerifyGateway = new SelectProjectLastUsedProjectsGateway(selectProjectTDS);
                if (selectProjectLastUsedProjectsVerifyGateway.Table.Rows.Count > 0)
                {
                    // If exists the project in the grid
                    foreach (GridViewRow row in grdProjects.Rows)
                    {
                        // Grid Data
                        int gridClientId = Int32.Parse(((Label)row.FindControl("lblClientId")).Text.Trim());
                        int gridProjectId = Int32.Parse(((Label)row.FindControl("lblProjectId")).Text.Trim());
                        int gridUserId = Int32.Parse(((Label)row.FindControl("lblUserId")).Text.Trim());
                        int gridCompanyId = Int32.Parse(((Label)row.FindControl("lblCompanyId")).Text.Trim());
                        DateTime gridLastLoggedInDate = DateTime.Parse(((Label)row.FindControl("lblLastLoggedInDate")).Text.Trim());
                        string workType = hdfWorkType.Value;

                        // New Data
                        DateTime newLastLoggedInDate = DateTime.Now;

                        if ((gridClientId.ToString() == ddlClient.SelectedValue) && (gridProjectId.ToString() == ddlProject.SelectedValue) && (gridUserId == Int32.Parse(hdfLoginId.Value)) && (gridCompanyId == Int32.Parse(hdfCompanyId.Value)))
                        {
                            // Update if exists
                            SelectProjectLastUsedProjects model = new SelectProjectLastUsedProjects(selectProjectTDS);
                            model.UpdateLogginDate(gridClientId, gridProjectId, gridUserId, gridLastLoggedInDate, gridCompanyId, false, workType, newLastLoggedInDate);
                            existsAtGrid = true;
                        }

                        Session["selectProjectTDS"] = selectProjectTDS;
                        Session["lastUsedProjects"] = selectProjectTDS.LastUsedProjects;

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

                // If doesn't exists at grid
                if (!existsAtGrid)
                {
                    if (grdProjects.Rows.Count < 5)
                    {
                        // Add data if exist at grid's foot
                        GrdProjectAdd();
                    }
                    else
                    {
                        int index = 1;
                        foreach (GridViewRow row in grdProjects.Rows)
                        {
                            if (index == 5)
                            {
                                // Grid Data
                                int gridClientId = Int32.Parse(((Label)row.FindControl("lblClientId")).Text.Trim());
                                int gridProjectId = Int32.Parse(((Label)row.FindControl("lblProjectId")).Text.Trim());
                                int gridUserId = Int32.Parse(((Label)row.FindControl("lblUserId")).Text.Trim());
                                int gridCompanyId = Int32.Parse(((Label)row.FindControl("lblCompanyId")).Text.Trim());
                                DateTime gridLastLoggedInDate = DateTime.Parse(((Label)row.FindControl("lblLastLoggedInDate")).Text.Trim());
                                string workType = hdfWorkType.Value;

                                // New Data
                                int newProjectId = Int32.Parse(ddlProject.SelectedValue);
                                int newClientId = Int32.Parse(ddlClient.SelectedValue);
                                int newCompanyId = Int32.Parse(hdfCompanyId.Value);
                                int newUserId = Convert.ToInt32(Session["loginID"]);
                                DateTime newLastLoggedInDate = DateTime.Now;
                                string newWorkType = hdfWorkType.Value;

                                ProjectGateway projectGateway = new ProjectGateway();
                                projectGateway.LoadByProjectId(newProjectId);
                                string newProjectName = projectGateway.GetName(newProjectId) + " (" + projectGateway.GetProjectNumber(newProjectId) + ")";

                                int companyId = Int32.Parse(hdfCompanyId.Value);
                                CompaniesGateway companiesGateway = new CompaniesGateway();
                                companiesGateway.LoadByCompaniesId(newClientId, companyId);
                                string newClientName = companiesGateway.GetName(newClientId);

                                // Update if exists
                                SelectProjectLastUsedProjects model = new SelectProjectLastUsedProjects(selectProjectTDS);
                                model.Update(gridClientId, gridProjectId, gridUserId, gridCompanyId, workType, newClientId, newProjectId, newUserId, newLastLoggedInDate, newCompanyId, false, newWorkType, newProjectName, newClientName);
                            }

                            index = index + 1;
                        }

                        Session["selectProjectTDS"] = selectProjectTDS;
                        Session["lastUsedProjects"] = selectProjectTDS.LastUsedProjects;

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

                // Update data at bd
                UpdateDatabase();
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["work_type"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in select_project.aspx");
                }

                // Security check
                if ((string)Request.QueryString["work_type"] == "Rehab Assessment")
                {
                    if (!(Convert.ToBoolean(Session["sgLFS_CWP_REHABASSESSMENT_VIEW"])))
                    {
                        Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                    }
                }

                if ((string)Request.QueryString["work_type"] == "Full Length Lining")
                {
                    if (!(Convert.ToBoolean(Session["sgLFS_CWP_FULLLENGTHLINING_VIEW"])))
                    {
                        Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                    }
                }

                if ((string)Request.QueryString["work_type"] == "Junction Lining")
                {
                    if (!(Convert.ToBoolean(Session["sgLFS_CWP_JUNCTIONLINING_VIEW"])))
                    {
                        Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                    }
                }

                if ((string)Request.QueryString["work_type"] == "Point Repairs")
                {
                    if (!(Convert.ToBoolean(Session["sgLFS_CWP_POINTREPAIRS_VIEW"])))
                    {
                        Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                    }
                }

                if ((string)Request.QueryString["work_type"] == "Manhole Rehabilitation")
                {
                    if (!(Convert.ToBoolean(Session["sgLFS_CWP_MANHOLEREHABILITATION_VIEW"])))
                    {
                        Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                    }
                }

                // Prepare initial data
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfLoginId.Value = Convert.ToInt32(Session["loginID"]).ToString();
                hdfWorkType.Value = Request.QueryString["work_type"];
                Session.Remove("lastUsedProjectsDummy");

                if (Request.QueryString["work_type"] == "Manhole Rehabilitation")
                {
                    //lblInProjectManhole.Visible = true;
                    //cbxInProject.Visible = true;
                    //btnSelectNoProject.Visible = false;
                    upnlForManholesInProject.Visible = true;
                }
                else
                {
                    //lblInProjectManhole.Visible = false;
                    //cbxInProject.Visible = false;
                    //btnSelectNoProject.Visible = false;
                    upnlForManholesInProject.Visible = true;
                }

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

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

                // If comming from
                // ...out
                // ... load batch verification
                if (hdfWorkType.Value == "Manhole Rehabilitation")
                {
                    if (Convert.ToBoolean(Session["sgLFS_CWP_MANHOLEREHABILITATION_ADMIN"]))
                    {
                        string script = "<script language='javascript'>";
                        script += "window.open('./../ManholeRehabilitation/mr_batch_verification.aspx?source_page=select_project.aspx', '_blank', 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=500, height=540')";
                        script += "</script>";
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Wizard", script, false);
                    }
                }

                if ((hdfWorkType.Value == "Rehab Assessment") || (hdfWorkType.Value == "Full Length Lining") || (hdfWorkType.Value == "Point Repairs") || (hdfWorkType.Value == "Junction Lining") || (hdfWorkType.Value == "Junction Lining Section") || (hdfWorkType.Value == "Manhole Rehabilitation"))
                {
                    DateTime today = DateTime.Today;

                    if ((Convert.ToBoolean(Session["sgLFS_CWP_GERENCIAL_PRODUCTION_REPORTS"])) && (today.DayOfWeek == DayOfWeek.Monday))
                    {
                        string script = "<script language='javascript'>";
                        script += "window.open('./../Common/gerencial_weekly_production_report.aspx?source_page=select_project.aspx', '_blank', 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=800, height=680')";
                        script += "</script>";
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Report", script, false);
                    }

                    // ... ... load Gerencial pop up ofr gerencial reports
                    if (Convert.ToBoolean(Session["sgLFS_CWP_GERENCIAL_PRODUCTION_REPORTS"]))
                    {
                        string script = "<script language='javascript'>";
                        script += "window.open('./../Common/gerencial_daily_production_report.aspx?source_page=select_project.aspx', '_blank', 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=800, height=680')";
                        script += "</script>";
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Report2", script, false);
                    }

                    // ... ... load eSewers Data
                    ddlClient.SelectedValue = "-1";
                    selectProjectTDS = new SelectProjectTDS();
                    SelectProjectLastUsedProjectsGateway selectProjectLastUsedProjectsGateway = new SelectProjectLastUsedProjectsGateway(selectProjectTDS);
                    selectProjectLastUsedProjectsGateway.LoadByLoginIdWorkType(Int32.Parse(hdfLoginId.Value), hdfWorkType.Value, Int32.Parse(hdfCompanyId.Value));
                }

                // ... left menu
                if (Request.QueryString["source_page"] == "lm")
                {
                    ddlClient.SelectedValue = (string)Request.QueryString["client"];
                    selectProjectTDS = new SelectProjectTDS();
                    SelectProjectLastUsedProjectsGateway selectProjectLastUsedProjectsGateway = new SelectProjectLastUsedProjectsGateway(selectProjectTDS);
                    selectProjectLastUsedProjectsGateway.LoadByLoginIdWorkType(Int32.Parse(hdfLoginId.Value), hdfWorkType.Value, Int32.Parse(hdfCompanyId.Value));
                }

                // ... Store datasets
                Session["selectProjectTDS"] = selectProjectTDS;
                Session["lastUsedProjects"] = selectProjectTDS.LastUsedProjects;
            }
            else
            {
                // Restore datasets
                selectProjectTDS = (SelectProjectTDS)Session["selectProjectTDS"];
            }
        }