tim@14: /* Copyright Mihai Bazon, 2002-2005 | www.bazon.net/mishoo tim@14: * ----------------------------------------------------------- tim@14: * tim@14: * The DHTML Calendar, version 1.0 "It is happening again" tim@14: * tim@14: * Details and latest version at: tim@14: * www.dynarch.com/projects/calendar tim@14: * tim@14: * This script is developed by Dynarch.com. Visit us at www.dynarch.com. tim@14: * tim@14: * This script is distributed under the GNU Lesser General Public License. tim@14: * Read the entire license text here: http://www.gnu.org/licenses/lgpl.html tim@14: */ tim@14: tim@14: // $Id: calendar.js,v 1.1 2007/12/06 13:55:16 drewnak Exp $ tim@14: tim@14: /** The Calendar object constructor. */ tim@14: Calendar = function (firstDayOfWeek, dateStr, onSelected, onClose) { tim@14: // member variables tim@14: this.activeDiv = null; tim@14: this.currentDateEl = null; tim@14: this.getDateStatus = null; tim@14: this.getDateToolTip = null; tim@14: this.getDateText = null; tim@14: this.timeout = null; tim@14: this.onSelected = onSelected || null; tim@14: this.onClose = onClose || null; tim@14: this.dragging = false; tim@14: this.hidden = false; tim@14: this.minYear = 1970; tim@14: this.maxYear = 2050; tim@14: this.dateFormat = Calendar._TT["DEF_DATE_FORMAT"]; tim@14: this.ttDateFormat = Calendar._TT["TT_DATE_FORMAT"]; tim@14: this.isPopup = true; tim@14: this.weekNumbers = true; tim@14: this.firstDayOfWeek = typeof firstDayOfWeek == "number" ? firstDayOfWeek : Calendar._FD; // 0 for Sunday, 1 for Monday, etc. tim@14: this.showsOtherMonths = false; tim@14: this.dateStr = dateStr; tim@14: this.ar_days = null; tim@14: this.showsTime = false; tim@14: this.time24 = true; tim@14: this.yearStep = 2; tim@14: this.hiliteToday = true; tim@14: this.multiple = null; tim@14: // HTML elements tim@14: this.table = null; tim@14: this.element = null; tim@14: this.tbody = null; tim@14: this.firstdayname = null; tim@14: // Combo boxes tim@14: this.monthsCombo = null; tim@14: this.yearsCombo = null; tim@14: this.hilitedMonth = null; tim@14: this.activeMonth = null; tim@14: this.hilitedYear = null; tim@14: this.activeYear = null; tim@14: // Information tim@14: this.dateClicked = false; tim@14: tim@14: // one-time initializations tim@14: if (typeof Calendar._SDN == "undefined") { tim@14: // table of short day names tim@14: if (typeof Calendar._SDN_len == "undefined") tim@14: Calendar._SDN_len = 3; tim@14: var ar = new Array(); tim@14: for (var i = 8; i > 0;) { tim@14: ar[--i] = Calendar._DN[i].substr(0, Calendar._SDN_len); tim@14: } tim@14: Calendar._SDN = ar; tim@14: // table of short month names tim@14: if (typeof Calendar._SMN_len == "undefined") tim@14: Calendar._SMN_len = 3; tim@14: ar = new Array(); tim@14: for (var i = 12; i > 0;) { tim@14: ar[--i] = Calendar._MN[i].substr(0, Calendar._SMN_len); tim@14: } tim@14: Calendar._SMN = ar; tim@14: } tim@14: }; tim@14: tim@14: // ** constants tim@14: tim@14: /// "static", needed for event handlers. tim@14: Calendar._C = null; tim@14: tim@14: /// detect a special case of "web browser" tim@14: Calendar.is_ie = ( /msie/i.test(navigator.userAgent) && tim@14: !/opera/i.test(navigator.userAgent) ); tim@14: tim@14: Calendar.is_ie5 = ( Calendar.is_ie && /msie 5\.0/i.test(navigator.userAgent) ); tim@14: tim@14: /// detect Opera browser tim@14: Calendar.is_opera = /opera/i.test(navigator.userAgent); tim@14: tim@14: /// detect KHTML-based browsers tim@14: Calendar.is_khtml = /Konqueror|Safari|KHTML/i.test(navigator.userAgent); tim@14: tim@14: // BEGIN: UTILITY FUNCTIONS; beware that these might be moved into a separate tim@14: // library, at some point. tim@14: tim@14: Calendar.getAbsolutePos = function(el) { tim@14: var SL = 0, ST = 0; tim@14: var is_div = /^div$/i.test(el.tagName); tim@14: if (is_div && el.scrollLeft) tim@14: SL = el.scrollLeft; tim@14: if (is_div && el.scrollTop) tim@14: ST = el.scrollTop; tim@14: var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST }; tim@14: if (el.offsetParent) { tim@14: var tmp = this.getAbsolutePos(el.offsetParent); tim@14: r.x += tmp.x; tim@14: r.y += tmp.y; tim@14: } tim@14: return r; tim@14: }; tim@14: tim@14: Calendar.isRelated = function (el, evt) { tim@14: var related = evt.relatedTarget; tim@14: if (!related) { tim@14: var type = evt.type; tim@14: if (type == "mouseover") { tim@14: related = evt.fromElement; tim@14: } else if (type == "mouseout") { tim@14: related = evt.toElement; tim@14: } tim@14: } tim@14: while (related) { tim@14: if (related == el) { tim@14: return true; tim@14: } tim@14: related = related.parentNode; tim@14: } tim@14: return false; tim@14: }; tim@14: tim@14: Calendar.removeClass = function(el, className) { tim@14: if (!(el && el.className)) { tim@14: return; tim@14: } tim@14: var cls = el.className.split(" "); tim@14: var ar = new Array(); tim@14: for (var i = cls.length; i > 0;) { tim@14: if (cls[--i] != className) { tim@14: ar[ar.length] = cls[i]; tim@14: } tim@14: } tim@14: el.className = ar.join(" "); tim@14: }; tim@14: tim@14: Calendar.addClass = function(el, className) { tim@14: Calendar.removeClass(el, className); tim@14: el.className += " " + className; tim@14: }; tim@14: tim@14: // FIXME: the following 2 functions totally suck, are useless and should be replaced immediately. tim@14: Calendar.getElement = function(ev) { tim@14: var f = Calendar.is_ie ? window.event.srcElement : ev.currentTarget; tim@14: while (f.nodeType != 1 || /^div$/i.test(f.tagName)) tim@14: f = f.parentNode; tim@14: return f; tim@14: }; tim@14: tim@14: Calendar.getTargetElement = function(ev) { tim@14: var f = Calendar.is_ie ? window.event.srcElement : ev.target; tim@14: while (f.nodeType != 1) tim@14: f = f.parentNode; tim@14: return f; tim@14: }; tim@14: tim@14: Calendar.stopEvent = function(ev) { tim@14: ev || (ev = window.event); tim@14: if (Calendar.is_ie) { tim@14: ev.cancelBubble = true; tim@14: ev.returnValue = false; tim@14: } else { tim@14: ev.preventDefault(); tim@14: ev.stopPropagation(); tim@14: } tim@14: return false; tim@14: }; tim@14: tim@14: Calendar.addEvent = function(el, evname, func) { tim@14: if (el.attachEvent) { // IE tim@14: el.attachEvent("on" + evname, func); tim@14: } else if (el.addEventListener) { // Gecko / W3C tim@14: el.addEventListener(evname, func, true); tim@14: } else { tim@14: el["on" + evname] = func; tim@14: } tim@14: }; tim@14: tim@14: Calendar.removeEvent = function(el, evname, func) { tim@14: if (el.detachEvent) { // IE tim@14: el.detachEvent("on" + evname, func); tim@14: } else if (el.removeEventListener) { // Gecko / W3C tim@14: el.removeEventListener(evname, func, true); tim@14: } else { tim@14: el["on" + evname] = null; tim@14: } tim@14: }; tim@14: tim@14: Calendar.createElement = function(type, parent) { tim@14: var el = null; tim@14: if (document.createElementNS) { tim@14: // use the XHTML namespace; IE won't normally get here unless tim@14: // _they_ "fix" the DOM2 implementation. tim@14: el = document.createElementNS("http://www.w3.org/1999/xhtml", type); tim@14: } else { tim@14: el = document.createElement(type); tim@14: } tim@14: if (typeof parent != "undefined") { tim@14: parent.appendChild(el); tim@14: } tim@14: return el; tim@14: }; tim@14: tim@14: // END: UTILITY FUNCTIONS tim@14: tim@14: // BEGIN: CALENDAR STATIC FUNCTIONS tim@14: tim@14: /** Internal -- adds a set of events to make some element behave like a button. */ tim@14: Calendar._add_evs = function(el) { tim@14: with (Calendar) { tim@14: addEvent(el, "mouseover", dayMouseOver); tim@14: addEvent(el, "mousedown", dayMouseDown); tim@14: addEvent(el, "mouseout", dayMouseOut); tim@14: if (is_ie) { tim@14: addEvent(el, "dblclick", dayMouseDblClick); tim@14: el.setAttribute("unselectable", true); tim@14: } tim@14: } tim@14: }; tim@14: tim@14: Calendar.findMonth = function(el) { tim@14: if (typeof el.month != "undefined") { tim@14: return el; tim@14: } else if (typeof el.parentNode.month != "undefined") { tim@14: return el.parentNode; tim@14: } tim@14: return null; tim@14: }; tim@14: tim@14: Calendar.findYear = function(el) { tim@14: if (typeof el.year != "undefined") { tim@14: return el; tim@14: } else if (typeof el.parentNode.year != "undefined") { tim@14: return el.parentNode; tim@14: } tim@14: return null; tim@14: }; tim@14: tim@14: Calendar.showMonthsCombo = function () { tim@14: var cal = Calendar._C; tim@14: if (!cal) { tim@14: return false; tim@14: } tim@14: var cal = cal; tim@14: var cd = cal.activeDiv; tim@14: var mc = cal.monthsCombo; tim@14: if (cal.hilitedMonth) { tim@14: Calendar.removeClass(cal.hilitedMonth, "hilite"); tim@14: } tim@14: if (cal.activeMonth) { tim@14: Calendar.removeClass(cal.activeMonth, "active"); tim@14: } tim@14: var mon = cal.monthsCombo.getElementsByTagName("div")[cal.date.getMonth()]; tim@14: Calendar.addClass(mon, "active"); tim@14: cal.activeMonth = mon; tim@14: var s = mc.style; tim@14: s.display = "block"; tim@14: if (cd.navtype < 0) tim@14: s.left = cd.offsetLeft + "px"; tim@14: else { tim@14: var mcw = mc.offsetWidth; tim@14: if (typeof mcw == "undefined") tim@14: // Konqueror brain-dead techniques tim@14: mcw = 50; tim@14: s.left = (cd.offsetLeft + cd.offsetWidth - mcw) + "px"; tim@14: } tim@14: s.top = (cd.offsetTop + cd.offsetHeight) + "px"; tim@14: }; tim@14: tim@14: Calendar.showYearsCombo = function (fwd) { tim@14: var cal = Calendar._C; tim@14: if (!cal) { tim@14: return false; tim@14: } tim@14: var cal = cal; tim@14: var cd = cal.activeDiv; tim@14: var yc = cal.yearsCombo; tim@14: if (cal.hilitedYear) { tim@14: Calendar.removeClass(cal.hilitedYear, "hilite"); tim@14: } tim@14: if (cal.activeYear) { tim@14: Calendar.removeClass(cal.activeYear, "active"); tim@14: } tim@14: cal.activeYear = null; tim@14: var Y = cal.date.getFullYear() + (fwd ? 1 : -1); tim@14: var yr = yc.firstChild; tim@14: var show = false; tim@14: for (var i = 12; i > 0; --i) { tim@14: if (Y >= cal.minYear && Y <= cal.maxYear) { tim@14: yr.innerHTML = Y; tim@14: yr.year = Y; tim@14: yr.style.display = "block"; tim@14: show = true; tim@14: } else { tim@14: yr.style.display = "none"; tim@14: } tim@14: yr = yr.nextSibling; tim@14: Y += fwd ? cal.yearStep : -cal.yearStep; tim@14: } tim@14: if (show) { tim@14: var s = yc.style; tim@14: s.display = "block"; tim@14: if (cd.navtype < 0) tim@14: s.left = cd.offsetLeft + "px"; tim@14: else { tim@14: var ycw = yc.offsetWidth; tim@14: if (typeof ycw == "undefined") tim@14: // Konqueror brain-dead techniques tim@14: ycw = 50; tim@14: s.left = (cd.offsetLeft + cd.offsetWidth - ycw) + "px"; tim@14: } tim@14: s.top = (cd.offsetTop + cd.offsetHeight) + "px"; tim@14: } tim@14: }; tim@14: tim@14: // event handlers tim@14: tim@14: Calendar.tableMouseUp = function(ev) { tim@14: var cal = Calendar._C; tim@14: if (!cal) { tim@14: return false; tim@14: } tim@14: if (cal.timeout) { tim@14: clearTimeout(cal.timeout); tim@14: } tim@14: var el = cal.activeDiv; tim@14: if (!el) { tim@14: return false; tim@14: } tim@14: var target = Calendar.getTargetElement(ev); tim@14: ev || (ev = window.event); tim@14: Calendar.removeClass(el, "active"); tim@14: if (target == el || target.parentNode == el) { tim@14: Calendar.cellClick(el, ev); tim@14: } tim@14: var mon = Calendar.findMonth(target); tim@14: var date = null; tim@14: if (mon) { tim@14: date = new Date(cal.date); tim@14: if (mon.month != date.getMonth()) { tim@14: date.setMonth(mon.month); tim@14: cal.setDate(date); tim@14: cal.dateClicked = false; tim@14: cal.callHandler(); tim@14: } tim@14: } else { tim@14: var year = Calendar.findYear(target); tim@14: if (year) { tim@14: date = new Date(cal.date); tim@14: if (year.year != date.getFullYear()) { tim@14: date.setFullYear(year.year); tim@14: cal.setDate(date); tim@14: cal.dateClicked = false; tim@14: cal.callHandler(); tim@14: } tim@14: } tim@14: } tim@14: with (Calendar) { tim@14: removeEvent(document, "mouseup", tableMouseUp); tim@14: removeEvent(document, "mouseover", tableMouseOver); tim@14: removeEvent(document, "mousemove", tableMouseOver); tim@14: cal._hideCombos(); tim@14: _C = null; tim@14: return stopEvent(ev); tim@14: } tim@14: }; tim@14: tim@14: Calendar.tableMouseOver = function (ev) { tim@14: var cal = Calendar._C; tim@14: if (!cal) { tim@14: return; tim@14: } tim@14: var el = cal.activeDiv; tim@14: var target = Calendar.getTargetElement(ev); tim@14: if (target == el || target.parentNode == el) { tim@14: Calendar.addClass(el, "hilite active"); tim@14: Calendar.addClass(el.parentNode, "rowhilite"); tim@14: } else { tim@14: if (typeof el.navtype == "undefined" || (el.navtype != 50 && (el.navtype == 0 || Math.abs(el.navtype) > 2))) tim@14: Calendar.removeClass(el, "active"); tim@14: Calendar.removeClass(el, "hilite"); tim@14: Calendar.removeClass(el.parentNode, "rowhilite"); tim@14: } tim@14: ev || (ev = window.event); tim@14: if (el.navtype == 50 && target != el) { tim@14: var pos = Calendar.getAbsolutePos(el); tim@14: var w = el.offsetWidth; tim@14: var x = ev.clientX; tim@14: var dx; tim@14: var decrease = true; tim@14: if (x > pos.x + w) { tim@14: dx = x - pos.x - w; tim@14: decrease = false; tim@14: } else tim@14: dx = pos.x - x; tim@14: tim@14: if (dx < 0) dx = 0; tim@14: var range = el._range; tim@14: var current = el._current; tim@14: var count = Math.floor(dx / 10) % range.length; tim@14: for (var i = range.length; --i >= 0;) tim@14: if (range[i] == current) tim@14: break; tim@14: while (count-- > 0) tim@14: if (decrease) { tim@14: if (--i < 0) tim@14: i = range.length - 1; tim@14: } else if ( ++i >= range.length ) tim@14: i = 0; tim@14: var newval = range[i]; tim@14: el.innerHTML = newval; tim@14: tim@14: cal.onUpdateTime(); tim@14: } tim@14: var mon = Calendar.findMonth(target); tim@14: if (mon) { tim@14: if (mon.month != cal.date.getMonth()) { tim@14: if (cal.hilitedMonth) { tim@14: Calendar.removeClass(cal.hilitedMonth, "hilite"); tim@14: } tim@14: Calendar.addClass(mon, "hilite"); tim@14: cal.hilitedMonth = mon; tim@14: } else if (cal.hilitedMonth) { tim@14: Calendar.removeClass(cal.hilitedMonth, "hilite"); tim@14: } tim@14: } else { tim@14: if (cal.hilitedMonth) { tim@14: Calendar.removeClass(cal.hilitedMonth, "hilite"); tim@14: } tim@14: var year = Calendar.findYear(target); tim@14: if (year) { tim@14: if (year.year != cal.date.getFullYear()) { tim@14: if (cal.hilitedYear) { tim@14: Calendar.removeClass(cal.hilitedYear, "hilite"); tim@14: } tim@14: Calendar.addClass(year, "hilite"); tim@14: cal.hilitedYear = year; tim@14: } else if (cal.hilitedYear) { tim@14: Calendar.removeClass(cal.hilitedYear, "hilite"); tim@14: } tim@14: } else if (cal.hilitedYear) { tim@14: Calendar.removeClass(cal.hilitedYear, "hilite"); tim@14: } tim@14: } tim@14: return Calendar.stopEvent(ev); tim@14: }; tim@14: tim@14: Calendar.tableMouseDown = function (ev) { tim@14: if (Calendar.getTargetElement(ev) == Calendar.getElement(ev)) { tim@14: return Calendar.stopEvent(ev); tim@14: } tim@14: }; tim@14: tim@14: Calendar.calDragIt = function (ev) { tim@14: var cal = Calendar._C; tim@14: if (!(cal && cal.dragging)) { tim@14: return false; tim@14: } tim@14: var posX; tim@14: var posY; tim@14: if (Calendar.is_ie) { tim@14: posY = window.event.clientY + document.body.scrollTop; tim@14: posX = window.event.clientX + document.body.scrollLeft; tim@14: } else { tim@14: posX = ev.pageX; tim@14: posY = ev.pageY; tim@14: } tim@14: cal.hideShowCovered(); tim@14: var st = cal.element.style; tim@14: st.left = (posX - cal.xOffs) + "px"; tim@14: st.top = (posY - cal.yOffs) + "px"; tim@14: return Calendar.stopEvent(ev); tim@14: }; tim@14: tim@14: Calendar.calDragEnd = function (ev) { tim@14: var cal = Calendar._C; tim@14: if (!cal) { tim@14: return false; tim@14: } tim@14: cal.dragging = false; tim@14: with (Calendar) { tim@14: removeEvent(document, "mousemove", calDragIt); tim@14: removeEvent(document, "mouseup", calDragEnd); tim@14: tableMouseUp(ev); tim@14: } tim@14: cal.hideShowCovered(); tim@14: }; tim@14: tim@14: Calendar.dayMouseDown = function(ev) { tim@14: var el = Calendar.getElement(ev); tim@14: if (el.disabled) { tim@14: return false; tim@14: } tim@14: var cal = el.calendar; tim@14: cal.activeDiv = el; tim@14: Calendar._C = cal; tim@14: if (el.navtype != 300) with (Calendar) { tim@14: if (el.navtype == 50) { tim@14: el._current = el.innerHTML; tim@14: addEvent(document, "mousemove", tableMouseOver); tim@14: } else tim@14: addEvent(document, Calendar.is_ie5 ? "mousemove" : "mouseover", tableMouseOver); tim@14: addClass(el, "hilite active"); tim@14: addEvent(document, "mouseup", tableMouseUp); tim@14: } else if (cal.isPopup) { tim@14: cal._dragStart(ev); tim@14: } tim@14: if (el.navtype == -1 || el.navtype == 1) { tim@14: if (cal.timeout) clearTimeout(cal.timeout); tim@14: cal.timeout = setTimeout("Calendar.showMonthsCombo()", 250); tim@14: } else if (el.navtype == -2 || el.navtype == 2) { tim@14: if (cal.timeout) clearTimeout(cal.timeout); tim@14: cal.timeout = setTimeout((el.navtype > 0) ? "Calendar.showYearsCombo(true)" : "Calendar.showYearsCombo(false)", 250); tim@14: } else { tim@14: cal.timeout = null; tim@14: } tim@14: return Calendar.stopEvent(ev); tim@14: }; tim@14: tim@14: Calendar.dayMouseDblClick = function(ev) { tim@14: Calendar.cellClick(Calendar.getElement(ev), ev || window.event); tim@14: if (Calendar.is_ie) { tim@14: document.selection.empty(); tim@14: } tim@14: }; tim@14: tim@14: Calendar.dayMouseOver = function(ev) { tim@14: var el = Calendar.getElement(ev); tim@14: if (Calendar.isRelated(el, ev) || Calendar._C || el.disabled) { tim@14: return false; tim@14: } tim@14: if (el.ttip) { tim@14: if (el.ttip.substr(0, 1) == "_") { tim@14: el.ttip = el.caldate.print(el.calendar.ttDateFormat) + el.ttip.substr(1); tim@14: } tim@14: el.calendar.tooltips.innerHTML = el.ttip; tim@14: } tim@14: if (el.navtype != 300) { tim@14: Calendar.addClass(el, "hilite"); tim@14: if (el.caldate) { tim@14: Calendar.addClass(el.parentNode, "rowhilite"); tim@14: } tim@14: } tim@14: return Calendar.stopEvent(ev); tim@14: }; tim@14: tim@14: Calendar.dayMouseOut = function(ev) { tim@14: with (Calendar) { tim@14: var el = getElement(ev); tim@14: if (isRelated(el, ev) || _C || el.disabled) tim@14: return false; tim@14: removeClass(el, "hilite"); tim@14: if (el.caldate) tim@14: removeClass(el.parentNode, "rowhilite"); tim@14: if (el.calendar) tim@14: el.calendar.tooltips.innerHTML = _TT["SEL_DATE"]; tim@14: return stopEvent(ev); tim@14: } tim@14: }; tim@14: tim@14: /** tim@14: * A generic "click" handler :) handles all types of buttons defined in this tim@14: * calendar. tim@14: */ tim@14: Calendar.cellClick = function(el, ev) { tim@14: var cal = el.calendar; tim@14: var closing = false; tim@14: var newdate = false; tim@14: var date = null; tim@14: if (typeof el.navtype == "undefined") { tim@14: if (cal.currentDateEl) { tim@14: Calendar.removeClass(cal.currentDateEl, "selected"); tim@14: Calendar.addClass(el, "selected"); tim@14: closing = (cal.currentDateEl == el); tim@14: if (!closing) { tim@14: cal.currentDateEl = el; tim@14: } tim@14: } tim@14: cal.date.setDateOnly(el.caldate); tim@14: date = cal.date; tim@14: var other_month = !(cal.dateClicked = !el.otherMonth); tim@14: if (!other_month && !cal.currentDateEl) tim@14: cal._toggleMultipleDate(new Date(date)); tim@14: else tim@14: newdate = !el.disabled; tim@14: // a date was clicked tim@14: if (other_month) tim@14: cal._init(cal.firstDayOfWeek, date); tim@14: } else { tim@14: if (el.navtype == 200) { tim@14: Calendar.removeClass(el, "hilite"); tim@14: cal.callCloseHandler(); tim@14: return; tim@14: } tim@14: date = new Date(cal.date); tim@14: if (el.navtype == 0) tim@14: date.setDateOnly(new Date()); // TODAY tim@14: // unless "today" was clicked, we assume no date was clicked so tim@14: // the selected handler will know not to close the calenar when tim@14: // in single-click mode. tim@14: // cal.dateClicked = (el.navtype == 0); tim@14: cal.dateClicked = false; tim@14: var year = date.getFullYear(); tim@14: var mon = date.getMonth(); tim@14: function setMonth(m) { tim@14: var day = date.getDate(); tim@14: var max = date.getMonthDays(m); tim@14: if (day > max) { tim@14: date.setDate(max); tim@14: } tim@14: date.setMonth(m); tim@14: }; tim@14: switch (el.navtype) { tim@14: case 400: tim@14: Calendar.removeClass(el, "hilite"); tim@14: var text = Calendar._TT["ABOUT"]; tim@14: if (typeof text != "undefined") { tim@14: text += cal.showsTime ? Calendar._TT["ABOUT_TIME"] : ""; tim@14: } else { tim@14: // FIXME: this should be removed as soon as lang files get updated! tim@14: text = "Help and about box text is not translated into this language.\n" + tim@14: "If you know this language and you feel generous please update\n" + tim@14: "the corresponding file in \"lang\" subdir to match calendar-en.js\n" + tim@14: "and send it back to to get it into the distribution ;-)\n\n" + tim@14: "Thank you!\n" + tim@14: "http://dynarch.com/mishoo/calendar.epl\n"; tim@14: } tim@14: alert(text); tim@14: return; tim@14: case -2: tim@14: if (year > cal.minYear) { tim@14: date.setFullYear(year - 1); tim@14: } tim@14: break; tim@14: case -1: tim@14: if (mon > 0) { tim@14: setMonth(mon - 1); tim@14: } else if (year-- > cal.minYear) { tim@14: date.setFullYear(year); tim@14: setMonth(11); tim@14: } tim@14: break; tim@14: case 1: tim@14: if (mon < 11) { tim@14: setMonth(mon + 1); tim@14: } else if (year < cal.maxYear) { tim@14: date.setFullYear(year + 1); tim@14: setMonth(0); tim@14: } tim@14: break; tim@14: case 2: tim@14: if (year < cal.maxYear) { tim@14: date.setFullYear(year + 1); tim@14: } tim@14: break; tim@14: case 100: tim@14: cal.setFirstDayOfWeek(el.fdow); tim@14: return; tim@14: case 50: tim@14: var range = el._range; tim@14: var current = el.innerHTML; tim@14: for (var i = range.length; --i >= 0;) tim@14: if (range[i] == current) tim@14: break; tim@14: if (ev && ev.shiftKey) { tim@14: if (--i < 0) tim@14: i = range.length - 1; tim@14: } else if ( ++i >= range.length ) tim@14: i = 0; tim@14: var newval = range[i]; tim@14: el.innerHTML = newval; tim@14: cal.onUpdateTime(); tim@14: return; tim@14: case 0: tim@14: // TODAY will bring us here tim@14: if ((typeof cal.getDateStatus == "function") && tim@14: cal.getDateStatus(date, date.getFullYear(), date.getMonth(), date.getDate())) { tim@14: return false; tim@14: } tim@14: break; tim@14: } tim@14: if (!date.equalsTo(cal.date)) { tim@14: cal.setDate(date); tim@14: newdate = true; tim@14: } else if (el.navtype == 0) tim@14: newdate = closing = true; tim@14: } tim@14: if (newdate) { tim@14: ev && cal.callHandler(); tim@14: } tim@14: if (closing) { tim@14: Calendar.removeClass(el, "hilite"); tim@14: ev && cal.callCloseHandler(); tim@14: } tim@14: }; tim@14: tim@14: // END: CALENDAR STATIC FUNCTIONS tim@14: tim@14: // BEGIN: CALENDAR OBJECT FUNCTIONS tim@14: tim@14: /** tim@14: * This function creates the calendar inside the given parent. If _par is tim@14: * null than it creates a popup calendar inside the BODY element. If _par is tim@14: * an element, be it BODY, then it creates a non-popup calendar (still tim@14: * hidden). Some properties need to be set before calling this function. tim@14: */ tim@14: Calendar.prototype.create = function (_par) { tim@14: var parent = null; tim@14: if (! _par) { tim@14: // default parent is the document body, in which case we create tim@14: // a popup calendar. tim@14: parent = document.getElementsByTagName("body")[0]; tim@14: this.isPopup = true; tim@14: } else { tim@14: parent = _par; tim@14: this.isPopup = false; tim@14: } tim@14: this.date = this.dateStr ? new Date(this.dateStr) : new Date(); tim@14: tim@14: var table = Calendar.createElement("table"); tim@14: this.table = table; tim@14: table.cellSpacing = 0; tim@14: table.cellPadding = 0; tim@14: table.calendar = this; tim@14: Calendar.addEvent(table, "mousedown", Calendar.tableMouseDown); tim@14: tim@14: var div = Calendar.createElement("div"); tim@14: this.element = div; tim@14: div.className = "calendar"; tim@14: if (this.isPopup) { tim@14: div.style.position = "absolute"; tim@14: div.style.display = "none"; tim@14: } tim@14: div.appendChild(table); tim@14: tim@14: var thead = Calendar.createElement("thead", table); tim@14: var cell = null; tim@14: var row = null; tim@14: tim@14: var cal = this; tim@14: var hh = function (text, cs, navtype) { tim@14: cell = Calendar.createElement("td", row); tim@14: cell.colSpan = cs; tim@14: cell.className = "button"; tim@14: if (navtype != 0 && Math.abs(navtype) <= 2) tim@14: cell.className += " nav"; tim@14: Calendar._add_evs(cell); tim@14: cell.calendar = cal; tim@14: cell.navtype = navtype; tim@14: cell.innerHTML = "
" + text + "
"; tim@14: return cell; tim@14: }; tim@14: tim@14: row = Calendar.createElement("tr", thead); tim@14: var title_length = 6; tim@14: (this.isPopup) && --title_length; tim@14: (this.weekNumbers) && ++title_length; tim@14: tim@14: hh("?", 1, 400).ttip = Calendar._TT["INFO"]; tim@14: this.title = hh("", title_length, 300); tim@14: this.title.className = "title"; tim@14: if (this.isPopup) { tim@14: this.title.ttip = Calendar._TT["DRAG_TO_MOVE"]; tim@14: this.title.style.cursor = "move"; tim@14: hh("×", 1, 200).ttip = Calendar._TT["CLOSE"]; tim@14: } tim@14: tim@14: row = Calendar.createElement("tr", thead); tim@14: row.className = "headrow"; tim@14: tim@14: this._nav_py = hh("«", 1, -2); tim@14: this._nav_py.ttip = Calendar._TT["PREV_YEAR"]; tim@14: tim@14: this._nav_pm = hh("‹", 1, -1); tim@14: this._nav_pm.ttip = Calendar._TT["PREV_MONTH"]; tim@14: tim@14: this._nav_now = hh(Calendar._TT["TODAY"], this.weekNumbers ? 4 : 3, 0); tim@14: this._nav_now.ttip = Calendar._TT["GO_TODAY"]; tim@14: tim@14: this._nav_nm = hh("›", 1, 1); tim@14: this._nav_nm.ttip = Calendar._TT["NEXT_MONTH"]; tim@14: tim@14: this._nav_ny = hh("»", 1, 2); tim@14: this._nav_ny.ttip = Calendar._TT["NEXT_YEAR"]; tim@14: tim@14: // day names tim@14: row = Calendar.createElement("tr", thead); tim@14: row.className = "daynames"; tim@14: if (this.weekNumbers) { tim@14: cell = Calendar.createElement("td", row); tim@14: cell.className = "name wn"; tim@14: cell.innerHTML = Calendar._TT["WK"]; tim@14: } tim@14: for (var i = 7; i > 0; --i) { tim@14: cell = Calendar.createElement("td", row); tim@14: if (!i) { tim@14: cell.navtype = 100; tim@14: cell.calendar = this; tim@14: Calendar._add_evs(cell); tim@14: } tim@14: } tim@14: this.firstdayname = (this.weekNumbers) ? row.firstChild.nextSibling : row.firstChild; tim@14: this._displayWeekdays(); tim@14: tim@14: var tbody = Calendar.createElement("tbody", table); tim@14: this.tbody = tbody; tim@14: tim@14: for (i = 6; i > 0; --i) { tim@14: row = Calendar.createElement("tr", tbody); tim@14: if (this.weekNumbers) { tim@14: cell = Calendar.createElement("td", row); tim@14: } tim@14: for (var j = 7; j > 0; --j) { tim@14: cell = Calendar.createElement("td", row); tim@14: cell.calendar = this; tim@14: Calendar._add_evs(cell); tim@14: } tim@14: } tim@14: tim@14: if (this.showsTime) { tim@14: row = Calendar.createElement("tr", tbody); tim@14: row.className = "time"; tim@14: tim@14: cell = Calendar.createElement("td", row); tim@14: cell.className = "time"; tim@14: cell.colSpan = 2; tim@14: cell.innerHTML = Calendar._TT["TIME"] || " "; tim@14: tim@14: cell = Calendar.createElement("td", row); tim@14: cell.className = "time"; tim@14: cell.colSpan = this.weekNumbers ? 4 : 3; tim@14: tim@14: (function(){ tim@14: function makeTimePart(className, init, range_start, range_end) { tim@14: var part = Calendar.createElement("span", cell); tim@14: part.className = className; tim@14: part.innerHTML = init; tim@14: part.calendar = cal; tim@14: part.ttip = Calendar._TT["TIME_PART"]; tim@14: part.navtype = 50; tim@14: part._range = []; tim@14: if (typeof range_start != "number") tim@14: part._range = range_start; tim@14: else { tim@14: for (var i = range_start; i <= range_end; ++i) { tim@14: var txt; tim@14: if (i < 10 && range_end >= 10) txt = '0' + i; tim@14: else txt = '' + i; tim@14: part._range[part._range.length] = txt; tim@14: } tim@14: } tim@14: Calendar._add_evs(part); tim@14: return part; tim@14: }; tim@14: var hrs = cal.date.getHours(); tim@14: var mins = cal.date.getMinutes(); tim@14: var t12 = !cal.time24; tim@14: var pm = (hrs > 12); tim@14: if (t12 && pm) hrs -= 12; tim@14: var H = makeTimePart("hour", hrs, t12 ? 1 : 0, t12 ? 12 : 23); tim@14: var span = Calendar.createElement("span", cell); tim@14: span.innerHTML = ":"; tim@14: span.className = "colon"; tim@14: var M = makeTimePart("minute", mins, 0, 59); tim@14: var AP = null; tim@14: cell = Calendar.createElement("td", row); tim@14: cell.className = "time"; tim@14: cell.colSpan = 2; tim@14: if (t12) tim@14: AP = makeTimePart("ampm", pm ? "pm" : "am", ["am", "pm"]); tim@14: else tim@14: cell.innerHTML = " "; tim@14: tim@14: cal.onSetTime = function() { tim@14: var pm, hrs = this.date.getHours(), tim@14: mins = this.date.getMinutes(); tim@14: if (t12) { tim@14: pm = (hrs >= 12); tim@14: if (pm) hrs -= 12; tim@14: if (hrs == 0) hrs = 12; tim@14: AP.innerHTML = pm ? "pm" : "am"; tim@14: } tim@14: H.innerHTML = (hrs < 10) ? ("0" + hrs) : hrs; tim@14: M.innerHTML = (mins < 10) ? ("0" + mins) : mins; tim@14: }; tim@14: tim@14: cal.onUpdateTime = function() { tim@14: var date = this.date; tim@14: var h = parseInt(H.innerHTML, 10); tim@14: if (t12) { tim@14: if (/pm/i.test(AP.innerHTML) && h < 12) tim@14: h += 12; tim@14: else if (/am/i.test(AP.innerHTML) && h == 12) tim@14: h = 0; tim@14: } tim@14: var d = date.getDate(); tim@14: var m = date.getMonth(); tim@14: var y = date.getFullYear(); tim@14: date.setHours(h); tim@14: date.setMinutes(parseInt(M.innerHTML, 10)); tim@14: date.setFullYear(y); tim@14: date.setMonth(m); tim@14: date.setDate(d); tim@14: this.dateClicked = false; tim@14: this.callHandler(); tim@14: }; tim@14: })(); tim@14: } else { tim@14: this.onSetTime = this.onUpdateTime = function() {}; tim@14: } tim@14: tim@14: var tfoot = Calendar.createElement("tfoot", table); tim@14: tim@14: row = Calendar.createElement("tr", tfoot); tim@14: row.className = "footrow"; tim@14: tim@14: cell = hh(Calendar._TT["SEL_DATE"], this.weekNumbers ? 8 : 7, 300); tim@14: cell.className = "ttip"; tim@14: if (this.isPopup) { tim@14: cell.ttip = Calendar._TT["DRAG_TO_MOVE"]; tim@14: cell.style.cursor = "move"; tim@14: } tim@14: this.tooltips = cell; tim@14: tim@14: div = Calendar.createElement("div", this.element); tim@14: this.monthsCombo = div; tim@14: div.className = "combo"; tim@14: for (i = 0; i < Calendar._MN.length; ++i) { tim@14: var mn = Calendar.createElement("div"); tim@14: mn.className = Calendar.is_ie ? "label-IEfix" : "label"; tim@14: mn.month = i; tim@14: mn.innerHTML = Calendar._SMN[i]; tim@14: div.appendChild(mn); tim@14: } tim@14: tim@14: div = Calendar.createElement("div", this.element); tim@14: this.yearsCombo = div; tim@14: div.className = "combo"; tim@14: for (i = 12; i > 0; --i) { tim@14: var yr = Calendar.createElement("div"); tim@14: yr.className = Calendar.is_ie ? "label-IEfix" : "label"; tim@14: div.appendChild(yr); tim@14: } tim@14: tim@14: this._init(this.firstDayOfWeek, this.date); tim@14: parent.appendChild(this.element); tim@14: }; tim@14: tim@14: /** keyboard navigation, only for popup calendars */ tim@14: Calendar._keyEvent = function(ev) { tim@14: var cal = window._dynarch_popupCalendar; tim@14: if (!cal || cal.multiple) tim@14: return false; tim@14: (Calendar.is_ie) && (ev = window.event); tim@14: var act = (Calendar.is_ie || ev.type == "keypress"), tim@14: K = ev.keyCode; tim@14: if (ev.ctrlKey) { tim@14: switch (K) { tim@14: case 37: // KEY left tim@14: act && Calendar.cellClick(cal._nav_pm); tim@14: break; tim@14: case 38: // KEY up tim@14: act && Calendar.cellClick(cal._nav_py); tim@14: break; tim@14: case 39: // KEY right tim@14: act && Calendar.cellClick(cal._nav_nm); tim@14: break; tim@14: case 40: // KEY down tim@14: act && Calendar.cellClick(cal._nav_ny); tim@14: break; tim@14: default: tim@14: return false; tim@14: } tim@14: } else switch (K) { tim@14: case 32: // KEY space (now) tim@14: Calendar.cellClick(cal._nav_now); tim@14: break; tim@14: case 27: // KEY esc tim@14: act && cal.callCloseHandler(); tim@14: break; tim@14: case 37: // KEY left tim@14: case 38: // KEY up tim@14: case 39: // KEY right tim@14: case 40: // KEY down tim@14: if (act) { tim@14: var prev, x, y, ne, el, step; tim@14: prev = K == 37 || K == 38; tim@14: step = (K == 37 || K == 39) ? 1 : 7; tim@14: function setVars() { tim@14: el = cal.currentDateEl; tim@14: var p = el.pos; tim@14: x = p & 15; tim@14: y = p >> 4; tim@14: ne = cal.ar_days[y][x]; tim@14: };setVars(); tim@14: function prevMonth() { tim@14: var date = new Date(cal.date); tim@14: date.setDate(date.getDate() - step); tim@14: cal.setDate(date); tim@14: }; tim@14: function nextMonth() { tim@14: var date = new Date(cal.date); tim@14: date.setDate(date.getDate() + step); tim@14: cal.setDate(date); tim@14: }; tim@14: while (1) { tim@14: switch (K) { tim@14: case 37: // KEY left tim@14: if (--x >= 0) tim@14: ne = cal.ar_days[y][x]; tim@14: else { tim@14: x = 6; tim@14: K = 38; tim@14: continue; tim@14: } tim@14: break; tim@14: case 38: // KEY up tim@14: if (--y >= 0) tim@14: ne = cal.ar_days[y][x]; tim@14: else { tim@14: prevMonth(); tim@14: setVars(); tim@14: } tim@14: break; tim@14: case 39: // KEY right tim@14: if (++x < 7) tim@14: ne = cal.ar_days[y][x]; tim@14: else { tim@14: x = 0; tim@14: K = 40; tim@14: continue; tim@14: } tim@14: break; tim@14: case 40: // KEY down tim@14: if (++y < cal.ar_days.length) tim@14: ne = cal.ar_days[y][x]; tim@14: else { tim@14: nextMonth(); tim@14: setVars(); tim@14: } tim@14: break; tim@14: } tim@14: break; tim@14: } tim@14: if (ne) { tim@14: if (!ne.disabled) tim@14: Calendar.cellClick(ne); tim@14: else if (prev) tim@14: prevMonth(); tim@14: else tim@14: nextMonth(); tim@14: } tim@14: } tim@14: break; tim@14: case 13: // KEY enter tim@14: if (act) tim@14: Calendar.cellClick(cal.currentDateEl, ev); tim@14: break; tim@14: default: tim@14: return false; tim@14: } tim@14: return Calendar.stopEvent(ev); tim@14: }; tim@14: tim@14: /** tim@14: * (RE)Initializes the calendar to the given date and firstDayOfWeek tim@14: */ tim@14: Calendar.prototype._init = function (firstDayOfWeek, date) { tim@14: var today = new Date(), tim@14: TY = today.getFullYear(), tim@14: TM = today.getMonth(), tim@14: TD = today.getDate(); tim@14: this.table.style.visibility = "hidden"; tim@14: var year = date.getFullYear(); tim@14: if (year < this.minYear) { tim@14: year = this.minYear; tim@14: date.setFullYear(year); tim@14: } else if (year > this.maxYear) { tim@14: year = this.maxYear; tim@14: date.setFullYear(year); tim@14: } tim@14: this.firstDayOfWeek = firstDayOfWeek; tim@14: this.date = new Date(date); tim@14: var month = date.getMonth(); tim@14: var mday = date.getDate(); tim@14: var no_days = date.getMonthDays(); tim@14: tim@14: // calendar voodoo for computing the first day that would actually be tim@14: // displayed in the calendar, even if it's from the previous month. tim@14: // WARNING: this is magic. ;-) tim@14: date.setDate(1); tim@14: var day1 = (date.getDay() - this.firstDayOfWeek) % 7; tim@14: if (day1 < 0) tim@14: day1 += 7; tim@14: date.setDate(-day1); tim@14: date.setDate(date.getDate() + 1); tim@14: tim@14: var row = this.tbody.firstChild; tim@14: var MN = Calendar._SMN[month]; tim@14: var ar_days = this.ar_days = new Array(); tim@14: var weekend = Calendar._TT["WEEKEND"]; tim@14: var dates = this.multiple ? (this.datesCells = {}) : null; tim@14: for (var i = 0; i < 6; ++i, row = row.nextSibling) { tim@14: var cell = row.firstChild; tim@14: if (this.weekNumbers) { tim@14: cell.className = "day wn"; tim@14: cell.innerHTML = date.getWeekNumber(); tim@14: cell = cell.nextSibling; tim@14: } tim@14: row.className = "daysrow"; tim@14: var hasdays = false, iday, dpos = ar_days[i] = []; tim@14: for (var j = 0; j < 7; ++j, cell = cell.nextSibling, date.setDate(iday + 1)) { tim@14: iday = date.getDate(); tim@14: var wday = date.getDay(); tim@14: cell.className = "day"; tim@14: cell.pos = i << 4 | j; tim@14: dpos[j] = cell; tim@14: var current_month = (date.getMonth() == month); tim@14: if (!current_month) { tim@14: if (this.showsOtherMonths) { tim@14: cell.className += " othermonth"; tim@14: cell.otherMonth = true; tim@14: } else { tim@14: cell.className = "emptycell"; tim@14: cell.innerHTML = " "; tim@14: cell.disabled = true; tim@14: continue; tim@14: } tim@14: } else { tim@14: cell.otherMonth = false; tim@14: hasdays = true; tim@14: } tim@14: cell.disabled = false; tim@14: cell.innerHTML = this.getDateText ? this.getDateText(date, iday) : iday; tim@14: if (dates) tim@14: dates[date.print("%Y%m%d")] = cell; tim@14: if (this.getDateStatus) { tim@14: var status = this.getDateStatus(date, year, month, iday); tim@14: if (this.getDateToolTip) { tim@14: var toolTip = this.getDateToolTip(date, year, month, iday); tim@14: if (toolTip) tim@14: cell.title = toolTip; tim@14: } tim@14: if (status === true) { tim@14: cell.className += " disabled"; tim@14: cell.disabled = true; tim@14: } else { tim@14: if (/disabled/i.test(status)) tim@14: cell.disabled = true; tim@14: cell.className += " " + status; tim@14: } tim@14: } tim@14: if (!cell.disabled) { tim@14: cell.caldate = new Date(date); tim@14: cell.ttip = "_"; tim@14: if (!this.multiple && current_month tim@14: && iday == mday && this.hiliteToday) { tim@14: cell.className += " selected"; tim@14: this.currentDateEl = cell; tim@14: } tim@14: if (date.getFullYear() == TY && tim@14: date.getMonth() == TM && tim@14: iday == TD) { tim@14: cell.className += " today"; tim@14: cell.ttip += Calendar._TT["PART_TODAY"]; tim@14: } tim@14: if (weekend.indexOf(wday.toString()) != -1) tim@14: cell.className += cell.otherMonth ? " oweekend" : " weekend"; tim@14: } tim@14: } tim@14: if (!(hasdays || this.showsOtherMonths)) tim@14: row.className = "emptyrow"; tim@14: } tim@14: this.title.innerHTML = Calendar._MN[month] + ", " + year; tim@14: this.onSetTime(); tim@14: this.table.style.visibility = "visible"; tim@14: this._initMultipleDates(); tim@14: // PROFILE tim@14: // this.tooltips.innerHTML = "Generated in " + ((new Date()) - today) + " ms"; tim@14: }; tim@14: tim@14: Calendar.prototype._initMultipleDates = function() { tim@14: if (this.multiple) { tim@14: for (var i in this.multiple) { tim@14: var cell = this.datesCells[i]; tim@14: var d = this.multiple[i]; tim@14: if (!d) tim@14: continue; tim@14: if (cell) tim@14: cell.className += " selected"; tim@14: } tim@14: } tim@14: }; tim@14: tim@14: Calendar.prototype._toggleMultipleDate = function(date) { tim@14: if (this.multiple) { tim@14: var ds = date.print("%Y%m%d"); tim@14: var cell = this.datesCells[ds]; tim@14: if (cell) { tim@14: var d = this.multiple[ds]; tim@14: if (!d) { tim@14: Calendar.addClass(cell, "selected"); tim@14: this.multiple[ds] = date; tim@14: } else { tim@14: Calendar.removeClass(cell, "selected"); tim@14: delete this.multiple[ds]; tim@14: } tim@14: } tim@14: } tim@14: }; tim@14: tim@14: Calendar.prototype.setDateToolTipHandler = function (unaryFunction) { tim@14: this.getDateToolTip = unaryFunction; tim@14: }; tim@14: tim@14: /** tim@14: * Calls _init function above for going to a certain date (but only if the tim@14: * date is different than the currently selected one). tim@14: */ tim@14: Calendar.prototype.setDate = function (date) { tim@14: if (!date.equalsTo(this.date)) { tim@14: this._init(this.firstDayOfWeek, date); tim@14: } tim@14: }; tim@14: tim@14: /** tim@14: * Refreshes the calendar. Useful if the "disabledHandler" function is tim@14: * dynamic, meaning that the list of disabled date can change at runtime. tim@14: * Just * call this function if you think that the list of disabled dates tim@14: * should * change. tim@14: */ tim@14: Calendar.prototype.refresh = function () { tim@14: this._init(this.firstDayOfWeek, this.date); tim@14: }; tim@14: tim@14: /** Modifies the "firstDayOfWeek" parameter (pass 0 for Synday, 1 for Monday, etc.). */ tim@14: Calendar.prototype.setFirstDayOfWeek = function (firstDayOfWeek) { tim@14: this._init(firstDayOfWeek, this.date); tim@14: this._displayWeekdays(); tim@14: }; tim@14: tim@14: /** tim@14: * Allows customization of what dates are enabled. The "unaryFunction" tim@14: * parameter must be a function object that receives the date (as a JS Date tim@14: * object) and returns a boolean value. If the returned value is true then tim@14: * the passed date will be marked as disabled. tim@14: */ tim@14: Calendar.prototype.setDateStatusHandler = Calendar.prototype.setDisabledHandler = function (unaryFunction) { tim@14: this.getDateStatus = unaryFunction; tim@14: }; tim@14: tim@14: /** Customization of allowed year range for the calendar. */ tim@14: Calendar.prototype.setRange = function (a, z) { tim@14: this.minYear = a; tim@14: this.maxYear = z; tim@14: }; tim@14: tim@14: /** Calls the first user handler (selectedHandler). */ tim@14: Calendar.prototype.callHandler = function () { tim@14: if (this.onSelected) { tim@14: this.onSelected(this, this.date.print(this.dateFormat)); tim@14: } tim@14: }; tim@14: tim@14: /** Calls the second user handler (closeHandler). */ tim@14: Calendar.prototype.callCloseHandler = function () { tim@14: if (this.onClose) { tim@14: this.onClose(this); tim@14: } tim@14: this.hideShowCovered(); tim@14: }; tim@14: tim@14: /** Removes the calendar object from the DOM tree and destroys it. */ tim@14: Calendar.prototype.destroy = function () { tim@14: var el = this.element.parentNode; tim@14: el.removeChild(this.element); tim@14: Calendar._C = null; tim@14: window._dynarch_popupCalendar = null; tim@14: }; tim@14: tim@14: /** tim@14: * Moves the calendar element to a different section in the DOM tree (changes tim@14: * its parent). tim@14: */ tim@14: Calendar.prototype.reparent = function (new_parent) { tim@14: var el = this.element; tim@14: el.parentNode.removeChild(el); tim@14: new_parent.appendChild(el); tim@14: }; tim@14: tim@14: // This gets called when the user presses a mouse button anywhere in the tim@14: // document, if the calendar is shown. If the click was outside the open tim@14: // calendar this function closes it. tim@14: Calendar._checkCalendar = function(ev) { tim@14: var calendar = window._dynarch_popupCalendar; tim@14: if (!calendar) { tim@14: return false; tim@14: } tim@14: var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev); tim@14: for (; el != null && el != calendar.element; el = el.parentNode); tim@14: if (el == null) { tim@14: // calls closeHandler which should hide the calendar. tim@14: window._dynarch_popupCalendar.callCloseHandler(); tim@14: return Calendar.stopEvent(ev); tim@14: } tim@14: }; tim@14: tim@14: /** Shows the calendar. */ tim@14: Calendar.prototype.show = function () { tim@14: var rows = this.table.getElementsByTagName("tr"); tim@14: for (var i = rows.length; i > 0;) { tim@14: var row = rows[--i]; tim@14: Calendar.removeClass(row, "rowhilite"); tim@14: var cells = row.getElementsByTagName("td"); tim@14: for (var j = cells.length; j > 0;) { tim@14: var cell = cells[--j]; tim@14: Calendar.removeClass(cell, "hilite"); tim@14: Calendar.removeClass(cell, "active"); tim@14: } tim@14: } tim@14: this.element.style.display = "block"; tim@14: this.hidden = false; tim@14: if (this.isPopup) { tim@14: window._dynarch_popupCalendar = this; tim@14: Calendar.addEvent(document, "keydown", Calendar._keyEvent); tim@14: Calendar.addEvent(document, "keypress", Calendar._keyEvent); tim@14: Calendar.addEvent(document, "mousedown", Calendar._checkCalendar); tim@14: } tim@14: this.hideShowCovered(); tim@14: }; tim@14: tim@14: /** tim@14: * Hides the calendar. Also removes any "hilite" from the class of any TD tim@14: * element. tim@14: */ tim@14: Calendar.prototype.hide = function () { tim@14: if (this.isPopup) { tim@14: Calendar.removeEvent(document, "keydown", Calendar._keyEvent); tim@14: Calendar.removeEvent(document, "keypress", Calendar._keyEvent); tim@14: Calendar.removeEvent(document, "mousedown", Calendar._checkCalendar); tim@14: } tim@14: this.element.style.display = "none"; tim@14: this.hidden = true; tim@14: this.hideShowCovered(); tim@14: }; tim@14: tim@14: /** tim@14: * Shows the calendar at a given absolute position (beware that, depending on tim@14: * the calendar element style -- position property -- this might be relative tim@14: * to the parent's containing rectangle). tim@14: */ tim@14: Calendar.prototype.showAt = function (x, y) { tim@14: var s = this.element.style; tim@14: s.left = x + "px"; tim@14: s.top = y + "px"; tim@14: this.show(); tim@14: }; tim@14: tim@14: /** Shows the calendar near a given element. */ tim@14: Calendar.prototype.showAtElement = function (el, opts) { tim@14: var self = this; tim@14: var p = Calendar.getAbsolutePos(el); tim@14: if (!opts || typeof opts != "string") { tim@14: this.showAt(p.x, p.y + el.offsetHeight); tim@14: return true; tim@14: } tim@14: function fixPosition(box) { tim@14: if (box.x < 0) tim@14: box.x = 0; tim@14: if (box.y < 0) tim@14: box.y = 0; tim@14: var cp = document.createElement("div"); tim@14: var s = cp.style; tim@14: s.position = "absolute"; tim@14: s.right = s.bottom = s.width = s.height = "0px"; tim@14: document.body.appendChild(cp); tim@14: var br = Calendar.getAbsolutePos(cp); tim@14: document.body.removeChild(cp); tim@14: if (Calendar.is_ie) { tim@14: br.y += document.body.scrollTop; tim@14: br.x += document.body.scrollLeft; tim@14: } else { tim@14: br.y += window.scrollY; tim@14: br.x += window.scrollX; tim@14: } tim@14: var tmp = box.x + box.width - br.x; tim@14: if (tmp > 0) box.x -= tmp; tim@14: tmp = box.y + box.height - br.y; tim@14: if (tmp > 0) box.y -= tmp; tim@14: }; tim@14: this.element.style.display = "block"; tim@14: Calendar.continuation_for_the_fucking_khtml_browser = function() { tim@14: var w = self.element.offsetWidth; tim@14: var h = self.element.offsetHeight; tim@14: self.element.style.display = "none"; tim@14: var valign = opts.substr(0, 1); tim@14: var halign = "l"; tim@14: if (opts.length > 1) { tim@14: halign = opts.substr(1, 1); tim@14: } tim@14: // vertical alignment tim@14: switch (valign) { tim@14: case "T": p.y -= h; break; tim@14: case "B": p.y += el.offsetHeight; break; tim@14: case "C": p.y += (el.offsetHeight - h) / 2; break; tim@14: case "t": p.y += el.offsetHeight - h; break; tim@14: case "b": break; // already there tim@14: } tim@14: // horizontal alignment tim@14: switch (halign) { tim@14: case "L": p.x -= w; break; tim@14: case "R": p.x += el.offsetWidth; break; tim@14: case "C": p.x += (el.offsetWidth - w) / 2; break; tim@14: case "l": p.x += el.offsetWidth - w; break; tim@14: case "r": break; // already there tim@14: } tim@14: p.width = w; tim@14: p.height = h + 40; tim@14: self.monthsCombo.style.display = "none"; tim@14: fixPosition(p); tim@14: self.showAt(p.x, p.y); tim@14: }; tim@14: if (Calendar.is_khtml) tim@14: setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10); tim@14: else tim@14: Calendar.continuation_for_the_fucking_khtml_browser(); tim@14: }; tim@14: tim@14: /** Customizes the date format. */ tim@14: Calendar.prototype.setDateFormat = function (str) { tim@14: this.dateFormat = str; tim@14: }; tim@14: tim@14: /** Customizes the tooltip date format. */ tim@14: Calendar.prototype.setTtDateFormat = function (str) { tim@14: this.ttDateFormat = str; tim@14: }; tim@14: tim@14: /** tim@14: * Tries to identify the date represented in a string. If successful it also tim@14: * calls this.setDate which moves the calendar to the given date. tim@14: */ tim@14: Calendar.prototype.parseDate = function(str, fmt) { tim@14: if (!fmt) tim@14: fmt = this.dateFormat; tim@14: this.setDate(Date.parseDate(str, fmt)); tim@14: }; tim@14: tim@14: Calendar.prototype.hideShowCovered = function () { tim@14: if (!Calendar.is_ie && !Calendar.is_opera) tim@14: return; tim@14: function getVisib(obj){ tim@14: var value = obj.style.visibility; tim@14: if (!value) { tim@14: if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // Gecko, W3C tim@14: if (!Calendar.is_khtml) tim@14: value = document.defaultView. tim@14: getComputedStyle(obj, "").getPropertyValue("visibility"); tim@14: else tim@14: value = ''; tim@14: } else if (obj.currentStyle) { // IE tim@14: value = obj.currentStyle.visibility; tim@14: } else tim@14: value = ''; tim@14: } tim@14: return value; tim@14: }; tim@14: tim@14: var tags = new Array("applet", "iframe", "select"); tim@14: var el = this.element; tim@14: tim@14: var p = Calendar.getAbsolutePos(el); tim@14: var EX1 = p.x; tim@14: var EX2 = el.offsetWidth + EX1; tim@14: var EY1 = p.y; tim@14: var EY2 = el.offsetHeight + EY1; tim@14: tim@14: for (var k = tags.length; k > 0; ) { tim@14: var ar = document.getElementsByTagName(tags[--k]); tim@14: var cc = null; tim@14: tim@14: for (var i = ar.length; i > 0;) { tim@14: cc = ar[--i]; tim@14: tim@14: p = Calendar.getAbsolutePos(cc); tim@14: var CX1 = p.x; tim@14: var CX2 = cc.offsetWidth + CX1; tim@14: var CY1 = p.y; tim@14: var CY2 = cc.offsetHeight + CY1; tim@14: tim@14: if (this.hidden || (CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) { tim@14: if (!cc.__msh_save_visibility) { tim@14: cc.__msh_save_visibility = getVisib(cc); tim@14: } tim@14: cc.style.visibility = cc.__msh_save_visibility; tim@14: } else { tim@14: if (!cc.__msh_save_visibility) { tim@14: cc.__msh_save_visibility = getVisib(cc); tim@14: } tim@14: cc.style.visibility = "hidden"; tim@14: } tim@14: } tim@14: } tim@14: }; tim@14: tim@14: /** Internal function; it displays the bar with the names of the weekday. */ tim@14: Calendar.prototype._displayWeekdays = function () { tim@14: var fdow = this.firstDayOfWeek; tim@14: var cell = this.firstdayname; tim@14: var weekend = Calendar._TT["WEEKEND"]; tim@14: for (var i = 0; i < 7; ++i) { tim@14: cell.className = "day name"; tim@14: var realday = (i + fdow) % 7; tim@14: if (i) { tim@14: cell.ttip = Calendar._TT["DAY_FIRST"].replace("%s", Calendar._DN[realday]); tim@14: cell.navtype = 100; tim@14: cell.calendar = this; tim@14: cell.fdow = realday; tim@14: Calendar._add_evs(cell); tim@14: } tim@14: if (weekend.indexOf(realday.toString()) != -1) { tim@14: Calendar.addClass(cell, "weekend"); tim@14: } tim@14: cell.innerHTML = Calendar._SDN[(i + fdow) % 7]; tim@14: cell = cell.nextSibling; tim@14: } tim@14: }; tim@14: tim@14: /** Internal function. Hides all combo boxes that might be displayed. */ tim@14: Calendar.prototype._hideCombos = function () { tim@14: this.monthsCombo.style.display = "none"; tim@14: this.yearsCombo.style.display = "none"; tim@14: }; tim@14: tim@14: /** Internal function. Starts dragging the element. */ tim@14: Calendar.prototype._dragStart = function (ev) { tim@14: if (this.dragging) { tim@14: return; tim@14: } tim@14: this.dragging = true; tim@14: var posX; tim@14: var posY; tim@14: if (Calendar.is_ie) { tim@14: posY = window.event.clientY + document.body.scrollTop; tim@14: posX = window.event.clientX + document.body.scrollLeft; tim@14: } else { tim@14: posY = ev.clientY + window.scrollY; tim@14: posX = ev.clientX + window.scrollX; tim@14: } tim@14: var st = this.element.style; tim@14: this.xOffs = posX - parseInt(st.left); tim@14: this.yOffs = posY - parseInt(st.top); tim@14: with (Calendar) { tim@14: addEvent(document, "mousemove", calDragIt); tim@14: addEvent(document, "mouseup", calDragEnd); tim@14: } tim@14: }; tim@14: tim@14: // BEGIN: DATE OBJECT PATCHES tim@14: tim@14: /** Adds the number of days array to the Date object. */ tim@14: Date._MD = new Array(31,28,31,30,31,30,31,31,30,31,30,31); tim@14: tim@14: /** Constants used for time computations */ tim@14: Date.SECOND = 1000 /* milliseconds */; tim@14: Date.MINUTE = 60 * Date.SECOND; tim@14: Date.HOUR = 60 * Date.MINUTE; tim@14: Date.DAY = 24 * Date.HOUR; tim@14: Date.WEEK = 7 * Date.DAY; tim@14: tim@14: Date.parseDate = function(str, fmt) { tim@14: var today = new Date(); tim@14: var y = 0; tim@14: var m = -1; tim@14: var d = 0; tim@14: var a = str.split(/\W+/); tim@14: var b = fmt.match(/%./g); tim@14: var i = 0, j = 0; tim@14: var hr = 0; tim@14: var min = 0; tim@14: for (i = 0; i < a.length; ++i) { tim@14: if (!a[i]) tim@14: continue; tim@14: switch (b[i]) { tim@14: case "%d": tim@14: case "%e": tim@14: d = parseInt(a[i], 10); tim@14: break; tim@14: tim@14: case "%m": tim@14: m = parseInt(a[i], 10) - 1; tim@14: break; tim@14: tim@14: case "%Y": tim@14: case "%y": tim@14: y = parseInt(a[i], 10); tim@14: (y < 100) && (y += (y > 29) ? 1900 : 2000); tim@14: break; tim@14: tim@14: case "%b": tim@14: case "%B": tim@14: for (j = 0; j < 12; ++j) { tim@14: if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { m = j; break; } tim@14: } tim@14: break; tim@14: tim@14: case "%H": tim@14: case "%I": tim@14: case "%k": tim@14: case "%l": tim@14: hr = parseInt(a[i], 10); tim@14: break; tim@14: tim@14: case "%P": tim@14: case "%p": tim@14: if (/pm/i.test(a[i]) && hr < 12) tim@14: hr += 12; tim@14: else if (/am/i.test(a[i]) && hr >= 12) tim@14: hr -= 12; tim@14: break; tim@14: tim@14: case "%M": tim@14: min = parseInt(a[i], 10); tim@14: break; tim@14: } tim@14: } tim@14: if (isNaN(y)) y = today.getFullYear(); tim@14: if (isNaN(m)) m = today.getMonth(); tim@14: if (isNaN(d)) d = today.getDate(); tim@14: if (isNaN(hr)) hr = today.getHours(); tim@14: if (isNaN(min)) min = today.getMinutes(); tim@14: if (y != 0 && m != -1 && d != 0) tim@14: return new Date(y, m, d, hr, min, 0); tim@14: y = 0; m = -1; d = 0; tim@14: for (i = 0; i < a.length; ++i) { tim@14: if (a[i].search(/[a-zA-Z]+/) != -1) { tim@14: var t = -1; tim@14: for (j = 0; j < 12; ++j) { tim@14: if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { t = j; break; } tim@14: } tim@14: if (t != -1) { tim@14: if (m != -1) { tim@14: d = m+1; tim@14: } tim@14: m = t; tim@14: } tim@14: } else if (parseInt(a[i], 10) <= 12 && m == -1) { tim@14: m = a[i]-1; tim@14: } else if (parseInt(a[i], 10) > 31 && y == 0) { tim@14: y = parseInt(a[i], 10); tim@14: (y < 100) && (y += (y > 29) ? 1900 : 2000); tim@14: } else if (d == 0) { tim@14: d = a[i]; tim@14: } tim@14: } tim@14: if (y == 0) tim@14: y = today.getFullYear(); tim@14: if (m != -1 && d != 0) tim@14: return new Date(y, m, d, hr, min, 0); tim@14: return today; tim@14: }; tim@14: tim@14: /** Returns the number of days in the current month */ tim@14: Date.prototype.getMonthDays = function(month) { tim@14: var year = this.getFullYear(); tim@14: if (typeof month == "undefined") { tim@14: month = this.getMonth(); tim@14: } tim@14: if (((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) && month == 1) { tim@14: return 29; tim@14: } else { tim@14: return Date._MD[month]; tim@14: } tim@14: }; tim@14: tim@14: /** Returns the number of day in the year. */ tim@14: Date.prototype.getDayOfYear = function() { tim@14: var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0); tim@14: var then = new Date(this.getFullYear(), 0, 0, 0, 0, 0); tim@14: var time = now - then; tim@14: return Math.floor(time / Date.DAY); tim@14: }; tim@14: tim@14: /** Returns the number of the week in year, as defined in ISO 8601. */ tim@14: Date.prototype.getWeekNumber = function() { tim@14: var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0); tim@14: var DoW = d.getDay(); tim@14: d.setDate(d.getDate() - (DoW + 6) % 7 + 3); // Nearest Thu tim@14: var ms = d.valueOf(); // GMT tim@14: d.setMonth(0); tim@14: d.setDate(4); // Thu in Week 1 tim@14: return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1; tim@14: }; tim@14: tim@14: /** Checks date and time equality */ tim@14: Date.prototype.equalsTo = function(date) { tim@14: return ((this.getFullYear() == date.getFullYear()) && tim@14: (this.getMonth() == date.getMonth()) && tim@14: (this.getDate() == date.getDate()) && tim@14: (this.getHours() == date.getHours()) && tim@14: (this.getMinutes() == date.getMinutes())); tim@14: }; tim@14: tim@14: /** Set only the year, month, date parts (keep existing time) */ tim@14: Date.prototype.setDateOnly = function(date) { tim@14: var tmp = new Date(date); tim@14: this.setDate(1); tim@14: this.setFullYear(tmp.getFullYear()); tim@14: this.setMonth(tmp.getMonth()); tim@14: this.setDate(tmp.getDate()); tim@14: }; tim@14: tim@14: /** Prints the date in a string according to the given format. */ tim@14: Date.prototype.print = function (str) { tim@14: var m = this.getMonth(); tim@14: var d = this.getDate(); tim@14: var y = this.getFullYear(); tim@14: var wn = this.getWeekNumber(); tim@14: var w = this.getDay(); tim@14: var s = {}; tim@14: var hr = this.getHours(); tim@14: var pm = (hr >= 12); tim@14: var ir = (pm) ? (hr - 12) : hr; tim@14: var dy = this.getDayOfYear(); tim@14: if (ir == 0) tim@14: ir = 12; tim@14: var min = this.getMinutes(); tim@14: var sec = this.getSeconds(); tim@14: s["%a"] = Calendar._SDN[w]; // abbreviated weekday name [FIXME: I18N] tim@14: s["%A"] = Calendar._DN[w]; // full weekday name tim@14: s["%b"] = Calendar._SMN[m]; // abbreviated month name [FIXME: I18N] tim@14: s["%B"] = Calendar._MN[m]; // full month name tim@14: // FIXME: %c : preferred date and time representation for the current locale tim@14: s["%C"] = 1 + Math.floor(y / 100); // the century number tim@14: s["%d"] = (d < 10) ? ("0" + d) : d; // the day of the month (range 01 to 31) tim@14: s["%e"] = d; // the day of the month (range 1 to 31) tim@14: // FIXME: %D : american date style: %m/%d/%y tim@14: // FIXME: %E, %F, %G, %g, %h (man strftime) tim@14: s["%H"] = (hr < 10) ? ("0" + hr) : hr; // hour, range 00 to 23 (24h format) tim@14: s["%I"] = (ir < 10) ? ("0" + ir) : ir; // hour, range 01 to 12 (12h format) tim@14: s["%j"] = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy; // day of the year (range 001 to 366) tim@14: s["%k"] = hr; // hour, range 0 to 23 (24h format) tim@14: s["%l"] = ir; // hour, range 1 to 12 (12h format) tim@14: s["%m"] = (m < 9) ? ("0" + (1+m)) : (1+m); // month, range 01 to 12 tim@14: s["%M"] = (min < 10) ? ("0" + min) : min; // minute, range 00 to 59 tim@14: s["%n"] = "\n"; // a newline character tim@14: s["%p"] = pm ? "PM" : "AM"; tim@14: s["%P"] = pm ? "pm" : "am"; tim@14: // FIXME: %r : the time in am/pm notation %I:%M:%S %p tim@14: // FIXME: %R : the time in 24-hour notation %H:%M tim@14: s["%s"] = Math.floor(this.getTime() / 1000); tim@14: s["%S"] = (sec < 10) ? ("0" + sec) : sec; // seconds, range 00 to 59 tim@14: s["%t"] = "\t"; // a tab character tim@14: // FIXME: %T : the time in 24-hour notation (%H:%M:%S) tim@14: s["%U"] = s["%W"] = s["%V"] = (wn < 10) ? ("0" + wn) : wn; tim@14: s["%u"] = w + 1; // the day of the week (range 1 to 7, 1 = MON) tim@14: s["%w"] = w; // the day of the week (range 0 to 6, 0 = SUN) tim@14: // FIXME: %x : preferred date representation for the current locale without the time tim@14: // FIXME: %X : preferred time representation for the current locale without the date tim@14: s["%y"] = ('' + y).substr(2, 2); // year without the century (range 00 to 99) tim@14: s["%Y"] = y; // year with the century tim@14: s["%%"] = "%"; // a literal '%' character tim@14: tim@14: var re = /%./g; tim@14: if (!Calendar.is_ie5 && !Calendar.is_khtml) tim@14: return str.replace(re, function (par) { return s[par] || par; }); tim@14: tim@14: var a = str.match(re); tim@14: for (var i = 0; i < a.length; i++) { tim@14: var tmp = s[a[i]]; tim@14: if (tmp) { tim@14: re = new RegExp(a[i], 'g'); tim@14: str = str.replace(re, tmp); tim@14: } tim@14: } tim@14: tim@14: return str; tim@14: }; tim@14: tim@14: Date.prototype.__msh_oldSetFullYear = Date.prototype.setFullYear; tim@14: Date.prototype.setFullYear = function(y) { tim@14: var d = new Date(this); tim@14: d.__msh_oldSetFullYear(y); tim@14: if (d.getMonth() != this.getMonth()) tim@14: this.setDate(28); tim@14: this.__msh_oldSetFullYear(y); tim@14: }; tim@14: tim@14: // END: DATE OBJECT PATCHES tim@14: tim@14: tim@14: // global object that remembers the calendar tim@14: window._dynarch_popupCalendar = null;