source: trunk/phpgwapi/js/jscalendar/calendar.js @ 2

Revision 2, 48.3 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

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