using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using Telerik.WebControls;

namespace OrderDelivery
{
    public partial class _Default : System.Web.UI.Page
    {
        enum DeliveryPeriod { Morning, Afternoon, Evening };

        #region properties
        public List<RadMaskedTextBox> Notes
        {
            get
            {
                List<RadMaskedTextBox> list = Session["Notes"] as List<RadMaskedTextBox>;
                if (list == null)
                {
                    Session["Notes"] = new List<RadMaskedTextBox>();
                }
                return Session["Notes"] as List<RadMaskedTextBox>;
            }
            set
            {
                Session["Notes"] = value;
            }
        }
        #endregion

        #region page events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                foreach (RadMaskedTextBox textBox in this.Notes)
                {
                    pnlNotes.Controls.Add(textBox);
                }
            }
            else
            {
                int year = DateTime.Today.Year;
                int month = DateTime.Today.Month;
                int lastDay = DateTime.DaysInMonth(year, month);
                rdiDate.MinDate = new DateTime(year, month, 1);
                rdiDate.MaxDate = new DateTime(year, month, lastDay);

                AddEnumerationMaskPart(rmtbTime.MaskParts, typeof(DeliveryPeriod));

                rmtbFirst.Focus();
            }
        }

        protected void btnAddNotes_Click(object sender, EventArgs e)
        {
            const string NEWLINE = "\r\n";

            RadMaskedTextBox rmtbNotes = new RadMaskedTextBox();
            rmtbNotes.Skin = "Office2007";
            rmtbNotes.TextMode = RadInputTextBoxMode.MultiLine;
            rmtbNotes.Width = new Unit("90%");
            rmtbNotes.Rows = 3; // show three rows

            AddLiteralMaskPart(rmtbNotes.MaskParts, string.Format("Notes ({0}) : ", DateTime.Now));
            AddLiteralMaskPart(rmtbNotes.MaskParts, NEWLINE);
            AddFreeMaskPart(rmtbNotes.MaskParts, 40);
            AddLiteralMaskPart(rmtbNotes.MaskParts, NEWLINE);
            AddFreeMaskPart(rmtbNotes.MaskParts, 40);

            pnlNotes.Controls.Add(rmtbNotes);
            this.Notes.Add(rmtbNotes);
            rmtbNotes.Focus();
        }

protected void btnDeliver_Click(object sender, EventArgs e)
{
    const string messageFormat = "Order for {0} {1}, delivery on {2} during the {3}";
    lblStatus.Text = string.Format(messageFormat, rmtbFirst.Text,
        rmtbLast.Text, rdiDate.SelectedDate.ToString("d"), rmtbTime.Text);
}

        #endregion

        #region private methods

        private void AddEnumerationMaskPart(MaskPartCollection maskParts, Type enumerationType)
        {
            EnumerationMaskPart enumerationMaskPart = new EnumerationMaskPart();
            foreach (string element in Enum.GetNames(enumerationType))
            {
                enumerationMaskPart.Items.Add(element);
            }
            maskParts.Add(enumerationMaskPart);
        }

        private void AddLiteralMaskPart(MaskPartCollection maskParts, string text)
        {
            LiteralMaskPart literalMaskPart = new LiteralMaskPart();
            literalMaskPart.Text = text;
            maskParts.Add(literalMaskPart);
        }

        private void AddFreeMaskPart(MaskPartCollection maskParts, int length)
        {
            for (int i = 0; i < length; i++)
            {
                maskParts.Add(new FreeMaskPart());
            }
        }

        #endregion
    }
}
