123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661 |
- 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("<iframe id='co_covering' style='z-index:1000;position:absolute;display:none;width:185px' noscrolling='true' frameborder='0'></iframe>");
- document.write("<div id='co_calendar' style='z-index:1001;position:absolute;display:none;text-align:center;overflow:visible' onselectstart='return false;'>");
- document.write(" <div style='width:185px;background-color:#f2f5f7;border:1px solid #dddddd;' class=ds_font>");
- document.write(" <table cellpadding=0 cellspacing=1 width='100%' height=25 bgcolor=#deedf7>");
- document.write(" <tr align=center>");
- document.write(" <td class='ds_normal' width=12% onmouseover=this.className='ds_over' onmouseout=this.className='ds_normal' onclick=addYear(this,-1) title='" + titles['subYear'] + "'><<</td>");
- document.write(" <td class='ds_normal' width=12% onmouseover=this.className='ds_over' onmouseout=this.className='ds_normal' onclick=addMonth(this,-1) title='" + titles['subMonth'] + "'><</td>");
- document.write(" <td id='co_calendar_y'><select id='co_sel_year' onchange='doChangeYear(this.value)'></select></td><td id='co_calendar_m'><select id='co_sel_month' onchange='doChangeMonth(this.value)'></select></td>");
- document.write(" <td class='ds_normal' width=12% onmouseover=this.className='ds_over' onmouseout=this.className='ds_normal' onclick=addMonth(this,1) title='" + titles['addYear'] + "'>></td>");
- document.write(" <td class='ds_normal' width=12% onmouseover=this.className='ds_over' onmouseout=this.className='ds_normal' onclick=addYear(this,1) title='" + titles['addMonth'] + "'>>></td>");
- document.write(" </tr>");
- document.write(" </table>");
-
- document.write(" <table cellpadding=0 cellspacing=0 width=100% height=20>");
- document.write(" <tr align=center>");
- for(i=0;i<7;i++) {
- document.write(" <td>"+week[i]+"</td>");
- }
- document.write(" </tr>");
- document.write(" </table>");
-
- document.write(" <table id='co_calendar_days' cellpadding=0 cellspacing=2 width=100% bgcolor=#f2f5f7>");
- for(i=0;i<6;i++)
- {
- document.write(" <tr align=center>");
- for(j=0;j<7;j++)
- document.write(" <td style='font-size:13px;' width=10% height=18 onmouseover=if(this.className=='ds_normal')this.className='ds_over' onmouseout=if(this.className=='ds_over')this.className='ds_normal'></td>");
- document.write(" </tr>");
- }
- document.write(" </table>");
- document.write(" <table id='co_calendar_times' width=100% style='margin-bottom:5px;display:none' align='center' cellpadding=1 cellspacing=1 bgcolor=#CEDAE7>");
- document.write(" <tr><td align=center width=12% onmouseover=this.className='ds_normal' onmouseout=this.className='' onclick=addHour(this,-1)><<</td>");
- document.write(" <td align=center width=12% onmouseover=this.className='ds_normal' onmouseout=this.className='' onclick=addMinute(this,-5)><</td>");
- document.write(" <td><select id='co_sel_hour' onchange=doChangeHour(this.value)></select></td><td><select id='co_sel_minute' onchange=doChangeMinute(this.value)></select></td>");
- document.write(" <td align=center width=12% onmouseover=this.className='ds_normal' onmouseout=this.className='' onclick=addMinute(this,5)>></td>");
- document.write(" <td align=center width=12% onmouseover=this.className='ds_normal' onmouseout=this.className='' onclick=addHour(this,1)>>></td></tr>");
- document.write(" </table>");
- document.write(" <span id='co_calendar_ok' class='ds_normal' onmouseover=this.className='ds_over' onmouseout=this.className='ds_normal'>" + titles['ok'] + "</span>");
- document.write(" <span id='co_calendar_clear' class='ds_normal' onmouseover=this.className='ds_over' onmouseout=this.className='ds_normal' onclick=clearDate()>" + titles['clear'] + "</span>");
- document.write(" <span class='ds_normal' onmouseover=this.className='ds_over' onmouseout=this.className='ds_normal' onclick=dateSel.hide()>" + titles['close'] + "</span>");
- document.write(" </div>");
- document.write("</div>");
- $("#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<obj.rows.length;i++)
- {
- for (j = 0; j < 7; j ++) {
- obj.rows[i].cells[j].innerHTML='';
- obj.rows[i].cells[j].className='ds_disabled';
- obj.rows[i].cells[j].onclick = null;
- }
- }
- for(i=0;i<length;i++)
- {
- var r = Math.floor((i+day)/7);
- var c = i + day - r * 7;
- var dtd = obj.rows[r].cells[c];
- dtd.innerHTML=(i+1);
- dtd.className='ds_normal';
- if(this.year==today.getFullYear()&&this.month==today.getMonth()&&(i+1)==today.getDate())
- dtd.className='ds_today';
- if(this.origYear==this.year&&this.origMonth==this.month&&this.origDate == (i+1)) {
- dtd.className='ds_selected';
- this.selectedDayElem = obj.rows[r].cells[c];
- }
- var matchResult = this.inputObj.className.match(/(before|after)(#\w+)?/);
- if (matchResult != null && matchResult.length > 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+5;i++) {
- var op = document.createElement("OPTION");
- op.text = i;
- op.value = i;
- selYear.options.add(op);
- }
- } else if (parseInt(selYear.options[0].value) > 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 = "";
- }
- }
- }
|