
// remember the calendar object so that we reuse it and avoid creating another
var calendar = null;

// This function gets called when an end-user clicks on some date
function selected(cal, date) {
	cal.input_field.value = date; 	// just update the value of the input field
	if (calendar.dateClicked) {
		calendar.callCloseHandler(); // this calls "onClose" (see above)
	}

}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks the "Close" (X) button.  It just hides the calendar without
// destroying it.
function closeHandler(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 popupCalendar(id, dateFormat) {
	var el = document.getElementById(id);
	
	if (calendar != null) {
		// we already have one created, so just update it.
		calendar.hide();		// hide the existing calendar
		//calendar.parseDate(el.value);  // set it to a new date
	} 
	else {
		// first-time call, create the calendar
		var cal = new Calendar(true, null, selected, closeHandler);
		calendar = cal;		// remember the calendar in the global
		cal.setRange(1900, 2070);	// min/max year allowed
		cal.setDateFormat(dateFormat);
		// uncomment the following to hide the week numbers
		// calendar.weekNumbers = false;
		calendar.create();		// create a popup calendar
	}
	
	calendar.input_field = el;		// inform it what input field we use
	calendar.parseDate(el.value);  	// set it to a new date
	calendar.showAtElement(el);	// show the calendar next to the input field
	return false;
}
