//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CommunityServer.Controls {

    /// <summary>
    /// Summary description for DatePicker.
    /// </summary>
    public class DatePicker : WebControl, INamingContainer {
        DropDownList days = new DropDownList();
        DropDownList months = new DropDownList();
        DropDownList years = new DropDownList();
        Label label = new Label();

        protected override void CreateChildControls() {

            // Add default text
            days.Items.Add(new ListItem("Day", ""));
            months.Items.Add(new ListItem("Month", ""));
            years.Items.Add(new ListItem("Year", ""));

            for (int i=1; i<32; i++)
                days.Items.Add(new ListItem(i.ToString(), i.ToString()));

            for (int i=1; i<13; i++)
                months.Items.Add(new ListItem(i.ToString(), i.ToString()));

            for (int i= (DateTime.Now.Year); i>(DateTime.Now.Year - 100); i--)
                years.Items.Add(new ListItem(i.ToString(), i.ToString()));


            Controls.Add(months);
            Controls.Add(new LiteralControl(" "));
            Controls.Add(days);
            Controls.Add(new LiteralControl(" "));
            Controls.Add(years);
            Controls.Add(label);

        }

        public DateTime SelectedDate {
            get { 
				this.EnsureChildControls();
				if ( days.SelectedValue == "Day" || months.SelectedValue == "Month" || years.SelectedValue == "Year" ) {
					return DateTime.MinValue;
				}
                try {
                    return DateTime.Parse(months.SelectedValue + "/" + days.SelectedValue + "/" + years.SelectedValue);
                } catch {
                    return DateTime.MinValue;
                }
            }
            set { 
				this.EnsureChildControls();
				days.SelectedValue = value.Day.ToString();
				months.SelectedValue = value.Month.ToString();
				years.SelectedValue = value.Year.ToString();
			}
        }

    }
}
