// remember the calendar object so that we reuse
// it and avoid creating another one
var calendar = window._dynarch_popupCalendar;

CalendarHandler = function()
{
}

// This function gets called when the end-user clicks on some date.
CalendarHandler.SelectHandler = function(cal, date)
{
    if (cal.dateClicked)
    {
        cal.sel.value = date;       // just update the date in the input field.
        cal.callCloseHandler();
        cal.sel.fireEvent("onchange");
    }
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
CalendarHandler.CloseHandler = function(cal)
{
    //showprops(cal, true);
    cal.hide();                     // hide the calendar
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
CalendarHandler.ShowCalendar = function ShowCalendar(id)
{
    var minYear = 2000;
	var maxYear = 2100;
    var el = document.getElementById(id);
    if (calendar != null)
    {
        // We already have some calendar created
        calendar.hide();                 // so we hide it first.
    }
    else
    {
        // First-time call, create the calendar
        calendar = new Calendar(1, null, CalendarHandler.SelectHandler, CalendarHandler.CloseHandler);
        // The following line to hide the week numbers
        calendar.weekNumbers = false;
        calendar.create();
        calendar.isPopup = false;
    }

    calendar.setRange(minYear, maxYear); // min/max year allowed.

    var format = Calendar._TT["DEF_DATE_FORMAT"];
    var ttFormat = Calendar._TT["TT_DATE_FORMAT"];

    calendar.setDateFormat(format);     // set the specified date format
    calendar.setTtDateFormat(ttFormat);

    if (el.value != "")
    {
        calendar.parseDate(el.value);   // try to parse the text in field
    }
    else
    {
        calendar.setDate(new Date());   // reset to current date
    }
    calendar.sel = el;                  // inform it what input field we use
    calendar.showAtElement(el, "Br");   // show the calendar below it

    return false;
}
// custom calendar validation
function ValidateCalendar(source, args)
{
  if (window.calendar)
  {
    var date = window.calendar.date;
    var fmt = window.calendar.dateFormat;
    var minDate = Date.parseDate(source.MinDate, fmt);
    var maxDate = Date.parseDate(source.MaxDate, fmt);
    if (date && date >= minDate && date <= maxDate)
    { args.IsValid = true; }
    else
    { args.IsValid = false; }
  }
}
