2 calendar.js - Calendar functions by Adrian Holovaty
5 function removeChildren(a) { // "a" is reference to an object
6 while (a.hasChildNodes()) a.removeChild(a.lastChild);
9 // quickElement(tagType, parentReference, textInChildNode, [, attribute, attributeValue ...]);
10 function quickElement() {
11 var obj = document.createElement(arguments[0]);
12 if (arguments[2] != '' && arguments[2] != null) {
13 var textNode = document.createTextNode(arguments[2]);
14 obj.appendChild(textNode);
16 var len = arguments.length;
17 for (var i = 3; i < len; i += 2) {
18 obj.setAttribute(arguments[i], arguments[i+1]);
20 arguments[1].appendChild(obj);
24 // CalendarNamespace -- Provides a collection of HTML calendar-related helper functions
25 var CalendarNamespace = {
26 monthsOfYear: gettext('January February March April May June July August September October November December').split(' '),
27 daysOfWeek: gettext('S M T W T F S').split(' '),
28 isLeapYear: function(year) {
29 return (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0));
31 getDaysInMonth: function(month,year) {
33 if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) {
36 else if (month==4 || month==6 || month==9 || month==11) {
39 else if (month==2 && CalendarNamespace.isLeapYear(year)) {
47 draw: function(month, year, div_id, callback) { // month = 1-12, year = 1-9999
48 month = parseInt(month);
49 year = parseInt(year);
50 var calDiv = document.getElementById(div_id);
51 removeChildren(calDiv);
52 var calTable = document.createElement('table');
53 quickElement('caption', calTable, CalendarNamespace.monthsOfYear[month-1] + ' ' + year);
54 var tableBody = quickElement('tbody', calTable);
56 // Draw days-of-week header
57 var tableRow = quickElement('tr', tableBody);
58 for (var i = 0; i < 7; i++) {
59 quickElement('th', tableRow, CalendarNamespace.daysOfWeek[i]);
62 var startingPos = new Date(year, month-1, 1).getDay();
63 var days = CalendarNamespace.getDaysInMonth(month, year);
65 // Draw blanks before first of month
66 tableRow = quickElement('tr', tableBody);
67 for (var i = 0; i < startingPos; i++) {
68 var _cell = quickElement('td', tableRow, ' ');
69 _cell.style.backgroundColor = '#f3f3f3';
74 for (var i = startingPos; currentDay <= days; i++) {
75 if (i%7 == 0 && currentDay != 1) {
76 tableRow = quickElement('tr', tableBody);
78 var cell = quickElement('td', tableRow, '');
79 quickElement('a', cell, currentDay, 'href', 'javascript:void(' + callback + '('+year+','+month+','+currentDay+'));');
83 // Draw blanks after end of month (optional, but makes for valid code)
84 while (tableRow.childNodes.length < 7) {
85 var _cell = quickElement('td', tableRow, ' ');
86 _cell.style.backgroundColor = '#f3f3f3';
89 calDiv.appendChild(calTable);
93 // Calendar -- A calendar instance
94 function Calendar(div_id, callback) {
95 // div_id (string) is the ID of the element in which the calendar will
97 // callback (string) is the name of a JavaScript function that will be
98 // called with the parameters (year, month, day) when a day in the
99 // calendar is clicked
100 this.div_id = div_id;
101 this.callback = callback;
102 this.today = new Date();
103 this.currentMonth = this.today.getMonth() + 1;
104 this.currentYear = this.today.getFullYear();
106 Calendar.prototype = {
107 drawCurrent: function() {
108 CalendarNamespace.draw(this.currentMonth, this.currentYear, this.div_id, this.callback);
110 drawDate: function(month, year) {
111 this.currentMonth = month;
112 this.currentYear = year;
115 drawPreviousMonth: function() {
116 if (this.currentMonth == 1) {
117 this.currentMonth = 12;
125 drawNextMonth: function() {
126 if (this.currentMonth == 12) {
127 this.currentMonth = 1;
135 drawPreviousYear: function() {
139 drawNextYear: function() {