1 /* Copyright Mihai Bazon, 2002, 2003 | http://dynarch.com/mishoo/
2 * ------------------------------------------------------------------
4 * The DHTML Calendar, version 0.9.6 "Keep cool but don't freeze"
6 * Details and latest version at:
7 * http://dynarch.com/mishoo/calendar.epl
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
14 /** The Calendar object constructor. */
15 Calendar = function (firstDayOfWeek, dateStr, onSelected, onClose) {
17 this.activeDiv = null;
18 this.currentDateEl = null;
19 this.getDateStatus = null;
21 this.onSelected = onSelected || null;
22 this.onClose = onClose || null;
23 this.dragging = false;
27 this.dateFormat = Calendar._TT["DEF_DATE_FORMAT"];
28 this.ttDateFormat = Calendar._TT["TT_DATE_FORMAT"];
30 this.weekNumbers = true;
31 this.firstDayOfWeek = firstDayOfWeek; // 0 for Sunday, 1 for Monday, etc.
32 this.showsOtherMonths = false;
33 this.dateStr = dateStr;
35 this.showsTime = false;
42 this.firstdayname = null;
44 this.monthsCombo = null;
45 this.yearsCombo = null;
46 this.hilitedMonth = null;
47 this.activeMonth = null;
48 this.hilitedYear = null;
49 this.activeYear = null;
51 this.dateClicked = false;
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;
59 for (var i = 8; i > 0;) {
60 ar[--i] = Calendar._DN[i].substr(0, Calendar._SDN_len);
63 // table of short month names
64 if (typeof Calendar._SMN_len == "undefined")
65 Calendar._SMN_len = 3;
67 for (var i = 12; i > 0;) {
68 ar[--i] = Calendar._MN[i].substr(0, Calendar._SMN_len);
76 /// "static", needed for event handlers.
79 /// detect a special case of "web browser"
80 Calendar.is_ie = ( /msie/i.test(navigator.userAgent) &&
81 !/opera/i.test(navigator.userAgent) );
83 Calendar.is_ie5 = ( Calendar.is_ie && /msie 5\.0/i.test(navigator.userAgent) );
85 /// detect Opera browser
86 Calendar.is_opera = /opera/i.test(navigator.userAgent);
88 /// detect KHTML-based browsers
89 Calendar.is_khtml = /Konqueror|Safari|KHTML/i.test(navigator.userAgent);
91 // BEGIN: UTILITY FUNCTIONS; beware that these might be moved into a separate
92 // library, at some point.
94 Calendar.getAbsolutePos = function(el) {
96 var is_div = /^div$/i.test(el.tagName);
97 if (is_div && el.scrollLeft)
99 if (is_div && el.scrollTop)
101 var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
102 if (el.offsetParent) {
103 var tmp = this.getAbsolutePos(el.offsetParent);
110 Calendar.isRelated = function (el, evt) {
111 var related = evt.relatedTarget;
114 if (type == "mouseover") {
115 related = evt.fromElement;
116 } else if (type == "mouseout") {
117 related = evt.toElement;
124 related = related.parentNode;
129 Calendar.removeClass = function(el, className) {
130 if (!(el && el.className)) {
133 var cls = el.className.split(" ");
134 var ar = new Array();
135 for (var i = cls.length; i > 0;) {
136 if (cls[--i] != className) {
137 ar[ar.length] = cls[i];
140 el.className = ar.join(" ");
143 Calendar.addClass = function(el, className) {
144 Calendar.removeClass(el, className);
145 el.className += " " + className;
148 Calendar.getElement = function(ev) {
149 if (Calendar.is_ie) {
150 return window.event.srcElement;
152 return ev.currentTarget;
156 Calendar.getTargetElement = function(ev) {
157 if (Calendar.is_ie) {
158 return window.event.srcElement;
164 Calendar.stopEvent = function(ev) {
165 ev || (ev = window.event);
166 if (Calendar.is_ie) {
167 ev.cancelBubble = true;
168 ev.returnValue = false;
171 ev.stopPropagation();
176 Calendar.addEvent = function(el, evname, func) {
177 if (el.attachEvent) { // IE
178 el.attachEvent("on" + evname, func);
179 } else if (el.addEventListener) { // Gecko / W3C
180 el.addEventListener(evname, func, true);
182 el["on" + evname] = func;
186 Calendar.removeEvent = function(el, evname, func) {
187 if (el.detachEvent) { // IE
188 el.detachEvent("on" + evname, func);
189 } else if (el.removeEventListener) { // Gecko / W3C
190 el.removeEventListener(evname, func, true);
192 el["on" + evname] = null;
196 Calendar.createElement = function(type, parent) {
198 if (document.createElementNS) {
199 // use the XHTML namespace; IE won't normally get here unless
200 // _they_ "fix" the DOM2 implementation.
201 el = document.createElementNS("http://www.w3.org/1999/xhtml", type);
203 el = document.createElement(type);
205 if (typeof parent != "undefined") {
206 parent.appendChild(el);
211 // END: UTILITY FUNCTIONS
213 // BEGIN: CALENDAR STATIC FUNCTIONS
215 /** Internal -- adds a set of events to make some element behave like a button. */
216 Calendar._add_evs = function(el) {
218 addEvent(el, "mouseover", dayMouseOver);
219 addEvent(el, "mousedown", dayMouseDown);
220 addEvent(el, "mouseout", dayMouseOut);
222 addEvent(el, "dblclick", dayMouseDblClick);
223 el.setAttribute("unselectable", true);
228 Calendar.findMonth = function(el) {
229 if (typeof el.month != "undefined") {
231 } else if (typeof el.parentNode.month != "undefined") {
232 return el.parentNode;
237 Calendar.findYear = function(el) {
238 if (typeof el.year != "undefined") {
240 } else if (typeof el.parentNode.year != "undefined") {
241 return el.parentNode;
246 Calendar.showMonthsCombo = function () {
247 var cal = Calendar._C;
252 var cd = cal.activeDiv;
253 var mc = cal.monthsCombo;
254 if (cal.hilitedMonth) {
255 Calendar.removeClass(cal.hilitedMonth, "hilite");
257 if (cal.activeMonth) {
258 Calendar.removeClass(cal.activeMonth, "active");
260 var mon = cal.monthsCombo.getElementsByTagName("div")[cal.date.getMonth()];
261 Calendar.addClass(mon, "active");
262 cal.activeMonth = mon;
266 s.left = cd.offsetLeft + "px";
268 var mcw = mc.offsetWidth;
269 if (typeof mcw == "undefined")
270 // Konqueror brain-dead techniques
272 s.left = (cd.offsetLeft + cd.offsetWidth - mcw) + "px";
274 s.top = (cd.offsetTop + cd.offsetHeight) + "px";
277 Calendar.showYearsCombo = function (fwd) {
278 var cal = Calendar._C;
283 var cd = cal.activeDiv;
284 var yc = cal.yearsCombo;
285 if (cal.hilitedYear) {
286 Calendar.removeClass(cal.hilitedYear, "hilite");
288 if (cal.activeYear) {
289 Calendar.removeClass(cal.activeYear, "active");
291 cal.activeYear = null;
292 var Y = cal.date.getFullYear() + (fwd ? 1 : -1);
293 var yr = yc.firstChild;
295 for (var i = 12; i > 0; --i) {
296 if (Y >= cal.minYear && Y <= cal.maxYear) {
297 yr.firstChild.data = Y;
299 yr.style.display = "block";
302 yr.style.display = "none";
305 Y += fwd ? cal.yearStep : -cal.yearStep;
311 s.left = cd.offsetLeft + "px";
313 var ycw = yc.offsetWidth;
314 if (typeof ycw == "undefined")
315 // Konqueror brain-dead techniques
317 s.left = (cd.offsetLeft + cd.offsetWidth - ycw) + "px";
319 s.top = (cd.offsetTop + cd.offsetHeight) + "px";
325 Calendar.tableMouseUp = function(ev) {
326 var cal = Calendar._C;
331 clearTimeout(cal.timeout);
333 var el = cal.activeDiv;
337 var target = Calendar.getTargetElement(ev);
338 ev || (ev = window.event);
339 Calendar.removeClass(el, "active");
340 if (target == el || target.parentNode == el) {
341 Calendar.cellClick(el, ev);
343 var mon = Calendar.findMonth(target);
346 date = new Date(cal.date);
347 if (mon.month != date.getMonth()) {
348 date.setMonth(mon.month);
350 cal.dateClicked = false;
354 var year = Calendar.findYear(target);
356 date = new Date(cal.date);
357 if (year.year != date.getFullYear()) {
358 date.setFullYear(year.year);
360 cal.dateClicked = false;
366 removeEvent(document, "mouseup", tableMouseUp);
367 removeEvent(document, "mouseover", tableMouseOver);
368 removeEvent(document, "mousemove", tableMouseOver);
371 return stopEvent(ev);
375 Calendar.tableMouseOver = function (ev) {
376 var cal = Calendar._C;
380 var el = cal.activeDiv;
381 var target = Calendar.getTargetElement(ev);
382 if (target == el || target.parentNode == el) {
383 Calendar.addClass(el, "hilite active");
384 Calendar.addClass(el.parentNode, "rowhilite");
386 if (typeof el.navtype == "undefined" || (el.navtype != 50 && (el.navtype == 0 || Math.abs(el.navtype) > 2)))
387 Calendar.removeClass(el, "active");
388 Calendar.removeClass(el, "hilite");
389 Calendar.removeClass(el.parentNode, "rowhilite");
391 ev || (ev = window.event);
392 if (el.navtype == 50 && target != el) {
393 var pos = Calendar.getAbsolutePos(el);
394 var w = el.offsetWidth;
405 var range = el._range;
406 var current = el._current;
407 var count = Math.floor(dx / 10) % range.length;
408 for (var i = range.length; --i >= 0;)
409 if (range[i] == current)
414 i = range.length - 1;
415 } else if ( ++i >= range.length )
417 var newval = range[i];
418 el.firstChild.data = newval;
422 var mon = Calendar.findMonth(target);
424 if (mon.month != cal.date.getMonth()) {
425 if (cal.hilitedMonth) {
426 Calendar.removeClass(cal.hilitedMonth, "hilite");
428 Calendar.addClass(mon, "hilite");
429 cal.hilitedMonth = mon;
430 } else if (cal.hilitedMonth) {
431 Calendar.removeClass(cal.hilitedMonth, "hilite");
434 if (cal.hilitedMonth) {
435 Calendar.removeClass(cal.hilitedMonth, "hilite");
437 var year = Calendar.findYear(target);
439 if (year.year != cal.date.getFullYear()) {
440 if (cal.hilitedYear) {
441 Calendar.removeClass(cal.hilitedYear, "hilite");
443 Calendar.addClass(year, "hilite");
444 cal.hilitedYear = year;
445 } else if (cal.hilitedYear) {
446 Calendar.removeClass(cal.hilitedYear, "hilite");
448 } else if (cal.hilitedYear) {
449 Calendar.removeClass(cal.hilitedYear, "hilite");
452 return Calendar.stopEvent(ev);
455 Calendar.tableMouseDown = function (ev) {
456 if (Calendar.getTargetElement(ev) == Calendar.getElement(ev)) {
457 return Calendar.stopEvent(ev);
461 Calendar.calDragIt = function (ev) {
462 var cal = Calendar._C;
463 if (!(cal && cal.dragging)) {
468 if (Calendar.is_ie) {
469 posY = window.event.clientY + document.body.scrollTop;
470 posX = window.event.clientX + document.body.scrollLeft;
475 cal.hideShowCovered();
476 var st = cal.element.style;
477 st.left = (posX - cal.xOffs) + "px";
478 st.top = (posY - cal.yOffs) + "px";
479 return Calendar.stopEvent(ev);
482 Calendar.calDragEnd = function (ev) {
483 var cal = Calendar._C;
487 cal.dragging = false;
489 removeEvent(document, "mousemove", calDragIt);
490 removeEvent(document, "mouseup", calDragEnd);
493 cal.hideShowCovered();
496 Calendar.dayMouseDown = function(ev) {
497 var el = Calendar.getElement(ev);
501 var cal = el.calendar;
504 if (el.navtype != 300) with (Calendar) {
505 if (el.navtype == 50) {
506 el._current = el.firstChild.data;
507 addEvent(document, "mousemove", tableMouseOver);
509 addEvent(document, Calendar.is_ie5 ? "mousemove" : "mouseover", tableMouseOver);
510 addClass(el, "hilite active");
511 addEvent(document, "mouseup", tableMouseUp);
512 } else if (cal.isPopup) {
515 if (el.navtype == -1 || el.navtype == 1) {
516 if (cal.timeout) clearTimeout(cal.timeout);
517 cal.timeout = setTimeout("Calendar.showMonthsCombo()", 250);
518 } else if (el.navtype == -2 || el.navtype == 2) {
519 if (cal.timeout) clearTimeout(cal.timeout);
520 cal.timeout = setTimeout((el.navtype > 0) ? "Calendar.showYearsCombo(true)" : "Calendar.showYearsCombo(false)", 250);
524 return Calendar.stopEvent(ev);
527 Calendar.dayMouseDblClick = function(ev) {
528 Calendar.cellClick(Calendar.getElement(ev), ev || window.event);
529 if (Calendar.is_ie) {
530 document.selection.empty();
534 Calendar.dayMouseOver = function(ev) {
535 var el = Calendar.getElement(ev);
536 if (Calendar.isRelated(el, ev) || Calendar._C || el.disabled) {
540 if (el.ttip.substr(0, 1) == "_") {
541 el.ttip = el.caldate.print(el.calendar.ttDateFormat) + el.ttip.substr(1);
543 el.calendar.tooltips.firstChild.data = el.ttip;
545 if (el.navtype != 300) {
546 Calendar.addClass(el, "hilite");
548 Calendar.addClass(el.parentNode, "rowhilite");
551 return Calendar.stopEvent(ev);
554 Calendar.dayMouseOut = function(ev) {
556 var el = getElement(ev);
557 if (isRelated(el, ev) || _C || el.disabled) {
560 removeClass(el, "hilite");
562 removeClass(el.parentNode, "rowhilite");
564 el.calendar.tooltips.firstChild.data = _TT["SEL_DATE"];
565 return stopEvent(ev);
570 * A generic "click" handler :) handles all types of buttons defined in this
573 Calendar.cellClick = function(el, ev) {
574 var cal = el.calendar;
578 if (typeof el.navtype == "undefined") {
579 Calendar.removeClass(cal.currentDateEl, "selected");
580 Calendar.addClass(el, "selected");
581 closing = (cal.currentDateEl == el);
583 cal.currentDateEl = el;
585 cal.date = new Date(el.caldate);
588 // a date was clicked
589 if (!(cal.dateClicked = !el.otherMonth))
590 cal._init(cal.firstDayOfWeek, date);
592 if (el.navtype == 200) {
593 Calendar.removeClass(el, "hilite");
594 cal.callCloseHandler();
597 date = (el.navtype == 0) ? new Date() : new Date(cal.date);
598 // unless "today" was clicked, we assume no date was clicked so
599 // the selected handler will know not to close the calenar when
600 // in single-click mode.
601 // cal.dateClicked = (el.navtype == 0);
602 cal.dateClicked = false;
603 var year = date.getFullYear();
604 var mon = date.getMonth();
605 function setMonth(m) {
606 var day = date.getDate();
607 var max = date.getMonthDays(m);
613 switch (el.navtype) {
615 Calendar.removeClass(el, "hilite");
616 var text = Calendar._TT["ABOUT"];
617 if (typeof text != "undefined") {
618 text += cal.showsTime ? Calendar._TT["ABOUT_TIME"] : "";
620 // FIXME: this should be removed as soon as lang files get updated!
621 text = "Help and about box text is not translated into this language.\n" +
622 "If you know this language and you feel generous please update\n" +
623 "the corresponding file in \"lang\" subdir to match calendar-en.js\n" +
624 "and send it back to <mishoo@infoiasi.ro> to get it into the distribution ;-)\n\n" +
626 "http://dynarch.com/mishoo/calendar.epl\n";
631 if (year > cal.minYear) {
632 date.setFullYear(year - 1);
638 } else if (year-- > cal.minYear) {
639 date.setFullYear(year);
646 } else if (year < cal.maxYear) {
647 date.setFullYear(year + 1);
652 if (year < cal.maxYear) {
653 date.setFullYear(year + 1);
657 cal.setFirstDayOfWeek(el.fdow);
660 var range = el._range;
661 var current = el.firstChild.data;
662 for (var i = range.length; --i >= 0;)
663 if (range[i] == current)
665 if (ev && ev.shiftKey) {
667 i = range.length - 1;
668 } else if ( ++i >= range.length )
670 var newval = range[i];
671 el.firstChild.data = newval;
675 // TODAY will bring us here
676 if ((typeof cal.getDateStatus == "function") && cal.getDateStatus(date, date.getFullYear(), date.getMonth(), date.getDate())) {
677 // remember, "date" was previously set to new
678 // Date() if TODAY was clicked; thus, it
679 // contains today date.
684 if (!date.equalsTo(cal.date)) {
693 Calendar.removeClass(el, "hilite");
694 cal.callCloseHandler();
698 // END: CALENDAR STATIC FUNCTIONS
700 // BEGIN: CALENDAR OBJECT FUNCTIONS
703 * This function creates the calendar inside the given parent. If _par is
704 * null than it creates a popup calendar inside the BODY element. If _par is
705 * an element, be it BODY, then it creates a non-popup calendar (still
706 * hidden). Some properties need to be set before calling this function.
708 Calendar.prototype.create = function (_par) {
711 // default parent is the document body, in which case we create
713 parent = document.getElementsByTagName("body")[0];
717 this.isPopup = false;
719 this.date = this.dateStr ? new Date(this.dateStr) : new Date();
721 var table = Calendar.createElement("table");
723 table.cellSpacing = 0;
724 table.cellPadding = 0;
725 table.calendar = this;
726 Calendar.addEvent(table, "mousedown", Calendar.tableMouseDown);
728 var div = Calendar.createElement("div");
730 div.className = "calendar";
732 div.style.position = "absolute";
733 div.style.display = "none";
735 div.appendChild(table);
737 var thead = Calendar.createElement("thead", table);
742 var hh = function (text, cs, navtype) {
743 cell = Calendar.createElement("td", row);
745 cell.className = "button";
746 if (navtype != 0 && Math.abs(navtype) <= 2)
747 cell.className += " nav";
748 Calendar._add_evs(cell);
750 cell.navtype = navtype;
751 if (text.substr(0, 1) != "&") {
752 cell.appendChild(document.createTextNode(text));
755 // FIXME: dirty hack for entities
756 cell.innerHTML = text;
761 row = Calendar.createElement("tr", thead);
762 var title_length = 6;
763 (this.isPopup) && --title_length;
764 (this.weekNumbers) && ++title_length;
766 hh("?", 1, 400).ttip = Calendar._TT["INFO"];
767 this.title = hh("", title_length, 300);
768 this.title.className = "title";
770 this.title.ttip = Calendar._TT["DRAG_TO_MOVE"];
771 this.title.style.cursor = "move";
772 hh("×", 1, 200).ttip = Calendar._TT["CLOSE"];
775 row = Calendar.createElement("tr", thead);
776 row.className = "headrow";
778 this._nav_py = hh("«", 1, -2);
779 this._nav_py.ttip = Calendar._TT["PREV_YEAR"];
781 this._nav_pm = hh("‹", 1, -1);
782 this._nav_pm.ttip = Calendar._TT["PREV_MONTH"];
784 this._nav_now = hh(Calendar._TT["TODAY"], this.weekNumbers ? 4 : 3, 0);
785 this._nav_now.ttip = Calendar._TT["GO_TODAY"];
787 this._nav_nm = hh("›", 1, 1);
788 this._nav_nm.ttip = Calendar._TT["NEXT_MONTH"];
790 this._nav_ny = hh("»", 1, 2);
791 this._nav_ny.ttip = Calendar._TT["NEXT_YEAR"];
794 row = Calendar.createElement("tr", thead);
795 row.className = "daynames";
796 if (this.weekNumbers) {
797 cell = Calendar.createElement("td", row);
798 cell.className = "name wn";
799 cell.appendChild(document.createTextNode(Calendar._TT["WK"]));
801 for (var i = 7; i > 0; --i) {
802 cell = Calendar.createElement("td", row);
803 cell.appendChild(document.createTextNode(""));
806 cell.calendar = this;
807 Calendar._add_evs(cell);
810 this.firstdayname = (this.weekNumbers) ? row.firstChild.nextSibling : row.firstChild;
811 this._displayWeekdays();
813 var tbody = Calendar.createElement("tbody", table);
816 for (i = 6; i > 0; --i) {
817 row = Calendar.createElement("tr", tbody);
818 if (this.weekNumbers) {
819 cell = Calendar.createElement("td", row);
820 cell.appendChild(document.createTextNode(""));
822 for (var j = 7; j > 0; --j) {
823 cell = Calendar.createElement("td", row);
824 cell.appendChild(document.createTextNode(""));
825 cell.calendar = this;
826 Calendar._add_evs(cell);
830 if (this.showsTime) {
831 row = Calendar.createElement("tr", tbody);
832 row.className = "time";
834 cell = Calendar.createElement("td", row);
835 cell.className = "time";
837 cell.innerHTML = Calendar._TT["TIME"] || " ";
839 cell = Calendar.createElement("td", row);
840 cell.className = "time";
841 cell.colSpan = this.weekNumbers ? 4 : 3;
844 function makeTimePart(className, init, range_start, range_end) {
845 var part = Calendar.createElement("span", cell);
846 part.className = className;
847 part.appendChild(document.createTextNode(init));
849 part.ttip = Calendar._TT["TIME_PART"];
852 if (typeof range_start != "number")
853 part._range = range_start;
855 for (var i = range_start; i <= range_end; ++i) {
857 if (i < 10 && range_end >= 10) txt = '0' + i;
859 part._range[part._range.length] = txt;
862 Calendar._add_evs(part);
865 var hrs = cal.date.getHours();
866 var mins = cal.date.getMinutes();
867 var t12 = !cal.time24;
869 if (t12 && pm) hrs -= 12;
870 var H = makeTimePart("hour", hrs, t12 ? 1 : 0, t12 ? 12 : 23);
871 var span = Calendar.createElement("span", cell);
872 span.appendChild(document.createTextNode(":"));
873 span.className = "colon";
874 var M = makeTimePart("minute", mins, 0, 59);
876 cell = Calendar.createElement("td", row);
877 cell.className = "time";
880 AP = makeTimePart("ampm", pm ? "pm" : "am", ["am", "pm"]);
882 cell.innerHTML = " ";
884 cal.onSetTime = function() {
885 var hrs = this.date.getHours();
886 var mins = this.date.getMinutes();
888 if (pm && t12) hrs -= 12;
889 H.firstChild.data = (hrs < 10) ? ("0" + hrs) : hrs;
890 M.firstChild.data = (mins < 10) ? ("0" + mins) : mins;
892 AP.firstChild.data = pm ? "pm" : "am";
895 cal.onUpdateTime = function() {
896 var date = this.date;
897 var h = parseInt(H.firstChild.data, 10);
899 if (/pm/i.test(AP.firstChild.data) && h < 12)
901 else if (/am/i.test(AP.firstChild.data) && h == 12)
904 var d = date.getDate();
905 var m = date.getMonth();
906 var y = date.getFullYear();
908 date.setMinutes(parseInt(M.firstChild.data, 10));
912 this.dateClicked = false;
917 this.onSetTime = this.onUpdateTime = function() {};
920 var tfoot = Calendar.createElement("tfoot", table);
922 row = Calendar.createElement("tr", tfoot);
923 row.className = "footrow";
925 cell = hh(Calendar._TT["SEL_DATE"], this.weekNumbers ? 8 : 7, 300);
926 cell.className = "ttip";
928 cell.ttip = Calendar._TT["DRAG_TO_MOVE"];
929 cell.style.cursor = "move";
931 this.tooltips = cell;
933 div = Calendar.createElement("div", this.element);
934 this.monthsCombo = div;
935 div.className = "combo";
936 for (i = 0; i < Calendar._MN.length; ++i) {
937 var mn = Calendar.createElement("div");
938 mn.className = Calendar.is_ie ? "label-IEfix" : "label";
940 mn.appendChild(document.createTextNode(Calendar._SMN[i]));
944 div = Calendar.createElement("div", this.element);
945 this.yearsCombo = div;
946 div.className = "combo";
947 for (i = 12; i > 0; --i) {
948 var yr = Calendar.createElement("div");
949 yr.className = Calendar.is_ie ? "label-IEfix" : "label";
950 yr.appendChild(document.createTextNode(""));
954 this._init(this.firstDayOfWeek, this.date);
955 parent.appendChild(this.element);
958 /** keyboard navigation, only for popup calendars */
959 Calendar._keyEvent = function(ev) {
960 if (!window.calendar) {
963 (Calendar.is_ie) && (ev = window.event);
964 var cal = window.calendar;
965 var act = (Calendar.is_ie || ev.type == "keypress");
967 switch (ev.keyCode) {
969 act && Calendar.cellClick(cal._nav_pm);
972 act && Calendar.cellClick(cal._nav_py);
974 case 39: // KEY right
975 act && Calendar.cellClick(cal._nav_nm);
978 act && Calendar.cellClick(cal._nav_ny);
983 } else switch (ev.keyCode) {
984 case 32: // KEY space (now)
985 Calendar.cellClick(cal._nav_now);
988 act && cal.callCloseHandler();
992 case 39: // KEY right
995 var date = cal.date.getDate() - 1;
996 var el = cal.currentDateEl;
998 var prev = (ev.keyCode == 37) || (ev.keyCode == 38);
999 switch (ev.keyCode) {
1000 case 37: // KEY left
1001 (--date >= 0) && (ne = cal.ar_days[date]);
1005 (date >= 0) && (ne = cal.ar_days[date]);
1007 case 39: // KEY right
1008 (++date < cal.ar_days.length) && (ne = cal.ar_days[date]);
1010 case 40: // KEY down
1012 (date < cal.ar_days.length) && (ne = cal.ar_days[date]);
1017 Calendar.cellClick(cal._nav_pm);
1019 Calendar.cellClick(cal._nav_nm);
1021 date = (prev) ? cal.date.getMonthDays() : 1;
1022 el = cal.currentDateEl;
1023 ne = cal.ar_days[date - 1];
1025 Calendar.removeClass(el, "selected");
1026 Calendar.addClass(ne, "selected");
1027 cal.date = new Date(ne.caldate);
1029 cal.currentDateEl = ne;
1032 case 13: // KEY enter
1041 return Calendar.stopEvent(ev);
1045 * (RE)Initializes the calendar to the given date and firstDayOfWeek
1047 Calendar.prototype._init = function (firstDayOfWeek, date) {
1048 var today = new Date();
1049 this.table.style.visibility = "hidden";
1050 var year = date.getFullYear();
1051 if (year < this.minYear) {
1052 year = this.minYear;
1053 date.setFullYear(year);
1054 } else if (year > this.maxYear) {
1055 year = this.maxYear;
1056 date.setFullYear(year);
1058 this.firstDayOfWeek = firstDayOfWeek;
1059 this.date = new Date(date);
1060 var month = date.getMonth();
1061 var mday = date.getDate();
1062 var no_days = date.getMonthDays();
1064 // calendar voodoo for computing the first day that would actually be
1065 // displayed in the calendar, even if it's from the previous month.
1066 // WARNING: this is magic. ;-)
1068 var day1 = (date.getDay() - this.firstDayOfWeek) % 7;
1071 date.setDate(-day1);
1072 date.setDate(date.getDate() + 1);
1074 var row = this.tbody.firstChild;
1075 var MN = Calendar._SMN[month];
1076 var ar_days = new Array();
1077 var weekend = Calendar._TT["WEEKEND"];
1078 for (var i = 0; i < 6; ++i, row = row.nextSibling) {
1079 var cell = row.firstChild;
1080 if (this.weekNumbers) {
1081 cell.className = "day wn";
1082 cell.firstChild.data = date.getWeekNumber();
1083 cell = cell.nextSibling;
1085 row.className = "daysrow";
1086 var hasdays = false;
1087 for (var j = 0; j < 7; ++j, cell = cell.nextSibling, date.setDate(date.getDate() + 1)) {
1088 var iday = date.getDate();
1089 var wday = date.getDay();
1090 cell.className = "day";
1091 var current_month = (date.getMonth() == month);
1092 if (!current_month) {
1093 if (this.showsOtherMonths) {
1094 cell.className += " othermonth";
1095 cell.otherMonth = true;
1097 cell.className = "emptycell";
1098 cell.innerHTML = " ";
1099 cell.disabled = true;
1103 cell.otherMonth = false;
1106 cell.disabled = false;
1107 cell.firstChild.data = iday;
1108 if (typeof this.getDateStatus == "function") {
1109 var status = this.getDateStatus(date, year, month, iday);
1110 if (status === true) {
1111 cell.className += " disabled";
1112 cell.disabled = true;
1114 if (/disabled/i.test(status))
1115 cell.disabled = true;
1116 cell.className += " " + status;
1119 if (!cell.disabled) {
1120 ar_days[ar_days.length] = cell;
1121 cell.caldate = new Date(date);
1123 if (current_month && iday == mday) {
1124 cell.className += " selected";
1125 this.currentDateEl = cell;
1127 if (date.getFullYear() == today.getFullYear() &&
1128 date.getMonth() == today.getMonth() &&
1129 iday == today.getDate()) {
1130 cell.className += " today";
1131 cell.ttip += Calendar._TT["PART_TODAY"];
1133 if (weekend.indexOf(wday.toString()) != -1) {
1134 cell.className += cell.otherMonth ? " oweekend" : " weekend";
1138 if (!(hasdays || this.showsOtherMonths))
1139 row.className = "emptyrow";
1141 this.ar_days = ar_days;
1142 this.title.firstChild.data = Calendar._MN[month] + ", " + year;
1144 this.table.style.visibility = "visible";
1146 // this.tooltips.firstChild.data = "Generated in " + ((new Date()) - today) + " ms";
1150 * Calls _init function above for going to a certain date (but only if the
1151 * date is different than the currently selected one).
1153 Calendar.prototype.setDate = function (date) {
1154 if (!date.equalsTo(this.date)) {
1155 this._init(this.firstDayOfWeek, date);
1160 * Refreshes the calendar. Useful if the "disabledHandler" function is
1161 * dynamic, meaning that the list of disabled date can change at runtime.
1162 * Just * call this function if you think that the list of disabled dates
1165 Calendar.prototype.refresh = function () {
1166 this._init(this.firstDayOfWeek, this.date);
1169 /** Modifies the "firstDayOfWeek" parameter (pass 0 for Synday, 1 for Monday, etc.). */
1170 Calendar.prototype.setFirstDayOfWeek = function (firstDayOfWeek) {
1171 this._init(firstDayOfWeek, this.date);
1172 this._displayWeekdays();
1176 * Allows customization of what dates are enabled. The "unaryFunction"
1177 * parameter must be a function object that receives the date (as a JS Date
1178 * object) and returns a boolean value. If the returned value is true then
1179 * the passed date will be marked as disabled.
1181 Calendar.prototype.setDateStatusHandler = Calendar.prototype.setDisabledHandler = function (unaryFunction) {
1182 this.getDateStatus = unaryFunction;
1185 /** Customization of allowed year range for the calendar. */
1186 Calendar.prototype.setRange = function (a, z) {
1191 /** Calls the first user handler (selectedHandler). */
1192 Calendar.prototype.callHandler = function () {
1193 if (this.onSelected) {
1194 this.onSelected(this, this.date.print(this.dateFormat));
1198 /** Calls the second user handler (closeHandler). */
1199 Calendar.prototype.callCloseHandler = function () {
1203 this.hideShowCovered();
1206 /** Removes the calendar object from the DOM tree and destroys it. */
1207 Calendar.prototype.destroy = function () {
1208 var el = this.element.parentNode;
1209 el.removeChild(this.element);
1211 window.calendar = null;
1215 * Moves the calendar element to a different section in the DOM tree (changes
1218 Calendar.prototype.reparent = function (new_parent) {
1219 var el = this.element;
1220 el.parentNode.removeChild(el);
1221 new_parent.appendChild(el);
1224 // This gets called when the user presses a mouse button anywhere in the
1225 // document, if the calendar is shown. If the click was outside the open
1226 // calendar this function closes it.
1227 Calendar._checkCalendar = function(ev) {
1228 if (!window.calendar) {
1231 var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
1232 for (; el != null && el != calendar.element; el = el.parentNode);
1234 // calls closeHandler which should hide the calendar.
1235 window.calendar.callCloseHandler();
1236 return Calendar.stopEvent(ev);
1240 /** Shows the calendar. */
1241 Calendar.prototype.show = function () {
1242 var rows = this.table.getElementsByTagName("tr");
1243 for (var i = rows.length; i > 0;) {
1244 var row = rows[--i];
1245 Calendar.removeClass(row, "rowhilite");
1246 var cells = row.getElementsByTagName("td");
1247 for (var j = cells.length; j > 0;) {
1248 var cell = cells[--j];
1249 Calendar.removeClass(cell, "hilite");
1250 Calendar.removeClass(cell, "active");
1253 this.element.style.display = "block";
1254 this.hidden = false;
1256 window.calendar = this;
1257 Calendar.addEvent(document, "keydown", Calendar._keyEvent);
1258 Calendar.addEvent(document, "keypress", Calendar._keyEvent);
1259 Calendar.addEvent(document, "mousedown", Calendar._checkCalendar);
1261 this.hideShowCovered();
1265 * Hides the calendar. Also removes any "hilite" from the class of any TD
1268 Calendar.prototype.hide = function () {
1270 Calendar.removeEvent(document, "keydown", Calendar._keyEvent);
1271 Calendar.removeEvent(document, "keypress", Calendar._keyEvent);
1272 Calendar.removeEvent(document, "mousedown", Calendar._checkCalendar);
1274 this.element.style.display = "none";
1276 this.hideShowCovered();
1280 * Shows the calendar at a given absolute position (beware that, depending on
1281 * the calendar element style -- position property -- this might be relative
1282 * to the parent's containing rectangle).
1284 Calendar.prototype.showAt = function (x, y) {
1285 var s = this.element.style;
1291 /** Shows the calendar near a given element. */
1292 Calendar.prototype.showAtElement = function (el, opts) {
1294 var p = Calendar.getAbsolutePos(el);
1295 if (!opts || typeof opts != "string") {
1296 this.showAt(p.x, p.y + el.offsetHeight);
1299 function fixPosition(box) {
1304 var cp = document.createElement("div");
1306 s.position = "absolute";
1307 s.right = s.bottom = s.width = s.height = "0px";
1308 document.body.appendChild(cp);
1309 var br = Calendar.getAbsolutePos(cp);
1310 document.body.removeChild(cp);
1311 if (Calendar.is_ie) {
1312 br.y += document.body.scrollTop;
1313 br.x += document.body.scrollLeft;
1315 br.y += window.scrollY;
1316 br.x += window.scrollX;
1318 var tmp = box.x + box.width - br.x;
1319 if (tmp > 0) box.x -= tmp;
1320 tmp = box.y + box.height - br.y;
1321 if (tmp > 0) box.y -= tmp;
1323 this.element.style.display = "block";
1324 Calendar.continuation_for_the_fucking_khtml_browser = function() {
1325 var w = self.element.offsetWidth;
1326 var h = self.element.offsetHeight;
1327 self.element.style.display = "none";
1328 var valign = opts.substr(0, 1);
1330 if (opts.length > 1) {
1331 halign = opts.substr(1, 1);
1333 // vertical alignment
1335 case "T": p.y -= h; break;
1336 case "B": p.y += el.offsetHeight; break;
1337 case "C": p.y += (el.offsetHeight - h) / 2; break;
1338 case "t": p.y += el.offsetHeight - h; break;
1339 case "b": break; // already there
1341 // horizontal alignment
1343 case "L": p.x -= w; break;
1344 case "R": p.x += el.offsetWidth; break;
1345 case "C": p.x += (el.offsetWidth - w) / 2; break;
1346 case "r": p.x += el.offsetWidth - w; break;
1347 case "l": break; // already there
1351 self.monthsCombo.style.display = "none";
1353 self.showAt(p.x, p.y);
1355 if (Calendar.is_khtml)
1356 setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
1358 Calendar.continuation_for_the_fucking_khtml_browser();
1361 /** Customizes the date format. */
1362 Calendar.prototype.setDateFormat = function (str) {
1363 this.dateFormat = str;
1366 /** Customizes the tooltip date format. */
1367 Calendar.prototype.setTtDateFormat = function (str) {
1368 this.ttDateFormat = str;
1372 * Tries to identify the date represented in a string. If successful it also
1373 * calls this.setDate which moves the calendar to the given date.
1375 Calendar.prototype.parseDate = function (str, fmt) {
1379 var a = str.split(/\W+/);
1381 fmt = this.dateFormat;
1383 var b = fmt.match(/%./g);
1387 for (i = 0; i < a.length; ++i) {
1393 d = parseInt(a[i], 10);
1397 m = parseInt(a[i], 10) - 1;
1402 y = parseInt(a[i], 10);
1403 (y < 100) && (y += (y > 29) ? 1900 : 2000);
1408 for (j = 0; j < 12; ++j) {
1409 if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { m = j; break; }
1417 hr = parseInt(a[i], 10);
1422 if (/pm/i.test(a[i]) && hr < 12)
1427 min = parseInt(a[i], 10);
1431 if (y != 0 && m != -1 && d != 0) {
1432 this.setDate(new Date(y, m, d, hr, min, 0));
1435 y = 0; m = -1; d = 0;
1436 for (i = 0; i < a.length; ++i) {
1437 if (a[i].search(/[a-zA-Z]+/) != -1) {
1439 for (j = 0; j < 12; ++j) {
1440 if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { t = j; break; }
1448 } else if (parseInt(a[i], 10) <= 12 && m == -1) {
1450 } else if (parseInt(a[i], 10) > 31 && y == 0) {
1451 y = parseInt(a[i], 10);
1452 (y < 100) && (y += (y > 29) ? 1900 : 2000);
1453 } else if (d == 0) {
1458 var today = new Date();
1459 y = today.getFullYear();
1461 if (m != -1 && d != 0) {
1462 this.setDate(new Date(y, m, d, hr, min, 0));
1466 Calendar.prototype.hideShowCovered = function () {
1468 Calendar.continuation_for_the_fucking_khtml_browser = function() {
1469 function getVisib(obj){
1470 var value = obj.style.visibility;
1472 if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // Gecko, W3C
1473 if (!Calendar.is_khtml)
1474 value = document.defaultView.
1475 getComputedStyle(obj, "").getPropertyValue("visibility");
1478 } else if (obj.currentStyle) { // IE
1479 value = obj.currentStyle.visibility;
1486 var tags = new Array("applet", "iframe", "select");
1487 var el = self.element;
1489 var p = Calendar.getAbsolutePos(el);
1491 var EX2 = el.offsetWidth + EX1;
1493 var EY2 = el.offsetHeight + EY1;
1495 for (var k = tags.length; k > 0; ) {
1496 var ar = document.getElementsByTagName(tags[--k]);
1499 for (var i = ar.length; i > 0;) {
1502 p = Calendar.getAbsolutePos(cc);
1504 var CX2 = cc.offsetWidth + CX1;
1506 var CY2 = cc.offsetHeight + CY1;
1508 if (self.hidden || (CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {
1509 if (!cc.__msh_save_visibility) {
1510 cc.__msh_save_visibility = getVisib(cc);
1512 cc.style.visibility = cc.__msh_save_visibility;
1514 if (!cc.__msh_save_visibility) {
1515 cc.__msh_save_visibility = getVisib(cc);
1517 cc.style.visibility = "hidden";
1522 if (Calendar.is_khtml)
1523 setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
1525 Calendar.continuation_for_the_fucking_khtml_browser();
1528 /** Internal function; it displays the bar with the names of the weekday. */
1529 Calendar.prototype._displayWeekdays = function () {
1530 var fdow = this.firstDayOfWeek;
1531 var cell = this.firstdayname;
1532 var weekend = Calendar._TT["WEEKEND"];
1533 for (var i = 0; i < 7; ++i) {
1534 cell.className = "day name";
1535 var realday = (i + fdow) % 7;
1537 cell.ttip = Calendar._TT["DAY_FIRST"].replace("%s", Calendar._DN[realday]);
1539 cell.calendar = this;
1540 cell.fdow = realday;
1541 Calendar._add_evs(cell);
1543 if (weekend.indexOf(realday.toString()) != -1) {
1544 Calendar.addClass(cell, "weekend");
1546 cell.firstChild.data = Calendar._SDN[(i + fdow) % 7];
1547 cell = cell.nextSibling;
1551 /** Internal function. Hides all combo boxes that might be displayed. */
1552 Calendar.prototype._hideCombos = function () {
1553 this.monthsCombo.style.display = "none";
1554 this.yearsCombo.style.display = "none";
1557 /** Internal function. Starts dragging the element. */
1558 Calendar.prototype._dragStart = function (ev) {
1559 if (this.dragging) {
1562 this.dragging = true;
1565 if (Calendar.is_ie) {
1566 posY = window.event.clientY + document.body.scrollTop;
1567 posX = window.event.clientX + document.body.scrollLeft;
1569 posY = ev.clientY + window.scrollY;
1570 posX = ev.clientX + window.scrollX;
1572 var st = this.element.style;
1573 this.xOffs = posX - parseInt(st.left);
1574 this.yOffs = posY - parseInt(st.top);
1576 addEvent(document, "mousemove", calDragIt);
1577 addEvent(document, "mouseup", calDragEnd);
1581 // BEGIN: DATE OBJECT PATCHES
1583 /** Adds the number of days array to the Date object. */
1584 Date._MD = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
1586 /** Constants used for time computations */
1587 Date.SECOND = 1000 /* milliseconds */;
1588 Date.MINUTE = 60 * Date.SECOND;
1589 Date.HOUR = 60 * Date.MINUTE;
1590 Date.DAY = 24 * Date.HOUR;
1591 Date.WEEK = 7 * Date.DAY;
1593 /** Returns the number of days in the current month */
1594 Date.prototype.getMonthDays = function(month) {
1595 var year = this.getFullYear();
1596 if (typeof month == "undefined") {
1597 month = this.getMonth();
1599 if (((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) && month == 1) {
1602 return Date._MD[month];
1606 /** Returns the number of day in the year. */
1607 Date.prototype.getDayOfYear = function() {
1608 var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
1609 var then = new Date(this.getFullYear(), 0, 0, 0, 0, 0);
1610 var time = now - then;
1611 return Math.floor(time / Date.DAY);
1614 /** Returns the number of the week in year, as defined in ISO 8601. */
1615 Date.prototype.getWeekNumber = function() {
1616 var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
1617 var DoW = d.getDay();
1618 d.setDate(d.getDate() - (DoW + 6) % 7 + 3); // Nearest Thu
1619 var ms = d.valueOf(); // GMT
1621 d.setDate(4); // Thu in Week 1
1622 return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1;
1625 /** Checks dates equality (ignores time) */
1626 Date.prototype.equalsTo = function(date) {
1627 return ((this.getFullYear() == date.getFullYear()) &&
1628 (this.getMonth() == date.getMonth()) &&
1629 (this.getDate() == date.getDate()) &&
1630 (this.getHours() == date.getHours()) &&
1631 (this.getMinutes() == date.getMinutes()));
1634 /** Prints the date in a string according to the given format. */
1635 Date.prototype.print = function (str) {
1636 var m = this.getMonth();
1637 var d = this.getDate();
1638 var y = this.getFullYear();
1639 var wn = this.getWeekNumber();
1640 var w = this.getDay();
1642 var hr = this.getHours();
1643 var pm = (hr >= 12);
1644 var ir = (pm) ? (hr - 12) : hr;
1645 var dy = this.getDayOfYear();
1648 var min = this.getMinutes();
1649 var sec = this.getSeconds();
1650 s["%a"] = Calendar._SDN[w]; // abbreviated weekday name [FIXME: I18N]
1651 s["%A"] = Calendar._DN[w]; // full weekday name
1652 s["%b"] = Calendar._SMN[m]; // abbreviated month name [FIXME: I18N]
1653 s["%B"] = Calendar._MN[m]; // full month name
1654 // FIXME: %c : preferred date and time representation for the current locale
1655 s["%C"] = 1 + Math.floor(y / 100); // the century number
1656 s["%d"] = (d < 10) ? ("0" + d) : d; // the day of the month (range 01 to 31)
1657 s["%e"] = d; // the day of the month (range 1 to 31)
1658 // FIXME: %D : american date style: %m/%d/%y
1659 // FIXME: %E, %F, %G, %g, %h (man strftime)
1660 s["%H"] = (hr < 10) ? ("0" + hr) : hr; // hour, range 00 to 23 (24h format)
1661 s["%I"] = (ir < 10) ? ("0" + ir) : ir; // hour, range 01 to 12 (12h format)
1662 s["%j"] = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy; // day of the year (range 001 to 366)
1663 s["%k"] = hr; // hour, range 0 to 23 (24h format)
1664 s["%l"] = ir; // hour, range 1 to 12 (12h format)
1665 s["%m"] = (m < 9) ? ("0" + (1+m)) : (1+m); // month, range 01 to 12
1666 s["%M"] = (min < 10) ? ("0" + min) : min; // minute, range 00 to 59
1667 s["%n"] = "\n"; // a newline character
1668 s["%p"] = pm ? "PM" : "AM";
1669 s["%P"] = pm ? "pm" : "am";
1670 // FIXME: %r : the time in am/pm notation %I:%M:%S %p
1671 // FIXME: %R : the time in 24-hour notation %H:%M
1672 s["%s"] = Math.floor(this.getTime() / 1000);
1673 s["%S"] = (sec < 10) ? ("0" + sec) : sec; // seconds, range 00 to 59
1674 s["%t"] = "\t"; // a tab character
1675 // FIXME: %T : the time in 24-hour notation (%H:%M:%S)
1676 s["%U"] = s["%W"] = s["%V"] = (wn < 10) ? ("0" + wn) : wn;
1677 s["%u"] = w + 1; // the day of the week (range 1 to 7, 1 = MON)
1678 s["%w"] = w; // the day of the week (range 0 to 6, 0 = SUN)
1679 // FIXME: %x : preferred date representation for the current locale without the time
1680 // FIXME: %X : preferred time representation for the current locale without the date
1681 s["%y"] = ('' + y).substr(2, 2); // year without the century (range 00 to 99)
1682 s["%Y"] = y; // year with the century
1683 s["%%"] = "%"; // a literal '%' character
1686 if (!Calendar.is_ie5)
1687 return str.replace(re, function (par) { return s[par] || par; });
1689 var a = str.match(re);
1690 for (var i = 0; i < a.length; i++) {
1693 re = new RegExp(a[i], 'g');
1694 str = str.replace(re, tmp);
1701 Date.prototype.__msh_oldSetFullYear = Date.prototype.setFullYear;
1702 Date.prototype.setFullYear = function(y) {
1703 var d = new Date(this);
1704 d.__msh_oldSetFullYear(y);
1705 if (d.getMonth() != this.getMonth())
1707 this.__msh_oldSetFullYear(y);
1710 // END: DATE OBJECT PATCHES
1713 // global object that remembers the calendar
1714 window.calendar = null;