/** * Copyright (c) 2004-2016 Omnitracs, LLC. All rights reserved. * Confidential and Proprietary - Omnitracs, LLC. * This software may be subject to U.S. and international export, re-export, or transfer * ("export") laws. * Diversion contrary to U.S. and international laws is strictly prohibited. */ // common.js // This file holds functions that can be used repeatedly throughout different // applications. This is a better place compared to validation.js // define array.indexOf for non firefox browsers if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt /*, from*/) { var len = this.length; var from = Number(arguments[1]) || 0; from = (from < 0) ? Math.ceil(from) : Math.floor(from); if (from < 0) from += len; for (; from < len; from++) { if (from in this && this[from] === elt) return from; } return -1; }; } // Encodes a string to html format. Replaces the characters ' ', '&', '<', // and '>' with their html counterparts function c_htmlEncode(input) { output = ""; for (input_index = 0; input_index < input.length; input_index++) { if (input.charAt(input_index) == ' ') { output += " "; } else if (input.charAt(input_index) == '&') { output += "&"; } else if (input.charAt(input_index) == '<') { output += "<"; } else if (input.charAt(input_index) == '>') { output += ">"; } else { output += input.charAt(input_index); } } return output; } // Decodes a html encoded string. Inserts the characters ', <, >, &, and ". // Equlvalent of undoing filter method in Struts ReponseUtils.java function c_htmlDecode(input) { output = input.replace(/&/g,"&"); output = output.replace(/"/g,"\""); output = output.replace(/ /g, " "); output = output.replace(/'/g,"'"); output = output.replace(/'/g,"'"); output = output.replace(/</g,"<"); output = output.replace(/>/g,">"); return output; } // Trims a string of leading and trailing spaces. // Input: String // Output: String function c_trim(str) { return str.replace(/^\s*|\s*$/g,""); } // Trims a string of leading spaces. // Input: String // Output: String function c_trimLeft(str) { return str.replace(/^\s*/g,""); } // Trims a string of trailing spaces. // Input: String // Output: String function c_trimRight(str) { return str.replace(/\s*$/g,""); } // Given an input field, get the selected/highlighted text from it. function getSelectedText(textbox) { var textSelected; var formattedText; if (document.selection != undefined) {//IE textbox.focus(); var sel = document.selection.createRange(); textSelected = sel.text; } else if (textbox.selectionStart != undefined) {//Mozilla (firefox) var startSelPos = textbox.selectionStart; var endSelPos = textbox.selectionEnd; textSelected = textbox.value.substring(startSelPos, endSelPos); } return textSelected; } // Returns the current cursor position in an input field. function getCursorPos (el) { var cursorPos = 0; // For IE if (document.selection) { el.focus(); var sel = document.selection.createRange(); sel.moveStart('character', -el.value.length); cursorPos = sel.text.length; // For firefox } else if (el.selectionStart || el.selectionStart == '0') { cursorPos = el.selectionStart; } return (cursorPos); } // Sets the current cursor position in an input field function setCursorPos(el, pos) { if(el.setSelectionRange) { el.setSelectionRange(pos,pos); } else if (el.createTextRange) { var range = el.createTextRange(); range.collapse(true); range.moveEnd('character', pos); range.moveStart('character', pos); range.select(); } } // Returns the version of Internet Explorer or a -1 // (indicating the use of another browser). function getInternetExplorerVersion() { var rv = -1; // Return value assumes failure. if (navigator.appName == 'Microsoft Internet Explorer') { var ua = navigator.userAgent; var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 ); } return rv; } // Change an input field to upper case (works in IE 7,8,9 and FF). // Input: 1.) event object 2.) max length (may be omitted) // Use like this: c_upperCase(e, 10); function c_capitalizeInput(e, maxFieldLen) { // use the value of event if available // if not assume it's IE and use window.event e=e||window.event; // Return target element in Firefox or IE var el=e.target||e.srcElement; // Get keycode in Firefox or IE var key=e.which||e.keyCode; // If this is a special char (backspace, delete, alt+tab, or tab key, etc) skip out of here. if ((key >= 8 && key <= 46) || (e.ctrlKey && (key === 86 || key === 118 || key === 99 || key === 120))) { return true; } // Prevent default so the letter is not added automatically. We want to capitalize it first. var $E = YAHOO.util.Event; $E.preventDefault(e); // Get cursor position before changing. var cursorPos = getCursorPos(el); // If there is any text selected, we need to delete it prior to replacing it with the input character. var selectedText = getSelectedText(el); if (selectedText != null && selectedText != "") { // Adjust the cursor position. IE sets it to the end of the selection, so we need to subtract // the length of the selected text. Firefox sets it to the beginning so nothing special needed. var ver = getInternetExplorerVersion(); if (ver > -1) { // IE cursorPos = cursorPos-selectedText.length; } // Now construct the text string with the selected text removed. var firstPart = el.value.substring(0,cursorPos); // get first part var secondPart = el.value.substring(cursorPos); // get 2nd part var finalText = firstPart + secondPart.replace(selectedText, ""); // remove the selected text el.value = finalText; } var target = el.value; // If a max length was specified, obey it. If not, then we only need to obey any max that may // have been defined on the html element. We need this extra check since we use preventDefault. if (((maxFieldLen !== undefined) && (target.length >= maxFieldLen)) || (maxFieldLen === undefined) && (el.maxLength <= el.value.length)) { return true; } // Add the newly typed character. Upper case if it's a lower case letter. if ((key >= 97) && (key <= 122)) { el.value = target.substring(0,cursorPos) + String.fromCharCode(key).toUpperCase() + target.substring(cursorPos); } else { el.value = target.substring(0,cursorPos) + String.fromCharCode(key) + target.substring(cursorPos); } // Put the cursor back where it should be. setCursorPos(el, cursorPos+1); return false; } // Intercepts keyboard input - allows only number input and return key (13) // Call with onkeypress="return c_filterInputIntOnly(event);" // Compatible with both IE and Netscape/Mozilla function c_filterInputIntOnly(evt) { evt = (evt) ? evt : ((window.event) ? window.event : ""); var ctrlV = 118; if (window.event) { // IE var key = evt.keyCode; if ((key < 48 || key > 57) && (key != 13)) { return false; } else { return true; } } else if (evt.which) { // Netscape and Mozilla - backspace has to be allowed explicitly key = evt.which; if(key==ctrlV) { return true; } if ((key < 48 || key > 57) && (key != 13) && (key != 8)) { return false; } else { return true; } } else { return true; // Can't do anything for other browsers } } // Intercepts keyboard input - allows only alphabetic input and return key (13) // Call with onkeypress="return c_filterInputAlphabeticOnly(event);" // Compatible with both IE and Netscape/Mozilla function c_filterInputAlphabeticOnly(evt) { evt = (evt) ? evt : ((window.event) ? window.event : ""); if (window.event) { // IE var key = evt.keyCode; if ((key > 64 && key < 91) || (key > 96 && key < 123) || (key == 13)) { return true; } else { return false; } } else if (evt.which) { // Netscape and Mozilla key = evt.which; if ((key > 64 && key < 91) || (key > 96 && key < 123) || (key == 13)) { return true; } else { return false; } } else { return true; // Can't do anything for other browsers } } // Intercepts keyboard input - allows only alphabetic input, the space char, and return key (13) // Call with onkeypress="return c_filterInputAlphabeticWithSpaceOnly(event);" // Compatible with both IE and Netscape/Mozilla function c_filterInputAlphabeticWithSpaceOnly(evt) { evt = (evt) ? evt : ((window.event) ? window.event : ""); if (window.event) { // IE var key = evt.keyCode; if ((key > 64 && key < 91) || (key > 96 && key < 123) || (key == 13) || (key == 32)) { return true; } else { return false; } } else if (evt.which) { // Netscape and Mozilla key = evt.which; if ((key > 64 && key < 91) || (key > 96 && key < 123) || (key == 13) || (key == 32)) { return true; } else { return false; } } else { return true; // Can't do anything for other browsers } } // Intercepts keyboard input - allows only alphabetic and number input and return key (13) // Call with onkeypress="return c_filterInputAlphaNumericOnly(event);" // Compatible with both IE and Netscape/Mozilla function c_filterInputAlphanumericOnly(evt) { evt = (evt) ? evt : ((window.event) ? window.event : ""); if (window.event) { // IE var key = evt.keyCode; if (((key > 64) && (key < 91)) || ((key > 96) && (key < 123)) || (key == 32) || (key == 13) || ((key > 47) && (key < 58))) { return true; } else { return false; } } else if (evt.which) { // Netscape and Mozilla key = evt.which; if ((key > 64 && key < 91) || (key > 96 && key < 123) || (key == 13) || (key == 32) || (key > 47 && key < 58)) { return true; } else { return false; } } else { return true; // Can't do anything for other browsers } } // Intercepts keyboard input - allows only number input return key (13) and decimal point (46 or 110) // Call with onkeypress="return c_filterInputFloatOnly(event);" // Compatible with both IE and Netscape/Mozilla // 02-06-2006 GW - renamed this function to be used as a helper function // the real version now will only accept a single "." for the value. function _filterInputFloatOnly(evt) { evt = (evt) ? evt : ((window.event) ? window.event : ""); if (window.event) { // IE var key = evt.keyCode; if ((key < 48 || key > 57) && (key != 8) && (key != 13) && (key != 46) && (key != 110)) { return false; } else { return true; } } else if (evt.which) { // Netscape and Mozilla key = evt.which; if ((key < 48 || key > 57) && (key != 8) && (key != 13) && (key != 46) && (key != 110)) { return false; } else { return true; } } else { return true; // Can't do anything for other browsers } } function c_filterInputFloatOnly(evt) { if (!_filterInputFloatOnly(evt)) return false; var key; if (window.event) key = evt.keyCode; if (evt.which) key = evt.which; var str = $F(evt.target || evt.srcElement); var decimal = (key == 46 || key == 110); var decimalExists = (str.indexOf('.',0) != -1); return !decimalExists || !decimal; } // This function encodes a URL string to pass values like // +,'," properly to the next page. function c_URLencode(sStr) { return escape(sStr).replace(/\+/g, '%2B').replace(/\@/g, '%40').replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27'); } // This function disables a button after it has been clicked. // Method takes in the ID of the button that is being disabled. // Sets the alt text to be "please wait" // Accesses the 'imagebusy' rule of the stylesheet function c_disableImageButton(buttonId, message){ button = document.getElementById(buttonId); button.disabled = true; button.alt = message; button.title = message; button.className="imageBusy"; } // Disables a html button. This method assumes that there is a corresponding "Busy" // style within the CSS. function c_disableHtmlButton(buttonId) { button = document.getElementById(buttonId); button.className = button.className + "Busy"; button.disabled = true; } // Enables an html button. // This assumes that the word "Busy" is always at the END of a css class. function c_enableHtmlButton(buttonId) { var btn = document.getElementById(buttonId); if (btn && btn.className != "") { btn.disabled = false; var newClass = btn.className; if (newClass.lastIndexOf("Busy") > 0) { btn.className = newClass.substring(0, newClass.lastIndexOf("Busy")); } } } // This function enables/disables an Input Text field. // Method takes in the ID of the text field that is // being enabled/disabled function c_disableInputText(textboxId,bool) { textbox = document.getElementById(textboxId); if (bool == 'true') { textbox.disabled = true; } else { textbox.disabled = false; } } // This function is used to cover an element. // cover just hides the element without directly affecting layouts function c_coverElement(id) { if (document.getElementById) { el = document.getElementById(id); if (el) el.style.visibility="hidden"; } } // This function uncovers an element function c_uncoverElement(id) { if (document.getElementById) { el = document.getElementById(id); if (el) el.style.visibility="visible"; } } // This function hides an element. // Hiding an element can visually change the page (like a collapse) function c_hideElement(id) { if (document.getElementById) { el = document.getElementById(id); if (el) el.style.display="none"; } } // This function shows an element (opposite to hide) // Showing an element can visually change a page by insert function c_showElement(id) { if (document.getElementById) { el = document.getElementById(id); // firefox needs the display to be set to blank to redraw the table if (el && el.tagName == "TR") { el.style.display = ""; return; } if (el && el.tagName.match(/SPAN/i)) el.style.display='inline' if (el) el.style.display='block'; } } // This function is the preferred way to add handlers to the onload event. // Instead of setting the body tag's onload event directly on the HTML, // use the c_addLoadEvent function to add as many handlers as needed. // The order to add them is the order that they are called. function c_addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); } } } // Detects client browser version // Returns "Mozilla" or "IE" function c_getBrowser() { var appName = navigator.appName.toLowerCase(); if (appName.indexOf("netscape") != -1) { return "Mozilla"; } else { return "IE"; } } // Returns the x, y position of an object on the document. function c_findPos(obj) { if ($(obj) != null) { obj = $(obj); } var curleft = curtop = 0; if (obj.offsetParent) { curleft = obj.offsetLeft curtop = obj.offsetTop while (obj = obj.offsetParent) { curleft += obj.offsetLeft curtop += obj.offsetTop } } return [curleft,curtop]; } // Returns the dimesions of the window object and returns {width: int, height: int} function c_getDim() { var wh = 0; var ww = 0; if (typeof(window.innerHeight)=="number") { wh = window.innerHeight; ww = window.innerWidth; } else { if (document.documentElement && document.documentElement.clientHeight) { wh = document.documentElement.clientHeight; ww = document.documentElement.clientWidth; } else { if (document.body && document.body.clientHeight) { wh = document.body.clientHeight; ww = document.body.clientWidth; } } } return {height: wh, width: ww}; } /* formats the date input such that if you use m/d/yy as your pattern, a value such as 08/01/07 will be returned as 8/01/07. This function is flexible in that the user can use delimiters such as . and -, in such cases, it is the server that will return an error. */ function c_formatQSPDate(strDate, strPattern) { /* split the pattern to find the month section, delimiters are '.', '/', '-' */ strPattern = strPattern.toLowerCase(); var delims = /[-./]/; var mPattern = /m{1,2}/; var delim = strPattern.match(delims)[0]; var splitPattern = strPattern.split(delims); var i = 0; /* this will tell the month section */ for ( ; i < splitPattern.length; i++) { if (splitPattern[i].match(mPattern)) {break;} } if (splitPattern[i] == 'mm') { return strDate; } var splitDate = strDate.split(delims); splitDate[i] = splitDate[i].charAt(0) == '0' ? splitDate[i].replace('0', '') : splitDate[i]; return splitDate.join(delim); } // Read a page's GET URL variables and return them as an associative array. function c_getUrlParms() { var parms = [], hash; var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); for(var i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); parms.push(hash[0]); parms[hash[0]] = hash[1]; } return parms; } /******************************************************************/ /* Shortcut function to reference values */ /******************************************************************/ function $(id) { if (typeof(id)=='string') return document.getElementById(id); else return id; } function $F(id) { return $(id).value; } function $S(id, value) { $(id).value = value; } /* Function detects if Enter key is pushed and "clicks" another button * * @param {Object} e the event * @param {Object} buttonId the id of the button to click */ function c_submitEnter(e, buttonId) { if (window.event) { //ie check if (window.event.keyCode == 13) { $(buttonId).click(); } } else { //firefox check if (e.which == 13) { $(buttonId).click(); } } } //////////////////////////////////////////////////////////////////// // Deprecated methods - methods renamed // //////////////////////////////////////////////////////////////////// // Deprecated name for disableImageButton function disableButton(buttonID, message) { c_disableImageButton(buttonID, message); } // Deprecated name for trimLeft function trimL(str) { return c_trimLeft(str); } // Deprecated name for trimRight function trimR(str) { return c_trimRight(str); } // Deprecated name for trim function trim(str) { return c_trim(str); } // Deprecated name for URLencode function URLencode(sStr) { return c_URLencode(sStr); } // Deprecated, IE only, see c_capitalizeInput(evt) for better implementation. // Capitalize letters in field // the value of 'a' is 97, the value of 'z' is 122, the difference between 'A' and 'a' is 32 function capitalize() { if ((event.keyCode >= 97) && (event.keyCode <= 122)) { event.keyCode = event.keyCode - 32; } } // Returns the outerHTML of an element regardless of browser. (Only IE supports the outerHTML property) function c_outerHTML(node) { // if IE, Chrome take the internal method otherwise build one return node.outerHTML || ( function (n) { var div = document.createElement('div'), h; div.appendChild(n.cloneNode(true)); h = div.innerHTML; div = null; return h; })(node); } // ******* DEPRECATED. USE c_capitalizeInput **********/ // capitalize letters in field // the value of 'a' is 97, the value of 'z' is 122, the difference between 'A' and 'a' is 32 function c_capitalize() { if ((event.keyCode >= 97) && (event.keyCode <= 122)) { event.keyCode = event.keyCode - 32; } } // adds commas to numbers greater than 999 and adds the requested number of decimals // as well. function formatNumberWithCommas(number, precision) { try { var num = parseFloat(number); if (precision) num = num.toFixed(precision); if (isNaN(num)) return number; num = num.toString(); var parts = num.split("."); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); return parts.join('.'); } catch (ex) { return number; } }