function RSRCalendar() { // this assumes "monthyear" is id of appropriate text element and // boxes have id's 0 through 42, and table has id RSRCalendar // yes, that means only one calendar per page //methods this.setMonth = setMonth; this.nextMonth = nextMonth; this.prevMonth = prevMonth; this.nextYear = nextYear; this.prevYear = prevYear; this.getDate = getDate; this.setDate = setDate; this.show = show; this.hide = hide; //this.showMonthYear = showMonthYear; // test purposes only //instance vars this.ref = document.getElementById("RSRCalendar"); //alert("this.ref is " + this.ref + " with style " + this.ref.style); this.y = 2005; this.m = 0; this.offset = 0; this.mLen = 0; var month = new Array("Jan.", "Feb.", "March", "April", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."); var msInDay = 1000 * 60 * 60 * 24; var def = "any"; var d = new Date(); //alert("In RSRCalendar with date " + d); setMonth(d.getFullYear(), d.getMonth()); // current month //this.offset = d.getDay(); function setMonth(year, month) { //alert("in setMonth with year " + year + " and month " + month); y = year; m = month; fillCal(); } function monthLen() { return (new Date(new Date(y, m + 1, 1) - msInDay)).getDate(); } function fillCal() { //alert("in filCal with instance data year " + y + " and month " + m); var i; offset = (new Date(y, m, 1)).getDay(); mLen = monthLen(); //alert("offset is " + offset + ", monthLen is " + mLen); document.getElementById("monthyear").value = month[m] + " " + y; for (i = 0; i < offset; i++) { document.getElementById(i).value = ""; } for (i = 0; i < mLen; i++) { document.getElementById(i + offset).value = i + 1; } for (i = mLen + offset; i < 37; i++) { document.getElementById(i).value = ""; } } function nextMonth() { if (m == 11) { m = 0; y++; } else m++; fillCal(); } function prevMonth() { if (m == 0) { m = 11; y--; } else m--; fillCal(); } function nextYear() { y++; fillCal(); } function prevYear() { y--; fillCal(); } function z2pad(n) { return ("0" + n).substr(-2); } function getDate(id) { var dt = id - offset + 1; return (dt > 0 && dt <= mLen) ? y + "-" + z2pad(m + 1) + "-" + z2pad(dt) : def; } function setDate(textDate) { if(textDate == def) { // if none set, use current var d = new Date(); setMonth(d.getFullYear(), d.getMonth()); // current month } else // assume format yyyy-mm-dd setMonth(parseInt(textDate.substr(0, 4)), parseInt(textDate.substr(5, 2)) - 1); } function show() { this.ref.style.visibility = "visible"; } function hide() { this.ref.style.visibility = "hidden"; } /* function showMonthYear() { alert("In showMonthYear: " + m + " " + y); } */ }