// ////////////////////////////////////////////////////////////////////////
        // 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);
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // 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;
            }
        }
        private void PostPageChanges()
        {
            JlLiningPlanJliner jlLiningPlanJliner = new JlLiningPlanJliner(jlLiningPlanTDS);

            foreach (GridViewRow row in grdLiningPlanLiners.Rows)
            {
                int assetId = int.Parse(((HiddenField)row.FindControl("hdfAssetId")).Value);
                int companyId = int.Parse(((HiddenField)row.FindControl("hdfCompanyId")).Value);
                int section_ = int.Parse(((HiddenField)row.FindControl("hdfSectionId")).Value);
                int selected = Int32.Parse(((DropDownList)row.FindControl("ddlSelected")).SelectedValue);

                jlLiningPlanJliner.Update(assetId, section_, companyId, selected);
            }

            // Store datasets
            Session["jlLiningPlanTDS"] = jlLiningPlanTDS;
        }
        /// <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);
                }
            }
        }