/**
* Arie Nugraha 2009
* this file need prototype. js
* library to works
*
* Modification of phpMyAdmin's calendar library
*/
var calendarPop;
var dateField;
var calContainer;
var dateType = 'datetime';
var day;
var month;
var year;
var hour;
var minute;
var second;
var month_names = new Array('January', 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December');
var day_names = new Array('Sun', 'Mon', 'Tue', 'Wed',
'Thu', 'Fri', 'Sat');
/**
* Open Calendar Window
*/
function openCalendar(strDatefieldID) {
if (calendarPop) {
calendarPop.show();
} else {
// inject calendar container to body
$(document.body).append('
'
+ '
Calendar
'
+ '
'
+ '
'
+ '
'
+ '
');
// positionize
calendarPop = $('#calendarPop');
}
// date input field
dateField = $('#'+strDatefieldID);
// get date input position
var dateFieldPos = dateField.offset();
calendarPop.css({'position': 'absolute', 'left': (dateFieldPos.left-2)+'px', 'top': (dateFieldPos.top-2)+'px'});
// reset all time value
day = 0; month = 0; year = 0;
// initialize calendar
initCalendar();
}
/**
* Formats number to two digits.
*
* @param int number to format.
* @param string type of number
*/
var formatNum2 = function(i, valtype) {
f = (i < 10 ? '0' : '') + i;
if (valtype && valtype != '') {
switch(valtype) {
case 'month':
f = (f > 12 ? 12 : f);
break;
case 'day':
f = (f > 31 ? 31 : f);
break;
case 'hour':
f = (f > 24 ? 24 : f);
break;
default:
case 'second':
case 'minute':
f = (f > 59 ? 59 : f);
break;
}
}
return f;
}
/**
* Formats number to two digits.
*
* @param int number to format.
* @param int default value
* @param string type of number
*/
var formatNum2d = function(i, default_v, valtype) {
i = parseInt(i, 10);
if (isNaN(i)) return default_v;
return formatNum2(i, valtype)
}
/**
* Formats number to four digits.
*
* @param int number to format.
*/
var formatNum4 = function(i) {
i = parseInt(i, 10)
return (i < 1000 ? i < 100 ? i < 10 ? '000' : '00' : '0' : '') + i;
}
/**
* open calendar window.
*
*/
var initCalendar = function() {
var dateFieldValue = dateField.val();
/* Called for first time */
if (!year && !month && !day) {
if (dateFieldValue) {
if (dateType == 'datetime' || dateType == 'date') {
if (dateType == 'datetime') {
var parts = dateFieldValue.split(' ');
dateFieldValue = parts[0];
if (parts[1]) {
time = parts[1].split(':');
hour = parseInt(time[0],10);
minute = parseInt(time[1],10);
second = parseInt(time[2],10);
}
}
date = dateFieldValue.split("-");
day = parseInt(date[2],10);
month = parseInt(date[1],10) - 1;
year = parseInt(date[0],10);
} else {
year = parseInt(dateFieldValue.substr(0,4),10);
month = parseInt(dateFieldValue.substr(4,2),10) - 1;
day = parseInt(dateFieldValue.substr(6,2),10);
hour = parseInt(dateFieldValue.substr(8,2),10);
minute = parseInt(dateFieldValue.substr(10,2),10);
second = parseInt(dateFieldValue.substr(12,2),10);
}
}
if (isNaN(year) || isNaN(month) || isNaN(day) || day == 0) {
dt = new Date();
year = dt.getFullYear();
month = dt.getMonth();
day = dt.getDate();
}
if (isNaN(hour) || isNaN(minute) || isNaN(second)) {
dt = new Date();
hour = dt.getHours();
minute = dt.getMinutes();
second = dt.getSeconds();
}
}
/* Moving in calendar */
if (month > 11) {
month = 0;
year++;
}
if (month < 0) {
month = 11;
year--;
}
// calendar container
calContainer = $('#calendarContainer');
var strTable = ""
//heading table
strTable += '';
strTable += '';
strTable += ' | ';
strTable += '';
strTable += ' |
---|
';
strTable += '';
for (i = 0; i < 7; i++) {
strTable += "" + day_names[i] + " | ";
}
strTable += "
";
var firstDay = new Date(year, month, 1).getDay();
var lastDay = new Date(year, month + 1, 0).getDate();
strTable += "";
dayInWeek = 0;
for (i = 0; i < firstDay; i++) {
strTable += " | ";
dayInWeek++;
}
for (i = 1; i <= lastDay; i++) {
if (dayInWeek == 7) {
strTable += "
";
dayInWeek = 0;
}
dispmonth = 1 + month;
if (dateType == 'datetime' || dateType == 'date') {
actVal = "" + formatNum4(year) + "-" + formatNum2(dispmonth, 'month') + "-" + formatNum2(i, 'day');
} else {
actVal = "" + formatNum4(year) + formatNum2(dispmonth, 'month') + formatNum2(i, 'day');
}
if (i == day) {
style = ' class="selected"';
current_date = actVal;
} else {
style = '';
}
strTable += "" + i + " | "
dayInWeek++;
}
for (i = dayInWeek; i < 7; i++) {
strTable += " | ";
}
strTable += "
";
calContainer.html(strTable);
if (dateType == 'datetime') {
// clock
var clockContainer = $('#clockContainer');
strTable = '';
init_hour = hour;
init_minute = minute;
init_second = second;
strTable += '';
clockContainer.html(strTable);
}
}
/**
* Returns date from calendar.
*
* @param string date text
*/
function returnDate(d) {
txt = d;
if (dateType != 'date') {
// need to get time
h = parseInt($('#hour').val(),10);
m = parseInt($('#minute').val(),10);
s = parseInt($('#second').val(),10);
if (dateType == 'datetime') {
txt += ' ' + formatNum2(h, 'hour') + ':' + formatNum2(m, 'minute') + ':' + formatNum2(s, 'second');
} else {
// timestamp
txt += formatNum2(h, 'hour') + formatNum2(m, 'minute') + formatNum2(s, 'second');
}
}
dateField.val(txt);
// close calendar window
calendarPop.hide();
}