// $Id: mff2.js 73028 2008-04-21 20:49:43Z dselfrid $ /* JAVSCRIPT SOURCE FILE 2/3 PLEASE MAKE SURE THAT THESE FILES DO NOT CROSS the 32K limit DATE AUTHOR DESCRIPTION 102097 TOM New functions JS_CheckDate, JS_IsDayValid, JS_IsMonthValid 102197 TOM New functions JS_CheckState, JS_UnCheckState 102397 TOM New function JS_EvaluateExpr, bug in JS_Passbackvalue 102997 TOM Added MMDDYYYY input mask to JS_FormatDate 122997 TOM Changed the default date format to MMDDYYYY. Optionally you can specify the input mask also. 012798 TOM Added function JS_CheckTextLength, JS_RPad 012998 TOM Added function JS_CheckUncheck 020698 TOM Modified JS_CheckDate 022498 TOM Added function JS_Round 110899 PRAYI Modified JS_Round (avoids the error if precision=0, pads ZEROS while rounding up) 060998 TOM New functions JS_ActivateURL and JS_CreateDelimitted 080398 TOM JS_CheckDate, JS_IsDateGreater, JS_IsDateLesser changed */ //---------------------------OTHER FUNCTIONS----------------------// function JS_ActivateURL (sURL) { window.location.href = sURL; } function JS_CreateDelimitted (iForm, sFieldName, sFieldType, sSeparator, iNumChars) { var sThisForm = document.forms[iForm]; var sDelmt = ''; var iCounter = 0; var lSep = ''; var lFieldVal = ''; if (sFieldType == "CHECKBOX") { for (var i=0; i < sThisForm.elements.length; i++) { if (sThisForm.elements[i].name == sFieldName) { if (sThisForm.elements[i].checked) { lFieldVal = sThisForm.elements[i].value; if (lFieldVal != '') { iCounter++; if (iCounter == 2) {lSep = ((!sSeparator) ? "," : sSeparator)}; if (!iNumChars) { sDelmt = sDelmt + lSep + lFieldVal; } else { sDelmt = sDelmt + lSep + lFieldVal.substring(0,iNumChars); } } } } } } if ((sFieldType == "SELECT-ONE") || (sFieldType == "SELECT-MULTIPLE")) { for (var i=0; i < sThisForm.elements[sFieldName].options.length; i++) { if (sThisForm.elements[sFieldName].options[i].selected == true) { lFieldVal = sThisForm.elements[sFieldName].options[i].value; if (lFieldVal != '') { iCounter++; if (iCounter == 2) {lSep = ((!sSeparator) ? "," : sSeparator)}; if (!iNumChars) { sDelmt = sDelmt + lSep + lFieldVal; } else { sDelmt = sDelmt + lSep + lFieldVal.substring(0,iNumChars); } } } } } if (sFieldType == "TEXT") { for (var i=0; i < sThisForm.elements.length; i++) { if (sThisForm.elements[i].name == sFieldName) { lFieldVal = sThisForm.elements[i].value; if (lFieldVal != '') { iCounter++; if (iCounter == 2) {lSep = ((!sSeparator) ? "," : sSeparator)}; if (!iNumChars) { sDelmt = sDelmt + lSep + lFieldVal; } else { sDelmt = sDelmt + lSep + lFieldVal.substring(0,iNumChars); } } } } } return sDelmt; } function JS_EvaluateExpr(sExpr, sMsg, sFocusField, sField) { var sThisForm = document.forms[0]; var TestValue = null; //if atleast one is null then exit gracefully for (var i=3; i sMax) { if (confirm('The max allowable characters for this field is ' + sMax + '.\nClick OK to get first ' + sMax + ' characters or CANCEL to go back and change it')) { sString.value = sString.value.substring(0,sMax-1); return true; } else { sString.focus(); return false; } } return true; } function JS_CheckState(CheckboxName, Setting) { var index; var sThisForm=document.forms[0]; for (var index=0 ; index < sThisForm.elements.length ; index++ ) { if (sThisForm.elements[index].name == CheckboxName) { if ( Setting == "ALL" ) { sThisForm.elements[index].checked = true; } if ( Setting == "TOGGLE" ) { sThisForm.elements[index].click(); } if ( Setting == "NONE" ) { sThisForm.elements[index].checked = false; } } } //if the hidden variable exists then change value if (sThisForm.L_SELECTALL) { sThisForm.L_SELECTALL.value = Setting; } } function JS_UnCheckState (sSelect, sCheckBox, sCheckBoxValue) { sThisForm = document.forms[0]; var Select = sThisForm.elements[sSelect]; Select.options[Select.selectedIndex].selected = true; var SelectValue = Select.options[Select.selectedIndex].value; //if sCheckBox is misssing then just select the selection and getout if (!sCheckBox) {return}; var CheckBox = sThisForm.elements[sCheckBox]; if (SelectValue != '') { //function called from select list if (!sCheckBoxValue) { if (sThisForm.L_SELECTALL.value == "ALL") { JS_CheckState(sCheckBox, 'ALL'); } //because of bug in IE we have to do this for (var i=0; i < sThisForm.elements.length; i++) { if (sCheckBox==sThisForm.elements[i].name) { if (SelectValue == sThisForm.elements[i].value) { sThisForm.elements[i].checked = false; return; } } } } //function called from checkbox else { if (SelectValue == sCheckBoxValue) { alert ('You cannot qualify the state of incorporation.'); //because of bug in IE we have to do this for (var i=0; i < sThisForm.elements.length; i++) { if (sCheckBox==sThisForm.elements[i].name) { if (SelectValue == sThisForm.elements[i].value) { sThisForm.elements[i].checked = false; return; } } } return; } } } return; } //---------------------------OTHER FUNCTIONS END----------------------// //---------------------------DATE FUNCTIONS----------------------// function JS_MonthArray (sIndex, sMonth, sDay) { this.Index = sIndex; this.Month = sMonth; this.Day = sDay; } function JS_CreateMonthArray () { MonthArray = new JS_MonthArray (11); MonthArray[0] = new JS_MonthArray ("01", "January", 31); MonthArray[1] = new JS_MonthArray ("02", "February", 29); MonthArray[2] = new JS_MonthArray ("03", "March", 31); MonthArray[3] = new JS_MonthArray ("04", "April", 30); MonthArray[4] = new JS_MonthArray ("05", "May", 31); MonthArray[5] = new JS_MonthArray ("06", "June", 30); MonthArray[6] = new JS_MonthArray ("07", "July", 31); MonthArray[7] = new JS_MonthArray ("08", "August", 31); MonthArray[8] = new JS_MonthArray ("09", "September", 30); MonthArray[9] = new JS_MonthArray ("10", "October", 31); MonthArray[10] = new JS_MonthArray ("11", "November", 30); MonthArray[11] = new JS_MonthArray ("12", "December", 31); } function JS_IsDayValid (sMonth, sDay) { JS_CreateMonthArray (); if (sDay <= 0) {return false}; if (sDay > (MonthArray[JS_GetMonthDigit(sMonth) - 1].Day)) {return false}; return true; } function JS_IsMonthValid (sMonth) { if (sMonth == "") {return true}; sMonth = sMonth.toUpperCase(); JS_CreateMonthArray (); for (var i=0; i<12; i++) { if (sMonth == MonthArray[i].Month.toUpperCase()) {return true}; } alert ('Invalid month entered.\nPlease check and try again.'); return false; } function JS_isNumber(element1) { for (var i=0; i <= (element1.length - 1); i++) { if (element1.charAt(i) < "0" || element1.charAt(i) > "9") { msg = "\n'" + element1 + "' is an invalid number. \nPlease enter the Number."; alert (msg); return false; } } return true; } function JS_CheckDate(sFocus, sMonth, sDay, sYear, sDateMode) { var sThisForm = document.forms[0]; var sFocusField = document.forms[0].elements[JS_FindAnswerKey(sFocus)]; var CheckDate = new Object; sMonth = sMonth.toUpperCase(); var lCheck = ((!sMonth||sMonth=='') ? '' : 'M') + ((!sDay||sDay=='') ? '' : 'D') + ((!sYear||sYear=='') ? '' : 'Y'); if ((lCheck == 'D') || (lCheck == 'M') || (lCheck == 'DY')) { alert ('Please enter a valid date to continue.'); sFocusField.focus(); return false; } //if day is invalid or year is invalid then alert if (!JS_CheckIntegerVal(sDay)) {sFocusField.focus(); return false;}; if (!JS_CheckIntegerVal(sYear)) {sFocusField.focus(); return false;}; //if Month is empty then assign one if (!sMonth||sMonth == '') { if ((sDateMode == 'PDATE') || (sDateMode == 'PEDATE')){ sMonth = 'JANUARY'; } else { sMonth = 'DECEMBER'; } } //if Day is empty then assign one if (sDay == '') { if ((!sDateMode) || (sDateMode == 'PDATE') || (sDateMode == 'PEDATE')) { sDay = "01"; } else { JS_CreateMonthArray (); sDay = MonthArray[JS_GetMonthDigit(sMonth) - 1].Day; if (sMonth == 'FEBRUARY') {sDay = "28";} } } //If month is february and year is empty allow only 28 days if ((sMonth == 'FEBRUARY') && (!sYear) && (sDay > 28)) { alert ('February can have only 28 days.\nPlease check and try again.'); sFocusField.focus(); return false; } //Check Month versus day if (JS_IsDayValid (sMonth, sDay)) { //if year is empty then return true if (!sYear || (sYear == "")) { return true; } } else { alert (sMonth + ' cannot have ' + sDay + ' days.\nPlease check and try again.'); sFocusField.focus(); return false; } if (!(sYear) || (sYear == '')) {return true}; //get the year in YYYY format if (sYear.length == 2) { if (sYear > 50) sYear = '20' + sYear; else sYear = '19' + sYear; } //Now check for future, past or regular date CheckDate.value = JS_GetMonthDigit(sMonth) + sDay + sYear; if (!sDateMode) { if (!JS_IsDate(CheckDate, sFocus)) {sFocusField.focus(); return false;}; return true; } if ((sDateMode == 'FDATE') || (sDateMode == 'FEDATE')){ if (!JS_IsDateGreater(CheckDate, sFocus, 'MMDDYYYY', sDateMode )) {sFocusField.focus(); return false;}; } if ((sDateMode == 'PDATE') || (sDateMode == 'PEDATE')){ if (!JS_IsDateLesser(CheckDate, sFocus, 'MMDDYYYY', sDateMode)) {sFocusField.focus(); return false}; } return true; } function JS_GetMonthDigit(sMonthName) { sMonthName = sMonthName.toUpperCase(); JS_CreateMonthArray (); for (var i=0; i<12; i++) { if (sMonthName==MonthArray[i].Month.toUpperCase()) { return MonthArray[i].Index; } } return ""; } function JS_GetMonth(sMonthDigit) { MonthDigit = parseFloat(sMonthDigit); if (MonthDigit>12 || MonthDigit<1 ||isNaN(MonthDigit)) {return ""}; JS_CreateMonthArray (); var ReturnMonth = MonthArray[MonthDigit-1].Month; return ReturnMonth; } function JS_FormatDate(sDateField, sOutputMask, sInputMask) { var tDay; var tMonth; var tYear; var FormatDate =""; //just incase the date passed is empty if (JS_IsEmpty(sDateField) == true) { return ""; } var DateField = sDateField.toUpperCase(); //First convert the date field to default MMDDYYYY format //based on the input mask provided. if (JS_FormatDate.arguments.length == 3) { if (sInputMask == "DD") DateField = "01" + DateField + "1000"; else if (sInputMask == "MM") DateField = DateField + "011000"; else if (sInputMask == "MON") DateField = JS_GetMonthDigit (DateField) + "011000"; else if (sInputMask == "YY") { //if the year is less than 50 assume it as 21th century else 20st century if (DateField < 50) DateField = "010120" + DateField; else DateField = "010119" + DateField; } else if (sInputMask == "YYYY") DateField = "0101" + DateField; else if (sInputMask == "MMDDYY") { //if the year is less than 50 assume it as 21th century else 20st century tYear = DateField.charAt(4) + DateField.charAt(5); if (tYear < 50) { tYear = "20" + tYear; } else { tYear = "19" + tYear; } DateField = DateField.charAt(0) + DateField.charAt(1) + DateField.charAt(2) + DateField.charAt(3) + tYear; } else if (sInputMask == "MMDDYYYY") DateField = DateField; } //Note the default is MMDDYYYY format if (sOutputMask=="DD") FormatDate= (DateField.charAt(2)!="0" ? DateField.charAt(2) : "") + DateField.charAt(3); else if (sOutputMask=="MM") FormatDate= (DateField.charAt(0)!="0" ? DateField.charAt(0) : "") + DateField.charAt(1); else if (sOutputMask=="MON") { tMonth=DateField.charAt(0) + DateField.charAt(1); FormatDate=JS_GetMonth(tMonth); } else if (sOutputMask=="YY") FormatDate=DateField.charAt(6) + DateField.charAt(7); else if (sOutputMask=="YYYY") { FormatDate=DateField.charAt(4) + DateField.charAt(5) + DateField.charAt(6) + DateField.charAt(7); } else if (sOutputMask=="MON DD,YYYY") { tDay= (DateField.charAt(2) != "0" ? DateField.charAt(2) : "") + DateField.charAt(3); tMonth=DateField.charAt(0) + DateField.charAt(1); tYear=DateField.charAt(4) + DateField.charAt(5)+ DateField.charAt(6) + DateField.charAt(7); //now format the date FormatDate=JS_GetMonth(tMonth) + " " + tDay + ", " + tYear; } return FormatDate; } function JS_IsDate(sDateField, sFocus, sInputMask) { if (!sInputMask) { sInputMask = 'MMDDYYYY'; } if (sDateField.value.length!=0) { if (!JS_CheckDateVal(sDateField.value, sInputMask)) { if (!sFocus) { sDateField.focus(); sDateField.value=""; sDateField.focus(); return false; } else { return false; } } } return true; } function JS_CheckDateVal(sDate, sInputMask) { var tDay; var tDate; var tYear; var msg = ""; //Exit the routine if length less than input mask length. The default format used is MMDDYYYY. if (sDate.length != sInputMask.length) { msg = "\n'" + sDate + "' is an invalid date. \nPlease enter the date in " + sInputMask + " format."; alert (msg); return false; } else { //Check all the characters before continuing for (i=0; i <= (sDate.length - 1); i++) { if (sDate.charAt(i) < "0" || sDate.charAt(i) > "9") { msg = "\n'" + sDate + "' is an invalid date. \nPlease enter the date in " + sInputMask + " format."; alert (msg); return false; } } //get the day, month and year variables tMonth = sDate.charAt(0) + sDate.charAt(1); tDay = sDate.charAt(2) + sDate.charAt(3); //if format is MMDDYY approximate the year else use full year if (sInputMask.length == 6) { tYear = sDate.charAt(4) + sDate.charAt(5); //check for the year if (tYear < 0 || tYear > 99) { msg = "\n'" + sDate + "' has invalid year. \nPlease enter the date in " + sInputMask + " format with year in the range 00 to 99."; alert (msg); return false; } //since we are using only YY of the year if year is between 00 and 49 //interpret as 2000 to 2049 else interpret as 1950-1999 if (tYear >= 0 && tYear < 50) tYear="20" + tYear; else tYear="19" + tYear; } else { tYear = sDate.charAt(4) + sDate.charAt(5) + sDate.charAt(6) + sDate.charAt(7); } //check for the month if (tMonth < 1 || tMonth > 12 ) { msg = "\n'" + sDate + "' has invalid month. \nPlease enter the date in " + sInputMask + " format with month in the range 01 to 12."; alert (msg); return false; } //check for the day else if (tDay < 1 || tDay > 31) { msg = "\n'" + sDate + "' has invalid day. \nPlease enter the date in " + sInputMask + " format with day in the range 01 to 31."; alert (msg); return false; } else //If the month is february if (tMonth == 02) { //Check for leap year also since the date is february if ((JS_IsLeapYear(tYear) == false) && (tDay > 28)) { msg = "\n'" + sDate + "' has invalid day for the month february. \nPlease enter the date in " + sInputMask + " format with day in the range 01 to 31."; alert (msg); return false; } else if ((JS_IsLeapYear(tYear) == true) && (tDay > 29)) { msg = "\n'" + sDate + "' has invalid day for the month february. \nPlease enter the date in " + sInputMask + " format with day in the range 01 to 31."; alert (msg); return false; } else return true; } //If month is september, april, june or november else if ((tMonth == 09 || tMonth == 04 || tMonth == 06 || tMonth == 11) && (tDay > 30)) { msg = "\n'" + sDate + "' has invalid day. \nPlease enter the date in " + sInputMask + " format with day in the range 01 to 31."; alert (msg); return false; } else { return true; } } } function JS_IsDateGreater(sDateField, sFocus, sInputMask, sMode) { var lMode = ((!sMode) ? 'FDATE' : sMode); //if input mask not specified the default to MMDDYYYY if (!sInputMask) { sInputMask = 'MMDDYYYY'; } if (!JS_IsDateValGreaterLesser(sDateField, lMode, sFocus, sInputMask)) { if (!sFocus) { sDateField.focus(); sDateField.value=""; sDateField.focus(); return false; } else { return false; } } else return true; } function JS_IsDateLesser(sDateField, sFocus, sInputMask, sMode) { var lMode = ((!sMode) ? 'PDATE' : sMode); //if input mask not specified the default to MMDDYYYY if (!sInputMask) { sInputMask = 'MMDDYYYY'; } if (!JS_IsDateValGreaterLesser(sDateField, lMode, sFocus, sInputMask)) { if (!sFocus) { sDateField.focus(); sDateField.value=""; sDateField.focus(); return false; } else { return false; } } else return true; } function JS_IsDateValGreaterLesser(sDateField, sMode, sFocus, sInputMask) { var CurDate = new Date(); var CurDateString = ""; var NewDateString = ""; var msg = ""; //if the date is empty then exit if (sDateField.value.length==0) return true; //first check if it is a valid date. if (!JS_IsDate(sDateField, sFocus, sInputMask)) return false; //due to a bug in navigator 2.0 we have to do this var FullYear = CurDate.getYear(); if (FullYear < 1000) FullYear += 1900; CurDateString = FullYear + "" + JS_LPad((CurDate.getMonth() + 1), 2, '0') + "" + JS_LPad(CurDate.getDate(), 2, '0'); if (sInputMask.length == 6) { NewDateString = JS_FormatDate(sDateField.value, "YYYY", sInputMask) + "" + JS_LPad (JS_FormatDate(sDateField.value, "MM", sInputMask), 2, '0') + "" + JS_LPad(JS_FormatDate(sDateField.value, "DD", sInputMask), 2, '0'); } else { NewDateString = JS_FormatDate(sDateField.value, "YYYY", sInputMask) + "" + JS_LPad (JS_FormatDate(sDateField.value, "MM", sInputMask), 2, '0') + "" + JS_LPad(JS_FormatDate(sDateField.value, "DD", sInputMask), 2, '0'); } msg = "\n'" + sDateField.value + "' is invalid. \nPlease enter a valid date in " + sInputMask + " format, "; if (sMode == "FDATE") { if (NewDateString > CurDateString) return true; else { msg = msg + "greater than current date."; alert(msg); return false; } } if (sMode == "FEDATE") { if (NewDateString >= CurDateString) return true; else { msg = msg + "greater than or equal to current date."; alert(msg); return false; } } if (sMode == "PDATE") { if (NewDateString < CurDateString) return true; else { msg = msg + "less than current date."; alert(msg); return false; } } if (sMode == "PEDATE") { if (NewDateString <= CurDateString) return true; else { msg = msg + "less than or equal to current date."; alert(msg); return false; } } } function JS_IsLeapYear(sYear) { //Check for leap year if (sYear % 4 == 0) { if (sYear % 100 == 0) { if (sYear % 400 == 0) return true; else return false; } else return true; } return false; } //---------------------------DATE FUNCTIONS END----------------------// //---------------------------NUMBER FUNCTIONS------------------------// function JS_IsInteger(sIntegerField) { if (sIntegerField.value.length!=0) { if (!JS_CheckIntegerVal(sIntegerField.value)) { sIntegerField.value=""; sIntegerField.focus(); return false; } } return true; } function JS_CheckIntegerVal(sNumber) { var ValidNumber = true; var TempChar = 0; var msg = ""; sNumber = sNumber + ""; if (JS_IsEmpty(sNumber) == true) return true; //do this check only if the number is still valid if (ValidNumber == true) { for (var i=0; i < sNumber.length; i++) { TempChar = sNumber.charAt(i); //check if all characters are in 0-9 if ((TempChar < '0') || (TempChar > '9')) { if (TempChar != ' ') { ValidNumber = false; break; } } } } if (ValidNumber == false) { msg = "\nThe number '" + sNumber + "' is invalid. \nPlease enter a valid number in this field."; alert (msg); return false; } else { return true; } } function JS_IsNumeric(sNumericField) { if (sNumericField.value.length!=0) { if (!JS_CheckNumericVal(sNumericField.value)) { sNumericField.value=""; sNumericField.focus(); return false; } } return true; } function JS_CheckNumericVal(sNumber) { var ValidNumber = true; var TempChar = 0; var msg = ""; var PeriodPos = 0; var more = 0; if (JS_IsEmpty(sNumber) == true) { ValidNumber = false; return true; } //check for first period PeriodPos = sNumber.indexOf('.'); var CommaStart = (PeriodPos == -1 ? sNumber.length-1 : PeriodPos-1); //check for more periods if (PeriodPos != -1) { if (sNumber.indexOf('.', PeriodPos + 1) != -1) { ValidNumber = false; } } //Make sure commas are before the period if (PeriodPos != -1) { more = sNumber.lastIndexOf(','); if (more > PeriodPos) { ValidNumber = false; } } //Check for commas if ((ValidNumber == true) && (more != -1)){ more = 0; for (var i = CommaStart ; i > 0; i--) { TempChar = sNumber.charAt(i); more++; if (TempChar == ',') { if (more !=4) { ValidNumber = false; break; } else { more = 0; } } } } //do this check only if the number is still valid if (ValidNumber == true) { for (var i=0; i < sNumber.length; i++) { TempChar = sNumber.charAt(i); //check if all characters are in 0-9 OR a period OR a comma if ((TempChar < '0') || (TempChar > '9')) { if ((TempChar != ' ') && (TempChar != '.') && (TempChar != ',')) { ValidNumber = false; break; } } } } if (ValidNumber == false) { msg = "\nThe number '" + sNumber + "' is invalid. \nPlease enter a valid number in this field."; alert (msg); return false; } else { return true; } } //---------------------------NUMBER FUNCTIONS END--------------------// //---------------------------GENERAL FUNCTIONS--------------------// function JS_IsEmpty (sFieldValue) { if ((sFieldValue == null) || (sFieldValue.length == 0)) { return true; } else { for (i=0; i= 0; i--) { if ((sChar.charAt(i) != ' ') || (AlphaFound == true)) { AlphaFound = true; ReturnChar += sChar.charAt(i); } } //Reverse the string sChar = ReturnChar; ReturnChar = ""; for (var i=sChar.length - 1; i >= 0; i--) { ReturnChar += sChar.charAt(i); } return ReturnChar; } function JS_Trim (sChar) { var ReturnChar=""; ReturnChar = JS_RTrim(JS_LTrim(sChar)); return ReturnChar; } function JS_Replace(pstr1, pstr2, pstr3) { if (pstr1 != "") { var rtnstr = ""; var searchstr = pstr1; var addlen = pstr2.length; var index = pstr1.indexOf(pstr2); while ((index != -1) && (searchstr != "")) { rtnstr = rtnstr + searchstr.substring(0, index); if (pstr3 != null) { rtnstr = rtnstr + pstr3; } searchstr = searchstr.substring(index + addlen, searchstr.length); if (searchstr != "") { index = searchstr.indexOf(pstr2); } else { index = -1; } } return (rtnstr + searchstr); } else { return ""; } } function JS_MakeUpper(sString) { if (!sString) { return ""; } else { return sString.toUpperCase(); } } //---------------------------GENERAL FUNCTIONS END--------------------//