private void DepthSpanButton_Click(object sender, EventArgs e)
        {
            int index;
            FormSpanInt frm;
            FORMSPANINPUT input = new FORMSPANINPUT();
            CSpan prev = null, current = null, next = null;

            if(m_bDepthSpanActiveLocked == true)
                return;
            m_bDepthSpanActiveLocked = true;

            // Transfer information from the GUI controls into the matrix
            GuiControlsToMatrixModel();

            if(m_activeSpanIndex == (index = GuiUtils.MatchIndex(m_spanButton, (Button)sender)))
            {
                // Initialize the FORM_SPAN_INT struct as needded.
                input.minInputIsReadOnly = input.maxInputIsReadOnly = true;
                input.szRange = "< Animat Depth <=";
                input.szCaption = "Behavior Transition Depth Span";
                input.szInstruction = "Set a depth span (meters):";

                // Modify the FORM_SPAN_INT struct as needded.
                current = m_spanMgr.GetSpan(index);
                input.minInput = -(int)current.shallow;
                input.maxInput = -(int)current.deep;

                input.lowerLimit = 0;
                input.uppperLimit = 3500;

                if(index > 0 && null != (prev = m_spanMgr.GetSpan(index-1)))
                {
                    input.lowerLimit = -(int)prev.shallow;
                    input.minInputIsReadOnly = false;
                }

                if(index+1 < m_spanMgr.SpanCount && null != (next = m_spanMgr.GetSpan(index+1)))
                {
                    input.uppperLimit = -(int)next.deep;
                    input.maxInputIsReadOnly = false;
                }

                if(input.lowerLimit == 0)
                    input.szRange = "<= Animat Depth <=";

                frm = new FormSpanInt(input);

                frm.Location =
                    new Point(this.Location.X + ((Button)sender).Location.X + ((Button)sender).Width + 10,
                              this.Location.Y + ((Button)sender).Location.Y + ((Button)sender).Height/2- frm.Height/2);


                frm.ShowDialog();
                if(frm.Modified == true)
                {
                    m_bModified = true;
                    current.shallow = -(double)frm.MinResult;
                    current.deep = -(double)frm.MaxResult;
                    if(prev != null)
                        prev.deep = current.shallow;
                    if(next != null)
                        next.shallow = current.deep;
                }
            }



            // Mangage depth span buttons
            m_activeSpanIndex = index;
            ManageDepthSpanControls();

            // Transfer the matrix into the GUI.
            m_activeMatrixRowIndex = 0;
            ManageMatrixControls();

            m_bDepthSpanActiveLocked = false;
        }
        private void ClkSpanButton_Click(object sender, EventArgs e)
        {
            int index;
            FromSpanClock frm;
            FORMSPANINPUT input = new FORMSPANINPUT();
            double[] prevRow = null, thisRow = null, nextRow = null;

            if(m_bDepthSpanActiveLocked == true)
                return;
            m_bDepthSpanActiveLocked = true;

            

            // Copy the current GUI control values into the matrix.
            GuiControlsToMatrixModel();

            // If the button clicked is the same as the previously clicked button then
            // open up the time span dialog box.
            if(m_activeMatrixRowIndex == (index = GuiUtils.MatchIndex(m_clkSpanButton, (Button)sender)))
            {
                Debug.Assert(index < Matrix.RowCount);
                // Initialize the FORMSPANINPUT struct as needed that is passed into the dialog box.

                input.szRange = "<= time of day <";
                input.szCaption = "Behavior Transition Time Span";
                input.szInstruction = "Set a clock span (HH:MM):";

                // Modify the FORM_SPAN_INT struct as needded.              
                thisRow = Matrix.a[index];
                input.minInput = thisRow[0]; // start of clock span
                input.maxInput = thisRow[1]; // end of clock span

                input.lowerLimit = 0;
                input.uppperLimit = 24.0;

                //if(index == CUtil.IndexToCircularBufferIndex(index + 1, Matrix.RowCount))
                  //  index = index;

                input.minInputIsReadOnly = input.maxInputIsReadOnly = false;
                if(Matrix.RowCount == 1)
                {
                    // If there is only a single row in the behavior transition matrix
                    // there is no need to be able to set the time span so make the time
                    // span read only
                    input.minInputIsReadOnly = input.maxInputIsReadOnly = true;
                }
                else
                {
                    // If there are only two rows then the previous row will be the same
                    // as the next row.  This is allowed.
                    prevRow = Matrix.a[CUtil.IndexToCircularIndex(index - 1, Matrix.RowCount)];
                    nextRow = Matrix.a[CUtil.IndexToCircularIndex(index + 1, Matrix.RowCount)];
                }

                frm = new FromSpanClock(input);
                frm.Location = 
                    new Point(this.Location.X + ((Button)sender).Location.X + ((Button)sender).Width + 10,
                              this.Location.Y + ((Button)sender).Location.Y + ((Button)sender).Height/2- frm.Height/2);

                frm.ShowDialog();

                if(frm.Modified == true)
                {
                    m_bModified = true;
                    thisRow[0] = frm.MinResult;
                    thisRow[1] = frm.MaxResult;
                    if(prevRow != null)
                        prevRow[1] = thisRow[0];
                    if(nextRow != null)
                        nextRow[0] = thisRow[1];
                }
            }
            m_activeMatrixRowIndex = index;
            ManageMatrixControls();
            m_bDepthSpanActiveLocked = false;

        }
        public FromSpanClock(FORMSPANINPUT FSI)
            : base(FSI.szCaption)
        {
            HHMMSS HMS = new HHMMSS();
            int adjust;

            InputWidth = 24;

            // Instruction String
            InstrctnLabel.Visible = InstrctnLabel.Enabled = true;
            InstrctnLabel.Text = FSI.szInstruction;

            //--------------------------------------------------------------------------//
            // Minimum Clock
            //--------------//
            m_minReslt = m_minOrgnl = FSI.minInput;
            HMS = CUtil.TwentyHrFloatClockToHHMMSSOrZeroInclusive(FSI.minInput);

            // Input 1 (Minimum HH)
            I1TextBox.Visible = I1TextBox.Enabled = true;
            I1TextBox.Enabled = !FSI.minInputIsReadOnly;
            I1TextBox.Text = String.Format("{0:00}", HMS.hh);

            Colon1Label.Visible = Colon1Label.Enabled = true;
            Colon1Label.Text = ":";

            // Input 2 (Minimum MM)
            I2TextBox.Visible = I2TextBox.Enabled = true;
            I2TextBox.Enabled = !FSI.minInputIsReadOnly;
            I2TextBox.Text = String.Format("{0:00}", HMS.mm);

            Colon2Label.Visible = Colon1Label.Enabled = true;
            Colon2Label.Text = ":";

            I3TextBox.Visible = true;
            I3TextBox.Text = String.Format("{0:00}", 0);
            //--------------------------------------------------------------------------//


            //--------------------------------------------------------------------------//
            // Maximum Clock
            //--------------//
            m_maxReslt = m_maxOrgnl = FSI.maxInput;
            HMS = CUtil.TwentyHrFloatClockToHHMMSSOrZeroInclusive(FSI.maxInput);

            // Input 2 (Maximum HH)
            I4TextBox.Visible = I4TextBox.Enabled = true;
            I4TextBox.Enabled = !FSI.maxInputIsReadOnly;
            I4TextBox.Text = String.Format("{0:00}", HMS.hh);

            Colon3Label.Visible = Colon2Label.Enabled = true;
            Colon3Label.Text = ":";

            // Input 2 (Maximum MM)
            I5TextBox.Visible = I5TextBox.Enabled = true;
            I5TextBox.Enabled = !FSI.maxInputIsReadOnly;
            I5TextBox.Text = String.Format("{0:00}", HMS.mm);

            Colon3Label.Visible = Colon2Label.Enabled = true;
            Colon3Label.Text = ":";

            I6TextBox.Visible = true;
            I6TextBox.Text = String.Format("{0:00}", 0);
            //--------------------------------------------------------------------------//

            //--------------------------------------------------------------------------//
            // Low Clock Limit
            //----------------//
            HMS = CUtil.TwentyHrFloatClockToHHMMSSOrZeroInclusive(FSI.lowerLimit);
            m_lowerLim = FSI.lowerLimit; // Save the orginal input
            Input1Label.Visible = Input1Label.Enabled = true;
            Input1Label.Text = String.Format("Earliest Settable: {0:00}:{1:00}:00", HMS.hh, HMS.mm);
            //--------------------------------------------------------------------------//

            //--------------------------------------------------------------------------//
            // High Clock Limit
            //-----------------//
            HMS = CUtil.TwentyHrFloatClockToHHMMSSOrZeroInclusive(FSI.uppperLimit);
            m_upperLim = FSI.uppperLimit;
            Input2Label.Visible = Input2Label.Enabled = true;
            Input2Label.Text = String.Format("Latest Settable: {0:00}:{1:00}:00", HMS.hh, HMS.mm);
            //--------------------------------------------------------------------------//

            //--------------------------------------------------------------------------//
            // Range Span String
            //-------------------//
            RangeLabel.Visible = RangeLabel.Enabled = true;
            RangeLabel.Text = FSI.szRange;
            //--------------------------------------------------------------------------//

            //--------------------------------------------------------------------------//
            // Control Placement
            //-------------------//
            Colon1Label.Location = new Point(I1TextBox.Location.X + I1TextBox.Width-3, Colon1Label.Location.Y);
            I2TextBox.Location = new Point(Colon1Label.Location.X + Colon1Label.Width-3, I2TextBox.Location.Y);
            Colon2Label.Location = new Point(I2TextBox.Location.X + I2TextBox.Width-3, Colon2Label.Location.Y);
            I3TextBox.Location = new Point(Colon2Label.Location.X + Colon2Label.Width-3, I3TextBox.Location.Y);
            RangeLabel.Location = new Point(I3TextBox.Location.X + I3TextBox.Width, RangeLabel.Location.Y);
            I4TextBox.Location = new Point(RangeLabel.Location.X + RangeLabel.Width, I4TextBox.Location.Y);
            Colon3Label.Location = new Point(I4TextBox.Location.X + I4TextBox.Width-3, Colon3Label.Location.Y);
            I5TextBox.Location = new Point(Colon3Label.Location.X + Colon3Label.Width-3, I5TextBox.Location.Y);
            Colon4Label.Location = new Point(I5TextBox.Location.X + I5TextBox.Width-3, Colon4Label.Location.Y);
            I6TextBox.Location = new Point(Colon4Label.Location.X + Colon4Label.Width-3, I6TextBox.Location.Y);


            Input1Label.Location = new Point(I1TextBox.Location.X, Input1Label.Location.Y);
            Input2Label.Location = new Point(I4TextBox.Location.X, Input2Label.Location.Y);

            if(Input2Label.Location.X + Input2Label.Width > I6TextBox.Location.X + I6TextBox.Width)
            {
                this.Width = Input2Label.Location.X + Input2Label.Width + RIGHTMARGIN;
                adjust = ((Input2Label.Location.X + Input2Label.Width) - (I5TextBox.Location.X + I5TextBox.Width))/2;

                AdjustCntrlLoc(I1TextBox, adjust);
                AdjustCntrlLoc(Colon1Label, adjust);
                AdjustCntrlLoc(I2TextBox, adjust);
                AdjustCntrlLoc(Colon2Label, adjust);
                AdjustCntrlLoc(I3TextBox, adjust);
                AdjustCntrlLoc(RangeLabel, adjust);
                AdjustCntrlLoc(I4TextBox, adjust);
                AdjustCntrlLoc(Colon3Label, adjust);
                AdjustCntrlLoc(I5TextBox, adjust);
                AdjustCntrlLoc(Colon4Label, adjust);
                AdjustCntrlLoc(I6TextBox, adjust);
            }
            else
            {
                this.Width = I6TextBox.Location.X + I6TextBox.Width + RIGHTMARGIN;
            }
            //--------------------------------------------------------------------------//

            // Commented out until a better limit system is established.
            //if(m_minOrgnl > m_maxOrgnl || m_minOrgnl < m_lowerLim || m_maxOrgnl > m_upperLim || m_lowerLim > m_upperLim)
              //   MyOKButton.Enabled = false;
        }
 //--------------------//
 // Class Constructors
 //--------------------//
 //public FormSpanInt(string Caption, string Instruction, int Min, int Max,
   //  int LowLimit, int HighLimit, string Range):
     //base(Caption, Instruction, Min, Max, LowLimit, HighLimit, Range) {}
 public FormSpanInt(FORMSPANINPUT FSI) : base(FSI) { }
        public FormSpanDouble(FORMSPANINPUT FSI)
            : base(FSI.szCaption)
        {
            int adjust;

            // Instruction String
            InstrctnLabel.Visible = InstrctnLabel.Enabled = true;
            InstrctnLabel.Text = FSI.szInstruction;

            // Input 1 (Minimum)
            I1TextBox.Visible = I1TextBox.Enabled = true;
            I1TextBox.Text = "" + FSI.minInput;
            I1TextBox.Enabled = !FSI.minInputIsReadOnly;
            m_minReslt = m_minOrgnl = FSI.minInput;
            

            // Input 2 (Maximum)
            I2TextBox.Visible = I2TextBox.Enabled = true;
            I2TextBox.Text = "" + FSI.maxInput;
            I2TextBox.Enabled = !FSI.maxInputIsReadOnly;
            m_maxReslt = m_maxOrgnl = FSI.maxInput;

            // Low Limit
            m_lowerLim = FSI.lowerLimit; // Save the orginal input
            Input1Label.Visible = Input1Label.Enabled = true;
            Input1Label.Text = "Lower Limit: " + m_lowerLim;

            // High Limit
            m_upperLim = FSI.uppperLimit;
            Input2Label.Visible = Input2Label.Enabled = true;
            Input2Label.Text = "Upper Limit: " + m_upperLim;

            // Range Span String
            RangeLabel.Visible = RangeLabel.Enabled = true;
            RangeLabel.Text = FSI.szRange;

            // Place the control locations and dialog box size.
            RangeLabel.Location = new Point(I1TextBox.Location.X + I1TextBox.Width, RangeLabel.Location.Y);
            I2TextBox.Location = new Point(RangeLabel.Location.X + RangeLabel.Width, I2TextBox.Location.Y);
            Input1Label.Location = new Point(I1TextBox.Location.X, Input1Label.Location.Y);
            Input2Label.Location = new Point(I2TextBox.Location.X, Input2Label.Location.Y);

            // Set the width of this control box and adjust the position of the input text
            // boxes if needed.
            if(Input2Label.Location.X + Input2Label.Width > I2TextBox.Location.X + I2TextBox.Width)
            {
                this.Width = Input2Label.Location.X + Input2Label.Width + RIGHTMARGIN;
                adjust = ((Input2Label.Location.X + Input2Label.Width) - (I2TextBox.Location.X + I2TextBox.Width))/2;
                AdjustCntrlLoc(I1TextBox, adjust);
                AdjustCntrlLoc(I2TextBox, adjust);
                AdjustCntrlLoc(RangeLabel, adjust);
             }
            else
            {
                this.Width = I2TextBox.Location.X + I2TextBox.Width + LEFTMARGIN + RIGHTMARGIN;
            }

            if(m_minOrgnl > m_maxOrgnl || m_minOrgnl < m_lowerLim || m_maxOrgnl > m_upperLim || m_lowerLim > m_upperLim)
                MyOKButton.Enabled = false;
        }