// ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_CWP_JUNCTIONLINING_VIEW"]) && Convert.ToBoolean(Session["sgLFS_CWP_JUNCTIONLINING_EDIT"])))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["client_id"] == null) || ((string)Request.QueryString["project_id"] == null) || ((string)Request.QueryString["row_asset_id"] == null) || ((string)Request.QueryString["row_focus"] == null) || ((string)Request.QueryString["work_type"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in jl_comments.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfCurrentProjectId.Value = Request.QueryString["project_id"].ToString();
                hdfCurrentClientId.Value = Request.QueryString["client_id"].ToString();
                hdfLoginId.Value = Convert.ToInt32(Session["loginID"]).ToString();
                hdfAssetId.Value = Convert.ToString(Request.QueryString["row_asset_id"]);
                hdfAdminPermission.Value = Convert.ToBoolean(Session["sgLFS_CWP_JUNCTIONLINING_ADMIN"]).ToString();
                hdfWorkType.Value = Request.QueryString["work_type"].ToString();
                hdfSection_.Value = Request.QueryString["section_"].ToString();
                hdfUpdate.Value = "yes";

                // ... Names for UserList
                string workType = hdfWorkType.Value.Trim();
                int companyId = Int32.Parse(hdfCompanyId.Value);

                LoginGateway loginGateway = new LoginGateway();
                loginGateway.LoadByLoginId(Convert.ToInt32(hdfLoginId.Value), companyId);
                hdfCreatedBy.Value = loginGateway.GetLastName(Convert.ToInt32(hdfLoginId.Value), companyId) + " " + loginGateway.GetFirstName(Convert.ToInt32(hdfLoginId.Value), companyId);
                hdfCreationDateTime.Value = DateTime.Now.ToString();

                // Prepare initial data
                Session.Remove("flatSectionJlCommentDetailsDummy");

                // If coming from
                // ... flat_section_jl_summary.aspx or flat_section_jl_edit.aspx
                if (Request.QueryString["source_page"] == "flat_section_jl_summary.aspx" || Request.QueryString["source_page"] == "flat_section_jl_edit.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];
                    Session["rowFocus"] = Convert.ToInt32(Request.QueryString["row_focus"].ToString());

                    // ... Load comments to edit
                    flatSectionJlTDS = (FlatSectionJlTDS)Session["flatSectionJlTDS"];
                    jlNavigatorTDS = (JlNavigatorTDS)Session["jlNavigatorTDS"];

                    // Get workId
                    int assetId = Int32.Parse(hdfAssetId.Value.Trim());
                    int currentProjectId = Int32.Parse(hdfCurrentProjectId.Value.Trim());
                    int jlWorkId = 0;
                    int raWorkId = 0;
                    int flWorkId = 0;

                    WorkGateway searchWorkGateway = new WorkGateway();
                    searchWorkGateway.LoadByProjectIdAssetIdWorkType(currentProjectId, assetId, "Junction Lining Lateral", companyId);

                    // ... If the project has jl works
                    if (searchWorkGateway.Table.Rows.Count > 0)
                    {
                        jlWorkId = searchWorkGateway.GetWorkId(assetId, "Junction Lining Lateral", currentProjectId);

                        // ... ...Tag Page
                        hdfWorkId.Value = jlWorkId.ToString().Trim();

                        // ... ...Get ra work id for comments
                        int section_ = Int32.Parse(hdfSection_.Value) ;
                        WorkGateway workGatewayRAFL = new WorkGateway();

                        // ... ...Get fl work id for comments
                        workGatewayRAFL.LoadByProjectIdAssetIdWorkType(currentProjectId, section_, "Full Length Lining", companyId);
                        if (workGatewayRAFL.Table.Rows.Count > 0)
                        {
                            flWorkId = workGatewayRAFL.GetWorkId(section_, "Full Length Lining", currentProjectId);
                        }

                        workGatewayRAFL.LoadByProjectIdAssetIdWorkType(currentProjectId, section_, "Rehab Assessment", companyId);
                        if (workGatewayRAFL.Table.Rows.Count > 0)
                        {
                            raWorkId = workGatewayRAFL.GetWorkId(section_, "Rehab Assessment", currentProjectId);
                        }

                        // ... ...Get comments
                        FlatSectionJlCommentDetailsGateway flatSectionJlCommentDetailsGateway = new FlatSectionJlCommentDetailsGateway(flatSectionJlTDS);
                        flatSectionJlCommentDetailsGateway.LoadAllByJlWorkIdRaWorkIdFlWorkId(jlWorkId, raWorkId, flWorkId, companyId);

                        FlatSectionJlCommentDetails flatSectionJlCommentDetails = new FlatSectionJlCommentDetails(flatSectionJlCommentDetailsGateway.Data);
                        flatSectionJlCommentDetails.UpdateForProcess();

                        // ... ...Get history
                        FlatSectionJlHistoryDetailsGateway flatSectionJlHistoryDetailsGateway = new FlatSectionJlHistoryDetailsGateway(flatSectionJlTDS);
                        flatSectionJlHistoryDetailsGateway.LoadAllByJlWorkIdRaWorkIdFlWorkId(jlWorkId, raWorkId, flWorkId, companyId);
                    }

                    // ... Store datasets
                    Session["flatSectionJlTDS"] = flatSectionJlTDS;
                    Session["jlNavigatorTDS"] = jlNavigatorTDS;
                    Session["flatSectionJlCommentDetails"] = flatSectionJlTDS.FlatSectionJlCommentDetails;
                }
            }
            else
            {
                // Restore datasets
                flatSectionJlTDS = (FlatSectionJlTDS)Session["flatSectionJlTDS"];
                jlNavigatorTDS = (JlNavigatorTDS)Session["jlNavigatorTDS"];
                flatSectionJlCommentDetails = flatSectionJlTDS.FlatSectionJlCommentDetails;

                // Store
                Session["flatSectionJlCommentDetails"] = flatSectionJlTDS.FlatSectionJlCommentDetails;
            }
        }
        /// <summary>
        /// Save all comments to database (direct)
        /// </summary>
        /// <param name="companyId">companyId</param>        
        public void Save(int companyId)
        {
            FlatSectionJlTDS flatSectionJlCommentDetailsChanges = (FlatSectionJlTDS)Data.GetChanges();

            if (flatSectionJlCommentDetailsChanges.FlatSectionJlCommentDetails.Rows.Count > 0)
            {
                FlatSectionJlCommentDetailsGateway flatSectionJlCommentDetailsGateway = new FlatSectionJlCommentDetailsGateway(flatSectionJlCommentDetailsChanges);

                foreach (FlatSectionJlTDS.FlatSectionJlCommentDetailsRow row in (FlatSectionJlTDS.FlatSectionJlCommentDetailsDataTable)flatSectionJlCommentDetailsChanges.FlatSectionJlCommentDetails)
                {
                    // Insert new comments
                    if ((!row.Deleted) && (!row.InDatabase))
                    {
                        WorkComments workComments = new WorkComments(null);
                        int? libraryFilesId = null; if (!row.IsLIBRARY_FILES_IDNull()) libraryFilesId = row.LIBRARY_FILES_ID;
                        string workType = ""; if (!row.IsWorkTypeNull()) workType = row.WorkType;

                        workComments.InsertDirect(row.WorkID, row.RefID, row.Type, row.Subject, row.UserID, row.DateTime_, row.Comment, libraryFilesId, row.Deleted, row.COMPANY_ID, workType);
                    }

                    // Update comments
                    if ((!row.Deleted) && (row.InDatabase))
                    {
                        int workId = row.WorkID;
                        int refId = row.RefID;
                        bool originalDeleted = false;
                        int originalCompanyId = companyId;

                        // original values
                        string originalType = flatSectionJlCommentDetailsGateway.GetTypeOriginal(workId, refId);
                        string originalSubject = flatSectionJlCommentDetailsGateway.GetSubjectOriginal(workId, refId);
                        int originalUserId = flatSectionJlCommentDetailsGateway.GetUserIdOriginal(workId, refId);
                        DateTime? originalDateTime = null; if (flatSectionJlCommentDetailsGateway.GetDateTime_Original(workId, refId) != null) originalDateTime = flatSectionJlCommentDetailsGateway.GetDateTime_Original(workId, refId);
                        string originalComment = flatSectionJlCommentDetailsGateway.GetCommentOriginal(workId, refId);
                        int? originalLibraryFilesId = null; if (flatSectionJlCommentDetailsGateway.GetLIBRARY_FILES_IDOriginal(workId, refId) != null) originalLibraryFilesId = flatSectionJlCommentDetailsGateway.GetLIBRARY_FILES_IDOriginal(workId, refId);
                        string originalWorkType = flatSectionJlCommentDetailsGateway.GetWorkTypeOriginal(workId, refId);

                        // new values
                        string newSubject = flatSectionJlCommentDetailsGateway.GetSubject(workId, refId);
                        string newComment = flatSectionJlCommentDetailsGateway.GetComment(workId, refId);
                        int? newLibraryFilesId = null; if (flatSectionJlCommentDetailsGateway.GetLIBRARY_FILES_IDOriginal(workId, refId) != null) originalLibraryFilesId = flatSectionJlCommentDetailsGateway.GetLIBRARY_FILES_ID(workId, refId);

                        WorkComments workComments = new WorkComments(null);
                        workComments.UpdateDirect(workId, refId, originalType, originalSubject, originalUserId, originalDateTime, originalComment, originalLibraryFilesId, originalDeleted, originalCompanyId, originalWorkType, workId, refId, originalType, newSubject, originalUserId, originalDateTime, newComment, newLibraryFilesId, originalDeleted, originalCompanyId, originalWorkType);
                    }

                    // Deleted comments
                    if ((row.Deleted) && (row.InDatabase))
                    {
                        WorkComments workComments = new WorkComments(null);
                        workComments.DeleteDirect(row.WorkID, row.RefID, row.COMPANY_ID);
                    }
                }
            }
        }
        // /////////////////////////////////////////////////////////////////////////////////////////////////////
        // STEP1 - BEGIN
        //
        // ////////////////////////////////////////////////////////////////////////
        // STEP1 - BEGIN - AUXILIAR EVENTS
        //
        protected void ddlFieldToUpdate_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Date fields
            if ((ddlFieldToUpdate.SelectedValue == "VideoInspection") || (ddlFieldToUpdate.SelectedValue == "PipeLocated") || (ddlFieldToUpdate.SelectedValue == "ServicesLocated") || (ddlFieldToUpdate.SelectedValue == "CoInstalled")
                || (ddlFieldToUpdate.SelectedValue == "BackfilledConcrete") || (ddlFieldToUpdate.SelectedValue == "BackfilledSoil") || (ddlFieldToUpdate.SelectedValue == "Grouted") || (ddlFieldToUpdate.SelectedValue == "Cored")
                || (ddlFieldToUpdate.SelectedValue == "Prepped") || (ddlFieldToUpdate.SelectedValue == "PreVideo") || (ddlFieldToUpdate.SelectedValue == "Measured") || (ddlFieldToUpdate.SelectedValue == "NoticeDelivered")
                || (ddlFieldToUpdate.SelectedValue == "InProcess") || (ddlFieldToUpdate.SelectedValue == "InStock") || (ddlFieldToUpdate.SelectedValue == "Delivered") || (ddlFieldToUpdate.SelectedValue == "LinerInstalled")
                || (ddlFieldToUpdate.SelectedValue == "FinalVideo") || (ddlFieldToUpdate.SelectedValue == "CoCutDown") || (ddlFieldToUpdate.SelectedValue == "FinalRestoration")
                || (ddlFieldToUpdate.SelectedValue == "DigRequiredPriorToLiningCompleted") || (ddlFieldToUpdate.SelectedValue == "DigRequiredAfterLiningCompleted") || (ddlFieldToUpdate.SelectedValue == "HoldClientIssueResolved")
                || (ddlFieldToUpdate.SelectedValue == "HoldLFSIssueResolved") || (ddlFieldToUpdate.SelectedValue == "LateralRequiresRoboticPrepCompleted")
               )
            {
                tkrdpValue.Visible = true;
                cbxValue.Visible = false;
                ddlCoPitLocationValue.Visible = false;
                tbxComments.Visible = false;
                ddlPrepType.Visible = false;
                ddlLinerType.Visible = false;
                tbxValue.Visible = false;
            }
            else
            {
                tbxValue.Visible = false;

                // String fields
                if (ddlFieldToUpdate.SelectedValue == "CoPitLocation")
                {
                    tkrdpValue.Visible = false;
                    cbxValue.Visible = false;
                    ddlCoPitLocationValue.Visible = true;
                    ddlPrepType.Visible = false;
                    ddlLinerType.Visible = false;
                    tbxComments.Visible = false;
                }

                // String fields
                if (ddlFieldToUpdate.SelectedValue == "PrepType")
                {
                    tkrdpValue.Visible = false;
                    cbxValue.Visible = false;
                    ddlCoPitLocationValue.Visible = false;
                    ddlPrepType.Visible = true;
                    ddlLinerType.Visible = false;
                    tbxComments.Visible = false;
                }

                // String fields
                if (ddlFieldToUpdate.SelectedValue == "LinerType")
                {
                    tkrdpValue.Visible = false;
                    cbxValue.Visible = false;
                    ddlCoPitLocationValue.Visible = false;
                    ddlPrepType.Visible = false;
                    ddlLinerType.Visible = true;
                    tbxComments.Visible = false;
                }

                // String fields
                if (ddlFieldToUpdate.SelectedValue == "ContractYear")
                {
                    tkrdpValue.Visible = false;
                    cbxValue.Visible = false;
                    ddlCoPitLocationValue.Visible = false;
                    ddlPrepType.Visible = false;
                    ddlLinerType.Visible = false;
                    tbxComments.Visible = false;
                    tbxValue.Visible = true;
                }

                // Boolean fields
                if ((ddlFieldToUpdate.SelectedValue == "CoRequired") || (ddlFieldToUpdate.SelectedValue == "OutOfScope") || (ddlFieldToUpdate.SelectedValue == "DigRequiredPriorToLining") || (ddlFieldToUpdate.SelectedValue == "DigRequiredAfterLining") || (ddlFieldToUpdate.SelectedValue == "HoldClientIssue")
                    || (ddlFieldToUpdate.SelectedValue == "HoldLFSIssue") || (ddlFieldToUpdate.SelectedValue == "LateralRequiresRoboticPrep"))
                {
                    tkrdpValue.Visible = false;
                    cbxValue.Visible = true;
                    ddlCoPitLocationValue.Visible = false;
                    ddlPrepType.Visible = false;
                    ddlLinerType.Visible = false;
                    tbxComments.Visible = false;
                }

                // Special fields - Comment
                if (ddlFieldToUpdate.SelectedValue == "Comment")
                {
                    tkrdpValue.Visible = false;
                    cbxValue.Visible = false;
                    ddlCoPitLocationValue.Visible = false;
                    ddlPrepType.Visible = false;
                    ddlLinerType.Visible = false;
                    tbxComments.Visible = true;

                    FlatSectionJlCommentDetailsGateway flatSectionJlCommentDetailsGateway = new FlatSectionJlCommentDetailsGateway(flatSectionJlTDS);
                    flatSectionJlCommentDetailsGateway.ClearBeforeFill = false;
                    FlatSectionJl flatSectionJl = new FlatSectionJl(flatSectionJlTDS);
                    DataView dataViewFlatSectionJl = new DataView(flatSectionJlTDS.FlatSectionJl);
                    dataViewFlatSectionJl.RowFilter = "(Selected = 1) AND (Deleted = 0)";

                    foreach (DataRowView row in dataViewFlatSectionJl)
                    {
                        int workId = Int32.Parse(row["WorkID"].ToString());
                        int companyId = Int32.Parse(Session["companyID"].ToString());

                        // Get comments
                        flatSectionJlCommentDetailsGateway.LoadAllByWorkIdWorkType(workId, companyId, "Junction Lining Lateral");
                    }
                }
            }
        }