ks-datepicker.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. var week=new Array('日','一','二','三','四','五','六');
  2. var titles = {year:'年',month:'月',hour:'时',minute:'分',subYear:'上一年',subMonth:'上一月',addYear:'下一年',addMonth:'下一月',ok:'确定', clear:'清除',close:'关闭'}
  3. var msgs = {'errformat':'日期格式有误! \n请输入格式如 {0} 的日期'}
  4. function dateSelector()
  5. {
  6. var myDate=__TODAY;
  7. this.year=myDate.getFullYear();
  8. this.month=myDate.getMonth();
  9. this.date=myDate.getDate();
  10. this.hour=myDate.getHours();
  11. this.minute=myDate.getMinutes();
  12. this.inputObj = null;
  13. this.allowEmpty = true;
  14. this.showTime = false;
  15. this.postBack = false;
  16. this.pattern = "yyyy-MM-dd";
  17. this.origYear = this.year;
  18. this.origMonth = this.month;
  19. this.origDate = this.date;
  20. this.selectedDayElem = null;
  21. this.initial=function() {
  22. 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>");
  23. document.write("<div id='co_calendar' style='z-index:1001;position:absolute;display:none;text-align:center;overflow:visible' onselectstart='return false;'>");
  24. document.write(" <div style='width:185px;background-color:#f2f5f7;border:1px solid #dddddd;' class=ds_font>");
  25. document.write(" <table cellpadding=0 cellspacing=1 width='100%' height=25 bgcolor=#deedf7>");
  26. document.write(" <tr align=center>");
  27. 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'] + "'>&lt;&lt;</td>");
  28. 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'] + "'>&lt;</td>");
  29. 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>");
  30. 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'] + "'>&gt;</td>");
  31. 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'] + "'>&gt;&gt;</td>");
  32. document.write(" </tr>");
  33. document.write(" </table>");
  34. document.write(" <table cellpadding=0 cellspacing=0 width=100% height=20>");
  35. document.write(" <tr align=center>");
  36. for(i=0;i<7;i++) {
  37. document.write(" <td>"+week[i]+"</td>");
  38. }
  39. document.write(" </tr>");
  40. document.write(" </table>");
  41. document.write(" <table id='co_calendar_days' cellpadding=0 cellspacing=2 width=100% bgcolor=#f2f5f7>");
  42. for(i=0;i<6;i++)
  43. {
  44. document.write(" <tr align=center>");
  45. for(j=0;j<7;j++)
  46. 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>");
  47. document.write(" </tr>");
  48. }
  49. document.write(" </table>");
  50. document.write(" <table id='co_calendar_times' width=100% style='margin-bottom:5px;display:none' align='center' cellpadding=1 cellspacing=1 bgcolor=#CEDAE7>");
  51. document.write(" <tr><td align=center width=12% onmouseover=this.className='ds_normal' onmouseout=this.className='' onclick=addHour(this,-1)>&lt;&lt;</td>");
  52. document.write(" <td align=center width=12% onmouseover=this.className='ds_normal' onmouseout=this.className='' onclick=addMinute(this,-5)>&lt;</td>");
  53. 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>");
  54. document.write(" <td align=center width=12% onmouseover=this.className='ds_normal' onmouseout=this.className='' onclick=addMinute(this,5)>&gt;</td>");
  55. document.write(" <td align=center width=12% onmouseover=this.className='ds_normal' onmouseout=this.className='' onclick=addHour(this,1)>&gt;&gt;</td></tr>");
  56. document.write(" </table>");
  57. document.write(" <span id='co_calendar_ok' class='ds_normal' onmouseover=this.className='ds_over' onmouseout=this.className='ds_normal'>" + titles['ok'] + "</span>");
  58. 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>");
  59. document.write(" <span class='ds_normal' onmouseover=this.className='ds_over' onmouseout=this.className='ds_normal' onclick=dateSel.hide()>" + titles['close'] + "</span>");
  60. document.write(" </div>");
  61. document.write("</div>");
  62. $("#co_calendar_ok").bind('click', true, selectDate);
  63. }
  64. function selectDate(evt) {
  65. if (dateSel.showTime && evt.data != true) {
  66. if (dateSel.selectedDayElem != null) {
  67. dateSel.selectedDayElem.className = '';
  68. }
  69. evt.target.className = 'ds_selected';
  70. dateSel.selectedDayElem = evt.target;
  71. }
  72. if (evt.data != true) {
  73. dateSel.date = parseInt(evt.target.innerHTML);
  74. }
  75. if (!dateSel.showTime || evt.data == true) {
  76. if (dateSel.inputObj) {
  77. var existedValue = dateSel.inputObj.value;
  78. dateSel.inputObj.value = getDateString(dateSel.year,
  79. dateSel.month + 1, dateSel.date, dateSel.hour,
  80. dateSel.minute, dateSel.pattern);
  81. $(dateSel.inputObj).blur();
  82. if (dateSel.postBack && existedValue != dateSel.inputObj.value) {
  83. __doPostBack(dateSel.id, '');
  84. }
  85. if (typeof(dateSel.inputObj.onchange) == 'function'
  86. && existedValue != dateSel.inputObj.value) {
  87. dateSel.inputObj.onchange.call(dateSel.inputObj,
  88. existedValue);
  89. }
  90. }
  91. dateSel.hide();
  92. }
  93. }
  94. this.setCurrent = function() {
  95. var myDate=__TODAY;
  96. this.year=myDate.getFullYear();
  97. this.month=myDate.getMonth();
  98. this.date=myDate.getDate();
  99. }
  100. this.refreshDate = function() {
  101. var myDate=new Date(this.year,this.month, 1);
  102. var today=__TODAY;
  103. var day=myDate.getDay();
  104. var length;
  105. switch(this.month + 1)
  106. {
  107. case 1:
  108. case 3:
  109. case 5:
  110. case 7:
  111. case 8:
  112. case 10:
  113. case 12:
  114. length=31;
  115. break;
  116. case 4:
  117. case 6:
  118. case 9:
  119. case 11:
  120. length=30;
  121. break;
  122. case 2:
  123. if((this.year%4==0)&&(this.year%100!=0)||(this.year%400==0))
  124. length=29;
  125. else
  126. length=28;
  127. }
  128. var obj = document.getElementById('co_calendar_days');
  129. for(i=0;i<obj.rows.length;i++)
  130. {
  131. for (j = 0; j < 7; j ++) {
  132. obj.rows[i].cells[j].innerHTML='';
  133. obj.rows[i].cells[j].className='ds_disabled';
  134. obj.rows[i].cells[j].onclick = null;
  135. }
  136. }
  137. for(i=0;i<length;i++)
  138. {
  139. var r = Math.floor((i+day)/7);
  140. var c = i + day - r * 7;
  141. var dtd = obj.rows[r].cells[c];
  142. dtd.innerHTML=(i+1);
  143. dtd.className='ds_normal';
  144. if(this.year==today.getFullYear()&&this.month==today.getMonth()&&(i+1)==today.getDate())
  145. dtd.className='ds_today';
  146. if(this.origYear==this.year&&this.origMonth==this.month&&this.origDate == (i+1)) {
  147. dtd.className='ds_selected';
  148. this.selectedDayElem = obj.rows[r].cells[c];
  149. }
  150. var matchResult = this.inputObj.className.match(/(before|after)(#\w+)?/);
  151. if (matchResult != null && matchResult.length > 1) {
  152. var otherDate = today;
  153. if (matchResult.length > 2 && matchResult[2]) {
  154. var otherDp = document.getElementById(matchResult[2].substr(1));
  155. if (otherDp != null && otherDp.value.length > 0) {
  156. var otherDateArr = extractDate(otherDp.value, this.pattern);
  157. otherDate = new Date(otherDateArr[0], otherDateArr[1], otherDateArr[2]);
  158. }
  159. }
  160. var thisDate = new Date(this.year, this.month, (i+1));
  161. if (thisDate.getTime() > otherDate.getTime() && matchResult[1] == 'before') {
  162. dtd.className = 'ds_disabled';
  163. } else if (thisDate.getTime() < otherDate.getTime() && matchResult[1] == 'after') {
  164. dtd.className = 'ds_disabled';
  165. }
  166. }
  167. if (dtd.className != 'ds_disabled') {
  168. $(dtd).bind('click', selectDate);
  169. } else {
  170. $(dtd).unbind('click');
  171. }
  172. }
  173. var selYear = document.getElementById('co_sel_year');
  174. if (selYear.options.length == 0) {
  175. for(i = this.year - 20; i < this.year + 5; i ++) {
  176. var op = document.createElement("OPTION");
  177. op.text = i;
  178. op.value = i;
  179. selYear.options.add(op);
  180. if (i == this.year) {
  181. op.selected = true;
  182. }
  183. }
  184. } else {
  185. if (parseInt(selYear.options[selYear.options.length - 1].value) < this.year) {
  186. for (i=0;i<5;i++) {
  187. selYear.options.remove(0);
  188. }
  189. for (i=this.year;i<this.year+5;i++) {
  190. var op = document.createElement("OPTION");
  191. op.text = i;
  192. op.value = i;
  193. selYear.options.add(op);
  194. }
  195. } else if (parseInt(selYear.options[0].value) > this.year) {
  196. for(i=0;i<5;i++) {
  197. selYear.options.remove(selYear.options.length - 1);
  198. }
  199. for (i=this.year;i>this.year-5;i--) {
  200. var op = document.createElement("OPTION");
  201. op.text = i;
  202. op.value = i;
  203. selYear.options.add(op, 0);
  204. }
  205. }
  206. selYear.value = this.year;
  207. }
  208. var selMonth = document.getElementById('co_sel_month');
  209. if (selMonth.options.length == 0) {
  210. for(i = 1; i <= 12; i ++) {
  211. var op = document.createElement("OPTION");
  212. op.text = i + titles['month'];
  213. op.value = i;
  214. selMonth.options.add(op);
  215. if (i == this.month + 1) {
  216. op.selected = true;
  217. }
  218. }
  219. } else {
  220. selMonth.selectedIndex = this.month;
  221. }
  222. }
  223. this.refreshTime = function() {
  224. if (this.showTime) {
  225. document.getElementById('co_calendar_times').style.display = '';
  226. var selHour = document.getElementById('co_sel_hour');
  227. if (selHour.options.length == 0) {
  228. for (i=0;i<24;i++) {
  229. var op = document.createElement("OPTION");
  230. op.text = i + titles['hour'];
  231. op.value = i;
  232. selHour.options.add(op);
  233. }
  234. }
  235. selHour.value = this.hour;
  236. var selMinute = document.getElementById('co_sel_minute');
  237. if (selMinute.options.length == 0) {
  238. for (i=0;i<60;i=i+5) {
  239. var op = document.createElement("OPTION");
  240. op.text = i + titles['minute'];
  241. op.value = i;
  242. selMinute.options.add(op);
  243. }
  244. }
  245. var nearestMinute = Math.round(this.minute/5)*5;
  246. selMinute.value = nearestMinute==60 ? 0 : nearestMinute;
  247. } else {
  248. document.getElementById('co_calendar_times').style.display = 'none';
  249. }
  250. }
  251. this.show = function(obj, x, y) {
  252. this.refreshDate();
  253. if (this.showTime) {
  254. this.refreshTime();
  255. }
  256. var cal = document.getElementById('co_calendar');
  257. cal.style.display = '';
  258. if (typeof(x) == 'undefined' || typeof(y) == 'undefined') {
  259. var ofs = $(obj).offset();
  260. var po = [ofs.left, ofs.top];
  261. x = po[0];
  262. y = po[1] + obj.offsetHeight;
  263. if (po[1] + cal.clientHeight + obj.offsetHeight > document.body.clientHeight) {
  264. if (po[1] - cal.clientHeight >= 0) {
  265. y = po[1] - cal.clientHeight;
  266. }
  267. }
  268. if (po[0] + cal.clientWidth > document.body.clientWidth + document.body.scrollLeft) {
  269. x = document.body.clientWidth + document.body.scrollLeft - cal.clientWidth;
  270. }
  271. }
  272. $(cal).css("left", x).css("top", y);
  273. if ($.browser.msie && $.browser.version < 7.0) {
  274. $('#co_covering').css("display", '').css("top", y).css("left", x).css('height', cal.clientHeight);
  275. }
  276. }
  277. this.hide = function() {
  278. var cal = document.getElementById('co_calendar');
  279. if ($.browser.msie && $.browser.version < 7.0) {
  280. var covering = document.getElementById('co_covering');
  281. covering.style.display = 'none';
  282. }
  283. cal.style.display = 'none';
  284. }
  285. }
  286. var dateSel = new dateSelector();
  287. dateSel.initial();
  288. if (document.attachEvent) {
  289. document.attachEvent("onmouseup", clickSpace);
  290. } else {
  291. document.addEventListener("mouseup", clickSpace, false);
  292. }
  293. function clickSpace(evt) {
  294. var evt = evt || window.event;
  295. var elm = evt.srcElement || evt.target;
  296. if (elm.tagName == 'SELECT' && elm.id.length > 6 && elm.id.substr(0, 7) == 'co_sel_') {
  297. return;
  298. }
  299. var cal = document.getElementById('co_calendar');
  300. if (!isAncensor(cal, elm)) {
  301. dateSel.hide();
  302. }
  303. }
  304. function isAncensor(anc, child) {
  305. while (child) {
  306. if (child == anc) {
  307. return true;
  308. }
  309. child = child.parentNode;
  310. }
  311. return false;
  312. }
  313. function addYear(obj,year)
  314. {
  315. dateSel.year += year;
  316. dateSel.refreshDate();
  317. }
  318. function addMonth(obj,month)
  319. {
  320. dateSel.month += month;
  321. if (dateSel.month > 11) {
  322. dateSel.month = 0;
  323. dateSel.year += 1;
  324. }
  325. if (dateSel.month < 0) {
  326. dateSel.month = 11;
  327. dateSel.year -= 1;
  328. }
  329. dateSel.refreshDate();
  330. }
  331. function addHour(obj, hour) {
  332. dateSel.hour += hour;
  333. if (dateSel.hour < 0) {
  334. dateSel.hour = 0;
  335. }
  336. if (dateSel.hour > 23) {
  337. dateSel.hour = 23;
  338. }
  339. dateSel.refreshTime();
  340. }
  341. function addMinute(obj, minute) {
  342. dateSel.minute += minute;
  343. if (dateSel.minute < 0) {
  344. dateSel.minute = 55;
  345. addHour(null, -1);
  346. }
  347. if (dateSel.minute > 55) {
  348. dateSel.minute = 0;
  349. addHour(null, 1);
  350. }
  351. dateSel.refreshTime();
  352. }
  353. function doChangeYear(ystr){
  354. dateSel.year = parseInt(ystr);
  355. dateSel.refreshDate();
  356. }
  357. function doChangeMonth(mstr){
  358. dateSel.month = parseInt(mstr)-1;
  359. dateSel.refreshDate();
  360. }
  361. function doChangeHour(hstr) {
  362. dateSel.hour = parseInt(hstr);
  363. }
  364. function doChangeMinute(mi) {
  365. dateSel.minute=parseInt(mi);
  366. }
  367. function clearDate() {
  368. if (dateSel.inputObj) {
  369. dateSel.inputObj.value = "";
  370. }
  371. dateSel.hide();
  372. }
  373. function cumulativeOffset(element) {
  374. var valueT = 0, valueL = 0;
  375. do {
  376. valueT += element.offsetTop || 0;
  377. valueL += element.offsetLeft || 0;
  378. element = element.offsetParent;
  379. } while (element);
  380. return [valueL, valueT];
  381. }
  382. function getDateString(year, month, date, hour, minute, pattern) {
  383. var ys = year.toString();
  384. var yi = ys.length - 1;
  385. var r = "";
  386. for (var i = pattern.length - 1; i >= 0; i--) {
  387. var c = pattern.charAt(i);
  388. if (c == 'y') {
  389. if (yi < 0) {
  390. r += '0';
  391. } else {
  392. r += ys.charAt(yi);
  393. }
  394. yi --;
  395. } else if (c == 'M') {
  396. if (i-1 >= 0 && pattern.charAt(i-1) == c) {
  397. if (month < 10) {
  398. r += month.toString() + '0';
  399. } else {
  400. r += reverse(month.toString());
  401. }
  402. i--;
  403. } else {
  404. r += reverse(month.toString());
  405. }
  406. } else if (c == 'd') {
  407. if (i-1 >= 0 && pattern.charAt(i-1) == c) {
  408. if (date < 10) {
  409. r += date.toString() + '0';
  410. } else {
  411. r += reverse(date.toString());
  412. }
  413. i--;
  414. } else {
  415. r += reverse(date.toString());
  416. }
  417. } else if (c == 'h'||c=='H') {
  418. if (i-1 >= 0 && pattern.charAt(i-1) == c) {
  419. if (hour < 10) {
  420. r += hour.toString() + '0';
  421. } else {
  422. r += reverse(hour.toString());
  423. }
  424. i--;
  425. } else {
  426. r += reverse(hour.toString());
  427. }
  428. } else if (c == 'm') {
  429. if (i-1 >= 0 && pattern.charAt(i-1) == c) {
  430. if (minute < 10) {
  431. r += minute.toString() + '0';
  432. } else {
  433. r += reverse(minute.toString());
  434. }
  435. i--;
  436. } else {
  437. r += reverse(minute.toString());
  438. }
  439. } else {
  440. r += c;
  441. }
  442. }
  443. return reverse(r);
  444. }
  445. function reverse(str) {
  446. var r = "";
  447. for (var i = str.length - 1; i >= 0; i --) {
  448. r += str.charAt(i);
  449. }
  450. return r;
  451. }
  452. function isDigit(c) {
  453. var isd = c >= '0' && c <= '9';
  454. return isd;
  455. }
  456. function extractDate(datestr, pattern) {
  457. var y = "";
  458. var m = "";
  459. var d = "";
  460. var h = "";
  461. var mi = "";
  462. var j = 0;
  463. for (var i = 0 ; i < pattern.length; i++) {
  464. var c = pattern.charAt(i);
  465. if (c == 'y') {
  466. if (i + 1 < pattern.length && pattern.charAt(i+1) == c) {
  467. if (i + 3 < pattern.length && pattern.charAt(i + 2) == c && pattern.charAt(i+3) == c) {
  468. y = datestr.substr(j, 4);
  469. i += 3;
  470. j += 4;
  471. } else {
  472. var today = __TODAY
  473. y = today.getFullYear().toString().substr(0, 2) + datestr.substr(j, 2);
  474. i += 1;
  475. j += 2;
  476. }
  477. }
  478. } else if (c == 'M') {
  479. if (isDigit(datestr.charAt(j+1))) {
  480. m = datestr.substr(j, 2);
  481. j += 2;
  482. } else {
  483. m = datestr.substr(j, 1);
  484. j ++;
  485. }
  486. if (i + 1 < pattern.length && pattern.charAt(i+1) == c) {
  487. i ++;
  488. }
  489. } else if (c == 'd') {
  490. if (isDigit(datestr.charAt(j+1))) {
  491. d = datestr.substr(j, 2);
  492. j += 2;
  493. } else {
  494. d = datestr.substr(j, 1);
  495. j ++;
  496. }
  497. if (i + 1 < pattern.length && pattern.charAt(i+1) == c) {
  498. i ++;
  499. }
  500. } else if (c == 'h'||c=='H') {
  501. if (isDigit(datestr.charAt(j+1))) {
  502. h = datestr.substr(j, 2);
  503. j += 2;
  504. } else {
  505. h = datestr.substr(j, 1);
  506. j ++;
  507. }
  508. if (i + 1 < pattern.length && pattern.charAt(i+1) == c) {
  509. i ++;
  510. }
  511. } else if (c == 'm') {
  512. if (isDigit(datestr.charAt(j+1))) {
  513. mi = datestr.substr(j, 2);
  514. j += 2;
  515. } else {
  516. mi = datestr.substr(j, 1);
  517. j ++;
  518. }
  519. if (i + 1 < pattern.length && pattern.charAt(i+1) == c) {
  520. i ++;
  521. }
  522. } else {
  523. j ++;
  524. }
  525. }
  526. if (m.charAt(0) == '0') m = m.substring(1);
  527. if (d.charAt(0) == '0') d = d.substring(1);
  528. if (h.charAt(0) == '0') h = h.substring(1);
  529. if (mi.charAt(0) == '0') mi = mi.substring(1);
  530. var today = __TODAY;
  531. 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)];
  532. }
  533. function popCalendar(obj, pattern, allowEmpty, showTime, postBack) {
  534. dateSel.pattern = pattern;
  535. dateSel.showTime = showTime;
  536. if (obj.value.length > 0 && validateDateFormat(obj.value, pattern)) {
  537. var d = extractDate(obj.value, pattern);
  538. if (!isValidDate(d[0], d[1], d[2], d[3], d[4])) {
  539. dateSel.setCurrent();
  540. } else {
  541. dateSel.year = d[0];
  542. dateSel.month = d[1];
  543. dateSel.date = d[2];
  544. dateSel.origYear = d[0];
  545. dateSel.origMonth = d[1];
  546. dateSel.origDate = d[2];
  547. if (dateSel.showTime) {
  548. dateSel.hour = d[3];
  549. dateSel.minute = d[4];
  550. }
  551. }
  552. } else {
  553. dateSel.setCurrent();
  554. }
  555. dateSel.inputObj = obj;
  556. dateSel.allowEmpty = allowEmpty;
  557. dateSel.postBack = postBack;
  558. dateSel.show(obj);
  559. }
  560. function isValidDate(year, month, day, hour, minute) {
  561. if (isNaN(year) || isNaN(month) || isNaN(day)) {
  562. return false;
  563. }
  564. if (month < 0 || month > 11) {
  565. return false;
  566. }
  567. if (day < 1 || day > 31) {
  568. return false;
  569. }
  570. if (year > 3000 || year < 1000) {
  571. return false;
  572. }
  573. if ((month == 3 || month == 5 || month == 8 || month == 10) &&
  574. (day == 31)) {
  575. return false;
  576. }
  577. if (month == 1) {
  578. var leap = (year % 4 == 0 &&
  579. (year % 100 != 0 || year % 400 == 0));
  580. if (day>29 || (day == 29 && !leap)) {
  581. return false;
  582. }
  583. }
  584. if (hour) {
  585. if (hour < 0 && hour > 23) return false;
  586. }
  587. if (minute) {
  588. if (minute<0 && minute>59) return false;
  589. }
  590. return true;
  591. }
  592. function validateDateFormat(value, datePattern) {
  593. var regStr = datePattern.replace(/y/g,'[0-9]').replace(/MM/g,'((0[1-9])|(1[0-2]))')
  594. .replace(/M/g,'([1-9]|(1[0-2]))').replace(/dd/g,'((0[1-9])|([1-2][0-9])|(3[0-1]))')
  595. .replace(/d/g,'([1-9]|([1-2][0-9])|(3[0-1]))')
  596. .replace(/hh/g,'(([0-1][0-9])|(2[0-3]))').replace(/h/g,'(([1-9])|(1[0-9])|(2[0-3]))')
  597. .replace(/HH/g,'(([0-1][0-9])|(2[0-3]))').replace(/H/g,'(([1-9])|(1[0-9])|(2[0-3]))')
  598. .replace(/mm/g,'([0-5][0-9])').replace(/m/g,'(([1-9])|([1-5][0-9]))')
  599. .replace(/\./g,'\\.');
  600. var reg = new RegExp('^' + regStr + '$');
  601. return reg.exec(value) != null;
  602. }
  603. function validateDate(value, datePattern, notify) {
  604. if (value.length == 0) return true;
  605. var v = validateDateFormat(value, datePattern);
  606. var d;
  607. if (v) {
  608. d = extractDate(value, datePattern);
  609. var v = isValidDate(d[0], d[1], d[2], d[3], d[4]);
  610. }
  611. if (! v && notify) {
  612. var today = __TODAY;
  613. alert(msgs['errformat'].replace(/\{0\}/, getDateString(today.getFullYear(), 5, 15, 14, 5, datePattern)));
  614. }
  615. return v;
  616. }
  617. function fixDate(obj, pattern) {
  618. if (obj.tagName == "INPUT" && obj.type == "text") {
  619. if (! validateDate(obj.value, pattern)) {
  620. obj.value = "";
  621. }
  622. }
  623. }