2 * Copyright (c) 2007 Kelvin Luck (http://www.kelvinluck.com/)
3 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
4 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
6 * $Id: jquery.datePicker.js 3739 2007-10-25 13:55:30Z kelvin.luck $
13 * Render a calendar table into any matched elements.
15 * @param Object s (optional) Customize your calendars.
16 * @option Number month The month to render (NOTE that months are zero based). Default is today's month.
17 * @option Number year The year to render. Default is today's year.
18 * @option Function renderCallback A reference to a function that is called as each cell is rendered and which can add classes and event listeners to the created nodes. Default is no callback.
19 * @option Number showHeader Whether or not to show the header row, possible values are: $.dpConst.SHOW_HEADER_NONE (no header), $.dpConst.SHOW_HEADER_SHORT (first letter of each day) and $.dpConst.SHOW_HEADER_LONG (full name of each day). Default is $.dpConst.SHOW_HEADER_SHORT.
20 * @option String hoverClass The class to attach to each cell when you hover over it (to allow you to use hover effects in IE6 which doesn't support the :hover pseudo-class on elements other than links). Default is dp-hover. Pass false if you don't want a hover class.
22 * @name renderCalendar
23 * @cat plugins/datePicker
24 * @author Kelvin Luck (http://www.kelvinluck.com/)
26 * @example $('#calendar-me').renderCalendar({month:0, year:2007});
27 * @desc Renders a calendar displaying January 2007 into the element with an id of calendar-me.
30 * var testCallback = function($td, thisDate, month, year)
32 * if ($td.is('.current-month') && thisDate.getDay() == 4) {
33 * var d = thisDate.getDate();
38 * alert('You clicked on ' + d + '/' + (Number(month)+1) + '/' + year);
40 * ).addClass('thursday');
41 * } else if (thisDate.getDay() == 5) {
42 * $td.html('Friday the ' + $td.html() + 'th');
45 * $('#calendar-me').renderCalendar({month:0, year:2007, renderCallback:testCallback});
47 * @desc Renders a calendar displaying January 2007 into the element with an id of calendar-me. Every Thursday in the current month has a class of "thursday" applied to it, is clickable and shows an alert when clicked. Every Friday on the calendar has the number inside replaced with text.
49 renderCalendar : function(s)
53 return document.createElement(a);
60 renderCallback : null,
61 showHeader : $.dpConst.SHOW_HEADER_SHORT,
63 hoverClass : 'dp-hover'
68 if (s.showHeader != $.dpConst.SHOW_HEADER_NONE) {
69 var headRow = $(dc('tr'));
70 for (var i=Date.firstDayOfWeek; i<Date.firstDayOfWeek+7; i++) {
72 var day = Date.dayNames[weekday];
74 jQuery(dc('th')).attr({'scope':'col', 'abbr':day, 'title':day, 'class':(weekday == 0 || weekday == 6 ? 'weekend' : 'weekday')}).html(s.showHeader == $.dpConst.SHOW_HEADER_SHORT ? day.substr(0, 1) : day)
79 var calendarTable = $(dc('table'))
83 'className':'jCalendar'
87 (s.showHeader != $.dpConst.SHOW_HEADER_NONE ?
94 var tbody = $(dc('tbody'));
96 var today = (new Date()).zeroTime();
98 var month = s.month == undefined ? today.getMonth() : s.month;
99 var year = s.year || today.getFullYear();
101 var currentDate = new Date(year, month, 1);
104 var firstDayOffset = Date.firstDayOfWeek - currentDate.getDay() + 1;
105 if (firstDayOffset > 1) firstDayOffset -= 7;
106 var weeksToDraw = Math.ceil(( (-1*firstDayOffset+1) + currentDate.getDaysInMonth() ) /7);
107 currentDate.addDays(firstDayOffset-1);
109 var doHover = function()
112 $(this).addClass(s.hoverClass);
115 var unHover = function()
118 $(this).removeClass(s.hoverClass);
123 while (w++<weeksToDraw) {
124 var r = jQuery(dc('tr'));
125 for (var i=0; i<7; i++) {
126 var thisMonth = currentDate.getMonth() == month;
128 .text(currentDate.getDate() + '')
129 .attr('className', (thisMonth ? 'current-month ' : 'other-month ') +
130 (currentDate.isWeekend() ? 'weekend ' : 'weekday ') +
131 (thisMonth && currentDate.getTime() == today.getTime() ? 'today ' : '')
133 .hover(doHover, unHover)
135 if (s.renderCallback) {
136 s.renderCallback(d, currentDate, month, year);
139 currentDate.addDays(1);
143 calendarTable.append(tbody);
148 $(this).empty().append(calendarTable);
153 * Create a datePicker associated with each of the matched elements.
155 * The matched element will receive a few custom events with the following signatures:
157 * dateSelected(event, date, $td, status)
158 * Triggered when a date is selected. event is a reference to the event, date is the Date selected, $td is a jquery object wrapped around the TD that was clicked on and status is whether the date was selected (true) or deselected (false)
160 * dpClosed(event, selected)
161 * Triggered when the date picker is closed. event is a reference to the event and selected is an Array containing Date objects.
163 * dpMonthChanged(event, displayedMonth, displayedYear)
164 * Triggered when the month of the popped up calendar is changed. event is a reference to the event, displayedMonth is the number of the month now displayed (zero based) and displayedYear is the year of the month.
166 * dpDisplayed(event, $datePickerDiv)
167 * Triggered when the date picker is created. $datePickerDiv is the div containing the date picker. Use this event to add custom content/ listeners to the popped up date picker.
169 * @param Object s (optional) Customize your date pickers.
170 * @option Number month The month to render when the date picker is opened (NOTE that months are zero based). Default is today's month.
171 * @option Number year The year to render when the date picker is opened. Default is today's year.
172 * @option String startDate The first date date can be selected.
173 * @option String endDate The last date that can be selected.
174 * @option Boolean inline Whether to create the datePicker as inline (e.g. always on the page) or as a model popup. Default is false (== modal popup)
175 * @option Boolean createButton Whether to create a .dp-choose-date anchor directly after the matched element which when clicked will trigger the showing of the date picker. Default is true.
176 * @option Boolean showYearNavigation Whether to display buttons which allow the user to navigate through the months a year at a time. Default is true.
177 * @option Boolean closeOnSelect Whether to close the date picker when a date is selected. Default is true.
178 * @option Boolean displayClose Whether to create a "Close" button within the date picker popup. Default is false.
179 * @option Boolean selectMultiple Whether a user should be able to select multiple dates with this date picker. Default is false.
180 * @option Boolean clickInput If the matched element is an input type="text" and this option is true then clicking on the input will cause the date picker to appear.
181 * @option Number verticalPosition The vertical alignment of the popped up date picker to the matched element. One of $.dpConst.POS_TOP and $.dpConst.POS_BOTTOM. Default is $.dpConst.POS_TOP.
182 * @option Number horizontalPosition The horizontal alignment of the popped up date picker to the matched element. One of $.dpConst.POS_LEFT and $.dpConst.POS_RIGHT.
183 * @option Number verticalOffset The number of pixels offset from the defined verticalPosition of this date picker that it should pop up in. Default in 0.
184 * @option Number horizontalOffset The number of pixels offset from the defined horizontalPosition of this date picker that it should pop up in. Default in 0.
185 * @option (Function|Array) renderCallback A reference to a function (or an array of separate functions) that is called as each cell is rendered and which can add classes and event listeners to the created nodes. Each callback function will receive four arguments; a jquery object wrapping the created TD, a Date object containing the date this TD represents, a number giving the currently rendered month and a number giving the currently rendered year. Default is no callback.
186 * @option String hoverClass The class to attach to each cell when you hover over it (to allow you to use hover effects in IE6 which doesn't support the :hover pseudo-class on elements other than links). Default is dp-hover. Pass false if you don't want a hover class.
189 * @cat plugins/datePicker
190 * @author Kelvin Luck (http://www.kelvinluck.com/)
192 * @example $('input.date-picker').datePicker();
193 * @desc Creates a date picker button next to all matched input elements. When the button is clicked on the value of the selected date will be placed in the corresponding input (formatted according to Date.format).
195 * @example demo/index.html
196 * @desc See the projects homepage for many more complex examples...
198 datePicker : function(s)
200 if (!$.event._dpCache) $.event._dpCache = [];
202 // initialise the date picker controller with the relevant settings...
207 startDate : undefined,
212 showYearNavigation : true,
213 closeOnSelect : true,
214 displayClose : false,
215 selectMultiple : false,
217 verticalPosition : $.dpConst.POS_TOP,
218 horizontalPosition : $.dpConst.POS_LEFT,
220 horizontalOffset : 0,
221 hoverClass : 'dp-hover'
230 var alreadyExists = true;
233 this._dpId = $.event.guid++;
234 $.event._dpCache[this._dpId] = new DatePicker(this);
235 alreadyExists = false;
239 s.createButton = false;
240 s.displayClose = false;
241 s.closeOnSelect = false;
245 var controller = $.event._dpCache[this._dpId];
249 if (!alreadyExists && s.createButton) {
251 controller.button = $('<a href="#" class="dp-choose-date" title="' + $.dpText.TEXT_CHOOSE_DATE + '">' + $.dpText.TEXT_CHOOSE_DATE + '</a>')
256 $this.dpDisplay(this);
261 $this.after(controller.button);
264 if (!alreadyExists && $this.is(':text')) {
268 function(e, selectedDate, $td)
270 this.value = selectedDate.asString();
276 var d = Date.fromString(this.value);
278 controller.setSelected(d, true, true);
291 var d = Date.fromString(this.value);
292 if (this.value != '' && d) {
293 controller.setSelected(d, true, true);
297 $this.addClass('dp-applied');
303 * Disables or enables this date picker
305 * @param Boolean s Whether to disable (true) or enable (false) this datePicker
307 * @name dpSetDisabled
308 * @cat plugins/datePicker
309 * @author Kelvin Luck (http://www.kelvinluck.com/)
311 * @example $('.date-picker').datePicker();
312 * $('.date-picker').dpSetDisabled(true);
313 * @desc Prevents this date picker from displaying and adds a class of dp-disabled to it (and it's associated button if it has one) for styling purposes. If the matched element is an input field then it will also set the disabled attribute to stop people directly editing the field.
315 dpSetDisabled : function(s)
317 return _w.call(this, 'setDisabled', s);
320 * Updates the first selectable date for any date pickers on any matched elements.
322 * @param String d A string representing the first selectable date (formatted according to Date.format).
324 * @name dpSetStartDate
325 * @cat plugins/datePicker
326 * @author Kelvin Luck (http://www.kelvinluck.com/)
328 * @example $('.date-picker').datePicker();
329 * $('.date-picker').dpSetStartDate('01/01/2000');
330 * @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the first selectable date for each of these to the first day of the millenium.
332 dpSetStartDate : function(d)
334 return _w.call(this, 'setStartDate', d);
337 * Updates the last selectable date for any date pickers on any matched elements.
339 * @param String d A string representing the last selectable date (formatted according to Date.format).
342 * @cat plugins/datePicker
343 * @author Kelvin Luck (http://www.kelvinluck.com/)
345 * @example $('.date-picker').datePicker();
346 * $('.date-picker').dpSetEndDate('01/01/2010');
347 * @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the last selectable date for each of these to the first Janurary 2010.
349 dpSetEndDate : function(d)
351 return _w.call(this, 'setEndDate', d);
354 * Gets a list of Dates currently selected by this datePicker. This will be an empty array if no dates are currently selected or NULL if there is no datePicker associated with the matched element.
357 * @name dpGetSelected
358 * @cat plugins/datePicker
359 * @author Kelvin Luck (http://www.kelvinluck.com/)
361 * @example $('.date-picker').datePicker();
362 * alert($('.date-picker').dpGetSelected());
363 * @desc Will alert an empty array (as nothing is selected yet)
365 dpGetSelected : function()
367 var c = _getController(this[0]);
369 return c.getSelected();
374 * Selects or deselects a date on any matched element's date pickers. Deselcting is only useful on date pickers where selectMultiple==true. Selecting will only work if the passed date is within the startDate and endDate boundries for a given date picker.
376 * @param String d A string representing the date you want to select (formatted according to Date.format).
377 * @param Boolean v Whether you want to select (true) or deselect (false) this date. Optional - default = true.
378 * @param Boolean m Whether you want the date picker to open up on the month of this date when it is next opened. Optional - default = true.
380 * @name dpSetSelected
381 * @cat plugins/datePicker
382 * @author Kelvin Luck (http://www.kelvinluck.com/)
384 * @example $('.date-picker').datePicker();
385 * $('.date-picker').dpSetSelected('01/01/2010');
386 * @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the selected date on these date pickers to the first Janurary 2010. When the date picker is next opened it will display Janurary 2010.
388 dpSetSelected : function(d, v, m)
390 if (v == undefined) v=true;
391 if (m == undefined) m=true;
392 return _w.call(this, 'setSelected', Date.fromString(d), v, m);
395 * Sets the month that will be displayed when the date picker is next opened. If the passed month is before startDate then the month containing startDate will be displayed instead. If the passed month is after endDate then the month containing the endDate will be displayed instead.
397 * @param Number m The month you want the date picker to display. Optional - defaults to the currently displayed month.
398 * @param Number y The year you want the date picker to display. Optional - defaults to the currently displayed year.
400 * @name dpSetDisplayedMonth
401 * @cat plugins/datePicker
402 * @author Kelvin Luck (http://www.kelvinluck.com/)
404 * @example $('.date-picker').datePicker();
405 * $('.date-picker').dpSetDisplayedMonth(10, 2008);
406 * @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the selected date on these date pickers to the first Janurary 2010. When the date picker is next opened it will display Janurary 2010.
408 dpSetDisplayedMonth : function(m, y)
410 return _w.call(this, 'setDisplayedMonth', Number(m), Number(y));
413 * Displays the date picker associated with the matched elements. Since only one date picker can be displayed at once then the date picker associated with the last matched element will be the one that is displayed.
415 * @param HTMLElement e An element that you want the date picker to pop up relative in position to. Optional - default behaviour is to pop up next to the element associated with this date picker.
418 * @cat plugins/datePicker
419 * @author Kelvin Luck (http://www.kelvinluck.com/)
421 * @example $('#date-picker').datePicker();
422 * $('#date-picker').dpDisplay();
423 * @desc Creates a date picker associated with the element with an id of date-picker and then causes it to pop up.
425 dpDisplay : function(e)
427 return _w.call(this, 'display', e);
430 * Sets a function or array of functions that is called when each TD of the date picker popup is rendered to the page
432 * @param (Function|Array) a A function or an array of functions that are called when each td is rendered. Each function will receive four arguments; a jquery object wrapping the created TD, a Date object containing the date this TD represents, a number giving the currently rendered month and a number giving the currently rendered year.
434 * @name dpSetRenderCallback
435 * @cat plugins/datePicker
436 * @author Kelvin Luck (http://www.kelvinluck.com/)
438 * @example $('#date-picker').datePicker();
439 * $('#date-picker').dpSetRenderCallback(function($td, thisDate, month, year)
441 * // do stuff as each td is rendered dependant on the date in the td and the displayed month and year
443 * @desc Creates a date picker associated with the element with an id of date-picker and then creates a function which is called as each td is rendered when this date picker is displayed.
445 dpSetRenderCallback : function(a)
447 return _w.call(this, 'setRenderCallback', a);
450 * Sets the position that the datePicker will pop up (relative to it's associated element)
452 * @param Number v The vertical alignment of the created date picker to it's associated element. Possible values are $.dpConst.POS_TOP and $.dpConst.POS_BOTTOM
453 * @param Number h The horizontal alignment of the created date picker to it's associated element. Possible values are $.dpConst.POS_LEFT and $.dpConst.POS_RIGHT
455 * @name dpSetPosition
456 * @cat plugins/datePicker
457 * @author Kelvin Luck (http://www.kelvinluck.com/)
459 * @example $('#date-picker').datePicker();
460 * $('#date-picker').dpSetPosition($.dpConst.POS_BOTTOM, $.dpConst.POS_RIGHT);
461 * @desc Creates a date picker associated with the element with an id of date-picker and makes it so that when this date picker pops up it will be bottom and right aligned to the #date-picker element.
463 dpSetPosition : function(v, h)
465 return _w.call(this, 'setPosition', v, h);
468 * Sets the offset that the popped up date picker will have from it's default position relative to it's associated element (as set by dpSetPosition)
470 * @param Number v The vertical offset of the created date picker.
471 * @param Number h The horizontal offset of the created date picker.
474 * @cat plugins/datePicker
475 * @author Kelvin Luck (http://www.kelvinluck.com/)
477 * @example $('#date-picker').datePicker();
478 * $('#date-picker').dpSetOffset(-20, 200);
479 * @desc Creates a date picker associated with the element with an id of date-picker and makes it so that when this date picker pops up it will be 20 pixels above and 200 pixels to the right of it's default position.
481 dpSetOffset : function(v, h)
483 return _w.call(this, 'setOffset', v, h);
486 * Closes the open date picker associated with this element.
490 * @cat plugins/datePicker
491 * @author Kelvin Luck (http://www.kelvinluck.com/)
493 * @example $('.date-pick')
499 * $(this).dpDisplay();
508 * @desc Creates a date picker and makes it appear when the relevant element is focused and disappear when it is blurred.
512 return _w.call(this, '_closeCalendar', false, this[0]);
514 // private function called on unload to clean up any expandos etc and prevent memory links...
515 _dpDestroy : function()
517 // TODO - implement this?
521 // private internal function to cut down on the amount of code needed where we forward
522 // dp* methods on the jQuery object on to the relevant DatePicker controllers...
523 var _w = function(f, a1, a2, a3)
528 var c = _getController(this);
536 function DatePicker(ele)
541 this.displayedMonth = null;
542 this.displayedYear = null;
543 this.startDate = null;
545 this.showYearNavigation = null;
546 this.closeOnSelect = null;
547 this.displayClose = null;
548 this.selectMultiple = null;
549 this.verticalPosition = null;
550 this.horizontalPosition = null;
551 this.verticalOffset = null;
552 this.horizontalOffset = null;
554 this.renderCallback = [];
555 this.selectedDates = {};
557 this.context = '#dp-popup';
560 DatePicker.prototype,
564 this.setStartDate(s.startDate);
565 this.setEndDate(s.endDate);
566 this.setDisplayedMonth(Number(s.month), Number(s.year));
567 this.setRenderCallback(s.renderCallback);
568 this.showYearNavigation = s.showYearNavigation;
569 this.closeOnSelect = s.closeOnSelect;
570 this.displayClose = s.displayClose;
571 this.selectMultiple = s.selectMultiple;
572 this.verticalPosition = s.verticalPosition;
573 this.horizontalPosition = s.horizontalPosition;
574 this.hoverClass = s.hoverClass;
575 this.setOffset(s.verticalOffset, s.horizontalOffset);
576 this.inline = s.inline;
578 this.context = this.ele;
582 setStartDate : function(d)
585 this.startDate = Date.fromString(d);
587 if (!this.startDate) {
588 this.startDate = (new Date()).zeroTime();
590 this.setDisplayedMonth(this.displayedMonth, this.displayedYear);
592 setEndDate : function(d)
595 this.endDate = Date.fromString(d);
598 this.endDate = (new Date('12/31/2999')); // using the JS Date.parse function which expects mm/dd/yyyy
600 if (this.endDate.getTime() < this.startDate.getTime()) {
601 this.endDate = this.startDate;
603 this.setDisplayedMonth(this.displayedMonth, this.displayedYear);
605 setPosition : function(v, h)
607 this.verticalPosition = v;
608 this.horizontalPosition = h;
610 setOffset : function(v, h)
612 this.verticalOffset = parseInt(v) || 0;
613 this.horizontalOffset = parseInt(h) || 0;
615 setDisabled : function(s)
618 $e[s ? 'addClass' : 'removeClass']('dp-disabled');
620 $but = $(this.button);
621 $but[s ? 'addClass' : 'removeClass']('dp-disabled');
622 $but.attr('title', s ? '' : $.dpText.TEXT_CHOOSE_DATE);
624 if ($e.is(':text')) {
625 $e.attr('disabled', s ? 'disabled' : '');
628 setDisplayedMonth : function(m, y)
630 if (this.startDate == undefined || this.endDate == undefined) {
633 var s = new Date(this.startDate.getTime());
635 var e = new Date(this.endDate.getTime());
639 if ((!m && !y) || (isNaN(m) && isNaN(y))) {
640 // no month or year passed - default to current month
641 t = new Date().zeroTime();
643 } else if (isNaN(m)) {
644 // just year passed in - presume we want the displayedMonth
645 t = new Date(y, this.displayedMonth, 1);
646 } else if (isNaN(y)) {
647 // just month passed in - presume we want the displayedYear
648 t = new Date(this.displayedYear, m, 1);
650 // year and month passed in - that's the date we want!
651 t = new Date(y, m, 1)
654 // check if the desired date is within the range of our defined startDate and endDate
655 if (t.getTime() < s.getTime()) {
657 } else if (t.getTime() > e.getTime()) {
660 this.displayedMonth = t.getMonth();
661 this.displayedYear = t.getFullYear();
663 setSelected : function(d, v, moveToMonth)
665 if (this.selectMultiple == false) {
666 this.selectedDates = {};
667 $('td.selected', this.context).removeClass('selected');
670 this.setDisplayedMonth(d.getMonth(), d.getFullYear());
672 this.selectedDates[d.toString()] = v;
674 isSelected : function(d)
676 return this.selectedDates[d.toString()];
678 getSelected : function()
681 for(s in this.selectedDates) {
682 if (this.selectedDates[s] == true) {
683 r.push(Date.parse(s));
688 display : function(eleAlignTo)
690 if ($(this.ele).is('.dp-disabled')) return;
692 eleAlignTo = eleAlignTo || this.ele;
694 var $ele = $(eleAlignTo);
695 var eleOffset = $ele.offset();
699 var attrsCalendarHolder;
703 $createIn = $(this.ele);
705 'id' : 'calendar-' + this.ele._dpId,
706 'className' : 'dp-popup dp-popup-inline'
711 $createIn = $('body');
714 'className' : 'dp-popup'
717 'top' : eleOffset.top + c.verticalOffset,
718 'left' : eleOffset.left + c.horizontalOffset
721 var _checkMouse = function(e)
724 var cal = $('#dp-popup')[0];
729 } else if (el == document) {
733 el = $(el).parent()[0];
737 this._checkMouse = _checkMouse;
739 this._closeCalendar(true);
750 $('<div class="dp-nav-prev"></div>')
752 $('<a class="dp-nav-prev-year" href="#" title="' + $.dpText.TEXT_PREV_YEAR + '"><<</a>')
757 return c._displayNewMonth.call(c, this, 0, -1);
760 $('<a class="dp-nav-prev-month" href="#" title="' + $.dpText.TEXT_PREV_MONTH + '"><</a>')
765 return c._displayNewMonth.call(c, this, -1, 0);
769 $('<div class="dp-nav-next"></div>')
771 $('<a class="dp-nav-next-year" href="#" title="' + $.dpText.TEXT_NEXT_YEAR + '">>></a>')
776 return c._displayNewMonth.call(c, this, 0, 1);
779 $('<a class="dp-nav-next-month" href="#" title="' + $.dpText.TEXT_NEXT_MONTH + '">></a>')
784 return c._displayNewMonth.call(c, this, 1, 0);
789 .attr('className', 'dp-calendar')
794 var $pop = this.inline ? $('.dp-popup', this.context) : $('#dp-popup');
796 if (this.showYearNavigation == false) {
797 $('.dp-nav-prev-year, .dp-nav-next-year', c.context).css('display', 'none');
799 if (this.displayClose) {
801 $('<a href="#" id="dp-close">' + $.dpText.TEXT_CLOSE + '</a>')
814 $(this.ele).trigger('dpDisplayed', $pop);
817 if (this.verticalPosition == $.dpConst.POS_BOTTOM) {
818 $pop.css('top', eleOffset.top + $ele.height() - $pop.height() + c.verticalOffset);
820 if (this.horizontalPosition == $.dpConst.POS_RIGHT) {
821 $pop.css('left', eleOffset.left + $ele.width() - $pop.width() + c.horizontalOffset);
823 $(document).bind('mousedown', this._checkMouse);
826 setRenderCallback : function(a)
828 if (a && typeof(a) == 'function') {
831 this.renderCallback = this.renderCallback.concat(a);
833 cellRender : function ($td, thisDate, month, year) {
834 var c = this.dpController;
835 var d = new Date(thisDate.getTime());
837 // add our click handlers to deal with it when the days are clicked...
844 if (!$this.is('.disabled')) {
845 c.setSelected(d, !$this.is('.selected') || !c.selectMultiple);
846 var s = c.isSelected(d);
847 $(c.ele).trigger('dateSelected', [d, $td, s]);
848 $(c.ele).trigger('change');
849 if (c.closeOnSelect) {
852 $this[s ? 'addClass' : 'removeClass']('selected');
858 if (c.isSelected(d)) {
859 $td.addClass('selected');
862 // call any extra renderCallbacks that were passed in
863 for (var i=0; i<c.renderCallback.length; i++) {
864 c.renderCallback[i].apply(this, arguments);
869 // ele is the clicked button - only proceed if it doesn't have the class disabled...
870 // m and y are -1, 0 or 1 depending which direction we want to go in...
871 _displayNewMonth : function(ele, m, y)
873 if (!$(ele).is('.disabled')) {
874 this.setDisplayedMonth(this.displayedMonth + m, this.displayedYear + y);
875 this._clearCalendar();
876 this._renderCalendar();
877 $(this.ele).trigger('dpMonthChanged', [this.displayedMonth, this.displayedYear]);
882 _renderCalendar : function()
885 $('h2', this.context).html(Date.monthNames[this.displayedMonth] + ' ' + this.displayedYear);
887 // render the calendar...
888 $('.dp-calendar', this.context).renderCalendar(
890 month : this.displayedMonth,
891 year : this.displayedYear,
892 renderCallback : this.cellRender,
894 hoverClass : this.hoverClass
898 // update the status of the control buttons and disable dates before startDate or after endDate...
899 // TODO: When should the year buttons be disabled? When you can't go forward a whole year from where you are or is that annoying?
900 if (this.displayedYear == this.startDate.getFullYear() && this.displayedMonth == this.startDate.getMonth()) {
901 $('.dp-nav-prev-year', this.context).addClass('disabled');
902 $('.dp-nav-prev-month', this.context).addClass('disabled');
903 $('.dp-calendar td.other-month', this.context).each(
907 if (Number($this.text()) > 20) {
908 $this.addClass('disabled');
912 var d = this.startDate.getDate();
913 $('.dp-calendar td.current-month', this.context).each(
917 if (Number($this.text()) < d) {
918 $this.addClass('disabled');
923 $('.dp-nav-prev-year', this.context).removeClass('disabled');
924 $('.dp-nav-prev-month', this.context).removeClass('disabled');
925 var d = this.startDate.getDate();
927 // check if the startDate is last month as we might need to add some disabled classes...
928 var sd = new Date(this.startDate.getTime());
930 if (this.displayedYear == sd.getFullYear() && this.displayedMonth == sd.getMonth()) {
931 $('dp-calendar td.other-month', this.context).each(
935 if (Number($this.text()) < d) {
936 $this.addClass('disabled');
943 if (this.displayedYear == this.endDate.getFullYear() && this.displayedMonth == this.endDate.getMonth()) {
944 $('.dp-nav-next-year', this.context).addClass('disabled');
945 $('.dp-nav-next-month', this.context).addClass('disabled');
946 $('.dp-calendar td.other-month', this.context).each(
950 if (Number($this.text()) < 14) {
951 $this.addClass('disabled');
955 var d = this.endDate.getDate();
956 $('.dp-calendar td.current-month', this.context).each(
960 if (Number($this.text()) > d) {
961 $this.addClass('disabled');
966 $('.dp-nav-next-year', this.context).removeClass('disabled');
967 $('.dp-nav-next-month', this.context).removeClass('disabled');
968 var d = this.endDate.getDate();
970 // check if the endDate is next month as we might need to add some disabled classes...
971 var ed = new Date(this.endDate.getTime());
973 if (this.displayedYear == ed.getFullYear() && this.displayedMonth == ed.getMonth()) {
974 $('.dp-calendar td.other-month', this.context).each(
978 if (Number($this.text()) > d) {
979 $this.addClass('disabled');
987 _closeCalendar : function(programatic, ele)
989 if (!ele || ele == this.ele)
991 $(document).unbind('mousedown', this._checkMouse);
992 this._clearCalendar();
993 $('#dp-popup a').unbind();
994 $('#dp-popup').empty().remove();
996 $(this.ele).trigger('dpClosed', [this.getSelected()]);
1000 // empties the current dp-calendar div and makes sure that all events are unbound
1001 // and expandos removed to avoid memory leaks...
1002 _clearCalendar : function()
1005 $('.dp-calendar td', this.context).unbind();
1006 $('.dp-calendar', this.context).empty();
1013 SHOW_HEADER_NONE : 0,
1014 SHOW_HEADER_SHORT : 1,
1015 SHOW_HEADER_LONG : 2,
1023 TEXT_PREV_YEAR : 'Previous year',
1024 TEXT_PREV_MONTH : 'Previous month',
1025 TEXT_NEXT_YEAR : 'Next year',
1026 TEXT_NEXT_MONTH : 'Next month',
1027 TEXT_CLOSE : 'Close',
1028 TEXT_CHOOSE_DATE : 'Choose date'
1031 $.dpVersion = '$Id: jquery.datePicker.js 3739 2007-10-25 13:55:30Z kelvin.luck $';
1033 function _getController(ele)
1035 if (ele._dpId) return $.event._dpCache[ele._dpId];
1039 // make it so that no error is thrown if bgIframe plugin isn't included (allows you to use conditional
1040 // comments to only include bgIframe where it is needed in IE without breaking this plugin).
1041 if ($.fn.bgIframe == undefined) {
1042 $.fn.bgIframe = function() {return this; };
1048 .bind('unload', function() {
1049 var els = $.event._dpCache || [];
1050 for (var i in els) {
1051 $(els[i].ele)._dpDestroy();
1060 * Date prototype extensions. Doesn't depend on any
1061 * other code. Doens't overwrite existing methods.
1063 * Adds dayNames, abbrDayNames, monthNames and abbrMonthNames static properties and isLeapYear,
1064 * isWeekend, isWeekDay, getDaysInMonth, getDayName, getMonthName, getDayOfYear, getWeekOfYear,
1065 * setDayOfYear, addYears, addMonths, addDays, addHours, addMinutes, addSeconds methods
1067 * Copyright (c) 2006 Jörn Zaefferer and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
1069 * Additional methods and properties added by Kelvin Luck: firstDayOfWeek, dateFormat, zeroTime, asString, fromString -
1070 * I've added my name to these methods so you know who to blame if they are broken!
1072 * Dual licensed under the MIT and GPL licenses:
1073 * http://www.opensource.org/licenses/mit-license.php
1074 * http://www.gnu.org/licenses/gpl.html
1079 * An Array of day names starting with Sunday.
1081 * @example dayNames[0]
1086 * @cat Plugins/Methods/Date
1088 Date.dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
1091 * An Array of abbreviated day names starting with Sun.
1093 * @example abbrDayNames[0]
1096 * @name abbrDayNames
1098 * @cat Plugins/Methods/Date
1100 Date.abbrDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
1103 * An Array of month names starting with Janurary.
1105 * @example monthNames[0]
1110 * @cat Plugins/Methods/Date
1112 Date.monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
1115 * An Array of abbreviated month names starting with Jan.
1117 * @example abbrMonthNames[0]
1122 * @cat Plugins/Methods/Date
1124 Date.abbrMonthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
1127 * The first day of the week for this locale.
1129 * @name firstDayOfWeek
1131 * @cat Plugins/Methods/Date
1132 * @author Kelvin Luck
1134 Date.firstDayOfWeek = 1;
1137 * The format that string dates should be represented as (e.g. 'dd/mm/yyyy' for UK, 'mm/dd/yyyy' for US, 'yyyy-mm-dd' for Unicode etc).
1141 * @cat Plugins/Methods/Date
1142 * @author Kelvin Luck
1144 Date.format = 'dd/mm/yyyy';
1145 //Date.format = 'mm/dd/yyyy';
1146 //Date.format = 'yyyy-mm-dd';
1147 //Date.format = 'dd mmm yy';
1150 * The first two numbers in the century to be used when decoding a two digit year. Since a two digit year is ambiguous (and date.setYear
1151 * only works with numbers < 99 and so doesn't allow you to set years after 2000) we need to use this to disambiguate the two digit year codes.
1155 * @cat Plugins/Methods/Date
1156 * @author Kelvin Luck
1158 Date.fullYearStart = '20';
1163 * Adds a given method under the given name
1164 * to the Date prototype if it doesn't
1169 function add(name, method) {
1170 if( !Date.prototype[name] ) {
1171 Date.prototype[name] = method;
1176 * Checks if the year is a leap year.
1178 * @example var dtm = new Date("01/12/2008");
1184 * @cat Plugins/Methods/Date
1186 add("isLeapYear", function() {
1187 var y = this.getFullYear();
1188 return (y%4==0 && y%100!=0) || y%400==0;
1192 * Checks if the day is a weekend day (Sat or Sun).
1194 * @example var dtm = new Date("01/12/2008");
1200 * @cat Plugins/Methods/Date
1202 add("isWeekend", function() {
1203 return this.getDay()==0 || this.getDay()==6;
1207 * Check if the day is a day of the week (Mon-Fri)
1209 * @example var dtm = new Date("01/12/2008");
1215 * @cat Plugins/Methods/Date
1217 add("isWeekDay", function() {
1218 return !this.isWeekend();
1222 * Gets the number of days in the month.
1224 * @example var dtm = new Date("01/12/2008");
1225 * dtm.getDaysInMonth();
1228 * @name getDaysInMonth
1230 * @cat Plugins/Methods/Date
1232 add("getDaysInMonth", function() {
1233 return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
1237 * Gets the name of the day.
1239 * @example var dtm = new Date("01/12/2008");
1241 * @result 'Saturday'
1243 * @example var dtm = new Date("01/12/2008");
1244 * dtm.getDayName(true);
1247 * @param abbreviated Boolean When set to true the name will be abbreviated.
1250 * @cat Plugins/Methods/Date
1252 add("getDayName", function(abbreviated) {
1253 return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
1257 * Gets the name of the month.
1259 * @example var dtm = new Date("01/12/2008");
1260 * dtm.getMonthName();
1261 * @result 'Janurary'
1263 * @example var dtm = new Date("01/12/2008");
1264 * dtm.getMonthName(true);
1267 * @param abbreviated Boolean When set to true the name will be abbreviated.
1270 * @cat Plugins/Methods/Date
1272 add("getMonthName", function(abbreviated) {
1273 return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
1277 * Get the number of the day of the year.
1279 * @example var dtm = new Date("01/12/2008");
1280 * dtm.getDayOfYear();
1283 * @name getDayOfYear
1285 * @cat Plugins/Methods/Date
1287 add("getDayOfYear", function() {
1288 var tmpdtm = new Date("1/1/" + this.getFullYear());
1289 return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
1293 * Get the number of the week of the year.
1295 * @example var dtm = new Date("01/12/2008");
1296 * dtm.getWeekOfYear();
1299 * @name getWeekOfYear
1301 * @cat Plugins/Methods/Date
1303 add("getWeekOfYear", function() {
1304 return Math.ceil(this.getDayOfYear() / 7);
1308 * Set the day of the year.
1310 * @example var dtm = new Date("01/12/2008");
1311 * dtm.setDayOfYear(1);
1313 * @result 'Tue Jan 01 2008 00:00:00'
1315 * @name setDayOfYear
1317 * @cat Plugins/Methods/Date
1319 add("setDayOfYear", function(day) {
1326 * Add a number of years to the date object.
1328 * @example var dtm = new Date("01/12/2008");
1331 * @result 'Mon Jan 12 2009 00:00:00'
1335 * @cat Plugins/Methods/Date
1337 add("addYears", function(num) {
1338 this.setFullYear(this.getFullYear() + num);
1343 * Add a number of months to the date object.
1345 * @example var dtm = new Date("01/12/2008");
1348 * @result 'Tue Feb 12 2008 00:00:00'
1352 * @cat Plugins/Methods/Date
1354 add("addMonths", function(num) {
1355 var tmpdtm = this.getDate();
1357 this.setMonth(this.getMonth() + num);
1359 if (tmpdtm > this.getDate())
1360 this.addDays(-this.getDate());
1366 * Add a number of days to the date object.
1368 * @example var dtm = new Date("01/12/2008");
1371 * @result 'Sun Jan 13 2008 00:00:00'
1375 * @cat Plugins/Methods/Date
1377 add("addDays", function(num) {
1378 this.setDate(this.getDate() + num);
1383 * Add a number of hours to the date object.
1385 * @example var dtm = new Date("01/12/2008");
1388 * @result 'Sun Jan 13 2008 00:00:00'
1392 * @cat Plugins/Methods/Date
1394 add("addHours", function(num) {
1395 this.setHours(this.getHours() + num);
1400 * Add a number of minutes to the date object.
1402 * @example var dtm = new Date("01/12/2008");
1403 * dtm.addMinutes(60);
1405 * @result 'Sat Jan 12 2008 01:00:00'
1409 * @cat Plugins/Methods/Date
1411 add("addMinutes", function(num) {
1412 this.setMinutes(this.getMinutes() + num);
1417 * Add a number of seconds to the date object.
1419 * @example var dtm = new Date("01/12/2008");
1420 * dtm.addSeconds(60);
1422 * @result 'Sat Jan 12 2008 00:01:00'
1426 * @cat Plugins/Methods/Date
1428 add("addSeconds", function(num) {
1429 this.setSeconds(this.getSeconds() + num);
1434 * Sets the time component of this Date to zero for cleaner, easier comparison of dates where time is not relevant.
1436 * @example var dtm = new Date();
1439 * @result 'Sat Jan 12 2008 00:01:00'
1443 * @cat Plugins/Methods/Date
1444 * @author Kelvin Luck
1446 add("zeroTime", function() {
1447 this.setMilliseconds(0);
1455 * Returns a string representation of the date object according to Date.format.
1456 * (Date.toString may be used in other places so I purposefully didn't overwrite it)
1458 * @example var dtm = new Date("01/12/2008");
1460 * @result '12/01/2008' // (where Date.format == 'dd/mm/yyyy'
1464 * @cat Plugins/Methods/Date
1465 * @author Kelvin Luck
1467 add("asString", function() {
1468 var r = Date.format;
1470 .split('yyyy').join(this.getFullYear())
1471 .split('yy').join((this.getFullYear() + '').substring(2))
1472 .split('mmm').join(this.getMonthName(true))
1473 .split('mm').join(_zeroPad(this.getMonth()+1))
1474 .split('dd').join(_zeroPad(this.getDate()));
1478 * Returns a new date object created from the passed String according to Date.format or false if the attempt to do this results in an invalid date object
1479 * (We can't simple use Date.parse as it's not aware of locale and I chose not to overwrite it incase it's functionality is being relied on elsewhere)
1481 * @example var dtm = Date.fromString("12/01/2008");
1483 * @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/yyyy'
1487 * @cat Plugins/Methods/Date
1488 * @author Kelvin Luck
1490 Date.fromString = function(s)
1492 var f = Date.format;
1493 var d = new Date('01/01/1977');
1494 var iY = f.indexOf('yyyy');
1496 d.setFullYear(Number(s.substr(iY, 4)));
1498 // TODO - this doesn't work very well - are there any rules for what is meant by a two digit year?
1499 d.setFullYear(Number(Date.fullYearStart + s.substr(f.indexOf('yy'), 2)));
1501 var iM = f.indexOf('mmm');
1503 var mStr = s.substr(iM, 3);
1504 for (var i=0; i<Date.abbrMonthNames.length; i++) {
1505 if (Date.abbrMonthNames[i] == mStr) break;
1509 d.setMonth(Number(s.substr(f.indexOf('mm'), 2)) - 1);
1511 d.setDate(Number(s.substr(f.indexOf('dd'), 2)));
1512 if (isNaN(d.getTime())) {
1519 var _zeroPad = function(num) {
1521 return s.substring(s.length-2)
1522 //return ('0'+num).substring(-2); // doesn't work on IE :(