/// ////////////////////////////////////////////////////////////////////////
        /// PUBLIC METHODS
        ///
        public ViewFullLengthLiningTDS.LfsM2TablesDataTable GetLfsM2New()
        {
            lfsM2Tables = (ViewFullLengthLiningTDS.LfsM2TablesDataTable)Session["lfsM2TablesDummy"];
            if (lfsM2Tables == null)
            {
                lfsM2Tables = (ViewFullLengthLiningTDS.LfsM2TablesDataTable)Session["lfsM2Tables"];
            }

            return lfsM2Tables;
        }
        protected void AddLfsM2TablesNewEmptyFix(GridView grdView)
        {
            if (grdView.Rows.Count == 0)
            {
                ViewFullLengthLiningTDS.LfsM2TablesDataTable dt = new ViewFullLengthLiningTDS.LfsM2TablesDataTable();
                dt.AddLfsM2TablesRow(Guid.NewGuid(), -1, -1, -1, "", "", "", "", "", "", "", "", false, false, false);
                Session["lfsM2TablesDummy"] = dt;

                grdView.DataBind();
            }

            // normally executes at all postbacks
            if (grdView.Rows.Count == 1)
            {
                ViewFullLengthLiningTDS.LfsM2TablesDataTable dt = (ViewFullLengthLiningTDS.LfsM2TablesDataTable)Session["lfsM2TablesDummy"];
                if (dt != null)
                {
                    grdView.Rows[0].Visible = false;
                    grdView.Rows[0].Controls.Clear();
                }
            }
        }
        private void GrdLfsM2TablesAdd()
        {
            if (ValidateFooter())
            {
                Page.Validate("dataFooter");
                if (Page.IsValid)
                {
                    Guid id = new Guid(Request.QueryString["id"].ToString());
                    int companyId = Convert.ToInt32(Session["companyID"]);

                    float? videoDistance = null; if(((TextBox)grdLfsM2.FooterRow.FindControl("tbxVideoDistanceFooter")).Text != "") videoDistance = Single.Parse(((TextBox)grdLfsM2.FooterRow.FindControl("tbxVideoDistanceFooter")).Text.Trim());
                    string clockPosition = ""; if (((TextBox)grdLfsM2.FooterRow.FindControl("tbxClockPositionFooter")).Text.Trim() != "") clockPosition = ((TextBox)grdLfsM2.FooterRow.FindControl("tbxClockPositionFooter")).Text.Trim();
                    string liveOrAbandoned = ""; if (((TextBox)grdLfsM2.FooterRow.FindControl("tbxLiveOrAbandonedFooter")).Text.Trim() != "") liveOrAbandoned = ((TextBox)grdLfsM2.FooterRow.FindControl("tbxLiveOrAbandonedFooter")).Text.Trim();
                    string distanceToCentreOfLateral = ""; if (((TextBox)grdLfsM2.FooterRow.FindControl("tbxDistanceToCentreFooter")).Text.Trim() != "") distanceToCentreOfLateral = ((TextBox)grdLfsM2.FooterRow.FindControl("tbxDistanceToCentreFooter")).Text.Trim();
                    string lateralDiameter = ""; if (((TextBox)grdLfsM2.FooterRow.FindControl("tbxLateralDiameterFooter")).Text.Trim() != "") lateralDiameter = ((TextBox)grdLfsM2.FooterRow.FindControl("tbxLateralDiameterFooter")).Text.Trim();
                    string lateralType = ""; if (((TextBox)grdLfsM2.FooterRow.FindControl("tbxLateralTypeFooter")).Text.Trim() != "") lateralType = ((TextBox)grdLfsM2.FooterRow.FindControl("tbxLateralTypeFooter")).Text.Trim();
                    string dateTimeOpened = ""; if (((TextBox)grdLfsM2.FooterRow.FindControl("tbxTimeOpenedFooter")).Text.Trim() != "") dateTimeOpened = ((TextBox)grdLfsM2.FooterRow.FindControl("tbxTimeOpenedFooter")).Text.Trim();
                    string comments = ""; if (((TextBox)grdLfsM2.FooterRow.FindControl("tbxCommentFooter")).Text.Trim() != "") comments = ((TextBox)grdLfsM2.FooterRow.FindControl("tbxCommentFooter")).Text.Trim();
                    string reverseSetup = "";
                    if (distanceToCentreOfLateral != "")
                    {
                        TDSLFSRecord.LFS_MASTER_AREARow lfsMasterAreaRow = tdsLfsRecord.LFS_MASTER_AREA.FindByIDCOMPANY_ID(new Guid(tbxID.Text), Convert.ToInt32(Session["companyID"]));
                        reverseSetup = Distance.Subtract(lfsMasterAreaRow.IsActualLengthNull() ? "" : lfsMasterAreaRow.ActualLength, distanceToCentreOfLateral);
                    }

                    bool archived = false;
                    bool deleted = false;
                    bool inDatabase = false;

                    ViewFullLengthLiningLfsM2Tables model = new ViewFullLengthLiningLfsM2Tables(viewFullLengthLiningTDS);
                    model.Insert(id, companyId, videoDistance, clockPosition, liveOrAbandoned, distanceToCentreOfLateral, lateralDiameter, lateralType, dateTimeOpened, comments, reverseSetup, deleted, archived, inDatabase);

                    Session.Remove("lfsM2TablesDummy");
                    Session["viewFullLengthLiningTDS"] = viewFullLengthLiningTDS;
                    lfsM2Tables = viewFullLengthLiningTDS.LfsM2Tables;
                    Session["lfsM2Tables"] = lfsM2Tables;

                    grdLfsM2.DataBind();
                    grdLfsM2.PageIndex = grdLfsM2.PageCount - 1;
                }
            }
        }
        /// ////////////////////////////////////////////////////////////////////////
        /// EVENTS
        ///
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                //--- Security check
                if (!Convert.ToBoolean(Session["sgLFS_APP_VIEW"]))
                {
                    Response.Redirect("./../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Prepare initial
                Session.Remove("lfsM2TablesDummy");

                //... Restore data set lfs record
                tdsLfsRecord = (TDSLFSRecord)Session["tdsLfsRecord"];

                // ... for client
                CompaniesGateway companiesGateway = new CompaniesGateway();
                tbxCOMPANIES_ID.Text = companiesGateway.GetName((int)tdsLfsRecord.LFS_MASTER_AREA.Rows[0]["COMPANIES_ID"], Convert.ToInt32(Session["companyID"]));

                // ... for measurement type
                LFSMeasurementTypeGateway lfsMeasurementTypeGateway = new LFSMeasurementTypeGateway();
                DataSet dsLfsMeasurementType = lfsMeasurementTypeGateway.GetLFSMeasurementTypeForDropDownList("");

                ddlMeasurementType.DataSource = dsLfsMeasurementType;
                ddlMeasurementType.DataTextField = "MeasurementType";

                // Load LfsM2Tables data
                int companyId = Convert.ToInt32(Session["companyID"]);
                Guid id = new Guid(Request.QueryString["id"].ToString());
                hdfId.Value = Request.QueryString["id"].ToString();

                viewFullLengthLiningTDS = new ViewFullLengthLiningTDS();
                ViewFullLengthLiningLfsM2TablesGateway viewFullLengthLiningLfsM2TablesGateway = new ViewFullLengthLiningLfsM2TablesGateway(viewFullLengthLiningTDS);
                viewFullLengthLiningLfsM2TablesGateway.LoadById(id, companyId);

                Session["viewFullLengthLiningTDS"] = viewFullLengthLiningTDS;
                lfsM2Tables = viewFullLengthLiningTDS.LfsM2Tables;
                Session["lfsM2Tables"] = lfsM2Tables;

                // Databind
                Page.DataBind();

                TDSLFSRecord.LFS_MASTER_AREARow lfsMasterAreaRow = tdsLfsRecord.LFS_MASTER_AREA.FindByIDCOMPANY_ID(new Guid(tbxID.Text), Convert.ToInt32(Session["companyID"]));
                ddlMeasurementType.SelectedValue = (lfsMasterAreaRow.IsMeasurementTypeNull()) ? "" : lfsMasterAreaRow.MeasurementType;
            }
            else
            {
                // Restore data set lfs record
                tdsLfsRecord = (TDSLFSRecord)Session["tdsLfsRecord"];
                viewFullLengthLiningTDS = (ViewFullLengthLiningTDS)Session["viewFullLengthLiningTDS"];
            }
        }