source: branches/2.0/phpgwapi/js/jscalendar/calendar.js @ 1647

Revision 1647, 49.0 KB checked in by wmerlotto, 15 years ago (diff)

Ticket #687 - Aplicando a alteração realizada na revisão [1642].

  • 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        var iday = date.getDate();
1106        var wday = date.getDay();
1107        var first = true;
1108        for (var i = 0; i < 6; ++i, row = row.nextSibling) {
1109                var cell = row.firstChild;
1110                if (this.weekNumbers) {
1111                        cell.className = "day wn";
1112                        Calendar._setCellText(cell,date.getWeekNumber());
1113                        if (this.hasWeekHandler) cell.caldate = new Date(date);
1114                        cell = cell.nextSibling;
1115                }
1116                row.className = "daysrow";
1117                var hasdays = false;
1118                for (var j = 0; j < 7; ++j, cell = cell.nextSibling, date.setDate(date.getDate() + 1)) {
1119
1120                        // Para evitar repetição de dias quando a instrução date.setDate(date.getDate() + 1)
1121                        // não incrementa a variável date corretamente.
1122                        if (!first && date.getDate() == iday)
1123                        {
1124                            newday = date.getDate() + 1;
1125                            date.setDate(newday);
1126                        }
1127                        else
1128                        {
1129                                first = false;
1130                        }
1131
1132                        iday = date.getDate();
1133                        wday = date.getDay();
1134                        cell.className = "day";
1135                        var current_month = (date.getMonth() == month);
1136                        if (!current_month) {
1137                                if (this.showsOtherMonths) {
1138                                        cell.className += " othermonth";
1139                                        cell.otherMonth = true;
1140                                } else {
1141                                        cell.className = "emptycell";
1142                                        cell.innerHTML = "&nbsp;";
1143                                        cell.disabled = true;
1144                                        continue;
1145                                }
1146                        } else {
1147                                cell.otherMonth = false;
1148                                hasdays = true;
1149                        }
1150                        cell.disabled = false;
1151                        Calendar._setCellText(cell,iday);
1152                        if (typeof this.getDateStatus == "function") {
1153                                var status = this.getDateStatus(date, year, month, iday);
1154                                if (status === true) {
1155                                        cell.className += " disabled";
1156                                        cell.disabled = true;
1157                                } else {
1158                                        if (/disabled/i.test(status))
1159                                                cell.disabled = true;
1160                                        cell.className += " " + status;
1161                                }
1162                        }
1163                        if (!cell.disabled) {
1164                                ar_days[ar_days.length] = cell;
1165                                cell.caldate = new Date(date);
1166                                cell.ttip = "_";
1167                                if (current_month && iday == mday) {
1168                                        cell.className += " selected";
1169                                        this.currentDateEl = cell;
1170                                }
1171                                if (date.getFullYear() == today.getFullYear() &&
1172                                    date.getMonth() == today.getMonth() &&
1173                                    iday == today.getDate()) {
1174                                        cell.className += " today";
1175                                        cell.ttip += Calendar._TT["PART_TODAY"];
1176                                }
1177                                if (weekend.indexOf(wday.toString()) != -1) {
1178                                        cell.className += cell.otherMonth ? " oweekend" : " weekend";
1179                                }
1180                        }
1181                }
1182                if (!(hasdays || this.showsOtherMonths))
1183                        row.className = "emptyrow";
1184        }
1185        this.ar_days = ar_days;
1186        Calendar._setCellText(this.title,this.params ? this.date.print(this.params.titleFormat) : Calendar._MN[month] + ", " + year);
1187        this.onSetTime();
1188        this.table.style.visibility = "visible";
1189        // PROFILE
1190        // Calendar._setCellText(this.tooltips,"Generated in " + ((new Date()) - today) + " ms");
1191};
1192
1193/**
1194 *  Calls _init function above for going to a certain date (but only if the
1195 *  date is different than the currently selected one).
1196 */
1197Calendar.prototype.setDate = function (date) {
1198        if (!date.equalsTo(this.date)) {
1199                this._init(this.firstDayOfWeek, date);
1200        }
1201};
1202
1203/**
1204 *  Refreshes the calendar.  Useful if the "disabledHandler" function is
1205 *  dynamic, meaning that the list of disabled date can change at runtime.
1206 *  Just * call this function if you think that the list of disabled dates
1207 *  should * change.
1208 */
1209Calendar.prototype.refresh = function () {
1210        this._init(this.firstDayOfWeek, this.date);
1211};
1212
1213/** Modifies the "firstDayOfWeek" parameter (pass 0 for Synday, 1 for Monday, etc.). */
1214Calendar.prototype.setFirstDayOfWeek = function (firstDayOfWeek) {
1215        this._init(firstDayOfWeek, this.date);
1216        this._displayWeekdays();
1217};
1218
1219/**
1220 *  Allows customization of what dates are enabled.  The "unaryFunction"
1221 *  parameter must be a function object that receives the date (as a JS Date
1222 *  object) and returns a boolean value.  If the returned value is true then
1223 *  the passed date will be marked as disabled.
1224 */
1225Calendar.prototype.setDateStatusHandler = Calendar.prototype.setDisabledHandler = function (unaryFunction) {
1226        this.getDateStatus = unaryFunction;
1227};
1228
1229/** Customization of allowed year range for the calendar. */
1230Calendar.prototype.setRange = function (a, z) {
1231        this.minYear = a;
1232        this.maxYear = z;
1233};
1234
1235/** Calls the first user handler (selectedHandler). */
1236Calendar.prototype.callHandler = function () {
1237        if (this.onSelected) {
1238                this.onSelected(this, this.date.print(this.dateFormat));
1239        }
1240};
1241
1242/** Calls the week-clicked user handler (selectedHandler). */
1243Calendar.prototype.hasWeekHandler = function () {
1244        return this.params && this.params.flat && this.params.flatWeekCallback;
1245};
1246
1247Calendar.prototype.callWeekHandler = function (weekstart) {
1248        if (this.hasWeekHandler()) {
1249                this.params.flatWeekCallback(this, weekstart);
1250        }
1251};
1252
1253/** Calls the week-clicked user handler (selectedHandler). */
1254Calendar.prototype.hasMonthHandler = function () {
1255        return this.params && this.params.flat && this.params.flatMonthCallback;
1256};
1257
1258Calendar.prototype.callMonthHandler = function () {
1259        if (this.hasMonthHandler()) {
1260                var monthstart = new Date(this.date);
1261                monthstart.setDate(1);
1262                this.params.flatMonthCallback(this, monthstart);
1263        }
1264};
1265
1266/** Calls the second user handler (closeHandler). */
1267Calendar.prototype.callCloseHandler = function () {
1268        if (this.onClose) {
1269                this.onClose(this);
1270        }
1271        this.hideShowCovered();
1272};
1273
1274/** Removes the calendar object from the DOM tree and destroys it. */
1275Calendar.prototype.destroy = function () {
1276        var el = this.element.parentNode;
1277        el.removeChild(this.element);
1278        Calendar._C = null;
1279        window.calendar = null;
1280};
1281
1282/**
1283 *  Moves the calendar element to a different section in the DOM tree (changes
1284 *  its parent).
1285 */
1286Calendar.prototype.reparent = function (new_parent) {
1287        var el = this.element;
1288        el.parentNode.removeChild(el);
1289        new_parent.appendChild(el);
1290};
1291
1292// This gets called when the user presses a mouse button anywhere in the
1293// document, if the calendar is shown.  If the click was outside the open
1294// calendar this function closes it.
1295Calendar._checkCalendar = function(ev) {
1296        if (!window.calendar) {
1297                return false;
1298        }
1299        var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
1300        for (; el != null && el != calendar.element; el = el.parentNode);
1301        if (el == null) {
1302                // calls closeHandler which should hide the calendar.
1303                window.calendar.callCloseHandler();
1304                return Calendar.stopEvent(ev);
1305        }
1306};
1307
1308/** Shows the calendar. */
1309Calendar.prototype.show = function () {
1310        var rows = this.table.getElementsByTagName("tr");
1311        for (var i = rows.length; i > 0;) {
1312                var row = rows[--i];
1313                Calendar.removeClass(row, "rowhilite");
1314                var cells = row.getElementsByTagName("td");
1315                for (var j = cells.length; j > 0;) {
1316                        var cell = cells[--j];
1317                        Calendar.removeClass(cell, "hilite");
1318                        Calendar.removeClass(cell, "active");
1319                }
1320        }
1321        this.element.style.display = "block";
1322        this.hidden = false;
1323        if (this.isPopup) {
1324                window.calendar = this;
1325                Calendar.addEvent(document, "keydown", Calendar._keyEvent);
1326                Calendar.addEvent(document, "keypress", Calendar._keyEvent);
1327                Calendar.addEvent(document, "mousedown", Calendar._checkCalendar);
1328        }
1329        this.hideShowCovered();
1330        var today = new Date();
1331        // See tickets #252 and #284 in the tracking of expressolivre.org.
1332        // The line below was discussed to solve these problems.
1333        //this._init(1,today);
1334        if (Calendar.is_ie5_mac && !this.isPopup)
1335                this.refresh();         // else the layout is broken
1336};
1337
1338/**
1339 *  Hides the calendar.  Also removes any "hilite" from the class of any TD
1340 *  element.
1341 */
1342Calendar.prototype.hide = function () {
1343        if (this.isPopup) {
1344                Calendar.removeEvent(document, "keydown", Calendar._keyEvent);
1345                Calendar.removeEvent(document, "keypress", Calendar._keyEvent);
1346                Calendar.removeEvent(document, "mousedown", Calendar._checkCalendar);
1347        }
1348        this.element.style.display = "none";
1349        this.hidden = true;
1350        this.hideShowCovered();
1351};
1352
1353/**
1354 *  Shows the calendar at a given absolute position (beware that, depending on
1355 *  the calendar element style -- position property -- this might be relative
1356 *  to the parent's containing rectangle).
1357 */
1358Calendar.prototype.showAt = function (x, y) {
1359        var s = this.element.style;
1360        s.left = x + "px";
1361        s.top = y + "px";
1362        this.show();
1363};
1364
1365/** Shows the calendar near a given element. */
1366Calendar.prototype.showAtElement = function (el, opts) {
1367        var self = this;
1368        var p = Calendar.getAbsolutePos(el);
1369        if (!opts || typeof opts != "string") {
1370                this.showAt(p.x, p.y + el.offsetHeight);
1371                return true;
1372        }
1373        function fixPosition(box) {
1374                if (box.x < 0)
1375                        box.x = 0;
1376                if (box.y < 0)
1377                        box.y = 0;
1378                if (Calendar.is_ie5_mac) {      // the other approach gives negative values for ie5 mac
1379                        var br = Calendar.getAbsolutePos(el);
1380                        br.x = document.body.offsetWidth;
1381                        br.y = document.body.offsetHeight;
1382                } else {
1383                        var cp = document.createElement("div");
1384                        var s = cp.style;
1385                        s.position = "absolute";
1386                        s.right = s.bottom = s.width = s.height = "0px";
1387                        document.body.appendChild(cp);
1388                        var br = Calendar.getAbsolutePos(cp);
1389                        document.body.removeChild(cp);
1390                }
1391                if (Calendar.is_ie) {
1392                        br.y += document.body.scrollTop;
1393                        br.x += document.body.scrollLeft;
1394                } else {
1395                        br.y += window.scrollY;
1396                        br.x += window.scrollX;
1397                }
1398                var tmp = box.x + box.width - br.x;
1399                if (tmp > 0) box.x -= tmp;
1400                tmp = box.y + box.height - br.y;
1401                if (tmp > 0) box.y -= tmp;
1402        };
1403        this.element.style.display = "block";
1404        Calendar.continuation_for_the_fucking_khtml_browser = function() {
1405                var w = self.element.offsetWidth;
1406                var h = self.element.offsetHeight;
1407                self.element.style.display = "none";
1408                var valign = opts.substr(0, 1);
1409                var halign = "l";
1410                if (opts.length > 1) {
1411                        halign = opts.substr(1, 1);
1412                }
1413                // vertical alignment
1414                switch (valign) {
1415                    case "T": p.y -= h; break;
1416                    case "B": p.y += el.offsetHeight; break;
1417                    case "C": p.y += (el.offsetHeight - h) / 2; break;
1418                    case "t": p.y += el.offsetHeight - h; break;
1419                    case "b": break; // already there
1420                }
1421                // horizontal alignment
1422                switch (halign) {
1423                    case "L": p.x -= w; break;
1424                    case "R": p.x += el.offsetWidth; break;
1425                    case "C": p.x += (el.offsetWidth - w) / 2; break;
1426                    case "r": p.x += el.offsetWidth - w; break;
1427                    case "l": break; // already there
1428                }
1429                p.width = w;
1430                p.height = h + 40;
1431                self.monthsCombo.style.display = "none";
1432                fixPosition(p);
1433                self.showAt(p.x, p.y);
1434        };
1435        if (Calendar.is_khtml)
1436                setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
1437        else
1438                Calendar.continuation_for_the_fucking_khtml_browser();
1439};
1440
1441/** Customizes the date format. */
1442Calendar.prototype.setDateFormat = function (str) {
1443        this.dateFormat = str;
1444};
1445
1446/** Customizes the tooltip date format. */
1447Calendar.prototype.setTtDateFormat = function (str) {
1448        this.ttDateFormat = str;
1449};
1450
1451/**
1452 *  Tries to identify the date represented in a string.  If successful it also
1453 *  calls this.setDate which moves the calendar to the given date.
1454 */
1455Calendar.prototype.parseDate = function (str, fmt) {
1456        var y = 0;
1457        var m = -1;
1458        var d = 0;
1459        var a = str.split(/\W+/);
1460        if (!fmt) {
1461                fmt = this.dateFormat;
1462        }
1463        var b = fmt.match(/%./g);
1464        var i = 0, j = 0;
1465        var hr = 0;
1466        var min = 0;
1467        for (i = 0; i < a.length; ++i) {
1468                if (!a[i])
1469                        continue;
1470                switch (b[i]) {
1471                    case "%d":
1472                    case "%e":
1473                        d = parseInt(a[i], 10);
1474                        break;
1475
1476                    case "%m":
1477                        m = parseInt(a[i], 10) - 1;
1478                        break;
1479
1480                    case "%Y":
1481                    case "%y":
1482                        y = parseInt(a[i], 10);
1483                        (y < 100) && (y += (y > 29) ? 1900 : 2000);
1484                        break;
1485
1486                    case "%b":
1487                        if (Calendar._SMN) {    // if we have short month-names, use them
1488                                for (j = 0; j < 12; ++j) {
1489                                        if (Calendar._SMN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { m = j; break; }
1490                                }
1491                                if (j < 12) break;
1492                        }
1493                    case "%B":
1494                        for (j = 0; j < 12; ++j) {
1495                                if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { m = j; break; }
1496                        }
1497                        break;
1498
1499                    case "%H":
1500                    case "%I":
1501                    case "%k":
1502                    case "%l":
1503                        hr = parseInt(a[i], 10);
1504                        break;
1505
1506                    case "%P":
1507                    case "%p":
1508                        if (/pm/i.test(a[i]) && hr < 12)
1509                                hr += 12;
1510                        break;
1511
1512                    case "%M":
1513                        min = parseInt(a[i], 10);
1514                        break;
1515                }
1516        }
1517        if (y != 0 && m != -1 && d != 0) {
1518                this.setDate(new Date(y, m, d, hr, min, 0));
1519                return;
1520        }
1521        y = 0; m = -1; d = 0;
1522        for (i = 0; i < a.length; ++i) {
1523                if (a[i].search(/[a-zA-Z]+/) != -1) {
1524                        var t = -1;
1525                        for (j = 0; j < 12; ++j) {
1526                                if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { t = j; break; }
1527                        }
1528                        if (t != -1) {
1529                                if (m != -1) {
1530                                        d = m+1;
1531                                }
1532                                m = t;
1533                        }
1534                } else if (parseInt(a[i], 10) <= 12 && m == -1) {
1535                        m = a[i]-1;
1536                } else if (parseInt(a[i], 10) > 31 && y == 0) {
1537                        y = parseInt(a[i], 10);
1538                        (y < 100) && (y += (y > 29) ? 1900 : 2000);
1539                } else if (d == 0) {
1540                        d = a[i];
1541                }
1542        }
1543        if (y == 0) {
1544                var today = new Date();
1545                y = today.getFullYear();
1546        }
1547        if (m != -1 && d != 0) {
1548                this.setDate(new Date(y, m, d, hr, min, 0));
1549        }
1550};
1551
1552Calendar.prototype.hideShowCovered = function () {
1553        var self = this;
1554        Calendar.continuation_for_the_fucking_khtml_browser = function() {
1555                function getVisib(obj){
1556                        var value = obj.style.visibility;
1557                        if (!value) {
1558                                if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // Gecko, W3C
1559                                        if (!Calendar.is_khtml)
1560                                                value = document.defaultView.
1561                                                        getComputedStyle(obj, "").getPropertyValue("visibility");
1562                                        else
1563                                                value = '';
1564                                } else if (obj.currentStyle) { // IE
1565                                        value = obj.currentStyle.visibility;
1566                                } else
1567                                        value = '';
1568                        }
1569                        return value;
1570                };
1571
1572                var tags = new Array("applet", "iframe", "select");
1573                var el = self.element;
1574
1575                var p = Calendar.getAbsolutePos(el);
1576                var EX1 = p.x;
1577                var EX2 = el.offsetWidth + EX1;
1578                var EY1 = p.y;
1579                var EY2 = el.offsetHeight + EY1;
1580
1581                for (var k = tags.length; k > 0; ) {
1582                        var ar = document.getElementsByTagName(tags[--k]);
1583                        var cc = null;
1584
1585                        for (var i = ar.length; i > 0;) {
1586                                cc = ar[--i];
1587
1588                                p = Calendar.getAbsolutePos(cc);
1589                                var CX1 = p.x;
1590                                var CX2 = cc.offsetWidth + CX1;
1591                                var CY1 = p.y;
1592                                var CY2 = cc.offsetHeight + CY1;
1593
1594                                if (self.hidden || (CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {
1595                                        if (!cc.__msh_save_visibility) {
1596                                                cc.__msh_save_visibility = getVisib(cc);
1597                                        }
1598                                        cc.style.visibility = cc.__msh_save_visibility;
1599                                } else {
1600                                        if (!cc.__msh_save_visibility) {
1601                                                cc.__msh_save_visibility = getVisib(cc);
1602                                        }
1603                                        cc.style.visibility = "hidden";
1604                                }
1605                        }
1606                }
1607        };
1608        if (Calendar.is_khtml)
1609                setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
1610        else
1611                Calendar.continuation_for_the_fucking_khtml_browser();
1612};
1613
1614/** Internal function; it displays the bar with the names of the weekday. */
1615Calendar.prototype._displayWeekdays = function () {
1616        var fdow = this.firstDayOfWeek;
1617        var cell = this.firstdayname;
1618        var weekend = Calendar._TT["WEEKEND"];
1619        for (var i = 0; i < 7; ++i) {
1620                cell.className = "day name";
1621                var realday = (i + fdow) % 7;
1622                if (i && !(this.params && this.params.disableFirstDowChange)) {
1623                        cell.ttip = Calendar._TT["DAY_FIRST"].replace("%s", Calendar._DN[realday]);
1624                        cell.navtype = 100;
1625                        cell.calendar = this;
1626                        cell.fdow = realday;
1627                        Calendar._add_evs(cell);
1628                }
1629                if (weekend.indexOf(realday.toString()) != -1) {
1630                        Calendar.addClass(cell, "weekend");
1631                }
1632                Calendar._setCellText(cell,Calendar._SDN[(i + fdow) % 7]);
1633                cell = cell.nextSibling;
1634        }
1635};
1636
1637/** Internal function.  Hides all combo boxes that might be displayed. */
1638Calendar.prototype._hideCombos = function () {
1639        this.monthsCombo.style.display = "none";
1640        this.yearsCombo.style.display = "none";
1641};
1642
1643/** Internal function.  Starts dragging the element. */
1644Calendar.prototype._dragStart = function (ev) {
1645        if (this.dragging) {
1646                return;
1647        }
1648        this.dragging = true;
1649        var posX;
1650        var posY;
1651        if (Calendar.is_ie) {
1652                posY = window.event.clientY + document.body.scrollTop;
1653                posX = window.event.clientX + document.body.scrollLeft;
1654        } else {
1655                posY = ev.clientY + window.scrollY;
1656                posX = ev.clientX + window.scrollX;
1657        }
1658        var st = this.element.style;
1659        this.xOffs = posX - parseInt(st.left);
1660        this.yOffs = posY - parseInt(st.top);
1661        with (Calendar) {
1662                addEvent(document, "mousemove", calDragIt);
1663                addEvent(document, "mouseup", calDragEnd);
1664        }
1665};
1666
1667// BEGIN: DATE OBJECT PATCHES
1668
1669/** Adds the number of days array to the Date object. */
1670Date._MD = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
1671
1672/** Constants used for time computations */
1673Date.SECOND = 1000 /* milliseconds */;
1674Date.MINUTE = 60 * Date.SECOND;
1675Date.HOUR   = 60 * Date.MINUTE;
1676Date.DAY    = 24 * Date.HOUR;
1677Date.WEEK   =  7 * Date.DAY;
1678
1679/** Returns the number of days in the current month */
1680Date.prototype.getMonthDays = function(month) {
1681        var year = this.getFullYear();
1682        if (typeof month == "undefined") {
1683                month = this.getMonth();
1684        }
1685        if (((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) && month == 1) {
1686                return 29;
1687        } else {
1688                return Date._MD[month];
1689        }
1690};
1691
1692/** Returns the number of day in the year. */
1693Date.prototype.getDayOfYear = function() {
1694        var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
1695        var then = new Date(this.getFullYear(), 0, 0, 0, 0, 0);
1696        var time = now - then;
1697        return Math.floor(time / Date.DAY);
1698};
1699
1700/** Returns the number of the week in year, as defined in ISO 8601. */
1701Date.prototype.getWeekNumber = function() {
1702        var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
1703        var DoW = d.getDay();
1704        d.setDate(d.getDate() - (DoW + 6) % 7 + 3); // Nearest Thu
1705        var ms = d.valueOf(); // GMT
1706        d.setMonth(0);
1707        d.setDate(4); // Thu in Week 1
1708        return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1;
1709};
1710
1711/** Checks dates equality (ignores time) */
1712Date.prototype.equalsTo = function(date) {
1713        return ((this.getFullYear() == date.getFullYear()) &&
1714                (this.getMonth() == date.getMonth()) &&
1715                (this.getDate() == date.getDate()) &&
1716                (this.getHours() == date.getHours()) &&
1717                (this.getMinutes() == date.getMinutes()));
1718};
1719
1720/** Prints the date in a string according to the given format. */
1721Date.prototype.print = function (str) {
1722        var m = this.getMonth();
1723        var d = this.getDate();
1724        var y = this.getFullYear();
1725        var wn = this.getWeekNumber();
1726        var w = this.getDay();
1727        var s = {};
1728        var hr = this.getHours();
1729        var pm = (hr >= 12);
1730        var ir = (pm) ? (hr - 12) : hr;
1731        var dy = this.getDayOfYear();
1732        if (ir == 0)
1733                ir = 12;
1734        var min = this.getMinutes();
1735        var sec = this.getSeconds();
1736        s["%a"] = Calendar._SDN[w]; // abbreviated weekday name [FIXME: I18N]
1737        s["%A"] = Calendar._DN[w]; // full weekday name
1738        s["%b"] = Calendar._SMN[m]; // abbreviated month name [FIXME: I18N]
1739        s["%B"] = Calendar._MN[m]; // full month name
1740        // FIXME: %c : preferred date and time representation for the current locale
1741        s["%C"] = 1 + Math.floor(y / 100); // the century number
1742        s["%d"] = (d < 10) ? ("0" + d) : d; // the day of the month (range 01 to 31)
1743        s["%e"] = d; // the day of the month (range 1 to 31)
1744        // FIXME: %D : american date style: %m/%d/%y
1745        // FIXME: %E, %F, %G, %g, %h (man strftime)
1746        s["%H"] = (hr < 10) ? ("0" + hr) : hr; // hour, range 00 to 23 (24h format)
1747        s["%I"] = (ir < 10) ? ("0" + ir) : ir; // hour, range 01 to 12 (12h format)
1748        s["%j"] = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy; // day of the year (range 001 to 366)
1749        s["%k"] = hr;           // hour, range 0 to 23 (24h format)
1750        s["%l"] = ir;           // hour, range 1 to 12 (12h format)
1751        s["%m"] = (m < 9) ? ("0" + (1+m)) : (1+m); // month, range 01 to 12
1752        s["%M"] = (min < 10) ? ("0" + min) : min; // minute, range 00 to 59
1753        s["%n"] = "\n";         // a newline character
1754        s["%p"] = pm ? "PM" : "AM";
1755        s["%P"] = pm ? "pm" : "am";
1756        // FIXME: %r : the time in am/pm notation %I:%M:%S %p
1757        // FIXME: %R : the time in 24-hour notation %H:%M
1758        s["%s"] = Math.floor(this.getTime() / 1000);
1759        s["%S"] = (sec < 10) ? ("0" + sec) : sec; // seconds, range 00 to 59
1760        s["%t"] = "\t";         // a tab character
1761        // FIXME: %T : the time in 24-hour notation (%H:%M:%S)
1762        s["%U"] = s["%W"] = s["%V"] = (wn < 10) ? ("0" + wn) : wn;
1763        s["%u"] = w + 1;        // the day of the week (range 1 to 7, 1 = MON)
1764        s["%w"] = w;            // the day of the week (range 0 to 6, 0 = SUN)
1765        // FIXME: %x : preferred date representation for the current locale without the time
1766        // FIXME: %X : preferred time representation for the current locale without the date
1767        s["%y"] = ('' + y).substr(2, 2); // year without the century (range 00 to 99)
1768        s["%Y"] = y;            // year with the century
1769        s["%%"] = "%";          // a literal '%' character
1770
1771        var re = /%./g;
1772        var a = str.match(re);
1773        for (var i = 0; i < a.length; i++) {
1774                var tmp = s[a[i]];
1775                if (tmp) {
1776                        re = new RegExp(a[i], 'g');
1777                        str = str.replace(re, tmp);
1778                }
1779        }
1780        return str;
1781};
1782
1783Date.prototype.__msh_oldSetFullYear = Date.prototype.setFullYear;
1784Date.prototype.setFullYear = function(y) {
1785        var d = new Date(this);
1786        d.__msh_oldSetFullYear(y);
1787        if (d.getMonth() != this.getMonth())
1788                this.setDate(28);
1789        this.__msh_oldSetFullYear(y);
1790};
1791
1792// END: DATE OBJECT PATCHES
1793
1794// global object that remembers the calendar
1795window.calendar = null;
Note: See TracBrowser for help on using the repository browser.