You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

598 lines
23 KiB

  1. /**
  2. * add auto hide when mouse moveout
  3. *
  4. * @version 1.0.1
  5. * @date 2010-11-23
  6. * @author coraldane@gmail.com
  7. */
  8. /**
  9. * Date Picker
  10. * @param inputObj The input object want to contain date.
  11. * @param dateFormatStyle Default Date Formatter is "yyyy-MM-dd", you could use your own defined format.
  12. * @param beginDate Default value is 1990-01-01
  13. * @param endDate Default value is 2020-01-01
  14. * @param lang 0(English)|1(Chinese) Default Language is 0(English).
  15. */
  16. function setday(inputObj,dateFormatStyle,beginDate,endDate,lang){
  17. if(null == inputObj){return null;}
  18. new Calendar(inputObj,dateFormatStyle,beginDate,endDate,lang).show();
  19. }
  20. /**
  21. * Month Picker
  22. * @param inputObj The input object want to contain date.
  23. * @param dateFormatStyle Default Date Formatter is "yyyy-MM", you could use your own defined format.
  24. * @param beginDate Default value is 1990-01
  25. * @param endDate Default value is 2020-01
  26. * @param lang 0(English)|1(Chinese) Default Language is 0(English).
  27. */
  28. function setmonth(inputObj,dateFormatStyle,beginDate,endDate,lang){
  29. if(null == inputObj){return null;}
  30. new Calendar(inputObj,dateFormatStyle,beginDate,endDate,lang,"m").show();
  31. }
  32. /**
  33. Calendar Style
  34. */
  35. Calendar.prototype.style = function(){
  36. var strStyle = "<style type='text/css'>";
  37. strStyle += ".calendar {font-size:12px; margin:0;padding:0px;border:1px solid #397EAE;background-color:#DBE7F2;}";
  38. strStyle += ".calendar ul {list-style-type:none; margin:0; padding:0;vertical-align:middle;}";
  39. strStyle += ".calendar li {float:left;}.calendar b{font-weight:bold;}";
  40. strStyle += ".calendar .day li {height:20px;}";
  41. strStyle += ".calendar .day li,.calendar .date li{float:left;width:14.13%;height:20px;line-height:20px;text-align:center;}";
  42. strStyle += ".calendar .day li{font-weight:bold;} .calendar .date li{background-color:#EDF3F9;}";
  43. strStyle += ".calendar .month li{float:left;width:24.8%;height:20px;line-height:20px;text-align:center;background-color:#EDF3F9;}";
  44. strStyle += ".calendar li a{ text-decoration:none; font-family:Tahoma; font-size:11px; color:#333}";
  45. strStyle += ".calendar li:hover {cursor:pointer;color:#f30; text-decoration:none;background-color:#EDF3F9;}";
  46. strStyle += ".calendar .date li:hover, .calendar .month li:hover{cursor:pointer;color:#f30; text-decoration:none;background-color:#DBE7F2;}";
  47. strStyle += ".calendarlihover {color:#f30;text-decoration:none;background-color:#E8F2FE;}";
  48. strStyle += ".calendar li a.hasArticle {font-weight:bold; color:#f60 !important}";
  49. strStyle += ".lastMonthDate, .nextMonthDate {color:#bbb;font-size:11px}";
  50. strStyle += ".selectThisYear, .selectThisMonth{text-decoration:none; margin:0px; color:#000; font-weight:bold}";
  51. strStyle += ".calendar .LastMonth, .calendar .NextMonth{text-decoration:none; color:#000; font-size:18px; font-weight:bold; line-height:16px;}";
  52. strStyle += ".calendarTitle{background:#EDF3F9;text-align:center;height:20px;line-height:20px;clear:both;width:100%;}";
  53. strStyle += ".calendarTitle .mark{text-decoration:none;color:#000;font-family:Tahoma;font-size:18px;font-weight:normal;}";
  54. strStyle += ".today{ background-color:#ffffaa;border:1px solid #f60;padding:0 1px;}";
  55. strStyle += ".today a { color:#f30; }";
  56. strStyle += ".calendarBottom {text-align:center;height:20px;line-height:20px;clear:both;width:100%;border-top:1px solid #ddd;}";
  57. strStyle += ".calendarBottom li{float:left;height:20px;line-height:20px;font-weight:bold;text-align:center;}";
  58. strStyle += "</style>";
  59. return strStyle;
  60. }
  61. /**
  62. //Classic Style
  63. Calendar.prototype.style = function(){
  64. var strStyle = "<style type='text/css'>";
  65. strStyle += ".calendar {font-size:12px; margin:0;padding:0px;border:1px solid #397EAE;}";
  66. strStyle += ".calendar ul {list-style-type:none; margin:0; padding:0;vertical-align:middle;}";
  67. strStyle += ".calendar li {float:left;}.calendar b{font-weight:bold;}";
  68. strStyle += ".calendar .day { background-color:#EDF5FF; height:20px;}";
  69. strStyle += ".calendar .day li,.calendar .date li{ float:left; width:14.13%; height:20px; line-height:20px; text-align:center}";
  70. strStyle += ".calendar .month li{ float:left; width:24.8%; height:20px; line-height:20px; text-align:center}";
  71. strStyle += ".calendar li a{ text-decoration:none; font-family:Tahoma; font-size:11px; color:#333}";
  72. strStyle += ".calendar li:hover {cursor:pointer;color:#f30; text-decoration:none;background-color:#E8F2FE;}";
  73. strStyle += ".calendarlihover {color:#f30;text-decoration:none;background-color:#E8F2FE;}";
  74. strStyle += ".calendar li a.hasArticle {font-weight:bold; color:#f60 !important}";
  75. strStyle += ".lastMonthDate, .nextMonthDate {color:#bbb;font-size:11px}";
  76. strStyle += ".selectThisYear, .selectThisMonth{text-decoration:none; margin:0px; color:#000; font-weight:bold}";
  77. strStyle += ".calendar .LastMonth, .calendar .NextMonth{text-decoration:none; color:#000; font-size:18px; font-weight:bold; line-height:16px;}";
  78. strStyle += ".calendarTitle {text-align:center;height:20px;line-height:20px;clear:both;width:100%;}";
  79. strStyle += ".calendarTitle .mark{text-decoration: none;color:#000;font-family:Tahoma;font-size:18px;font-weight:normal;line-height: 16px;}";
  80. strStyle += ".today{ background-color:#ffffaa;border:1px solid #f60;padding:0 1px;}";
  81. strStyle += ".today a { color:#f30; }";
  82. strStyle += ".calendarBottom {text-align:center;height:20px;line-height:20px;clear:both;width:100%;border-top:1px solid #ddd;}";
  83. strStyle += ".calendarBottom li{float:left;height:20px;line-height:20px;font-weight:bold;text-align:center;}";
  84. strStyle += "</style>";
  85. return strStyle;
  86. }
  87. */
  88. function getFrameDocument(frame){
  89. if ( frame.contentDocument ) { // DOM
  90. var doc = frame.contentDocument;
  91. } else if (frame.contentWindow) { // IE win
  92. var doc = frame.contentWindow.document;
  93. }
  94. return doc;
  95. }
  96. /**
  97. * Parse Date value from String
  98. * @param format the pattern of date
  99. */
  100. String.prototype.toDate = function(format){
  101. if(null == format) format="yyyy-MM-dd";
  102. var pattern = format.replace("yyyy", "(\\~1{4})").replace("yy", "(\\~1{2})")
  103. .replace("MM", "(\\~1{2})").replace("M", "(\\~1{1,2})")
  104. .replace("dd", "(\\~1{2})").replace("d", "(\\~1{1,2})").replace(/~1/g, "d");
  105. var returnDate;
  106. if (new RegExp(pattern).test(this)) {
  107. var yPos = format.indexOf("yyyy");
  108. var mPos = format.indexOf("MM");
  109. var dPos = format.indexOf("dd");
  110. if (mPos == -1) mPos = format.indexOf("M");
  111. if (yPos == -1) yPos = format.indexOf("yy");
  112. if (dPos == -1) dPos = format.indexOf("d");
  113. var pos = new Array(yPos + "y", mPos + "m", dPos + "d");
  114. var data = { y: 0, m: 0, d: 1};
  115. var m = this.match(pattern);
  116. for (var i = 1; i < m.length; i++) {
  117. if (i == 0) return;
  118. var flag = pos[i - 1].split('')[1];
  119. data[flag] = m[i];
  120. //alert(pos[i-1] + ",flag:"+flag + ",i:" + i + "," + data[flag]);
  121. };
  122. if (data.y.toString().length == 2) {
  123. data.y = parseInt("20" + data.y);
  124. }
  125. data.m = data.m - 1;
  126. returnDate = new Date(data.y, data.m, data.d);
  127. }
  128. if (returnDate == null || isNaN(returnDate)) returnDate = new Date();
  129. return returnDate;
  130. };
  131. /**
  132. * Date Format
  133. * @param style date format like 'yyyyMMdd'
  134. */
  135. Date.prototype.format = function(style) {
  136. var o = {
  137. "M+" : this.getMonth() + 1, //month
  138. "d+" : this.getDate(), //day
  139. "h+" : this.getHours(), //hour
  140. "m+" : this.getMinutes(), //minute
  141. "s+" : this.getSeconds(), //second
  142. "w+" : "日一二三四五六".charAt(this.getDay()), //week
  143. "q+" : Math.floor((this.getMonth() + 3) / 3), //quarter
  144. "S" : this.getMilliseconds() //millisecond
  145. }
  146. if(/(y+)/.test(style)) {
  147. style = style.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  148. }
  149. for(var k in o){
  150. if(new RegExp("("+ k +")").test(style)){
  151. style = style.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  152. }
  153. }
  154. return style;
  155. };
  156. /**
  157. Date add by interval
  158. @param interval y Year,m Month,d Day,w Week
  159. @param number
  160. */
  161. Date.prototype.dateAdd = function(interval, number) {
  162. switch (interval) {
  163. case "y":
  164. return new Date(this.getFullYear() + number, this.getMonth(), this.getDate());
  165. break;
  166. case "m":
  167. return new Date(this.getFullYear(), this.getMonth() + number, checkDate(this.getFullYear(), this.getMonth() + number, this.getDate()));
  168. break;
  169. case "d":
  170. return new Date(this.getFullYear(), this.getMonth(), this.getDate() + number);
  171. break;
  172. case "w":
  173. return new Date(this.getFullYear(), this.getMonth(), 7 * number + this.getDate());
  174. break;
  175. }
  176. }
  177. function checkDate(year, month, date){
  178. var enddate = ["31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"];
  179. var returnDate = "";
  180. if (year % 4 == 0) {
  181. enddate[1] = "29";
  182. }
  183. if (date > enddate[month]) {
  184. returnDate = enddate[month];
  185. } else {
  186. returnDate = date;
  187. }
  188. return returnDate;
  189. }
  190. /**
  191. Calendar language pack
  192. default support english and chinese,if you want to add some other language, please extend it.
  193. */
  194. Calendar.language = {
  195. "title":[["",""],["年","月"]],
  196. "months":[["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"],
  197. ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
  198. ],
  199. "weeks":[["S","M","T","W","T","F","S"],
  200. ["日","一","二","三","四","五","六"]
  201. ],
  202. weekday:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],
  203. "clear":[["Clear"], ["清空"]],
  204. "today":[["Today","Current"], ["今天","当月"]],
  205. "close":[["Close"], ["关闭"]]
  206. };
  207. /**
  208. * Calendar class
  209. * @param beginDate 1990-01-01
  210. * @param endDate 2020-01-01
  211. * @param lang 0(English)|1(Chinese)
  212. * @param dateFormatStyle "yyyy-MM-dd";
  213. * @param type d Date Picker/m Month Picker
  214. * @version 2010-08-20
  215. * @author Coral(coraldane@gmail.com)
  216. * @update
  217. */
  218. function Calendar(inputObj, dateFormatStyle, beginDate, endDate, lang, type) {
  219. this.beginDate = "1900-01-01".toDate();
  220. this.endDate = "2020-01-01".toDate();
  221. this.lang = 0; //default language
  222. this.type = "d";
  223. this.dateFormatStyle = "yyyy-MM-dd";
  224. if(null != type){
  225. this.type = type;
  226. if("m" == this.type){
  227. this.dateFormatStyle = "yyyy-MM";
  228. }
  229. }
  230. if (dateFormatStyle != null){
  231. this.dateFormatStyle = dateFormatStyle;
  232. }
  233. this.currentDate = new Date();
  234. var currDate = new Date();
  235. if(null != inputObj.value && "" != inputObj.value){
  236. currDate = inputObj.value.toDate(this.dateFormatStyle);
  237. }
  238. if(null != currDate){
  239. this.date = currDate;
  240. }else{
  241. this.date = new Date();
  242. }
  243. if (null != beginDate){
  244. this.beginDate = beginDate;
  245. }
  246. if(null != endDate){
  247. this.endDate = endDate;
  248. }
  249. if (lang != null){
  250. this.lang = lang;
  251. }
  252. this.dateControl = inputObj;
  253. this.panel = document.getElementById("calendarPanel");
  254. this.iframe = document.getElementById("calendarIframe");
  255. this.isFocus = false;
  256. this.draw();
  257. }
  258. Calendar.prototype.draw = function() {
  259. var currDate = this.date.format("yyyy-MM").toDate("yyyy-MM");
  260. if(currDate < this.beginDate){
  261. this.date = this.beginDate;
  262. }
  263. if(currDate > this.endDate){
  264. this.date = this.endDate;
  265. }
  266. this.year = this.date.getFullYear();
  267. this.month = this.date.getMonth();
  268. var head = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' +
  269. '<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' +
  270. this.style() + '</head><body style="padding:0px;margin:0px;">';
  271. var thisMonthFirstDate = this.date.dateAdd("d",1-this.date.getDate());
  272. var lastMonthEndDate = thisMonthFirstDate.dateAdd("d",-1);
  273. var lastMonthDate = thisMonthFirstDate.getDay();
  274. lastMonthEndDate = lastMonthEndDate.getDate();
  275. var thisMonthLastDate = thisMonthFirstDate.dateAdd("m",1).dateAdd("d",-1);
  276. var thisMonthEndDate = thisMonthLastDate.getDate();
  277. var thisMonthEndDay = thisMonthLastDate.getDay();
  278. var lis = "<div id='calendar' class='calendar' style='width:";
  279. if("d" == this.type){
  280. lis += "150";
  281. }else{
  282. lis += "120";
  283. }
  284. lis += "px;'>";
  285. lis += "<div class='calendarTitle'><ul>";
  286. lis += "<li id='PrevYear' class='mark' style='width:12%;' title='Previous Year'>&laquo;</li>";
  287. if("d" == this.type){
  288. lis += "<li id='PrevMonth' class='mark' style='width:12%;' title='Previous Month'>&lsaquo;</li>";
  289. lis += "<li style='width:30%;'><span id='selectThisYear' class='selectThisYear'>" + this.date.getFullYear() + "</span>" + Calendar.language["title"][this.lang][0] + "</li>";
  290. if(0 == this.lang){
  291. lis += "<li style='width:20%;'><span id='selectThisMonth' class='selectThisMonth'>" + Calendar.language["months"][this.lang][this.date.getMonth()] + "</span></li>";
  292. }else{
  293. lis += "<li style='width:20%;'><span id='selectThisMonth' class='selectThisMonth'>" + (this.date.getMonth() +1) + "</span>" + Calendar.language["title"][this.lang][1] + "</li>";
  294. }
  295. lis += "<li id='NextMonth' class='mark' style='width:12%;' title='Next Month'>&rsaquo;</li>";
  296. lis += "<li id='NextYear' class='mark' style='width:12%;' title='Next Year'>&raquo;</li></ul></div>";
  297. lis += "<div class='calendarBody'>";
  298. lis += "<ul class='day'>";
  299. for(var i=0;i<Calendar.language.weeks[this.lang].length;i++){
  300. lis += "<li title='" + Calendar.language.weekday[i] + "'>" + Calendar.language.weeks[this.lang][i] + "</li>";
  301. }
  302. lis += "</ul><ul class='date' id='thisMonthDate'>";
  303. var lastMonthLis = "";
  304. for (var i = 0; i < lastMonthDate; i++) { // Last Month's Date
  305. //alert(lastMonthDate + "," + lastMonthEndDate);
  306. lastMonthLis = "<li class='lastMonthDate'>" + lastMonthEndDate + "</li>" + lastMonthLis;
  307. lastMonthEndDate--;
  308. }
  309. lis += lastMonthLis;
  310. for (i = 1; i <= thisMonthEndDate; i++) { // Current Month's Date
  311. var currentDate = thisMonthFirstDate.dateAdd("d",(i-1));
  312. if(currentDate < this.beginDate || currentDate > this.endDate){
  313. lis += "<li class='lastMonthDate'>" + i + "</li>";
  314. continue;
  315. }
  316. lis += "<li class='thisMonth' title='" + currentDate.format("yyyy-MM-dd") + "'><a href='javascript:void(0);' ";
  317. if(currentDate.format("yyyy-MM-dd") == (this.date).format("yyyy-MM-dd")){
  318. lis += "class='today' ";
  319. }
  320. lis += ">" + i + "</a></li>";
  321. }
  322. var j = 1;
  323. for (i = thisMonthEndDay; i < 6; i++) { // Next Month's Date
  324. lis += "<li class='nextMonthDate'>" + j + "</li>";
  325. j++;
  326. }
  327. lis += "</ul>"
  328. lis += "</div>";//close calendarBody
  329. lis += "<div class='calendarBottom'><ul>";
  330. lis += "<li id='emptyCalendar' style='width:27%;' title='Clear'>" + Calendar.language.clear[this.lang] +"</li>";
  331. lis += "<li id='selectCurrent' style='width:45%;' title='Today'>" + Calendar.language.today[this.lang][0] +"</li>";
  332. }else{
  333. lis += "<li style='width:74%;'><span id='selectThisYear' class='selectThisYear'>" + this.date.getFullYear() + "</span>" + Calendar.language["title"][this.lang][0] + "</li>";
  334. lis += "<li id='NextYear' class='mark' style='width:12%;' title='Next Year'>&raquo;</li></ul></div>";
  335. lis += "<div class='calendarBody'>";
  336. lis += "</ul><ul class='month' id='thisMonth'>";
  337. for(var i=1; i<=12; i++){
  338. var currentDate = (this.year + "-" + (i>9?i:"0"+i)).toDate("yyyy-MM");
  339. if(currentDate < this.beginDate || currentDate > this.endDate){
  340. lis += "<li class='lastMonthDate'>" + i + "</li>";
  341. continue;
  342. }
  343. lis += "<li class='thisMonth' title='" + this.year + "-" + (i>9?i:"0"+i) + "-01'><a href='javascript:void(0);'";
  344. if((this.year+"-"+(i>9?i:"0"+i)) == (this.date).format("yyyy-MM")){
  345. lis += " class='today' ";
  346. }
  347. lis += ">" + i + "</a></li>";
  348. }
  349. lis += "</ul>"
  350. lis += "</div>";//close calendarBody
  351. lis += "<div class='calendarBottom'><ul>";
  352. lis += "<li id='emptyCalendar' style='width:27%;' title='Clear'>" + Calendar.language.clear[this.lang] +"</li>";
  353. lis += "<li id='selectCurrent' style='width:45%;' title='Current Month'>" + Calendar.language.today[this.lang][1] +"</li>";
  354. }
  355. lis += "<li id='closeCalendar' style='width:27%;' title='Close'>" + Calendar.language.close[this.lang] +"</li>";
  356. lis += "</ul></div>";//close calendarBottom
  357. lis += "</div>";//close calendar
  358. lis += "</body></html>";
  359. var doc = getFrameDocument(this.iframe);
  360. doc.writeln(head);
  361. doc.writeln(lis);
  362. doc.close();
  363. this.document = doc;
  364. this.bingEvent();
  365. }
  366. /**
  367. * Bind Click Event into Calendar
  368. */
  369. Calendar.prototype.bingEvent = function(){
  370. var calendar = this;
  371. this.setAutoHeight();
  372. this.panel.onmouseover = function(){calendar.isFocus = true;}
  373. this.panel.onmouseout = function(){calendar.isFocus = false;}
  374. this.dateControl.onblur = function(){
  375. if(!calendar.isFocus){
  376. calendar.hide();
  377. }
  378. }
  379. this.getElementById("selectCurrent").onclick = function(){
  380. calendar.date = new Date();
  381. calendar.valueSelected(calendar.date);
  382. calendar.hide();
  383. }
  384. this.getElementById("emptyCalendar").onclick = function(){calendar.dateControl.value = "";calendar.hide();}
  385. this.getElementById("closeCalendar").onclick = function(){calendar.hide();}
  386. this.getElementById("PrevYear").onclick = function(){
  387. calendar.date = calendar.date.dateAdd("y",-1);
  388. calendar.draw();
  389. }
  390. if(this.getElementById("PrevMonth")){
  391. this.getElementById("PrevMonth").onclick = function(){
  392. calendar.date = calendar.date.dateAdd("m",-1);
  393. calendar.draw();
  394. }
  395. this.getElementById("NextMonth").onclick = function(){
  396. calendar.date = calendar.date.dateAdd("m",1);
  397. calendar.draw();
  398. }
  399. }
  400. this.getElementById("NextYear").onclick = function(){
  401. calendar.date = calendar.date.dateAdd("y",1);
  402. calendar.draw();
  403. }
  404. this.getElementById("selectThisYear").onclick = function(){calendar.selectThisYear();}
  405. if("d" == this.type){
  406. this.getElementById("selectThisMonth").onclick = function(){calendar.selectThisMonth();}
  407. }
  408. var elements = getElementsByClassName(this.document, "li", "thisMonth");
  409. for(var i=0; i<elements.length; i++){
  410. elements[i].onclick = function(){
  411. calendar.date = this.title.toDate();
  412. calendar.valueSelected(calendar.date);
  413. calendar.hide();
  414. }
  415. }
  416. }
  417. Calendar.prototype.selectThisYear = function(){
  418. var calendar = this;
  419. var curYear = this.date.getFullYear();
  420. var beginYear = this.beginDate.getFullYear();
  421. var endYear = this.endDate.getFullYear();
  422. var spanObj = this.getElementById("selectThisYear");
  423. var selectStr = "<select style='font-size:10px;'>";
  424. for(var i = endYear; i >= beginYear; i--){
  425. selectStr += "<option value='" + i + "'>" + i + "</option>";
  426. }
  427. selectStr += "</select>";
  428. spanObj.innerHTML = selectStr;
  429. var selectYearObj = spanObj.childNodes(0);
  430. selectYearObj.value = curYear;
  431. selectYearObj.onchange = function(){
  432. calendar.date.setFullYear(selectYearObj.value);
  433. calendar.draw();
  434. }
  435. }
  436. Calendar.prototype.selectThisMonth = function(){
  437. var calendar = this;
  438. var curMonth = this.date.getMonth() + 1;
  439. var curYear = this.date.getFullYear();
  440. var endYear = this.endDate.getFullYear();
  441. var endMonth = 12;
  442. if(curYear == endYear){
  443. endMonth = this.endDate.getMonth + 1;
  444. }
  445. var spanObj = this.getElementById("selectThisMonth");
  446. var selectStr = "<select style='font-size:10px;'>";
  447. for(var i = 1; i <= endMonth; i++){
  448. selectStr += "<option value='" + i + "'>" + Calendar.language["months"][this.lang][i-1] + "</option>";
  449. }
  450. selectStr += "</select>";
  451. spanObj.innerHTML = selectStr;
  452. var selectMonthObj = spanObj.childNodes(0);
  453. selectMonthObj.value = curMonth;
  454. selectMonthObj.onchange = function(){
  455. calendar.date.setMonth(selectMonthObj.value-1);
  456. calendar.draw();
  457. }
  458. }
  459. Calendar.prototype.valueSelected = function(date){
  460. this.dateControl.value = date.format(this.dateFormatStyle);
  461. }
  462. /**
  463. * Set Auto Height for Calendar Panel Div
  464. */
  465. Calendar.prototype.setAutoHeight = function(){
  466. var height = this.document.body.scrollHeight;
  467. var width = this.getElementById("calendar").style.width;
  468. width = (parseInt(width.substr(0,width.length-1)) + 2) + "px";
  469. this.iframe.style.height = height;
  470. this.panel.style.height = height;
  471. this.panel.style.width = width;
  472. }
  473. //Extend document.getElementById(id)
  474. Calendar.prototype.getElementById = function(id){
  475. if (typeof(id) != "string" || id == "") return null;
  476. if(null == this.document) return null;
  477. if (this.document.getElementById) return this.document.getElementById(id);
  478. if (this.document.all) return this.document.all(id);
  479. try {return eval(id);} catch(e){ return null;}
  480. }
  481. //Extend object.getElementsByTagName(tagName)
  482. Calendar.prototype.getElementsByTagName = function(tagName){
  483. if(null == this.document) return null;
  484. if (this.document.getElementsByTagName) return this.document.getElementsByTagName(tagName);
  485. if (this.document.all) return this.document.all.tags(tagName);
  486. }
  487. /**
  488. * Find a HTML Object by TagName and className
  489. * @param oElm parentNode Object
  490. * @param strTagName tag name want to find
  491. * @param strClassName class name
  492. */
  493. function getElementsByClassName(oElm, strTagName, strClassName){
  494. var arrElements = (strTagName == "*" && oElm.all)? oElm.all:oElm.getElementsByTagName(strTagName);
  495. var arrReturnElements = new Array();
  496. strClassName = strClassName.replace(/\-/g, "\\-");
  497. var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
  498. var oElement;
  499. for(var i=0; i < arrElements.length; i++){
  500. oElement = arrElements[i];
  501. if(oRegExp.test(oElement.className)){
  502. arrReturnElements.push(oElement);
  503. }
  504. }
  505. return (arrReturnElements)
  506. }
  507. //find the absolute position
  508. Calendar.prototype.getAbsPoint = function (e){
  509. var x = e.offsetLeft;
  510. var y = e.offsetTop;
  511. while(e = e.offsetParent){
  512. x += e.offsetLeft;
  513. y += e.offsetTop;
  514. }
  515. return {"x": x, "y": y};
  516. }
  517. //显示日历
  518. Calendar.prototype.show = function () {
  519. var xy = this.getAbsPoint(this.dateControl);
  520. this.panel.style.left = xy.x + "px";
  521. this.panel.style.top = (xy.y + this.dateControl.offsetHeight) + "px";
  522. this.setDisplayStyle("select", "hidden");
  523. this.panel.style.visibility = "visible";
  524. }
  525. //Hide Calendar
  526. Calendar.prototype.hide = function() {
  527. this.setDisplayStyle("select", "visible");
  528. this.panel.style.visibility = "hidden";
  529. this.isFocus = false;
  530. }
  531. //Set Calendar Picker visible or invisible
  532. Calendar.prototype.setDisplayStyle = function(tagName, style) {
  533. var tags = this.getElementsByTagName(tagName)
  534. for(var i = 0; i < tags.length; i++) {
  535. if (tagName.toLowerCase() == "select" &&
  536. (tags[i].name == "calendarYear" ||
  537. tags[i].name == "calendarMonth")){
  538. continue;
  539. }
  540. tags[i].style.visibility = style;
  541. }
  542. }
  543. document.write('<div id="calendarPanel" style="position:absolute;visibility:hidden;z-index:9999;background-color:#FFFFFF;font-size:12px;width:20px;">');
  544. document.write("<iframe id='calendarIframe' scrolling='no' frameborder='0' width='100%' height='100%'></iframe></div>");