document.getElementsByClassName = function(clsName){
var retVal = new Array();
var elements = document.getElementsByTagName("*");
for(var i = 0;i < elements.length;i++){
if(elements[i].className.indexOf(" ") >= 0){
var classes = elements[i].className.split(" ");
for(var j = 0;j < classes.length;j++){
if(classes[j] == clsName)
retVal.push(elements[i]);
}
}
else if(elements[i].className == clsName)
retVal.push(elements[i]);
}
return retVal;
}
function WforceCalendar() {}
WforceCalendar.prototype = {
monthNamesArray: ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'],
headerText: '%MM%, %YYYY%',
daysFirstLetters: 'MTWTFSS',
divClassName: 'calendar_div',
headerClassName: 'calendar_header',
dayNamesClassName: 'calendar_day_names',
daysClassName: 'calendar_days',
daysBeforeTodayClassName: 'calendar_till_today',
daysAfterTodayClassName: 'calendar_after_today',
todayIdName: 'calendar_today',
nextMonthClassName: 'calendar_next_month',
previousMonthClassName: 'calendar_previous_month',
selectedColor: '#FFCC00',
normalDayColor: '#F8F8F8',
allowSelectPast: false,
getCalendar: function(y, m, edit_id, after_click, uniq_str, date_format, select_date) {
var monthDaysArray = [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var monthDate = new Date(y, m-1, 7);
monthDaysArray[1] = (((monthDate.getFullYear() % 100 != 0) && (monthDate.getFullYear() % 4 == 0)) || (monthDate.getFullYear() % 400 == 0))?29:28;
var todayDate = new Date();
var scanTodayDate = ((y == todayDate.getFullYear()) && (m == todayDate.getMonth() + 1))?todayDate.getDate():0;
var year_before = y - (m==1?1:0);
var month_before = (m==1?12:(m-1));
var year_after = y + (m==12?1:0);
var month_after = (m==12?1:(m+1));
var calendarBuffer = '';
if (document.getElementById(uniq_str + '_selected')) {
select_date = document.getElementById(uniq_str + '_selected').value;
}
var select_day = select_date.replace(/^[0-9]{4}\-[0-9]{1,2}\-/, "");
var select_month = select_date.replace(/[0-9]{4}\-/, "").replace(/\-[0-9]{1,2}/, "");
var select_year = select_date.replace(/\-[0-9]{1,2}\-[0-9]{1,2}/, "");
var tmpHeadText = this.headerText;
tmpHeadText = tmpHeadText.replace('%MM%', this.monthNamesArray[m-1]);
tmpHeadText = tmpHeadText.replace('%M%', m);
tmpHeadText = tmpHeadText.replace('%YYYY%', y);
calendarBuffer += '<input type="hidden" id="' + uniq_str + '_selected" value="' + select_date + '" /><div class="' + this.divClassName + '">';
calendarBuffer += '<table cols="7" cellpadding="0" cellspacing="0">';
calendarBuffer += '<tr align="center">';
calendarBuffer += '<td class="' + this.nextMonthClassName + '"><small><a href="javascript:void(0)" onclick="this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.innerHTML = WfCalendar.getCalendar(' + year_before + ', ' + month_before + ', \'' + edit_id + '\', \'' + after_click.replace(/\'/g, '\\\'') + '\', \'' + uniq_str + '\', \'' + date_format + '\', \'' + select_date + '\');">&lt;&lt;</a></small></td>';
calendarBuffer += '<td colspan="5" class="' + this.headerClassName + '">' + tmpHeadText + '</td>';
calendarBuffer += '<td class="' + this.previousMonthClassName + '"><small><a href="javascript:void(0)" onclick="this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.innerHTML = WfCalendar.getCalendar(' + year_after + ', ' + month_after + ', \'' + edit_id + '\', \'' + after_click.replace(/\'/g, '\\\'') + '\', \'' + uniq_str + '\', \'' + date_format + '\', \'' + select_date + '\');">&gt;&gt;</a></small></td>';
calendarBuffer += '</tr>';
calendarBuffer += '<tr align="center">';
for(i = 0; i < 8; i++) {
calendarBuffer += '<td class="' + this.dayNamesClassName + '">' + this.daysFirstLetters.substr(i, 1) + '</td>';
}
calendarBuffer += '</tr><tr align="center">';
for(i = 1; i <= 42; i++) {
var dayNow = ((i - monthDate.getDay() >= 0) && (i - monthDate.getDay() <= monthDaysArray[m - 1]))?(i - monthDate.getDay()):'';
var appendString = "";
if (scanTodayDate == 0) {
if (dayNow) {
if ((todayDate.getFullYear() * 100 + todayDate.getMonth() + 1) > (y * 100 + m)) { // month before today month
if (this.allowSelectPast && ((dayNow == select_day) && (m == select_month) && (y == select_year))) {// this day should be selected
appendString = ' style="background-color: ' + this.selectedColor + '" onclick="WfCalendar.clearAll(\'' + uniq_str + '\'); this.style.backgroundColor = \'' + this.selectedColor + '\';document.getElementById(\'' + edit_id + '\').value = WfCalendar.getFormattedDate(\'' + uniq_str + '\', \'' + date_format + '\', ' + y + ', ' + (m<10?'0'+m:m) + ', ' + (dayNow<10?"0"+dayNow:dayNow) + '); ' + after_click + '"';
} else if (this.allowSelectPast) {
appendString = ' onclick="WfCalendar.clearAll(\'' + uniq_str + '\'); this.style.backgroundColor = \'' + this.selectedColor + '\';document.getElementById(\'' + edit_id + '\').value = WfCalendar.getFormattedDate(\'' + uniq_str + '\', \'' + date_format + '\', ' + y + ', ' + (m<10?'0'+m:m) + ', ' + (dayNow<10?"0"+dayNow:dayNow) + '); ' + after_click + '"';
}
calendarBuffer += '<td' + appendString + ' class="' + this.daysBeforeTodayClassName + ' ' + this.daysClassName + ' ' + uniq_str + '">' + dayNow + '</td>';
} else {
if ((dayNow == select_day) && (m == select_month) && (y == select_year)) {
appendString = ' style="background-color: ' + this.selectedColor + '"';
}
calendarBuffer += '<td' + appendString + ' class="' + this.daysAfterTodayClassName + ' ' + this.daysClassName + ' ' + uniq_str + '" onclick="WfCalendar.clearAll(\'' + uniq_str + '\'); this.style.backgroundColor = \'' + this.selectedColor + '\';document.getElementById(\'' + edit_id + '\').value = WfCalendar.getFormattedDate(\'' + uniq_str + '\', \'' + date_format + '\', ' + y + ', ' + (m<10?'0'+m:m) + ', ' + (dayNow<10?"0"+dayNow:dayNow) + '); ' + after_click + '">' + dayNow + '</td>';
}
} else {
calendarBuffer += '<td class="' + this.daysClassName + '">&nbsp;</td>';
}
} else {
if (dayNow) {
if (dayNow > scanTodayDate) {
if ((dayNow == select_day) && (m == select_month) && (y == select_year)) {
appendString = ' style="background-color: ' + this.selectedColor + '"';
}
calendarBuffer += '<td' + appendString + ' class="' + this.daysAfterTodayClassName + ' ' + this.daysClassName + ' ' + uniq_str + '" onclick="WfCalendar.clearAll(\'' + uniq_str + '\'); this.style.backgroundColor = \'' + this.selectedColor + '\'; document.getElementById(\'' + edit_id + '\').value = WfCalendar.getFormattedDate(\'' + uniq_str + '\', \'' + date_format + '\', ' + y + ', ' + (m<10?'0'+m:m) + ', ' + (dayNow<10?"0"+dayNow:dayNow) + '); ' + after_click + '">' + dayNow + '</td>';
} else if (dayNow == scanTodayDate) {
if ((dayNow == select_day) && (m == select_month) && (y == select_year)) {
appendString = ' style="background-color: ' + this.selectedColor + '"';
}
calendarBuffer += '<td' + appendString + ' class="' + this.daysClassName + ' ' + uniq_str + '" id="' + this.todayIdName + '" onclick="WfCalendar.clearAll(\'' + uniq_str + '\'); this.style.backgroundColor = \'' + this.selectedColor + '\'; document.getElementById(\'' + edit_id + '\').value = WfCalendar.getFormattedDate(\'' + uniq_str + '\', \'' + date_format + '\', ' + y + ', ' + (m<10?'0'+m:m) + ', ' + (dayNow<10?"0"+dayNow:dayNow) + '); ' + after_click + '">' + dayNow + '</td>';
} else {
if (this.allowSelectPast && ((dayNow == select_day) && (m == select_month) && (y == select_year))) {
appendString = ' style="background-color: ' + this.selectedColor + '" onclick="WfCalendar.clearAll(\'' + uniq_str + '\'); this.style.backgroundColor = \'' + this.selectedColor + '\';document.getElementById(\'' + edit_id + '\').value = WfCalendar.getFormattedDate(\'' + uniq_str + '\', \'' + date_format + '\', ' + y + ', ' + (m<10?'0'+m:m) + ', ' + (dayNow<10?"0"+dayNow:dayNow) + '); ' + after_click + '"';
} else if (this.allowSelectPast) {
appendString = ' onclick="WfCalendar.clearAll(\'' + uniq_str + '\'); this.style.backgroundColor = \'' + this.selectedColor + '\';document.getElementById(\'' + edit_id + '\').value = WfCalendar.getFormattedDate(\'' + uniq_str + '\', \'' + date_format + '\', ' + y + ', ' + (m<10?'0'+m:m) + ', ' + (dayNow<10?"0"+dayNow:dayNow) + '); ' + after_click + '"';
}
calendarBuffer += '<td' + appendString + ' class="' + this.daysBeforeTodayClassName + ' ' + this.daysClassName + ' ' + uniq_str + '">' + dayNow + '</td>';
}
} else {
calendarBuffer += '<td class="' + this.daysClassName + '">&nbsp;</td>';
}
}
if ((i % 7 == 0) && (i < 36)) {
calendarBuffer += '</tr><tr align="center">';
}
}
return calendarBuffer += '</tr></table></div>';
},
clearAll: function(uniq_str) {
var all_objects = document.getElementsByClassName(uniq_str);
for (i = 0; i < all_objects.length; i++) {
all_objects[i].style.backgroundColor = this.normalDayColor;
}
},
getFormattedDate: function(uniq_str, date_format, y, m, d) {
date_format = date_format.replace('Y', y);
date_format = date_format.replace('m', (m<10?'0'+m:m));
date_format = date_format.replace('d', (d<10?"0"+d:d));
if (document.getElementById(uniq_str + '_selected')) {
document.getElementById(uniq_str + '_selected').value = y + '-' + m + '-' + d;
}
return date_format;
}
}
var WfCalendar = new WforceCalendar();