bgneal@1: /* bgneal@1: calendar.js - Calendar functions by Adrian Holovaty bgneal@1: */ bgneal@1: bgneal@1: function removeChildren(a) { // "a" is reference to an object bgneal@1: while (a.hasChildNodes()) a.removeChild(a.lastChild); bgneal@1: } bgneal@1: bgneal@1: // quickElement(tagType, parentReference, textInChildNode, [, attribute, attributeValue ...]); bgneal@1: function quickElement() { bgneal@1: var obj = document.createElement(arguments[0]); bgneal@1: if (arguments[2] != '' && arguments[2] != null) { bgneal@1: var textNode = document.createTextNode(arguments[2]); bgneal@1: obj.appendChild(textNode); bgneal@1: } bgneal@1: var len = arguments.length; bgneal@1: for (var i = 3; i < len; i += 2) { bgneal@1: obj.setAttribute(arguments[i], arguments[i+1]); bgneal@1: } bgneal@1: arguments[1].appendChild(obj); bgneal@1: return obj; bgneal@1: } bgneal@1: bgneal@1: // CalendarNamespace -- Provides a collection of HTML calendar-related helper functions bgneal@1: var CalendarNamespace = { bgneal@1: monthsOfYear: gettext('January February March April May June July August September October November December').split(' '), bgneal@1: daysOfWeek: gettext('S M T W T F S').split(' '), bgneal@1: isLeapYear: function(year) { bgneal@1: return (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0)); bgneal@1: }, bgneal@1: getDaysInMonth: function(month,year) { bgneal@1: var days; bgneal@1: if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) { bgneal@1: days = 31; bgneal@1: } bgneal@1: else if (month==4 || month==6 || month==9 || month==11) { bgneal@1: days = 30; bgneal@1: } bgneal@1: else if (month==2 && CalendarNamespace.isLeapYear(year)) { bgneal@1: days = 29; bgneal@1: } bgneal@1: else { bgneal@1: days = 28; bgneal@1: } bgneal@1: return days; bgneal@1: }, bgneal@1: draw: function(month, year, div_id, callback) { // month = 1-12, year = 1-9999 bgneal@1: month = parseInt(month); bgneal@1: year = parseInt(year); bgneal@1: var calDiv = document.getElementById(div_id); bgneal@1: removeChildren(calDiv); bgneal@1: var calTable = document.createElement('table'); bgneal@1: quickElement('caption', calTable, CalendarNamespace.monthsOfYear[month-1] + ' ' + year); bgneal@1: var tableBody = quickElement('tbody', calTable); bgneal@1: bgneal@1: // Draw days-of-week header bgneal@1: var tableRow = quickElement('tr', tableBody); bgneal@1: for (var i = 0; i < 7; i++) { bgneal@1: quickElement('th', tableRow, CalendarNamespace.daysOfWeek[i]); bgneal@1: } bgneal@1: bgneal@1: var startingPos = new Date(year, month-1, 1).getDay(); bgneal@1: var days = CalendarNamespace.getDaysInMonth(month, year); bgneal@1: bgneal@1: // Draw blanks before first of month bgneal@1: tableRow = quickElement('tr', tableBody); bgneal@1: for (var i = 0; i < startingPos; i++) { bgneal@1: var _cell = quickElement('td', tableRow, ' '); bgneal@1: _cell.style.backgroundColor = '#f3f3f3'; bgneal@1: } bgneal@1: bgneal@1: // Draw days of month bgneal@1: var currentDay = 1; bgneal@1: for (var i = startingPos; currentDay <= days; i++) { bgneal@1: if (i%7 == 0 && currentDay != 1) { bgneal@1: tableRow = quickElement('tr', tableBody); bgneal@1: } bgneal@1: var cell = quickElement('td', tableRow, ''); bgneal@1: quickElement('a', cell, currentDay, 'href', 'javascript:void(' + callback + '('+year+','+month+','+currentDay+'));'); bgneal@1: currentDay++; bgneal@1: } bgneal@1: bgneal@1: // Draw blanks after end of month (optional, but makes for valid code) bgneal@1: while (tableRow.childNodes.length < 7) { bgneal@1: var _cell = quickElement('td', tableRow, ' '); bgneal@1: _cell.style.backgroundColor = '#f3f3f3'; bgneal@1: } bgneal@1: bgneal@1: calDiv.appendChild(calTable); bgneal@1: } bgneal@1: } bgneal@1: bgneal@1: // Calendar -- A calendar instance bgneal@1: function Calendar(div_id, callback) { bgneal@1: // div_id (string) is the ID of the element in which the calendar will bgneal@1: // be displayed bgneal@1: // callback (string) is the name of a JavaScript function that will be bgneal@1: // called with the parameters (year, month, day) when a day in the bgneal@1: // calendar is clicked bgneal@1: this.div_id = div_id; bgneal@1: this.callback = callback; bgneal@1: this.today = new Date(); bgneal@1: this.currentMonth = this.today.getMonth() + 1; bgneal@1: this.currentYear = this.today.getFullYear(); bgneal@1: } bgneal@1: Calendar.prototype = { bgneal@1: drawCurrent: function() { bgneal@1: CalendarNamespace.draw(this.currentMonth, this.currentYear, this.div_id, this.callback); bgneal@1: }, bgneal@1: drawDate: function(month, year) { bgneal@1: this.currentMonth = month; bgneal@1: this.currentYear = year; bgneal@1: this.drawCurrent(); bgneal@1: }, bgneal@1: drawPreviousMonth: function() { bgneal@1: if (this.currentMonth == 1) { bgneal@1: this.currentMonth = 12; bgneal@1: this.currentYear--; bgneal@1: } bgneal@1: else { bgneal@1: this.currentMonth--; bgneal@1: } bgneal@1: this.drawCurrent(); bgneal@1: }, bgneal@1: drawNextMonth: function() { bgneal@1: if (this.currentMonth == 12) { bgneal@1: this.currentMonth = 1; bgneal@1: this.currentYear++; bgneal@1: } bgneal@1: else { bgneal@1: this.currentMonth++; bgneal@1: } bgneal@1: this.drawCurrent(); bgneal@1: }, bgneal@1: drawPreviousYear: function() { bgneal@1: this.currentYear--; bgneal@1: this.drawCurrent(); bgneal@1: }, bgneal@1: drawNextYear: function() { bgneal@1: this.currentYear++; bgneal@1: this.drawCurrent(); bgneal@1: } bgneal@1: }