// how reliable is this test?
isIE = (document.all ? true : false);
isDOM = (document.getElementById ? true : false);
// Initialize arrays.
var dayLabels = new Array("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa");
var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec");
var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31);
var displayMonth = new Date().getMonth();
var displayYear = new Date().getFullYear();
var displayDivName;
var displayElement;
var chosenDates = new Array();
function dateChoice(name) {
this.name = name;
this.month = null;
this.day = null;
this.year = null;
return this;
}
function getDays(month, year) {
// Test for leap year when February is selected.
if (1 == month) {
return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
} else {
return daysInMonth[month];
}
}
function getToday() {
// Generate today's date.
this.now = new Date();
this.year = this.now.getFullYear();
this.month = this.now.getMonth();
this.day = this.now.getDate();
}
// Start with a calendar for today.
today = new getToday();
function newCalendar(eltName, attachedElement) {
return newCalendar(eltName, attachedElement, false);
}
function newCalendar(eltName,attachedElement, firstTime) {
if (attachedElement) {
if (displayDivName && displayDivName != eltName) hideElement(displayDivName);
displayElement = attachedElement;
}
displayDivName = eltName;
today = new getToday();
var parseYear = parseInt(displayYear + '');
var newCal;
if (firstTime) {
if (typeof chosenDates != "undefined" && chosenDates != null && chosenDates[eltName] != null) {
newCal = new Date(chosenDates[eltName].year, chosenDates[eltName].month, 1);
displayMonth = chosenDates[eltName].month;
displayYear = chosenDates[eltName].year;
} else {
displayMonth = new Date().getMonth();
displayYear = new Date().getFullYear();
newCal = new Date(displayYear, displayMonth, 1);
}
} else {
newCal = new Date(parseYear,displayMonth,1);
}
var day = -1;
var startDayOfWeek = newCal.getDay();
if ((today.year == newCal.getFullYear()) && (today.month == newCal.getMonth())) {
day = today.day;
}
var intDaysInMonth = getDays(newCal.getMonth(), newCal.getFullYear());
var daysGrid = makeDaysGrid(startDayOfWeek,day,intDaysInMonth,newCal,eltName)
if (isIE) {
var elt = document.all[eltName];
elt.innerHTML = daysGrid;
} else if (isDOM) {
var elt = document.getElementById(eltName);
elt.innerHTML = daysGrid;
} else {
var elt = document.layers[eltName].document;
elt.open();
elt.write(daysGrid);
elt.close();
}
}
function incMonth(delta,eltName) {
displayMonth += delta;
if (displayMonth >= 12) {
displayMonth = 0;
incYear(1,eltName);
} else if (displayMonth <= -1) {
displayMonth = 11;
incYear(-1,eltName);
} else {
newCalendar(eltName);
}
}
function bounceToToday(eltName) {
displayMonth = parseInt(new Date().getMonth());
displayYear = parseInt(new Date().getFullYear());
newCalendar(eltName);
}
function incYear(delta,eltName) {
displayYear = parseInt(displayYear + '') + delta;
newCalendar(eltName);
}
function makeDaysGrid(startDay,day,intDaysInMonth,newCal,eltName) {
var daysGrid;
var month = newCal.getMonth();
var year = newCal.getFullYear();
var isThisYear = (year == new Date().getFullYear());
var isThisMonth = (day > -1)
var yearInt = parseInt(year);
var thisYearInt = parseInt(new Date().getFullYear());
var thisMonthInt = parseInt(new Date().getMonth());
var choice = chosenDates[eltName];
// Do we show the year increment link?
var showNextYear = false;
if ((yearInt <= thisYearInt - 2) || (yearInt <= thisYearInt - 1 && month <= thisMonthInt)) {
showNextYear = true;
}
daysGrid = '<table width=170 border=0 cellspacing=0 cellpadding=0 bgcolor=#ffffff>';
daysGrid += '<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;font-size : 11px;">';
daysGrid += '<tr><td colspan=2 nowrap>';
// row "0" : 'close window' control
daysGrid += '<img src="http://web1.shutterfly.com/img_/datepicker/calendar_top.gif" width=153 height=21 style="vertical-align:bottom" alt=""/><a href="javascript:hideElement(\'' + eltName + '\')"><img src="http://web1.shutterfly.com/img_/datepicker/calendar_top_x.gif" style="vertical-align:bottom" height=21 width=17 alt="X"/></a>';
daysGrid += '</td></tr>';
// row "1" : year incr/decr controls
daysGrid += '<tr><td width=85 align=left style="vertical-align:middle; border-left-width: 1px; border-style: solid; border-bottom-width: 0px; border-top-width: 0px; border-right-width: 0px; border-color: #999999">';
daysGrid += '<a href="javascript:incYear(-1,\'' + eltName + '\')"><img src="http://web1.shutterfly.com/img_/datepicker/calendar_triangle_arrow_l.gif" style="vertical-align:middle" height=11 width=11 alt=""/></a><img src="http://web1.shutterfly.com/img_/misc/sp.gif" width=3/><a href="javascript:incYear(-1,\'' + eltName + '\')">' + (yearInt - 1) + '</a>';
daysGrid += '</td><td width=85 align=right style="vertical-align:middle; border-left-width: 0px; border-style: solid; border-bottom-width: 0px; border-top-width: 0px; border-right-width: 1px; border-color: #999999">';
if(!isThisYear) {
if (showNextYear) {
daysGrid += '<a href="javascript:incYear(1,\'' + eltName + '\')">' + (yearInt + 1) + '</a><img src="http://web1.shutterfly.com/img_/misc/sp.gif" width=3/><a href="javascript:incYear(1,\'' + eltName + '\')"><img src="http://web1.shutterfly.com/img_/datepicker/calendar_triangle_arrow_r.gif" style="vertical-align:middle" height=11 width=11 alt=""/></a>';
} else {
daysGrid += '<a href="javascript:bounceToToday(\'' + eltName + '\')">' + (yearInt + 1) + '</a><img src="http://web1.shutterfly.com/img_/misc/sp.gif" width=3/><a href="javascript:bounceToToday(\'' + eltName + '\')"><img src="http://web1.shutterfly.com/img_/datepicker/calendar_triangle_arrow_r.gif" style="vertical-align:middle" height=11 width=11 alt=""/></a>';
}
} else {
daysGrid += '&nbsp;';
}
daysGrid += '</td></tr>';
// row "2" : month display and and control
daysGrid += '<tr><td style="vertical-align:middle; border-left-width: 1px; border-style: solid; border-bottom-width: 0px; border-top-width: 0px; border-right-width: 1px; border-color: #999999" colspan=2 align=center>';
daysGrid += '<a href="javascript:incMonth(-1,\'' + eltName + '\')"><img src="http://web1.shutterfly.com/img_/datepicker/calendar_round_arrow_l.gif" height=11 width=11 alt=""/></a> ';
daysGrid += '<font color="black">';
if (isThisMonth) {
daysGrid += '<b>' + months[month] + ' ' + year + '</b>';
} else {
daysGrid += months[month] + ' ' + year;
}
daysGrid += '</font>';
if (!isThisMonth) {
daysGrid += ' <a href="javascript:incMonth(1,\'' + eltName + '\')"><img src="http://web1.shutterfly.com/img_/datepicker/calendar_round_arrow_r.gif" height=11 width=11 alt=""/></a>';
} else {
daysGrid += '&nbsp;&nbsp;';
}
daysGrid += '</td></tr>';
// row '3' and up : the days grid
daysGrid += '<tr><td colspan=2 align=center style="vertical-align:top">';
daysGrid += '<table width=170 border=0 cellpadding=0 cellspacing=0>';
daysGrid += '<tr>';
for (var intDayLbl = 0; intDayLbl < dayLabels.length; intDayLbl++) {
daysGrid += '<td class="';
if (intDayLbl != dayLabels.length - 1) {
daysGrid += 'calDays';
} else {
daysGrid += 'calDaysRt';
}
daysGrid += '" width=23>' + dayLabels[intDayLbl] + '</td>';
}
daysGrid += '</tr>';
var dayOfMonthOfFirstSunday = (7 - startDay + 1);
var numWeeks = 5;
if ((intDaysInMonth == 30 && dayOfMonthOfFirstSunday == 2) ||
(intDaysInMonth == 31 && dayOfMonthOfFirstSunday == 2) ||
(intDaysInMonth == 31 && dayOfMonthOfFirstSunday == 3)) {
numWeeks = 6;
}
var dayOfMonth;
var pickedDay;
for (var intWeek = 0; intWeek < numWeeks; intWeek++) {
daysGrid += '<tr>';
for (var intDay = 0; intDay < 7; intDay++) {
dayOfMonth = (intWeek * 7) + intDay + dayOfMonthOfFirstSunday - 7;
if (dayOfMonth <= 0) {
daysGrid += "<td class='calCell' width=23><img src='http://web1.shutterfly.com/img_/misc/sp.gif' width=1/></td>";
} else if (dayOfMonth <= intDaysInMonth) {
var cellStyle = "";
if (day > 0 && day == dayOfMonth) {
cellStyle = "color:#000000; font-weight: bold";
}
daysGrid += '<td width=23 onclick="setDay(\'' + dayOfMonth + '\', \'' + eltName + '\')"class="';
if(typeof choice != "undefined" && choice != null && choice.day == dayOfMonth && choice.month == month && choice.year == year) {
//alert("yo");
if (intWeek != numWeeks - 1) {
if (intDay != 6) {
daysGrid += 'calCellSel';
} else {
daysGrid += 'calRtCellSel';
}
} else {
if (intDay != 6) {
daysGrid += 'calBtmCellSel';
} else {
daysGrid += 'calCnrCellSel';
}
}
daysGrid += '" >';
//alert("yo");
//daysGrid += '<span width=23 style="background-color:orange">';
var domStr = dayOfMonth + "";
if (domStr.length < 2) {
daysGrid += '&nbsp;';
}
daysGrid += '<a href="javascript:setDay(' + dayOfMonth;
daysGrid += ',\'' + eltName + '\')" style="' + cellStyle + '">' + dayOfMonth + '</a>';
if (domStr.length < 2) {
daysGrid += '&nbsp;';
}
daysGrid += '</td>';
} else {
if (intWeek != numWeeks - 1) {
if (intDay != 6) {
daysGrid += 'calCell';
} else {
daysGrid += 'calRtCell';
}
} else {
if (intDay != 6) {
daysGrid += 'calBtmCell';
} else {
daysGrid += 'calCnrCell';
}
}
daysGrid += '" >';
daysGrid += '<a href="javascript:setDay(' + dayOfMonth;
daysGrid += ',\'' + eltName + '\')" style="' + cellStyle + '">' + dayOfMonth + '</a></td>';
}
} else {
if (intDay != 6) {
daysGrid += "<td class='calBtmCell' width=23><img src='http://web1.shutterfly.com/img_/misc/sp.gif' width=1/></td>";
} else {
daysGrid += "<td class='calCnrCell' width=23><img src='http://web1.shutterfly.com/img_/misc/sp.gif' width=1/></td>";
}
}
}
daysGrid += "</tr>";
}
//daysGrid += '<tr><td colspan=7 style="vertical-align:top;cellpadding:0"><img src="http://web1.shutterfly.com/img_/datepicker/calendar_bottom.gif" height=5 width=172 style="vertical-align:top"/></td></tr>';
daysGrid += '</table><img src="http://web1.shutterfly.com/img_/misc/sp.gif" width=3/>';
return daysGrid + "</td></tr></span></table>";
}
function setDay(day,eltName) {
var choice = new dateChoice(eltName);
choice.day = day;
choice.month = displayMonth;
choice.year = displayYear;
chosenDates[eltName] = choice;
displayElement.value = (displayMonth + 1) + "/" + day + "/" + displayYear;
hideElement(eltName);
}
function resetCal(names) {
if (typeof names != "undefined" && names != null && names.length > 0) {
var i;
for (i = 0; i < names.length; i++) {
//            alert("hiding cal: " + names[i]);
bounceToToday(names[i]);
hideElement(names[i]);
}
if (typeof chosenDates != "undefined" && chosenDates != null) {
for (i = 0; i < names.length; i++) {
//                alert("destroying cal: " + names[i]);
chosenDates[names[i]] = null;
}
}
}
}
function hideCalendars(names) {
if (typeof names != "undefined" && names != null && names.length > 0) {
var i;
for (i = 0; i < names.length; i++) {
hideElement(names[i]);
}
}
}