var cal; var isfocus = false; function selectdate(obj) { if (obj.value == null) { obj.value = obj.innertext; obj.novalue = true; } var date = new date(); var by = date.getfullyear() - 100; var ey = date.getfullyear(); cal = (cal == null) ? new calendar(by, ey, 0) : cal; cal.show(obj); } string.prototype.todate = function(style) { var y = this.substring(style.indexof('y'), style.lastindexof('y') + 1); var m = this.substring(style.indexof('m'), style.lastindexof('m') + 1); var d = this.substring(style.indexof('d'), style.lastindexof('d') + 1); if (isnan(y)) y = new date().getfullyear(); if (isnan(m)) m = new date().getmonth(); if (isnan(d)) d = new date().getdate(); var dt; eval("dt = new date('" + y + "', '" + (m - 1) + "','" + d + "')"); return dt; } date.prototype.format = function(style) { var o = { "m+": this.getmonth() + 1, "d+": this.getdate(), "h+": this.gethours(), "m+": this.getminutes(), "s+": this.getseconds(), "w+": "天一二三四五六".charat(this.getday()), "q+": math.floor((this.getmonth() + 3) / 3), "s": this.getmilliseconds() } 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; }; function calendar(beginyear, endyear, lang, dateformatstyle) { this.beginyear = 1990; this.endyear = 2010; this.lang = 0; this.dateformatstyle = "yyyy-mm-dd"; if (beginyear != null && endyear != null) { this.beginyear = beginyear; this.endyear = endyear; } if (lang != null) { this.lang = lang } if (dateformatstyle != null) { this.dateformatstyle = dateformatstyle } this.datecontrol = null; this.panel = this.getelementbyid("calendarpanel"); this.container = this.getelementbyid("containerpanel"); this.form = null; this.date = new date(); this.year = this.date.getfullyear(); this.month = this.date.getmonth(); this.colors = { "cur_word": "#ffffff", "cur_bg": "#f5ac21", "sel_bg": "#6a6f6f", "sun_word": "#ff0000", "sat_word": "#ff0000", "other_word": "#000000", "td_word_light": "#333333", "td_word_dark": "#cccccc", "td_bg_out": "#e5f4fe", "td_bg_over": "#3eaadb", "tr_word": "#ffffff", "tr_bg": "#3eaadb", "input_border": "#cccccc", "input_bg": "#e5f5ff", "cal_bg": "#e5f5ff" } this.draw(); this.bindyear(); this.bindmonth(); this.changeselect(); this.binddata(); } calendar.language = { "year": [[""], [""]], "months": [["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"], ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]], "weeks": [["日", "一", "二", "三", "四", "五", "六"], ["sun", "mon", "tur", "wed", "thu", "fri", "sat"]], "clear": [["清空"], ["cls"]], "today": [["今天"], ["today"]], "close": [["关闭"], ["close"]] } calendar.prototype.draw = function() { calendar = this; var mvary = []; mvary[mvary.length] = '
'; mvary[mvary.length] = ' '; mvary[mvary.length] = ' '; mvary[mvary.length] = ' '; mvary[mvary.length] = ' '; mvary[mvary.length] = ' '; mvary[mvary.length] = ' '; mvary[mvary.length] = '
'; mvary[mvary.length] = ' '; mvary[mvary.length] = ' '; for (var i = 0; i < 7; i++) { mvary[mvary.length] = ' '; } mvary[mvary.length] = ' '; for (var i = 0; i < 6; i++) { mvary[mvary.length] = ' '; for (var j = 0; j < 7; j++) { if (j == 0) { mvary[mvary.length] = ' '; } else if (j == 6) { mvary[mvary.length] = ' '; } else { mvary[mvary.length] = ' '; } } mvary[mvary.length] = ' '; } mvary[mvary.length] = ' '; mvary[mvary.length] = ' '; mvary[mvary.length] = ' '; mvary[mvary.length] = ' '; mvary[mvary.length] = ' '; mvary[mvary.length] = '
' + calendar.language["weeks"][this.lang][i] + '
'; mvary[mvary.length] = '
'; this.panel.innerhtml = mvary.join(""); var obj = this.getelementbyid("prevmonth"); obj.onclick = function() { calendar.goprevmonth(calendar); } obj.onblur = function() { calendar.onblur(); } this.prevmonth = obj; obj = this.getelementbyid("nextmonth"); obj.onclick = function() { calendar.gonextmonth(calendar); } obj.onblur = function() { calendar.onblur(); } this.nextmonth = obj; obj = this.getelementbyid("calendarclear"); obj.onclick = function() { calendar.datecontrol.value = ""; calendar.hide(); } this.calendarclear = obj; obj = this.getelementbyid("calendarclose"); obj.onclick = function() { calendar.hide(); } this.calendarclose = obj; obj = this.getelementbyid("calendaryear"); obj.onchange = function() { calendar.update(calendar); } obj.onblur = function() { calendar.onblur(); } this.calendaryear = obj; obj = this.getelementbyid("calendarmonth"); with (obj) { onchange = function() { calendar.update(calendar); } onblur = function() { calendar.onblur(); } } this.calendarmonth = obj; obj = this.getelementbyid("calendartoday"); obj.onclick = function() { var today = new date(); calendar.date = today; calendar.year = today.getfullyear(); calendar.month = today.getmonth(); calendar.changeselect(); calendar.binddata(); calendar.datecontrol.value = today.format(calendar.dateformatstyle); calendar.hide(); } this.calendartoday = obj; } calendar.prototype.bindyear = function() { var cy = this.calendaryear; cy.length = 0; for (var i = this.beginyear; i <= this.endyear; i++) { cy.options[cy.length] = new option(i + calendar.language["year"][this.lang], i); } } calendar.prototype.bindmonth = function() { var cm = this.calendarmonth; cm.length = 0; for (var i = 0; i < 12; i++) { cm.options[cm.length] = new option(calendar.language["months"][this.lang][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 cy = this.calendaryear; var cm = this.calendarmonth; for (var i = 0; i < cy.length; i++) { if (cy.options[i].value == this.date.getfullyear()) { cy[i].selected = true; break; } } for (var i = 0; i < cm.length; i++) { if (cm.options[i].value == this.date.getmonth()) { cm[i].selected = true; break; } } } calendar.prototype.update = function(e) { this.year = e.calendaryear.options[e.calendaryear.selectedindex].value; this.month = e.calendarmonth.options[e.calendarmonth.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.getmonthviewarray(this.date.getfullyear(), this.date.getmonth()); var tds = this.getelementbyid("calendartable").getelementsbytagname("td"); for (var i = 0; i < tds.length; i++) { tds[i].style.backgroundcolor = calendar.colors["td_bg_out"]; tds[i].onclick = function() { return; } tds[i].onmouseover = function() { return; } tds[i].onmouseout = function() { return; } if (i > datearray.length - 1) break; tds[i].innerhtml = datearray[i]; if (datearray[i] != " ") { tds[i].onclick = function() { if (calendar.datecontrol != null) { calendar.datecontrol.value = new date(calendar.date.getfullyear(), calendar.date.getmonth(), this.innerhtml).format(calendar.dateformatstyle); } calendar.hide(); } tds[i].onmouseover = function() { this.style.backgroundcolor = calendar.colors["td_bg_over"]; } tds[i].onmouseout = function() { this.style.backgroundcolor = calendar.colors["td_bg_out"]; } if (new date().format(calendar.dateformatstyle) == new date(calendar.date.getfullyear(), calendar.date.getmonth(), datearray[i]).format(calendar.dateformatstyle)) { tds[i].style.backgroundcolor = calendar.colors["cur_bg"]; tds[i].onmouseover = function() { this.style.backgroundcolor = calendar.colors["td_bg_over"]; } tds[i].onmouseout = function() { this.style.backgroundcolor = calendar.colors["cur_bg"]; } } if (calendar.datecontrol != null && calendar.datecontrol.value == new date(calendar.date.getfullyear(), calendar.date.getmonth(), datearray[i]).format(calendar.dateformatstyle)) { tds[i].style.backgroundcolor = calendar.colors["sel_bg"]; tds[i].onmouseover = function() { this.style.backgroundcolor = calendar.colors["td_bg_over"]; } tds[i].onmouseout = function() { this.style.backgroundcolor = calendar.colors["sel_bg"]; } } } } } calendar.prototype.getmonthviewarray = function(y, m) { var mvarray = []; var dayoffirstday = new date(y, m, 1).getday(); var daysofmonth = new date(y, m + 1, 0).getdate(); for (var i = 0; i < 42; i++) { mvarray[i] = " "; } for (var i = 0; i < daysofmonth; i++) { mvarray[i + dayoffirstday] = i + 1; } return mvarray; } calendar.prototype.getelementbyid = function(id) { if (typeof (id) != "string" || id == "") return null; if (document.getelementbyid) return document.getelementbyid(id); if (document.all) return document.all(id); try { return eval(id); } catch (e) { return null; } } calendar.prototype.getelementsbytagname = function(object, tagname) { if (document.getelementsbytagname) return document.getelementsbytagname(tagname); if (document.all) return 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 }; } calendar.prototype.show = function(dateobj, popcontrol) { if (dateobj == null) { throw new error("arguments[0] is necessary") } this.datecontrol = dateobj; this.date = (dateobj.value.length > 0) ? new date(dateobj.value.todate(this.dateformatstyle)) : new date(); this.year = this.date.getfullyear(); this.month = this.date.getmonth(); this.changeselect(); this.binddata(); if (popcontrol == null) { popcontrol = dateobj; } var xy = this.getabspoint(popcontrol); this.panel.style.left = xy.x - 25 + "px"; this.panel.style.top = (xy.y + dateobj.offsetheight) + "px"; this.panel.style.display = ""; this.container.style.display = ""; dateobj.onblur = function() { calendar.onblur(); } this.container.onmouseover = function() { isfocus = true; } this.container.onmouseout = function() { isfocus = false; } } calendar.prototype.hide = function() { this.panel.style.display = "none"; this.container.style.display = "none"; isfocus = false; if (this.datecontrol.novalue) { this.datecontrol.innertext = this.datecontrol.value; if (this.datecontrol.changetime) this.datecontrol.changetime(); } } calendar.prototype.onblur = function() { if (!isfocus) { this.hide(); } } document.write('');