private JlNavigatorTDS SubmitSearch()
        {
            // Retrieve clauses
            string whereClause = GetWhereClause();
            string orderByClause = GetOrderByClause();
            string conditionValue = "";
            string conditionValue2 = "";
            string workType = hdfWorkType.Value.Trim();
            int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
            int currentProjectId = Int32.Parse(hdfCurrentProjectId.Value.Trim());

            // ... Load data for condition 1
            WorkTypeViewConditionGateway workTypeViewConditionGateway = new WorkTypeViewConditionGateway();
            workTypeViewConditionGateway.LoadByWorkTypeConditionId(workType, int.Parse(ddlCondition1.SelectedValue), companyId);
            conditionValue = workTypeViewConditionGateway.GetColumn_(workType, companyId, int.Parse(ddlCondition1.SelectedValue));

            // ... If condition 2 exists
            if (ddlCondition2.SelectedValue != "-1")
            {
                // ... Load data for condition 2
                WorkTypeViewConditionGateway workTypeViewConditionGateway2 = new WorkTypeViewConditionGateway();
                workTypeViewConditionGateway2.LoadByWorkTypeConditionId(workType, int.Parse(ddlCondition2.SelectedValue), companyId);
                conditionValue2 = workTypeViewConditionGateway2.GetColumn_(workType, companyId, int.Parse(ddlCondition2.SelectedValue));
            }

            JlNavigator jlNavigator = new JlNavigator();
            jlNavigator.Load(whereClause, orderByClause, conditionValue, tbxCondition1.Text.Trim(), conditionValue2, tbxCondition2.Text.Trim(), companyId, currentProjectId, workType);

            return (JlNavigatorTDS)jlNavigator.Data;
        }
        private JlNavigatorTDS SubmitSearchForViews()
        {
            string sqlCommand = "";
            int viewId = Int32.Parse(ddlView.SelectedValue.Trim());

            JlNavigator jlNavigator = new JlNavigator();
            string workType = "Junction Lining Lateral";
            int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
            int currentProjectId = Int32.Parse(hdfCurrentProjectId.Value.Trim());

            // ... Load SqlCommand
            WorkViewGateway workViewGateway = new WorkViewGateway();
            workViewGateway.LoadByViewId(viewId, companyId);

            sqlCommand = workViewGateway.GetSqlCommand(viewId);

            // ... Load data
            jlNavigator.LoadForViewsProjectIdCompanyIdWorkType(sqlCommand, currentProjectId, companyId, workType);

            return (JlNavigatorTDS)jlNavigator.Data;
        }
        private void UpdateDatabase()
        {
            DB.Open();
            DB.BeginTransaction();
            try
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);

                // Update Comments and History
                FlatSectionJlCommentDetails flatSectionJlCommentDetails = new FlatSectionJlCommentDetails(flatSectionJlTDS);
                flatSectionJlCommentDetails.Save(companyId);

                FlatSectionJlHistoryDetails flatSectionJlHistoryDetails = new FlatSectionJlHistoryDetails(flatSectionJlTDS);
                flatSectionJlHistoryDetails.Save(companyId);

                // Update FlatSectionJl for new Comments
                int workId = Int32.Parse(hdfWorkId.Value.Trim());
                string workType = "Junction Lining Lateral";

                FlatSectionJl flatSectionJl = new FlatSectionJl(flatSectionJlTDS);
                flatSectionJl.UpdateCommentsHistoryForSummaryEdit(workId, workType, companyId);

                JlNavigator jlNavigator = new JlNavigator(jlNavigatorTDS);
                jlNavigator.UpdateComments(workId, workType, companyId);

                Session["flatSectionJlTDS"] = flatSectionJlTDS;
                Session["jlNavigatorTDS"] = jlNavigatorTDS;

                DB.CommitTransaction();
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        protected void btnPrintLateralLocationSheet_Click(object sender, EventArgs e)
        {
            string url = "";
            string name = "";
            string client = "";
            string project = "";
            string title = "Junction Lining Search Results";
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int projectId = Int32.Parse(hdfCurrentProjectId.Value);

            // ... for client
            int currentClientId = Int32.Parse(hdfCurrentClientId.Value.ToString());
            CompaniesGateway companiesGateway = new CompaniesGateway();
            companiesGateway.LoadByCompaniesId(currentClientId, companyId);
            client += "Client: " + companiesGateway.GetName(currentClientId);

            // ... for project
            int currentProjectId = Int32.Parse(hdfCurrentProjectId.Value.ToString());
            ProjectGateway projectGateway = new ProjectGateway();
            projectGateway.LoadByProjectId(currentProjectId);
            project = projectGateway.GetName(currentProjectId);
            name = client + " > Project: " + project + " (" + projectGateway.GetProjectNumber(currentProjectId) + ")";

            // ... for title view
            if (hdfBtnOrigin.Value == "Go")
            {
                int viewId = Int32.Parse(ddlView.SelectedValue.Trim());

                // ... Load name view
                WorkViewGateway workViewGateway = new WorkViewGateway();
                workViewGateway.LoadByViewId(viewId, companyId);

                title = workViewGateway.GetName(viewId);
            }

            // ... For comments and history options
            JlNavigator jlNavigator = new JlNavigator(jlNavigatorTDS);

            // Report call
            Page.Validate();
            if (Page.IsValid)
            {
                PostPageChanges();

                int assetId = GetFirstSectionAssetId();
                if (assetId > 0)
                {
                    Response.Write("<script language='javascript'> {window.open('./../../CWP/Common/jl_lateral_location_sheet_report.aspx?source_page=lm&project_id=" + hdfCurrentProjectId.Value + "&client_id=" + hdfCurrentClientId.Value + "', '_blank', 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=800, height=680');}</script>");
                }
                else
                {
                    cvSelection.IsValid = false;
                }
            }

            if (url != "") Response.Redirect(url);
        }
        protected void btnPreviewList_Click(object sender, EventArgs e)
        {
            string url = "";
            string headerValues = "";
            int totalColumnsPreview = 57;
            int totalColumnsExport = 59;
            string name = "";
            string client = "";
            string project = "";
            string title = "Junction Lining Search Results";
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int projectId = Int32.Parse(hdfCurrentProjectId.Value);

            // ... for client
            int currentClientId = Int32.Parse(hdfCurrentClientId.Value.ToString());
            CompaniesGateway companiesGateway = new CompaniesGateway();
            companiesGateway.LoadByCompaniesId(currentClientId, companyId);
            client += "Client: " + companiesGateway.GetName(currentClientId);

            // ... for project
            int currentProjectId = Int32.Parse(hdfCurrentProjectId.Value.ToString());
            ProjectGateway projectGateway = new ProjectGateway();
            projectGateway.LoadByProjectId(currentProjectId);
            project = projectGateway.GetName(currentProjectId);
            name = client + " > Project: " + project + " (" + projectGateway.GetProjectNumber(currentProjectId) + ")";

            // ... for title view
            if (hdfBtnOrigin.Value == "Go")
            {
                int viewId = Int32.Parse(ddlView.SelectedValue.Trim());

                // ... Load name view
                WorkViewGateway workViewGateway = new WorkViewGateway();
                workViewGateway.LoadByViewId(viewId, companyId);

                title = workViewGateway.GetName(viewId);
            }

            // ... For comments and history options
            string comments = "None";
            string history = "None";
            JlNavigator jlNavigator = new JlNavigator(jlNavigatorTDS);

            // Update for comments
            jlNavigator.UpdateEnterForReport(projectId, companyId);

            // Establishing header values
            if (grdJLNavigator.Columns[2].Visible) headerValues += "LateralID";
            if (grdJLNavigator.Columns[3].Visible) headerValues += " * Old CWP ID";
            if (grdJLNavigator.Columns[4].Visible) headerValues += " * Client Lateral ID";
            if (grdJLNavigator.Columns[5].Visible) headerValues += " * Hamilton Inspection Number";
            if (grdJLNavigator.Columns[6].Visible) headerValues += " * Street";
            if (grdJLNavigator.Columns[7].Visible) headerValues += " * MN";
            if (grdJLNavigator.Columns[8].Visible) headerValues += " * USMH";
            if (grdJLNavigator.Columns[9].Visible) headerValues += " * DSMH";
            if (grdJLNavigator.Columns[10].Visible) headerValues += " * V1 Inspection";
            if (grdJLNavigator.Columns[11].Visible) headerValues += " * Video Length To PL";
            if (grdJLNavigator.Columns[12].Visible) headerValues += " * Depth Of Pipe";
            if (grdJLNavigator.Columns[13].Visible) headerValues += " * Pipe Located";
            if (grdJLNavigator.Columns[14].Visible) headerValues += " * Services Located";
            if (grdJLNavigator.Columns[15].Visible) headerValues += " * Prep Type";
            if (grdJLNavigator.Columns[16].Visible) headerValues += " * C/O Req.";
            if (grdJLNavigator.Columns[17].Visible) headerValues += " * Pit Req.";
            if (grdJLNavigator.Columns[18].Visible) headerValues += " * CO/Pit Location";
            if (grdJLNavigator.Columns[19].Visible) headerValues += " * C/O Installed";
            if (grdJLNavigator.Columns[20].Visible) headerValues += " * Backfilled Con.";
            if (grdJLNavigator.Columns[21].Visible) headerValues += " * Backfilled Soil";
            if (grdJLNavigator.Columns[22].Visible) headerValues += " * Grouted";
            if (grdJLNavigator.Columns[23].Visible) headerValues += " * Cored";
            if (grdJLNavigator.Columns[24].Visible) headerValues += " * Prepped";
            if (grdJLNavigator.Columns[25].Visible) headerValues += " * Pre-Video";
            if (grdJLNavigator.Columns[26].Visible) headerValues += " * Measured";
            if (grdJLNavigator.Columns[27].Visible) headerValues += " * Connection Type";
            if (grdJLNavigator.Columns[28].Visible) headerValues += " * Liner Type";
            if (grdJLNavigator.Columns[29].Visible) headerValues += " * Flange";
            if (grdJLNavigator.Columns[30].Visible) headerValues += " * Gasket";
            if (grdJLNavigator.Columns[31].Visible) headerValues += " * Main Size";
            if (grdJLNavigator.Columns[32].Visible) headerValues += " * Liner Size";
            if (grdJLNavigator.Columns[33].Visible) headerValues += " * Lining Thru C/O";
            if (grdJLNavigator.Columns[34].Visible) headerValues += " * Notice Delivered";
            if (grdJLNavigator.Columns[35].Visible) headerValues += " * In Process";
            if (grdJLNavigator.Columns[36].Visible) headerValues += " * In Stock";
            if (grdJLNavigator.Columns[37].Visible) headerValues += " * Delivered";
            if (grdJLNavigator.Columns[38].Visible) headerValues += " * Liner Installed";
            if (grdJLNavigator.Columns[39].Visible) headerValues += " * Final Video";
            if (grdJLNavigator.Columns[40].Visible) headerValues += " * Dist. From USMH";
            if (grdJLNavigator.Columns[41].Visible) headerValues += " * Dist. From DSMH";
            if (grdJLNavigator.Columns[42].Visible) headerValues += " * Post Contract Dig?";
            if (grdJLNavigator.Columns[43].Visible) headerValues += " * C/O Cut Down?";
            if (grdJLNavigator.Columns[44].Visible) headerValues += " * Final Restoration";
            if (grdJLNavigator.Columns[45].Visible) headerValues += " * Cost";
            if (grdJLNavigator.Columns[46].Visible) headerValues += " * Build / Rebuild";
            if (grdJLNavigator.Columns[49].Visible) headerValues += " * Dig Required Prior To Lining";
            if (grdJLNavigator.Columns[50].Visible) headerValues += " * Dig Required Prior To Lining Completed";
            if (grdJLNavigator.Columns[51].Visible) headerValues += " * Dig Required After Lining";
            if (grdJLNavigator.Columns[52].Visible) headerValues += " * Dig Required After Lining Completed";
            if (grdJLNavigator.Columns[53].Visible) headerValues += " * Out Of Scope";
            if (grdJLNavigator.Columns[54].Visible) headerValues += " * Hold - Client Issue";
            if (grdJLNavigator.Columns[55].Visible) headerValues += " * Hold - Client Issue Resolved";
            if (grdJLNavigator.Columns[56].Visible) headerValues += " * Hold - LFS Issue";
            if (grdJLNavigator.Columns[57].Visible) headerValues += " * Hold - LFS Issue Resolved";
            if (grdJLNavigator.Columns[58].Visible) headerValues += " * Requires Robotic Prep";
            if (grdJLNavigator.Columns[59].Visible) headerValues += " * Requires Robotic Prep Completed";

            // Establishing columns to display
            string[] columns = headerValues.Split('*');
            string columnsForReport = "";
            int j;

            // ... for visible columns
            for (int i = 0; i < columns.Length; i++)
            {
                j = i + 1;
                columnsForReport += "&header" + j + "=" + columns[i].Trim();
            }

            // ... For not visible columns
            for (int i = columns.Length; i < totalColumnsPreview; i++)
            {
                j = i + 1;
                columnsForReport += "&header" + j + "=None";
            }

            if (grdJLNavigator.Columns[47].Visible) { comments = "Comments"; }
            if (grdJLNavigator.Columns[48].Visible) { history = "History"; }

            // Report call
            Page.Validate();
            if (Page.IsValid)
            {
                PostPageChanges();
                title = title.Replace("'", "%27");
                Response.Write("<script language='javascript'> {window.open('./jl_print_search_results_report.aspx?" + columnsForReport + "&comments=" + comments + "&history=" + history + "&totalColumnsPreview=" + totalColumnsPreview + "&totalColumnsExport=" + totalColumnsExport + "&totalSelectedColumns=" + columns.Length + "&name=" + Server.UrlEncode(name) + "&title=" + Server.UrlEncode(title) + "&format=pdf', '_blank', 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=800, height=680');}</script>");
            }

            if (url != "") Response.Redirect(url);
        }
        private void PostPageChanges()
        {
            JlNavigator jlNavigator = new JlNavigator(jlNavigatorTDS);

            // Update jlNavigator rows
            foreach (GridViewRow row in grdJLNavigator.Rows)
            {
                string assetIdLabel = ((Label)row.FindControl("lblAssetID")).Text.Trim();
                int assetId = Int32.Parse(assetIdLabel.ToString().Trim());
                bool selected = ((CheckBox)row.FindControl("cbxSelected")).Checked;

                jlNavigator.Update(assetId, selected);
            }

            jlNavigator.Data.AcceptChanges();

            // Store datasets
            Session["jlNavigatorTDS"] = jlNavigatorTDS;
        }
        // ////////////////////////////////////////////////////////////////////////
        // 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"])))
                {
                    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["work_type"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in jl_navigator2.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfCurrentProjectId.Value = Request.QueryString["project_id"].ToString();
                hdfCurrentClientId.Value = Request.QueryString["client_id"].ToString();
                hdfWorkType.Value = Request.QueryString["work_type"].ToString();

                // Prepare initial data
                // ... For sortByList
                odsSortByList.DataBind();
                ddlSortBy.DataSourceID = "odsSortByList";
                ddlSortBy.DataValueField = "SortID";
                ddlSortBy.DataTextField = "Name";
                ddlSortBy.DataBind();

                // ... For
                odsViewForDisplayList.DataBind();
                odsViewForDisplayList2.DataBind();

                // ... For view ddl
                int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                string workType = hdfWorkType.Value;
                int loginId = Convert.ToInt32(Session["loginID"]);
                string viewTypeGlobal = "";
                string viewTypePersonal = "Personal";

                // Global Views check
                if (Convert.ToBoolean(Session["sgLFS_GLOBALVIEWS_VIEW"]))
                {
                    viewTypeGlobal = "Global";
                }

                WorkViewList workViewList = new WorkViewList();
                workViewList.LoadAndAddItem(workType, viewTypeGlobal, viewTypePersonal, loginId, companyId);
                ddlView.DataSource = workViewList.Table;
                ddlView.DataValueField = "ViewID";
                ddlView.DataTextField = "Name";
                ddlView.DataBind();
                ddlView.SelectedIndex = 1;

                WorkTypeViewSubAreaList workTypeViewSubAreaList = new WorkTypeViewSubAreaList();
                workTypeViewSubAreaList.LoadAndAddItem("Junction Lining", Int32.Parse(hdfCompanyId.Value), int.Parse(hdfCurrentProjectId.Value), "(All)");
                ddlSubArea.DataSource = workTypeViewSubAreaList.Table;
                ddlSubArea.DataValueField = "SubArea";
                ddlSubArea.DataTextField = "SubArea";
                ddlSubArea.DataBind();

                // ... for client
                int currentClientId = Int32.Parse(hdfCurrentClientId.Value.ToString());

                CompaniesGateway companiesGateway = new CompaniesGateway();
                companiesGateway.LoadByCompaniesId(currentClientId, companyId);
                lblTitleClientName.Text = "Client: " + companiesGateway.GetName(currentClientId);

                // ... for project
                int currentProjectId = Int32.Parse(hdfCurrentProjectId.Value.ToString());
                ProjectGateway projectGateway = new ProjectGateway();
                projectGateway.LoadByProjectId(currentProjectId);
                string name = projectGateway.GetName(currentProjectId);
                if (name.Length > 23) name = name.Substring(0, 20) + "...";
                lblTitleProjectName.Text = " > Project: " + name + " (" + projectGateway.GetProjectNumber(currentProjectId) + ")";

                // If coming from
                // ... jl_navigator.aspx or jl_navigator2.aspx
                if ((Request.QueryString["source_page"] == "jl_navigator.aspx") || (Request.QueryString["source_page"] == "jl_navigator2.aspx"))
                {
                    // Restore data
                    RestoreNavigatorState();
                    jlNavigatorTDS = (JlNavigatorTDS)Session["jlNavigatorTDS"];
                }

                // ... flat_section_jl_edit.aspx, flat_section_jl_summary.aspx or jl_delete.aspx
                if ((Request.QueryString["source_page"] == "flat_section_jl_edit.aspx") || (Request.QueryString["source_page"] == "flat_section_jl_summary.aspx") || (Request.QueryString["source_page"] == "jl_delete.aspx"))
                {
                    RestoreNavigatorState();

                    if (Request.QueryString["update"] == "no")
                    {
                        jlNavigatorTDS = (JlNavigatorTDS)Session["jlNavigatorTDS"];
                    }
                    else
                    {
                        // ... Delete store data
                        Session.Contents.Remove("jlNavigatorTDS");

                        // ... Search data with updates
                        if (hdfBtnOrigin.Value == "Search")
                        {
                            jlNavigatorTDS = SubmitSearch();
                        }
                        else
                        {
                            if (hdfBtnOrigin.Value == "Go")
                            {
                                jlNavigatorTDS = SubmitSearchForViews();
                            }
                        }

                        // ... store datasets
                        Session["jlNavigatorTDS"] = jlNavigatorTDS;
                    }
                }

                // ... jl_delete.aspx, flat_section_jl_summary.aspx or flat_section_jl_edit.aspx
                if ((Request.QueryString["source_page"] == "jl_delete.aspx") || (Request.QueryString["source_page"] == "flat_section_jl_summary.aspx") || (Request.QueryString["source_page"] == "flat_section_jl_edit.aspx"))
                {
                    if (jlNavigatorTDS.JlNavigator.Rows.Count == 0)
                    {
                        string url = "./jl_navigator.aspx?source_page=jl_navigator2.aspx&client_id=" + hdfCurrentClientId.Value + "&project_id=" + hdfCurrentProjectId.Value + "&work_type=" + hdfWorkType.Value + GetNavigatorState() + "&no_results=yes";
                        Response.Redirect(url);
                    }
                }

                // Update for comments
                JlNavigator jlNavigator = new JlNavigator(jlNavigatorTDS);
                jlNavigator.UpdateEnterForComments();

                // Store data
                Session["jlNavigatorTDS"] = jlNavigatorTDS;

                // For the grid
                grdJLNavigator.DataSource = jlNavigatorTDS.JlNavigator;
                grdJLNavigator.DataBind();

                // ... for the total rows
                if (jlNavigatorTDS.JlNavigator.Rows.Count > 0)
                {
                    lblTotalRows.Text = "Total Rows: " + jlNavigatorTDS.JlNavigator.Rows.Count;
                    lblTotalRows.Visible = true;
                }
                else
                {
                    lblTotalRows.Visible = false;
                }
            }
            else
            {
                // Restore TDS
                jlNavigatorTDS = (JlNavigatorTDS)Session["jlNavigatorTDS"];

                // Update for comments
                JlNavigator jlNavigator = new JlNavigator(jlNavigatorTDS);
                jlNavigator.UpdateEnterForComments();

                // ... for the total rows
                if (jlNavigatorTDS.JlNavigator.Rows.Count > 0)
                {
                    lblTotalRows.Text = "Total Rows: " + jlNavigatorTDS.JlNavigator.Rows.Count;
                    lblTotalRows.Visible = true;
                }
                else
                {
                    lblTotalRows.Visible = false;
                }
            }
        }