Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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