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;

public partial class PopupCalendar : System.Web.UI.UserControl
{
    /// <summary>
    /// Sets or gets the date for this user control
    /// </summary>
    public DateTime SelectedDate
    {
        get 
        { 
            if (this.txtDate != null)
            {
                DateTime dt = DateTime.Now;
                DateTime.TryParse(this.txtDate.Text, out dt);
                this._SelectedDate = dt;
                return dt;
            }
            
            return _SelectedDate; 
        }
        set 
        { 
            _SelectedDate = value;
            if (this.txtDate != null)
                this.txtDate.Text = this.SelectedDateString;
        }
    }
    private DateTime _SelectedDate = DateTime.Today;

    
    /// <summary>
    /// String representation of the currently selected date. Can be used to set or get date.
    /// </summary>
    public string SelectedDateString
    {
        get 
        {
            if (this._SelectedDate == DateTime.MinValue)
                return "";

            return this._SelectedDate.ToString("d");
        }
        set 
        {
            DateTime dt = DateTime.Now;
            DateTime.TryParse(value, out dt);
            this._SelectedDate = dt;
        }
    }
    



    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Calendar_SelectionChanged(object sender, EventArgs e)
    {
        this.SelectedDate = this.Calendar.SelectedDate;
        this.txtDate.Text = this.SelectedDate.ToString("d");
        this.MakeCalendarVisible(false);
    }
    
    protected void btnDateDropDown_Click(object sender, EventArgs e)
    {
        this.MakeCalendarVisible(true);

        string DateVal = this.txtDate.Text;
        DateTime dt = DateTime.Now;
        DateTime.TryParse(DateVal,out dt);

        this.Calendar.VisibleDate = dt;
        this.Calendar.SelectedDate = dt;
    }

    protected void btnCloseDateDropDown_Click(object sender, EventArgs e)
    {
        this.MakeCalendarVisible(false);
    }

    protected void MakeCalendarVisible(bool Visible)
    {
        if (Visible)
            this.CalendarContent.Style.Add(HtmlTextWriterStyle.Display, "");
        else
            this.CalendarContent.Style.Add(HtmlTextWriterStyle.Display, "none");
    }

    protected override void OnLoad(EventArgs e)
    {
        if (this.txtDate.Text == string.Empty)
            this.SelectedDateString = Request.Form[this.txtDate.UniqueID];

        base.OnLoad(e);
    }
   
    
}
