
// 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.
function showCalendar(id) {
  
  var el = $(id);

  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, onSelect, onClose);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;

	_dynarch_popupCalendar = cal;                  // remember it in the global var
	cal.showsOtherMonths = true;
	cal.setRange(1900, 2070);        // min/max year allowed.
	
	cal.setDateStatusHandler(dateStatusHandler);
	cal.create();
  }

  _dynarch_popupCalendar.setDateFormat("%d/%m/%Y");    // set the specified date format

	if(el.value)
	{
		_dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
	}
	else
	{
		if(el.name == "edate")
		{
			_dynarch_popupCalendar.parseDate($('sdate').value);
		}
	}
  
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el, "Tl");        // show the calendar
	
  return false;

}

function dateStatusHandler(date, y, m, d) 
{
	var now = new Date()
	now.setHours(0);
	now.setMinutes(0);
	now.setSeconds(0);
	var thisdate = new Date(y,m,d,23,59,59);
	return thisdate.getTime() < now.getTime();
}

function onSelect(calendar, date) 
{
	calendar.sel.value = date;

	if (calendar.dateClicked) {
		calendar.callCloseHandler(); // this calls "onClose" (see above)
	}
}

function onClose(calendar) 
{
	calendar.hide();
  _dynarch_popupCalendar = null;

}
	
