datetime.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /********************************************************************************************************************************
  2. * Licensed Materials - Property of IBM *
  3. * *
  4. * IBM Cognos Products: HTS *
  5. * *
  6. * (C) Copyright IBM Corp. 2005, 2010 *
  7. * *
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. *
  9. *********************************************************************************************************************************/
  10. function hts_datetime(dateFrom,dateTo,msgs)
  11. {
  12. this.m_dateFrom = dateFrom;
  13. this.m_dateTo = dateTo;
  14. this.m_msgs = msgs;
  15. this.m_dateTimeUtil = new hts_dateTimeUtil();
  16. }
  17. hts_datetime.prototype = {
  18. checkDate: function(fromDate,newDate) {
  19. var dateIsTheSame=true;
  20. iYear = newDate.substring(0,4);
  21. iMonth = newDate.substring(5, 7);
  22. iDay = newDate.substring(8, 10);
  23. if (fromDate) {
  24. newDateFrom = this.getDateFromValue();
  25. iNewStartYear = newDateFrom.substring(0,4);
  26. iNewStartMonth = newDateFrom.substring(5, 7);
  27. iNewStartDay = newDateFrom.substring(8, 10);
  28. dateIsTheSame = iYear == iNewStartYear && iMonth == iNewStartMonth && iDay == iNewStartDay;
  29. }
  30. else {
  31. newDateTo = this.getDateToValue();
  32. iNewEndYear = newDateTo.substring(0,4);
  33. iNewEndMonth = newDateTo.substring(5, 7);
  34. iNewEndDay = newDateTo.substring(8, 10);
  35. dateIsTheSame = iYear == iNewEndYear && iMonth == iNewEndMonth && iDay == iNewEndDay;
  36. }
  37. return dateIsTheSame;
  38. },
  39. //check that a date is not the current date
  40. checkCurrentDate: function(newDate) {
  41. currentDate=this.getCurrentDateValue();
  42. iCurrentYear = currentDate.substring(0,4);
  43. iCurrentMonth = currentDate.substring(5, 7);
  44. iCurrentDay = currentDate.substring(8, 10);
  45. iNewYear = newDate.substring(0,4);
  46. iNewMonth = newDate.substring(5, 7);
  47. iNewDay = newDate.substring(8, 10);
  48. isTodaysDate = iCurrentYear == iNewYear && iCurrentMonth == iNewMonth && iCurrentDay == iNewDay;
  49. return isTodaysDate;
  50. },
  51. getCurrentDateValue: function() {
  52. return this.m_dateTimeUtil.getCurrentDateValue();
  53. },
  54. getDateFromFormatValue: function() {
  55. if (this.m_dateFrom != null && this.m_dateFrom != '')
  56. {
  57. return window['pickerControl'+ this.m_dateFrom].sGetFormatValue();
  58. } else
  59. {
  60. return '';
  61. }
  62. },
  63. getDateToFormatValue: function() {
  64. if (this.m_dateTo != null && this.m_dateTo != '')
  65. {
  66. return window['pickerControl'+ this.m_dateTo].sGetFormatValue();
  67. } else
  68. {
  69. return '';
  70. }
  71. },
  72. getDateFromValue: function() {
  73. if (this.m_dateFrom != null && this.m_dateFrom != '')
  74. {
  75. var picker = window['pickerControl'+ this.m_dateFrom];
  76. if (picker.isValid()) {
  77. return picker.sGetValue();
  78. }
  79. else {
  80. //if an invalid date has been entered sGetValue will return a boolean so in this instance we return an zeroed date string
  81. return "00000000";
  82. }
  83. } else
  84. {
  85. return '';
  86. }
  87. },
  88. getDateToValue: function() {
  89. if (this.m_dateTo != null && this.m_dateTo != '')
  90. {
  91. var picker = window['pickerControl'+ this.m_dateTo];
  92. if (picker.isValid()) {
  93. return picker.sGetValue();
  94. }
  95. else {
  96. //if an invalid date has been entered sGetValue will return a boolean so in this instance we return an zeroed date string
  97. return "00000000";
  98. }
  99. } else
  100. {
  101. return '';
  102. }
  103. },
  104. setDateFromValue: function (dateStr) {
  105. var picker = window['pickerControl'+ this.m_dateFrom];
  106. var ymd = this.convertDateToYMD(dateStr);
  107. picker.setValue(ymd);
  108. },
  109. setDateToValue: function (dateStr) {
  110. var picker = window['pickerControl'+ this.m_dateTo];
  111. var ymd = this.convertDateToYMD(dateStr);
  112. picker.setValue(ymd);
  113. },
  114. convertDateToYMD: function (dateStr) {
  115. var ymd="";
  116. if (dateStr!=='') {
  117. iYear = dateStr.substring(0,4);
  118. iMonth = dateStr.substring(5, 7);
  119. iDay = dateStr.substring(8, 10);
  120. ymd = iYear+'-'+iMonth+'-'+iDay;
  121. }
  122. return ymd;
  123. },
  124. simpleValidate:function(validateFrom){
  125. if (validateFrom) {
  126. if ( !this.isDateValid(this.m_dateFrom) )
  127. {
  128. alert (this.m_msgs['MSG_ERR_INVALID_START_DATE']);
  129. return false;
  130. }
  131. }
  132. else {
  133. if ( !this.isDateValid(this.m_dateTo) )
  134. {
  135. alert (this.m_msgs['MSG_ERR_INVALID_END_DATE']);
  136. return false;
  137. }
  138. }
  139. return (true);
  140. },
  141. isDateValid: function(str)
  142. {
  143. return this.m_dateTimeUtil.isDateValid(str);
  144. },
  145. validateFromDate: function () {
  146. if (!this.simpleValidate(true))
  147. {
  148. return false;
  149. }
  150. // Check the Start Date Time.
  151. if (this.getDateFromValue() == '')
  152. {
  153. alert (this.m_msgs['MSG_ERR_INVALID_START_DATE']);
  154. return false;
  155. }
  156. /*if (this.checkCurrentDate(this.getDateFromValue())) {
  157. var dateFrom = this.getDateFromValue();
  158. //set the hours and minutes to 23:59 if the start date is todays date
  159. var newDateFrom = dateFrom.replace("00:00.000","23:59.000");
  160. alert('The start date has been set to the current date. The deadline will expire at 23:59 today');
  161. this.setDateFromValue(newDateFrom);
  162. //alert (this.m_msgs['MSG_ERR_START_CANNOT_USE_CURRENT_DATE']);
  163. //return false;
  164. }*/
  165. if (this.getDateToValue()!='' && !(this.validateDateSequence()))
  166. {
  167. return false;
  168. }
  169. return (true);
  170. },
  171. validateToDate: function () {
  172. if (!this.simpleValidate(false))
  173. {
  174. return false;
  175. }
  176. // Check the End Date Time.
  177. if (this.getDateToValue() == '')
  178. {
  179. alert (this.m_msgs['MSG_ERR_INVALID_END_DATE']);
  180. return false;
  181. }
  182. if (this.checkCurrentDate(this.getDateToValue())) {
  183. alert (this.m_msgs['MSG_ERR_COMPLETE_CANNOT_USE_CURRENT_DATE']);
  184. return false;
  185. }
  186. if (this.getDateFromValue!='' && !(this.validateDateSequence())) {
  187. return false;
  188. }
  189. return (true);
  190. },
  191. validateDates: function()
  192. {
  193. if (!this.validateFromDate()) {
  194. return false;
  195. }
  196. if (!this.validateToDate()) {
  197. return false;
  198. }
  199. // We could create date objects and compare them but that involves too much work to format
  200. // our date values to JS acceptable format.
  201. if (!(this.validateDateSequence()))
  202. {
  203. return false;
  204. }
  205. return(true);
  206. },
  207. // ..............................................................................
  208. // Checks for end date being earlier than or the same as the start date.
  209. validateDateSequence: function()
  210. {
  211. iStartYear = this.getDateFromValue().substring(0,4);
  212. iStartMonth = this.getDateFromValue().substring(5, 7);
  213. iStartDay = this.getDateFromValue().substring(8, 10);
  214. iEndYear = this.getDateToValue().substring(0,4);
  215. iEndMonth = this.getDateToValue().substring(5, 7);
  216. iEndDay = this.getDateToValue().substring(8, 10);
  217. var errMsg = 'MSG_ERR_INCOMPATIBLE_DATES';
  218. var bRetVal = this.m_dateTimeUtil.getDateSequence(iStartDay, iStartMonth, iStartYear, iEndDay, iEndMonth, iEndYear);
  219. if(bRetVal == 0)
  220. {
  221. //end date equal to start date
  222. errMsg = 'MSG_ERR_EQUAL_DATES';
  223. }
  224. if(bRetVal != 1)
  225. {
  226. alert(this.m_msgs[errMsg]);
  227. return false
  228. }
  229. else
  230. {
  231. //end date later then start date
  232. return true;
  233. }
  234. }
  235. }
  236. /*
  237. Utility to validate a single date.
  238. The constructor expects the same id of the that is passed when
  239. creating the date picker i.e. the id of the field passed to the
  240. genSelectDateHTML(...) function.
  241. e.g.'_THIS_'+'taskOptions_dueDate'
  242. */
  243. function hts_datePickerUtil(dateTime)
  244. {
  245. this.dateTime = dateTime;
  246. this.m_dateTimeUtil = new hts_dateTimeUtil();
  247. }
  248. hts_datePickerUtil.prototype = {
  249. isDateValid: function()
  250. {
  251. return this.m_dateTimeUtil.isDateValid(this.dateTime);
  252. },
  253. isDateEmpty: function()
  254. {
  255. var isDateEmpty = false;
  256. if(this.getDateValue() == '')
  257. {
  258. isDateEmpty = true;
  259. }
  260. return isDateEmpty;
  261. },
  262. getDateValue: function()
  263. {
  264. var dateValue = '';
  265. if (this.dateTime != null && this.dateTime != '')
  266. {
  267. dateValue = window['pickerControl'+ this.dateTime].sGetValue();
  268. }
  269. return dateValue;
  270. },
  271. //NOTE: An empty value will also be compared, to check if a date is set or not
  272. // use isEmpty()
  273. isDateLaterThanCurrentDate: function()
  274. {
  275. var isDateLaterThanCurrentDate = false;
  276. //check if the date is valid
  277. if(this.isDateValid())
  278. {
  279. iDateYear = this.getDateValue().substring(0,4);
  280. iDateMonth = this.getDateValue().substring(5, 7);
  281. iDateDay = this.getDateValue().substring(8, 10);
  282. isDateLaterThanCurrentDate = this.m_dateTimeUtil.isDateLaterThanCurrentDate(iDateDay, iDateMonth, iDateYear);
  283. }
  284. return isDateLaterThanCurrentDate;
  285. },
  286. //NOTE: An empty value will also be compared, to check if a date is set or not
  287. // use isEmpty()
  288. isDateLaterThanOrEqualToCurrentDate: function()
  289. {
  290. var isDateLaterThanCurrentDate = false;
  291. //check if the date is valid
  292. if(this.isDateValid())
  293. {
  294. iDateYear = this.getDateValue().substring(0,4);
  295. iDateMonth = this.getDateValue().substring(5, 7);
  296. iDateDay = this.getDateValue().substring(8, 10);
  297. isDateLaterThanCurrentDate = this.m_dateTimeUtil.isDateLaterThanOrEqualToCurrentDate(iDateDay, iDateMonth, iDateYear);
  298. }
  299. return isDateLaterThanCurrentDate;
  300. }
  301. }
  302. /*
  303. Generic date time utilities. Each function should be a self
  304. contained function with each parameter passed explicitly.
  305. */
  306. function hts_dateTimeUtil()
  307. {
  308. }
  309. hts_dateTimeUtil.prototype = {
  310. isDateValid: function(str)
  311. {
  312. var t = true;
  313. if (str != '')
  314. {
  315. t = window['pickerControl' + str].isValid();
  316. }
  317. return t;
  318. },
  319. getCurrentDateValue: function()
  320. {
  321. //convert to something like this : "2009-04-22"
  322. currentDate = new Date();
  323. year = this.getYear(currentDate);
  324. month = this.getMonth(currentDate);
  325. day = this.getDay(currentDate);
  326. return year+"-"+month+"-"+day;
  327. },
  328. // ..............................................................................
  329. // Checks which of the end date or start date is earlier.
  330. // returns -1 if endate is earlier the start date
  331. // 0 if the end date and start date are equal
  332. // 1 if end date is later then the start date
  333. // NOTE: this function does not validate the dates, it just compares
  334. // the passed in year, month and day values assuming they are
  335. // a part of a valid Date. Use other validate functions to
  336. // validate if the date is valid or not.
  337. //...............................................................................
  338. getDateSequence: function(iStartDay, iStartMonth, iStartYear, iEndDay, iEndMonth, iEndYear)
  339. {
  340. var bRetVal = 1;
  341. if (iEndYear < iStartYear)
  342. {
  343. bRetVal = -1;
  344. }
  345. else if (iEndYear == iStartYear)
  346. {
  347. if (iEndMonth < iStartMonth)
  348. {
  349. bRetVal = -1;
  350. }
  351. else if (iEndMonth == iStartMonth)
  352. {
  353. if (iEndDay < iStartDay)
  354. {
  355. bRetVal = -1;
  356. }
  357. else if (iEndDay == iStartDay)
  358. {
  359. bRetVal = 0;
  360. }
  361. }
  362. }
  363. return bRetVal;
  364. },
  365. //NOTE: An empty value will also be compared, to check if a date is set or not
  366. // use isEmpty()
  367. isDateLaterThanCurrentDate: function(iDateDay, iDateMonth, iDateYear)
  368. {
  369. currentDate = new Date();
  370. var dateSequence = this.getDateSequence(this.getDay(currentDate), this.getMonth(currentDate), this.getYear(currentDate), iDateDay, iDateMonth, iDateYear);
  371. if(dateSequence == 1)
  372. {
  373. //the passed in date is later than the current date
  374. return true;
  375. }
  376. else
  377. {
  378. //the passed in date is earlier or equal to the current date
  379. return false;
  380. }
  381. },
  382. //NOTE: An empty value will also be compared, to check if a date is set or not
  383. // use isEmpty()
  384. isDateLaterThanOrEqualToCurrentDate: function(iDateDay, iDateMonth, iDateYear)
  385. {
  386. currentDate = new Date();
  387. var dateSequence = this.getDateSequence(this.getDay(currentDate), this.getMonth(currentDate), this.getYear(currentDate), iDateDay, iDateMonth, iDateYear);
  388. if(dateSequence != -1)
  389. {
  390. //the passed in date is later than or equal to the current date
  391. return true;
  392. }
  393. else
  394. {
  395. //the passed in date is earlier or equal to the current date
  396. return false;
  397. }
  398. },
  399. //pass in the javascript Date type
  400. getYear: function(dateValue)
  401. {
  402. //return the year from e.g. "2009-04-22"
  403. year = dateValue.getFullYear();
  404. return year + "";
  405. },
  406. getMonth: function(dateValue)
  407. {
  408. //return the month from e.g. "2009-04-22"
  409. month = (dateValue.getMonth()+1).toString();
  410. //prefix with zero
  411. if (month.length==1) {
  412. month = "0"+month;
  413. }
  414. return month + "";
  415. },
  416. getDay: function(dateValue)
  417. {
  418. //return the day from e.g. "2009-04-22"
  419. day = dateValue.getDate().toString();
  420. //prefix with zero
  421. if (day.length==1) {
  422. day = "0"+day;
  423. }
  424. return day + "";
  425. }
  426. }
  427. /*
  428. The following two functions add disable functionality to the datePickers.
  429. I considered going further, and intercepting the genSelectDateHTML function,
  430. to add enable/disable functions directly onto the enclosing div (more OO),
  431. but might be more awkward to follow.
  432. Here we simply manage the functions on the image (cache when disabled), and
  433. disable the textbox.
  434. */
  435. function hts_disableDatePicker(datePickerId) {
  436. //check first
  437. var dateInput= $('txtDate'+datePickerId);
  438. if(dateInput.disabled == true) {
  439. //already disabled
  440. return;
  441. }
  442. //disable the input
  443. dateInput.disabled=true;
  444. //disable the functions on the image (cache the functions)
  445. var dateImg= $(datePickerId+"imgPicker");
  446. dateImg.cacheOnClick = dateImg.onclick;
  447. dateImg.cacheOnMouseDown = dateImg.onmousedown;
  448. dateImg.cacheOnMouseOut = dateImg.onmouseout;
  449. dateImg.cacheOnMouseOver = dateImg.onmouseover;
  450. dateImg.cacheImgSrc = dateImg.src;
  451. //and remove
  452. dateImg.onclick = null;
  453. dateImg.onmousedown = null;
  454. dateImg.onmouseout = null;
  455. dateImg.onmouseover = null;
  456. dateImg.src = _F_Config.webContent + "/fragments/myinbox/images/icon_datepicker_disable.gif"
  457. }
  458. function hts_enableDatePicker(datePickerId) {
  459. //check first
  460. var dateInput= $('txtDate'+datePickerId);
  461. if(dateInput.disabled == false) {
  462. //already enabled
  463. return;
  464. }
  465. //enable the input
  466. dateInput.disabled=false;
  467. //enable the functions on the image - get from cache
  468. var dateImg= $(datePickerId+"imgPicker");
  469. dateImg.onclick = dateImg.cacheOnClick;
  470. dateImg.onmousedown = dateImg.cacheOnMouseDown;
  471. dateImg.onmouseout = dateImg.cacheOnMouseOut;
  472. dateImg.onmouseover = dateImg.cacheOnMouseOver;
  473. dateImg.src = dateImg.cacheImgSrc;
  474. //reset cache
  475. dateImg.cacheOnClick = null;
  476. dateImg.cacheOnMouseDown = null;
  477. dateImg.cacheOnMouseOut = null;
  478. dateImg.cacheOnMouseOver = null;
  479. dateImg.cacheImgSrc = null;
  480. }