calendar2.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Title: Tigra Calendar
  2. // URL: http://www.softcomplex.com/products/tigra_calendar/
  3. // Version: 3.3 (American date format)
  4. // Date: 09/01/2005 (mm/dd/yyyy)
  5. // Note: Permission given to use this script in ANY kind of applications if
  6. // header lines are left unchanged.
  7. // Note: Script consists of two files: calendar?.js and calendar.html
  8. // if two digit year input dates after this year considered 20 century.
  9. var NUM_CENTYEAR = 30;
  10. // is time input control required by default
  11. var BUL_TIMECOMPONENT = false;
  12. // are year scrolling buttons required by default
  13. var BUL_YEARSCROLL = true;
  14. var calendars = [];
  15. var RE_NUM = /^\-?\d+$/;
  16. function calendar2(obj_target) {
  17. // assigning methods
  18. this.gen_date = cal_gen_date2;
  19. this.gen_time = cal_gen_time2;
  20. this.gen_tsmp = cal_gen_tsmp2;
  21. this.prs_date = cal_prs_date2;
  22. this.prs_time = cal_prs_time2;
  23. this.prs_tsmp = cal_prs_tsmp2;
  24. this.popup = cal_popup2;
  25. // validate input parameters
  26. if (!obj_target)
  27. return cal_error("Error calling the calendar: no target control specified");
  28. if (obj_target.value == null)
  29. return cal_error("Error calling the calendar: parameter specified is not valid target control");
  30. this.target = obj_target;
  31. this.time_comp = BUL_TIMECOMPONENT;
  32. this.year_scroll = BUL_YEARSCROLL;
  33. // register in global collections
  34. this.id = calendars.length;
  35. calendars[this.id] = this;
  36. }
  37. function cal_popup2 (str_datetime) {
  38. if (str_datetime) {
  39. this.dt_current = this.prs_tsmp(str_datetime);
  40. }
  41. else {
  42. this.dt_current = this.prs_tsmp(this.target.value);
  43. this.dt_selected = this.dt_current;
  44. }
  45. if (!this.dt_current) return;
  46. var obj_calwindow = window.open(
  47. './jsp/common/calendar.html?datetime=' + this.dt_current.valueOf()+ '&id=' + this.id,
  48. 'Calendar', 'width=200,height='+(this.time_comp ? 215 : 190)+
  49. ',status=no,resizable=no,top=200,left=200,dependent=yes,alwaysRaised=yes'
  50. );
  51. obj_calwindow.opener = window;
  52. obj_calwindow.focus();
  53. }
  54. // timestamp generating function
  55. function cal_gen_tsmp2 (dt_datetime) {
  56. return(this.gen_date(dt_datetime) + ' ' + this.gen_time(dt_datetime));
  57. }
  58. // date generating function
  59. function cal_gen_date2 (dt_datetime) {
  60. return (
  61. (dt_datetime.getMonth() < 9 ? '0' : '') + (dt_datetime.getMonth() + 1) + "/"
  62. + (dt_datetime.getDate() < 10 ? '0' : '') + dt_datetime.getDate() + "/"
  63. + dt_datetime.getFullYear()
  64. );
  65. }
  66. // time generating function
  67. function cal_gen_time2 (dt_datetime) {
  68. return (
  69. (dt_datetime.getHours() < 10 ? '0' : '') + dt_datetime.getHours() + ":"
  70. + (dt_datetime.getMinutes() < 10 ? '0' : '') + (dt_datetime.getMinutes()) + ":"
  71. + (dt_datetime.getSeconds() < 10 ? '0' : '') + (dt_datetime.getSeconds())
  72. );
  73. }
  74. // timestamp parsing function
  75. function cal_prs_tsmp2 (str_datetime) {
  76. // if no parameter specified return current timestamp
  77. if (!str_datetime)
  78. return (new Date());
  79. // if positive integer treat as milliseconds from epoch
  80. if (RE_NUM.exec(str_datetime))
  81. return new Date(str_datetime);
  82. // else treat as date in string format
  83. var arr_datetime = str_datetime.split(' ');
  84. return this.prs_time(arr_datetime[1], this.prs_date(arr_datetime[0]));
  85. }
  86. // date parsing function
  87. function cal_prs_date2 (str_date) {
  88. var arr_date = str_date.split('/');
  89. if (arr_date.length != 3) return alert ("Invalid date format: '" + str_date + "'.\nFormat accepted is dd-mm-yyyy.");
  90. if (!arr_date[1]) return alert ("Invalid date format: '" + str_date + "'.\nNo day of month value can be found.");
  91. if (!RE_NUM.exec(arr_date[1])) return alert ("Invalid day of month value: '" + arr_date[1] + "'.\nAllowed values are unsigned integers.");
  92. if (!arr_date[0]) return alert ("Invalid date format: '" + str_date + "'.\nNo month value can be found.");
  93. if (!RE_NUM.exec(arr_date[0])) return alert ("Invalid month value: '" + arr_date[0] + "'.\nAllowed values are unsigned integers.");
  94. if (!arr_date[2]) return alert ("Invalid date format: '" + str_date + "'.\nNo year value can be found.");
  95. if (!RE_NUM.exec(arr_date[2])) return alert ("Invalid year value: '" + arr_date[2] + "'.\nAllowed values are unsigned integers.");
  96. var dt_date = new Date();
  97. dt_date.setDate(1);
  98. if (arr_date[0] < 1 || arr_date[0] > 12) return alert ("Invalid month value: '" + arr_date[0] + "'.\nAllowed range is 01-12.");
  99. dt_date.setMonth(arr_date[0]-1);
  100. if (arr_date[2] < 100) arr_date[2] = Number(arr_date[2]) + (arr_date[2] < NUM_CENTYEAR ? 2000 : 1900);
  101. dt_date.setFullYear(arr_date[2]);
  102. var dt_numdays = new Date(arr_date[2], arr_date[0], 0);
  103. dt_date.setDate(arr_date[1]);
  104. if (dt_date.getMonth() != (arr_date[0]-1)) return alert ("Invalid day of month value: '" + arr_date[1] + "'.\nAllowed range is 01-"+dt_numdays.getDate()+".");
  105. return (dt_date)
  106. }
  107. // time parsing function
  108. function cal_prs_time2 (str_time, dt_date) {
  109. if (!dt_date) return null;
  110. var arr_time = String(str_time ? str_time : '').split(':');
  111. if (!arr_time[0]) dt_date.setHours(0);
  112. else if (RE_NUM.exec(arr_time[0]))
  113. if (arr_time[0] < 24) dt_date.setHours(arr_time[0]);
  114. else return cal_error ("Invalid hours value: '" + arr_time[0] + "'.\nAllowed range is 00-23.");
  115. else return cal_error ("Invalid hours value: '" + arr_time[0] + "'.\nAllowed values are unsigned integers.");
  116. if (!arr_time[1]) dt_date.setMinutes(0);
  117. else if (RE_NUM.exec(arr_time[1]))
  118. if (arr_time[1] < 60) dt_date.setMinutes(arr_time[1]);
  119. else return cal_error ("Invalid minutes value: '" + arr_time[1] + "'.\nAllowed range is 00-59.");
  120. else return cal_error ("Invalid minutes value: '" + arr_time[1] + "'.\nAllowed values are unsigned integers.");
  121. if (!arr_time[2]) dt_date.setSeconds(0);
  122. else if (RE_NUM.exec(arr_time[2]))
  123. if (arr_time[2] < 60) dt_date.setSeconds(arr_time[2]);
  124. else return cal_error ("Invalid seconds value: '" + arr_time[2] + "'.\nAllowed range is 00-59.");
  125. else return cal_error ("Invalid seconds value: '" + arr_time[2] + "'.\nAllowed values are unsigned integers.");
  126. dt_date.setMilliseconds(0);
  127. return dt_date;
  128. }
  129. function cal_error (str_message) {
  130. alert (str_message);
  131. return null;
  132. }