// CommonJSLib.js (v1.1): Reusable JavaScript Library (stored as a Domino Page design element) // - contains functions commonly used by any application // ** Do not change the code in this page ** as it is used by the Building Block forms and views. Create a copy of this page and give it a new name if you need a customized version of this JS library for your application. // Last updated: August 2 2003 CJD // Functions in this library (TIP: use [Edit | Find] to locate a function in this page) // * Common functions // - displayPopWin (new v1.1) // - displayDoc (revised v1.1) // - displayDialog (revised v1.1) // - displayAddressDialog (revised v1.1) // - displayKWFldDialog // - ReplaceSubstring // - Trim // - isMember (new v1.1) // - isEnterKey // - doClickViewRefreshFields (new v1.1) // * Field object related functions // - validateFieldRqrd (revised v1.1) // - getFieldValue (revised v1.1) // - deselectAll (revised v1.1) // - setFieldFocus (revised v1.1) // - forceUncheckedItems (new v1.1) // ================================================================ // Declare global variables (available to any of the functions) // ================================================================ // Start of text to be translated (actual text is stored in Computed Text fields) var g_strMsgValueRqrd = "A value is required for this field."; var g_strMsgInvalidObject = "An invalid object was passed to this function."; // (new v1.1) // End of text to be translated // ================================================================ // Common functions // ================================================================ function displayPopWin(strURL, nHeight, nWidth) // // Function: displayPopWin // // Last Updated: May 15 2003 CJD (new in v1.1) // // Purpose: // Open a URL in a popup window and give it focus // // Parameters: // strURL >> URL to be opened in the popup window // nHeight >> height of the popup window (optional) // nWidth >> width of the popup window (optional) // // Return value: // none // // Syntax: // Usage Example: // strURL = "path/db.nsf/(bbhlpLkupByHelpTopic)/DocKey?OpenDocument"; // displayPopWin(strURL) // { strURL = ReplaceSubstring(ReplaceSubstring(strURL, "\\", "/"), " ", "%20"); if (nWidth); else var nWidth = "500"; if (nHeight); else var nHeight = "340"; var strWindowName = "wPopWin"; var nPosTop = 10; var nPosLeft = 10; var strWindowAttribs = "scrollbars=1,resizable=1" + ",width=" + nWidth + ",height=" + nHeight + ",top=" + nPosTop + ",left=" + nPosLeft + ",screenY=" + nPosTop + ",screenX=" + nPosLeft; tmpPopWin = window.open(strURL, strWindowName, strWindowAttribs); tmpPopWin.focus(); } // End of Function: displayPopWin function displayDoc(strDB, strView, strDocKey, strRefererURL, nHeight, nWidth) // // Function: displayDoc // // Last Updated: May 15 2003 CJD (revised in v1.1) // // Purpose: // Open a document in a new browser window // // Parameters: // strDB >> name of the database the contains the document // strView >> name of the view that is sorted by the document key // strDocKey >> key of the document to be opened // strRefererURL >> URL of original source window/page that opened the document (optional) // nHeight >> height of the new browser window (optional) // nWidth >> width of the new browser window (optional) // // Return value: // none // // Syntax: // Usage Example: // strDB = "path/db.nsf"; // strView = "(bbhlpLkupByHelpTopic)"; // strDocKey = "Using the Main Topic Form"; // displayDoc(strDB, strView, strDocKey, strRefererURL) // { strDB = ReplaceSubstring(ReplaceSubstring(strDB, "\\", "/"), " ", "%20"); strView = ReplaceSubstring(ReplaceSubstring(strView, "\\", "/"), " ", "%20"); // Note: Document title must have special characters encoded so the URL will load correctly strDocKey = ReplaceSubstring(ReplaceSubstring(strDocKey, "\\", "%5C"), " ", "%20"); strDocKey = ReplaceSubstring(ReplaceSubstring(strDocKey, "/", "%2F"), "#", "%23"); strDocKey = ReplaceSubstring(ReplaceSubstring(strDocKey, "?", "%3F"), "+", "%2B"); strDocKey = ReplaceSubstring(strDocKey, "!", "%21"); var strURL = window.location.protocol + "//" + window.location.host + "/" + strDB + "/" + strView + "/" + strDocKey + "?OpenDocument"; // Pass original source window URL as a parameter (&RefererURL=) to the target document // window so the target window can re-open the original source window (if applicable). // NOTE: This parameter must be the last parameter in the URL that is being built by this function if (strRefererURL) { strURL += "&RefererURL=" + strRefererURL; } if (nWidth); else var nWidth = "500"; if (nHeight); else var nHeight = "340"; // Build internal name for window (no special chars allowed) var strWindowName = "wDspDoc" + ReplaceSubstring( ReplaceSubstring( ReplaceSubstring( ReplaceSubstring( ReplaceSubstring( strView, "(", "" ), "$", "" ), ")", "" ), "/", "" ), "%20", "" ); var nPosTop = 10; var nPosLeft = 10; var strWindowAttribs = "scrollbars=1,resizable=1" + ",width=" + nWidth + ",height=" + nHeight + ",top=" + nPosTop + ",left=" + nPosLeft + ",screenY=" + nPosTop + ",screenX=" + nPosLeft; window.open(strURL, strWindowName, strWindowAttribs); } // End of Function: displayDoc function displayDialog(strDB, strDlgForm, strSubForm, strActionType, strArgs, strTitle, nWidth, nHeight) // // Function: displayDialog // // Last Updated: May 15 2003 CJD (revised in v1.1) // // Purpose: // Builds the window.open JavaScript command to open a new browser window using // the specified Domino form in the specified Domino database and passes the other // display variables as the argument string after the ?OpenForm Domino URL action. // // For example: // strURL = http://www.server.com/strDB/strDlgForm?OpenForm // + &SubForm= // + &ActionType= // + strArgs // + &Title= // + &RefererURL=strURL // strWindowName = "wDlgBox" // strWindowAttribs = "scrollbars=1,resizable=1,width=nWidth,height=nHeight" // window.open(strURL, strWindowName, strWindowAttribs) // // Notes: // -- The arguments passed after the "?OpenForm" will have the blanks converted to "%20"; // therefore, the "%20" must be converted back to blanks by the form that is being // opened before they are used on the form (that is the text in the QUERY_STRING // variable must be converted before the arguments are parsed out of the string.) // // Parameters: // strDB >> name of the database the contains the form/subform to be opened // strDlgForm >> name of the form to be opened // strSubForm >> name of the subform to be loaded in the form (optional) // strActionType >> action type value (optional) // strArgs >> arguments to be passed to the form/subform (optional) // strTitle >> title to be displayed in the form/subform (optional) // nWidth >> width of the new browser window // nHeight >> height of the new browser window // // Return value: // none // // Syntax: // Usage Example: // strDB = "path/db.nsf" // strArgs = "&Prompt=Select a help topic.&Key=knHelp-fmgtDocType" // displayDialog(strDB, "(bbstkwDlgBox)", "(bbhlpwHelpDlgBox)", "", strArgs, "Select Help Topic", "385", "210") // { strDB = ReplaceSubstring(ReplaceSubstring(strDB, "\\", "/"), " ", "%20"); strDlgForm = ReplaceSubstring(ReplaceSubstring(strDlgForm, "\\", "/"), " ", "%20"); var strURL = window.location.protocol + "//" + window.location.host + "/" + strDB + "/" + strDlgForm + "?OpenForm"; if (strSubForm) { strURL += "&Subform=" + ReplaceSubstring(strSubForm, " ", "%20"); } if (strActionType) { strURL += "&ActionType=" + ReplaceSubstring(strActionType, " ", "%20"); } if (strArgs) { strURL += ReplaceSubstring(strArgs, " ", "%20"); } if (strTitle) { strURL += "&Title=" + ReplaceSubstring(strTitle, " ", "%20"); } // Pass original dialog window URL as a parameter (&RefererURL=) to the dialog window so // the target window of the dialog window can re-open the dialog window (if applicable). // NOTE: This parameter must be the last parameter in the URL that is being built by this function strURL += "&RefererURL=" + strURL; // Build internal name for window (no special chars allowed) var strWindowName = "wDlgBox" + ReplaceSubstring( ReplaceSubstring( ReplaceSubstring( ReplaceSubstring( ReplaceSubstring( strDlgForm, "(", "" ), "$", "" ), ")", "" ), "/", "" ), "%20", "" ); if (strSubForm) { strWindowName += ReplaceSubstring( ReplaceSubstring( ReplaceSubstring( ReplaceSubstring( strSubForm, "(", "" ), "$", "" ), ")", "" ), " ", "" ); } var nPosTop = 10; var nPosLeft = 10; var strWindowAttribs = "scrollbars=1,resizable=1" + ",width=" + nWidth + ",height=" + nHeight + ",top=" + nPosTop + ",left=" + nPosLeft + ",screenY=" + nPosTop + ",screenX=" + nPosLeft; window.open(strURL, strWindowName, strWindowAttribs); } // End of Function: displayDialog function displayAddressDialog(strDB, strField, strMultiValueOK, strRefreshFunc, strFieldType, strFieldWeb, nWidth, nHeight) // // Function: displayAddressDialog // // Last Updated: January 28 2003 CJD (revised in v1.1) // // Purpose: // Display Web Address dialog window to select names from an address book. // This feature simulates the Notes interface for name-type fields that have // the 'Use address dialog for choices' option enabled for the Notes field. // // Parameters: // strDB >> name of the database that contains Address dialog box form // strField >> name of the input field on the calling form that contains the names // strMultiValueOK >> indicate if multiple name selections are allowed (1=Yes, 0=No) // strRefreshFunc >> name of the function to call to refresh fields when the names field is updated on calling form (optional) // strFieldType >> indicate if type of field; HTMLSelectFld or HTMLInputFld (optional) // strFieldWeb >> name of the Web Select field on the calling form (only required if strFieldType = HTMLSelectFld) (optional) // nWidth >> width of dialog window (optional) // nHeight >> height of dialog window (optional) // // Return value: // none // // Syntax: // Usage Example: // strDB = "path/db.nsf"; // strField = "UserName"; // strMultiValueOk= "1"; // strRefreshFunc = "RefreshFields"; // strFieldType = "HTMLSelectFld"; // strFieldWeb = "UserNameWeb"; // displayAddressDialog(strDB, strField, strMultiValueOk, strRefreshFunc, strFieldType, strFieldWeb); // { var strAddrFieldNameArg = "&AddrFieldName=" + strField; var strAddrMultiValueOKArg = "&AddrMultiValueOK=" + strMultiValueOK; var strAddrRefreshFuncArg = ""; if (strRefreshFunc) { strAddrRefreshFuncArg = "&AddrRefreshFunc=" + strRefreshFunc; } var strAddrFieldTypeArg = ""; var strAddrFieldNameWebArg = ""; if (strFieldType) { strAddrFieldTypeArg = "&AddrFieldType=" + strFieldType; strAddrFieldNameWebArg = "&AddrFieldNameWeb=" + strFieldWeb; } // Specify other parameters for the Address dialog box var strDlgForm = "(bbstkwAddressDlgBox)"; var strSubForm = ""; var strActionType = ""; var strArgs = strAddrFieldNameArg + strAddrMultiValueOKArg + strAddrRefreshFuncArg + strAddrFieldTypeArg + strAddrFieldNameWebArg; var strTitle = ""; if (nWidth); else var nWidth = "760"; if (nHeight); else var nHeight = "540"; // Display the Address dialog box in a separate Web browser window displayDialog(strDB, strDlgForm, strSubForm, strActionType, strArgs, strTitle, nWidth, nHeight); } // End of Function: displayAddressDialog function displayKWFldDialog(strDB, strField, strView, nWidth, nHeight) // // Function: displayKWFldDialog // // Last Updated: April 27 2001 CJD // // Purpose: // Display Web dialog window containing keyword selection interface. // This feature simulates the Notes interface for keyword fields that have // have the following settings for the Notes field: // Display list, Allow multiple values, Allow values not in list // // Parameters: // strDB >> name of the database the contains the keyword choices dialog box form/subform // strField >> name of the input field on the calling form that contains the keyword selection choices // strView >> name of the view that generates the list of selection choices for the keyword field // nWidth >> width of dialog window (optional) // nHeight >> height of dialog window (optional) // // Return value: // none // // Syntax: // Usage Example: // strDB = "path/db.nsf"; // strField = "Categories"; // strView = "(bbhlpLkupHelpCategories)"; // displayKWFldDialog(strDB, strField, strView); // { // Start of text to be translated (actual text is stored in Computed Text fields) var strMsgA = "Select Keywords"; // End of text to be translated var strKWFieldNameArg = "&KWFieldName=" + strField; var strKWLkupViewArg = "&KWLkupView=" + strView; // Specify other parameters for the Help dialog box var strDlgForm = "(bbstkwDlgBox)"; var strSubForm = "(bbstkwKWFldDlgBox)"; var strActionType = ""; var strArgs = strKWFieldNameArg + strKWLkupViewArg; var strTitle = strMsgA; if (nWidth); else var nWidth = "450"; if (nHeight); else var nHeight = "320"; // Display the keyword dialog box in a separate Web browser window displayDialog(strDB, strDlgForm, strSubForm, strActionType, strArgs, strTitle, nWidth, nHeight); } // End of Function: displayKWFldDialog function ReplaceSubstring(strStringIn, strFromChar, strToChar) // // Function: ReplaceSubstring // // Last Updated: January 22 2001 CJD // // Purpose: // Replaces specific characters in a text string with characters that you specify. // // Takes the input string and replaces all occurrences of one substring with another substring // and returns the output string as its result. // // For example, it can be used to convert all blanks to %20. This is useful // when working with Domino URLs. // // Notes: // -- Case sensitive // -- Does not work with arrays or lists // -- Using an empty (null) value for strToChar gives the result of // deleting the strFromChar substring from the input text string // // Parameters: // strStringIn >> input text string // strFromChar >> substring to be converted from; can be one or more characters // strToChar >> substring to be converted to; can be empty (null), or one or more characters // // Return value: // resulting output text string. // // Syntax: // Usage Example: // NewString = ReplaceSubstring(OldString, " ", "%20") // { var nFromCharLen = strFromChar.length; for (var x=0; x < strStringIn.length; x++) { if (strStringIn.substring(x, x + nFromCharLen) == strFromChar) { strStringIn = strStringIn.substring(0, x) + strToChar + strStringIn.substring(x + nFromCharLen, strStringIn.length); } } return strStringIn; } // End of Function: ReplaceSubstring function Trim(strStringIn) // // Function: Trim // // Last Updated: January 22 2001 CJD // // Purpose: // Removes leading and training blanks (spaces) as well as all // extraneous spaces in the string, reducing them to single spaces // within the string. // // Notes: // -- Does not work with arrays or lists // // Parameters: // strStringIn >> input text string // // Return value: // resulting output text string. // // Syntax: // Usage Example: // NewString = Trim(OldString) // { var strStringOut = ""; var strThisChar = ""; var strPrevChar = " "; for (var x = 0; x < strStringIn.length; x++) { strThisChar = strStringIn.charAt(x); if ((strThisChar != " ") || (strPrevChar != " ")) { strStringOut += strThisChar; } strPrevChar = strThisChar; } if (strStringOut.charAt(strStringOut.length - 1) == " ") { strStringOut = strStringOut.substring(0, strStringOut.length - 1); } return strStringOut; } // End of Function: Trim function isMember(arStrings, strValue) // // Function: isMember // // Last Updated: April 9 2003 CJD (new in v1.1) // // Purpose: // Tests to see if a string is a member of an array of strings // // Parameters: // strStrings >> array of strings // strValue >> string to be searched for in the array // // Return value: // True if string is found in the array, otherwise False // { var index; for ( index = 0; index < arStrings.length; index++) { if (arStrings[index] == strValue) { return true; } } return false; } // End of Function: isMember function isEnterKey(objEvent) // // Function: isEnterKey // // Last Updated: June 5 2001 CJD // // Purpose: // Returns true if the Enter key was pressed. // // Notes: // Call this function from the onKeyPress event for input fields on a form that you want to disable the Enter key for. // // Parameters: // objEvent >> event object from calling window // // Return value: // True if the Enter key was pressed, false if not. // // Syntax: // Usage Example: // if (isEnterKey( event )) {return false} // { var isNav = (document.layers) ? 1:0; var isIE = (document.all) ? 1:0; var nKeycode; if ( isNav ) { nKeycode = objEvent.which; } else { nKeycode = objEvent.keyCode; } if ( 13 == nKeycode ) { return true; } else { return false; } } // End of function: isEnterKey function doClickViewRefreshFields() // // Function: doClickViewRefreshFields // // Last Updated: February 3 2003 CJD (new in v1.1) // // Purpose: // Runs the @Command([ViewRefreshFields]) Notes/Domino command to force a document to refreshed // // Notes: // Call this function when you want the fields on a document to be refreshed/recomputed. // // Parameters: // none // // Return value: // none // { _doClick("$Refresh", this, null); } // End of function: doClickViewRefreshFields // ================================================================ // Field object related functions // ================================================================ function validateFieldRqrd(objField, strType, strTitle, strSetFocus) // // Function: validateFieldRqrd // // Last Updated: April 9 2003 CJD (revised in v1.1) // // Purpose: // Validate a required input field to ensure a value is entered or selected. // If no value is entered or selected, an error message is displayed and // the field is given focus. // // Parameters: // objField >> field object to be validated // strType >> HTML input data type, such as text, textarea, select, radio, checkbox // strTitle >> field label // strSetFocus >> indicate if focus is to be set for the field; default = yes (optional) // // Return value: // False if error found, else True // // Syntax: // validateFieldRqrd(thisform.Categories, "select", "Help Type") // { var strThisFuncName = "validateFieldRqrd"; // Display error message if an invalid field object was passed and get out now if (undefined == objField || null == objField) { alert(strThisFuncName + ": " + g_strMsgInvalidObject); return false; } var strType = strType.toLowerCase(); if (strSetFocus) { strSetFocus = strSetFocus.toLowerCase(); if ("no" == strSetFocus); else strSetFocus = "yes"; } else // default value = yes if parameter not passed to function { strSetFocus = "yes"; } var strValue = getFieldValue(objField, strType); strValue = Trim(strValue); if ("" == strValue) { alert(strTitle + ": " + g_strMsgValueRqrd); if ("yes" == strSetFocus) { setFieldFocus(objField, strType); } return false; } return true; } // End of Function: validateFieldRqrd function getFieldValue(objField, strType) // // Function: getFieldValue // // Last Updated: April 9 2003 CJD (revised in v1.1) // // Purpose: // Get the value or value list for a field, based on the field type. // // Parameters: // objField >> field object // strType >> HTML input data type, such as text, textarea, select, radio, checkbox // // Return value: // value or value list for the field // // Syntax: // getFieldValue(thisform.Categories, "select") // { var strThisFuncName = "getFieldValue"; var index; var strValue = ""; var nHits = 0; var strSep = ""; var strSepChar = "; "; var strType = strType.toLowerCase(); // Display error message if an invalid field object was passed and get out now if (undefined == objField || null == objField) { alert(strThisFuncName + ": " + g_strMsgInvalidObject); return ( "" ); } // for text (text, date, number), the user-entered value is a string if ("text" == strType) { return ( objField.value ); } // for textarea, the user-entered value is a string if ("textarea" == strType) { return ( objField.value ); } // for select (keyword text list), the selection list is an array of selection pointers // to an array of strings representing the keyword choices, and user-selected keywords // in the array have the SELECTED property if ("select" == strType) { for (index = 0; index < objField.options.length; index++) { if (objField.options[index].selected) { nHits++; if (nHits > 1) { strSep = strSepChar; } if (objField.options[index].value) { strValue += strSep + objField.options[index].value } else { strValue += strSep + objField.options[index].text } } } return ( strValue ); } // for radio (keyword radio button list): // -- if a single item selection list, the user-entered value is the keyword value as a string // -- if a multiple item selection list, the selection list is an array of keyword choices, and // the user-selected keywords have the CHECKED property if ("radio" == strType) { // Test for a multiple item selection list if (objField[0]) { // Loop through multiple item selection list for (index = 0; index < objField.length; index++) { if (objField[index].checked) { strValue = objField[index].value; // get out of loop now as only one radio button can be selected break; } } return ( strValue ); } // Single item selection list if (objField.checked ) { return ( objField.value ); } else { return ( "" ); } } // for checkbox (keyword checkbox list), // -- if a single item selection list, the user-entered value is the keyword value as a string // -- if a multiple item selection list, the selection list is an array of keyword choices, and // the user-selected keywords have the CHECKED property if ("checkbox" == strType) { // Test for a multiple item selection list if (objField[0]) { // Loop through multiple item selection list for (index = 0; index < objField.length; index++) { if (objField[index].checked) { nHits++; if (nHits > 1) { strSep = strSepChar; } strValue += strSep + objField[index].value } } return ( strValue ); } // Single item selection list if (objField.checked) { return ( objField.value ); } else { return ( "" ); } } // Logic would fall through to here if an invalid type was passed to this function; return a null value. return ( "" ); } // End of Function: getFieldValue function deselectAll(objField, strType) // // Function: deselectAll // // Last Updated: April 9 2003 CJD (revised in v1.1) // // Purpose: // Deselects (or unchecks) all the options for a select, radio, or checkbox input field // // Parameters: // objField >> field object // strType >> HTML input data type, such as select, radio, checkbox // // Return value: // none // // Syntax: // Usage Example: // deselectAll(thisform.Categories, "select") // { var strThisFuncName = "deselectAll"; // Display error message if an invalid field object was passed and get out now if (undefined == objField || null == objField) { alert(strThisFuncName + ": " + g_strMsgInvalidObject); return; } var index; var strType = strType.toLowerCase(); if ("select" == strType) { for (index = 0 ; index < objField.options.length ; ++index) { if (objField.options[index].selected) { objField.options[index].selected = false; } } return; } if ("radio" == strType || "checkbox" == strType) { // Test for a multiple item selection list if (objField[0]) { // Loop through multiple item selection list for (index = 0; index < objField.length; ++index) { if (objField[index].checked) { objField[index].checked = false; } } } else { // Single item selection list if (objField.checked ) { objField.checked = false; } } return; } } // End of function: deselectAll function setFieldFocus(objField, strType) // // Function: setFieldFocus // // Last Updated: April 9 2003 CJD (revised in v1.1) // // Purpose: // Put the focus on a field, based on the field type // // Parameters: // objField >> field object // strType >> HTML input data type, such as text, textarea, select, radio, checkbox // // Return value: // none // // Syntax: // Usage Example: // setFieldFocus(thisform.Categories, "select") // { var strThisFuncName = "setFieldFocus"; // Display error message if an invalid field object was passed and get out now if (undefined == objField || null == objField) { alert(strThisFuncName + ": " + g_strMsgInvalidObject); return; } var strType = strType.toLowerCase(); switch (strType) { case "text": objField.focus(); objField.select(); break; case "textarea": objField.focus(); break; case "select": objField.focus(); break; case ("radio"): if (objField[0]) // Multiple item selection list { objField[0].focus(); } else // Single item selection list { objField.focus(); } break; case "checkbox": if (objField[0]) // Multiple item selection list { objField[0].focus(); } else // Single item selection list { objField.focus(); } break; default: // Unknown field type, do nothing } } // End of function: setFieldFocus function forceUncheckedItems(objField) { // // Function: forceUncheckedItems // // Last Updated: August 2 2003 CJD (new in v1.1) // // Purpose: // This function forces a null value back to the Domino server // or each unchecked selection item in a Checkbox field. // // Notes: // This function can only be called at the point where the // document will be submitted to the server; that is that all // validations have passed and the last action is to submit it. // It may cause data entry problems if the user is returned back // to the form after this function has been run. // // Parameters: // objField >> field object // // Return value: // none // // Syntax: // Usage Example: // forceUncheckedItems(thisform.CheckBoxA) // var index; // Test for a multiple item selection list if (objField[0]) { // Loop through multiple item selection list for (index = 0; index < objField.length; ++index) { if (objField[index].checked == false) { objField[index].checked = true; objField[index].value = ""; } } } else { // Single item selection list if (objField.checked == false) { objField.checked = true; objField.value = ""; } } } // End of function: forceUncheckedItems // ================================================================ // End of JavaScript Library: CommonJSLib.js // ================================================================