tim@14: /*  Copyright Mihai Bazon, 2002, 2003  |  http://dynarch.com/mishoo/
tim@14:  * ---------------------------------------------------------------------------
tim@14:  *
tim@14:  * The DHTML Calendar
tim@14:  *
tim@14:  * Details and latest version at:
tim@14:  * http://dynarch.com/mishoo/calendar.epl
tim@14:  *
tim@14:  * This script is distributed under the GNU Lesser General Public License.
tim@14:  * Read the entire license text here: http://www.gnu.org/licenses/lgpl.html
tim@14:  *
tim@14:  * This file defines helper functions for setting up the calendar.  They are
tim@14:  * intended to help non-programmers get a working calendar on their site
tim@14:  * quickly.  This script should not be seen as part of the calendar.  It just
tim@14:  * shows you what one can do with the calendar, while in the same time
tim@14:  * providing a quick and simple method for setting it up.  If you need
tim@14:  * exhaustive customization of the calendar creation process feel free to
tim@14:  * modify this code to suit your needs (this is recommended and much better
tim@14:  * than modifying calendar.js itself).
tim@14:  */
tim@14: 
tim@14: // $Id: calendar-setup.js,v 1.1 2007/12/06 13:55:16 drewnak Exp $
tim@14: 
tim@14: /**
tim@14:  *  This function "patches" an input field (or other element) to use a calendar
tim@14:  *  widget for date selection.
tim@14:  *
tim@14:  *  The "params" is a single object that can have the following properties:
tim@14:  *
tim@14:  *    prop. name   | description
tim@14:  *  -------------------------------------------------------------------------------------------------
tim@14:  *   inputField    | the ID of an input field to store the date
tim@14:  *   displayArea   | the ID of a DIV or other element to show the date
tim@14:  *   button        | ID of a button or other element that will trigger the calendar
tim@14:  *   eventName     | event that will trigger the calendar, without the "on" prefix (default: "click")
tim@14:  *   ifFormat      | date format that will be stored in the input field
tim@14:  *   daFormat      | the date format that will be used to display the date in displayArea
tim@14:  *   singleClick   | (true/false) wether the calendar is in single click mode or not (default: true)
tim@14:  *   firstDay      | numeric: 0 to 6.  "0" means display Sunday first, "1" means display Monday first, etc.
tim@14:  *   align         | alignment (default: "Br"); if you don't know what's this see the calendar documentation
tim@14:  *   range         | array with 2 elements.  Default: [1900, 2999] -- the range of years available
tim@14:  *   weekNumbers   | (true/false) if it's true (default) the calendar will display week numbers
tim@14:  *   flat          | null or element ID; if not null the calendar will be a flat calendar having the parent with the given ID
tim@14:  *   flatCallback  | function that receives a JS Date object and returns an URL to point the browser to (for flat calendar)
tim@14:  *   disableFunc   | function that receives a JS Date object and should return true if that date has to be disabled in the calendar
tim@14:  *   onSelect      | function that gets called when a date is selected.  You don't _have_ to supply this (the default is generally okay)
tim@14:  *   onClose       | function that gets called when the calendar is closed.  [default]
tim@14:  *   onUpdate      | function that gets called after the date is updated in the input field.  Receives a reference to the calendar.
tim@14:  *   date          | the date that the calendar will be initially displayed to
tim@14:  *   showsTime     | default: false; if true the calendar will include a time selector
tim@14:  *   timeFormat    | the time format; can be "12" or "24", default is "12"
tim@14:  *   electric      | if true (default) then given fields/date areas are updated for each move; otherwise they're updated only on close
tim@14:  *   step          | configures the step of the years in drop-down boxes; default: 2
tim@14:  *   position      | configures the calendar absolute position; default: null
tim@14:  *   cache         | if "true" (but default: "false") it will reuse the same calendar object, where possible
tim@14:  *   showOthers    | if "true" (but default: "false") it will show days from other months too
tim@14:  *
tim@14:  *  None of them is required, they all have default values.  However, if you
tim@14:  *  pass none of "inputField", "displayArea" or "button" you'll get a warning
tim@14:  *  saying "nothing to setup".
tim@14:  */
tim@14: Calendar.setup = function (params) {
tim@14: 	function param_default(pname, def) { if (typeof params[pname] == "undefined") { params[pname] = def; } };
tim@14: 
tim@14: 	param_default("inputField",     null);
tim@14: 	param_default("displayArea",    null);
tim@14: 	param_default("button",         null);
tim@14: 	param_default("eventName",      "click");
tim@14: 	param_default("ifFormat",       "%Y/%m/%d");
tim@14: 	param_default("daFormat",       "%Y/%m/%d");
tim@14: 	param_default("singleClick",    true);
tim@14: 	param_default("disableFunc",    null);
tim@14: 	param_default("dateStatusFunc", params["disableFunc"]);	// takes precedence if both are defined
tim@14: 	param_default("dateText",       null);
tim@14: 	param_default("firstDay",       null);
tim@14: 	param_default("align",          "Br");
tim@14: 	param_default("range",          [1900, 2999]);
tim@14: 	param_default("weekNumbers",    true);
tim@14: 	param_default("flat",           null);
tim@14: 	param_default("flatCallback",   null);
tim@14: 	param_default("onSelect",       null);
tim@14: 	param_default("onClose",        null);
tim@14: 	param_default("onUpdate",       null);
tim@14: 	param_default("date",           null);
tim@14: 	param_default("showsTime",      false);
tim@14: 	param_default("timeFormat",     "24");
tim@14: 	param_default("electric",       true);
tim@14: 	param_default("step",           2);
tim@14: 	param_default("position",       null);
tim@14: 	param_default("cache",          false);
tim@14: 	param_default("showOthers",     false);
tim@14: 	param_default("multiple",       null);
tim@14: 
tim@14: 	var tmp = ["inputField", "displayArea", "button"];
tim@14: 	for (var i in tmp) {
tim@14: 		if (typeof params[tmp[i]] == "string") {
tim@14: 			params[tmp[i]] = document.getElementById(params[tmp[i]]);
tim@14: 		}
tim@14: 	}
tim@14: 	if (!(params.flat || params.multiple || params.inputField || params.displayArea || params.button)) {
tim@14: 		alert("Calendar.setup:\n  Nothing to setup (no fields found).  Please check your code");
tim@14: 		return false;
tim@14: 	}
tim@14: 
tim@14: 	function onSelect(cal) {
tim@14: 		var p = cal.params;
tim@14: 		var update = (cal.dateClicked || p.electric);
tim@14: 		if (update && p.inputField) {
tim@14: 			p.inputField.value = cal.date.print(p.ifFormat);
tim@14: 			if (typeof p.inputField.onchange == "function")
tim@14: 				p.inputField.onchange();
tim@14: 		}
tim@14: 		if (update && p.displayArea)
tim@14: 			p.displayArea.innerHTML = cal.date.print(p.daFormat);
tim@14: 		if (update && typeof p.onUpdate == "function")
tim@14: 			p.onUpdate(cal);
tim@14: 		if (update && p.flat) {
tim@14: 			if (typeof p.flatCallback == "function")
tim@14: 				p.flatCallback(cal);
tim@14: 		}
tim@14: 		if (update && p.singleClick && cal.dateClicked)
tim@14: 			cal.callCloseHandler();
tim@14: 	};
tim@14: 
tim@14: 	if (params.flat != null) {
tim@14: 		if (typeof params.flat == "string")
tim@14: 			params.flat = document.getElementById(params.flat);
tim@14: 		if (!params.flat) {
tim@14: 			alert("Calendar.setup:\n  Flat specified but can't find parent.");
tim@14: 			return false;
tim@14: 		}
tim@14: 		var cal = new Calendar(params.firstDay, params.date, params.onSelect || onSelect);
tim@14: 		cal.showsOtherMonths = params.showOthers;
tim@14: 		cal.showsTime = params.showsTime;
tim@14: 		cal.time24 = (params.timeFormat == "24");
tim@14: 		cal.params = params;
tim@14: 		cal.weekNumbers = params.weekNumbers;
tim@14: 		cal.setRange(params.range[0], params.range[1]);
tim@14: 		cal.setDateStatusHandler(params.dateStatusFunc);
tim@14: 		cal.getDateText = params.dateText;
tim@14: 		if (params.ifFormat) {
tim@14: 			cal.setDateFormat(params.ifFormat);
tim@14: 		}
tim@14: 		if (params.inputField && typeof params.inputField.value == "string") {
tim@14: 			cal.parseDate(params.inputField.value);
tim@14: 		}
tim@14: 		cal.create(params.flat);
tim@14: 		cal.show();
tim@14: 		return false;
tim@14: 	}
tim@14: 
tim@14: 	var triggerEl = params.button || params.displayArea || params.inputField;
tim@14: 	triggerEl["on" + params.eventName] = function() {
tim@14: 		var dateEl = params.inputField || params.displayArea;
tim@14: 		var dateFmt = params.inputField ? params.ifFormat : params.daFormat;
tim@14: 		var mustCreate = false;
tim@14: 		var cal = window.calendar;
tim@14: 		if (dateEl)
tim@14: 			params.date = Date.parseDate(dateEl.value || dateEl.innerHTML, dateFmt);
tim@14: 		if (!(cal && params.cache)) {
tim@14: 			window.calendar = cal = new Calendar(params.firstDay,
tim@14: 							     params.date,
tim@14: 							     params.onSelect || onSelect,
tim@14: 							     params.onClose || function(cal) { cal.hide(); });
tim@14: 			cal.showsTime = params.showsTime;
tim@14: 			cal.time24 = (params.timeFormat == "24");
tim@14: 			cal.weekNumbers = params.weekNumbers;
tim@14: 			mustCreate = true;
tim@14: 		} else {
tim@14: 			if (params.date)
tim@14: 				cal.setDate(params.date);
tim@14: 			cal.hide();
tim@14: 		}
tim@14: 		if (params.multiple) {
tim@14: 			cal.multiple = {};
tim@14: 			for (var i = params.multiple.length; --i >= 0;) {
tim@14: 				var d = params.multiple[i];
tim@14: 				var ds = d.print("%Y%m%d");
tim@14: 				cal.multiple[ds] = d;
tim@14: 			}
tim@14: 		}
tim@14: 		cal.showsOtherMonths = params.showOthers;
tim@14: 		cal.yearStep = params.step;
tim@14: 		cal.setRange(params.range[0], params.range[1]);
tim@14: 		cal.params = params;
tim@14: 		cal.setDateStatusHandler(params.dateStatusFunc);
tim@14: 		cal.getDateText = params.dateText;
tim@14: 		cal.setDateFormat(dateFmt);
tim@14: 		if (mustCreate)
tim@14: 			cal.create();
tim@14: 		cal.refresh();
tim@14: 		if (!params.position)
tim@14: 			cal.showAtElement(params.button || params.displayArea || params.inputField, params.align);
tim@14: 		else
tim@14: 			cal.showAt(params.position[0], params.position[1]);
tim@14: 		return false;
tim@14: 	};
tim@14: 
tim@14: 	return cal;
tim@14: };