calendar.js
上传用户:zhouquan
上传日期:2021-05-12
资源大小:16497k
文件大小:49k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

HTML/CSS

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