fix one-time card charging not pulling in exp date?
[freeside.git] / httemplate / elements / calendar-setup.js
1 /*  Copyright Mihai Bazon, 2002, 2003  |  http://dynarch.com/mishoo/
2  * ---------------------------------------------------------------------------
3  *
4  * The DHTML Calendar
5  *
6  * Details and latest version at:
7  * http://dynarch.com/mishoo/calendar.epl
8  *
9  * This script is distributed under the GNU Lesser General Public License.
10  * Read the entire license text here: http://www.gnu.org/licenses/lgpl.html
11  *
12  * This file defines helper functions for setting up the calendar.  They are
13  * intended to help non-programmers get a working calendar on their site
14  * quickly.  This script should not be seen as part of the calendar.  It just
15  * shows you what one can do with the calendar, while in the same time
16  * providing a quick and simple method for setting it up.  If you need
17  * exhaustive customization of the calendar creation process feel free to
18  * modify this code to suit your needs (this is recommended and much better
19  * than modifying calendar.js itself).
20  */
21
22 // $Id: calendar-setup.js,v 1.3 2003-11-07 10:53:35 ivan Exp $
23
24 /**
25  *  This function "patches" an input field (or other element) to use a calendar
26  *  widget for date selection.
27  *
28  *  The "params" is a single object that can have the following properties:
29  *
30  *    prop. name   | description
31  *  -------------------------------------------------------------------------------------------------
32  *   inputField    | the ID of an input field to store the date
33  *   displayArea   | the ID of a DIV or other element to show the date
34  *   button        | ID of a button or other element that will trigger the calendar
35  *   eventName     | event that will trigger the calendar, without the "on" prefix (default: "click")
36  *   ifFormat      | date format that will be stored in the input field
37  *   daFormat      | the date format that will be used to display the date in displayArea
38  *   singleClick   | (true/false) wether the calendar is in single click mode or not (default: true)
39  *   mondayFirst   | (true/false) if true Monday is the first day of week, Sunday otherwise (default: true)
40  *   align         | alignment (default: "Bl"); if you don't know what's this see the calendar documentation
41  *   range         | array with 2 elements.  Default: [1900, 2999] -- the range of years available
42  *   weekNumbers   | (true/false) if it's true (default) the calendar will display week numbers
43  *   flat          | null or element ID; if not null the calendar will be a flat calendar having the parent with the given ID
44  *   flatCallback  | function that receives a JS Date object and returns an URL to point the browser to (for flat calendar)
45  *   disableFunc   | function that receives a JS Date object and should return true if that date has to be disabled in the calendar
46  *   onSelect      | function that gets called when a date is selected.  You don't _have_ to supply this (the default is generally okay)
47  *   onClose       | function that gets called when the calendar is closed.  [default]
48  *   onUpdate      | function that gets called after the date is updated in the input field.  Receives a reference to the calendar.
49  *   date          | the date that the calendar will be initially displayed to
50  *   showsTime     | default: false; if true the calendar will include a time selector
51  *   timeFormat    | the time format; can be "12" or "24", default is "12"
52  *
53  *  None of them is required, they all have default values.  However, if you
54  *  pass none of "inputField", "displayArea" or "button" you'll get a warning
55  *  saying "nothing to setup".
56  */
57 Calendar.setup = function (params) {
58         function param_default(pname, def) { if (typeof params[pname] == "undefined") { params[pname] = def; } };
59
60         param_default("inputField",     null);
61         param_default("displayArea",    null);
62         param_default("button",         null);
63         param_default("eventName",      "click");
64         param_default("ifFormat",       "%Y/%m/%d");
65         param_default("daFormat",       "%Y/%m/%d");
66         param_default("singleClick",    true);
67         param_default("disableFunc",    null);
68         param_default("dateStatusFunc", params["disableFunc"]); // takes precedence if both are defined
69         param_default("mondayFirst",    true);
70         param_default("align",          "Bl");
71         param_default("range",          [1900, 2999]);
72         param_default("weekNumbers",    true);
73         param_default("flat",           null);
74         param_default("flatCallback",   null);
75         param_default("onSelect",       null);
76         param_default("onClose",        null);
77         param_default("onUpdate",       null);
78         param_default("date",           null);
79         param_default("showsTime",      false);
80         param_default("timeFormat",     "24");
81
82         var tmp = ["inputField", "displayArea", "button"];
83         for (var i in tmp) {
84                 if (typeof params[tmp[i]] == "string") {
85                         params[tmp[i]] = document.getElementById(params[tmp[i]]);
86                 }
87         }
88         if (!(params.flat || params.inputField || params.displayArea || params.button)) {
89                 alert("Calendar.setup:\n  Nothing to setup (no fields found).  Please check your code");
90                 return false;
91         }
92
93         function onSelect(cal) {
94                 if (cal.params.flat) {
95                         if (typeof cal.params.flatCallback == "function") {
96                                 cal.params.flatCallback(cal);
97                         } else {
98                                 alert("No flatCallback given -- doing nothing.");
99                         }
100                         return false;
101                 }
102                 if (cal.params.inputField) {
103                         cal.params.inputField.value = cal.date.print(cal.params.ifFormat);
104                 }
105                 if (cal.params.displayArea) {
106                         cal.params.displayArea.innerHTML = cal.date.print(cal.params.daFormat);
107                 }
108                 if (cal.params.singleClick && cal.dateClicked) {
109                         cal.callCloseHandler();
110                 }
111                 if (typeof cal.params.onUpdate == "function") {
112                         cal.params.onUpdate(cal);
113                 }
114         };
115
116         if (params.flat != null) {
117                 params.flat = document.getElementById(params.flat);
118                 if (!params.flat) {
119                         alert("Calendar.setup:\n  Flat specified but can't find parent.");
120                         return false;
121                 }
122                 var cal = new Calendar(params.mondayFirst, params.date, params.onSelect || onSelect);
123                 cal.showsTime = params.showsTime;
124                 cal.time24 = (params.timeFormat == "24");
125                 cal.params = params;
126                 cal.weekNumbers = params.weekNumbers;
127                 cal.setRange(params.range[0], params.range[1]);
128                 cal.setDateStatusHandler(params.dateStatusFunc);
129                 cal.create(params.flat);
130                 cal.show();
131                 return false;
132         }
133
134         var triggerEl = params.button || params.displayArea || params.inputField;
135         triggerEl["on" + params.eventName] = function() {
136                 var dateEl = params.inputField || params.displayArea;
137                 var dateFmt = params.inputField ? params.ifFormat : params.daFormat;
138                 var mustCreate = false;
139                 var cal = window.calendar;
140                 if (!window.calendar) {
141                         window.calendar = cal = new Calendar(params.mondayFirst,
142                                                              params.date,
143                                                              params.onSelect || onSelect,
144                                                              params.onClose || function(cal) { cal.hide(); });
145                         cal.showsTime = params.showsTime;
146                         cal.time24 = (params.timeFormat == "24");
147                         cal.weekNumbers = params.weekNumbers;
148                         mustCreate = true;
149                 } else {
150                         cal.hide();
151                 }
152                 cal.setRange(params.range[0], params.range[1]);
153                 cal.params = params;
154                 cal.setDateStatusHandler(params.dateStatusFunc);
155                 cal.setDateFormat(dateFmt);
156                 if (mustCreate)
157                         cal.create();
158                 cal.parseDate(dateEl.value || dateEl.innerHTML);
159                 cal.refresh();
160                 cal.showAtElement(params.displayArea || params.inputField, params.align);
161                 return false;
162         };
163 };