utilities.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: ps
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2011
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. // Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  9. // Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  10. //-- Browser detection
  11. var isNS = false, isIE = false;
  12. navigator.appName == "Netscape" ? isNS = true : isIE = true;
  13. //-- List supported URL protocols here.
  14. var g_sValidURLProtocols = "http:// https:// file:// ftp:// crn/ ../";
  15. //-- Removes leading and trailing spaces
  16. function trim(s) {
  17. var j = s.length;
  18. var strCd;
  19. for (var i = 0; i < j; i++) {
  20. strCd = s.charCodeAt(i);
  21. if (32 != strCd && 12288 != strCd) { //-- check for unicode space - normal and wide.
  22. break;
  23. }
  24. }
  25. if (i < j - 1) {
  26. for (; j > i; j--) {
  27. strCd = s.charCodeAt(j - 1);
  28. if (32 != strCd && 12288 != strCd) {
  29. break;
  30. }
  31. }
  32. }
  33. return s.substring(i, j);
  34. }
  35. /**
  36. * Verifies the port range: return true if it is between 0 and 65535, false otherwise.
  37. */
  38. function isPortRangeValid(field) {
  39. if (field != null) {
  40. var value = field.value;
  41. if (!isNaN(value) && (parseInt(value, 10) == value) && value >= 0 && value <= 65535) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. // Checks that the specified form field contains an integer value that
  48. // is greater than or equal to zero.
  49. function checkPositiveInteger(field) {
  50. if (field) {
  51. if (field.value < 1 || isNaN(field.value) || parseInt(field.value, 10) != field.value) {
  52. field.value = '0';
  53. }
  54. }
  55. }
  56. // Examines the value of the given object, if it's not a numerical
  57. // value greater than or equal to 0, it is replaced with the
  58. // supplied default value.
  59. function checkPositiveIntegerDefault(field, def) {
  60. if (field) {
  61. if (field.value < 1 || isNaN(field.value) || parseInt(field.value, 10) != field.value)
  62. {
  63. field.value = def;
  64. }
  65. }
  66. }
  67. function checkURLFormat(field) {
  68. var item = field.value.toLowerCase();
  69. var aValidProtocols = [];
  70. var bIsValidProtocol = false;
  71. aValidProtocols = g_sValidURLProtocols.toLowerCase().split(" ");
  72. for (var i = 0; i < aValidProtocols.length; i++) {
  73. // Reject URL's that do not start with or are equal to the list of URL prefixes defined in g_sValidURLProtocols.
  74. // In addition to the supported protocols, we disallow the following characters from immediatelly following the protocol: ? @ : / \ #
  75. if (item.indexOf(aValidProtocols[i]) == 0 && aValidProtocols[i] != item && item.indexOf(aValidProtocols[i] + "?") != 0 && item.indexOf(aValidProtocols[i] + "\\") != 0) {
  76. var s = "^" + aValidProtocols[i] + "(@|:|#|/| )";
  77. var re = new RegExp(s);
  78. if (!re.test(item)) {
  79. bIsValidProtocol = true;
  80. break;
  81. }
  82. }
  83. }
  84. if (bIsValidProtocol) {
  85. if (item == "http://www.") {
  86. bIsValidProtocol = false;
  87. }
  88. }
  89. return bIsValidProtocol;
  90. }
  91. // Format is name value pairs.
  92. // To override the use of the default document name (pform is assumed),
  93. // the word "form" must be specified as the first parameter followed by the form name as the value for the second parameter
  94. // in the name/value pair list. Example setSelectParams("form","myformname",...);
  95. function setSelectParams() {
  96. var docform;
  97. var args = arguments.length;
  98. if (args > 0) {
  99. var i = 0;
  100. if (arguments[i] == 'form') {
  101. docform = document[arguments[++i]];
  102. i++;
  103. } else {
  104. docform = document.pform;
  105. }
  106. for (; i < args ; i++) {
  107. nm = arguments[i];
  108. vl = arguments[++i];
  109. docform[nm].value = vl;
  110. }
  111. }
  112. }
  113. // Sets the value of the form input field - otherwise creates the entry on the form.
  114. // When the type (typ) is not specified, hidden is assumed.
  115. // When the formname is not specified, pform is assumed.
  116. function setFormInputElement(elename, val, typ, form) {
  117. var docform;
  118. if (typeof form != "undefined" && form != null) {
  119. docform = document[form];
  120. } else {
  121. docform = document.pform;
  122. }
  123. if (docform[elename]) {
  124. docform[elename].value = val;
  125. } else {
  126. var eleTyp;
  127. if (typeof typ != "undefined" && typ != null) {
  128. eleTyp = typ;
  129. } else {
  130. eleTyp = "hidden";
  131. }
  132. var formElement = document.createElement("input");
  133. formElement.setAttribute("type", eleTyp);
  134. formElement.setAttribute("name", elename);
  135. formElement.setAttribute("value", val);
  136. docform.appendChild(formElement);
  137. }
  138. }
  139. // Checks if elements starting with a particular prefix are checked.
  140. // Typically used to determine if any checkboxes in a column of checkboxes are selected
  141. function isSelected(p) {
  142. for (var i = 0; i < document.pform.elements.length; i++) {
  143. var e = document.pform.elements[i];
  144. if (e.name.substring(0, p.length) == p) {
  145. if (e.checked == true) {
  146. return true;
  147. }
  148. }
  149. }
  150. return false;
  151. }
  152. // Checks if element is checked and returns the value for it.
  153. function getSelectedValueForFormElement(p, form) {
  154. for (var i=0;i<form.elements.length;i++) {
  155. var e = form.elements[i];
  156. if (e.name == p && e.checked) {
  157. return e.value;
  158. }
  159. }
  160. return "";
  161. }
  162. function getSelectedValue(p) {
  163. for (var i=0;i<document.pform.elements.length;i++) {
  164. var e = document.pform.elements[i];
  165. if (e.name == p && e.checked) {
  166. return e.value;
  167. }
  168. }
  169. return "";
  170. }
  171. function checkB4Download(downloadName)
  172. {
  173. var changeName=false;
  174. for(i=0;i < downloadName.length; i++)
  175. {
  176. switch(downloadName.charCodeAt(i))
  177. {
  178. case 0x22: // Quote
  179. case 0x2A: // Asterisk
  180. case 0x2F: // Forward slash
  181. case 0x3A: // Colon
  182. case 0x3C: // Left Arrow
  183. case 0x3E: // Right Arrow
  184. case 0x3F: // Question mark
  185. case 0x5C: // Back slash
  186. case 0x7C: // Vertical bar
  187. {
  188. changeName=true;
  189. }
  190. default:
  191. {
  192. continue;
  193. }
  194. }
  195. }
  196. return changeName;
  197. }
  198. function checkDocumentDownload(downloadName, downloadMsg, sURL) {
  199. if (checkB4Download(downloadName)) {
  200. alert(downloadMsg);
  201. }
  202. window.setTimeout("window.location.href='" + sURL + "';", 1);
  203. }
  204. function scrollingTableResizeHandler(evt, isIE, divId, divBody, divHeader, nBottomSpace, minSize) {
  205. var eBody = document.body;
  206. var div_results_content = document.getElementById(divId);
  207. var div_results_contentBody = document.getElementById(divBody);
  208. var div_results_contentHeader = document.getElementById(divHeader);
  209. if (!div_results_content && !div_results_contentBody) {
  210. return;
  211. }
  212. var nNewHeight = eBody.clientHeight - scrollingTableGetTop(div_results_content) - nBottomSpace;
  213. // adjust height of the results content div (right pane)
  214. if (isIE) {
  215. if (nNewHeight > minSize) {
  216. div_results_content.style.height = nNewHeight;
  217. } else {
  218. div_results_content.style.height = minSize;
  219. }
  220. } else {
  221. if (nNewHeight > minSize) {
  222. div_results_contentBody.style.height = nNewHeight - div_results_contentHeader.offsetHeight;
  223. } else {
  224. div_results_contentBody.style.height = minSize;
  225. }
  226. }
  227. }
  228. function scrollingTableGetTop(obj) {
  229. var curtop = 0;
  230. try {
  231. while (obj.offsetParent) {
  232. curtop += obj.offsetTop;
  233. obj = obj.offsetParent;
  234. }
  235. } catch (e) {}
  236. return curtop;
  237. }
  238. function scrollingTableAddEvent(obj, eventName, handler) {
  239. if (obj.attachEvent) {
  240. obj.attachEvent("on" + eventName, handler);
  241. } else if (obj.addEventListener) {
  242. obj.addEventListener(eventName, handler, false);
  243. }
  244. }
  245. // Checks if element is checked and returns the value for it.
  246. function getSelectedValueForFormElement(p, form) {
  247. for (var i=0;i<form.elements.length;i++) {
  248. var e = form.elements[i];
  249. if (e.name == p && e.checked) {
  250. return e.value;
  251. }
  252. }
  253. return "";
  254. }
  255. function getSelectedValue(p) {
  256. for (var i=0;i<document.pform.elements.length;i++) {
  257. var e = document.pform.elements[i];
  258. if (e.name == p && e.checked) {
  259. return e.value;
  260. }
  261. }
  262. return "";
  263. }
  264. function xmlDecodeResponse(sResponse)
  265. {
  266. // Decode stuff which has been encoded/escaped out.
  267. // ORDER IS IMPORTANT - amp_re must be the last value to replace!
  268. return (((sResponse.replace(/&lt;/g, "<")).replace(/&gt;/g, ">")).replace(/&apos;/g, "'")).replace(/&amp;/g, "&");
  269. }