var CALENDAR_MonthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var CALENDAR_MonthShortNames = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."];
var CALENDAR_DayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var CALENDAR_DayShortNames = ["S", "M", "T", "W", "Th", "F", "S"];
// var CALENDAR_DayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
// var CALENDAR_DayShortNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
// var CALENDAR_DayShortNames = ["S", "M", "T", "W", "T", "F", "S"];
// This will set the current date as the date on the user's clock. Change to server date here if necessary.
// Remember: Month is 0-indexed in javascript.
//var DAYS_OUT = 3;
var temp = new Date();
if (!CALENDAR_CurrentDate) {
var CALENDAR_CurrentDate = new Date(temp.getFullYear(), temp.getMonth(), temp.getDate());
}
// Main constructor function. Takes the object variable name as a parameter (for self-refferential behavior with links)
function Calendar(selfName) {
this.name = selfName; // Self-refferential property. Necessary!!!
this.selectedDate = CALENDAR_CurrentDate;
this.currentMonth = this.selectedDate.getMonth();
this.currentYear = this.selectedDate.getFullYear();
this.layer = false; // Layer to regenerate into
// Application-specific behaviors
this.autoRender = true; // Should we regenerate if the user selects something?
this.onDateSelect = false; // If not false, must contain a valid js command to execute when date is selected
this.onMonthSelect = false; // If not false, must contain a valid js command to execute when month is selected
this.backgroundcolor = false;
this.weekdayHeaderBackgroundColor = false;
this.dayBackgroundColor = false;
this.currentDayBackgroundColor = false;
this.font_size = false;
this.lowerLimit = false; // If not false, must contain a valid date (lower bound)
this.upperLimit = false; // If not false, must contain a valid date (upper bound)
this.outOfBounds = CalendarOutOfBounds;
this.getMonthName = CalendarGetMonthName;
this.getMonthShortName = CalendarGetMonthShortName;
this.getSelectedDate = CalendarGetSelectedDate;
this.getDaysInMonth = CalendarGetDaysInMonth;
this.select = CalendarSelect;
this.selectMonth = CalendarSelectMonth;
this.renderDays = CalendarRenderDays;
this.renderDaySelectLink = CalendarRenderDaySelect;
this.renderPrevMonthLink = CalendarRenderPrevMonth;
this.renderNextMonthLink = CalendarRenderNextMonth;
this.renderHeader = CalendarRenderHeader;
this.render = CalendarRender;
this.renderToLayer = CalendarRenderToLayer;
this.refresh = CalendarRefresh;
}
//----------------------------- Methods
function CalendarOutOfBounds(d) {
var out = false;
var dv = d.valueOf();
if (this.lowerLimit) {
out = (this.lowerLimit.valueOf() > d);
}
if (this.upperLimit && !out) {
out = (this.upperLimit.valueOf() < d);
}
return out;
}
function CalendarGetMonthName() {
return CALENDAR_MonthNames[this.currentMonth];
}
function CalendarGetMonthShortName() {
return CALENDAR_MonthShortNames[this.currentMonth];
}
function CalendarGetSelectedDate() {
return this.selectedDate;
}
function CalendarGetDaysInMonth(year, month) {
var a = new Date(year, month, 1);
if (month == 11) {
var b = new Date(year+1, 0, 1);
} else {
var b = new Date(year, month+1, 1);
}
var millsDiff = b-a;
return Math.round(millsDiff / 86400000); // 86,400,000 milliseconds in a day
}
//------------- Actions
function CalendarSelect(y, m, d) {
this.selectedDate = new Date(y, m, d);
this.refresh();
if (this.onDateSelect) {
eval(this.onDateSelect);
}
}
function CalendarSelectMonth(y, m) {
this.currentMonth = m;
this.currentYear = y;
this.refresh();
if (this.onMonthSelect) {
eval(this.onMonthSelect);
}
}
//------------- HTML Rendering routines
function CalendarRenderDays() {
var output = "";
// Figure out how many days are being shown
var totalDays = this.getDaysInMonth(this.currentYear, this.currentMonth);
// Identify what day-of-week to start on
var startDay = (new Date(this.currentYear, this.currentMonth, 1)).getDay();
// Add day-name header row
output += "
";
for (var i in CALENDAR_DayShortNames) {
output += "| " + CALENDAR_DayShortNames[i] + " | ";
}
output += " ";
var showDays = false;
var currentDay = 1;
for (var w = 0; w < 6; w++) {
output += "";
for (var d = 0; d < 7; d++) {
showDays = (showDays || (!showDays && d == startDay));
if (!showDays || currentDay > totalDays) {
// Show disabled box
output += "| | ";
} else {
var currentDate = new Date(this.currentYear, this.currentMonth, currentDay);
//alert(this.selectedDate);
if (this.outOfBounds(currentDate)) {
// Show disabled box
output += ""+currentDay+" | ";
} else if (this.selectedDate && currentDate.valueOf() == this.selectedDate.valueOf()) {
// Show current date box
output += "";
output += this.renderDaySelectLink(currentDate);
output += " | ";
} else {
// Show selectable date box
output += "";
output += this.renderDaySelectLink(currentDate);
output += " | ";
}
currentDay++;
}
}
output += " \n";
}
output += "
|
";
return output;
}
function CalendarRenderDaySelect(date) {
var output = ""+date.getDate()+"";
} else {
output += "' style='text-decoration:none;'>"+date.getDate()+"";
}
return output;
}
function CalendarRenderHeader() {
var output = "";
output += "";
output += "";
output += "| ";
output += this.renderPrevMonthLink();
output += " | ";
output += "";
output += this.getMonthName(this.currentMonth) + " " + this.currentYear;
output += " | ";
output += "";
output += this.renderNextMonthLink();
output += " |
";
return output;
}
function CalendarRenderPrevMonth() {
var output = "";
if (this.currentMonth == 0) {
var m = 11;
var y = this.currentYear - 1;
} else {
var m = this.currentMonth - 1;
var y = this.currentYear;
}
if (!this.outOfBounds(new Date(y, m, this.getDaysInMonth(y,m)))) {
output += "<";
}else{
output += "
";
}
return output;
}
function CalendarRenderNextMonth() {
var output = "";
if (this.currentMonth == 11) {
var m = 0;
var y = this.currentYear + 1;
} else {
var m = this.currentMonth + 1;
var y = this.currentYear;
}
if (!this.outOfBounds(new Date(y, m, 1))) {
output += "";
}else{
output += "
";
}
return output;
}
function CalendarRender() {
var output = "";
output += this.renderHeader();
output += this.renderDays();
return output;
}
function CalendarRenderToLayer(layerName, outerLayer) {
outerLayerName = new String(outerLayer);
if(outerLayerName == 'undefined')
{
outerLayerName = '';
}
this.layer = new Layer(layerName, outerLayerName);
this.layer.setHtml(this.render());
}
function CalendarRefresh() {
if (this.autoRender && this.layer) {
this.layer.setHtml(this.render());
}
}