/******************************************************************************************************************************** * Licensed Materials - Property of IBM * * * * IBM Cognos Products: HTS * * * * (C) Copyright IBM Corp. 2005, 2010 * * * * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * *********************************************************************************************************************************/ function hts_datetime(dateFrom,dateTo,msgs) { this.m_dateFrom = dateFrom; this.m_dateTo = dateTo; this.m_msgs = msgs; this.m_dateTimeUtil = new hts_dateTimeUtil(); } hts_datetime.prototype = { checkDate: function(fromDate,newDate) { var dateIsTheSame=true; iYear = newDate.substring(0,4); iMonth = newDate.substring(5, 7); iDay = newDate.substring(8, 10); if (fromDate) { newDateFrom = this.getDateFromValue(); iNewStartYear = newDateFrom.substring(0,4); iNewStartMonth = newDateFrom.substring(5, 7); iNewStartDay = newDateFrom.substring(8, 10); dateIsTheSame = iYear == iNewStartYear && iMonth == iNewStartMonth && iDay == iNewStartDay; } else { newDateTo = this.getDateToValue(); iNewEndYear = newDateTo.substring(0,4); iNewEndMonth = newDateTo.substring(5, 7); iNewEndDay = newDateTo.substring(8, 10); dateIsTheSame = iYear == iNewEndYear && iMonth == iNewEndMonth && iDay == iNewEndDay; } return dateIsTheSame; }, //check that a date is not the current date checkCurrentDate: function(newDate) { currentDate=this.getCurrentDateValue(); iCurrentYear = currentDate.substring(0,4); iCurrentMonth = currentDate.substring(5, 7); iCurrentDay = currentDate.substring(8, 10); iNewYear = newDate.substring(0,4); iNewMonth = newDate.substring(5, 7); iNewDay = newDate.substring(8, 10); isTodaysDate = iCurrentYear == iNewYear && iCurrentMonth == iNewMonth && iCurrentDay == iNewDay; return isTodaysDate; }, getCurrentDateValue: function() { return this.m_dateTimeUtil.getCurrentDateValue(); }, getDateFromFormatValue: function() { if (this.m_dateFrom != null && this.m_dateFrom != '') { return window['pickerControl'+ this.m_dateFrom].sGetFormatValue(); } else { return ''; } }, getDateToFormatValue: function() { if (this.m_dateTo != null && this.m_dateTo != '') { return window['pickerControl'+ this.m_dateTo].sGetFormatValue(); } else { return ''; } }, getDateFromValue: function() { if (this.m_dateFrom != null && this.m_dateFrom != '') { var picker = window['pickerControl'+ this.m_dateFrom]; if (picker.isValid()) { return picker.sGetValue(); } else { //if an invalid date has been entered sGetValue will return a boolean so in this instance we return an zeroed date string return "00000000"; } } else { return ''; } }, getDateToValue: function() { if (this.m_dateTo != null && this.m_dateTo != '') { var picker = window['pickerControl'+ this.m_dateTo]; if (picker.isValid()) { return picker.sGetValue(); } else { //if an invalid date has been entered sGetValue will return a boolean so in this instance we return an zeroed date string return "00000000"; } } else { return ''; } }, setDateFromValue: function (dateStr) { var picker = window['pickerControl'+ this.m_dateFrom]; var ymd = this.convertDateToYMD(dateStr); picker.setValue(ymd); }, setDateToValue: function (dateStr) { var picker = window['pickerControl'+ this.m_dateTo]; var ymd = this.convertDateToYMD(dateStr); picker.setValue(ymd); }, convertDateToYMD: function (dateStr) { var ymd=""; if (dateStr!=='') { iYear = dateStr.substring(0,4); iMonth = dateStr.substring(5, 7); iDay = dateStr.substring(8, 10); ymd = iYear+'-'+iMonth+'-'+iDay; } return ymd; }, simpleValidate:function(validateFrom){ if (validateFrom) { if ( !this.isDateValid(this.m_dateFrom) ) { alert (this.m_msgs['MSG_ERR_INVALID_START_DATE']); return false; } } else { if ( !this.isDateValid(this.m_dateTo) ) { alert (this.m_msgs['MSG_ERR_INVALID_END_DATE']); return false; } } return (true); }, isDateValid: function(str) { return this.m_dateTimeUtil.isDateValid(str); }, validateFromDate: function () { if (!this.simpleValidate(true)) { return false; } // Check the Start Date Time. if (this.getDateFromValue() == '') { alert (this.m_msgs['MSG_ERR_INVALID_START_DATE']); return false; } /*if (this.checkCurrentDate(this.getDateFromValue())) { var dateFrom = this.getDateFromValue(); //set the hours and minutes to 23:59 if the start date is todays date var newDateFrom = dateFrom.replace("00:00.000","23:59.000"); alert('The start date has been set to the current date. The deadline will expire at 23:59 today'); this.setDateFromValue(newDateFrom); //alert (this.m_msgs['MSG_ERR_START_CANNOT_USE_CURRENT_DATE']); //return false; }*/ if (this.getDateToValue()!='' && !(this.validateDateSequence())) { return false; } return (true); }, validateToDate: function () { if (!this.simpleValidate(false)) { return false; } // Check the End Date Time. if (this.getDateToValue() == '') { alert (this.m_msgs['MSG_ERR_INVALID_END_DATE']); return false; } if (this.checkCurrentDate(this.getDateToValue())) { alert (this.m_msgs['MSG_ERR_COMPLETE_CANNOT_USE_CURRENT_DATE']); return false; } if (this.getDateFromValue!='' && !(this.validateDateSequence())) { return false; } return (true); }, validateDates: function() { if (!this.validateFromDate()) { return false; } if (!this.validateToDate()) { return false; } // We could create date objects and compare them but that involves too much work to format // our date values to JS acceptable format. if (!(this.validateDateSequence())) { return false; } return(true); }, // .............................................................................. // Checks for end date being earlier than or the same as the start date. validateDateSequence: function() { iStartYear = this.getDateFromValue().substring(0,4); iStartMonth = this.getDateFromValue().substring(5, 7); iStartDay = this.getDateFromValue().substring(8, 10); iEndYear = this.getDateToValue().substring(0,4); iEndMonth = this.getDateToValue().substring(5, 7); iEndDay = this.getDateToValue().substring(8, 10); var errMsg = 'MSG_ERR_INCOMPATIBLE_DATES'; var bRetVal = this.m_dateTimeUtil.getDateSequence(iStartDay, iStartMonth, iStartYear, iEndDay, iEndMonth, iEndYear); if(bRetVal == 0) { //end date equal to start date errMsg = 'MSG_ERR_EQUAL_DATES'; } if(bRetVal != 1) { alert(this.m_msgs[errMsg]); return false } else { //end date later then start date return true; } } } /* Utility to validate a single date. The constructor expects the same id of the that is passed when creating the date picker i.e. the id of the field passed to the genSelectDateHTML(...) function. e.g.'_THIS_'+'taskOptions_dueDate' */ function hts_datePickerUtil(dateTime) { this.dateTime = dateTime; this.m_dateTimeUtil = new hts_dateTimeUtil(); } hts_datePickerUtil.prototype = { isDateValid: function() { return this.m_dateTimeUtil.isDateValid(this.dateTime); }, isDateEmpty: function() { var isDateEmpty = false; if(this.getDateValue() == '') { isDateEmpty = true; } return isDateEmpty; }, getDateValue: function() { var dateValue = ''; if (this.dateTime != null && this.dateTime != '') { dateValue = window['pickerControl'+ this.dateTime].sGetValue(); } return dateValue; }, //NOTE: An empty value will also be compared, to check if a date is set or not // use isEmpty() isDateLaterThanCurrentDate: function() { var isDateLaterThanCurrentDate = false; //check if the date is valid if(this.isDateValid()) { iDateYear = this.getDateValue().substring(0,4); iDateMonth = this.getDateValue().substring(5, 7); iDateDay = this.getDateValue().substring(8, 10); isDateLaterThanCurrentDate = this.m_dateTimeUtil.isDateLaterThanCurrentDate(iDateDay, iDateMonth, iDateYear); } return isDateLaterThanCurrentDate; }, //NOTE: An empty value will also be compared, to check if a date is set or not // use isEmpty() isDateLaterThanOrEqualToCurrentDate: function() { var isDateLaterThanCurrentDate = false; //check if the date is valid if(this.isDateValid()) { iDateYear = this.getDateValue().substring(0,4); iDateMonth = this.getDateValue().substring(5, 7); iDateDay = this.getDateValue().substring(8, 10); isDateLaterThanCurrentDate = this.m_dateTimeUtil.isDateLaterThanOrEqualToCurrentDate(iDateDay, iDateMonth, iDateYear); } return isDateLaterThanCurrentDate; } } /* Generic date time utilities. Each function should be a self contained function with each parameter passed explicitly. */ function hts_dateTimeUtil() { } hts_dateTimeUtil.prototype = { isDateValid: function(str) { var t = true; if (str != '') { t = window['pickerControl' + str].isValid(); } return t; }, getCurrentDateValue: function() { //convert to something like this : "2009-04-22" currentDate = new Date(); year = this.getYear(currentDate); month = this.getMonth(currentDate); day = this.getDay(currentDate); return year+"-"+month+"-"+day; }, // .............................................................................. // Checks which of the end date or start date is earlier. // returns -1 if endate is earlier the start date // 0 if the end date and start date are equal // 1 if end date is later then the start date // NOTE: this function does not validate the dates, it just compares // the passed in year, month and day values assuming they are // a part of a valid Date. Use other validate functions to // validate if the date is valid or not. //............................................................................... getDateSequence: function(iStartDay, iStartMonth, iStartYear, iEndDay, iEndMonth, iEndYear) { var bRetVal = 1; if (iEndYear < iStartYear) { bRetVal = -1; } else if (iEndYear == iStartYear) { if (iEndMonth < iStartMonth) { bRetVal = -1; } else if (iEndMonth == iStartMonth) { if (iEndDay < iStartDay) { bRetVal = -1; } else if (iEndDay == iStartDay) { bRetVal = 0; } } } return bRetVal; }, //NOTE: An empty value will also be compared, to check if a date is set or not // use isEmpty() isDateLaterThanCurrentDate: function(iDateDay, iDateMonth, iDateYear) { currentDate = new Date(); var dateSequence = this.getDateSequence(this.getDay(currentDate), this.getMonth(currentDate), this.getYear(currentDate), iDateDay, iDateMonth, iDateYear); if(dateSequence == 1) { //the passed in date is later than the current date return true; } else { //the passed in date is earlier or equal to the current date return false; } }, //NOTE: An empty value will also be compared, to check if a date is set or not // use isEmpty() isDateLaterThanOrEqualToCurrentDate: function(iDateDay, iDateMonth, iDateYear) { currentDate = new Date(); var dateSequence = this.getDateSequence(this.getDay(currentDate), this.getMonth(currentDate), this.getYear(currentDate), iDateDay, iDateMonth, iDateYear); if(dateSequence != -1) { //the passed in date is later than or equal to the current date return true; } else { //the passed in date is earlier or equal to the current date return false; } }, //pass in the javascript Date type getYear: function(dateValue) { //return the year from e.g. "2009-04-22" year = dateValue.getFullYear(); return year + ""; }, getMonth: function(dateValue) { //return the month from e.g. "2009-04-22" month = (dateValue.getMonth()+1).toString(); //prefix with zero if (month.length==1) { month = "0"+month; } return month + ""; }, getDay: function(dateValue) { //return the day from e.g. "2009-04-22" day = dateValue.getDate().toString(); //prefix with zero if (day.length==1) { day = "0"+day; } return day + ""; } } /* The following two functions add disable functionality to the datePickers. I considered going further, and intercepting the genSelectDateHTML function, to add enable/disable functions directly onto the enclosing div (more OO), but might be more awkward to follow. Here we simply manage the functions on the image (cache when disabled), and disable the textbox. */ function hts_disableDatePicker(datePickerId) { //check first var dateInput= $('txtDate'+datePickerId); if(dateInput.disabled == true) { //already disabled return; } //disable the input dateInput.disabled=true; //disable the functions on the image (cache the functions) var dateImg= $(datePickerId+"imgPicker"); dateImg.cacheOnClick = dateImg.onclick; dateImg.cacheOnMouseDown = dateImg.onmousedown; dateImg.cacheOnMouseOut = dateImg.onmouseout; dateImg.cacheOnMouseOver = dateImg.onmouseover; dateImg.cacheImgSrc = dateImg.src; //and remove dateImg.onclick = null; dateImg.onmousedown = null; dateImg.onmouseout = null; dateImg.onmouseover = null; dateImg.src = _F_Config.webContent + "/fragments/myinbox/images/icon_datepicker_disable.gif" } function hts_enableDatePicker(datePickerId) { //check first var dateInput= $('txtDate'+datePickerId); if(dateInput.disabled == false) { //already enabled return; } //enable the input dateInput.disabled=false; //enable the functions on the image - get from cache var dateImg= $(datePickerId+"imgPicker"); dateImg.onclick = dateImg.cacheOnClick; dateImg.onmousedown = dateImg.cacheOnMouseDown; dateImg.onmouseout = dateImg.cacheOnMouseOut; dateImg.onmouseover = dateImg.cacheOnMouseOver; dateImg.src = dateImg.cacheImgSrc; //reset cache dateImg.cacheOnClick = null; dateImg.cacheOnMouseDown = null; dateImg.cacheOnMouseOut = null; dateImg.cacheOnMouseOver = null; dateImg.cacheImgSrc = null; }