// ////////////////////////////////////////////////////////////////////////
        // PUBLIC METHODS
        //
        /// <summary>
        /// Load liners
        /// </summary>
        /// <param name="jlLiningPlanTDS">jlLiningPlanTDS</param>       
        /// <param name="companyId">companyId</param>
        public void LoadLiners(JlLiningPlanTDS jlLiningPlanTDS, int companyId)
        {
            JlLiningPlanJlinerGateway jlLiningPlanJlinerGateway = new JlLiningPlanJlinerGateway(jlLiningPlanTDS.JlLiningPlanJliner.DataSet);
            jlLiningPlanJlinerGateway.ClearBeforeFill = false;
            JlLiningPlanJliner jlLiningPlanJliner = new JlLiningPlanJliner(jlLiningPlanTDS.JlLiningPlanJliner.DataSet);

            foreach (JlLiningPlanTDS.JlLiningPlanRow jlLiningPlanRow in jlLiningPlanTDS.JlLiningPlan.Rows)
            {
                if (jlLiningPlanRow.Selected != "9")
                {
                    string linerMN = jlLiningPlanRow.LinerMN;

                    jlLiningPlanJlinerGateway.LoadByAssetIdMnSelected(jlLiningPlanRow.AssetID, jlLiningPlanRow.COMPANY_ID, linerMN, jlLiningPlanRow.Selected);
                    jlLiningPlanJliner.UpdateForReport(companyId);
                }
            }
        }
 /// <summary>
 /// InitData
 /// </summary>
 protected override void InitData()
 {
     _data = new JlLiningPlanTDS();
 }
        // ////////////////////////////////////////////////////////////////////////
        // 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))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in jl_lining_plan.aspx");
                }

                // Tag Page
                hdfGeneralCompanyId.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 client
                int currentClientId = Int32.Parse(hdfCurrentClientId.Value.ToString());
                int companyId = Int32.Parse(Session["companyID"].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) + ")";

                // ... for the grid
                jlLiningPlanTDS = new JlLiningPlanTDS();
                JlLiningPlanGateway jlLiningPlanGateway = new JlLiningPlanGateway(jlLiningPlanTDS);
                jlLiningPlanGateway.ClearBeforeFill = false;
                jlLiningPlanGateway.LoadByProjectIdIssueWithLateralsNoOutOfScope(int.Parse(hdfGeneralCompanyId.Value), int.Parse(hdfCurrentProjectId.Value));
                jlLiningPlanGateway.LoadByProjectIdOtherIssueWithLaterals(int.Parse(hdfGeneralCompanyId.Value), int.Parse(hdfCurrentProjectId.Value));

                grdLiningPlan.DataSource = jlLiningPlanGateway.Table;
                grdLiningPlan.DataBind();

                // Check results
                if (jlLiningPlanTDS.JlLiningPlan.Rows.Count > 0)
                {
                    tPreview.Visible = true;
                    tdNoResults.Visible = false;
                    tbFooterToolbar.Visible = true;
                }
                else
                {
                    tPreview.Visible = false;
                    tdNoResults.Visible = true;
                    tbFooterToolbar.Visible = false;
                }

                // Check results
                if (jlLiningPlanTDS.JlLiningPlan.Rows.Count == 1)
                {
                    tbFooterToolbar.Visible = false;
                }

                // Store dataset
                Session["jlLiningPlanTDS"] = jlLiningPlanTDS;
            }
            else
            {
                // Restore dataset
                jlLiningPlanTDS = (JlLiningPlanTDS)Session["jlLiningPlanTDS"];

                // ... for the grid
                JlLiningPlanGateway jlLiningPlanGateway = new JlLiningPlanGateway(jlLiningPlanTDS);
                grdLiningPlan.DataSource = jlLiningPlanGateway.Table;
                grdLiningPlan.DataBind();
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // 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["work_type"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in jl_lining_plan_laterals.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
                Session.Remove("jlLinersDummy");

                // If coming from
                if (Request.QueryString["source_page"] == "jl_lining_plan.aspx")
                {
                    // ... for the grid
                    jlLiningPlanTDS = (JlLiningPlanTDS)Session["jlLiningPlanTDS"];
                    jlLiners = jlLiningPlanTDS.JlLiningPlanJliner;

                    // Load data
                    int companyId = Int32.Parse(hdfCompanyId.Value);
                    JlLiningPlanJliner jlLiningPlanJliner  =  new JlLiningPlanJliner();
                    jlLiningPlanJliner.LoadLiners(jlLiningPlanTDS, companyId);

                    jlLiningPlanTDS.JlLiningPlanJliner.DefaultView.Sort = " ParentInstallOrder DESC";

                    // Store dataset
                    Session["jlLiningPlanTDS"] = jlLiningPlanTDS;
                    jlLiners = jlLiningPlanTDS.JlLiningPlanJliner;
                    Session["jlLiners"] = jlLiners;
                    grdLiningPlanLiners.DataBind();

                    // Check results
                    JlLiningPlanJlinerGateway jlLiningPlanJlinerGateway = new JlLiningPlanJlinerGateway(jlLiningPlanTDS);
                    if (jlLiningPlanJlinerGateway.Table.Rows.Count > 0)
                    {
                        tdNoResults.Visible = false;
                        btnPreview.Visible = true;
                        btnOrder.Visible = true;
                        ddlOrder.Visible = true;
                        lblOrder.Visible = true;
                    }
                    else
                    {
                        tdNoResults.Visible = true;
                        btnPreview.Visible = false;
                        btnOrder.Visible = false;
                        ddlOrder.Visible = false;
                        lblOrder.Visible = false;
                    }
                }
            }
            else
            {
                // Restore datasets
                jlLiningPlanTDS = (JlLiningPlanTDS)Session["jlLiningPlanTDS"];
                jlLiners = jlLiningPlanTDS.JlLiningPlanJliner;

                // Store
                Session["jlLiners"] = jlLiningPlanTDS.JlLiningPlanJliner;
            }
        }
        /// <summary>
        /// ProcessForReport
        /// </summary>
        /// <param name="jlLiningPlanTDS">TDS for process</param>
        public void ProcessForReport(JlLiningPlanTDS jlLiningPlanTDS)
        {
            JlLiningPlanJlinerGateway jlLiningPlanJlinerGateway = new JlLiningPlanJlinerGateway(Data);
            jlLiningPlanJlinerGateway.ClearBeforeFill = false;
            JlLiningPlanJliner jlLiningPlanJliner = new JlLiningPlanJliner(Data);

            foreach (JlLiningPlanTDS.JlLiningPlanRow jlLiningPlanRow in jlLiningPlanTDS.JlLiningPlan.Rows)
            {
                if (jlLiningPlanRow.Selected != "9")
                {
                    // Create row for report
                    JlLiningPlanTDS.JlLiningPlanRow newRow = ((JlLiningPlanTDS.JlLiningPlanDataTable)Table).NewJlLiningPlanRow();

                    newRow.AssetID = jlLiningPlanRow.AssetID;
                    newRow.COMPANY_ID = jlLiningPlanRow.COMPANY_ID;
                    newRow.SectionID = jlLiningPlanRow.SectionID;
                    if (!jlLiningPlanRow.IsStreetNull()) newRow.Street = jlLiningPlanRow.Street;
                    if (!jlLiningPlanRow.IsSize_Null())
                    {
                        if (Distance.IsValidDistance(jlLiningPlanRow.Size_))
                        {
                            Distance distance = new Distance(jlLiningPlanRow.Size_);

                            switch (distance.DistanceType)
                            {
                                case 2:
                                    jlLiningPlanRow.Size_ = distance.ToStringInEng1();
                                    break;
                                case 3:
                                    if (Convert.ToDouble(jlLiningPlanRow.Size_) > 99)
                                    {
                                        double newSize_ = 0;
                                        newSize_ = Convert.ToDouble(jlLiningPlanRow.Size_) * 0.03937;
                                        jlLiningPlanRow.Size_ = Convert.ToString(Math.Ceiling(newSize_)) + "\"";
                                    }
                                    else
                                    {
                                        jlLiningPlanRow.Size_ = jlLiningPlanRow.Size_ + "\"";
                                    }
                                    break;
                                case 4:
                                    jlLiningPlanRow.Size_ = distance.ToStringInEng1();
                                    break;
                                case 5:
                                    jlLiningPlanRow.Size_ = distance.ToStringInEng1();
                                    break;
                            }
                        }
                    }
                    if (!jlLiningPlanRow.IsStandardBypassNull()) newRow.StandardBypass = jlLiningPlanRow.StandardBypass;
                    if (!jlLiningPlanRow.IsTrafficControlNull()) newRow.TrafficControl = jlLiningPlanRow.TrafficControl;
                    if (!jlLiningPlanRow.IsNumLatsNull()) newRow.NumLats = jlLiningPlanRow.NumLats;
                    if (!jlLiningPlanRow.IsNotLinedYetNull()) newRow.NotLinedYet = jlLiningPlanRow.NotLinedYet;
                    if (!jlLiningPlanRow.IsLengthNull()) newRow.Length = jlLiningPlanRow.Length;
                    if (!jlLiningPlanRow.IsUSMHNull()) newRow.USMH = jlLiningPlanRow.USMH;
                    if (!jlLiningPlanRow.IsDSMHNull()) newRow.DSMH = jlLiningPlanRow.DSMH;
                    if (!jlLiningPlanRow.IsAllMeasuredNull()) newRow.AllMeasured = jlLiningPlanRow.AllMeasured;
                    if (!jlLiningPlanRow.IsDate_Null()) newRow.Date_ = jlLiningPlanRow.Date_;
                    if (!jlLiningPlanRow.IsFlusherNull()) newRow.Flusher = jlLiningPlanRow.Flusher;
                    if (!jlLiningPlanRow.IsFlusherMNNull()) newRow.FlusherMN = jlLiningPlanRow.FlusherMN;
                    if (!jlLiningPlanRow.IsLinerNull()) newRow.Liner = jlLiningPlanRow.Liner;
                    if (!jlLiningPlanRow.IsLinerMNNull()) newRow.LinerMN = jlLiningPlanRow.LinerMN;
                    if (!jlLiningPlanRow.IsRotatorNull()) newRow.Rotator = jlLiningPlanRow.Rotator;
                    if (!jlLiningPlanRow.IsRotatorMNNull()) newRow.RotatorMN = jlLiningPlanRow.RotatorMN;
                    if (!jlLiningPlanRow.IsCompressorNull()) newRow.Compressor = jlLiningPlanRow.Compressor;
                    if (!jlLiningPlanRow.IsCompressorMNNull()) newRow.CompressorMN = jlLiningPlanRow.CompressorMN;
                    if (!jlLiningPlanRow.IsUSMHDescriptionNull()) newRow.USMHDescription = jlLiningPlanRow.USMHDescription; else newRow.SetUSMHDescriptionNull();
                    if (!jlLiningPlanRow.IsDSMHDescriptionNull()) newRow.DSMHDescription = jlLiningPlanRow.DSMHDescription; else newRow.SetDSMHDescriptionNull();
                    if (!jlLiningPlanRow.IsUSMHAddressNull()) newRow.USMHAddress = jlLiningPlanRow.USMHAddress; else newRow.SetUSMHAddressNull();
                    if (!jlLiningPlanRow.IsDSMHAddressNull()) newRow.DSMHAddress = jlLiningPlanRow.DSMHAddress; else newRow.SetDSMHAddressNull();
                    newRow.Selected = jlLiningPlanRow.Selected;
                    newRow.FlowOrderID = jlLiningPlanRow.FlowOrderID;
                    newRow.StandardBypassComments = jlLiningPlanRow.StandardBypassComments;
                    newRow.TrafficControlDetails = jlLiningPlanRow.TrafficControlDetails;

                    ((JlLiningPlanTDS.JlLiningPlanDataTable)Table).AddJlLiningPlanRow(newRow);

                    // Select jliners for report
                    foreach (JlLiningPlanTDS.JlLiningPlanJlinerRow jlLiningPlanJlinerRow in jlLiningPlanTDS.JlLiningPlanJliner.Rows)
                    {
                        if (jlLiningPlanJlinerRow.Selected != 30)
                        {
                            jlLiningPlanJlinerGateway.LoadBySection_AssetIdAndLateralOrder(jlLiningPlanRow.AssetID, jlLiningPlanRow.COMPANY_ID, newRow.LinerMN, jlLiningPlanJlinerRow.Selected, jlLiningPlanRow.Selected, jlLiningPlanJlinerRow.AssetID);
                        }
                    }

                    jlLiningPlanJliner.UpdateForReport(jlLiningPlanRow.COMPANY_ID);
                }
            }
        }