/**
* Author: Chris Wetherell
* Calendar (object)
*
* A Calendar class that can create a calendar XML grid
* and transform it into HTML as needed.
*
* Typical usage:
* var cal = new Calendar();
* cal.makeCalendarXML(TODAYS_DATE);
* cal.transformCalendarXML();
*/
function Calendar()
{
// properties (with default values)
this.id="DefaultCalendarId";
this.XSLPath="defaultCalendar.xsl";
this.setTitles = this.setDefaultAbbreviations;
this.DataXSLPath = null;
// properties
this.xml;
this.html;
this.date;
this.destinationObj;
this.HTMLElement;
this.monthAbbreviations;
this.weekdayAbbreviations;
/**
* getCalendar()
*
* Get the calendar as an HTML element.
*
* args: none
* returns: an HTML element
*/
this.getCalendar = function()
{
var TODAYS_DATE = new Date();
// store the number of calendars created
// in a globally accessible variable
if (!window._calCount) window._calCount=0;
window._calCount++;
// set the month and weekday names
this.setTitles();
// create the calendar XML and store it (this.xml)
this.makeCalendarXML(TODAYS_DATE);
// transform the calendar XML into HTML and store it (this.HTMLElement)
this.transformCalendarXML();
// store this calendar in a globally accessible
// variable that is unique to the calendar type
eval("window._"+this.id+" = this");
return this.HTMLElement;
}
/**
* setDefaultAbbreviations()
*
* Set the names of the months and days of the week to
* abbrieviated versions.
*
* args: none
* returns: none
*/
this.setDefaultAbbreviations = function() {
this.monthAbbreviations = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
this.weekdayAbbreviations = new Array("S","M","T","W","T","F","S");
}
/**
* setLongNames()
*
* Set the names of the months and days of the week to
* longer names.
*
* args: none
* returns: none
*/
this.setLongNames = function() {
this.monthAbbreviations = new Array("January","February","March","April","May","June","July",
"August","September","October","November","December");
this.weekdayAbbreviations = new Array("Sun","Mon","Tues","Wed","Thur","Fri","Sat");
}
/**
* transformCalendarXML()
*
* Transform the Calendar XML to HTML and store it
* as a property.
*
* args: none
* returns: none
*/
this.transformCalendarXML = function() {
objXML = CreateXMLFromString(this.xml);
objXSL = GetXMLViaHTTP(this.XSLPath); //see xml.js
this.HTMLElement = TransformNodeToHTMLObject(objXML,objXSL);
}
/**
* makeAndReplaceCalendar()
*
* Make a new Calendar XML document, transform it to HTML, and replace
* the calendar object's HTML element.
*
* args: none
* returns: none
*/
this.makeAndReplaceCalendar = function() {
this.makeCalendarXML(this.date);
this.transformCalendarXML();
this.replaceCalendar();
}
/**
* replaceCalendar()
*
* Replace the calendar object's appended-to-page HTML element
* with the stored HTML element property.
*
* args: none
* returns: none
*/
this.replaceCalendar = function() {
thisObj = document.getElementById(this.id);
thisObj.parentNode.replaceChild(this.HTMLElement,thisObj);
}
/**
* changeDateToNextMonth()
*
* Set the date property one month ahead.
*
* args: none
* returns: none
*/
this.changeDateToNextMonth = function() {
thisMonth = this.date.getMonth();
thisYear = this.date.getFullYear();
nextMonth = (thisMonth==11 ? 0 : thisMonth+1);
nextMonthsYear = (nextMonth==0 ? thisYear+1 : thisYear);
this.date = new Date(nextMonthsYear,nextMonth,1);
}
/**
* changeDateToPreviousMonth()
*
* Set the date property one month previous.
*
* args: none
* returns: none
*/
this.changeDateToPreviousMonth = function() {
thisMonth = this.date.getMonth();
thisYear = this.date.getFullYear();
previousMonth = (thisMonth==0 ? 11 : thisMonth-1);
previousMonthsYear = (previousMonth==11 ? thisYear-1 : thisYear);
this.date = new Date(previousMonthsYear,previousMonth,1);
}
/**
* forwardOneMonth()
*
* Set the calendar one month ahead.
*
* args: none
* returns: none
*/
this.forwardOneMonth = function() {
this.changeDateToNextMonth();
this.makeAndReplaceCalendar();
try {
document.getElementById(this.id+"_thisdate").style.display="block";
} catch(e) {}
}
/**
* backOneMonth()
*
* Set the calendar one month previous.
*
* args: none
* returns: none
*/
this.backOneMonth = function() {
this.changeDateToPreviousMonth();
this.makeAndReplaceCalendar();
try {
document.getElementById(this.id+"_thisdate").style.display="block";
} catch(e) {}
}
/**
* toTodaysDate()
*
* Set the calendar to this month.
*
* args: none
* returns: none
*/
this.toTodaysDate = function() {
this.date = new Date();
this.makeAndReplaceCalendar();
}
this.makeCalendarXML = makeCalendarXML;
}
function makeCalendarXML(date) {
this.date = date;
var now = this.date;
var todaysdate = new Date();
var month = now.getMonth();
var year = now.getFullYear();
var daysinmonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
if ( ( (year%4 == 0)&&(year%100 != 0) ) || (year%400 == 0) ) {
daysinmonth[2] = 29;
}
var StartDay;
var currDate = new Date(year,month,1);
var prevDate = new Date(year,month-1,1);
var thisMonthStartsOnWeekDay = currDate.getDay();
if (thisMonthStartsOnWeekDay>0) {
StartDay = daysinmonth[prevDate.getMonth()] - (thisMonthStartsOnWeekDay - 1);
}
/*set header*/
var xml="";
xml+="\n";
for (var i=0;i\n";
}
xml+="\n";
/*set grid*/
c=0;
var grid = new Array();
for (var i=StartDay;i<=daysinmonth[prevDate.getMonth()];i++) {
grid[c]=new Array(i,"outOfMonth");
c++;
}
for (var i=1;i<=daysinmonth[now.getMonth()];i++) {
var dayType="";
if (
i==todaysdate.getDate()
&& month==todaysdate.getMonth()
&& year==todaysdate.getFullYear()
) dayType="today";
grid[c]=new Array(i,dayType);
c++;
}
var daysLeft = 42-grid.length;
for (var i=1;i<=daysLeft;i++) {
grid[c]=new Array(i,"outOfMonth");
c++;
}
/*transform grid*/
for (var i=0;i\n";
}
if (IsWeekEnd) {
if (type!="outOfMonth") type = "weekendEnd";
}
xml+="0) xml+=" type=\""+type+"\"";
xml+=" day=\""+date+"\" />\n";
if (IsWeekEnd) {
xml+="\n";
}
}
xml="\n"
+ xml +
"\n";
// add data as needed
if (this.DataXSLPath!=null) {
try {
var objXML = CreateXMLFromString(xml);
var objXSL = GetXMLViaHTTP(this.DataXSLPath); //see xml.js
var doc = TransformNodeToDocumentObject(objXML,objXSL);
xml = doc.xml;
} catch(e) {alert("Couldn't find Data XSL at "+this.DataXSLPath);}
}
this.xml=xml;
}