var week=new Array('日','一','二','三','四','五','六'); var titles = {year:'年',month:'月',hour:'时',minute:'分',subYear:'上一年',subMonth:'上一月',addYear:'下一年',addMonth:'下一月',ok:'确定', clear:'清除',close:'关闭'} var msgs = {'errformat':'日期格式有误! \n请输入格式如 {0} 的日期'} function dateSelector() { var myDate=__TODAY; this.year=myDate.getFullYear(); this.month=myDate.getMonth(); this.date=myDate.getDate(); this.hour=myDate.getHours(); this.minute=myDate.getMinutes(); this.inputObj = null; this.allowEmpty = true; this.showTime = false; this.postBack = false; this.pattern = "yyyy-MM-dd"; this.origYear = this.year; this.origMonth = this.month; this.origDate = this.date; this.selectedDayElem = null; this.initial=function() { if ($.browser.msie && $.browser.version < 7.0) document.write(""); document.write(""); $("#co_calendar_ok").bind('click', true, selectDate); } function selectDate(evt) { if (dateSel.showTime && evt.data != true) { if (dateSel.selectedDayElem != null) { dateSel.selectedDayElem.className = ''; } evt.target.className = 'ds_selected'; dateSel.selectedDayElem = evt.target; } if (evt.data != true) { dateSel.date = parseInt(evt.target.innerHTML); } if (!dateSel.showTime || evt.data == true) { if (dateSel.inputObj) { var existedValue = dateSel.inputObj.value; dateSel.inputObj.value = getDateString(dateSel.year, dateSel.month + 1, dateSel.date, dateSel.hour, dateSel.minute, dateSel.pattern); $(dateSel.inputObj).blur(); if (dateSel.postBack && existedValue != dateSel.inputObj.value) { __doPostBack(dateSel.id, ''); } if (typeof(dateSel.inputObj.onchange) == 'function' && existedValue != dateSel.inputObj.value) { dateSel.inputObj.onchange.call(dateSel.inputObj, existedValue); } } dateSel.hide(); } } this.setCurrent = function() { var myDate=__TODAY; this.year=myDate.getFullYear(); this.month=myDate.getMonth(); this.date=myDate.getDate(); } this.refreshDate = function() { var myDate=new Date(this.year,this.month, 1); var today=__TODAY; var day=myDate.getDay(); var length; switch(this.month + 1) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: length=31; break; case 4: case 6: case 9: case 11: length=30; break; case 2: if((this.year%4==0)&&(this.year%100!=0)||(this.year%400==0)) length=29; else length=28; } var obj = document.getElementById('co_calendar_days'); for(i=0;i 1) { var otherDate = today; if (matchResult.length > 2 && matchResult[2]) { var otherDp = document.getElementById(matchResult[2].substr(1)); if (otherDp != null && otherDp.value.length > 0) { var otherDateArr = extractDate(otherDp.value, this.pattern); otherDate = new Date(otherDateArr[0], otherDateArr[1], otherDateArr[2]); } } var thisDate = new Date(this.year, this.month, (i+1)); if (thisDate.getTime() > otherDate.getTime() && matchResult[1] == 'before') { dtd.className = 'ds_disabled'; } else if (thisDate.getTime() < otherDate.getTime() && matchResult[1] == 'after') { dtd.className = 'ds_disabled'; } } if (dtd.className != 'ds_disabled') { $(dtd).bind('click', selectDate); } else { $(dtd).unbind('click'); } } var selYear = document.getElementById('co_sel_year'); if (selYear.options.length == 0) { for(i = this.year - 20; i < this.year + 5; i ++) { var op = document.createElement("OPTION"); op.text = i; op.value = i; selYear.options.add(op); if (i == this.year) { op.selected = true; } } } else { if (parseInt(selYear.options[selYear.options.length - 1].value) < this.year) { for (i=0;i<5;i++) { selYear.options.remove(0); } for (i=this.year;i this.year) { for(i=0;i<5;i++) { selYear.options.remove(selYear.options.length - 1); } for (i=this.year;i>this.year-5;i--) { var op = document.createElement("OPTION"); op.text = i; op.value = i; selYear.options.add(op, 0); } } selYear.value = this.year; } var selMonth = document.getElementById('co_sel_month'); if (selMonth.options.length == 0) { for(i = 1; i <= 12; i ++) { var op = document.createElement("OPTION"); op.text = i + titles['month']; op.value = i; selMonth.options.add(op); if (i == this.month + 1) { op.selected = true; } } } else { selMonth.selectedIndex = this.month; } } this.refreshTime = function() { if (this.showTime) { document.getElementById('co_calendar_times').style.display = ''; var selHour = document.getElementById('co_sel_hour'); if (selHour.options.length == 0) { for (i=0;i<24;i++) { var op = document.createElement("OPTION"); op.text = i + titles['hour']; op.value = i; selHour.options.add(op); } } selHour.value = this.hour; var selMinute = document.getElementById('co_sel_minute'); if (selMinute.options.length == 0) { for (i=0;i<60;i=i+5) { var op = document.createElement("OPTION"); op.text = i + titles['minute']; op.value = i; selMinute.options.add(op); } } var nearestMinute = Math.round(this.minute/5)*5; selMinute.value = nearestMinute==60 ? 0 : nearestMinute; } else { document.getElementById('co_calendar_times').style.display = 'none'; } } this.show = function(obj, x, y) { this.refreshDate(); if (this.showTime) { this.refreshTime(); } var cal = document.getElementById('co_calendar'); cal.style.display = ''; if (typeof(x) == 'undefined' || typeof(y) == 'undefined') { var ofs = $(obj).offset(); var po = [ofs.left, ofs.top]; x = po[0]; y = po[1] + obj.offsetHeight; if (po[1] + cal.clientHeight + obj.offsetHeight > document.body.clientHeight) { if (po[1] - cal.clientHeight >= 0) { y = po[1] - cal.clientHeight; } } if (po[0] + cal.clientWidth > document.body.clientWidth + document.body.scrollLeft) { x = document.body.clientWidth + document.body.scrollLeft - cal.clientWidth; } } $(cal).css("left", x).css("top", y); if ($.browser.msie && $.browser.version < 7.0) { $('#co_covering').css("display", '').css("top", y).css("left", x).css('height', cal.clientHeight); } } this.hide = function() { var cal = document.getElementById('co_calendar'); if ($.browser.msie && $.browser.version < 7.0) { var covering = document.getElementById('co_covering'); covering.style.display = 'none'; } cal.style.display = 'none'; } } var dateSel = new dateSelector(); dateSel.initial(); if (document.attachEvent) { document.attachEvent("onmouseup", clickSpace); } else { document.addEventListener("mouseup", clickSpace, false); } function clickSpace(evt) { var evt = evt || window.event; var elm = evt.srcElement || evt.target; if (elm.tagName == 'SELECT' && elm.id.length > 6 && elm.id.substr(0, 7) == 'co_sel_') { return; } var cal = document.getElementById('co_calendar'); if (!isAncensor(cal, elm)) { dateSel.hide(); } } function isAncensor(anc, child) { while (child) { if (child == anc) { return true; } child = child.parentNode; } return false; } function addYear(obj,year) { dateSel.year += year; dateSel.refreshDate(); } function addMonth(obj,month) { dateSel.month += month; if (dateSel.month > 11) { dateSel.month = 0; dateSel.year += 1; } if (dateSel.month < 0) { dateSel.month = 11; dateSel.year -= 1; } dateSel.refreshDate(); } function addHour(obj, hour) { dateSel.hour += hour; if (dateSel.hour < 0) { dateSel.hour = 0; } if (dateSel.hour > 23) { dateSel.hour = 23; } dateSel.refreshTime(); } function addMinute(obj, minute) { dateSel.minute += minute; if (dateSel.minute < 0) { dateSel.minute = 55; addHour(null, -1); } if (dateSel.minute > 55) { dateSel.minute = 0; addHour(null, 1); } dateSel.refreshTime(); } function doChangeYear(ystr){ dateSel.year = parseInt(ystr); dateSel.refreshDate(); } function doChangeMonth(mstr){ dateSel.month = parseInt(mstr)-1; dateSel.refreshDate(); } function doChangeHour(hstr) { dateSel.hour = parseInt(hstr); } function doChangeMinute(mi) { dateSel.minute=parseInt(mi); } function clearDate() { if (dateSel.inputObj) { dateSel.inputObj.value = ""; } dateSel.hide(); } function cumulativeOffset(element) { var valueT = 0, valueL = 0; do { valueT += element.offsetTop || 0; valueL += element.offsetLeft || 0; element = element.offsetParent; } while (element); return [valueL, valueT]; } function getDateString(year, month, date, hour, minute, pattern) { var ys = year.toString(); var yi = ys.length - 1; var r = ""; for (var i = pattern.length - 1; i >= 0; i--) { var c = pattern.charAt(i); if (c == 'y') { if (yi < 0) { r += '0'; } else { r += ys.charAt(yi); } yi --; } else if (c == 'M') { if (i-1 >= 0 && pattern.charAt(i-1) == c) { if (month < 10) { r += month.toString() + '0'; } else { r += reverse(month.toString()); } i--; } else { r += reverse(month.toString()); } } else if (c == 'd') { if (i-1 >= 0 && pattern.charAt(i-1) == c) { if (date < 10) { r += date.toString() + '0'; } else { r += reverse(date.toString()); } i--; } else { r += reverse(date.toString()); } } else if (c == 'h'||c=='H') { if (i-1 >= 0 && pattern.charAt(i-1) == c) { if (hour < 10) { r += hour.toString() + '0'; } else { r += reverse(hour.toString()); } i--; } else { r += reverse(hour.toString()); } } else if (c == 'm') { if (i-1 >= 0 && pattern.charAt(i-1) == c) { if (minute < 10) { r += minute.toString() + '0'; } else { r += reverse(minute.toString()); } i--; } else { r += reverse(minute.toString()); } } else { r += c; } } return reverse(r); } function reverse(str) { var r = ""; for (var i = str.length - 1; i >= 0; i --) { r += str.charAt(i); } return r; } function isDigit(c) { var isd = c >= '0' && c <= '9'; return isd; } function extractDate(datestr, pattern) { var y = ""; var m = ""; var d = ""; var h = ""; var mi = ""; var j = 0; for (var i = 0 ; i < pattern.length; i++) { var c = pattern.charAt(i); if (c == 'y') { if (i + 1 < pattern.length && pattern.charAt(i+1) == c) { if (i + 3 < pattern.length && pattern.charAt(i + 2) == c && pattern.charAt(i+3) == c) { y = datestr.substr(j, 4); i += 3; j += 4; } else { var today = __TODAY y = today.getFullYear().toString().substr(0, 2) + datestr.substr(j, 2); i += 1; j += 2; } } } else if (c == 'M') { if (isDigit(datestr.charAt(j+1))) { m = datestr.substr(j, 2); j += 2; } else { m = datestr.substr(j, 1); j ++; } if (i + 1 < pattern.length && pattern.charAt(i+1) == c) { i ++; } } else if (c == 'd') { if (isDigit(datestr.charAt(j+1))) { d = datestr.substr(j, 2); j += 2; } else { d = datestr.substr(j, 1); j ++; } if (i + 1 < pattern.length && pattern.charAt(i+1) == c) { i ++; } } else if (c == 'h'||c=='H') { if (isDigit(datestr.charAt(j+1))) { h = datestr.substr(j, 2); j += 2; } else { h = datestr.substr(j, 1); j ++; } if (i + 1 < pattern.length && pattern.charAt(i+1) == c) { i ++; } } else if (c == 'm') { if (isDigit(datestr.charAt(j+1))) { mi = datestr.substr(j, 2); j += 2; } else { mi = datestr.substr(j, 1); j ++; } if (i + 1 < pattern.length && pattern.charAt(i+1) == c) { i ++; } } else { j ++; } } if (m.charAt(0) == '0') m = m.substring(1); if (d.charAt(0) == '0') d = d.substring(1); if (h.charAt(0) == '0') h = h.substring(1); if (mi.charAt(0) == '0') mi = mi.substring(1); var today = __TODAY; return [y.length == 0 ? today.getFullYear() : parseInt(y), m.length == 0 ? today.getMonth() : parseInt(m) - 1, d.length == 0 ? today.getDate() : parseInt(d), h.length == 0 ? today.getHours() : parseInt(h), mi.length == 0 ? today.getMinutes() : parseInt(mi)]; } function popCalendar(obj, pattern, allowEmpty, showTime, postBack) { dateSel.pattern = pattern; dateSel.showTime = showTime; if (obj.value.length > 0 && validateDateFormat(obj.value, pattern)) { var d = extractDate(obj.value, pattern); if (!isValidDate(d[0], d[1], d[2], d[3], d[4])) { dateSel.setCurrent(); } else { dateSel.year = d[0]; dateSel.month = d[1]; dateSel.date = d[2]; dateSel.origYear = d[0]; dateSel.origMonth = d[1]; dateSel.origDate = d[2]; if (dateSel.showTime) { dateSel.hour = d[3]; dateSel.minute = d[4]; } } } else { dateSel.setCurrent(); } dateSel.inputObj = obj; dateSel.allowEmpty = allowEmpty; dateSel.postBack = postBack; dateSel.show(obj); } function isValidDate(year, month, day, hour, minute) { if (isNaN(year) || isNaN(month) || isNaN(day)) { return false; } if (month < 0 || month > 11) { return false; } if (day < 1 || day > 31) { return false; } if (year > 3000 || year < 1000) { return false; } if ((month == 3 || month == 5 || month == 8 || month == 10) && (day == 31)) { return false; } if (month == 1) { var leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); if (day>29 || (day == 29 && !leap)) { return false; } } if (hour) { if (hour < 0 && hour > 23) return false; } if (minute) { if (minute<0 && minute>59) return false; } return true; } function validateDateFormat(value, datePattern) { var regStr = datePattern.replace(/y/g,'[0-9]').replace(/MM/g,'((0[1-9])|(1[0-2]))') .replace(/M/g,'([1-9]|(1[0-2]))').replace(/dd/g,'((0[1-9])|([1-2][0-9])|(3[0-1]))') .replace(/d/g,'([1-9]|([1-2][0-9])|(3[0-1]))') .replace(/hh/g,'(([0-1][0-9])|(2[0-3]))').replace(/h/g,'(([1-9])|(1[0-9])|(2[0-3]))') .replace(/HH/g,'(([0-1][0-9])|(2[0-3]))').replace(/H/g,'(([1-9])|(1[0-9])|(2[0-3]))') .replace(/mm/g,'([0-5][0-9])').replace(/m/g,'(([1-9])|([1-5][0-9]))') .replace(/\./g,'\\.'); var reg = new RegExp('^' + regStr + '$'); return reg.exec(value) != null; } function validateDate(value, datePattern, notify) { if (value.length == 0) return true; var v = validateDateFormat(value, datePattern); var d; if (v) { d = extractDate(value, datePattern); var v = isValidDate(d[0], d[1], d[2], d[3], d[4]); } if (! v && notify) { var today = __TODAY; alert(msgs['errformat'].replace(/\{0\}/, getDateString(today.getFullYear(), 5, 15, 14, 5, datePattern))); } return v; } function fixDate(obj, pattern) { if (obj.tagName == "INPUT" && obj.type == "text") { if (! validateDate(obj.value, pattern)) { obj.value = ""; } } }