function padZeros(num, size) {
var str = num.toString();
var numZeros = size - str.length;
for (var i = 0; i < numZeros; i++)
str = "0" + str;
return str;
}
function showCal() {
var form = document.dateForm;
var year = oDate.getYear();
var month = oDate.getMonth();
var date = oDate.getDate();
var i, dval, oCell;
if (!curDate) curDate = date;
var thisMonth = new Date(oDate);
thisMonth.setDate(1);
var dayof1 = thisMonth.getDay();
var nextMonth = new Date(oDate);
nextMonth.setDate(1); nextMonth.setMonth(month + 1); nextMonth.setDate(0);
var numdays = nextMonth.getDate();
// ¼¿µé ÃʱâÈ
for (i=0; i<42; i++) dateCells[i].innerText = "";
if (curCell) curCell.className = "";
// ¼¿ °ª Àç¹èÄ¡
if (curDate > numdays) curDate = numdays;
for (i=0; i<numdays; i++) {
dval = i + 1; oCell = dateCells[i + dayof1];
oCell.innerText = dval;
if (dval == curDate) (curCell = oCell).className = "selected";
}
}
function nextMonth() { oDate.setMonth(oDate.getMonth() + 1); showCal(); }
function prevMonth() { oDate.setMonth(oDate.getMonth() - 1); showCal(); }
function cellOnClick() {
if (this.innerText=="") return;
curDate = parseInt(this.innerText);
curCell.className = "";
(curCell = this).className = "selected";
}
function updateYear(obj) {
var year = parseInt(obj.value);
if (isNaN(year)) return false;
var testDate = new Date();
testDate.setYear(year);
if (testDate.getYear() != year) return false;
oDate.setYear(year);
showCal();
return true;
}
function updateMonth(obj) {
var month = obj.selectedIndex;
oDate.setMonth(month);
showCal();
}
function getDateStr() {
var form = document.dateForm;
return form.year.value + "-" +
form.month[form.month.selectedIndex].value + "-" +
padZeros(curDate,2);
}
function init() {
var row, cell;
var tbody = calTable.childNodes[0];
calTable.appendChild(tbody);
for (var i=0; i<6; i++) {
row = document.createElement("TR");
tbody.appendChild(row);
for (var j=0; j<7; j++) {
cell = document.createElement("TD");
row.appendChild(cell);
dateCells[i*7+j] = cell;
cell.onclick = cellOnClick;
}
}
showCal();
}
</script>
<body onLoad="init()">