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

Revision 1622, 49.0 KB checked in by rafaelraymundo, 14 years ago (diff)

Ticket #722 - Implementação da pesquisa avançada no expressoMail.

  • 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                //TODO: tornar zIndex uma das opções de inicialização
752                div.style.zIndex = "100000"; // hardcoded,
753        }
754        div.appendChild(table);
755
756        var thead = Calendar.createElement("thead", table);
757        var cell = null;
758        var row = null;
759
760        var cal = this;
761        var hh = function (text, cs, navtype) {
762                cell = Calendar.createElement("td", row);
763                cell.colSpan = cs;
764                cell.className = "button";
765                if (navtype != 0 && Math.abs(navtype) <= 2)
766                        cell.className += " nav";
767                Calendar._add_evs(cell);
768                cell.calendar = cal;
769                cell.navtype = navtype;
770                if (text.substr(0, 1) != "&") {
771                        cell.appendChild(document.createTextNode(text));
772                }
773                else {
774                        // FIXME: dirty hack for entities
775                        cell.innerHTML = text;
776                }
777                return cell;
778        };
779
780        row = Calendar.createElement("tr", thead);
781        var title_length = 6;
782        (this.isPopup) && --title_length;
783        (this.weekNumbers) && ++title_length;
784
785        hh("?", 1, 400).ttip = Calendar._TT["INFO"];
786        if (this.hasMonthHandler()) {
787                this.title = hh("", title_length, 501);
788                if (this.params.flatMonthTTip) this.title.ttip = this.params.flatMonthTTip;
789        } else {
790                this.title = hh("", title_length, 300);
791        }
792        this.title.className = "title";
793        if (this.isPopup) {
794                this.title.ttip = Calendar._TT["DRAG_TO_MOVE"];
795                this.title.style.cursor = "move";
796                hh("&#x00d7;", 1, 200).ttip = Calendar._TT["CLOSE"];
797        }
798
799        row = Calendar.createElement("tr", thead);
800        row.className = "headrow";
801
802        this._nav_py = hh("&#x00ab;", 1, -2);
803        this._nav_py.ttip = Calendar._TT["PREV_YEAR"];
804
805        this._nav_pm = hh("&#x2039;", 1, -1);
806        this._nav_pm.ttip = Calendar._TT["PREV_MONTH"];
807
808        this._nav_now = hh(Calendar._TT["TODAY"], this.weekNumbers ? 4 : 3, 0);
809        this._nav_now.ttip = Calendar._TT["GO_TODAY"];
810
811        this._nav_nm = hh("&#x203a;", 1, 1);
812        this._nav_nm.ttip = Calendar._TT["NEXT_MONTH"];
813
814        this._nav_ny = hh("&#x00bb;", 1, 2);
815        this._nav_ny.ttip = Calendar._TT["NEXT_YEAR"];
816
817        // day names
818        row = Calendar.createElement("tr", thead);
819        row.className = "daynames";
820        if (this.weekNumbers) {
821                cell = Calendar.createElement("td", row);
822                cell.className = "name wn";
823                cell.appendChild(document.createTextNode(Calendar._TT["WK"]));
824        }
825        for (var i = 7; i > 0; --i) {
826                cell = Calendar.createElement("td", row);
827                cell.appendChild(document.createTextNode(""));
828                if (!i) {
829                        cell.navtype = 100;
830                        cell.calendar = this;
831                        Calendar._add_evs(cell);
832                }
833        }
834        this.firstdayname = (this.weekNumbers) ? row.firstChild.nextSibling : row.firstChild;
835        this._displayWeekdays();
836
837        var tbody = Calendar.createElement("tbody", table);
838        this.tbody = tbody;
839
840        for (i = 6; i > 0; --i) {
841                row = Calendar.createElement("tr", tbody);
842                if (this.weekNumbers) {
843                        if (this.hasWeekHandler()) {
844                                cell = hh("",1,500);
845                                if (this.params.flatWeekTTip) cell.ttip = this.params.flatWeekTTip;
846                        } else {
847                                cell = Calendar.createElement("td", row);
848                                cell.appendChild(document.createTextNode(""));
849                        }
850                }
851                for (var j = 7; j > 0; --j) {
852                        cell = Calendar.createElement("td", row);
853                        cell.appendChild(document.createTextNode(""));
854                        cell.calendar = this;
855                        Calendar._add_evs(cell);
856                }
857        }
858
859        if (this.showsTime) {
860                row = Calendar.createElement("tr", tbody);
861                row.className = "time";
862
863                cell = Calendar.createElement("td", row);
864                cell.className = "time";
865                cell.colSpan = 2;
866                cell.innerHTML = Calendar._TT["TIME"] || "&nbsp;";
867
868                cell = Calendar.createElement("td", row);
869                cell.className = "time";
870                cell.colSpan = this.weekNumbers ? 4 : 3;
871
872                (function(){
873                        function makeTimePart(className, init, range_start, range_end) {
874                                var part = Calendar.createElement("span", cell);
875                                part.className = className;
876                                part.appendChild(document.createTextNode(init));
877                                part.calendar = cal;
878                                part.ttip = Calendar._TT["TIME_PART"];
879                                part.navtype = 50;
880                                part._range = [];
881                                if (typeof range_start != "number")
882                                        part._range = range_start;
883                                else {
884                                        for (var i = range_start; i <= range_end; ++i) {
885                                                var txt;
886                                                if (i < 10 && range_end >= 10) txt = '0' + i;
887                                                else txt = '' + i;
888                                                part._range[part._range.length] = txt;
889                                        }
890                                }
891                                Calendar._add_evs(part);
892                                return part;
893                        };
894                        var hrs = cal.date.getHours();
895                        var mins = cal.date.getMinutes();
896                        var t12 = !cal.time24;
897                        var pm = (hrs > 12);
898                        if (t12 && pm) hrs -= 12;
899                        var H = makeTimePart("hour", hrs, t12 ? 1 : 0, t12 ? 12 : 23);
900                        var span = Calendar.createElement("span", cell);
901                        span.appendChild(document.createTextNode(":"));
902                        span.className = "colon";
903                        var M = makeTimePart("minute", mins, 0, 59);
904                        var AP = null;
905                        cell = Calendar.createElement("td", row);
906                        cell.className = "time";
907                        cell.colSpan = 2;
908                        if (t12)
909                                AP = makeTimePart("ampm", pm ? "pm" : "am", ["am", "pm"]);
910                        else
911                                cell.innerHTML = "&nbsp;";
912
913                        cal.onSetTime = function() {
914                                var hrs = this.date.getHours();
915                                var mins = this.date.getMinutes();
916                                var pm = (hrs > 12);
917                                if (pm && t12) hrs -= 12;
918                                Calendar._setCellText(H,(hrs < 10) ? ("0" + hrs) : hrs);
919                                Calendar._setCellText(M,(mins < 10) ? ("0" + mins) : mins);
920                                if (t12)
921                                        Calendar._setCellText(AP,pm ? "pm" : "am");
922                        };
923
924                        cal.onUpdateTime = function() {
925                                var date = this.date;
926                                var h = parseInt(H.firstChild.data, 10);
927                                if (t12) {
928                                        if (/pm/i.test(AP.firstChild.data) && h < 12)
929                                                h += 12;
930                                        else if (/am/i.test(AP.firstChild.data) && h == 12)
931                                                h = 0;
932                                }
933                                var d = date.getDate();
934                                var m = date.getMonth();
935                                var y = date.getFullYear();
936                                date.setHours(h);
937                                date.setMinutes(parseInt(M.firstChild.data, 10));
938                                date.setFullYear(y);
939                                date.setMonth(m);
940                                date.setDate(d);
941                                this.dateClicked = false;
942                                this.callHandler();
943                        };
944                })();
945        } else {
946                this.onSetTime = this.onUpdateTime = function() {};
947        }
948
949        var tfoot = Calendar.createElement("tfoot", table);
950
951        row = Calendar.createElement("tr", tfoot);
952        row.className = "footrow";
953
954        cell = hh(Calendar._TT["SEL_DATE"], this.weekNumbers ? 8 : 7, 300);
955        cell.className = "ttip";
956        if (this.isPopup) {
957                cell.ttip = Calendar._TT["DRAG_TO_MOVE"];
958                cell.style.cursor = "move";
959        }
960        this.tooltips = cell;
961
962        div = Calendar.createElement("div", this.element);
963        this.monthsCombo = div;
964        div.className = "combo";
965        for (i = 0; i < Calendar._MN.length; ++i) {
966                var mn = Calendar.createElement("div");
967                mn.className = Calendar.is_ie ? "label-IEfix" : "label";
968                mn.month = i;
969                mn.appendChild(document.createTextNode(Calendar._SMN[i]));
970                div.appendChild(mn);
971        }
972
973        div = Calendar.createElement("div", this.element);
974        this.yearsCombo = div;
975        div.className = "combo";
976        for (i = 12; i > 0; --i) {
977                var yr = Calendar.createElement("div");
978                yr.className = Calendar.is_ie ? "label-IEfix" : "label";
979                yr.appendChild(document.createTextNode(""));
980                div.appendChild(yr);
981        }
982
983        this._init(this.firstDayOfWeek, this.date);
984        parent.appendChild(this.element);
985};
986
987/** keyboard navigation, only for popup calendars */
988Calendar._keyEvent = function(ev) {
989        if (!window.calendar) {
990                return false;
991        }
992        (Calendar.is_ie) && (ev = window.event);
993        var cal = window.calendar;
994        var act = (Calendar.is_ie || ev.type == "keypress");
995        if (ev.ctrlKey) {
996                switch (ev.keyCode) {
997                    case 37: // KEY left
998                        act && Calendar.cellClick(cal._nav_pm);
999                        break;
1000                    case 38: // KEY up
1001                        act && Calendar.cellClick(cal._nav_py);
1002                        break;
1003                    case 39: // KEY right
1004                        act && Calendar.cellClick(cal._nav_nm);
1005                        break;
1006                    case 40: // KEY down
1007                        act && Calendar.cellClick(cal._nav_ny);
1008                        break;
1009                    default:
1010                        return false;
1011                }
1012        } else switch (ev.keyCode) {
1013            case 32: // KEY space (now)
1014                Calendar.cellClick(cal._nav_now);
1015                break;
1016            case 27: // KEY esc
1017                act && cal.callCloseHandler();
1018                break;
1019            case 37: // KEY left
1020            case 38: // KEY up
1021            case 39: // KEY right
1022            case 40: // KEY down
1023                if (act) {
1024                        var date = cal.date.getDate() - 1;
1025                        var el = cal.currentDateEl;
1026                        var ne = null;
1027                        var prev = (ev.keyCode == 37) || (ev.keyCode == 38);
1028                        switch (ev.keyCode) {
1029                            case 37: // KEY left
1030                                (--date >= 0) && (ne = cal.ar_days[date]);
1031                                break;
1032                            case 38: // KEY up
1033                                date -= 7;
1034                                (date >= 0) && (ne = cal.ar_days[date]);
1035                                break;
1036                            case 39: // KEY right
1037                                (++date < cal.ar_days.length) && (ne = cal.ar_days[date]);
1038                                break;
1039                            case 40: // KEY down
1040                                date += 7;
1041                                (date < cal.ar_days.length) && (ne = cal.ar_days[date]);
1042                                break;
1043                        }
1044                        if (!ne) {
1045                                if (prev) {
1046                                        Calendar.cellClick(cal._nav_pm);
1047                                } else {
1048                                        Calendar.cellClick(cal._nav_nm);
1049                                }
1050                                date = (prev) ? cal.date.getMonthDays() : 1;
1051                                el = cal.currentDateEl;
1052                                ne = cal.ar_days[date - 1];
1053                        }
1054                        Calendar.removeClass(el, "selected");
1055                        Calendar.addClass(ne, "selected");
1056                        cal.date = new Date(ne.caldate);
1057                        cal.callHandler();
1058                        cal.currentDateEl = ne;
1059                }
1060                break;
1061            case 13: // KEY enter
1062                if (act) {
1063                        cal.callHandler();
1064                        cal.hide();
1065                }
1066                break;
1067            default:
1068                return false;
1069        }
1070        return Calendar.stopEvent(ev);
1071};
1072
1073/**
1074 *  (RE)Initializes the calendar to the given date and firstDayOfWeek
1075 */
1076Calendar.prototype._init = function (firstDayOfWeek, date) {
1077        var today = new Date();
1078        this.table.style.visibility = "hidden";
1079        var year = date.getFullYear();
1080        if (year < this.minYear) {
1081                year = this.minYear;
1082                date.setFullYear(year);
1083        } else if (year > this.maxYear) {
1084                year = this.maxYear;
1085                date.setFullYear(year);
1086        }
1087        this.firstDayOfWeek = firstDayOfWeek;
1088        this.date = new Date(date);
1089        var month = date.getMonth();
1090        var mday = date.getDate();
1091        var no_days = date.getMonthDays();
1092
1093        // calendar voodoo for computing the first day that would actually be
1094        // displayed in the calendar, even if it's from the previous month.
1095        // WARNING: this is magic. ;-)
1096        date.setDate(1);
1097        var day1 = (date.getDay() - this.firstDayOfWeek) % 7;
1098        if (day1 < 0)
1099                day1 += 7;
1100        date.setDate(-day1);
1101        date.setDate(date.getDate() + 1);
1102
1103        var row = this.tbody.firstChild;
1104        var MN = Calendar._SMN[month];
1105        var ar_days = new Array();
1106        var weekend = Calendar._TT["WEEKEND"];
1107        for (var i = 0; i < 6; ++i, row = row.nextSibling) {
1108                var cell = row.firstChild;
1109                if (this.weekNumbers) {
1110                        cell.className = "day wn";
1111                        Calendar._setCellText(cell,date.getWeekNumber());
1112                        if (this.hasWeekHandler) cell.caldate = new Date(date);
1113                        cell = cell.nextSibling;
1114                }
1115                row.className = "daysrow";
1116                var hasdays = false;
1117                for (var j = 0; j < 7; ++j, cell = cell.nextSibling, date.setDate(date.getDate() + 1)) {
1118                        var iday = date.getDate();
1119                        var wday = date.getDay();
1120                        cell.className = "day";
1121                        var current_month = (date.getMonth() == month);
1122                        if (!current_month) {
1123                                if (this.showsOtherMonths) {
1124                                        cell.className += " othermonth";
1125                                        cell.otherMonth = true;
1126                                } else {
1127                                        cell.className = "emptycell";
1128                                        cell.innerHTML = "&nbsp;";
1129                                        cell.disabled = true;
1130                                        continue;
1131                                }
1132                        } else {
1133                                cell.otherMonth = false;
1134                                hasdays = true;
1135                        }
1136                        cell.disabled = false;
1137                        Calendar._setCellText(cell,iday);
1138                        if (typeof this.getDateStatus == "function") {
1139                                var status = this.getDateStatus(date, year, month, iday);
1140                                if (status === true) {
1141                                        cell.className += " disabled";
1142                                        cell.disabled = true;
1143                                } else {
1144                                        if (/disabled/i.test(status))
1145                                                cell.disabled = true;
1146                                        cell.className += " " + status;
1147                                }
1148                        }
1149                        if (!cell.disabled) {
1150                                ar_days[ar_days.length] = cell;
1151                                cell.caldate = new Date(date);
1152                                cell.ttip = "_";
1153                                if (current_month && iday == mday) {
1154                                        cell.className += " selected";
1155                                        this.currentDateEl = cell;
1156                                }
1157                                if (date.getFullYear() == today.getFullYear() &&
1158                                    date.getMonth() == today.getMonth() &&
1159                                    iday == today.getDate()) {
1160                                        cell.className += " today";
1161                                        cell.ttip += Calendar._TT["PART_TODAY"];
1162                                }
1163                                if (weekend.indexOf(wday.toString()) != -1) {
1164                                        cell.className += cell.otherMonth ? " oweekend" : " weekend";
1165                                }
1166                        }
1167                }
1168                if (!(hasdays || this.showsOtherMonths))
1169                        row.className = "emptyrow";
1170        }
1171        this.ar_days = ar_days;
1172        Calendar._setCellText(this.title,this.params ? this.date.print(this.params.titleFormat) : Calendar._MN[month] + ", " + year);
1173        this.onSetTime();
1174        this.table.style.visibility = "visible";
1175        // PROFILE
1176        // Calendar._setCellText(this.tooltips,"Generated in " + ((new Date()) - today) + " ms");
1177};
1178
1179/**
1180 *  Calls _init function above for going to a certain date (but only if the
1181 *  date is different than the currently selected one).
1182 */
1183Calendar.prototype.setDate = function (date) {
1184        if (!date.equalsTo(this.date)) {
1185                this._init(this.firstDayOfWeek, date);
1186        }
1187};
1188
1189/**
1190 *  Refreshes the calendar.  Useful if the "disabledHandler" function is
1191 *  dynamic, meaning that the list of disabled date can change at runtime.
1192 *  Just * call this function if you think that the list of disabled dates
1193 *  should * change.
1194 */
1195Calendar.prototype.refresh = function () {
1196        this._init(this.firstDayOfWeek, this.date);
1197};
1198
1199/** Modifies the "firstDayOfWeek" parameter (pass 0 for Synday, 1 for Monday, etc.). */
1200Calendar.prototype.setFirstDayOfWeek = function (firstDayOfWeek) {
1201        this._init(firstDayOfWeek, this.date);
1202        this._displayWeekdays();
1203};
1204
1205/**
1206 *  Allows customization of what dates are enabled.  The "unaryFunction"
1207 *  parameter must be a function object that receives the date (as a JS Date
1208 *  object) and returns a boolean value.  If the returned value is true then
1209 *  the passed date will be marked as disabled.
1210 */
1211Calendar.prototype.setDateStatusHandler = Calendar.prototype.setDisabledHandler = function (unaryFunction) {
1212        this.getDateStatus = unaryFunction;
1213};
1214
1215/** Customization of allowed year range for the calendar. */
1216Calendar.prototype.setRange = function (a, z) {
1217        this.minYear = a;
1218        this.maxYear = z;
1219};
1220
1221/** Calls the first user handler (selectedHandler). */
1222Calendar.prototype.callHandler = function () {
1223        if (this.onSelected) {
1224                this.onSelected(this, this.date.print(this.dateFormat));
1225        }
1226};
1227
1228/** Calls the week-clicked user handler (selectedHandler). */
1229Calendar.prototype.hasWeekHandler = function () {
1230        return this.params && this.params.flat && this.params.flatWeekCallback;
1231};
1232
1233Calendar.prototype.callWeekHandler = function (weekstart) {
1234        if (this.hasWeekHandler()) {
1235                this.params.flatWeekCallback(this, weekstart);
1236        }
1237};
1238
1239/** Calls the week-clicked user handler (selectedHandler). */
1240Calendar.prototype.hasMonthHandler = function () {
1241        return this.params && this.params.flat && this.params.flatMonthCallback;
1242};
1243
1244Calendar.prototype.callMonthHandler = function () {
1245        if (this.hasMonthHandler()) {
1246                var monthstart = new Date(this.date);
1247                monthstart.setDate(1);
1248                this.params.flatMonthCallback(this, monthstart);
1249        }
1250};
1251
1252/** Calls the second user handler (closeHandler). */
1253Calendar.prototype.callCloseHandler = function () {
1254        if (this.onClose) {
1255                this.onClose(this);
1256        }
1257        this.hideShowCovered();
1258};
1259
1260/** Removes the calendar object from the DOM tree and destroys it. */
1261Calendar.prototype.destroy = function () {
1262        var el = this.element.parentNode;
1263        el.removeChild(this.element);
1264        Calendar._C = null;
1265        window.calendar = null;
1266};
1267
1268/**
1269 *  Moves the calendar element to a different section in the DOM tree (changes
1270 *  its parent).
1271 */
1272Calendar.prototype.reparent = function (new_parent) {
1273        var el = this.element;
1274        el.parentNode.removeChild(el);
1275        new_parent.appendChild(el);
1276};
1277
1278// This gets called when the user presses a mouse button anywhere in the
1279// document, if the calendar is shown.  If the click was outside the open
1280// calendar this function closes it.
1281Calendar._checkCalendar = function(ev) {
1282        if (!window.calendar) {
1283                return false;
1284        }
1285        var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
1286        for (; el != null && el != calendar.element; el = el.parentNode);
1287        if (el == null) {
1288                // calls closeHandler which should hide the calendar.
1289                window.calendar.callCloseHandler();
1290                return Calendar.stopEvent(ev);
1291        }
1292};
1293
1294/** Shows the calendar. */
1295Calendar.prototype.show = function () {
1296        var rows = this.table.getElementsByTagName("tr");
1297        for (var i = rows.length; i > 0;) {
1298                var row = rows[--i];
1299                Calendar.removeClass(row, "rowhilite");
1300                var cells = row.getElementsByTagName("td");
1301                for (var j = cells.length; j > 0;) {
1302                        var cell = cells[--j];
1303                        Calendar.removeClass(cell, "hilite");
1304                        Calendar.removeClass(cell, "active");
1305                }
1306        }
1307        this.element.style.display = "block";
1308        this.hidden = false;
1309        if (this.isPopup) {
1310                window.calendar = this;
1311                Calendar.addEvent(document, "keydown", Calendar._keyEvent);
1312                Calendar.addEvent(document, "keypress", Calendar._keyEvent);
1313                Calendar.addEvent(document, "mousedown", Calendar._checkCalendar);
1314        }
1315        this.hideShowCovered();
1316        var today = new Date();
1317        // See tickets #252 and #284 in the tracking of expressolivre.org.
1318        // The line below was discussed to solve these problems.
1319        //this._init(1,today);
1320        if (Calendar.is_ie5_mac && !this.isPopup)
1321                this.refresh();         // else the layout is broken
1322};
1323
1324/**
1325 *  Hides the calendar.  Also removes any "hilite" from the class of any TD
1326 *  element.
1327 */
1328Calendar.prototype.hide = function () {
1329        if (this.isPopup) {
1330                Calendar.removeEvent(document, "keydown", Calendar._keyEvent);
1331                Calendar.removeEvent(document, "keypress", Calendar._keyEvent);
1332                Calendar.removeEvent(document, "mousedown", Calendar._checkCalendar);
1333        }
1334        this.element.style.display = "none";
1335        this.hidden = true;
1336        this.hideShowCovered();
1337};
1338
1339/**
1340 *  Shows the calendar at a given absolute position (beware that, depending on
1341 *  the calendar element style -- position property -- this might be relative
1342 *  to the parent's containing rectangle).
1343 */
1344Calendar.prototype.showAt = function (x, y) {
1345        var s = this.element.style;
1346        s.left = x + "px";
1347        s.top = y + "px";
1348        this.show();
1349};
1350
1351/** Shows the calendar near a given element. */
1352Calendar.prototype.showAtElement = function (el, opts) {
1353        var self = this;
1354        var p = Calendar.getAbsolutePos(el);
1355        if (!opts || typeof opts != "string") {
1356                this.showAt(p.x, p.y + el.offsetHeight);
1357                return true;
1358        }
1359        function fixPosition(box) {
1360                if (box.x < 0)
1361                        box.x = 0;
1362                if (box.y < 0)
1363                        box.y = 0;
1364                if (Calendar.is_ie5_mac) {      // the other approach gives negative values for ie5 mac
1365                        var br = Calendar.getAbsolutePos(el);
1366                        br.x = document.body.offsetWidth;
1367                        br.y = document.body.offsetHeight;
1368                } else {
1369                        var cp = document.createElement("div");
1370                        var s = cp.style;
1371                        s.position = "absolute";
1372                        s.right = s.bottom = s.width = s.height = "0px";
1373                        document.body.appendChild(cp);
1374                        var br = Calendar.getAbsolutePos(cp);
1375                        document.body.removeChild(cp);
1376                }
1377                if (Calendar.is_ie) {
1378                        br.y += document.body.scrollTop;
1379                        br.x += document.body.scrollLeft;
1380                } else {
1381                        br.y += window.scrollY;
1382                        br.x += window.scrollX;
1383                }
1384                var tmp = box.x + box.width - br.x;
1385                if (tmp > 0) box.x -= tmp;
1386                tmp = box.y + box.height - br.y;
1387                if (tmp > 0) box.y -= tmp;
1388        };
1389        this.element.style.display = "block";
1390        Calendar.continuation_for_the_fucking_khtml_browser = function() {
1391                var w = self.element.offsetWidth;
1392                var h = self.element.offsetHeight;
1393                self.element.style.display = "none";
1394                var valign = opts.substr(0, 1);
1395                var halign = "l";
1396                if (opts.length > 1) {
1397                        halign = opts.substr(1, 1);
1398                }
1399                // vertical alignment
1400                switch (valign) {
1401                    case "T": p.y -= h; break;
1402                    case "B": p.y += el.offsetHeight; break;
1403                    case "C": p.y += (el.offsetHeight - h) / 2; break;
1404                    case "t": p.y += el.offsetHeight - h; break;
1405                    case "b": break; // already there
1406                }
1407                // horizontal alignment
1408                switch (halign) {
1409                    case "L": p.x -= w; break;
1410                    case "R": p.x += el.offsetWidth; break;
1411                    case "C": p.x += (el.offsetWidth - w) / 2; break;
1412                    case "r": p.x += el.offsetWidth - w; break;
1413                    case "l": break; // already there
1414                }
1415                p.width = w;
1416                p.height = h + 40;
1417                self.monthsCombo.style.display = "none";
1418                fixPosition(p);
1419                self.showAt(p.x, p.y);
1420        };
1421        if (Calendar.is_khtml)
1422                setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
1423        else
1424                Calendar.continuation_for_the_fucking_khtml_browser();
1425};
1426
1427/** Customizes the date format. */
1428Calendar.prototype.setDateFormat = function (str) {
1429        this.dateFormat = str;
1430};
1431
1432/** Customizes the tooltip date format. */
1433Calendar.prototype.setTtDateFormat = function (str) {
1434        this.ttDateFormat = str;
1435};
1436
1437/**
1438 *  Tries to identify the date represented in a string.  If successful it also
1439 *  calls this.setDate which moves the calendar to the given date.
1440 */
1441Calendar.prototype.parseDate = function (str, fmt) {
1442        var y = 0;
1443        var m = -1;
1444        var d = 0;
1445        var a = str.split(/\W+/);
1446        if (!fmt) {
1447                fmt = this.dateFormat;
1448        }
1449        var b = fmt.match(/%./g);
1450        var i = 0, j = 0;
1451        var hr = 0;
1452        var min = 0;
1453        for (i = 0; i < a.length; ++i) {
1454                if (!a[i])
1455                        continue;
1456                switch (b[i]) {
1457                    case "%d":
1458                    case "%e":
1459                        d = parseInt(a[i], 10);
1460                        break;
1461
1462                    case "%m":
1463                        m = parseInt(a[i], 10) - 1;
1464                        break;
1465
1466                    case "%Y":
1467                    case "%y":
1468                        y = parseInt(a[i], 10);
1469                        (y < 100) && (y += (y > 29) ? 1900 : 2000);
1470                        break;
1471
1472                    case "%b":
1473                        if (Calendar._SMN) {    // if we have short month-names, use them
1474                                for (j = 0; j < 12; ++j) {
1475                                        if (Calendar._SMN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { m = j; break; }
1476                                }
1477                                if (j < 12) break;
1478                        }
1479                    case "%B":
1480                        for (j = 0; j < 12; ++j) {
1481                                if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { m = j; break; }
1482                        }
1483                        break;
1484
1485                    case "%H":
1486                    case "%I":
1487                    case "%k":
1488                    case "%l":
1489                        hr = parseInt(a[i], 10);
1490                        break;
1491
1492                    case "%P":
1493                    case "%p":
1494                        if (/pm/i.test(a[i]) && hr < 12)
1495                                hr += 12;
1496                        break;
1497
1498                    case "%M":
1499                        min = parseInt(a[i], 10);
1500                        break;
1501                }
1502        }
1503        if (y != 0 && m != -1 && d != 0) {
1504                this.setDate(new Date(y, m, d, hr, min, 0));
1505                return;
1506        }
1507        y = 0; m = -1; d = 0;
1508        for (i = 0; i < a.length; ++i) {
1509                if (a[i].search(/[a-zA-Z]+/) != -1) {
1510                        var t = -1;
1511                        for (j = 0; j < 12; ++j) {
1512                                if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { t = j; break; }
1513                        }
1514                        if (t != -1) {
1515                                if (m != -1) {
1516                                        d = m+1;
1517                                }
1518                                m = t;
1519                        }
1520                } else if (parseInt(a[i], 10) <= 12 && m == -1) {
1521                        m = a[i]-1;
1522                } else if (parseInt(a[i], 10) > 31 && y == 0) {
1523                        y = parseInt(a[i], 10);
1524                        (y < 100) && (y += (y > 29) ? 1900 : 2000);
1525                } else if (d == 0) {
1526                        d = a[i];
1527                }
1528        }
1529        if (y == 0) {
1530                var today = new Date();
1531                y = today.getFullYear();
1532        }
1533        if (m != -1 && d != 0) {
1534                this.setDate(new Date(y, m, d, hr, min, 0));
1535        }
1536};
1537
1538Calendar.prototype.hideShowCovered = function () {
1539        var self = this;
1540        Calendar.continuation_for_the_fucking_khtml_browser = function() {
1541                function getVisib(obj){
1542                        var value = obj.style.visibility;
1543                        if (!value) {
1544                                if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // Gecko, W3C
1545                                        if (!Calendar.is_khtml)
1546                                                value = document.defaultView.
1547                                                        getComputedStyle(obj, "").getPropertyValue("visibility");
1548                                        else
1549                                                value = '';
1550                                } else if (obj.currentStyle) { // IE
1551                                        value = obj.currentStyle.visibility;
1552                                } else
1553                                        value = '';
1554                        }
1555                        return value;
1556                };
1557
1558                var tags = new Array("applet", "iframe", "select");
1559                var el = self.element;
1560
1561                var p = Calendar.getAbsolutePos(el);
1562                var EX1 = p.x;
1563                var EX2 = el.offsetWidth + EX1;
1564                var EY1 = p.y;
1565                var EY2 = el.offsetHeight + EY1;
1566
1567                var calendarDivs = getCalendarDivs();
1568
1569                for (var k = tags.length; k > 0; ) {
1570                    for (var l = 0; l < calendarDivs.length; l++)
1571                    {
1572                        //var ar = document.getElementsByTagName(tags[--k]);
1573                        var ar = calendarDivs[l].getElementsByTagName(tags[--k]);
1574                        var cc = null;
1575
1576                        for (var i = ar.length; i > 0;) {
1577                                cc = ar[--i];
1578
1579                                p = Calendar.getAbsolutePos(cc);
1580                                var CX1 = p.x;
1581                                var CX2 = cc.offsetWidth + CX1;
1582                                var CY1 = p.y;
1583                                var CY2 = cc.offsetHeight + CY1;
1584
1585                                if (self.hidden || (CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {
1586                                        if (!cc.__msh_save_visibility) {
1587                                                cc.__msh_save_visibility = getVisib(cc);
1588                                        }
1589                                        cc.style.visibility = cc.__msh_save_visibility;
1590                                } else {
1591                                        if (!cc.__msh_save_visibility) {
1592                                                cc.__msh_save_visibility = getVisib(cc);
1593                                        }
1594                                        cc.style.visibility = "hidden";
1595                                }
1596                        }
1597                    }
1598                }
1599        };
1600        if (Calendar.is_khtml)
1601                setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
1602        else
1603                Calendar.continuation_for_the_fucking_khtml_browser();
1604};
1605
1606/** Internal function; it displays the bar with the names of the weekday. */
1607Calendar.prototype._displayWeekdays = function () {
1608        var fdow = this.firstDayOfWeek;
1609        var cell = this.firstdayname;
1610        var weekend = Calendar._TT["WEEKEND"];
1611        for (var i = 0; i < 7; ++i) {
1612                cell.className = "day name";
1613                var realday = (i + fdow) % 7;
1614                if (i && !(this.params && this.params.disableFirstDowChange)) {
1615                        cell.ttip = Calendar._TT["DAY_FIRST"].replace("%s", Calendar._DN[realday]);
1616                        cell.navtype = 100;
1617                        cell.calendar = this;
1618                        cell.fdow = realday;
1619                        Calendar._add_evs(cell);
1620                }
1621                if (weekend.indexOf(realday.toString()) != -1) {
1622                        Calendar.addClass(cell, "weekend");
1623                }
1624                Calendar._setCellText(cell,Calendar._SDN[(i + fdow) % 7]);
1625                cell = cell.nextSibling;
1626        }
1627};
1628
1629/** Internal function.  Hides all combo boxes that might be displayed. */
1630Calendar.prototype._hideCombos = function () {
1631        this.monthsCombo.style.display = "none";
1632        this.yearsCombo.style.display = "none";
1633};
1634
1635/** Internal function.  Starts dragging the element. */
1636Calendar.prototype._dragStart = function (ev) {
1637        if (this.dragging) {
1638                return;
1639        }
1640        this.dragging = true;
1641        var posX;
1642        var posY;
1643        if (Calendar.is_ie) {
1644                posY = window.event.clientY + document.body.scrollTop;
1645                posX = window.event.clientX + document.body.scrollLeft;
1646        } else {
1647                posY = ev.clientY + window.scrollY;
1648                posX = ev.clientX + window.scrollX;
1649        }
1650        var st = this.element.style;
1651        this.xOffs = posX - parseInt(st.left);
1652        this.yOffs = posY - parseInt(st.top);
1653        with (Calendar) {
1654                addEvent(document, "mousemove", calDragIt);
1655                addEvent(document, "mouseup", calDragEnd);
1656        }
1657};
1658
1659// BEGIN: DATE OBJECT PATCHES
1660
1661/** Adds the number of days array to the Date object. */
1662Date._MD = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
1663
1664/** Constants used for time computations */
1665Date.SECOND = 1000 /* milliseconds */;
1666Date.MINUTE = 60 * Date.SECOND;
1667Date.HOUR   = 60 * Date.MINUTE;
1668Date.DAY    = 24 * Date.HOUR;
1669Date.WEEK   =  7 * Date.DAY;
1670
1671/** Returns the number of days in the current month */
1672Date.prototype.getMonthDays = function(month) {
1673        var year = this.getFullYear();
1674        if (typeof month == "undefined") {
1675                month = this.getMonth();
1676        }
1677        if (((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) && month == 1) {
1678                return 29;
1679        } else {
1680                return Date._MD[month];
1681        }
1682};
1683
1684/** Returns the number of day in the year. */
1685Date.prototype.getDayOfYear = function() {
1686        var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
1687        var then = new Date(this.getFullYear(), 0, 0, 0, 0, 0);
1688        var time = now - then;
1689        return Math.floor(time / Date.DAY);
1690};
1691
1692/** Returns the number of the week in year, as defined in ISO 8601. */
1693Date.prototype.getWeekNumber = function() {
1694        var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
1695        var DoW = d.getDay();
1696        d.setDate(d.getDate() - (DoW + 6) % 7 + 3); // Nearest Thu
1697        var ms = d.valueOf(); // GMT
1698        d.setMonth(0);
1699        d.setDate(4); // Thu in Week 1
1700        return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1;
1701};
1702
1703/** Checks dates equality (ignores time) */
1704Date.prototype.equalsTo = function(date) {
1705        return ((this.getFullYear() == date.getFullYear()) &&
1706                (this.getMonth() == date.getMonth()) &&
1707                (this.getDate() == date.getDate()) &&
1708                (this.getHours() == date.getHours()) &&
1709                (this.getMinutes() == date.getMinutes()));
1710};
1711
1712/** Prints the date in a string according to the given format. */
1713Date.prototype.print = function (str) {
1714        var m = this.getMonth();
1715        var d = this.getDate();
1716        var y = this.getFullYear();
1717        var wn = this.getWeekNumber();
1718        var w = this.getDay();
1719        var s = {};
1720        var hr = this.getHours();
1721        var pm = (hr >= 12);
1722        var ir = (pm) ? (hr - 12) : hr;
1723        var dy = this.getDayOfYear();
1724        if (ir == 0)
1725                ir = 12;
1726        var min = this.getMinutes();
1727        var sec = this.getSeconds();
1728        s["%a"] = Calendar._SDN[w]; // abbreviated weekday name [FIXME: I18N]
1729        s["%A"] = Calendar._DN[w]; // full weekday name
1730        s["%b"] = Calendar._SMN[m]; // abbreviated month name [FIXME: I18N]
1731        s["%B"] = Calendar._MN[m]; // full month name
1732        // FIXME: %c : preferred date and time representation for the current locale
1733        s["%C"] = 1 + Math.floor(y / 100); // the century number
1734        s["%d"] = (d < 10) ? ("0" + d) : d; // the day of the month (range 01 to 31)
1735        s["%e"] = d; // the day of the month (range 1 to 31)
1736        // FIXME: %D : american date style: %m/%d/%y
1737        // FIXME: %E, %F, %G, %g, %h (man strftime)
1738        s["%H"] = (hr < 10) ? ("0" + hr) : hr; // hour, range 00 to 23 (24h format)
1739        s["%I"] = (ir < 10) ? ("0" + ir) : ir; // hour, range 01 to 12 (12h format)
1740        s["%j"] = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy; // day of the year (range 001 to 366)
1741        s["%k"] = hr;           // hour, range 0 to 23 (24h format)
1742        s["%l"] = ir;           // hour, range 1 to 12 (12h format)
1743        s["%m"] = (m < 9) ? ("0" + (1+m)) : (1+m); // month, range 01 to 12
1744        s["%M"] = (min < 10) ? ("0" + min) : min; // minute, range 00 to 59
1745        s["%n"] = "\n";         // a newline character
1746        s["%p"] = pm ? "PM" : "AM";
1747        s["%P"] = pm ? "pm" : "am";
1748        // FIXME: %r : the time in am/pm notation %I:%M:%S %p
1749        // FIXME: %R : the time in 24-hour notation %H:%M
1750        s["%s"] = Math.floor(this.getTime() / 1000);
1751        s["%S"] = (sec < 10) ? ("0" + sec) : sec; // seconds, range 00 to 59
1752        s["%t"] = "\t";         // a tab character
1753        // FIXME: %T : the time in 24-hour notation (%H:%M:%S)
1754        s["%U"] = s["%W"] = s["%V"] = (wn < 10) ? ("0" + wn) : wn;
1755        s["%u"] = w + 1;        // the day of the week (range 1 to 7, 1 = MON)
1756        s["%w"] = w;            // the day of the week (range 0 to 6, 0 = SUN)
1757        // FIXME: %x : preferred date representation for the current locale without the time
1758        // FIXME: %X : preferred time representation for the current locale without the date
1759        s["%y"] = ('' + y).substr(2, 2); // year without the century (range 00 to 99)
1760        s["%Y"] = y;            // year with the century
1761        s["%%"] = "%";          // a literal '%' character
1762
1763        var re = /%./g;
1764        var a = str.match(re);
1765        for (var i = 0; i < a.length; i++) {
1766                var tmp = s[a[i]];
1767                if (tmp) {
1768                        re = new RegExp(a[i], 'g');
1769                        str = str.replace(re, tmp);
1770                }
1771        }
1772        return str;
1773};
1774
1775Date.prototype.__msh_oldSetFullYear = Date.prototype.setFullYear;
1776Date.prototype.setFullYear = function(y) {
1777        var d = new Date(this);
1778        d.__msh_oldSetFullYear(y);
1779        if (d.getMonth() != this.getMonth())
1780                this.setDate(28);
1781        this.__msh_oldSetFullYear(y);
1782};
1783
1784// END: DATE OBJECT PATCHES
1785
1786function getCalendarDivs(){
1787    var divs = document.getElementsByTagName('div');
1788    var result = new Array();
1789    for (var i = 0; i < divs.length; i++){
1790        if (divs[i].className == 'calendar')
1791        {
1792            result.push(divs[i]);
1793        }
1794    }
1795    return result;
1796}
1797
1798// global object that remembers the calendar
1799window.calendar = null;
Note: See TracBrowser for help on using the repository browser.