/** * Generic global functions for validating input values. Most of these functions take the input element * as a parameter and validate its current value. If invalid, resets the element to its "defaultValue" if defined. */ /** * Method to check if two fields add up to a field's value * @param theSum, value should add up to * @param theInputeOne, one field to add * @param theInputTwo, the second field to add * * @return Boolean Whether or not the test passed */ function validateSumOfFields(theSum,theInputOne,theInputTwo) { var aSum = 0; var aOldSum = theSum.value; if (theInputOne != null) { aSum += theInputOne.value - 0; } if (theInputTwo != null) { aSum +=theInputTwo.value - 0; } if (theSum != null) { if (theSum.value == aSum) { return true; } else { alert(" Invalid sum."); theSum.focus(); return false; } } if ((theSum == null) && (aSum != 0)) { alert("."); theSum.focus(); return false; } } // two digit month/two or four digit year var reMonthYear = /^\d?\d\/\d?\d?\d\d$/ /** * Method to check for valid month/year format. * @param theInputElement Form element whose value is to be tested * * @return Boolean Whether or not the test passed */ function validateMonthYear(theInputElement) { var aValue = theInputElement.value; if ((aValue == null) || (trim(aValue).length == 0)) { return true; } if (reMonthYear.test(aValue)) { if (!isDate(aValue, "M/y")) { alert("Please enter two digit month/two or four digit year."); theInputElement.focus(); return false; } return true; } else { alert("Please enter two digit month/two or four digit year."); theInputElement.focus(); return false; } } // followed by '@' and one or more chars, which are // then followed by '.' and one or more characters var reEmail = /^.+\@.+\..+$/ /** * Method to check for valid email addresses. The input is tested * against a regular expression to determine if the input is in * a valid format. * * @param theInputElement Form element object whose * value is to be tested * * @return Boolean Whether or not the test passed */ function validateEmail(theInputElement) { var aValue = theInputElement.value; if((aValue == null) || (trim(aValue).length == 0) ) { return true; } if(reEmail.test(aValue)) { return true; } else { alert("Please enter a valid email address."); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } } /** * Utility function used to trim white spaces from a String * * @param theString String which will be stripped of beginning or ending white spaces * * @return String The cleaned up String */ function trim(theString) { // Remove Beginning Spaces while(theString.charAt(0)==' '){ theString = theString.substring(1,theString.length) } // Remove Ending Spaces while(theString.charAt(theString.length-1)==' '){ theString = theString.substring(0,theString.length-1) } return theString; } /** * Creates a report no * String aReportNo=aUser.ContractNo+"-"+aMonthString+aDayString+aYearString+"-"+aUser.InspectorID; */ function createReportNo(theContractNoField,theDateField,theInspectorIDField,theRecordNoField){ var aDateArray = theDateField.value.split('/'); var aDateFieldValue=""; if(aDateArray.length < 3){ return false; } var aDateString = ""; var i=0; for(i=0;i<3;i++){ if(aDateArray[i].length ==1){ aDateFieldValue=aDateFieldValue+aDateArray[i]; aDateString="0"+aDateString+aDateArray[i]; }else{ aDateFieldValue=aDateFieldValue+aDateArray[i]; aDateString=aDateString+aDateArray[i]; } if(i!=2){ aDateFieldValue=aDateFieldValue+"/"; } theDateField.value=aDateFieldValue; } var aReportNo = theContractNoField.value+"-"+aDateString+"-"+theInspectorIDField.value; theRecordNoField.value=aReportNo; /**for(i=0;i= 0 && aNumber <= 100)){ alert("Please enter a number between 0 and 100"); theInputElement.focus(); return false; } } return true; } /** * Validates an input to make sure it is a real positive number, or blank, or 0 * * @param theInputElement Form element which is validated * * @return boolean Whether the test passed or failed */ function validateRealPositiveNumber(theInputElement){ if (theInputElement.value == null) return true; if (theInputElement.value == "") return true; var aValue = theInputElement.value; if(isNotANumber(aValue) || !isWellFormatted(aValue)){ alert(aValue+" is not a number."); theInputElement.focus(); return false; }else{ var aNumber = getNumber(aValue); if(!(aNumber >= 0 && aNumber <= 100)){ alert("Please enter a number between " + 0 + " and " + 100 + "."); theInputElement.focus(); return false; } } return true; } /** * Same as validatePercent(), but also accepts blank values * * @param theInputElement Form element which is validated * * @return boolean Whether the test passed or failed */ function validatePercentOrBlank(theInputElement){ if (theInputElement.value == null) return true; if (theInputElement.value == "") return true; if(!validatePositiveOrZeroNumber(theInputElement)){ return false; } return validatePercent(theInputElement); } /** * validates the given input field element for a valid number between 0 and 100. If less than 1, multiplies by 100. * appends a "%" character * * @param theInputElement Form element which is validated * * @return boolean Whether the test passed or failed */ function validatePercent(theInputElement){ if(!validatePositiveOrZeroNumber(theInputElement)){ return false; } var aValue = theInputElement.value; var alertValue = aValue; if ((aValue == null) || (aValue.length == 0)) { alert("you must enter a valid percent"); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } aValue="" + getNumber(aValue); // check if followed by a percent sign var isPercent = false; if (aValue.charAt(aValue.length-1)=='%'){ aValue = aValue.substring(0,aValue.length-1); isPercent = true; } // check if a valid percent. If number was followed by a percent sign, we allow // values over 1 if (isNaN(aValue)) { alert(alertValue+" is not a valid number."); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } // convert to percent form if ((!isPercent) && (aValue < 1) && (aValue > -1)) aValue *= 100; if ((aValue < 0) || (aValue > 100)){ alert("Please enter a number between " + 0 + " and " + 100 + "."); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } theInputElement.defaultValue = theInputElement.value = aValue; return true; } /** * validates the given input field element for a valid positive number * * @param theInputElement Form element which is validated * * @return boolean Whether the test passed or failed */ function validatePositiveNumber(theInputElement){ //alert("getnumber"); var aValue = theInputElement.value; //var aValue = theInputElement.value; if((aValue == null) || (trim(aValue).length == 0) ) { return true; } if(isNotANumber(aValue) || !isWellFormatted(aValue)){ alert(theInputElement.value + " is not a valid positive number. Enter a number greater than or equal to 0."); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } aValue =getNumber(theInputElement.value); //aValue=parseFloat(aValue); //alert("number returned"+aValue); //var aNumber = parseFloat(aValue); if (isNaN(aValue) || (aValue <= 0) || (aValue == Number.POSITIVE_INFINITY) || (aValue == Number.NEGATIVE_INFINITY)) { alert(theInputElement.value + " is not a valid positive number. Enter a number greater than or equal to 0."); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } else { //once we have determined that this is a valid pos number do a parseFloat to remove any leading zero's //var newValue = parseFloat(aValue); //the value may have any number of decimal places at this point, //but no more than two are ever needed, so round it off //var newValue = roundNumberToSecondDecimal(aValue); //theInputElement.defaultValue = newValue; //theInputElement.value = newValue; var aFormattedNumber=reformatNumber(theInputElement.value); theInputElement.defaultValue=aFormattedNumber; theInputElement.value=aFormattedNumber; return true; } } /** * validates the given input field element for a valid positive number * * @param theInputElement Form element which is validated * * @return boolean Whether the test passed or failed */ function validatePositiveOrZeroNumber(theInputElement){ //alert("getnumber"); var aValue = theInputElement.value; //var aValue = theInputElement.value; if((aValue == null) || (trim(aValue).length == 0) ) { return true; } if(isNotANumber(aValue) || !isWellFormatted(aValue)){ alert(theInputElement.value + " is not a valid positive number. Enter a number greater than or equal to 0."); theInputElement.focus(); return false; } aValue =getNumber(theInputElement.value); //aValue=parseFloat(aValue); //alert("number returned"+aValue); //var aNumber = parseFloat(aValue); if (isNaN(aValue) || (aValue < 0) || (aValue == Number.POSITIVE_INFINITY) || (aValue == Number.NEGATIVE_INFINITY)) { alert(theInputElement.value + " is not a valid positive number. Enter a number greater than or equal to 0."); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } else { //once we have determined that this is a valid pos number do a parseFloat to remove any leading zero's //var newValue = parseFloat(aValue); //the value may have any number of decimal places at this point, //but no more than two are ever needed, so round it off //var newValue = roundNumberToSecondDecimal(aValue); //theInputElement.defaultValue = newValue; //theInputElement.value = newValue; var aFormattedNumber=reformatNumber(theInputElement.value); theInputElement.defaultValue=aFormattedNumber; theInputElement.value=aFormattedNumber; return true; } } /** * validates the given input field element for a valid positive number * * @param theInputElement Form element which is validated * * @return boolean Whether the test passed or failed */ function validatePositiveOrZeroIntegerNumber(theInputElement){ //alert("getnumber"); var aValue = theInputElement.value; //var aValue = theInputElement.value; if((aValue == null) || (trim(aValue).length == 0) ) { return true; } if(isNotANumber(aValue) || !isWellFormatted(aValue)){ alert(theInputElement.value + " is not a valid positive integer. Enter a number greater than or equal to 0."); theInputElement.focus(); return false; } if((aValue % 1) > 0){ alert(theInputElement.value + " is not a valid positive integer. Enter a number greater than or equal to 0."); theInputElement.focus(); return false; } aValue =getNumber(theInputElement.value); //aValue=parseFloat(aValue); //alert("number returned"+aValue); //var aNumber = parseFloat(aValue); if (isNaN(aValue) || (aValue < 0) || (aValue == Number.POSITIVE_INFINITY) || (aValue == Number.NEGATIVE_INFINITY)) { alert(theInputElement.value + " is not a valid positive integer. Enter a number greater than or equal to 0."); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } else { //once we have determined that this is a valid pos number do a parseFloat to remove any leading zero's //var newValue = parseFloat(aValue); //the value may have any number of decimal places at this point, //but no more than two are ever needed, so round it off //var newValue = roundNumberToSecondDecimal(aValue); //theInputElement.defaultValue = newValue; //theInputElement.value = newValue; var aFormattedNumber=reformatNumber(theInputElement.value); theInputElement.defaultValue=aFormattedNumber; theInputElement.value=aFormattedNumber; return true; } } /** * validates the given input field element for a valid number * * @param theInputElement Form element which is validated * * @return boolean Whether the test passed or failed */ function validateNumber(theInputElement){ var aValue = theInputElement.value; if((aValue == null) || (trim(aValue).length == 0) ) { return true; } if(isNotANumber(aValue) || !isWellFormatted(aValue)){ alert(theInputElement.value + " Invalid Number."); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } aValue =getNumber(theInputElement.value); if (isNaN(aValue) || (aValue == Number.POSITIVE_INFINITY) || (aValue == Number.NEGATIVE_INFINITY)) { alert(theInputElement.value + " Invalid Number."); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } else { var aFormattedNumber=aValue; theInputElement.defaultValue=aValue; theInputElement.value=aValue; return true; } } /** * Returns the currency format of the number */ function formatCurrency(theNum) { var aNum = theNum.toString().replace(/\$|\,/g,''); if(isNaN(aNum)) aNum = "0"; var aSign = (aNum == (aNum = Math.abs(aNum))); aNum = Math.floor(aNum*100+0.50000000001); var aCents = aNum%100; aNum = Math.floor(aNum/100).toString(); if(aCents<10) aCents = "0" + aCents; for (var i = 0; i < Math.floor((aNum.length-(1+i))/3); i++) aNum = aNum.substring(0,aNum.length-(4*i+3))+','+ aNum.substring(aNum.length-(4*i+3)); return (((aSign)?'':'-') + aNum + '.' + aCents); } /** * Reformats the number to the user's preferences * * @return String the formatted number */ function reformatNumber(theNumber){ var aValue=new String(trim(theNumber)); var aLength=aValue.length; var i=0; var aNumberValue=""; var aIsFloat = false; var aAfterPoint = 0; for(i=0;i 0){ return false; } }else if(isNaN(aCharVal)){ return false; } //finally make sure if we reach the end, //and there was a thousand separator, the thousandseplocation == 3 if(i == (aLength-1) && doesContainThousandSep){ if(aThousandSepLocation!=2 && !isAfterDecimal){ return false; } } aThousandSepLocation++; } return true; } /** * Rounds the number given to the second decimal place. * Multiply the number given by 100 to move the decimal * place over two. Then round the new number. Then * divide by 100 to put the number back to it's original * format. * * @param theNumberToRound number which is checked for more than two decimal places and rounded * * @return number the rounded number */ function roundNumberToSecondDecimal(theNumberToRound) { theNumberToRound = theNumberToRound * 100; theNumberToRound = Math.round(theNumberToRound); theNumberToRound = theNumberToRound / 100; return theNumberToRound; } /** * validates the given input field element for a valid positive number * * @param theInputElement Form element which is validated * * @return boolean Whether the test passed or failed */ function validateLength(theInputElement, theMinLength, theMaxLength){ var aValue = theInputElement.value; var aName = theInputElement.name; if((aValue == null) || (trim(aValue).length == 0) ) { return true; } else if(trim(aValue).length < theMinLength || trim(aValue).length > theMaxLength) { alert("Invalid input length. Must be less than "+(theMaxLength+1)+" characters. "); theInputElement.value = theInputElement.defaultValue; markInputElementRed(theInputElement); theInputElement.focus(); return false; } } /** * Changes the border of an input element red. Can be used to mark * the element with validation errors. */ function markInputElementRed(theInputElement) { theInputElement.style.borderStyle = "solid"; theInputElement.style.borderColor = "CC3333"; } /** * Validates the given input field elements match. * * @param theFirstInputElement Form element to validate against * @param theSecondInputElement Form element to validate * * @retun boolean Whether the test passed or failed */ function validatePasswords(theFirstInputElement, theSecondInputElement) { var aVal1 = theFirstInputElement.value; var aVal2 = theSecondInputElement.value; if (aVal1 != aVal2) { alert("The Password and Confirm Password fields do not match."); theSecondInputElement.focus(); return false } } /** * validates the given input field element for a valid integer * * @param theInputElement The element to be validated * * @return boolean Whether the test passed or failed */ function validateInt(theInputElement){ var aValue = theInputElement.value; var aNum = parseInt(aValue); if (aValue.length == 0) { return true; } else if (isNaN(aNum)) { alert(aValue+" is not a valid integer"); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } else if (!isNaN(aNum)) { aNumString = "" + aNum; if (aNumString.length != aValue.length) { alert(aValue+" is not a valid integer"); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } else { return true; } } else { theInputElement.defaultValue = aValue; theInputElement.value = aValue; return true; } } /** * validates the given input field element for a valid positive int * * @param theInputElement Form element which is validated * * @return boolean Whether the test passed or failed */ function validatePositiveInt(theInputElement){ var aValue = theInputElement.value; if((aValue == null) || (trim(aValue).length == 0) ) { return true; } var aNumber = parseInt(aValue); if (isNaN(aNumber) || (aNumber < 0) || (aNumber == Number.POSITIVE_INFINITY)) { alert(aValue + " is not a valid positive integer. Enter a number greater than or equal to 0."); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } else { theInputElement.defaultValue = Math.round(aNumber); theInputElement.value = Math.round(aNumber); return true; } } /** * returns the actual formTable parameter object for current row * * @param theFormName The name of the formTable's Form * @param theInputElement Form element to get the current row's suffix * @param theElementName The name of the parameter to be extended with row's suffix * * @return form object */ function getFormTableRowParameter(theFormName,theInputElement,theElementName){ var aRowName=theElementName; var aName=theInputElement.name; aLength=aName.length; for(i=aLength-1;i>=0;i--){ aCharVal=aName.charAt(i); if(aCharVal=="_"){ aRowName=aRowName+aName.substring(i,aName.length); break; }else{ }}; return document.forms[theFormName].elements[aRowName]; } /** * validate the given input field element in given range * * @param theInputElement Form element to be validated * @param theMin Minimum acceptable * @param theMax Maximum acceptable * * @return boolean Whether the test passed or failed */ function validateInRange(theInputElement,theMin,theMax){ var aValue = theInputElement.value; if((aValue == null) || (trim(aValue).length == 0) ) { return true; } if(isNotANumber(aValue) || !isWellFormatted(aValue)){ alert(aValue+" is not a valid number."); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } aValue =getNumber(theInputElement.value); aMinValue=getNumber(theMin.value); aMaxValue=getNumber(theMax.value); if (isNaN(aValue) || (aValue < aMinValue) || (aValue > aMaxValue)) { alert("Please enter a number between " + aMinValue + " and " + aMaxValue + "."); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } else { theInputElement.defaultValue=aValue; theInputElement.value=aValue; return true; } } /** * Function to make sure that the birthDate is before the deathDate * * @param theFormName The name of the form containing the elements that * are validated on * @param theBeforeDate The name of the element containing the date which should * be before the after date * @param theAfterDate The name of the element containing the date which should * be after the before date */ function validateFutureDeathDate(theFormName, theBeforeDate, theAfterDate) { aReturnValue = validateFutureDate(theFormName, theBeforeDate, theAfterDate); if(aReturnValue == DATE_ANACHRONISTIC) { alert("alert.invalidFutureDeathDate."); return false; } return true; } /** * Function to make sure that the one date is after another * * @param theFormName The name of the form containing the elements that * are validated on * @param theBeforeDate The name of the element containing the date which should * be before the after date - one field date * @param theAfterDate The name of the element containing the date which should * be after the before date */ function validateFutureDateWithNotEditableDate(theFormName, theBeforeNotEditableDate, theAfterDate) { // get all the month, day, year elements based on theBeforeNotEditableDate // name tacked on with .month, .day, and .year aBeforeMonth = document.forms[theFormName].elements[theBeforeNotEditableDate + ".month"].value; aBeforeYear = document.forms[theFormName].elements[theBeforeNotEditableDate + ".year"].value; aBeforeDay = document.forms[theFormName].elements[theBeforeNotEditableDate + ".day"].value; // get all the month, day, year elements based on theAfterDate // name tacked on with .month, .day, and .year aAfterDaysObject = document.forms[theFormName].elements[theAfterDate + ".day"]; aAfterMonthObject = document.forms[theFormName].elements[theAfterDate + ".month"]; aAfterYearObject = document.forms[theFormName].elements[theAfterDate + ".year"]; aAfterMonth = aAfterMonthObject[aAfterMonthObject.selectedIndex].value; aAfterYear = aAfterYearObject[aAfterYearObject.selectedIndex].value; aAfterDay = aAfterDaysObject[aAfterDaysObject.selectedIndex].value; if(!nonBlankDate(aAfterYear, aAfterMonth, aAfterDay)) { aAfterDaysObject.focus(); return DATE_INVALID; } aAfterDate = new Date(aAfterYear, aAfterMonth, aAfterDay); aBeforeDate = new Date(aBeforeYear, aBeforeMonth, aBeforeDay); if(aBeforeDate.getTime() >= aAfterDate.getTime()) { return DATE_ANACHRONISTIC; } return DATE_OK; } /** * Function to make sure that the creationDate is before the requestedDate * * @param theFormName The name of the form containing the elements that * are validated on * @param theBeforeDate The name of the element containing the date which should * be before the after date * @param theAfterDate The name of the element containing the date which should * be after the before date */ function validateFutureRequestedDate(theFormName, theBeforeNotEditableDate, theAfterDate) { aReturnValue = validateFutureDateWithNotEditableDate(theFormName, theBeforeNotEditableDate, theAfterDate); if(aReturnValue == DATE_ANACHRONISTIC) { alert("The requested date must be a date after the creation date."); return false; } if(aReturnValue == DATE_INVALID) { alert("You have entered an invalid date."); return false; } return true; } /** * Function to make sure that the effectiveDate is before the expirationDate * If the second date is filled in, and the first one is left blank, a * DATE_INVALID will be return from the call to validateRequiredFutureDate * If this occurs give an alert to the user * * @param theFormName The name of the form containing the elements that * are validated on * @param theBeforeDate The name of the element containing the date which should * be before the after date * @param theAfterDate The name of the element containing the date which should * be after the before date */ function validateFutureExpirationDate(theFormName, theBeforeDate, theAfterDate) { aReturnValue = validateRequiredFutureDate(theFormName, theBeforeDate, theAfterDate); if(aReturnValue == DATE_ANACHRONISTIC) { alert("The expiration date must be a date after the effective date."); return false; } if(aReturnValue == DATE_INVALID) { alert("The expiration date is only valid if there is an effective date."); return false; } return true; } /** * Function to make sure that the beforeDate is before the current date * * @param theFormName The name of the form containing the elements that * are validated on * @param theBeforeDate The name of the element containing the date which should * be before the current date */ function validateFutureAdvanceDate(theFormName, theBeforeDate) { // get all the month, day, year elements based on theBeforeDate // name tacked on with .month, .day, and .year aBeforeDaysObject = document.forms[theFormName].elements[theBeforeDate + ".day"]; aBeforeMonthObject = document.forms[theFormName].elements[theBeforeDate + ".month"]; aBeforeYearObject = document.forms[theFormName].elements[theBeforeDate + ".year"]; aBeforeMonth = aBeforeMonthObject[aBeforeMonthObject.selectedIndex].value; aBeforeYear = aBeforeYearObject[aBeforeYearObject.selectedIndex].value; aBeforeDay = aBeforeDaysObject[aBeforeDaysObject.selectedIndex].value; // test to see if the date field is empty // if so don't bother doing any validation // just return true if(!nonBlankDate(aBeforeYear, aBeforeMonth, aBeforeDay)) { return true; } aCurrentMonth = 4; aCurrentYear = 2024; aCurrentDay = 6; // create Date object for the 2 dates to make comparison convenient aBeforeDate = new Date(aBeforeYear, aBeforeMonth, aBeforeDay); aCurrentDate = new Date(aCurrentYear, aCurrentMonth, aCurrentDay); // Do the check to make sure the date given as a parameter // is greater than the current date. // If the test fails give an alert message and place focus on the // invalid date form element if(aBeforeDate.getTime() <= aCurrentDate.getTime()) { alert("The date must be in the future."); aBeforeDaysObject.focus(); return false; } return true; } /** * Function to make sure that the one date is after another * * @param theFormName The name of the form containing the elements that * are validated on * @param theBeforeDate The name of the element containing the date which should * be before the after date * @param theAfterDate The name of the element containing the date which should * be after the before date */ function validateFutureDate(theFormName, theBeforeDate, theAfterDate) { // get all the month, day, year elements based on theBeforeDate // name tacked on with .month, .day, and .year aBeforeDaysObject = document.forms[theFormName].elements[theBeforeDate + ".day"]; aBeforeMonthObject = document.forms[theFormName].elements[theBeforeDate + ".month"]; aBeforeYearObject = document.forms[theFormName].elements[theBeforeDate + ".year"]; aBeforeMonth = aBeforeMonthObject[aBeforeMonthObject.selectedIndex].value; aBeforeYear = aBeforeYearObject[aBeforeYearObject.selectedIndex].value; aBeforeDay = aBeforeDaysObject[aBeforeDaysObject.selectedIndex].value; // get all the month, day, year elements based on theAfterDate // name tacked on with .month, .day, and .year aAfterDaysObject = document.forms[theFormName].elements[theAfterDate + ".day"]; aAfterMonthObject = document.forms[theFormName].elements[theAfterDate + ".month"]; aAfterYearObject = document.forms[theFormName].elements[theAfterDate + ".year"]; aAfterMonth = aAfterMonthObject[aAfterMonthObject.selectedIndex].value; aAfterYear = aAfterYearObject[aAfterYearObject.selectedIndex].value; aAfterDay = aAfterDaysObject[aAfterDaysObject.selectedIndex].value; if(!nonBlankDate(aBeforeYear, aBeforeMonth, aBeforeDay)) { aBeforeDaysObject.focus(); return DATE_INVALID; } if(!nonBlankDate(aAfterYear, aAfterMonth, aAfterDay)) { aAfterDaysObject.focus(); return DATE_INVALID; } aBeforeDate = new Date(aBeforeYear, aBeforeMonth, aBeforeDay); aAfterDate = new Date(aAfterYear, aAfterMonth, aAfterDay); if(aBeforeDate.getTime() > aAfterDate.getTime()) { return DATE_ANACHRONISTIC; } return DATE_OK; } /** * Function to make sure that the one date is after another. This function * also allows: * 1. Both dates can be blank * 2. Second date can be blank, and no validation will occur * 3. The second date can not be filled in if the first is left empty * * @param theFormName The name of the form containing the elements that * are validated on * @param theBeforeDate The name of the element containing the date which should * be before the after date * @param theAfterDate The name of the element containing the date which should * be after the before date */ function validateRequiredFutureDate(theFormName, theBeforeDate, theAfterDate) { // get all the month, day, year elements based on theBeforeDate // name tacked on with .month, .day, and .year aBeforeDaysObject = document.forms[theFormName].elements[theBeforeDate + ".day"]; aBeforeMonthObject = document.forms[theFormName].elements[theBeforeDate + ".month"]; aBeforeYearObject = document.forms[theFormName].elements[theBeforeDate + ".year"]; aBeforeMonth = aBeforeMonthObject[aBeforeMonthObject.selectedIndex].value; aBeforeYear = aBeforeYearObject[aBeforeYearObject.selectedIndex].value; aBeforeDay = aBeforeDaysObject[aBeforeDaysObject.selectedIndex].value; // get all the month, day, year elements based on theAfterDate // name tacked on with .month, .day, and .year aAfterDaysObject = document.forms[theFormName].elements[theAfterDate + ".day"]; aAfterMonthObject = document.forms[theFormName].elements[theAfterDate + ".month"]; aAfterYearObject = document.forms[theFormName].elements[theAfterDate + ".year"]; aAfterMonth = aAfterMonthObject[aAfterMonthObject.selectedIndex].value; aAfterYear = aAfterYearObject[aAfterYearObject.selectedIndex].value; aAfterDay = aAfterDaysObject[aAfterDaysObject.selectedIndex].value; // check for disabled date fields, in which case validation is // not neccessary if(aBeforeDaysObject.disabled || aAfterDaysObject.disabled) { return DATE_OK; } // if the second date is empty there is nothing to validate so // return true if(!nonBlankDate(aAfterYear, aAfterMonth, aAfterDay)) { return DATE_OK; } // if the second date is filled in, it must be validated against the // first date. So if the first date is left empty return an error if(!nonBlankDate(aBeforeYear, aBeforeMonth, aBeforeDay) && nonBlankDate(aAfterYear, aAfterMonth, aAfterDay)) { aBeforeDaysObject.focus(); return DATE_INVALID; } aBeforeDate = new Date(aBeforeYear, aBeforeMonth, aBeforeDay); aAfterDate = new Date(aAfterYear, aAfterMonth, aAfterDay); if(aBeforeDate.getTime() > aAfterDate.getTime()) { aAfterDaysObject.focus(); return DATE_ANACHRONISTIC; } return DATE_OK; } function validateNonrequiredFutureDate(theFormName, theBeforeDate, theAfterDate) { // get all the month, day, year elements based on theBeforeDate // name tacked on with .month, .day, and .year aBeforeDaysObject = document.forms[theFormName].elements[theBeforeDate + ".day"]; aBeforeMonthObject = document.forms[theFormName].elements[theBeforeDate + ".month"]; aBeforeYearObject = document.forms[theFormName].elements[theBeforeDate + ".year"]; aBeforeMonth = aBeforeMonthObject[aBeforeMonthObject.selectedIndex].value; aBeforeYear = aBeforeYearObject[aBeforeYearObject.selectedIndex].value; aBeforeDay = aBeforeDaysObject[aBeforeDaysObject.selectedIndex].value; // get all the month, day, year elements based on theAfterDate // name tacked on with .month, .day, and .year aAfterDaysObject = document.forms[theFormName].elements[theAfterDate + ".day"]; aAfterMonthObject = document.forms[theFormName].elements[theAfterDate + ".month"]; aAfterYearObject = document.forms[theFormName].elements[theAfterDate + ".year"]; aAfterMonth = aAfterMonthObject[aAfterMonthObject.selectedIndex].value; aAfterYear = aAfterYearObject[aAfterYearObject.selectedIndex].value; aAfterDay = aAfterDaysObject[aAfterDaysObject.selectedIndex].value; if(!nonBlankDate(aBeforeYear, aBeforeMonth, aBeforeDay)) { aBeforeDaysObject.focus(); return DATE_INVALID; } if(!nonBlankDate(aAfterYear, aAfterMonth, aAfterDay)) { return DATE_INVALID; } aBeforeDate = new Date(aBeforeYear, aBeforeMonth, aBeforeDay); aAfterDate = new Date(aAfterYear, aAfterMonth, aAfterDay); if(aBeforeDate.getTime() > aAfterDate.getTime()) { return DATE_ANACHRONISTIC; } return DATE_OK; } /** * Function to make required date field if another date field is filled * * @param theFormName The name of the form containing the elements that * are validated on * @param theRequiredDate The name of the element containing the date which should * be required * @param theFilledDate The name of the element containing the date which can be * filled */ function validateRequiredDateIfDateFilled(theFormName, theRequiredDate, theFilledDate) { // get the day element based on theFilledDate aFilledDaysObject = document.forms[theFormName].elements[theFilledDate + ".day"]; aFilledDay = aFilledDaysObject[aFilledDaysObject.selectedIndex].value; // if theFilledDate is not filled in, theRequiredDate isn't required if (aFilledDay == "") { return true; } // get the day element based on theRequiredDate aRequiredDaysObject = document.forms[theFormName].elements[theRequiredDate + ".day"]; aRequiredDay = aRequiredDaysObject[aRequiredDaysObject.selectedIndex].value; if (aRequiredDay == "") { alert("Please fill in the date."); aRequiredDaysObject.focus(); return false; } return true; } /** * Convenience method used to ensure that given date is a valid date by * making sure the the fields are all nonblank. */ function nonBlankDate(theYear, theMonth, theDay) { return (theYear != "" && theMonth != "" && theDay != ""); } /** * Given two select boxes, when one index changes it chooses the * other option * */ function changeDelimeter(theFormName, theChangeSelect, theAffectedSelect){ var aChangeObject = document.forms[theFormName].elements[theChangeSelect]; var aAffectedObject = document.forms[theFormName].elements[theAffectedSelect]; aAffectedObject.selectedIndex=aChangeObject.selectedIndex; } /** * Returns the number of days in a month depending on the month and year * * @param theMonth Month for which the day is being determined * @param theYear Year which will be checked for leap year status * to help determine the number of months in Feb * @return the number of days in the given month */ function daysInMonth(theMonth, theYear) { // increment month to handle the months starting at 0 on the html page ++theMonth; var aDaysInMonth = 31; if (theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11) aDaysInMonth = 30; if (theMonth == 2 && (theYear/4) != Math.floor(theYear/4)) { aDaysInMonth = 28; } if (theMonth == 2 && (theYear/4) == Math.floor(theYear/4)) { aDaysInMonth = 29; } return aDaysInMonth+1; } /** * Function to change the available days in a months. The form name * and the base element name of the 3 fields used to display the day, month, * and year are required. And the name of the 3 form fields are expected to * be of the format theBaseDate.day, theBaseDate.month, and theBaseDate.year. * Depending on the values in the month and year fields the options of the * day field change. The options of the day field will generally grow and shrink * unbeknownst to the user. The only visible change will occur when a user has * selected a day that is greater then the number of days allowed in the newly * selected month. In that case the day field will be reset to 1. * * @param theFormName The form name to which the date elements belong * @param theBaseDate This is the base name of the 3 date fields, which * must be of the format of theBaseDate.day, * theBaseDate.month, theBaseDate.year */ function changeOptionDays(theFormName, theBaseDate) { aDaysObject = document.forms[theFormName].elements[theBaseDate + ".day"]; aMonthObject = document.forms[theFormName].elements[theBaseDate + ".month"]; aYearObject = document.forms[theFormName].elements[theBaseDate + ".year"]; aMonth = aMonthObject[aMonthObject.selectedIndex].value; aYear = aYearObject[aYearObject.selectedIndex].value; aDaysForThisSelection = daysInMonth(aMonth, aYear); aCurrentDaysInSelection = aDaysObject.length; if (aCurrentDaysInSelection > aDaysForThisSelection) { for (i=0; i<(aCurrentDaysInSelection-aDaysForThisSelection); i++) { aDaysObject.options[aDaysObject.options.length - 1] = null } } if (aDaysForThisSelection > aCurrentDaysInSelection) { for (i=0; i<(aDaysForThisSelection-aCurrentDaysInSelection); i++) { aNewOption = new Option(aDaysObject.options.length); aDaysObject.options[aDaysObject.options.length] = aNewOption; } } if (aDaysObject.selectedIndex < 0) { alert(aDaysObject.selectedIndex); aDaysObject.selectedIndex = 0; } } var DATE_OK = 0; var DATE_INVALID = 1; var DATE_ANACHRONISTIC = 2; /** * Validates the date field. Returns true if all fields are filled or all * fields are empty. In other words, returns false if the date field is * partially filled. */ function validateDate(theFormName, theFieldName) { aDaysObject = document.forms[theFormName].elements[theFieldName + ".day"]; aMonthObject = document.forms[theFormName].elements[theFieldName + ".month"]; aYearObject = document.forms[theFormName].elements[theFieldName + ".year"]; aMonth = aMonthObject.value; aYear = aYearObject.value; aDays = aDaysObject.value; if(aDays == "" && aMonth == "" && aYear == "" || aDays != "" && aMonth != "" && aYear != "") { return true; } else { aDateField=document.forms[theFormName].elements[theFieldName]; aDateField.focus(); alert("Date in wrong format."); return false; } } function fillInDate(theDateField,theFormName,theFieldName){ var aDaysObject = document.forms[theFormName].elements[theFieldName + ".day"]; var aMonthObject = document.forms[theFormName].elements[theFieldName + ".month"]; var aYearObject = document.forms[theFormName].elements[theFieldName + ".year"]; var aArrayOfDate=theDateField.value.split('/'); if( theDateField.value == null || theDateField.value == ""){ aMonthObject.value=-1; aDaysObject.value=-1; aYearObject.value=-1; return; } if(aArrayOfDate.length > 3 || aArrayOfDate.length ==1 || aArrayOfDate.length == 2){ alert("Date is in incorrect format"); aMonthObject.value=""; theDateField.focus(); }else{ aMonthObject.value=aArrayOfDate[0]; aDaysObject.value=aArrayOfDate[1]; aYearObject.value=aArrayOfDate[2]; if(aMonthObject.value.length == 1){ aMonthObject.value = 0 + aMonthObject.value; } if(aDaysObject.value.length == 1){ aDaysObject.value = 0 + aDaysObject.value; } if(aYearObject.value.length == 2){ aYearObject.value = 20 + aYearObject.value; } theDateField.value=aMonthObject.value+"/"+aDaysObject.value+"/"+aYearObject.value; } } function fillInToday(theDateField,theFormName,theFieldName) { var now = new Date(); theDateField.value = (now.getMonth()+1)+"/"+now.getDate()+"/"+now.getFullYear(); fillInDate(theDateField, theFormName, theFieldName); theDateField.focus(); } /** * validates military time format */ function validateMilitaryTime(theInputElement){ var aValue = theInputElement.value; if ((aValue == null) || (trim(aValue).length == 0)) return true; if ((aValue.length == 5) && (aValue.charAt(2) == ":")){ var aHours = aValue.substring(0, 2); var aMins = aValue.substring(3, 5); if ((aHours >= "00") && (aHours <= "23") && (aMins >= "00") && (aMins <= "59")){ return true; } } alert(aValue + "Expected time format (e.g.: 02:30, 21:59)"); theInputElement.value = theInputElement.defaultValue; theInputElement.focus(); return false; } /** * Multiplies input elements * * @param theFirstInputElement First element to multiply * @param theSecondInputElement Second element to multipy * @param theResultantElement The resultant of multiplying the first and second element * * @retun boolean Whether the test passed or failed */ function multiplyElements(theFirstInputElement, theSecondInputElement,theResultantElement) { var aVal1 = theFirstInputElement.value; var aVal2 = theSecondInputElement.value; var aVal3=aVal1*aVal2 theResultantElement.value=aVal3; } /** * cheap way to check if object is array */ function isArray(obj) { //returns true is it is an array if (obj.length) return true; else return false; } /** * Function makes sure the the required field elements specified in the requiredList are filled in and prompts * the user to fill in any of the required elements which are blank. * requiredList is an array of Required Objects, which contain the name of each required element and * the prompt given to the user associated with each element. * * @return boolean */ function checkForRequired(theForm) { updateRTEs(); if(requiredList.length > 0){ for(var i = 0; i < requiredList.length; i++) { var name = requiredList[i].formname; if (name == theForm) { if (isArray(requiredList[i].name)) { var aRArry = requiredList[i].name; if (aRArry.length > 0) { if (aRArry[0].type == "radio") { var aIsOneSelected = false; for (var j = 0; j < aRArry.length; j++ ) { if (aRArry[j].checked) { aIsOneSelected = true; break; } } if (!aIsOneSelected) { alert("Please fill in " + requiredList[i].prompt + "." ); try { aRArry[0].focus(); } catch (e) { } return false; } } else { if(requiredList[i].name.disabled) { return true; } var val = requiredList[i].name.value; if((val == null) || (trim(val).length == 0)) { alert("Please fill in " + requiredList[i].prompt + "." ); try { requiredList[i].name.focus(); } catch (e) { } return false; } } } } else { if(requiredList[i].name.disabled) { return true; } var val = requiredList[i].name.value; if((val == null) || (trim(val).length == 0)) { alert("Please fill in " + requiredList[i].prompt + "." ); try { requiredList[i].name.focus(); } catch (e) { } return false; } } } } return true; } else { return true; } } /** * Function to add the Required Object to the global required list. The requiredList is used to * assure that all required fields are filled in if there are any. The requiredList is checked upon * submission using checkForRequired(). * * @param RequiredField the field that we want to gaurantee a value */ function addRequiredField(theInputElement) { requiredList = requiredList.concat( theInputElement ); } /** * Object to contain the name of the required form element, and the name to display to the user when * prompting them that the field is required * * @param theInputElementName The name of the form element * @param thePrompt The prompt that the user sees when the field is not filled out. * *function RequiredField(theInputElementName, thePrompt) { * this.name = theInputElementName; * this.prompt = thePrompt; *} */ function RequiredField(theInputElementName, thePrompt, theFormName) { this.formname = theFormName; this.name = theInputElementName; this.prompt = thePrompt; } /** * Function to disable or enable a form element * * @param theInputElement Form element to be disabled or enabled * @param theIsDisabled Boolean given as true for disabling * or false for enabling * * @todo Make this work for Netscape */ function disableOrEnableField(theInputElement, theIsDisabled) { theInputElement.disabled = theIsDisabled; } /** * This is a row object. Contains values for a row * */ function Row(){ var rowMemberArray = new Array(); /** * Adds the row member */ this.addRowMember = function addRowMember(theInputElement){ //rowMemberArray.push(theInputElement); rowMemberArray[rowMemberArray.length] = theInputElement; }; this.getSize = function size(){ return rowMemberArray.length; }; /** * Get rowmember array */ this.getRowMemberArray = function(){ return rowMemberArray; } } /** * requiredList is an Array which is filled with Required objects, used as the list of required fields in a form */ var requiredList = new Array(); /** * Array of Validation to be performed */ var validationList = new Array(); /** * Function to add the validation function to the list of validations * * @param theValidationFunction The function call to be passed in as a String * i.e checkValidNumber(theElementToCheck) */ function addValidationFunctions(theValidationFunction) { validationList[validationList.length] = theValidationFunction; } /** * Function to kick off all the validation functions */ function runValidationScripts() { if(validationList.length > 0) { for(var i = 0; i < validationList.length; i++) { var validationScript = validationList[i]; if(eval(validationScript) == false) { return false; } } return true; } else { return true; } } /** * methods below are for select update * */ function clearList(list) { var i = 0; var o = list.options; for (i = o.length; i >= 0; --i) o[i] = null; list.disabled = true; } function addElement(list, text_in, value_in) { var o = list.options; var nIdx; if (o.length < 0) //IE for Mac 4.5 sets length to -1 if list is empty nIdx = 0; else nIdx = o.length; o[nIdx] = new Option(text_in, value_in); list.disabled = false; } function addElementAtPos(list, pos, text_in, value_in) { var o = list.options; var nIdx = 0; if ((pos < 0) || (pos > o.length)) return; addElement(list, '', ''); for (nIdx = o.length - 1; nIdx > pos; nIdx--) { o[nIdx].text = o[nIdx - 1].text; o[nIdx].value = o[nIdx - 1].value; } o[pos] = new Option(text_in, value_in); list.disabled = false; } function setDefaultByText(list, text_in) { with (list) { for (var i = 0; i < (options.length); i++) { if (options[i].text == text_in) { selectedIndex = i; return; } } } } function setDefaultByValue(list, value_in) { with (list) { for (var i = 0; i < (options.length); i++) { if (options[i].value == value_in) { selectedIndex = i; return; } } } } function setDefaultByPosition(list, aPosition){ with (list) { selectedIndex=aPosition; } } /** * Updates a child select list. theParentValue is used as a key to * lookup an array in theParentChildArray. theParentChildArray is an * array of arrays. The looked up array is used to fill in theChildList. * theChildList should be a select field. * */ function updateChild(theParentValue,theParentChildArray, theChildValueArray,theChildList){ aLookedupArray = theParentChildArray[theParentValue]; var i =0; for(;i'; document.getElementById('dialog-body').contentWindow.dialogArguments = arg; document.getElementById('dialog-close').addEventListener('click', function(e) { e.preventDefault(); dialog.close(); }); if (typeof dialog.showModal === "function") { dialog.showModal(); } else { if (dialogPolyfill) { dialogPolyfill.registerDialog(dialog); dialog.showModal(); } else { alert("The dialog API is not supported"); } } //if using yield or async/await if(caller.indexOf('yield') >= 0 || caller.indexOf('await') >= 0) { return new Promise(function(resolve, reject) { dialog.addEventListener('close', function() { var returnValue = document.getElementById('dialog-body').contentWindow.returnValue; document.body.removeChild(dialog); resolve(returnValue); }); }); } //if using eval var isNext = false; var nextStmts = caller.split('\n').filter(function(stmt) { if(isNext || stmt.indexOf('showModalDialog(') >= 0) return isNext = true; return false; }); dialog.addEventListener('close', function() { var returnValue = document.getElementById('dialog-body').contentWindow.returnValue; document.body.removeChild(dialog); nextStmts[0] = nextStmts[0].replace(/(window\.)?showModalDialog\(.*\)/g, JSON.stringify(returnValue)); eval('{\n' + nextStmts.join('\n')); }); throw 'Execution stopped until showModalDialog is closed'; }; } function createPopup(thePopupViewName, theInputField, theUserFieldName, theFieldName, theFieldNameValue){ var aQueryString = "/control/?viewName=popupframe&popupPage="+thePopupViewName+"&"+theFieldName+"="+theFieldNameValue; if(theFieldName != null && theFieldNameValue == ""){ alert("Please input a value for:" +theUserFieldName); return; } aValue = showModalDialog(aQueryString,"", "dialogHeight:300px; dialogWidth:700px; resizable:yes; status:no; toolbar:no; help:no; center:yes; border:thin; menubar:no;"); if(aValue != null){ theInputField.value=aValue; theInputField.onblur(); } } /** * Submittal scripts * */ function returnRevNumber(theForm){ var i=0; var aNumberOfRows = theForm.numberOfRows.value - 0; var aLargestNumber = 0; for(i;i