var rc2Calendar = null; // remember the calendar object so that we reuse it and
                     // avoid creation other calendars.

// This function gets called when the end-user clicks on some date.
function rc2CalendarSelected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  cal.callCloseHandler();
}

// 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.
function rc2CalendarCloseHandler(cal) {
  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.
function rc2ShowCalendar(id, format) {
  var el = document.getElementById(id);
 
  if (rc2Calendar != null) {
    // we already have some calendar created
    rc2Calendar.hide();                 // so we hide it first.
  } else {
  
    // first-time call, create the calendar
    var now = new Date();
    var cal = new Calendar(false, now.toString(), rc2CalendarSelected, rc2CalendarCloseHandler);
    cal.weekNumbers = false;
    rc2Calendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);           // min/max year allowed.
    cal.create();
  }
  rc2Calendar.setDateFormat(format);    // set the specified date format
  rc2Calendar.parseDate(el.value);      // try to parse the text in field
  rc2Calendar.sel = el;                 // inform it what input field we use
  rc2Calendar.showAtElement(el);        // show the calendar below it

  return false;
}