/** * calendar * @param beginyear 1990 * @param endyear 2010 * @param language 0(zh_cn)|1(en_us)|2(en_en)|3(zh_tw) * @param patterndelimiter "-" * @param date2stringpattern "yyyy-mm-dd" * @param string2datepattern "ymd" * @version 1.0 build 2006-04-01 * @version 1.1 build 2006-12-17 * @author kimsoft (jinqinghua [at] gmail.com) * note! you can use it free, but keep the copyright please * important:you must include this script file inner html body elment */ function calendar(beginyear, endyear, language, patterndelimiter, date2stringpattern, string2datepattern) { this.beginyear = beginyear || 2010; this.endyear = endyear || 2030; this.language = language || 0; this.patterndelimiter = patterndelimiter || "-"; this.date2stringpattern = date2stringpattern || calendar.language["date2stringpattern"][this.language].replace(/\-/g, this.patterndelimiter); this.string2datepattern = string2datepattern || calendar.language["string2datepattern"][this.language]; this.datecontrol = null; this.panel = this.getelementbyid("__calendarpanel"); this.iframe = window.frames["__calendariframe"]; this.form = null; this.date = new date(); this.year = this.date.getfullyear(); this.month = this.date.getmonth(); this.colors = {"bg_cur_day":"#00cc33","bg_over":"#efefef","bg_out":"#ffcc00"} }; calendar.language = { "year" : ["\u5e74", "", "", "\u5e74"], "months" : [ ["\u4e00\u6708","\u4e8c\u6708","\u4e09\u6708","\u56db\u6708","\u4e94\u6708","\u516d\u6708","\u4e03\u6708","\u516b\u6708","\u4e5d\u6708","\u5341\u6708","\u5341\u4e00\u6708","\u5341\u4e8c\u6708"], ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"], ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"], ["\u4e00\u6708","\u4e8c\u6708","\u4e09\u6708","\u56db\u6708","\u4e94\u6708","\u516d\u6708","\u4e03\u6708","\u516b\u6708","\u4e5d\u6708","\u5341\u6708","\u5341\u4e00\u6708","\u5341\u4e8c\u6708"] ], "weeks" : [["\u65e5","\u4e00","\u4e8c","\u4e09","\u56db","\u4e94","\u516d"], ["sun","mon","tur","wed","thu","fri","sat"], ["sun","mon","tur","wed","thu","fri","sat"], ["\u65e5","\u4e00","\u4e8c","\u4e09","\u56db","\u4e94","\u516d"] ], "clear" : ["\u6e05\u7a7a", "clear", "clear", "\u6e05\u7a7a"], "today" : ["\u4eca\u5929", "today", "today", "\u4eca\u5929"], "close" : ["\u5173\u95ed", "close", "close", "\u95dc\u9589"], "date2stringpattern" : ["yyyy-mm-dd", "yyyy-mm-dd", "yyyy-mm-dd", "yyyy-mm-dd"], "string2datepattern" : ["ymd","ymd", "ymd", "ymd"] }; calendar.prototype.draw = function() { calendar = this; var _cs = []; _cs[_cs.length] = '
'; _cs[_cs.length] = ''; _cs[_cs.length] = ' '; _cs[_cs.length] = ' '; for(var i = 0; i < 7; i++) { _cs[_cs.length] = ''; for(var j = 0; j < 7; j++) { switch (j) { case 0: _cs[_cs.length] = ''; _cs[_cs.length] = '
<\/th>'; _cs[_cs.length] = ' <\/select><\/th>'; _cs[_cs.length] = ' <\/th>'; _cs[_cs.length] = ' <\/tr>'; _cs[_cs.length] = '
'; _cs[_cs.length] = calendar.language["weeks"][this.language][i]; _cs[_cs.length] = '<\/th>'; } _cs[_cs.length] = '<\/tr>'; for(var i = 0; i < 6; i++){ _cs[_cs.length] = '
 <\/td>'; break; case 6: _cs[_cs.length] = ' <\/td>'; break; default:_cs[_cs.length] = ' <\/td>'; break; } } _cs[_cs.length] = '<\/tr>'; } _cs[_cs.length] = '
<\/th>'; _cs[_cs.length] = ' <\/th>'; _cs[_cs.length] = ' <\/th>'; _cs[_cs.length] = ' <\/tr>'; _cs[_cs.length] = '<\/table>'; _cs[_cs.length] = '<\/form>'; this.iframe.document.body.innerhtml = _cs.join(""); this.form = this.iframe.document.forms["__calendarform"]; this.form.clearbutton.value = calendar.language["clear"][this.language]; this.form.selecttodaybutton.value = calendar.language["today"][this.language]; this.form.closebutton.value = calendar.language["close"][this.language]; this.form.goprevmonthbutton.onclick = function () {calendar.goprevmonth(this);} this.form.gonextmonthbutton.onclick = function () {calendar.gonextmonth(this);} this.form.yearselect.onchange = function () {calendar.update(this);} this.form.monthselect.onchange = function () {calendar.update(this);} this.form.clearbutton.onclick = function () {calendar.datecontrol.value = "";calendar.hide();} this.form.closebutton.onclick = function () {calendar.hide();} this.form.selecttodaybutton.onclick = function () { var today = new date(); calendar.date = today; calendar.year = today.getfullyear(); calendar.month = today.getmonth(); calendar.datecontrol.value = today.format(calendar.date2stringpattern); calendar.hide(); } }; calendar.prototype.bindyear = function() { var ys = this.form.yearselect; ys.length = 0; for (var i = this.beginyear; i <= this.endyear; i++){ ys.options[ys.length] = new option(i + calendar.language["year"][this.language], i); } }; calendar.prototype.bindmonth = function() { var ms = this.form.monthselect; ms.length = 0; for (var i = 0; i < 12; i++){ ms.options[ms.length] = new option(calendar.language["months"][this.language][i], i); } }; calendar.prototype.goprevmonth = function(e){ if (this.year == this.beginyear && this.month == 0){return;} this.month--; if (this.month == -1) { this.year--; this.month = 11; } this.date = new date(this.year, this.month, 1); this.changeselect(); this.binddata(); }; calendar.prototype.gonextmonth = function(e){ if (this.year == this.endyear && this.month == 11){return;} this.month++; if (this.month == 12) { this.year++; this.month = 0; } this.date = new date(this.year, this.month, 1); this.changeselect(); this.binddata(); }; calendar.prototype.changeselect = function() { var ys = this.form.yearselect; var ms = this.form.monthselect; for (var i= 0; i < ys.length; i++){ if (ys.options[i].value == this.date.getfullyear()){ ys[i].selected = true; break; } } for (var i= 0; i < ms.length; i++){ if (ms.options[i].value == this.date.getmonth()){ ms[i].selected = true; break; } } }; calendar.prototype.update = function (e){ this.year = e.form.yearselect.options[e.form.yearselect.selectedindex].value; this.month = e.form.monthselect.options[e.form.monthselect.selectedindex].value; this.date = new date(this.year, this.month, 1); this.changeselect(); this.binddata(); }; calendar.prototype.binddata = function () { var calendar = this; var datearray = this.getmonthviewdatearray(this.date.getfullyear(), this.date.getmonth()); var tds = this.getelementsbytagname("td", this.getelementbyid("__calendartable", this.iframe.document)); for(var i = 0; i < tds.length; i++) { tds[i].style.backgroundcolor = calendar.colors["bg_over"]; tds[i].onclick = null; tds[i].onmouseover = null; tds[i].onmouseout = null; tds[i].innerhtml = datearray[i] || " "; if (i > datearray.length - 1) continue; if (datearray[i]){ tds[i].onclick = function () { if (calendar.datecontrol){ calendar.datecontrol.value = new date(calendar.date.getfullyear(), calendar.date.getmonth(), this.innerhtml).format(calendar.date2stringpattern); } calendar.hide(); } tds[i].onmouseover = function () {this.style.backgroundcolor = calendar.colors["bg_out"];} tds[i].onmouseout = function () {this.style.backgroundcolor = calendar.colors["bg_over"];} var today = new date(); if (today.getfullyear() == calendar.date.getfullyear()) { if (today.getmonth() == calendar.date.getmonth()) { if (today.getdate() == datearray[i]) { tds[i].style.backgroundcolor = calendar.colors["bg_cur_day"]; tds[i].onmouseover = function () {this.style.backgroundcolor = calendar.colors["bg_out"];} tds[i].onmouseout = function () {this.style.backgroundcolor = calendar.colors["bg_cur_day"];} } } } }//end if }//end for }; calendar.prototype.getmonthviewdatearray = function (y, m) { var datearray = new array(42); var dayoffirstdate = new date(y, m, 1).getday(); var datecountofmonth = new date(y, m + 1, 0).getdate(); for (var i = 0; i < datecountofmonth; i++) { datearray[i + dayoffirstdate] = i + 1; } return datearray; }; calendar.prototype.show = function (datecontrol, popucontrol) { if (this.panel.style.visibility == "visible") { this.panel.style.visibility = "hidden"; } if (!datecontrol){ throw new error("arguments[0] is necessary!") } this.datecontrol = datecontrol; popucontrol = popucontrol || datecontrol; this.draw(); this.bindyear(); this.bindmonth(); if (datecontrol.value.length > 0){ this.date = new date(datecontrol.value.todate(this.patterndelimiter, this.string2datepattern)); this.year = this.date.getfullyear(); this.month = this.date.getmonth(); } this.changeselect(); this.binddata(); var xy = this.getabspoint(popucontrol); this.panel.style.left = xy.x + "px"; this.panel.style.top = (xy.y + datecontrol.offsetheight) + "px"; this.panel.style.visibility = "visible"; }; calendar.prototype.hide = function() { this.panel.style.visibility = "hidden"; }; calendar.prototype.getelementbyid = function(id, object){ object = object || document; return document.getelementbyid ? object.getelementbyid(id) : document.all(id); }; calendar.prototype.getelementsbytagname = function(tagname, object){ object = object || document; return document.getelementsbytagname ? object.getelementsbytagname(tagname) : document.all.tags(tagname); }; calendar.prototype.getabspoint = function (e){ var x = e.offsetleft; var y = e.offsettop; while(e = e.offsetparent){ x += e.offsetleft; y += e.offsettop; } return {"x": x, "y": y}; }; /** * @param d the delimiter * @param p the pattern of your date * @author meizz * @author kimsoft add w+ pattern */ date.prototype.format = function(style) { var o = { "m+" : this.getmonth() + 1, //month "d+" : this.getdate(), //day "h+" : this.gethours(), //hour "m+" : this.getminutes(), //minute "s+" : this.getseconds(), //second "w+" : "\u65e5\u4e00\u4e8c\u4e09\u56db\u4e94\u516d".charat(this.getday()), //week "q+" : math.floor((this.getmonth() + 3) / 3), //quarter "s" : this.getmilliseconds() //millisecond } if (/(y+)/.test(style)) { style = style.replace(regexp.$1, (this.getfullyear() + "").substr(4 - regexp.$1.length)); } for(var k in o){ if (new regexp("("+ k +")").test(style)){ style = style.replace(regexp.$1, regexp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); } } return style; }; /** * @param d the delimiter * @param p the pattern of your date * @rebuilder kimsoft * @version build 2006.12.15 */ string.prototype.todate = function(delimiter, pattern) { delimiter = delimiter || "-"; pattern = pattern || "ymd"; var a = this.split(delimiter); var y = parseint(a[pattern.indexof("y")], 10); //remember to change this next century ;) if(y.tostring().length <= 2) y += 2000; if(isnan(y)) y = new date().getfullyear(); var m = parseint(a[pattern.indexof("m")], 10) - 1; var d = parseint(a[pattern.indexof("d")], 10); if(isnan(d)) d = 1; return new date(y, m, d); }; document.writeln('