to get it into the distribution ;-)\n\n" +
"Thank you!\n" +
"http://www.Ithelp.com\n";
}
alert(text);
return;
case -2:
if (year > cal.minYear) {
date.setFullYear(year - 1);
}
break;
case -1:
if (mon > 0) {
setMonth(mon - 1);
} else if (year-- > cal.minYear) {
date.setFullYear(year);
setMonth(11);
}
break;
case 1:
if (mon < 11) {
setMonth(mon + 1);
} else if (year < cal.maxYear) {
date.setFullYear(year + 1);
setMonth(0);
}
break;
case 2:
if (year < cal.maxYear) {
date.setFullYear(year + 1);
}
break;
case 100:
cal.setFirstDayOfWeek(el.fdow);
Ithelp.Calendar.prefs.fdow = cal.firstDayOfWeek;
Ithelp.Calendar.savePrefs();
if (cal.onFDOW)
cal.onFDOW(cal.firstDayOfWeek);
return;
case 50:
var range = el._range;
var current = el.firstChild.data;
for (var i = range.length; --i >= 0;)
if (range[i] == current)
break;
if (ev && ev.shiftKey) {
if (--i < 0)
i = range.length - 1;
} else if ( ++i >= range.length )
i = 0;
el.firstChild.data = range[i];
cal.onUpdateTime();
return;
case 201: // timepart, UP
case 202: // timepart, DOWN
var cel = el.timePart;
var val = parseInt(cel.firstChild.data, 10);
var range = cel._range;
for (var i = range.length; --i >= 0;)
if (val == range[i]) {
val = i;
break;
}
var step = cel._step;
if (el.navtype == 201) {
val = step*Math.floor(val/step);
val += step;
if (val >= range.length)
val = 0;
} else {
val = step*Math.ceil(val/step);
val -= step;
if (val < 0)
val = range.length-1;
}
cel.firstChild.data = range[val];
cal.onUpdateTime();
return;
case 0:
// TODAY will bring us here
if (cal.getDateStatus && cal.getDateStatus(date, date.getFullYear(), date.getMonth(), date.getDate())) {
// remember, "date" was previously set to new
// Date() if TODAY was clicked; thus, it
// contains today date.
return false;
}
break;
}
if (!date.equalsTo(cal.date)) {
cal.setDate(date);
newdate = true;
}
}
if (newdate) {
cal.callHandler();
}
if (closing) {
Ithelp.Utils.removeClass(el, "hilite");
cal.callCloseHandler();
}
};
// END: CALENDAR STATIC FUNCTIONS
// BEGIN: CALENDAR OBJECT FUNCTIONS
/**
* This function creates the calendar HTML elements inside the given parent.
* If _par is null than it creates a popup calendar inside the BODY element.
* If _par is an element, be it BODY, then it creates a non-popup calendar
* (still hidden).
*
* The function looks rather complicated, but what it does is quite simple.
* The basic calendar elements will be created, that is, a containing DIV, a
* TABLE that contains a header (title, navigation bar and day names bar), a
* body containing 6 rows with 7 or 8 cells each (this depends on whether week
* numbers are on or off) and a footer containing the status bar. Appropriate
* event handlers are assigned to all buttons or to the title and status bar
* (for drag'n'drop).
*
* This function also builds the time selector if the calendar is configured
* so, and it also creates the elements required for combo boxes (years,
* months, history).
*
* This function does not display day names or dates. This is done in
* Ithelp.Calendar.prototype._init(). Therefore, by separating these 2
* actions we can make date switching happen much faster because the _init
* function will already have the elements in place (so we don't need to create
* them again and again). This was a major improvement which got in
* the calendar v0.9.1.
*
* @param _par
*/
Ithelp.Calendar.prototype.create = function (_par) {
var parent = null;
if (! _par) {
// default parent is the document body, in which case we create
// a popup calendar.
parent = window.document.getElementsByTagName("body")[0];
this.isPopup = true;
this.WCH = Ithelp.Utils.createWCH();
} else {
parent = _par;
this.isPopup = false;
}
this.date = this.dateStr ? new Date(this.dateStr) : new Date();
var table = Ithelp.Utils.createElement("table");
this.table = table;
table.cellSpacing = 0;
table.cellPadding = 0;
table.calendar = this;
Ithelp.Utils.addEvent(table, "mousedown", Ithelp.Calendar.tableMouseDown);
var div = Ithelp.Utils.createElement("div");
this.element = div;
div.className = "calendar";
if (this.isPopup) {
div.style.position = "absolute";
div.style.display = "none";
}
div.appendChild(table);
var thead = Ithelp.Utils.createElement("thead", table);
var cell = null;
var row = null;
var cal = this;
var hh = function (text, cs, navtype) {
cell = Ithelp.Utils.createElement("td", row);
cell.colSpan = cs;
cell.className = "button";
if (Math.abs(navtype) <= 2)
cell.className += " nav";
Ithelp.Calendar._add_evs(cell);
cell.calendar = cal;
cell.navtype = navtype;
if (text.substr(0, 1) != "&") {
cell.appendChild(document.createTextNode(text));
}
else {
// FIXME: dirty hack for entities
cell.innerHTML = text;
}
return cell;
};
row = Ithelp.Utils.createElement("tr", thead);
var title_length = 6;
this.isPopup && --title_length;
this.weekNumbers && ++title_length;
hh("?", 1, 400).ttip = Ithelp.Calendar.i18n("INFO");
this.title = hh("", title_length, 300);
this.title.className = "title";
if (this.isPopup) {
this.title.ttip = Ithelp.Calendar.i18n("DRAG_TO_MOVE");
this.title.style.cursor = "move";
hh("×", 1, 200).ttip = Ithelp.Calendar.i18n("CLOSE");
}
row = Ithelp.Utils.createElement("tr", thead);
row.className = "headrow";
this._nav_py = hh("«", 1, -2);
this._nav_py.ttip = Ithelp.Calendar.i18n("PREV_YEAR");
this._nav_pm = hh("‹", 1, -1);
this._nav_pm.ttip = Ithelp.Calendar.i18n("PREV_MONTH");
this._nav_now = hh(Ithelp.Calendar.i18n("TODAY"), this.weekNumbers ? 4 : 3, 0);
this._nav_now.ttip = Ithelp.Calendar.i18n("GO_TODAY");
this._nav_nm = hh("›", 1, 1);
this._nav_nm.ttip = Ithelp.Calendar.i18n("NEXT_MONTH");
this._nav_ny = hh("»", 1, 2);
this._nav_ny.ttip = Ithelp.Calendar.i18n("NEXT_YEAR");
// day names
row = Ithelp.Utils.createElement("tr", thead);
row.className = "daynames";
if (this.weekNumbers) {
cell = Ithelp.Utils.createElement("td", row);
cell.className = "name wn";
cell.appendChild(window.document.createTextNode(Ithelp.Calendar.i18n("WK")));
var cal_wk = Ithelp.Calendar.i18n("WK")
if (cal_wk == null) {
//if it's not defined in the language file, leave it blank
cal_wk = "";
}
}
for (var i = 7; i > 0; --i) {
cell = Ithelp.Utils.createElement("td", row);
cell.appendChild(window.document.createTextNode(""));
if (!i) {
cell.navtype = 100;
cell.calendar = this;
Ithelp.Calendar._add_evs(cell);
}
}
this.firstdayname = row.childNodes[this.weekNumbers?1:0];
this._displayWeekdays();
var tbody = Ithelp.Utils.createElement("tbody", table);
this.tbody = tbody;
for (i = 6; i > 0; --i) {
row = Ithelp.Utils.createElement("tr", tbody);
if (this.weekNumbers) {
cell = Ithelp.Utils.createElement("td", row);
cell.appendChild(document.createTextNode(""));
}
for (var j = 7; j > 0; --j) {
cell = Ithelp.Utils.createElement("td", row);
cell.appendChild(document.createTextNode(""));
cell.calendar = this;
Ithelp.Calendar._add_evs(cell);
}
}
var tfoot = Ithelp.Utils.createElement("tfoot", table);
if (this.showsTime) {
row = Ithelp.Utils.createElement("tr", tfoot);
row.className = "time";
cell = Ithelp.Utils.createElement("td", row);
cell.className = "timetext";
cell.colSpan = this.weekNumbers ? 2 : 1;
cell.innerHTML = Ithelp.Calendar.i18n("TIME") || " ";
(function(){
function makeTimePart(className, init, range_start, range_end) {
var table, tbody, tr, tr2, part;
if (range_end) {
cell = Ithelp.Utils.createElement("td", row);
cell.colSpan = 2;
cell.className = "parent-" + className;
table = Ithelp.Utils.createElement("table", cell);
table.cellSpacing = table.cellPadding = 0;
if (className == "hour")
table.align = "right";
table.className = "calendar-time-scroller";
tbody = Ithelp.Utils.createElement("tbody", table);
tr = Ithelp.Utils.createElement("tr", tbody);
tr2 = Ithelp.Utils.createElement("tr", tbody);
} else
tr = row;
part = Ithelp.Utils.createElement("td", tr);
part.className = className;
part.appendChild(window.document.createTextNode(init));
part.calendar = cal;
part.ttip = Ithelp.Calendar.i18n("TIME_PART");
part.navtype = 50;
part._range = [];
if (!range_end)
part._range = range_start;
else {
part.rowSpan = 2;
for (var i = range_start; i <= range_end; ++i) {
var txt;
if (i < 10 && range_end >= 10) txt = '0' + i;
else txt = '' + i;
part._range[part._range.length] = txt;
}
var up = Ithelp.Utils.createElement("td", tr);
up.className = "up";
up.navtype = 201;
up.calendar = cal;
up.timePart = part;
if (Ithelp.is_khtml)
up.innerHTML = " ";
Ithelp.Calendar._add_evs(up);
var down = Ithelp.Utils.createElement("td", tr2);
down.className = "down";
down.navtype = 202;
down.calendar = cal;
down.timePart = part;
if (Ithelp.is_khtml)
down.innerHTML = " ";
Ithelp.Calendar._add_evs(down);
}
Ithelp.Calendar._add_evs(part);
return part;
};
var hrs = cal.date.getHours();
var mins = cal.date.getMinutes();
var t12 = !cal.time24;
var pm = (hrs > 12);
if (t12 && pm) hrs -= 12;
var H = makeTimePart("hour", hrs, t12 ? 1 : 0, t12 ? 12 : 23);
H._step = 1;
cell = Ithelp.Utils.createElement("td", row);
cell.innerHTML = ":";
cell.className = "colon";
var M = makeTimePart("minute", mins, 0, 59);
M._step = 5; // FIXME: make this part configurable
var AP = null;
if (t12) {
AP = makeTimePart("ampm", pm ? "pm" : "am", ["am", "pm"]);
AP.className += " button";
} else
Ithelp.Utils.createElement("td", row).innerHTML = " ";
cal.onSetTime = function() {
var hrs = this.date.getHours();
var mins = this.date.getMinutes();
var pm = (hrs > 12);
if (pm && t12) hrs -= 12;
H.firstChild.data = (hrs < 10) ? ("0" + hrs) : hrs;
M.firstChild.data = (mins < 10) ? ("0" + mins) : mins;
if (t12)
AP.firstChild.data = pm ? "pm" : "am";
};
cal.onUpdateTime = function() {
var date = this.date;
var h = parseInt(H.firstChild.data, 10);
if (t12) {
if (/pm/i.test(AP.firstChild.data) && h < 12)
h += 12;
else if (/am/i.test(AP.firstChild.data) && h == 12)
h = 0;
}
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
date.setHours(h);
date.setMinutes(parseInt(M.firstChild.data, 10));
date.setFullYear(y);
date.setMonth(m);
date.setDate(d);
this.dateClicked = false;
this.callHandler();
};
})();
} else {
this.onSetTime = this.onUpdateTime = function() {};
}
row = Ithelp.Utils.createElement("tr", tfoot);
row.className = "footrow";
cell = hh(Ithelp.Calendar.i18n("SEL_DATE"), this.weekNumbers ? 8 : 7, 300);
cell.className = "ttip";
if (this.isPopup) {
cell.ttip = Ithelp.Calendar.i18n("DRAG_TO_MOVE");
cell.style.cursor = "move";
}
this.tooltips = cell;
div = this.monthsCombo = Ithelp.Utils.createElement("div", this.element);
div.className = "combo";
for (i = 0; i < 12; ++i) {
var mn = Ithelp.Utils.createElement("div");
mn.className = Ithelp.is_ie ? "label-IEfix" : "label";
mn.month = i;
mn.appendChild(window.document.createTextNode(Ithelp.Calendar.i18n(i, "smn")));
div.appendChild(mn);
}
div = this.yearsCombo = Ithelp.Utils.createElement("div", this.element);
div.className = "combo";
for (i = 12; i > 0; --i) {
var yr = Ithelp.Utils.createElement("div");
yr.className = Ithelp.is_ie ? "label-IEfix" : "label";
yr.appendChild(window.document.createTextNode(""));
div.appendChild(yr);
}
div = this.histCombo = Ithelp.Utils.createElement("div", this.element);
div.className = "combo history";
this._init(this.firstDayOfWeek, this.date);
parent.appendChild(this.element);
};
/**
* This function handles keypress events that occur while a popup calendar is
* displayed. The implementation is quite complicated; this function calls
* cellClick in order to set the new date as if it was clicked.
*
* @param ev [Event] the event object
* @return false
*/
Ithelp.Calendar._keyEvent = function(ev) {
if (!window.calendar) {
return false;
}
(Ithelp.is_ie) && (ev = window.event);
var cal = window.calendar;
var act = (Ithelp.is_ie || ev.type == "keypress");
var K = ev.keyCode;
if (ev.ctrlKey) {
switch (K) {
case 37: // KEY left
act && Ithelp.Calendar.cellClick(cal._nav_pm);
break;
case 38: // KEY up
act && Ithelp.Calendar.cellClick(cal._nav_py);
break;
case 39: // KEY right
act && Ithelp.Calendar.cellClick(cal._nav_nm);
break;
case 40: // KEY down
act && Ithelp.Calendar.cellClick(cal._nav_ny);
break;
default:
return false;
}
} else switch (K) {
case 32: // KEY space (now)
Ithelp.Calendar.cellClick(cal._nav_now);
break;
case 27: // KEY esc
act && cal.callCloseHandler();
break;
case 37: // KEY left
case 38: // KEY up
case 39: // KEY right
case 40: // KEY down
if (act) {
var prev, pos, x, y, ne, el;
prev = (K == 37) || (K == 38);
function setVars() {
el = cal.currentDateEl;
pos = el.pos;
y = pos[0];
x = pos[1];
ne = cal.ar_days[y][x];
};setVars();
function prevMonth() {
//Ithelp.Calendar.cellClick(cal._nav_pm);
var date = new Date(cal.date.getFullYear(), cal.date.getMonth()-1, 1);
date.setDate(date.getMonthDays());
cal.setDate(date);
};
function nextMonth() {
//Ithelp.Calendar.cellClick(cal._nav_nm);
var date = new Date(cal.date.getFullYear(), cal.date.getMonth()+1, 1);
cal.setDate(date);
};
while (1) {
switch (K) {
case 37: // KEY left
if (--x >= 0)
ne = cal.ar_days[y][x];
else {
x = 6;
K = 38;
continue;
}
break;
case 38: // KEY up
if (--y >= 0)
ne = cal.ar_days[y][x];
else {
prevMonth();
setVars();
}
break;
case 39: // KEY right
if (++x < 7)
ne = cal.ar_days[y][x];
else {
x = 0;
K = 40;
continue;
}
break;
case 40: // KEY down
if (++y < cal.ar_days.length)
ne = cal.ar_days[y][x];
else {
nextMonth();
setVars();
}
break;
}
break;
}
if (ne) {
if (!ne.otherMonth) {
Ithelp.Utils.removeClass(el, "selected");
Ithelp.Utils.addClass(ne, "selected");
cal.date.setDateOnly(new Date(ne.caldate[0], ne.caldate[1], ne.caldate[2]));
cal.currentDateEl = ne;
} else {
if (!ne.disabled)
Ithelp.Calendar.cellClick(ne);
else if (prev)
prevMonth();
else
nextMonth();
}
}
cal.callHandler();
}
break;
case 13: // KEY enter
if (act) {
cal.callHandler();
cal.hide();
}
break;
default:
return false;
}
return Ithelp.Utils.stopEvent(ev);
};
/**
* (RE)Initializes the calendar to the given date and firstDayOfWeek.
*
* This function perform the action of actually displaying the day names and
* dates in the calendar. But first, it checks if the passed date fits in the
* allowed range, configured by the "minYear", "maxYear", "minMonth" and
* "maxMonth" properties of the Calendar object.
*
* It takes care to highlight special days (calling the
* calendar.getDateStatus() function which can be overridden by external
* scripts) or to highlight any dates that might be selected (for instance when
* multiple dates is on, this function will call _initMultipleDates() to
* highlight selected dates accordingly).
*
* This function is highly optimized for speed, therefore the code in it is not
* trivial and what it does might not seem obvious. :-) So, WARNING, this is
* voodoo. If you want to properly understand the code you should analyze it
* line by line and try to execute it step by step; use the Venkman JS
* debugger.
*
* @param firstDayOfWeek [int] the first day of week, 0 for Sunday, 1 for Monday, etc.
* @param date [Date] the date to initialize the calendar to
*
* @return
*/
Ithelp.Calendar.prototype._init = function (firstDayOfWeek, date) {
var
today = new Date(),
TD = today.getDate(),
TY = today.getFullYear(),
TM = today.getMonth();
//this.table.style.visibility = "hidden";
var year = date.getFullYear();
var month = date.getMonth();
if (year < this.minYear)
date.setFullYear(year = this.minYear);
else if (year > this.maxYear)
date.setFullYear(year = this.maxYear);
if (year == this.minYear && month < this.minMonth)
date.setMonth(month = this.minMonth);
else if (year == this.maxYear && month > this.maxMonth)
date.setMonth(month = this.maxMonth);
this.firstDayOfWeek = firstDayOfWeek;
(this.date = new Date(this.date)).setDateOnly(date);
var mday = date.getDate();
var no_days = date.getMonthDays();
// calendar voodoo for computing the first day that would actually be
// displayed in the calendar, even if it's from the previous month.
// WARNING: this is magic. ;-)
date.setDate(1);
var day1 = (date.getDay() - this.firstDayOfWeek) % 7;
if (day1 < 0)
day1 += 7;
date.setDate(-day1);
date.setDate(date.getDate() + 1);
var row = this.tbody.firstChild;
var MN = Ithelp.Calendar.i18n(month, "smn");
var ar_days = this.ar_days = [];
var weekend = Ithelp.Calendar.i18n("WEEKEND");
var dates = this.multiple ? (this.datesCells = {}) : null;
var DATETXT = this.getDateText;
for (var i = 7; --i > 0; row = row.nextSibling) {
var cell = row.firstChild;
if (this.weekNumbers) {
cell.className = "day wn";
cell.innerHTML = date.getWeekNumber();
cell = cell.nextSibling;
}
var ar_days_y = ar_days[ar_days.length] = [];
row.className = "daysrow";
var hasdays = false, iday;
for (; cell && (iday = date.getDate()); date.setDate(iday+1), cell = cell.nextSibling) {
cell.pos = [6-i, ar_days_y.length];
ar_days_y[ar_days_y.length] = cell;
var
wday = date.getDay(),
dmonth = date.getMonth(),
dyear = date.getFullYear();
cell.className = "day";
var current_month = !(cell.otherMonth = !(dmonth == month));
if (!current_month) {
if (this.showsOtherMonths)
cell.className += " othermonth";
else {
cell.className = "emptycell";
cell.innerHTML = " ";
cell.disabled = true;
continue;
}
} else
hasdays = true;
cell.disabled = false;
cell.innerHTML = DATETXT ? DATETXT(date, year, month, iday) : iday;
dates && (dates[date.print("%Y%m%d")] = cell);
if (this.getDateStatus) {
var status = this.getDateStatus(date, year, month, iday);
if (this.getDateToolTip) {
var toolTip = this.getDateToolTip(date, year, month, iday);
if (toolTip)
cell.title = toolTip;
}
if (status === true) {
cell.className += " disabled";
cell.disabled = true;
} else {
if (/disabled/i.test(status))
cell.disabled = true;
cell.className += " " + status;
}
}
if (!cell.disabled) {
cell.caldate = [dyear, dmonth, iday];
cell.ttip = "_";
if (!this.multiple && current_month && iday == mday && this.hiliteToday) {
cell.className += " selected";
this.currentDateEl = cell;
}
if (dyear == TY && dmonth == TM && iday == TD) {
cell.className += " today";
cell.ttip += Ithelp.Calendar.i18n("PART_TODAY");
}
if ((weekend != null) && (weekend.indexOf(wday.toString()) != -1)) {
cell.className += cell.otherMonth ? " oweekend" : " weekend";
}
}
}
if (!(hasdays || this.showsOtherMonths))
row.className = "emptyrow";
}
//this.title.firstChild.data = Ithelp.Calendar.i18n(month, "mn") + ", " + year;
this.title.innerHTML = Ithelp.Calendar.i18n(month, "mn") + ", " + year;
this.onSetTime();
//this.table.style.visibility = "visible";
this._initMultipleDates();
this.updateWCH();
// PROFILE
// this.showHint("Generated in " + ((new Date()) - today) + " ms");
};
/**
* If "multiple dates" is selected (the calendar.multiple property) this
* function will highlight cells that display dates that are selected. It is
* only called from the _init() function.
*/
Ithelp.Calendar.prototype._initMultipleDates = function() {
if (this.multiple) {
for (var i in this.multiple) {
var cell = this.datesCells[i];
var d = this.multiple[i];
if (!d)
continue;
if (cell)
cell.className += " selected";
}
}
};
/**
* Given a Date object, this function will "toggle" it in the calendar; that
* is, it will select it if not already selected, or unselect it if was already
* selected. The array of dates is updated accordingly and the cell object
* will be added or removed the appropriate class name ("selected"). Of
* course, this only takes place if "multiple dates" is selected.
*
* @param date [Date] the date to (un)select.
*/
Ithelp.Calendar.prototype._toggleMultipleDate = function(date) {
if (this.multiple) {
var ds = date.print("%Y%m%d");
var cell = this.datesCells[ds];
if (cell) {
var d = this.multiple[ds];
if (!d) {
Ithelp.Utils.addClass(cell, "selected");
this.multiple[ds] = date;
} else {
Ithelp.Utils.removeClass(cell, "selected");
delete this.multiple[ds];
}
}
}
};
/**
* Call this in order to install a function handler that returns a tooltip for
* the given date. For example:
*
* \code
* function myHandler(date) {
* var str = date.print("%Y/%m/%d");
* if (str == "1979/08/03") {
* return "Happy birthday Mishoo! :D";
* }
* return str;
* }
* calendar.setDateToolTipHandler(myHandler);
* \endcode
*
* The tooltip handler is a "unary" function (receives one argument). The
* argument passed is a date object and the function should return the tooltip
* for that date.
*
* @param unaryFunction [function] your tooltip handler, as described above
*/
Ithelp.Calendar.prototype.setDateToolTipHandler = function (unaryFunction) {
this.getDateToolTip = unaryFunction;
};
/**
* Moves the calendar to the specified date. If \em date is not passed, then
* the "today" date is assumed. This function does range checking and displays
* an error in the status bar if the new date is not allowed by the configured
* calendar range. Otherwise, it simply calls _init() with the new date.
*
* @param date [Date, optional] the date object.
*/
Ithelp.Calendar.prototype.setDate = function (date) {
// workaround for some bugs in our parseDate code
if (!date)
date = new Date();
if (!date.equalsTo(this.date)) {
var year = date.getFullYear(), m = date.getMonth();
if (year == this.minYear && m < this.minMonth)
this.showHint("" + Ithelp.Calendar.i18n("E_RANGE") + " »»»
");
else if (year == this.maxYear && m > this.maxMonth)
this.showHint("««« " + Ithelp.Calendar.i18n("E_RANGE") + "
");
this._init(this.firstDayOfWeek, date);
}
};
/**
* Displays a hint in the status bar
*
* @param text [string] what to display
*/
Ithelp.Calendar.prototype.showHint = function(text) {
this.tooltips.innerHTML = text;
};
/**
* Refreshes the calendar. Useful if the "disabledHandler" function is
* dynamic, meaning that the list of disabled date can change at runtime. Just
* call this function if you think that the list of disabled dates should
* change.
*
* This function simply calls _init() using the current firstDayOfWeek and the
* current calendar date.
*/
Ithelp.Calendar.prototype.reinit = function() {
this._init(this.firstDayOfWeek, this.date);
};
/**
* "refresh()" isn't a good name for it: this function _destroys_ the calendar
* object and creates another one with the same parameters. This comes in
* handy for the calendar wizard where we need to reconstruct the calendar for
* virtually any property change.
*/
Ithelp.Calendar.prototype.refresh = function() {
var p = this.isPopup ? null : this.element.parentNode;
var x = parseInt(this.element.style.left);
var y = parseInt(this.element.style.top);
this.destroy();
this.dateStr = this.date;
this.create(p);
if (this.isPopup)
this.showAt(x, y);
else
this.show();
};
/**
* Configures the "firstDayOfWeek" parameter of the calendar.
*
* @param firstDayOfWeek [int] the new first day of week, 0 for Sunday, 1 for Monday, etc.
*/
Ithelp.Calendar.prototype.setFirstDayOfWeek = function (firstDayOfWeek) {
if (this.firstDayOfWeek != firstDayOfWeek) {
this._init(firstDayOfWeek, this.date);
this._displayWeekdays();
}
};
/**
* These functions allow one to install a handler that gets called for each
* date when a month is displayed in the calendar. Based on this handler's
* return value, that date can be disabled or highlighted using a class name
* returned by the handler.
*
* The handler has the following prototype:
*
* \code
* function dateStatus(date, year, month, day);
* \endcode
*
* While all 4 parameters are passed, the handler can for instance use only the
* first one. The year, month and day can all be determined from the first
* parameter which is a Date object, but because many people will explicitely
* need the year, month or day, we pass them too to speed things up (since we
* already know them at the time the handler is called).
*
* Here is an example of a not-so-complex handler:
*
* \code
* function my_DateStatus(date, year, month, day) {
* var str = date.print("%Y/%m/%d");
* if (str >= '2000/01/01' && str <= '2000/12/31') {
* return true; // disable all dates in 2000
* }
* if (str == "1979/08/03") {
* return "birthday";
* }
* return false;
* }
* calendar.setDateStatusHandler(my_DateStatus);
* \endcode
*
* The above handler will disable all dates in 2000 (returns true) and
* highlight "1979/08/03" using the class "birthday". From this example we can
* notice that the handler can return a boolean value or a string value. The
* "boolean" return type is supported for backwards compatibility (function
* setDisabledHandler, which is deprecated by setDateStatusHandler). Here's
* what the return value means:
*
* - \b true: the date will be disabled.
* - \b false: no action taken (date stays enabled).
* - "disabled": the date will be disabled.
* - "other_string": the "other_string" will be added to the cell's class name.
* - "disabled other_string": the date will be disabled and "disabled other_string"
* will be added to cell's class name; this helps one make a date disabled while
* still highlighting it in a special way.
*
* Note that user defined class names should have an associated CSS part
* somewhere in the document that specifies how the days will look like;
* otherwise, no difference will be visible. For instance, for highlighting
* "birthday" dates, one should also add:
*
* \code
* .birthday { color: #f00; }
* \endcode
*
* somewhere in the CSS of the calling document. (the above will make them
* red).
*
* Disabled dates are not clickable; however, if one overrides the "disable"
* CSS class, or if the cell also gets an "other_string" class that contains
* settings that override the "disabled" class, those cells might not look
* "disabled" but still behave so.
*
* \b WARNING: this function gets called 28 to 31 times each time a month is
* displayed. This means that if you are doing crazy computations in order to
* determine the status of a day, things \em will slow down dramatically. You
* have been warned.
*
* @param unaryFunction [function] handler that decides the status of the passed date
*/
Ithelp.Calendar.prototype.setDateStatusHandler = Ithelp.Calendar.prototype.setDisabledHandler = function (unaryFunction) {
this.getDateStatus = unaryFunction;
};
/**
* Configures a range of allowed dates for the calendar. Currently, this
* function supports setting a range on a "month granularity". This means,
* using it you can't disable part of a month. Both parameters are numeric and
* can be float. The range is "inclusive".
*
* This function might seem somehow complicated, but it's designed in a way
* that keeps backwards compatibility with the calendar v0.9.6.
*
* -# when the end points are integers, the full years will be included. That
* is, if you call calendar.setRange(1999, 2005) then only dates between and
* including 1999/01/01 and 2005/12/31 will be allowed.
* -# when the end points are floats, the decimal part specifies the month.
* Therefore, calendar.setRange(1999.03, 2005.05) will allow dates between
* and including 1999/03/01 (March 1) and 2005/05/31 (May 31).
*
* The above statements mean that the following two lines are equivalent:
*
* \code
* calendar.setDate(1999, 2005); // or
* calendar.setDate(1999.01, 2005.12);
* \endcode
*
* @param A [number] the range start point
* @param Z [number] the range end point
*/
Ithelp.Calendar.prototype.setRange = function (A, Z) {
var m,
a = Math.min(A, Z),
z = Math.max(A, Z);
this.minYear = m = Math.floor(a);
this.minMonth = (m == a) ? 0 : Math.ceil((a-m)*100-1);
this.maxYear = m = Math.floor(z);
this.maxMonth = (m == z) ? 11 : Math.ceil((z-m)*100-1);
};
/**
* Call the calendar's "onSelected" handler, if defined. The passed arguments
* are the date object and a string with the date formatted by the specifier in
* calendar.dateFormat.
*/
Ithelp.Calendar.prototype.callHandler = function () {
if (this.onSelected) {
this.onSelected(this, this.date.print(this.dateFormat));
}
};
/**
* This function updates the calendar history and saves the cookie. The
* history is a string containing date and time formatted as "%Y/%m/%d/%H/%M"
* (that is, all time parts separated by slashes, in a "most significant to
* least significant order"). Further, such formats are separated by commas,
* and the current calendar date is added the first, then the cookie saved.
*/
Ithelp.Calendar.prototype.updateHistory = function () {
var a, i, d, tmp, s, str = "", len = Ithelp.Calendar.prefs.hsize - 1;
if (Ithelp.Calendar.prefs.history) {
a = Ithelp.Calendar.prefs.history.split(/,/);
i = 0;
while (i < len && (tmp = a[i++])) {
s = tmp.split(/\//);
d = new Date(parseInt(s[0], 10), parseInt(s[1], 10)-1, parseInt(s[2], 10),
parseInt(s[3], 10), parseInt(s[4], 10));
if (!d.dateEqualsTo(this.date))
str += "," + tmp;
}
}
Ithelp.Calendar.prefs.history = this.date.print("%Y/%m/%d/%H/%M") + str;
Ithelp.Calendar.savePrefs();
};
/**
* Calls the calendar's onClose handler, if present. Either way, this function
* calls updateHistory() in order to update the history cookie.
*/
Ithelp.Calendar.prototype.callCloseHandler = function () {
if (this.dateClicked) {
this.updateHistory();
}
if (this.onClose) {
this.onClose(this);
}
this.hideShowCovered();
};
/** Removes the calendar object from the DOM tree and destroys it. */
Ithelp.Calendar.prototype.destroy = function () {
this.hide(); // this also removes keyboard events :-\
Ithelp.Utils.destroy(this.element);
Ithelp.Utils.destroy(this.WCH);
Ithelp.Calendar._C = null;
window.calendar = null;
};
/**
* Moves the calendar element to a different section in the DOM tree (changes
* its parent). This might be useful for flat calendars.
*
* @param new_parent [HTMLElement] the new parent for the calendar.
*/
Ithelp.Calendar.prototype.reparent = function (new_parent) {
var el = this.element;
el.parentNode.removeChild(el);
new_parent.appendChild(el);
};
/**
* This gets called when the user presses a mouse button anywhere in the
* document, if the calendar is shown. If the click was outside the open
* calendar this function closes it and stops the event from propagating.
*
* @param ev [Event] the event object.
* @return false if the event is stopped.
*/
Ithelp.Calendar._checkCalendar = function(ev) {
if (!window.calendar) {
return false;
}
var el = Ithelp.is_ie ? Ithelp.Utils.getElement(ev) : Ithelp.Utils.getTargetElement(ev);
for (; el != null && el != calendar.element; el = el.parentNode);
if (el == null) {
// calls closeHandler which should hide the calendar.
window.calendar.callCloseHandler();
return Ithelp.Utils.stopEvent(ev);
}
};
/**
* Updates the calendar "WCH" (windowed controls hider). A WCH is an
* "invention" (read: "miserable hack") that works around one of the most
* common and old bug in Internet Explorer: the SELECT boxes or IFRAMES show on
* top of any other HTML element. This function makes sure that the WCH covers
* correctly the calendar element and another element if passed.
*
* @param other_el [HTMLElement, optional] a second element that the WCH should cover.
*/
Ithelp.Calendar.prototype.updateWCH = function(other_el) {
Ithelp.Utils.setupWCH_el(this.WCH, this.element, other_el);
};
/**
* Displays a hidden calendar. It walks quickly through the HTML elements and
* makes sure that they don't have "hover" or "active" class names that might
* be there from a previous time the same calendar was displayed. This
* function also calls updateWCH() and hideShowCovered() to workaround
* miserable IE bugs.
*
* If the calendar is a popup calendar and doesn't have the "noGrab" property
* set, this function also adds document event handlers to intercept key events
* or to close the calendar when one clicks outside it.
*/
Ithelp.Calendar.prototype.show = function () {
var rows = this.table.getElementsByTagName("tr");
for (var i = rows.length; i > 0;) {
var row = rows[--i];
Ithelp.Utils.removeClass(row, "rowhilite");
var cells = row.getElementsByTagName("td");
for (var j = cells.length; j > 0;) {
var cell = cells[--j];
Ithelp.Utils.removeClass(cell, "hilite");
Ithelp.Utils.removeClass(cell, "active");
}
}
this.element.style.display = "block";
this.hidden = false;
if (this.isPopup) {
this.updateWCH();
window.calendar = this;
if (!this.noGrab) {
Ithelp.Utils.addEvent(window.document, "keydown", Ithelp.Calendar._keyEvent);
Ithelp.Utils.addEvent(window.document, "keypress", Ithelp.Calendar._keyEvent);
Ithelp.Utils.addEvent(window.document, "mousedown", Ithelp.Calendar._checkCalendar);
}
}
this.hideShowCovered();
};
/**
* Hides the calendar. Also removes any "hilite" from the class of any TD
* element. Unregisters the document event handlers for key presses and
* mousedown.
*/
Ithelp.Calendar.prototype.hide = function () {
if (this.isPopup) {
Ithelp.Utils.removeEvent(window.document, "keydown", Ithelp.Calendar._keyEvent);
Ithelp.Utils.removeEvent(window.document, "keypress", Ithelp.Calendar._keyEvent);
Ithelp.Utils.removeEvent(window.document, "mousedown", Ithelp.Calendar._checkCalendar);
}
this.element.style.display = "none";
Ithelp.Utils.hideWCH(this.WCH);
this.hidden = true;
this.hideShowCovered();
};
/**
* Shows the calendar at a given absolute position (beware that, depending on
* the calendar element style -- position property -- this might be relative to
* the parent's containing rectangle).
*
* @param x [int] the X position
* @param y [int] the Y position
*/
Ithelp.Calendar.prototype.showAt = function (x, y) {
var s = this.element.style;
s.left = x + "px";
s.top = y + "px";
this.show();
};
/**
* This function displays the calendar near a given "anchor" element, according
* to some rules passed in \em opts. The \em opts argument is a string
* containing one or 2 letters. The first letter decides the vertical
* alignment, and the second letter decides the horizontal alignment relative
* to the anchor element. Following we will describe these options; in parens
* we will use simple descriptions like "top to bottom" which means that the
* top margin of the calendar is aligned with the bottom margin of the object.
*
* \b Vertical align:
*
* - T -- the calendar is completely above the element (bottom to top)
* - t -- the calendar is above the element but might overlap it (bottom to bottom)
* - C -- the calendar is vertically centered to the element
* - b -- the calendar is below the element but might overlap it (top to top)
* - B -- the calendar is completely below the element (top to bottom)
*
* \b Horizontal align (defaults to 'l' if no letter passed):
*
* - L -- the calendar is completely to the left of the element (right to left)
* - l -- the calendar is to the left of the element but might overlap it (right to right)
* - C -- the calendar is horizontally centered to the element
* - r -- the calendar is to the right of the element but might overlap it (left to left)
* - R -- the calendar is completely to the right of the element (left to right)
*
* @param el [HTMLElement] the anchor element
* @param opts [string, optional] the align options, as described above. Defaults to "Bl" if nothing passed.
*/
Ithelp.Calendar.prototype.showAtElement = function (el, opts) {
var self = this;
var p = Ithelp.Utils.getAbsolutePos(el);
if (!opts || typeof opts != "string") {
this.showAt(p.x, p.y + el.offsetHeight);
return true;
}
this.element.style.display = "block";
var w = self.element.offsetWidth;
var h = self.element.offsetHeight;
self.element.style.display = "none";
var valign = opts.substr(0, 1);
var halign = "l";
if (opts.length > 1) {
halign = opts.substr(1, 1);
}
// vertical alignment
switch (valign) {
case "T": p.y -= h; break;
case "B": p.y += el.offsetHeight; break;
case "C": p.y += (el.offsetHeight - h) / 2; break;
case "t": p.y += el.offsetHeight - h; break;
case "b": break; // already there
}
// horizontal alignment
switch (halign) {
case "L": p.x -= w; break;
case "R": p.x += el.offsetWidth; break;
case "C": p.x += (el.offsetWidth - w) / 2; break;
case "l": p.x += el.offsetWidth - w; break;
case "r": break; // already there
}
p.width = w;
p.height = h + 40;
self.monthsCombo.style.display = "none";
Ithelp.Utils.fixBoxPosition(p);
self.showAt(p.x, p.y);
};
/**
* Customizes the date format that will be reported to the onSelect handler.
* The format string is described in Date.prototype.print().
*
* @param str [string] the date format.
*/
Ithelp.Calendar.prototype.setDateFormat = function (str) {
this.dateFormat = str;
};
/** Customizes the tooltip date format. See
* Ithelp.Calendar.prototype.setDateFormat() for a description of the \em str
* format.
*
* @param str [string] the "tooltip" date format
*/
Ithelp.Calendar.prototype.setTtDateFormat = function (str) {
this.ttDateFormat = str;
};
/**
* Tries to identify the date represented in a string. If successful it also
* calls this.setDate which moves the calendar to the given date.
*
* @param str [string] a date
* @param fmt [string] the format to try to parse \em str in
*/
Ithelp.Calendar.prototype.parseDate = function (str, fmt) {
// Konqueror
if (!str)
return this.setDate(this.date);
if (!fmt)
fmt = this.dateFormat;
var date = Date.parseDate(str, fmt);
return this.setDate(date);
};
/**
* This function hides or shows "windowed controls" accordingly so that the
* calendar isn't obtured by any such control. Historically, this function was
* used for any browser. It simply walks through all SELECT, IFRAME and APPLET
* elements present in the DOM, checks if they intersect the calendar and hides
* them if so or makes them visible otherwise. This approacy has a number of
* problems, the most important being that if the end-user's code contains a
* SELECT which is already hidden and it must stay hidden, it will still be
* made visible when the calendar closes. The other obvious problem is that
* there's an ugly effect generated by elements that suddenly (dis)appear when
* you drag the calendar around the screen.
*
* Currently this function is only used on IE5.0/Windows, browser that leaves
* no room for a better workaround to this problem. For IE5.5+/Windows an
* workaround is possible, albeit amazingly ugly (WCH). For other browsers
* such crazy techniques are not anymore useful because the bugs related to
* windowed controls were fixed.
*/
Ithelp.Calendar.prototype.hideShowCovered = function () {
if (!Ithelp.is_ie5)
return;
var self = this;
function getVisib(obj){
var value = obj.style.visibility;
if (!value) {
if (window.document.defaultView && typeof (window.document.defaultView.getComputedStyle) == "function") { // Gecko, W3C
if (!Ithelp.is_khtml)
value = window.document.defaultView.
getComputedStyle(obj, "").getPropertyValue("visibility");
else
value = '';
} else if (obj.currentStyle) { // IE
value = obj.currentStyle.visibility;
} else
value = '';
}
return value;
};
var tags = ["applet", "iframe", "select"];
var el = self.element;
var p = Ithelp.Utils.getAbsolutePos(el);
var EX1 = p.x;
var EX2 = el.offsetWidth + EX1;
var EY1 = p.y;
var EY2 = el.offsetHeight + EY1;
for (var k = tags.length; k > 0; ) {
var ar = window.document.getElementsByTagName(tags[--k]);
var cc = null;
for (var i = ar.length; i > 0;) {
cc = ar[--i];
p = Ithelp.Utils.getAbsolutePos(cc);
var CX1 = p.x;
var CX2 = cc.offsetWidth + CX1;
var CY1 = p.y;
var CY2 = cc.offsetHeight + CY1;
if (self.hidden || (CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {
if (!cc.__msh_save_visibility) {
cc.__msh_save_visibility = getVisib(cc);
}
cc.style.visibility = cc.__msh_save_visibility;
} else {
if (!cc.__msh_save_visibility) {
cc.__msh_save_visibility = getVisib(cc);
}
cc.style.visibility = "hidden";
}
}
}
};
/**
* This function displays the week day names in the calendar header, according
* to the current "firstDayOfWeek".
*/
Ithelp.Calendar.prototype._displayWeekdays = function () {
var fdow = this.firstDayOfWeek;
var cell = this.firstdayname;
var weekend = Ithelp.Calendar.i18n("WEEKEND");
for (var i = 0; i < 7; ++i) {
cell.className = "day name";
var realday = (i + fdow) % 7;
if (i) {
if (Ithelp.Calendar.i18n("DAY_FIRST") != null) {
cell.ttip = Ithelp.Calendar.i18n("DAY_FIRST").replace("%s", Ithelp.Calendar.i18n(realday, "dn"));
}
cell.navtype = 100;
cell.calendar = this;
cell.fdow = realday;
Ithelp.Calendar._add_evs(cell);
}
if ((weekend != null) && (weekend.indexOf(realday.toString()) != -1)) {
Ithelp.Utils.addClass(cell, "weekend");
}
cell.innerHTML = Ithelp.Calendar.i18n((i + fdow) % 7, "sdn");
cell = cell.nextSibling;
}
};
/** \internal Hides all combo boxes that might be displayed. */
Ithelp.Calendar.prototype._hideCombos = function () {
this.monthsCombo.style.display = "none";
this.yearsCombo.style.display = "none";
this.histCombo.style.display = "none";
this.updateWCH();
};
/** \internal Starts dragging the element. */
Ithelp.Calendar.prototype._dragStart = function (ev) {
ev || (ev = window.event);
if (this.dragging) {
return;
}
this.dragging = true;
var posX = ev.clientX + window.document.body.scrollLeft;
var posY = ev.clientY + window.document.body.scrollTop;
var st = this.element.style;
this.xOffs = posX - parseInt(st.left);
this.yOffs = posY - parseInt(st.top);
Ithelp.Utils.addEvent(window.document, "mousemove", Ithelp.Calendar.calDragIt);
Ithelp.Utils.addEvent(window.document, "mouseover", Ithelp.Calendar.calDragIt);
Ithelp.Utils.addEvent(window.document, "mouseup", Ithelp.Calendar.calDragEnd);
};
// BEGIN: DATE OBJECT PATCHES
/** \defgroup DateExtras Augmenting the Date object with some utility functions
* and variables.
*/
//@{
Date._MD = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; /**< Number of days in each month */
Date.SECOND = 1000; /**< One second has 1000 milliseconds. */
Date.MINUTE = 60 * Date.SECOND; /**< One minute has 60 seconds. */
Date.HOUR = 60 * Date.MINUTE; /**< One hour has 60 minutes. */
Date.DAY = 24 * Date.HOUR; /**< One day has 24 hours. */
Date.WEEK = 7 * Date.DAY; /**< One week has 7 days. */
/** Returns the number of days in the month. The \em month parameter is
* optional; if not passed, the current month of \b this Date object is
* assumed.
*
* @param month [int, optional] the month number, 0 for January.
*/
Date.prototype.getMonthDays = function(month) {
var year = this.getFullYear();
if (typeof month == "undefined") {
month = this.getMonth();
}
if (((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) && month == 1) {
return 29;
} else {
return Date._MD[month];
}
};
/** Returns the number of the current day in the current year. */
Date.prototype.getDayOfYear = function() {
var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
var then = new Date(this.getFullYear(), 0, 0, 0, 0, 0);
var time = now - then;
return Math.floor(time / Date.DAY);
};
/** Returns the number of the week in year, as defined in ISO 8601. */
Date.prototype.getWeekNumber = function() {
var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
var DoW = d.getDay();
d.setDate(d.getDate() - (DoW + 6) % 7 + 3); // Nearest Thu
var ms = d.valueOf(); // GMT
d.setMonth(0);
d.setDate(4); // Thu in Week 1
return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1;
};
/** Checks dates equality. Checks time too. */
Date.prototype.equalsTo = function(date) {
return ((this.getFullYear() == date.getFullYear()) &&
(this.getMonth() == date.getMonth()) &&
(this.getDate() == date.getDate()) &&
(this.getHours() == date.getHours()) &&
(this.getMinutes() == date.getMinutes()));
};
/** Checks dates equality. Ignores time. */
Date.prototype.dateEqualsTo = function(date) {
return ((this.getFullYear() == date.getFullYear()) &&
(this.getMonth() == date.getMonth()) &&
(this.getDate() == date.getDate()));
};
/** Set only the year, month, date parts (keep existing time) */
Date.prototype.setDateOnly = function(date) {
var tmp = new Date(date);
this.setDate(1);
this.setFullYear(tmp.getFullYear());
this.setMonth(tmp.getMonth());
this.setDate(tmp.getDate());
};
/** Prints the date in a string according to the given format.
*
* The format (\b str) may contain the following specialties:
*
* - %%a - Abbreviated weekday name
* - %%A - Full weekday name
* - %%b - Abbreviated month name
* - %%B - Full month name
* - %%C - Century number
* - %%d - The day of the month (00 .. 31)
* - %%e - The day of the month (0 .. 31)
* - %%H - Hour (00 .. 23)
* - %%I - Hour (01 .. 12)
* - %%j - The day of the year (000 .. 366)
* - %%k - Hour (0 .. 23)
* - %%l - Hour (1 .. 12)
* - %%m - Month (01 .. 12)
* - %%M - Minute (00 .. 59)
* - %%n - A newline character
* - %%p - "PM" or "AM"
* - %%P - "pm" or "am"
* - %%S - Second (00 .. 59)
* - %%s - Number of seconds since Epoch
* - %%t - A tab character
* - %%W - The week number (as per ISO 8601)
* - %%u - The day of week (1 .. 7, 1 = Monday)
* - %%w - The day of week (0 .. 6, 0 = Sunday)
* - %%y - Year without the century (00 .. 99)
* - %%Y - Year including the century (ex. 1979)
* - %%% - A literal %% character
*
* They are almost the same as for the POSIX strftime function.
*
* @param str [string] the format to print date in.
*/
Date.prototype.print = function (str) {
var m = this.getMonth();
var d = this.getDate();
var y = this.getFullYear();
var wn = this.getWeekNumber();
var w = this.getDay();
var s = {};
var hr = this.getHours();
var pm = (hr >= 12);
var ir = (pm) ? (hr - 12) : hr;
var dy = this.getDayOfYear();
if (ir == 0)
ir = 12;
var min = this.getMinutes();
var sec = this.getSeconds();
s["%a"] = Ithelp.Calendar.i18n(w, "sdn"); // abbreviated weekday name [FIXME: I18N]
s["%A"] = Ithelp.Calendar.i18n(w, "dn"); // full weekday name
s["%b"] = Ithelp.Calendar.i18n(m, "smn"); // abbreviated month name [FIXME: I18N]
s["%B"] = Ithelp.Calendar.i18n(m, "mn"); // full month name
// FIXME: %c : preferred date and time representation for the current locale
s["%C"] = 1 + Math.floor(y / 100); // the century number
s["%d"] = (d < 10) ? ("0" + d) : d; // the day of the month (range 01 to 31)
s["%e"] = d; // the day of the month (range 1 to 31)
// FIXME: %D : american date style: %m/%d/%y
// FIXME: %E, %F, %G, %g, %h (man strftime)
s["%H"] = (hr < 10) ? ("0" + hr) : hr; // hour, range 00 to 23 (24h format)
s["%I"] = (ir < 10) ? ("0" + ir) : ir; // hour, range 01 to 12 (12h format)
s["%j"] = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy; // day of the year (range 001 to 366)
s["%k"] = hr; // hour, range 0 to 23 (24h format)
s["%l"] = ir; // hour, range 1 to 12 (12h format)
s["%m"] = (m < 9) ? ("0" + (1+m)) : (1+m); // month, range 01 to 12
s["%M"] = (min < 10) ? ("0" + min) : min; // minute, range 00 to 59
s["%n"] = "\n"; // a newline character
s["%p"] = pm ? "PM" : "AM";
s["%P"] = pm ? "pm" : "am";
// FIXME: %r : the time in am/pm notation %I:%M:%S %p
// FIXME: %R : the time in 24-hour notation %H:%M
s["%s"] = Math.floor(this.getTime() / 1000);
s["%S"] = (sec < 10) ? ("0" + sec) : sec; // seconds, range 00 to 59
s["%t"] = "\t"; // a tab character
// FIXME: %T : the time in 24-hour notation (%H:%M:%S)
s["%U"] = s["%W"] = s["%V"] = (wn < 10) ? ("0" + wn) : wn;
s["%u"] = w + 1; // the day of the week (range 1 to 7, 1 = MON)
s["%w"] = w; // the day of the week (range 0 to 6, 0 = SUN)
// FIXME: %x : preferred date representation for the current locale without the time
// FIXME: %X : preferred time representation for the current locale without the date
s["%y"] = ('' + y).substr(2, 2); // year without the century (range 00 to 99)
s["%Y"] = y; // year with the century
s["%%"] = "%"; // a literal '%' character
var re = /%./g;
if (!Ithelp.is_ie5 && !Ithelp.is_khtml && !Ithelp.is_mac_ie)
return str.replace(re, function (par) { return s[par] || par; });
var a = str.match(re);
for (var i = 0; i < a.length; i++) {
var tmp = s[a[i]];
if (tmp) {
re = new RegExp(a[i], 'g');
str = str.replace(re, tmp);
}
}
return str;
};
/**
* Parses a date from a string in the specified format.
*
* @param str [string] the date as a string
* @param fmt [string] the format to try to parse the date in
*
* @return [Date] a date object containing the parsed date or \b null if for
* some reason the date couldn't be parsed.
*/
Date.parseDate = function (str, fmt) {
// Konqueror
if (!str)
return new Date();
var y = 0;
var m = -1;
var d = 0;
var a = str.split(/\W+/);
var b = fmt.match(/%./g);
var i = 0, j = 0;
var hr = 0;
var min = 0;
for (i = 0; i < a.length; ++i) {
if (!a[i])
continue;
switch (b[i]) {
case "%d":
case "%e":
d = parseInt(a[i], 10);
break;
case "%m":
m = parseInt(a[i], 10) - 1;
break;
case "%Y":
case "%y":
y = parseInt(a[i], 10);
(y < 100) && (y += (y > 29) ? 1900 : 2000);
break;
case "%b":
case "%B":
for (j = 0; j < 12; ++j)
if (Ithelp.Calendar.i18n(j, "mn").substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) {
m = j;
break;
}
break;
case "%H":
case "%I":
case "%k":
case "%l":
hr = parseInt(a[i], 10);
break;
case "%P":
case "%p":
if (/pm/i.test(a[i]) && hr < 12)
hr += 12;
break;
case "%M":
min = parseInt(a[i], 10);
break;
}
}
if (y != 0 && m != -1 && d != 0)
return new Date(y, m, d, hr, min, 0);
y = 0; m = -1; d = 0;
for (i = 0; i < a.length; ++i) {
if (a[i].search(/[a-zA-Z]+/) != -1) {
var t = -1;
for (j = 0; j < 12; ++j)
if (Ithelp.Calendar.i18n(j, "mn").substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) {
t = j;
break;
}
if (t != -1) {
if (m != -1)
d = m+1;
m = t;
}
} else if (parseInt(a[i], 10) <= 12 && m == -1) {
m = a[i]-1;
} else if (parseInt(a[i], 10) > 31 && y == 0) {
y = parseInt(a[i], 10);
(y < 100) && (y += (y > 29) ? 1900 : 2000);
} else if (d == 0) {
d = a[i];
}
}
if (y == 0) {
var today = new Date();
y = today.getFullYear();
}
if (m != -1 && d != 0)
return new Date(y, m, d, hr, min, 0);
return null;
};
Date.prototype.__msh_oldSetFullYear = Date.prototype.setFullYear; /**< save a reference to the original setFullYear function */
/**
* This function replaces the original Date.setFullYear() with a "safer"
* function which makes sure that the month or date aren't modified (unless in
* the exceptional case where the date is February 29 but the new year doesn't
* contain it).
*
* @param y [int] the new year to move this date to
*/
Date.prototype.setFullYear = function(y) {
var d = new Date(this);
d.__msh_oldSetFullYear(y);
if (d.getMonth() != this.getMonth())
this.setDate(28);
this.__msh_oldSetFullYear(y);
};
//@}
// END: DATE OBJECT PATCHES
window.calendar = null; /**< global object that remembers the calendar */
// initialize the preferences object;
// embed it in a try/catch so we don't have any surprises
try {
Ithelp.Calendar.loadPrefs();
} catch(e) {};