andrew4582

site.core.js

Mar 17th, 2011
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.65 KB | None | 0 0
  1. /*
  2.  
  3. site.core.js
  4. Collection of useful scripts.
  5.  
  6. Change log:
  7. Created:    Thursday March 17,2011 5:04 PM
  8.  
  9. */
  10. String.prototype.add = function (A) {
  11.     return this + A
  12. };
  13. String.prototype.trim = function () {
  14.     return this.replace(/^\s+|\s+$/g, "")
  15. };
  16. String.prototype.truncWithEllipis = function (B) {
  17.     var A = this.trim();
  18.     return [A.substring(0, B), "..."].join("")
  19. };
  20. String.prototype.stripHTML = function () {
  21.     return this.replace(/(<([^>]+)>)/ig, "")
  22. };
  23.  
  24.  
  25. // Popup window function
  26. function basicPopup(url) {
  27.     popupWindow = window.open(url, 'popUpWindow', 'height=500,width=500,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
  28. }
  29.  
  30. //positionedPopup(this.href,'myWindow','500','300','100','200','yes')
  31. var popupWindowpositioned = null;
  32. function positionedPopup(url, winName, w, h, t, l, scroll) {
  33.     settings =
  34.     'height=' + h + ',width=' + w + ',top=' + t + ',left=' + l + ',scrollbars=' + scroll + ',resizable'
  35.     popupWindowpositioned = window.open(url, winName, settings)
  36. }
  37.  
  38. //centeredPopup(this.href,'myWindow','500','300','yes');
  39. function centeredPopup(url, winName, w, h, scroll) {
  40.     LeftPosition = (screen.width) ? (screen.width - w) / 2 : 0;
  41.     TopPosition = (screen.height) ? (screen.height - h) / 2 : 0;
  42.     settings =
  43.     'height=' + h + ',width=' + w + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',resizable'
  44.     return window.open(url, winName, settings)
  45. }
  46.  
  47. function getRandom() {
  48.     var result, i, j;
  49.     result = '';
  50.     for (j = 0; j < 32; j++) {
  51.         if (j == 8 || j == 12 || j == 16 || j == 20) result = result + '-';
  52.         i = Math.floor(Math.random() * 16).toString(16).toUpperCase();
  53.         result = result + i;
  54.     }
  55.     return result
  56. }
  57.  
  58. function zulu_date(c) {
  59.     var b = new Date(c),
  60.         a = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  61.     return b.getDate() + " " + a[b.getUTCMonth()] + " " + String(b.getUTCFullYear()).slice(-2) + " " + ("00" + b.getUTCHours()).slice(-2) + ":" + ("00" + b.getUTCMinutes()).slice(-2) + "Z"
  62. };
  63.  
  64. function createXmlHttp() {
  65.  
  66.     var xmlHttp = null;
  67.     if (typeof XMLHttpRequest != "undefined") {
  68.         xmlHttp = new XMLHttpRequest();
  69.     }
  70.     else if (typeof window.ActiveXObject != "undefined") {
  71.         try {
  72.             xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
  73.         }
  74.         catch (e) {
  75.             try {
  76.                 xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
  77.             }
  78.             catch (e) {
  79.                 try {
  80.                     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  81.                 }
  82.                 catch (e) {
  83.                     xmlHttp = null;
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     return xmlHttp;
  89. }
  90.  
  91. /*is_number Return true if a value represents a number, else return false.*/
  92. function is_number(value) {
  93.     var str = value + "";
  94.     return str.match(/^-?\d*\.?\d+$/) ? true : false;
  95. }
  96.  
  97. /* is_blank Return true if value is a blank (i.e. "").*/
  98. function is_blank(value) {
  99.     var str = value + "";
  100.     return str.match(/^\s*$/) ? true : false;
  101. }
  102.  
  103. /*trim_string Remove leading and trailing blank spaces from a string. */
  104. function trim_string(str) {
  105.     var trim = str + "";
  106.     trim = trim.replace(/^\s*/, "");
  107.     return trim.replace(/\s*$/, "");
  108. }
  109.  
  110. /* are_values_equal()
  111. *   Compare values of types boolean, string and number. The types may be different.
  112. *   Returns true if values are equal */
  113. function are_values_equal(val1, val2) {
  114.     /* Make sure we can handle these values. */
  115.     switch (typeof (val1)) {
  116.         case 'boolean':
  117.         case 'string':
  118.         case 'number':
  119.             break;
  120.         default:
  121.             // alert("are_values_equal does not handle the type '" + typeof(val1) + "' of val1 '" + val1 + "'.");
  122.             return false;
  123.     }
  124.  
  125.     switch (typeof (val2)) {
  126.         case 'boolean':
  127.             switch (typeof (val1)) {
  128.                 case 'boolean':
  129.                     return (val1 == val2);
  130.                 case 'string':
  131.                     if (val2) {
  132.                         return (val1 == "1" || val1.toLowerCase() == "true" || val1.toLowerCase() == "on");
  133.                     } else {
  134.                         return (val1 == "0" || val1.toLowerCase() == "false" || val1.toLowerCase() == "off");
  135.                     }
  136.                     break;
  137.                 case 'number':
  138.                     return (val1 == val2 * 1);
  139.             }
  140.             break;
  141.         case 'string':
  142.             switch (typeof (val1)) {
  143.                 case 'boolean':
  144.                     if (val1) {
  145.                         return (val2 == "1" || val2.toLowerCase() == "true" || val2.toLowerCase() == "on");
  146.                     } else {
  147.                         return (val2 == "0" || val2.toLowerCase() == "false" || val2.toLowerCase() == "off");
  148.                     }
  149.                     break;
  150.                 case 'string':
  151.                     if (val2 == "1" || val2.toLowerCase() == "true" || val2.toLowerCase() == "on") {
  152.                         return (val1 == "1" || val1.toLowerCase() == "true" || val1.toLowerCase() == "on");
  153.                     }
  154.                     if (val2 == "0" || val2.toLowerCase() == "false" || val2.toLowerCase() == "off") {
  155.                         return (val1 == "0" || val1.toLowerCase() == "false" || val1.toLowerCase() == "off");
  156.                     }
  157.                     return (val2 == val1);
  158.                 case 'number':
  159.                     if (val2 == "1" || val2.toLowerCase() == "true" || val2.toLowerCase() == "on") {
  160.                         return (val1 == 1);
  161.                     }
  162.                     if (val2 == "0" || val2.toLowerCase() == "false" || val2.toLowerCase() == "off") {
  163.                         return (val1 === 0);
  164.                     }
  165.                     return (val2 == val1 + "");
  166.             }
  167.             break;
  168.         case 'number':
  169.             switch (typeof (val1)) {
  170.                 case 'boolean':
  171.                     return (val1 * 1 == val2);
  172.                 case 'string':
  173.                     if (val1 == "1" || val1.toLowerCase() == "true" || val1.toLowerCase() == "on") {
  174.                         return (val2 == 1);
  175.                     }
  176.                     if (val1 == "0" || val1.toLowerCase() == "false" || val1.toLowerCase() == "off") {
  177.                         return (val2 === 0);
  178.                     }
  179.                     return (val1 == val2 + "");
  180.                 case 'number':
  181.                     return (val2 == val1);
  182.             }
  183.             break;
  184.         default:
  185.             return false;
  186.     }
  187. }
  188.  
  189. /*
  190. * copy_select_options(from_select, to_select )
  191. *   copy options from the given select element. Do not copy the first option which is the descriptive
  192. *      label such as <option value="-1">Computer Name</option>
  193. *
  194. * NOTE: Attempts to preserve the current selection, or reverts to "-1" if the selection has disappeared.
  195. */
  196. function copy_select_options(from_select, to_select) {
  197.     /*
  198.     * Disable the select element so that it does not conflict with any user-interaction
  199.     */
  200.     var saved_state = to_select.disabled;
  201.  
  202.     if (!saved_state) {
  203.         to_select.disabled = true;
  204.     }
  205.  
  206.     /*
  207.     * Remember the selection value of this pulldown, if any
  208.     */
  209.     var current_value_selection = null;
  210.     if (to_select.selectedIndex) {
  211.         var current_selected_index = to_select.selectedIndex;
  212.         current_value_selection = to_select.value;
  213.     } else {
  214.         current_value_selection = "-1";
  215.     }
  216.  
  217.     /*
  218.     * Clear the select options as we are now going to refresh the list
  219.     *   Keep the descriptive label
  220.     */
  221.     for (var k = to_select.options.length - 1; k > 0; k--) {
  222.         to_select.options[k] = null;
  223.     }
  224.  
  225.     /*
  226.     * Copy options from the new select
  227.     *   Don't touch the descriptive label, start from entry 1
  228.     *   opt.text = "Computer Name"; opt.value = "-1";
  229.     */
  230.     var selected_index = 0;
  231.     for (var entry = 1; entry < from_select.options.length; ++entry) {
  232.         to_select.options.add(new Option(from_select.options[entry].text, from_select.options[entry].value));
  233.  
  234.         /*
  235.         * If the previously selected value matches this entry then we remeber the
  236.         * index of it so that we can restore the selected position
  237.         */
  238.         if (from_select.options[entry].value == current_value_selection) {
  239.             selected_index = entry;
  240.         }
  241.     }
  242.  
  243.     /*
  244.     * Restore the selected position
  245.     */
  246.     to_select.selectedIndex = selected_index;
  247.  
  248.     /*
  249.     * Re-enable the select element
  250.     */
  251.     to_select.disabled = saved_state;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment