/* * Will sort the options of the item passed to it. * Must pass it the UI item reference. If called from in the list object you can use "item", * otherwise would have to use "page.F_DropDown" or "form.getPage('P_Page').F_DropDown" * * Will keep track of the currently selected value and insure that it is re-selected after the list is sorted */ app.getSharedData().sortList = function(theItem) { //store the current value var value = theItem.getBOAttr().getValue(); //sort the list var options = theItem.getOptions(); if(options !== null) { options.sort(function(a,b) { return get(a, 'title').localeCompare(get(b, 'title')); }); theItem.setOptions(options); //reset back to the current value theItem.getBOAttr().getValue(value); } } /* Returns the number of working dates between the two dates. * includeWeekends - Pass true if you want to include weekends otherwise false. */ app.getSharedData().workingDaysBetweenDates = function(startDt, endDt, includeWeekends) { // Validate input if (endDt < startDt) return 0; // Calculate days between dates var millisecondsPerDay = 86400 * 1000; // Day in milliseconds startDt.setHours(0,0,0,1); // Start just after midnight endDt.setHours(23,59,59,999); // End just before midnight var diff = endDt.getTime() - startDt.getTime(); // Milliseconds between datetime objects var days = Math.ceil(diff / millisecondsPerDay); if(!includeWeekends) { // Subtract two weekend days for every week in between var weeks = Math.floor(days / 7); var days = days - (weeks * 2); // Handle special cases var startDay = startDt.getDay(); var endDay = endDt.getDay(); // Remove weekend not previously removed. if (startDay - endDay > 1) days = days - 2; // Remove start day if span starts on Sunday but ends before Saturday if (startDay === 0 && endDay !== 6) days = days - 1 // Remove end day if span ends on Saturday but starts after Sunday if (endDay === 6 && startDay !== 0) days = days - 1 } return days; } app.getSharedData().workingDaysBetweenDates = function(startDt, endDt, includeWeekends, excludeHolidays) { // Validate input if (endDt < startDt) return 0; // Calculate days between dates var millisecondsPerDay = 86400 * 1000; // Day in milliseconds startDt.setHours(0,0,0,1); // Start just after midnight endDt.setHours(23,59,59,999); // End just before midnight var diff = endDt.getTime() - startDt.getTime(); // Milliseconds between datetime objects var days = Math.ceil(diff / millisecondsPerDay); if(!includeWeekends) { // Subtract two weekend days for every week in between var weeks = Math.floor(days / 7); var days = days - (weeks * 2); // Handle special cases var startDay = startDt.getDay(); var endDay = endDt.getDay(); // Remove weekend not previously removed. if (startDay - endDay > 1) days = days - 2; // Remove start day if span starts on Sunday but ends before Saturday if (startDay === 0 && endDay !== 6) days = days - 1 // Remove end day if span ends on Saturday but starts after Sunday if (endDay === 6 && startDay !== 0) days = days - 1 } //remove any holidays that might be in the range if(excludeHolidays !== null) { for(var h=0; h startDt.getTime()) { days--; } } } return days; } /* Returns the number of hours from the two times provided * startTime - Can be date, time or dateTime * endTime - Can be date, time or dateTime * rndHours - Round the result to the highest integer (hour). Values are true or false. */ app.getSharedData().calcDuration = function(startTime, endTime, rndHours) { //convert to milliseconds var d1 = startTime.getTime(); var d2 = endTime.getTime(); var diff = d2 - d1; var durHours = diff / 1000 / 60 / 60; if(rndHours) { Math.ceil(durHours); } return durHours; } /* Given the BO of a date field this function will validate that the entered date is * after today. * dateObj1 - the business object of the date field (i.e. BOA, BO.F_Date1) * errorMsg - The message to display to the user when the field is set to invalid */ app.getSharedData().isDateAfterToday = function(dateObj1, errorMsg) { //get todays date, including the current time var today = new Date(); var d = dateObj1.getValue(); //get date from passed object //set the time of the date to just before midnight d.setSeconds(59); d.setMinutes(59); d.setHours(23); //D1 MUST be less then today if(d < today) { dateObj1.setValid(false, errorMsg); } else { dateObj1.setValid(true, ""); } } /* Given the BO of a date field this function will validate that the entered date is * before today (in the past). * dateObj1 - the business object of the date field (i.e. BOA, BO.F_Date1) * errorMsg - The message to display to the user when the field is set to invalid */ app.getSharedData().isDateBeforeToday = function(dateObj1, errorMsg) { //get todays date, including the current time var today = new Date(); var d = dateObj1.getValue(); //get date from passed object //set the time of the date to just before midnight d.setSeconds(59); d.setMinutes(59); d.setHours(23); //D1 MUST be less then today if(d < today) { dateObj1.setValid(true, ""); } else { dateObj1.setValid(false, errorMsg); } } /* Given two date fields the function will validate that they are numOfDays apart. * If the dateObj2 is not numOfDays greater than dateObj1 then dateObj2 will be set * invalid with the specified errorMsg. * * dateObj1 - the first date field (e.g. BO.F_Date1) * dateObj2 - the second date field (e.g. BO.F_Date2) * numOfDays - the number of days that D2 must be grater than D1 * errorMsg - The error message to display for D2 when invalid * * Usage: Place function in the Settings...onStart * Call the function in the onItemChange of the two date fields: * app.getSharedData().compareDatesByDays(BO.F_Date2, BO.F_Date3, 14, "The date must be 14 days past D1."); */ app.getSharedData().compareDatesByDays = function(dateObj1, dateObj2, numOfDays, errorMsg) { var d1 = dateObj1.getValue(); //get first date var d2 = dateObj2.getValue(); //get sec date var diff = 0; //convert to milliseconds var d1_milli = d1.getTime(); var d2_milli = d2.getTime(); //get difference and convert back to days var diff_milli = d2_milli - d1_milli; var nDays = Math.ceil(diff_milli / 1000 / 60 / 60 / 24); //if the diff is less then numOfDays set the d2 field invalid if(nDays < numOfDays) { dateObj2.setValid(false, errorMsg); } else { dateObj2.setValid(true, ""); } } app.getSharedData().removeDuplicatesFromList = function(theList) { var newList = new Array(); //loop through the current list for(var j=0; j 0) { rowObjs.push(theTable.get(0)); theTable.remove(theTable.get(0)); } for(var i = 0; i < rowObjs.length; i++) { var firstRow = get(rowObjs, i); var secondRow = get(rowObjs, i+1); if(!rowMoved) { if(secondRow === theRow) { //add second row first, then the current row theTable.add(secondRow); i++; //increment counter again because we just processed two rows in one loop rowMoved = true; } } theTable.add(firstRow); } } /* Change the order of the table by moving a row down one position. * * theTable: the BO object of the table * theRow: the BO object of the row to be moved * * usage: Place in a button's onClick event: * app.getSharedData().moveSelectionDown(BO.F_InputParameters, page.F_InputParameters.getSelection()) * page.F_InputParameters.setSelection(-1); * * Clearing the selection after moving a row is critical otherwise errors may insue. */ app.getSharedData().moveSelectionDown = function(theTable, theRow) { var rowObjs = new Array(); var rowMoved = false; //remove all rows from the table //store each row in a separate array while(theTable.getLength() > 0) { rowObjs.push(theTable.get(0)); theTable.remove(theTable.get(0)); } for(var i = 0; i < rowObjs.length; i++) { var firstRow = get(rowObjs, i); // check to make sure we are not the last row, if we are just put the row back in the table if(i < rowObjs.length - 1) { var secondRow = get(rowObjs, i+1); if(!rowMoved) { if(firstRow === theRow) { //add second row, then the current row theTable.add(secondRow); i++; //increment counter again because we just processed two rows in one loop rowMoved = true; } } } theTable.add(firstRow); } } /* * Returns the index of the specified row in the specified table * theTable: the BO object of the table * theRow: the BO object of the row to be moved */ app.getSharedData().getIndexInTable = function(theTable, theRow) { if(theTable !== null && theRow !== null) { for(var i=0; i"; //this controls the formatting of each column in the table row } s += "

"; //this controls what happens when we move to the next row } s += "

"; //close the html elements that were started at the beginning return s; } /** Returns a random number between the numbers specified. */ app.getSharedData().randomFromInterval = function(from,to) { return Math.floor(Math.random()*(to-from+1)+from); } /** Randomizes the selections of the specified list item. * usage: in the onShow event of the list you want to randomize place: app.getSharedData().randomList(item); */ app.getSharedData().randomList = function(theItem) { //store the current value var value = theItem.getBOAttr().getValue(); //set up the arrays var arrOptions = theItem.getOptions(); var tmpOptions = theItem.getOptions(); var newOptions = new Array(); //Randomise the list if(arrOptions !== null) { for ( var i = 0; i < arrOptions.length; i++ ) { //the random number should be between 0 and the length of the array var j = app.getSharedData().randomFromInterval(0,tmpOptions.length-1); var temp = get(tmpOptions, j); //assign the item to the new array newOptions.push(temp); //remove the item from the temp array tmpOptions.splice(j, 1); } //Set the options back theItem.setOptions(newOptions); //reset back to the current value theItem.getBOAttr().getValue(value); } } /** * Returns the Label of the dropdown item, searches by value */ app.getSharedData().getDropdownItemTitle = function(theList, compareValue) { var opts = theList.getOptions(); var retVal = ""; //loop all the items and return if you find the compareValue for(var i=0;i= theLength) { theField.setDisplayValue(v.substr(0,theLength)); } } /* * This is the function where you place the logic that you want to perform on the item that you are currently looking at. * The recursive function passes the handle to the current item, from which you can then access any of its properties */ app.getSharedData().processItem = function(item) { //do whatever you want to the item... alert(item.getId() + " : " + item.getValue()); //if(item.getType() === "text") { //do your thing //} } /* * Returns true if the current item has children, otherwise false. */ app.getSharedData().hasItems = function(containerID) { var list = containerID.getChildren(); if(list.getLength() > 0) { return true; } else { return false; } } /* * Recursive function used for counting form items. * containerID: UI item (i.e. page or item) * processItem: the function that contains the work we want to perform on the item we have accessed */ app.getSharedData().getItem = function(containerID, processItem) { var itemList; var pageList; var pageCount = 1; debugger; //check to see if the container is a form as it requires different processing if(containerID.getType() === "form") { pageList = containerID.getPageIds(); //list of the page IDs - not the actual objects!! pageCount = pageList.length; } else { itemList = containerID.getChildren(); } //need a loop to account for different pages for(var p=0; p 0) { d2 = new Date(Math.ceil(d2.getTime() + millisecondsPerDay)); var theDay = d2.getDay(); //dont count sat and sunday if(theDay !== 0 && theDay !==6) { counter--; } } return d2; } /* * Subtract number of days from a date. * dateObj1 - The date field (i.e. BO.F_Date) * numOfDays - The number of days to subtract */ app.getSharedData().subtractDaysFromDate = function(dateObj1, numOfDays) { var d1 = dateObj1.getValue(); //get first date var d1_milli = d1.getTime(); return new Date(Math.ceil(d1_milli - (1000 * 60 * 60 * 24 * numOfDays))); } /* Subtract number of working days (not incl Sat or Sun) to a date. * dateObj1 - The date field (i.e. BO.F_Date) * numOfDays - The number of working days to subtract */ app.getSharedData().subtractWorkingDaysFromDate = function(dateObj1, numOfDays) { var d1 = dateObj1.getValue(); //get first date var d2 = dateObj1.getValue(); var counter = numOfDays; var millisecondsPerDay = 86400 * 1000; while (counter > 0) { d2 = new Date(Math.ceil(d2.getTime() - millisecondsPerDay)); var theDay = d2.getDay(); //dont count sat and sunday if(theDay !== 0 && theDay !==6) { counter--; } } return d2; } /* Convert a standard AD date into a Buddhist Date * dateAD: the date to convert * * usage: Place in onLoad or beforeSubmit event to convert a * date. The code would be something like: * BO.F_DATETIME.setValue(app.getSharedData().convertToBuddhistDate(new Date())); */ app.getSharedData().convertToBuddhistDate = function(dateAD){ var dateBE = new Date(dateAD); dateBE.setFullYear(dateAD.getFullYear() + 543); return dateBE; } /* Calculates the age from a specified birth date. * * birthDay - the date to calculate the age from * * usage: Place function in Settings...Events...Custom Actions * BO.F_Number.setValue(app.getSharedData().getAgeFromBirthDate(BOA.getValue(), true)); * BO.F_SingleLine.setValue(app.getSharedData().getAgeFromBirthDate(BOA.getValue(), false)); */ app.getSharedData().getAgeFromBirthDate = function(birthDay) { //31557600000 is 24 * 3600 * 365.25 * 1000 Which is the length of a year, the length of a year is 365 days and 6 hours which is 0.25 day. // double tilde converts float to integer return ~~((Date.now() - birthDay) / (31557600000)); } /* * Function that can be used to check the content of a field and set it to invalid if it exceeds a specified length. *Params: * theItem - the UI object of the item to validate * theLength - the max length of the field, if exceeded the field will be set to invalid * theMsg - The text to render if the length is exceeded. * usage: * - copy the entire function into the Settings...Events. * - place in the onLiveItemChange event of the field to validate and change parameters as desired: * app.getSharedData().fieldLengthValidation(item, 3, 'Field will only accept 3 characters or less'); */ app.getSharedData().fieldLengthValidation = function (theItem, theLength, theMsg) { var dval = theItem.getDisplayValue(); if(dval.length > theLength) { theItem.getBOAttr().setValid(false, theMsg); } else { theItem.getBOAttr().setValid(true, ''); } } /* * Returns the sum of all the fields specified * * fields - array of field objects - new Array(BO.F_Field1, BO.F_Field2, ...) */ app.getSharedData().sumFields = function(fields) { var sum = 0; for(var i = 0; i < fields.length; i++) { var f = get(fields, i); if(f.getValue() !== "") { sum += parseFloat(f.getValue()); } } return sum; } app.getSharedData().multiplyFields = function(fields) { var result = 1; for(var i = 0; i < fields.length; i++) { var f = get(fields, i); if(f.getValue() !== "") { result *= parseFloat(f.getValue()); } } return result; } /* Returns the Month name of the specified date. * * theDateField - pass it the date value like BO.F_Date.getValue() */ app.getSharedData().getMonthNameFromDate = function(theDateField) { var month = theDateField.getMonth(); var monthName = ""; switch (month) { case 0: monthName = "January"; break; case 1: monthName = "February"; break; case 2: monthName = "March"; break; case 3: monthName = "April"; break; case 4: monthName = "May"; break; case 5: monthName = "June"; break; case 6: monthName = "July"; break; case 7: monthName = "August"; break; case 8: monthName = "September"; break; case 9: monthName = "October"; break; case 10: monthName = "November"; break; case 11: monthName = "December"; break; } return monthName; } app.getSharedData().isInputField = function(theItemType) { var r = false; if(theItemType === "text" || theItemType === "textArea" || theItemType === "date" || theItemType === "checkGroup" || theItemType === "radioGroup" || theItemType === "number" || theItemType === "currency" || theItemType === "comboBox" || theItemType === "horizontalSlider" || theItemType === "choiceSlider" || theItemType === "time" || theItemType === "webLink" || theItemType === "radioGroup" || theItemType === "horizontalSlider" || theItemType === "choiceSlider" || theItemType === "surveyQuestion" || theItemType === "emailAddress" || theItemType === "password" || theItemType === "timeStamp") { r = true; } return r; }