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