Bug 5877 : Offline circulation improvements : upload all files, apply at once
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / lib / calendar / calendar.js
1 /*  Copyright Mihai Bazon, 2002, 2003  |  http://dynarch.com/mishoo/
2  * ------------------------------------------------------------------
3  *
4  * The DHTML Calendar, version 0.9.6 "Keep cool but don't freeze"
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
13
14 /** The Calendar object constructor. */
15 Calendar = function (firstDayOfWeek, dateStr, onSelected, onClose) {
16         // member variables
17         this.activeDiv = null;
18         this.currentDateEl = null;
19         this.getDateStatus = null;
20         this.timeout = null;
21         this.onSelected = onSelected || null;
22         this.onClose = onClose || null;
23         this.dragging = false;
24         this.hidden = false;
25         this.minYear = 1970;
26         this.maxYear = 2050;
27         this.dateFormat = Calendar._TT["DEF_DATE_FORMAT"];
28         this.ttDateFormat = Calendar._TT["TT_DATE_FORMAT"];
29         this.isPopup = true;
30         this.weekNumbers = true;
31         this.firstDayOfWeek = firstDayOfWeek; // 0 for Sunday, 1 for Monday, etc.
32         this.showsOtherMonths = false;
33         this.dateStr = dateStr;
34         this.ar_days = null;
35         this.showsTime = false;
36         this.time24 = true;
37         this.yearStep = 2;
38         // HTML elements
39         this.table = null;
40         this.element = null;
41         this.tbody = null;
42         this.firstdayname = null;
43         // Combo boxes
44         this.monthsCombo = null;
45         this.yearsCombo = null;
46         this.hilitedMonth = null;
47         this.activeMonth = null;
48         this.hilitedYear = null;
49         this.activeYear = null;
50         // Information
51         this.dateClicked = false;
52
53         // one-time initializations
54         if (typeof Calendar._SDN == "undefined") {
55                 // table of short day names
56                 if (typeof Calendar._SDN_len == "undefined")
57                         Calendar._SDN_len = 3;
58                 var ar = new Array();
59                 for (var i = 8; i > 0;) {
60                         ar[--i] = Calendar._DN[i].substr(0, Calendar._SDN_len);
61                 }
62                 Calendar._SDN = ar;
63                 // table of short month names
64                 if (typeof Calendar._SMN_len == "undefined")
65                         Calendar._SMN_len = 3;
66                 ar = new Array();
67                 for (var i = 12; i > 0;) {
68                         ar[--i] = Calendar._MN[i].substr(0, Calendar._SMN_len);
69                 }
70                 Calendar._SMN = ar;
71         }
72 };
73
74 // ** constants
75
76 /// "static", needed for event handlers.
77 Calendar._C = null;
78
79 /// detect a special case of "web browser"
80 Calendar.is_ie = ( /msie/i.test(navigator.userAgent) &&
81                    !/opera/i.test(navigator.userAgent) );
82
83 Calendar.is_ie5 = ( Calendar.is_ie && /msie 5\.0/i.test(navigator.userAgent) );
84
85 /// detect Opera browser
86 Calendar.is_opera = /opera/i.test(navigator.userAgent);
87
88 /// detect KHTML-based browsers
89 Calendar.is_khtml = /Konqueror|Safari|KHTML/i.test(navigator.userAgent);
90
91 // BEGIN: UTILITY FUNCTIONS; beware that these might be moved into a separate
92 //        library, at some point.
93
94 Calendar.getAbsolutePos = function(el) {
95         var SL = 0, ST = 0;
96         var is_div = /^div$/i.test(el.tagName);
97         if (is_div && el.scrollLeft)
98                 SL = el.scrollLeft;
99         if (is_div && el.scrollTop)
100                 ST = el.scrollTop;
101         var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
102         if (el.offsetParent) {
103                 var tmp = this.getAbsolutePos(el.offsetParent);
104                 r.x += tmp.x;
105                 r.y += tmp.y;
106         }
107         return r;
108 };
109
110 Calendar.isRelated = function (el, evt) {
111         var related = evt.relatedTarget;
112         if (!related) {
113                 var type = evt.type;
114                 if (type == "mouseover") {
115                         related = evt.fromElement;
116                 } else if (type == "mouseout") {
117                         related = evt.toElement;
118                 }
119         }
120         while (related) {
121                 if (related == el) {
122                         return true;
123                 }
124                 related = related.parentNode;
125         }
126         return false;
127 };
128
129 Calendar.removeClass = function(el, className) {
130         if (!(el && el.className)) {
131                 return;
132         }
133         var cls = el.className.split(" ");
134         var ar = new Array();
135         for (var i = cls.length; i > 0;) {
136                 if (cls[--i] != className) {
137                         ar[ar.length] = cls[i];
138                 }
139         }
140         el.className = ar.join(" ");
141 };
142
143 Calendar.addClass = function(el, className) {
144         Calendar.removeClass(el, className);
145         el.className += " " + className;
146 };
147
148 Calendar.getElement = function(ev) {
149         if (Calendar.is_ie) {
150                 return window.event.srcElement;
151         } else {
152                 return ev.currentTarget;
153         }
154 };
155
156 Calendar.getTargetElement = function(ev) {
157         if (Calendar.is_ie) {
158                 return window.event.srcElement;
159         } else {
160                 return ev.target;
161         }
162 };
163
164 Calendar.stopEvent = function(ev) {
165         ev || (ev = window.event);
166         if (Calendar.is_ie) {
167                 ev.cancelBubble = true;
168                 ev.returnValue = false;
169         } else {
170                 ev.preventDefault();
171                 ev.stopPropagation();
172         }
173         return false;
174 };
175
176 Calendar.addEvent = function(el, evname, func) {
177         if (el.attachEvent) { // IE
178                 el.attachEvent("on" + evname, func);
179         } else if (el.addEventListener) { // Gecko / W3C
180                 el.addEventListener(evname, func, true);
181         } else {
182                 el["on" + evname] = func;
183         }
184 };
185
186 Calendar.removeEvent = function(el, evname, func) {
187         if (el.detachEvent) { // IE
188                 el.detachEvent("on" + evname, func);
189         } else if (el.removeEventListener) { // Gecko / W3C
190                 el.removeEventListener(evname, func, true);
191         } else {
192                 el["on" + evname] = null;
193         }
194 };
195
196 Calendar.createElement = function(type, parent) {
197         var el = null;
198         if (document.createElementNS) {
199                 // use the XHTML namespace; IE won't normally get here unless
200                 // _they_ "fix" the DOM2 implementation.
201                 el = document.createElementNS("http://www.w3.org/1999/xhtml", type);
202         } else {
203                 el = document.createElement(type);
204         }
205         if (typeof parent != "undefined") {
206                 parent.appendChild(el);
207         }
208         return el;
209 };
210
211 // END: UTILITY FUNCTIONS
212
213 // BEGIN: CALENDAR STATIC FUNCTIONS
214
215 /** Internal -- adds a set of events to make some element behave like a button. */
216 Calendar._add_evs = function(el) {
217         with (Calendar) {
218                 addEvent(el, "mouseover", dayMouseOver);
219                 addEvent(el, "mousedown", dayMouseDown);
220                 addEvent(el, "mouseout", dayMouseOut);
221                 if (is_ie) {
222                         addEvent(el, "dblclick", dayMouseDblClick);
223                         el.setAttribute("unselectable", true);
224                 }
225         }
226 };
227
228 Calendar.findMonth = function(el) {
229         if (typeof el.month != "undefined") {
230                 return el;
231         } else if (typeof el.parentNode.month != "undefined") {
232                 return el.parentNode;
233         }
234         return null;
235 };
236
237 Calendar.findYear = function(el) {
238         if (typeof el.year != "undefined") {
239                 return el;
240         } else if (typeof el.parentNode.year != "undefined") {
241                 return el.parentNode;
242         }
243         return null;
244 };
245
246 Calendar.showMonthsCombo = function () {
247         var cal = Calendar._C;
248         if (!cal) {
249                 return false;
250         }
251         var cal = cal;
252         var cd = cal.activeDiv;
253         var mc = cal.monthsCombo;
254         if (cal.hilitedMonth) {
255                 Calendar.removeClass(cal.hilitedMonth, "hilite");
256         }
257         if (cal.activeMonth) {
258                 Calendar.removeClass(cal.activeMonth, "active");
259         }
260         var mon = cal.monthsCombo.getElementsByTagName("div")[cal.date.getMonth()];
261         Calendar.addClass(mon, "active");
262         cal.activeMonth = mon;
263         var s = mc.style;
264         s.display = "block";
265         if (cd.navtype < 0)
266                 s.left = cd.offsetLeft + "px";
267         else {
268                 var mcw = mc.offsetWidth;
269                 if (typeof mcw == "undefined")
270                         // Konqueror brain-dead techniques
271                         mcw = 50;
272                 s.left = (cd.offsetLeft + cd.offsetWidth - mcw) + "px";
273         }
274         s.top = (cd.offsetTop + cd.offsetHeight) + "px";
275 };
276
277 Calendar.showYearsCombo = function (fwd) {
278         var cal = Calendar._C;
279         if (!cal) {
280                 return false;
281         }
282         var cal = cal;
283         var cd = cal.activeDiv;
284         var yc = cal.yearsCombo;
285         if (cal.hilitedYear) {
286                 Calendar.removeClass(cal.hilitedYear, "hilite");
287         }
288         if (cal.activeYear) {
289                 Calendar.removeClass(cal.activeYear, "active");
290         }
291         cal.activeYear = null;
292         var Y = cal.date.getFullYear() + (fwd ? 1 : -1);
293         var yr = yc.firstChild;
294         var show = false;
295         for (var i = 12; i > 0; --i) {
296                 if (Y >= cal.minYear && Y <= cal.maxYear) {
297                         yr.firstChild.data = Y;
298                         yr.year = Y;
299                         yr.style.display = "block";
300                         show = true;
301                 } else {
302                         yr.style.display = "none";
303                 }
304                 yr = yr.nextSibling;
305                 Y += fwd ? cal.yearStep : -cal.yearStep;
306         }
307         if (show) {
308                 var s = yc.style;
309                 s.display = "block";
310                 if (cd.navtype < 0)
311                         s.left = cd.offsetLeft + "px";
312                 else {
313                         var ycw = yc.offsetWidth;
314                         if (typeof ycw == "undefined")
315                                 // Konqueror brain-dead techniques
316                                 ycw = 50;
317                         s.left = (cd.offsetLeft + cd.offsetWidth - ycw) + "px";
318                 }
319                 s.top = (cd.offsetTop + cd.offsetHeight) + "px";
320         }
321 };
322
323 // event handlers
324
325 Calendar.tableMouseUp = function(ev) {
326         var cal = Calendar._C;
327         if (!cal) {
328                 return false;
329         }
330         if (cal.timeout) {
331                 clearTimeout(cal.timeout);
332         }
333         var el = cal.activeDiv;
334         if (!el) {
335                 return false;
336         }
337         var target = Calendar.getTargetElement(ev);
338         ev || (ev = window.event);
339         Calendar.removeClass(el, "active");
340         if (target == el || target.parentNode == el) {
341                 Calendar.cellClick(el, ev);
342         }
343         var mon = Calendar.findMonth(target);
344         var date = null;
345         if (mon) {
346                 date = new Date(cal.date);
347                 if (mon.month != date.getMonth()) {
348                         date.setMonth(mon.month);
349                         cal.setDate(date);
350                         cal.dateClicked = false;
351                         cal.callHandler();
352                 }
353         } else {
354                 var year = Calendar.findYear(target);
355                 if (year) {
356                         date = new Date(cal.date);
357                         if (year.year != date.getFullYear()) {
358                                 date.setFullYear(year.year);
359                                 cal.setDate(date);
360                                 cal.dateClicked = false;
361                                 cal.callHandler();
362                         }
363                 }
364         }
365         with (Calendar) {
366                 removeEvent(document, "mouseup", tableMouseUp);
367                 removeEvent(document, "mouseover", tableMouseOver);
368                 removeEvent(document, "mousemove", tableMouseOver);
369                 cal._hideCombos();
370                 _C = null;
371                 return stopEvent(ev);
372         }
373 };
374
375 Calendar.tableMouseOver = function (ev) {
376         var cal = Calendar._C;
377         if (!cal) {
378                 return;
379         }
380         var el = cal.activeDiv;
381         var target = Calendar.getTargetElement(ev);
382         if (target == el || target.parentNode == el) {
383                 Calendar.addClass(el, "hilite active");
384                 Calendar.addClass(el.parentNode, "rowhilite");
385         } else {
386                 if (typeof el.navtype == "undefined" || (el.navtype != 50 && (el.navtype == 0 || Math.abs(el.navtype) > 2)))
387                         Calendar.removeClass(el, "active");
388                 Calendar.removeClass(el, "hilite");
389                 Calendar.removeClass(el.parentNode, "rowhilite");
390         }
391         ev || (ev = window.event);
392         if (el.navtype == 50 && target != el) {
393                 var pos = Calendar.getAbsolutePos(el);
394                 var w = el.offsetWidth;
395                 var x = ev.clientX;
396                 var dx;
397                 var decrease = true;
398                 if (x > pos.x + w) {
399                         dx = x - pos.x - w;
400                         decrease = false;
401                 } else
402                         dx = pos.x - x;
403
404                 if (dx < 0) dx = 0;
405                 var range = el._range;
406                 var current = el._current;
407                 var count = Math.floor(dx / 10) % range.length;
408                 for (var i = range.length; --i >= 0;)
409                         if (range[i] == current)
410                                 break;
411                 while (count-- > 0)
412                         if (decrease) {
413                                 if (--i < 0)
414                                         i = range.length - 1;
415                         } else if ( ++i >= range.length )
416                                 i = 0;
417                 var newval = range[i];
418                 el.firstChild.data = newval;
419
420                 cal.onUpdateTime();
421         }
422         var mon = Calendar.findMonth(target);
423         if (mon) {
424                 if (mon.month != cal.date.getMonth()) {
425                         if (cal.hilitedMonth) {
426                                 Calendar.removeClass(cal.hilitedMonth, "hilite");
427                         }
428                         Calendar.addClass(mon, "hilite");
429                         cal.hilitedMonth = mon;
430                 } else if (cal.hilitedMonth) {
431                         Calendar.removeClass(cal.hilitedMonth, "hilite");
432                 }
433         } else {
434                 if (cal.hilitedMonth) {
435                         Calendar.removeClass(cal.hilitedMonth, "hilite");
436                 }
437                 var year = Calendar.findYear(target);
438                 if (year) {
439                         if (year.year != cal.date.getFullYear()) {
440                                 if (cal.hilitedYear) {
441                                         Calendar.removeClass(cal.hilitedYear, "hilite");
442                                 }
443                                 Calendar.addClass(year, "hilite");
444                                 cal.hilitedYear = year;
445                         } else if (cal.hilitedYear) {
446                                 Calendar.removeClass(cal.hilitedYear, "hilite");
447                         }
448                 } else if (cal.hilitedYear) {
449                         Calendar.removeClass(cal.hilitedYear, "hilite");
450                 }
451         }
452         return Calendar.stopEvent(ev);
453 };
454
455 Calendar.tableMouseDown = function (ev) {
456         if (Calendar.getTargetElement(ev) == Calendar.getElement(ev)) {
457                 return Calendar.stopEvent(ev);
458         }
459 };
460
461 Calendar.calDragIt = function (ev) {
462         var cal = Calendar._C;
463         if (!(cal && cal.dragging)) {
464                 return false;
465         }
466         var posX;
467         var posY;
468         if (Calendar.is_ie) {
469                 posY = window.event.clientY + document.body.scrollTop;
470                 posX = window.event.clientX + document.body.scrollLeft;
471         } else {
472                 posX = ev.pageX;
473                 posY = ev.pageY;
474         }
475         cal.hideShowCovered();
476         var st = cal.element.style;
477         st.left = (posX - cal.xOffs) + "px";
478         st.top = (posY - cal.yOffs) + "px";
479         return Calendar.stopEvent(ev);
480 };
481
482 Calendar.calDragEnd = function (ev) {
483         var cal = Calendar._C;
484         if (!cal) {
485                 return false;
486         }
487         cal.dragging = false;
488         with (Calendar) {
489                 removeEvent(document, "mousemove", calDragIt);
490                 removeEvent(document, "mouseup", calDragEnd);
491                 tableMouseUp(ev);
492         }
493         cal.hideShowCovered();
494 };
495
496 Calendar.dayMouseDown = function(ev) {
497         var el = Calendar.getElement(ev);
498         if (el.disabled) {
499                 return false;
500         }
501         var cal = el.calendar;
502         cal.activeDiv = el;
503         Calendar._C = cal;
504         if (el.navtype != 300) with (Calendar) {
505                 if (el.navtype == 50) {
506                         el._current = el.firstChild.data;
507                         addEvent(document, "mousemove", tableMouseOver);
508                 } else
509                         addEvent(document, Calendar.is_ie5 ? "mousemove" : "mouseover", tableMouseOver);
510                 addClass(el, "hilite active");
511                 addEvent(document, "mouseup", tableMouseUp);
512         } else if (cal.isPopup) {
513                 cal._dragStart(ev);
514         }
515         if (el.navtype == -1 || el.navtype == 1) {
516                 if (cal.timeout) clearTimeout(cal.timeout);
517                 cal.timeout = setTimeout("Calendar.showMonthsCombo()", 250);
518         } else if (el.navtype == -2 || el.navtype == 2) {
519                 if (cal.timeout) clearTimeout(cal.timeout);
520                 cal.timeout = setTimeout((el.navtype > 0) ? "Calendar.showYearsCombo(true)" : "Calendar.showYearsCombo(false)", 250);
521         } else {
522                 cal.timeout = null;
523         }
524         return Calendar.stopEvent(ev);
525 };
526
527 Calendar.dayMouseDblClick = function(ev) {
528         Calendar.cellClick(Calendar.getElement(ev), ev || window.event);
529         if (Calendar.is_ie) {
530                 document.selection.empty();
531         }
532 };
533
534 Calendar.dayMouseOver = function(ev) {
535         var el = Calendar.getElement(ev);
536         if (Calendar.isRelated(el, ev) || Calendar._C || el.disabled) {
537                 return false;
538         }
539         if (el.ttip) {
540                 if (el.ttip.substr(0, 1) == "_") {
541                         el.ttip = el.caldate.print(el.calendar.ttDateFormat) + el.ttip.substr(1);
542                 }
543                 el.calendar.tooltips.firstChild.data = el.ttip;
544         }
545         if (el.navtype != 300) {
546                 Calendar.addClass(el, "hilite");
547                 if (el.caldate) {
548                         Calendar.addClass(el.parentNode, "rowhilite");
549                 }
550         }
551         return Calendar.stopEvent(ev);
552 };
553
554 Calendar.dayMouseOut = function(ev) {
555         with (Calendar) {
556                 var el = getElement(ev);
557                 if (isRelated(el, ev) || _C || el.disabled) {
558                         return false;
559                 }
560                 removeClass(el, "hilite");
561                 if (el.caldate) {
562                         removeClass(el.parentNode, "rowhilite");
563                 }
564                 el.calendar.tooltips.firstChild.data = _TT["SEL_DATE"];
565                 return stopEvent(ev);
566         }
567 };
568
569 /**
570  *  A generic "click" handler :) handles all types of buttons defined in this
571  *  calendar.
572  */
573 Calendar.cellClick = function(el, ev) {
574         var cal = el.calendar;
575         var closing = false;
576         var newdate = false;
577         var date = null;
578         if (typeof el.navtype == "undefined") {
579                 Calendar.removeClass(cal.currentDateEl, "selected");
580                 Calendar.addClass(el, "selected");
581                 closing = (cal.currentDateEl == el);
582                 if (!closing) {
583                         cal.currentDateEl = el;
584                 }
585                 cal.date = new Date(el.caldate);
586                 date = cal.date;
587                 newdate = true;
588                 // a date was clicked
589                 if (!(cal.dateClicked = !el.otherMonth))
590                         cal._init(cal.firstDayOfWeek, date);
591         } else {
592                 if (el.navtype == 200) {
593                         Calendar.removeClass(el, "hilite");
594                         cal.callCloseHandler();
595                         return;
596                 }
597                 date = (el.navtype == 0) ? new Date() : new Date(cal.date);
598                 // unless "today" was clicked, we assume no date was clicked so
599                 // the selected handler will know not to close the calenar when
600                 // in single-click mode.
601                 // cal.dateClicked = (el.navtype == 0);
602                 cal.dateClicked = false;
603                 var year = date.getFullYear();
604                 var mon = date.getMonth();
605                 function setMonth(m) {
606                         var day = date.getDate();
607                         var max = date.getMonthDays(m);
608                         if (day > max) {
609                                 date.setDate(max);
610                         }
611                         date.setMonth(m);
612                 };
613                 switch (el.navtype) {
614                     case 400:
615                         Calendar.removeClass(el, "hilite");
616                         var text = Calendar._TT["ABOUT"];
617                         if (typeof text != "undefined") {
618                                 text += cal.showsTime ? Calendar._TT["ABOUT_TIME"] : "";
619                         } else {
620                                 // FIXME: this should be removed as soon as lang files get updated!
621                                 text = "Help and about box text is not translated into this language.\n" +
622                                         "If you know this language and you feel generous please update\n" +
623                                         "the corresponding file in \"lang\" subdir to match calendar-en.js\n" +
624                                         "and send it back to <mishoo@infoiasi.ro> to get it into the distribution  ;-)\n\n" +
625                                         "Thank you!\n" +
626                                         "http://dynarch.com/mishoo/calendar.epl\n";
627                         }
628                         alert(text);
629                         return;
630                     case -2:
631                         if (year > cal.minYear) {
632                                 date.setFullYear(year - 1);
633                         }
634                         break;
635                     case -1:
636                         if (mon > 0) {
637                                 setMonth(mon - 1);
638                         } else if (year-- > cal.minYear) {
639                                 date.setFullYear(year);
640                                 setMonth(11);
641                         }
642                         break;
643                     case 1:
644                         if (mon < 11) {
645                                 setMonth(mon + 1);
646                         } else if (year < cal.maxYear) {
647                                 date.setFullYear(year + 1);
648                                 setMonth(0);
649                         }
650                         break;
651                     case 2:
652                         if (year < cal.maxYear) {
653                                 date.setFullYear(year + 1);
654                         }
655                         break;
656                     case 100:
657                         cal.setFirstDayOfWeek(el.fdow);
658                         return;
659                     case 50:
660                         var range = el._range;
661                         var current = el.firstChild.data;
662                         for (var i = range.length; --i >= 0;)
663                                 if (range[i] == current)
664                                         break;
665                         if (ev && ev.shiftKey) {
666                                 if (--i < 0)
667                                         i = range.length - 1;
668                         } else if ( ++i >= range.length )
669                                 i = 0;
670                         var newval = range[i];
671                         el.firstChild.data = newval;
672                         cal.onUpdateTime();
673                         return;
674                     case 0:
675                         // TODAY will bring us here
676                         if ((typeof cal.getDateStatus == "function") && cal.getDateStatus(date, date.getFullYear(), date.getMonth(), date.getDate())) {
677                                 // remember, "date" was previously set to new
678                                 // Date() if TODAY was clicked; thus, it
679                                 // contains today date.
680                                 return false;
681                         }
682                         break;
683                 }
684                 if (!date.equalsTo(cal.date)) {
685                         cal.setDate(date);
686                         newdate = true;
687                 }
688         }
689         if (newdate) {
690                 cal.callHandler();
691         }
692         if (closing) {
693                 Calendar.removeClass(el, "hilite");
694                 cal.callCloseHandler();
695         }
696 };
697
698 // END: CALENDAR STATIC FUNCTIONS
699
700 // BEGIN: CALENDAR OBJECT FUNCTIONS
701
702 /**
703  *  This function creates the calendar inside the given parent.  If _par is
704  *  null than it creates a popup calendar inside the BODY element.  If _par is
705  *  an element, be it BODY, then it creates a non-popup calendar (still
706  *  hidden).  Some properties need to be set before calling this function.
707  */
708 Calendar.prototype.create = function (_par) {
709         var parent = null;
710         if (! _par) {
711                 // default parent is the document body, in which case we create
712                 // a popup calendar.
713                 parent = document.getElementsByTagName("body")[0];
714                 this.isPopup = true;
715         } else {
716                 parent = _par;
717                 this.isPopup = false;
718         }
719         this.date = this.dateStr ? new Date(this.dateStr) : new Date();
720
721         var table = Calendar.createElement("table");
722         this.table = table;
723         table.cellSpacing = 0;
724         table.cellPadding = 0;
725         table.calendar = this;
726         Calendar.addEvent(table, "mousedown", Calendar.tableMouseDown);
727
728         var div = Calendar.createElement("div");
729         this.element = div;
730         div.className = "calendar";
731         if (this.isPopup) {
732                 div.style.position = "absolute";
733                 div.style.display = "none";
734         }
735         div.appendChild(table);
736
737         var thead = Calendar.createElement("thead", table);
738         var cell = null;
739         var row = null;
740
741         var cal = this;
742         var hh = function (text, cs, navtype) {
743                 cell = Calendar.createElement("td", row);
744                 cell.colSpan = cs;
745                 cell.className = "button";
746                 if (navtype != 0 && Math.abs(navtype) <= 2)
747                         cell.className += " nav";
748                 Calendar._add_evs(cell);
749                 cell.calendar = cal;
750                 cell.navtype = navtype;
751                 if (text.substr(0, 1) != "&") {
752                         cell.appendChild(document.createTextNode(text));
753                 }
754                 else {
755                         // FIXME: dirty hack for entities
756                         cell.innerHTML = text;
757                 }
758                 return cell;
759         };
760
761         row = Calendar.createElement("tr", thead);
762         var title_length = 6;
763         (this.isPopup) && --title_length;
764         (this.weekNumbers) && ++title_length;
765
766         hh("?", 1, 400).ttip = Calendar._TT["INFO"];
767         this.title = hh("", title_length, 300);
768         this.title.className = "title";
769         if (this.isPopup) {
770                 this.title.ttip = Calendar._TT["DRAG_TO_MOVE"];
771                 this.title.style.cursor = "move";
772                 hh("&#x00d7;", 1, 200).ttip = Calendar._TT["CLOSE"];
773         }
774
775         row = Calendar.createElement("tr", thead);
776         row.className = "headrow";
777
778         this._nav_py = hh("&#x00ab;", 1, -2);
779         this._nav_py.ttip = Calendar._TT["PREV_YEAR"];
780
781         this._nav_pm = hh("&#x2039;", 1, -1);
782         this._nav_pm.ttip = Calendar._TT["PREV_MONTH"];
783
784         this._nav_now = hh(Calendar._TT["TODAY"], this.weekNumbers ? 4 : 3, 0);
785         this._nav_now.ttip = Calendar._TT["GO_TODAY"];
786
787         this._nav_nm = hh("&#x203a;", 1, 1);
788         this._nav_nm.ttip = Calendar._TT["NEXT_MONTH"];
789
790         this._nav_ny = hh("&#x00bb;", 1, 2);
791         this._nav_ny.ttip = Calendar._TT["NEXT_YEAR"];
792
793         // day names
794         row = Calendar.createElement("tr", thead);
795         row.className = "daynames";
796         if (this.weekNumbers) {
797                 cell = Calendar.createElement("td", row);
798                 cell.className = "name wn";
799                 cell.appendChild(document.createTextNode(Calendar._TT["WK"]));
800         }
801         for (var i = 7; i > 0; --i) {
802                 cell = Calendar.createElement("td", row);
803                 cell.appendChild(document.createTextNode(""));
804                 if (!i) {
805                         cell.navtype = 100;
806                         cell.calendar = this;
807                         Calendar._add_evs(cell);
808                 }
809         }
810         this.firstdayname = (this.weekNumbers) ? row.firstChild.nextSibling : row.firstChild;
811         this._displayWeekdays();
812
813         var tbody = Calendar.createElement("tbody", table);
814         this.tbody = tbody;
815
816         for (i = 6; i > 0; --i) {
817                 row = Calendar.createElement("tr", tbody);
818                 if (this.weekNumbers) {
819                         cell = Calendar.createElement("td", row);
820                         cell.appendChild(document.createTextNode(""));
821                 }
822                 for (var j = 7; j > 0; --j) {
823                         cell = Calendar.createElement("td", row);
824                         cell.appendChild(document.createTextNode(""));
825                         cell.calendar = this;
826                         Calendar._add_evs(cell);
827                 }
828         }
829
830         if (this.showsTime) {
831                 row = Calendar.createElement("tr", tbody);
832                 row.className = "time";
833
834                 cell = Calendar.createElement("td", row);
835                 cell.className = "time";
836                 cell.colSpan = 2;
837                 cell.innerHTML = Calendar._TT["TIME"] || "&nbsp;";
838
839                 cell = Calendar.createElement("td", row);
840                 cell.className = "time";
841                 cell.colSpan = this.weekNumbers ? 4 : 3;
842
843                 (function(){
844                         function makeTimePart(className, init, range_start, range_end) {
845                                 var part = Calendar.createElement("span", cell);
846                                 part.className = className;
847                                 part.appendChild(document.createTextNode(init));
848                                 part.calendar = cal;
849                                 part.ttip = Calendar._TT["TIME_PART"];
850                                 part.navtype = 50;
851                                 part._range = [];
852                                 if (typeof range_start != "number")
853                                         part._range = range_start;
854                                 else {
855                                         for (var i = range_start; i <= range_end; ++i) {
856                                                 var txt;
857                                                 if (i < 10 && range_end >= 10) txt = '0' + i;
858                                                 else txt = '' + i;
859                                                 part._range[part._range.length] = txt;
860                                         }
861                                 }
862                                 Calendar._add_evs(part);
863                                 return part;
864                         };
865                         var hrs = cal.date.getHours();
866                         var mins = cal.date.getMinutes();
867                         var t12 = !cal.time24;
868                         var pm = (hrs > 12);
869                         if (t12 && pm) hrs -= 12;
870                         var H = makeTimePart("hour", hrs, t12 ? 1 : 0, t12 ? 12 : 23);
871                         var span = Calendar.createElement("span", cell);
872                         span.appendChild(document.createTextNode(":"));
873                         span.className = "colon";
874                         var M = makeTimePart("minute", mins, 0, 59);
875                         var AP = null;
876                         cell = Calendar.createElement("td", row);
877                         cell.className = "time";
878                         cell.colSpan = 2;
879                         if (t12)
880                                 AP = makeTimePart("ampm", pm ? "pm" : "am", ["am", "pm"]);
881                         else
882                                 cell.innerHTML = "&nbsp;";
883
884                         cal.onSetTime = function() {
885                                 var hrs = this.date.getHours();
886                                 var mins = this.date.getMinutes();
887                                 var pm = (hrs > 12);
888                                 if (pm && t12) hrs -= 12;
889                                 H.firstChild.data = (hrs < 10) ? ("0" + hrs) : hrs;
890                                 M.firstChild.data = (mins < 10) ? ("0" + mins) : mins;
891                                 if (t12)
892                                         AP.firstChild.data = pm ? "pm" : "am";
893                         };
894
895                         cal.onUpdateTime = function() {
896                                 var date = this.date;
897                                 var h = parseInt(H.firstChild.data, 10);
898                                 if (t12) {
899                                         if (/pm/i.test(AP.firstChild.data) && h < 12)
900                                                 h += 12;
901                                         else if (/am/i.test(AP.firstChild.data) && h == 12)
902                                                 h = 0;
903                                 }
904                                 var d = date.getDate();
905                                 var m = date.getMonth();
906                                 var y = date.getFullYear();
907                                 date.setHours(h);
908                                 date.setMinutes(parseInt(M.firstChild.data, 10));
909                                 date.setFullYear(y);
910                                 date.setMonth(m);
911                                 date.setDate(d);
912                                 this.dateClicked = false;
913                                 this.callHandler();
914                         };
915                 })();
916         } else {
917                 this.onSetTime = this.onUpdateTime = function() {};
918         }
919
920         var tfoot = Calendar.createElement("tfoot", table);
921
922         row = Calendar.createElement("tr", tfoot);
923         row.className = "footrow";
924
925         cell = hh(Calendar._TT["SEL_DATE"], this.weekNumbers ? 8 : 7, 300);
926         cell.className = "ttip";
927         if (this.isPopup) {
928                 cell.ttip = Calendar._TT["DRAG_TO_MOVE"];
929                 cell.style.cursor = "move";
930         }
931         this.tooltips = cell;
932
933         div = Calendar.createElement("div", this.element);
934         this.monthsCombo = div;
935         div.className = "combo";
936         for (i = 0; i < Calendar._MN.length; ++i) {
937                 var mn = Calendar.createElement("div");
938                 mn.className = Calendar.is_ie ? "label-IEfix" : "label";
939                 mn.month = i;
940                 mn.appendChild(document.createTextNode(Calendar._SMN[i]));
941                 div.appendChild(mn);
942         }
943
944         div = Calendar.createElement("div", this.element);
945         this.yearsCombo = div;
946         div.className = "combo";
947         for (i = 12; i > 0; --i) {
948                 var yr = Calendar.createElement("div");
949                 yr.className = Calendar.is_ie ? "label-IEfix" : "label";
950                 yr.appendChild(document.createTextNode(""));
951                 div.appendChild(yr);
952         }
953
954         this._init(this.firstDayOfWeek, this.date);
955         parent.appendChild(this.element);
956 };
957
958 /** keyboard navigation, only for popup calendars */
959 Calendar._keyEvent = function(ev) {
960         if (!window.calendar) {
961                 return false;
962         }
963         (Calendar.is_ie) && (ev = window.event);
964         var cal = window.calendar;
965         var act = (Calendar.is_ie || ev.type == "keypress");
966         if (ev.ctrlKey) {
967                 switch (ev.keyCode) {
968                     case 37: // KEY left
969                         act && Calendar.cellClick(cal._nav_pm);
970                         break;
971                     case 38: // KEY up
972                         act && Calendar.cellClick(cal._nav_py);
973                         break;
974                     case 39: // KEY right
975                         act && Calendar.cellClick(cal._nav_nm);
976                         break;
977                     case 40: // KEY down
978                         act && Calendar.cellClick(cal._nav_ny);
979                         break;
980                     default:
981                         return false;
982                 }
983         } else switch (ev.keyCode) {
984             case 32: // KEY space (now)
985                 Calendar.cellClick(cal._nav_now);
986                 break;
987             case 27: // KEY esc
988                 act && cal.callCloseHandler();
989                 break;
990             case 37: // KEY left
991             case 38: // KEY up
992             case 39: // KEY right
993             case 40: // KEY down
994                 if (act) {
995                         var date = cal.date.getDate() - 1;
996                         var el = cal.currentDateEl;
997                         var ne = null;
998                         var prev = (ev.keyCode == 37) || (ev.keyCode == 38);
999                         switch (ev.keyCode) {
1000                             case 37: // KEY left
1001                                 (--date >= 0) && (ne = cal.ar_days[date]);
1002                                 break;
1003                             case 38: // KEY up
1004                                 date -= 7;
1005                                 (date >= 0) && (ne = cal.ar_days[date]);
1006                                 break;
1007                             case 39: // KEY right
1008                                 (++date < cal.ar_days.length) && (ne = cal.ar_days[date]);
1009                                 break;
1010                             case 40: // KEY down
1011                                 date += 7;
1012                                 (date < cal.ar_days.length) && (ne = cal.ar_days[date]);
1013                                 break;
1014                         }
1015                         if (!ne) {
1016                                 if (prev) {
1017                                         Calendar.cellClick(cal._nav_pm);
1018                                 } else {
1019                                         Calendar.cellClick(cal._nav_nm);
1020                                 }
1021                                 date = (prev) ? cal.date.getMonthDays() : 1;
1022                                 el = cal.currentDateEl;
1023                                 ne = cal.ar_days[date - 1];
1024                         }
1025                         Calendar.removeClass(el, "selected");
1026                         Calendar.addClass(ne, "selected");
1027                         cal.date = new Date(ne.caldate);
1028                         cal.callHandler();
1029                         cal.currentDateEl = ne;
1030                 }
1031                 break;
1032             case 13: // KEY enter
1033                 if (act) {
1034                         cal.callHandler();
1035                         cal.hide();
1036                 }
1037                 break;
1038             default:
1039                 return false;
1040         }
1041         return Calendar.stopEvent(ev);
1042 };
1043
1044 /**
1045  *  (RE)Initializes the calendar to the given date and firstDayOfWeek
1046  */
1047 Calendar.prototype._init = function (firstDayOfWeek, date) {
1048         var today = new Date();
1049         this.table.style.visibility = "hidden";
1050         var year = date.getFullYear();
1051         if (year < this.minYear) {
1052                 year = this.minYear;
1053                 date.setFullYear(year);
1054         } else if (year > this.maxYear) {
1055                 year = this.maxYear;
1056                 date.setFullYear(year);
1057         }
1058         this.firstDayOfWeek = firstDayOfWeek;
1059         this.date = new Date(date);
1060         var month = date.getMonth();
1061         var mday = date.getDate();
1062         var no_days = date.getMonthDays();
1063
1064         // calendar voodoo for computing the first day that would actually be
1065         // displayed in the calendar, even if it's from the previous month.
1066         // WARNING: this is magic. ;-)
1067         date.setDate(1);
1068         var day1 = (date.getDay() - this.firstDayOfWeek) % 7;
1069         if (day1 < 0)
1070                 day1 += 7;
1071         date.setDate(-day1);
1072         date.setDate(date.getDate() + 1);
1073
1074         var row = this.tbody.firstChild;
1075         var MN = Calendar._SMN[month];
1076         var ar_days = new Array();
1077         var weekend = Calendar._TT["WEEKEND"];
1078         for (var i = 0; i < 6; ++i, row = row.nextSibling) {
1079                 var cell = row.firstChild;
1080                 if (this.weekNumbers) {
1081                         cell.className = "day wn";
1082                         cell.firstChild.data = date.getWeekNumber();
1083                         cell = cell.nextSibling;
1084                 }
1085                 row.className = "daysrow";
1086                 var hasdays = false;
1087                 for (var j = 0; j < 7; ++j, cell = cell.nextSibling, date.setDate(date.getDate() + 1)) {
1088                         var iday = date.getDate();
1089                         var wday = date.getDay();
1090                         cell.className = "day";
1091                         var current_month = (date.getMonth() == month);
1092                         if (!current_month) {
1093                                 if (this.showsOtherMonths) {
1094                                         cell.className += " othermonth";
1095                                         cell.otherMonth = true;
1096                                 } else {
1097                                         cell.className = "emptycell";
1098                                         cell.innerHTML = "&nbsp;";
1099                                         cell.disabled = true;
1100                                         continue;
1101                                 }
1102                         } else {
1103                                 cell.otherMonth = false;
1104                                 hasdays = true;
1105                         }
1106                         cell.disabled = false;
1107                         cell.firstChild.data = iday;
1108                         if (typeof this.getDateStatus == "function") {
1109                                 var status = this.getDateStatus(date, year, month, iday);
1110                                 if (status === true) {
1111                                         cell.className += " disabled";
1112                                         cell.disabled = true;
1113                                 } else {
1114                                         if (/disabled/i.test(status))
1115                                                 cell.disabled = true;
1116                                         cell.className += " " + status;
1117                                 }
1118                         }
1119                         if (!cell.disabled) {
1120                                 ar_days[ar_days.length] = cell;
1121                                 cell.caldate = new Date(date);
1122                                 cell.ttip = "_";
1123                                 if (current_month && iday == mday) {
1124                                         cell.className += " selected";
1125                                         this.currentDateEl = cell;
1126                                 }
1127                                 if (date.getFullYear() == today.getFullYear() &&
1128                                     date.getMonth() == today.getMonth() &&
1129                                     iday == today.getDate()) {
1130                                         cell.className += " today";
1131                                         cell.ttip += Calendar._TT["PART_TODAY"];
1132                                 }
1133                                 if (weekend.indexOf(wday.toString()) != -1) {
1134                                         cell.className += cell.otherMonth ? " oweekend" : " weekend";
1135                                 }
1136                         }
1137                 }
1138                 if (!(hasdays || this.showsOtherMonths))
1139                         row.className = "emptyrow";
1140         }
1141         this.ar_days = ar_days;
1142         this.title.firstChild.data = Calendar._MN[month] + ", " + year;
1143         this.onSetTime();
1144         this.table.style.visibility = "visible";
1145         // PROFILE
1146         // this.tooltips.firstChild.data = "Generated in " + ((new Date()) - today) + " ms";
1147 };
1148
1149 /**
1150  *  Calls _init function above for going to a certain date (but only if the
1151  *  date is different than the currently selected one).
1152  */
1153 Calendar.prototype.setDate = function (date) {
1154         if (!date.equalsTo(this.date)) {
1155                 this._init(this.firstDayOfWeek, date);
1156         }
1157 };
1158
1159 /**
1160  *  Refreshes the calendar.  Useful if the "disabledHandler" function is
1161  *  dynamic, meaning that the list of disabled date can change at runtime.
1162  *  Just * call this function if you think that the list of disabled dates
1163  *  should * change.
1164  */
1165 Calendar.prototype.refresh = function () {
1166         this._init(this.firstDayOfWeek, this.date);
1167 };
1168
1169 /** Modifies the "firstDayOfWeek" parameter (pass 0 for Synday, 1 for Monday, etc.). */
1170 Calendar.prototype.setFirstDayOfWeek = function (firstDayOfWeek) {
1171         this._init(firstDayOfWeek, this.date);
1172         this._displayWeekdays();
1173 };
1174
1175 /**
1176  *  Allows customization of what dates are enabled.  The "unaryFunction"
1177  *  parameter must be a function object that receives the date (as a JS Date
1178  *  object) and returns a boolean value.  If the returned value is true then
1179  *  the passed date will be marked as disabled.
1180  */
1181 Calendar.prototype.setDateStatusHandler = Calendar.prototype.setDisabledHandler = function (unaryFunction) {
1182         this.getDateStatus = unaryFunction;
1183 };
1184
1185 /** Customization of allowed year range for the calendar. */
1186 Calendar.prototype.setRange = function (a, z) {
1187         this.minYear = a;
1188         this.maxYear = z;
1189 };
1190
1191 /** Calls the first user handler (selectedHandler). */
1192 Calendar.prototype.callHandler = function () {
1193         if (this.onSelected) {
1194                 this.onSelected(this, this.date.print(this.dateFormat));
1195         }
1196 };
1197
1198 /** Calls the second user handler (closeHandler). */
1199 Calendar.prototype.callCloseHandler = function () {
1200         if (this.onClose) {
1201                 this.onClose(this);
1202         }
1203         // this.hideShowCovered();
1204 };
1205
1206 /** Removes the calendar object from the DOM tree and destroys it. */
1207 Calendar.prototype.destroy = function () {
1208         var el = this.element.parentNode;
1209         el.removeChild(this.element);
1210         Calendar._C = null;
1211         window.calendar = null;
1212 };
1213
1214 /**
1215  *  Moves the calendar element to a different section in the DOM tree (changes
1216  *  its parent).
1217  */
1218 Calendar.prototype.reparent = function (new_parent) {
1219         var el = this.element;
1220         el.parentNode.removeChild(el);
1221         new_parent.appendChild(el);
1222 };
1223
1224 // This gets called when the user presses a mouse button anywhere in the
1225 // document, if the calendar is shown.  If the click was outside the open
1226 // calendar this function closes it.
1227 Calendar._checkCalendar = function(ev) {
1228         if (!window.calendar) {
1229                 return false;
1230         }
1231         var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
1232         for (; el != null && el != calendar.element; el = el.parentNode);
1233         if (el == null) {
1234                 // calls closeHandler which should hide the calendar.
1235                 window.calendar.callCloseHandler();
1236                 return Calendar.stopEvent(ev);
1237         }
1238 };
1239
1240 /** Shows the calendar. */
1241 Calendar.prototype.show = function () {
1242         var rows = this.table.getElementsByTagName("tr");
1243         for (var i = rows.length; i > 0;) {
1244                 var row = rows[--i];
1245                 Calendar.removeClass(row, "rowhilite");
1246                 var cells = row.getElementsByTagName("td");
1247                 for (var j = cells.length; j > 0;) {
1248                         var cell = cells[--j];
1249                         Calendar.removeClass(cell, "hilite");
1250                         Calendar.removeClass(cell, "active");
1251                 }
1252         }
1253         this.element.style.display = "block";
1254         this.hidden = false;
1255         if (this.isPopup) {
1256                 window.calendar = this;
1257                 Calendar.addEvent(document, "keydown", Calendar._keyEvent);
1258                 Calendar.addEvent(document, "keypress", Calendar._keyEvent);
1259                 Calendar.addEvent(document, "mousedown", Calendar._checkCalendar);
1260         }
1261         // this.hideShowCovered();
1262 };
1263
1264 /**
1265  *  Hides the calendar.  Also removes any "hilite" from the class of any TD
1266  *  element.
1267  */
1268 Calendar.prototype.hide = function () {
1269         if (this.isPopup) {
1270                 Calendar.removeEvent(document, "keydown", Calendar._keyEvent);
1271                 Calendar.removeEvent(document, "keypress", Calendar._keyEvent);
1272                 Calendar.removeEvent(document, "mousedown", Calendar._checkCalendar);
1273         }
1274         this.element.style.display = "none";
1275         this.hidden = true;
1276         // this.hideShowCovered();
1277 };
1278
1279 /**
1280  *  Shows the calendar at a given absolute position (beware that, depending on
1281  *  the calendar element style -- position property -- this might be relative
1282  *  to the parent's containing rectangle).
1283  */
1284 Calendar.prototype.showAt = function (x, y) {
1285         var s = this.element.style;
1286         s.left = x + "px";
1287         s.top = y + "px";
1288         this.show();
1289 };
1290
1291 /** Shows the calendar near a given element. */
1292 Calendar.prototype.showAtElement = function (el, opts) {
1293         var self = this;
1294         var p = Calendar.getAbsolutePos(el);
1295         if (!opts || typeof opts != "string") {
1296                 this.showAt(p.x, p.y + el.offsetHeight);
1297                 return true;
1298         }
1299         function fixPosition(box) {
1300                 if (box.x < 0)
1301                         box.x = 0;
1302                 if (box.y < 0)
1303                         box.y = 0;
1304                 var cp = document.createElement("div");
1305                 var s = cp.style;
1306                 s.position = "absolute";
1307                 s.right = s.bottom = s.width = s.height = "0px";
1308                 document.body.appendChild(cp);
1309                 var br = Calendar.getAbsolutePos(cp);
1310                 document.body.removeChild(cp);
1311                 if (Calendar.is_ie) {
1312                         br.y += document.body.scrollTop;
1313                         br.x += document.body.scrollLeft;
1314                 } else {
1315                         br.y += window.scrollY;
1316                         br.x += window.scrollX;
1317                 }
1318                 var tmp = box.x + box.width - br.x;
1319                 if (tmp > 0) box.x -= tmp;
1320                 tmp = box.y + box.height - br.y;
1321                 if (tmp > 0) box.y -= tmp;
1322         };
1323         this.element.style.display = "block";
1324         Calendar.continuation_for_the_fucking_khtml_browser = function() {
1325                 var w = self.element.offsetWidth;
1326                 var h = self.element.offsetHeight;
1327                 self.element.style.display = "none";
1328                 var valign = opts.substr(0, 1);
1329                 var halign = "l";
1330                 if (opts.length > 1) {
1331                         halign = opts.substr(1, 1);
1332                 }
1333                 // vertical alignment
1334                 switch (valign) {
1335                     case "T": p.y -= h; break;
1336                     case "B": p.y += el.offsetHeight; break;
1337                     case "C": p.y += (el.offsetHeight - h) / 2; break;
1338                     case "t": p.y += el.offsetHeight - h; break;
1339                     case "b": break; // already there
1340                 }
1341                 // horizontal alignment
1342                 switch (halign) {
1343                     case "L": p.x -= w; break;
1344                     case "R": p.x += el.offsetWidth; break;
1345                     case "C": p.x += (el.offsetWidth - w) / 2; break;
1346                     case "r": p.x += el.offsetWidth - w; break;
1347                     case "l": break; // already there
1348                 }
1349                 p.width = w;
1350                 p.height = h + 40;
1351                 self.monthsCombo.style.display = "none";
1352                 fixPosition(p);
1353                 self.showAt(p.x, p.y);
1354         };
1355         if (Calendar.is_khtml)
1356                 setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
1357         else
1358                 Calendar.continuation_for_the_fucking_khtml_browser();
1359 };
1360
1361 /** Customizes the date format. */
1362 Calendar.prototype.setDateFormat = function (str) {
1363         this.dateFormat = str;
1364 };
1365
1366 /** Customizes the tooltip date format. */
1367 Calendar.prototype.setTtDateFormat = function (str) {
1368         this.ttDateFormat = str;
1369 };
1370
1371 /**
1372  *  Tries to identify the date represented in a string.  If successful it also
1373  *  calls this.setDate which moves the calendar to the given date.
1374  */
1375 Calendar.prototype.parseDate = function (str, fmt) {
1376         var y = 0;
1377         var m = -1;
1378         var d = 0;
1379         var a = str.split(/\W+/);
1380         if (!fmt) {
1381                 fmt = this.dateFormat;
1382         }
1383         var b = fmt.match(/%./g);
1384         var i = 0, j = 0;
1385         var hr = 0;
1386         var min = 0;
1387         for (i = 0; i < a.length; ++i) {
1388                 if (!a[i])
1389                         continue;
1390                 switch (b[i]) {
1391                     case "%d":
1392                     case "%e":
1393                         d = parseInt(a[i], 10);
1394                         break;
1395
1396                     case "%m":
1397                         m = parseInt(a[i], 10) - 1;
1398                         break;
1399
1400                     case "%Y":
1401                     case "%y":
1402                         y = parseInt(a[i], 10);
1403                         (y < 100) && (y += (y > 29) ? 1900 : 2000);
1404                         break;
1405
1406                     case "%b":
1407                     case "%B":
1408                         for (j = 0; j < 12; ++j) {
1409                                 if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { m = j; break; }
1410                         }
1411                         break;
1412
1413                     case "%H":
1414                     case "%I":
1415                     case "%k":
1416                     case "%l":
1417                         hr = parseInt(a[i], 10);
1418                         break;
1419
1420                     case "%P":
1421                     case "%p":
1422                         if (/pm/i.test(a[i]) && hr < 12)
1423                                 hr += 12;
1424                         break;
1425
1426                     case "%M":
1427                         min = parseInt(a[i], 10);
1428                         break;
1429                 }
1430         }
1431         if (y != 0 && m != -1 && d != 0) {
1432                 this.setDate(new Date(y, m, d, hr, min, 0));
1433                 return;
1434         }
1435         y = 0; m = -1; d = 0;
1436         for (i = 0; i < a.length; ++i) {
1437                 if (a[i].search(/[a-zA-Z]+/) != -1) {
1438                         var t = -1;
1439                         for (j = 0; j < 12; ++j) {
1440                                 if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { t = j; break; }
1441                         }
1442                         if (t != -1) {
1443                                 if (m != -1) {
1444                                         d = m+1;
1445                                 }
1446                                 m = t;
1447                         }
1448                 } else if (parseInt(a[i], 10) <= 12 && m == -1) {
1449                         m = a[i]-1;
1450                 } else if (parseInt(a[i], 10) > 31 && y == 0) {
1451                         y = parseInt(a[i], 10);
1452                         (y < 100) && (y += (y > 29) ? 1900 : 2000);
1453                 } else if (d == 0) {
1454                         d = a[i];
1455                 }
1456         }
1457         if (y == 0) {
1458                 var today = new Date();
1459                 y = today.getFullYear();
1460         }
1461         if (m != -1 && d != 0) {
1462                 this.setDate(new Date(y, m, d, hr, min, 0));
1463         }
1464 };
1465
1466 /* Calendar.prototype.hideShowCovered = function () {
1467         var self = this;
1468         Calendar.continuation_for_the_fucking_khtml_browser = function() {
1469                 function getVisib(obj){
1470                         var value = obj.style.visibility;
1471                         if (!value) {
1472                                 if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // Gecko, W3C
1473                                         if (!Calendar.is_khtml)
1474                                                 value = document.defaultView.
1475                                                         getComputedStyle(obj, "").getPropertyValue("visibility");
1476                                         else
1477                                                 value = '';
1478                                 } else if (obj.currentStyle) { // IE
1479                                         value = obj.currentStyle.visibility;
1480                                 } else
1481                                         value = '';
1482                         }
1483                         return value;
1484                 };
1485
1486                 var tags = new Array("applet", "iframe", "select");
1487                 var el = self.element;
1488
1489                 var p = Calendar.getAbsolutePos(el);
1490                 var EX1 = p.x;
1491                 var EX2 = el.offsetWidth + EX1;
1492                 var EY1 = p.y;
1493                 var EY2 = el.offsetHeight + EY1;
1494
1495                 for (var k = tags.length; k > 0; ) {
1496                         var ar = document.getElementsByTagName(tags[--k]);
1497                         var cc = null;
1498
1499                         for (var i = ar.length; i > 0;) {
1500                                 cc = ar[--i];
1501
1502                                 p = Calendar.getAbsolutePos(cc);
1503                                 var CX1 = p.x;
1504                                 var CX2 = cc.offsetWidth + CX1;
1505                                 var CY1 = p.y;
1506                                 var CY2 = cc.offsetHeight + CY1;
1507
1508                                 if (self.hidden || (CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {
1509                                         if (!cc.__msh_save_visibility) {
1510                                                 cc.__msh_save_visibility = getVisib(cc);
1511                                         }
1512                                         cc.style.visibility = cc.__msh_save_visibility;
1513                                 } else {
1514                                         if (!cc.__msh_save_visibility) {
1515                                                 cc.__msh_save_visibility = getVisib(cc);
1516                                         }
1517                                         cc.style.visibility = "hidden";
1518                                 }
1519                         }
1520                 }
1521         };
1522         if (Calendar.is_khtml)
1523                 setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
1524         else
1525                 Calendar.continuation_for_the_fucking_khtml_browser();
1526 }; */
1527
1528 /** Internal function; it displays the bar with the names of the weekday. */
1529 Calendar.prototype._displayWeekdays = function () {
1530         var fdow = this.firstDayOfWeek;
1531         var cell = this.firstdayname;
1532         var weekend = Calendar._TT["WEEKEND"];
1533         for (var i = 0; i < 7; ++i) {
1534                 cell.className = "day name";
1535                 var realday = (i + fdow) % 7;
1536                 if (i) {
1537                         cell.ttip = Calendar._TT["DAY_FIRST"].replace("%s", Calendar._DN[realday]);
1538                         cell.navtype = 100;
1539                         cell.calendar = this;
1540                         cell.fdow = realday;
1541                         Calendar._add_evs(cell);
1542                 }
1543                 if (weekend.indexOf(realday.toString()) != -1) {
1544                         Calendar.addClass(cell, "weekend");
1545                 }
1546                 cell.firstChild.data = Calendar._SDN[(i + fdow) % 7];
1547                 cell = cell.nextSibling;
1548         }
1549 };
1550
1551 /** Internal function.  Hides all combo boxes that might be displayed. */
1552 Calendar.prototype._hideCombos = function () {
1553         this.monthsCombo.style.display = "none";
1554         this.yearsCombo.style.display = "none";
1555 };
1556
1557 /** Internal function.  Starts dragging the element. */
1558 Calendar.prototype._dragStart = function (ev) {
1559         if (this.dragging) {
1560                 return;
1561         }
1562         this.dragging = true;
1563         var posX;
1564         var posY;
1565         if (Calendar.is_ie) {
1566                 posY = window.event.clientY + document.body.scrollTop;
1567                 posX = window.event.clientX + document.body.scrollLeft;
1568         } else {
1569                 posY = ev.clientY + window.scrollY;
1570                 posX = ev.clientX + window.scrollX;
1571         }
1572         var st = this.element.style;
1573         this.xOffs = posX - parseInt(st.left);
1574         this.yOffs = posY - parseInt(st.top);
1575         with (Calendar) {
1576                 addEvent(document, "mousemove", calDragIt);
1577                 addEvent(document, "mouseup", calDragEnd);
1578         }
1579 };
1580
1581 // BEGIN: DATE OBJECT PATCHES
1582
1583 /** Adds the number of days array to the Date object. */
1584 Date._MD = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
1585
1586 /** Constants used for time computations */
1587 Date.SECOND = 1000 /* milliseconds */;
1588 Date.MINUTE = 60 * Date.SECOND;
1589 Date.HOUR   = 60 * Date.MINUTE;
1590 Date.DAY    = 24 * Date.HOUR;
1591 Date.WEEK   =  7 * Date.DAY;
1592
1593 /** Returns the number of days in the current month */
1594 Date.prototype.getMonthDays = function(month) {
1595         var year = this.getFullYear();
1596         if (typeof month == "undefined") {
1597                 month = this.getMonth();
1598         }
1599         if (((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) && month == 1) {
1600                 return 29;
1601         } else {
1602                 return Date._MD[month];
1603         }
1604 };
1605
1606 /** Returns the number of day in the year. */
1607 Date.prototype.getDayOfYear = function() {
1608         var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
1609         var then = new Date(this.getFullYear(), 0, 0, 0, 0, 0);
1610         var time = now - then;
1611         return Math.floor(time / Date.DAY);
1612 };
1613
1614 /** Returns the number of the week in year, as defined in ISO 8601. */
1615 Date.prototype.getWeekNumber = function() {
1616         var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
1617         var DoW = d.getDay();
1618         d.setDate(d.getDate() - (DoW + 6) % 7 + 3); // Nearest Thu
1619         var ms = d.valueOf(); // GMT
1620         d.setMonth(0);
1621         d.setDate(4); // Thu in Week 1
1622         return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1;
1623 };
1624
1625 /** Checks dates equality (ignores time) */
1626 Date.prototype.equalsTo = function(date) {
1627         return ((this.getFullYear() == date.getFullYear()) &&
1628                 (this.getMonth() == date.getMonth()) &&
1629                 (this.getDate() == date.getDate()) &&
1630                 (this.getHours() == date.getHours()) &&
1631                 (this.getMinutes() == date.getMinutes()));
1632 };
1633
1634 /** Prints the date in a string according to the given format. */
1635 Date.prototype.print = function (str) {
1636         var m = this.getMonth();
1637         var d = this.getDate();
1638         var y = this.getFullYear();
1639         var wn = this.getWeekNumber();
1640         var w = this.getDay();
1641         var s = {};
1642         var hr = this.getHours();
1643         var pm = (hr >= 12);
1644         var ir = (pm) ? (hr - 12) : hr;
1645         var dy = this.getDayOfYear();
1646         if (ir == 0)
1647                 ir = 12;
1648         var min = this.getMinutes();
1649         var sec = this.getSeconds();
1650         s["%a"] = Calendar._SDN[w]; // abbreviated weekday name [FIXME: I18N]
1651         s["%A"] = Calendar._DN[w]; // full weekday name
1652         s["%b"] = Calendar._SMN[m]; // abbreviated month name [FIXME: I18N]
1653         s["%B"] = Calendar._MN[m]; // full month name
1654         // FIXME: %c : preferred date and time representation for the current locale
1655         s["%C"] = 1 + Math.floor(y / 100); // the century number
1656         s["%d"] = (d < 10) ? ("0" + d) : d; // the day of the month (range 01 to 31)
1657         s["%e"] = d; // the day of the month (range 1 to 31)
1658         // FIXME: %D : american date style: %m/%d/%y
1659         // FIXME: %E, %F, %G, %g, %h (man strftime)
1660         s["%H"] = (hr < 10) ? ("0" + hr) : hr; // hour, range 00 to 23 (24h format)
1661         s["%I"] = (ir < 10) ? ("0" + ir) : ir; // hour, range 01 to 12 (12h format)
1662         s["%j"] = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy; // day of the year (range 001 to 366)
1663         s["%k"] = hr;           // hour, range 0 to 23 (24h format)
1664         s["%l"] = ir;           // hour, range 1 to 12 (12h format)
1665         s["%m"] = (m < 9) ? ("0" + (1+m)) : (1+m); // month, range 01 to 12
1666         s["%M"] = (min < 10) ? ("0" + min) : min; // minute, range 00 to 59
1667         s["%n"] = "\n";         // a newline character
1668         s["%p"] = pm ? "PM" : "AM";
1669         s["%P"] = pm ? "pm" : "am";
1670         // FIXME: %r : the time in am/pm notation %I:%M:%S %p
1671         // FIXME: %R : the time in 24-hour notation %H:%M
1672         s["%s"] = Math.floor(this.getTime() / 1000);
1673         s["%S"] = (sec < 10) ? ("0" + sec) : sec; // seconds, range 00 to 59
1674         s["%t"] = "\t";         // a tab character
1675         // FIXME: %T : the time in 24-hour notation (%H:%M:%S)
1676         s["%U"] = s["%W"] = s["%V"] = (wn < 10) ? ("0" + wn) : wn;
1677         s["%u"] = w + 1;        // the day of the week (range 1 to 7, 1 = MON)
1678         s["%w"] = w;            // the day of the week (range 0 to 6, 0 = SUN)
1679         // FIXME: %x : preferred date representation for the current locale without the time
1680         // FIXME: %X : preferred time representation for the current locale without the date
1681         s["%y"] = ('' + y).substr(2, 2); // year without the century (range 00 to 99)
1682         s["%Y"] = y;            // year with the century
1683         s["%%"] = "%";          // a literal '%' character
1684
1685         var re = /%./g;
1686         if (!Calendar.is_ie5)
1687                 return str.replace(re, function (par) { return s[par] || par; });
1688
1689         var a = str.match(re);
1690         for (var i = 0; i < a.length; i++) {
1691                 var tmp = s[a[i]];
1692                 if (tmp) {
1693                         re = new RegExp(a[i], 'g');
1694                         str = str.replace(re, tmp);
1695                 }
1696         }
1697
1698         return str;
1699 };
1700
1701 Date.prototype.__msh_oldSetFullYear = Date.prototype.setFullYear;
1702 Date.prototype.setFullYear = function(y) {
1703         var d = new Date(this);
1704         d.__msh_oldSetFullYear(y);
1705         if (d.getMonth() != this.getMonth())
1706                 this.setDate(28);
1707         this.__msh_oldSetFullYear(y);
1708 };
1709
1710 // END: DATE OBJECT PATCHES
1711
1712
1713 // global object that remembers the calendar
1714 window.calendar = null;