Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- site.core.js
- Collection of useful scripts.
- Change log:
- Created: Thursday March 17,2011 5:04 PM
- */
- String.prototype.add = function (A) {
- return this + A
- };
- String.prototype.trim = function () {
- return this.replace(/^\s+|\s+$/g, "")
- };
- String.prototype.truncWithEllipis = function (B) {
- var A = this.trim();
- return [A.substring(0, B), "..."].join("")
- };
- String.prototype.stripHTML = function () {
- return this.replace(/(<([^>]+)>)/ig, "")
- };
- // Popup window function
- function basicPopup(url) {
- 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');
- }
- //positionedPopup(this.href,'myWindow','500','300','100','200','yes')
- var popupWindowpositioned = null;
- function positionedPopup(url, winName, w, h, t, l, scroll) {
- settings =
- 'height=' + h + ',width=' + w + ',top=' + t + ',left=' + l + ',scrollbars=' + scroll + ',resizable'
- popupWindowpositioned = window.open(url, winName, settings)
- }
- //centeredPopup(this.href,'myWindow','500','300','yes');
- function centeredPopup(url, winName, w, h, scroll) {
- LeftPosition = (screen.width) ? (screen.width - w) / 2 : 0;
- TopPosition = (screen.height) ? (screen.height - h) / 2 : 0;
- settings =
- 'height=' + h + ',width=' + w + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',resizable'
- return window.open(url, winName, settings)
- }
- function getRandom() {
- var result, i, j;
- result = '';
- for (j = 0; j < 32; j++) {
- if (j == 8 || j == 12 || j == 16 || j == 20) result = result + '-';
- i = Math.floor(Math.random() * 16).toString(16).toUpperCase();
- result = result + i;
- }
- return result
- }
- function zulu_date(c) {
- var b = new Date(c),
- a = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
- return b.getDate() + " " + a[b.getUTCMonth()] + " " + String(b.getUTCFullYear()).slice(-2) + " " + ("00" + b.getUTCHours()).slice(-2) + ":" + ("00" + b.getUTCMinutes()).slice(-2) + "Z"
- };
- function createXmlHttp() {
- var xmlHttp = null;
- if (typeof XMLHttpRequest != "undefined") {
- xmlHttp = new XMLHttpRequest();
- }
- else if (typeof window.ActiveXObject != "undefined") {
- try {
- xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
- }
- catch (e) {
- try {
- xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
- }
- catch (e) {
- try {
- xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
- }
- catch (e) {
- xmlHttp = null;
- }
- }
- }
- }
- return xmlHttp;
- }
- /*is_number Return true if a value represents a number, else return false.*/
- function is_number(value) {
- var str = value + "";
- return str.match(/^-?\d*\.?\d+$/) ? true : false;
- }
- /* is_blank Return true if value is a blank (i.e. "").*/
- function is_blank(value) {
- var str = value + "";
- return str.match(/^\s*$/) ? true : false;
- }
- /*trim_string Remove leading and trailing blank spaces from a string. */
- function trim_string(str) {
- var trim = str + "";
- trim = trim.replace(/^\s*/, "");
- return trim.replace(/\s*$/, "");
- }
- /* are_values_equal()
- * Compare values of types boolean, string and number. The types may be different.
- * Returns true if values are equal */
- function are_values_equal(val1, val2) {
- /* Make sure we can handle these values. */
- switch (typeof (val1)) {
- case 'boolean':
- case 'string':
- case 'number':
- break;
- default:
- // alert("are_values_equal does not handle the type '" + typeof(val1) + "' of val1 '" + val1 + "'.");
- return false;
- }
- switch (typeof (val2)) {
- case 'boolean':
- switch (typeof (val1)) {
- case 'boolean':
- return (val1 == val2);
- case 'string':
- if (val2) {
- return (val1 == "1" || val1.toLowerCase() == "true" || val1.toLowerCase() == "on");
- } else {
- return (val1 == "0" || val1.toLowerCase() == "false" || val1.toLowerCase() == "off");
- }
- break;
- case 'number':
- return (val1 == val2 * 1);
- }
- break;
- case 'string':
- switch (typeof (val1)) {
- case 'boolean':
- if (val1) {
- return (val2 == "1" || val2.toLowerCase() == "true" || val2.toLowerCase() == "on");
- } else {
- return (val2 == "0" || val2.toLowerCase() == "false" || val2.toLowerCase() == "off");
- }
- break;
- case 'string':
- if (val2 == "1" || val2.toLowerCase() == "true" || val2.toLowerCase() == "on") {
- return (val1 == "1" || val1.toLowerCase() == "true" || val1.toLowerCase() == "on");
- }
- if (val2 == "0" || val2.toLowerCase() == "false" || val2.toLowerCase() == "off") {
- return (val1 == "0" || val1.toLowerCase() == "false" || val1.toLowerCase() == "off");
- }
- return (val2 == val1);
- case 'number':
- if (val2 == "1" || val2.toLowerCase() == "true" || val2.toLowerCase() == "on") {
- return (val1 == 1);
- }
- if (val2 == "0" || val2.toLowerCase() == "false" || val2.toLowerCase() == "off") {
- return (val1 === 0);
- }
- return (val2 == val1 + "");
- }
- break;
- case 'number':
- switch (typeof (val1)) {
- case 'boolean':
- return (val1 * 1 == val2);
- case 'string':
- if (val1 == "1" || val1.toLowerCase() == "true" || val1.toLowerCase() == "on") {
- return (val2 == 1);
- }
- if (val1 == "0" || val1.toLowerCase() == "false" || val1.toLowerCase() == "off") {
- return (val2 === 0);
- }
- return (val1 == val2 + "");
- case 'number':
- return (val2 == val1);
- }
- break;
- default:
- return false;
- }
- }
- /*
- * copy_select_options(from_select, to_select )
- * copy options from the given select element. Do not copy the first option which is the descriptive
- * label such as <option value="-1">Computer Name</option>
- *
- * NOTE: Attempts to preserve the current selection, or reverts to "-1" if the selection has disappeared.
- */
- function copy_select_options(from_select, to_select) {
- /*
- * Disable the select element so that it does not conflict with any user-interaction
- */
- var saved_state = to_select.disabled;
- if (!saved_state) {
- to_select.disabled = true;
- }
- /*
- * Remember the selection value of this pulldown, if any
- */
- var current_value_selection = null;
- if (to_select.selectedIndex) {
- var current_selected_index = to_select.selectedIndex;
- current_value_selection = to_select.value;
- } else {
- current_value_selection = "-1";
- }
- /*
- * Clear the select options as we are now going to refresh the list
- * Keep the descriptive label
- */
- for (var k = to_select.options.length - 1; k > 0; k--) {
- to_select.options[k] = null;
- }
- /*
- * Copy options from the new select
- * Don't touch the descriptive label, start from entry 1
- * opt.text = "Computer Name"; opt.value = "-1";
- */
- var selected_index = 0;
- for (var entry = 1; entry < from_select.options.length; ++entry) {
- to_select.options.add(new Option(from_select.options[entry].text, from_select.options[entry].value));
- /*
- * If the previously selected value matches this entry then we remeber the
- * index of it so that we can restore the selected position
- */
- if (from_select.options[entry].value == current_value_selection) {
- selected_index = entry;
- }
- }
- /*
- * Restore the selected position
- */
- to_select.selectedIndex = selected_index;
- /*
- * Re-enable the select element
- */
- to_select.disabled = saved_state;
- }
Advertisement
Add Comment
Please, Sign In to add comment