
// The first thing we need to do is add the Array.push() method for those browsers
// that do not support it (IE5.0 on Windows, and IE 5.1 on Mac -- THANKS Microsoft!)
function isUndefined(property) {
    return (typeof property == 'undefined');
}

// Array.push() - Add an element to the end of an array
if (isUndefined(Array.push) == true) {
    Array.prototype.push = function() {
        var currentLength = this.length;
        for (var i = 0; i < arguments.length; i++) {
            this[currentLength + i] = arguments[i];
        }
        return this.length;
    };
}


function globalOnLoad() {
    // Assign link events
    for (var i = 0; i < document.links.length; i++) {
        if (document.links[i].className.match('External')) {
            document.links[i].onclick = function() { offsitePopup(this.href); return false; }
        }
        if (document.links[i].className.match('popup')) {
            document.links[i].onclick = function() { FM.popupNoScroll(this.href); return false; }
        }
    }

    // Run page onLoad function, if it exists
    if (typeof onLoad != 'undefined') {
        onLoad();
    }

}

function offsitePopup(url) {
    var popUp = window.open(url, 'offsiteWindow', 'width=750,height=500,scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes');
    if (typeof popUp == 'object') {
        popUp.focus();
    }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/**
 * Checks a string containing an email address and makes sure that it's valid
 *
 * @param string value  An email address to be checked for validity
 * @return boolean
 */
function checkEmail(value) {
    var result = false;

    // First, check for normal (non-quoted) email addresses
    // The username part of the RegEx is from Breaking Par (www.breakingpar.com)
    // For the domain part, I put an arbitrary but absurdly reasonable
    // limit of 6 sub-domains after the top level domain
    var check = value.match(/^([^<>()[\]:;@\\,"\s.]+(\.[^<>()[\]:;@\\,"\s.]+)*)@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}[a-zA-Z0-9]\.){1,6}[a-zA-Z]{2,64}$/);

    if (check != null) {
        // This is an arbitrary but reasonable limit of 100 characters for the local-part (username)
        if (check[1].length <= 100) {
            result = true;
        }
    } else {
        // If it didn't match that, see if the local-part is a quoted-string
        // (again, with a limit of 100 characters)
        check = value.match(/^"(.{1,100})"@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}[a-zA-Z0-9]\.){1,6}[a-zA-Z]{2,64}$/);
        if (check != null) {
            if (check[1].search(/[^\\]"/) == -1) {
                result = true;
            }
        }
    }

    return(result);
} // checkEmail()

String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

// Make sure the "FM" namespace object exists
if (typeof FM != 'object') {
    FM = new Object();
}

/**
 * Displays the specified URL in a new window with all menus, tools, and scrollbars
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   url             URL to display in the popup window
 * @param   width           (Optional) The width of the new popup window
 * @param   height          (Optional) The height of the new popup window
 * @param   windowName      (Optional) The name of the window
 */
FM.popup = function(url, width, height, windowName) {
    // Default to a 700 x 500 window
    width = (!isNaN(width) ? width + '' : '700');
    height = (!isNaN(height) ? height + '' : '500');
    windowName = (windowName == undefined || windowName == '' ? 'popup' : windowName);

    // Create it
    var popUp = window.open(url, windowName, 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes');

    // Bring it to the front
    if (typeof popUp == 'object') { popUp.focus(); }

}  // popup()

/**
 * Displays the specified URL in a new window with NO menus or tools, but with scrollbars
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   url             URL to display in the popup window
 * @param   width           (Optional) The width of the new popup window
 * @param   height          (Optional) The height of the new popup window
 * @param   windowName      (Optional) The name of the window
 */
FM.popupNoTools = function(url, width, height, windowName) {
    // Default to a 700 x 500 window
    width = (!isNaN(width) ? width + '' : '700');
    height = (!isNaN(height) ? height + '' : '500');
    windowName = (windowName == undefined || windowName == '' ? 'popup' : windowName);

    // Create it
    var popUp = window.open(url, windowName, 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,menubar=no,toolbar=no,location=no');

    // Bring it to the front
    if (typeof popUp == 'object') { popUp.focus(); }

}  // popupNoTools()

/**
 * Displays the specified URL in a new window with NO menus, tools OR scrollbars
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   url             URL to display in the popup window
 * @param   width           (Optional) The width of the new popup window
 * @param   height          (Optional) The height of the new popup window
 * @param   windowName      (Optional) The name of the window
 */
FM.popupNoScroll = function(url, width, height, windowName) {
    // Default to a 700 x 500 window
    width = (!isNaN(width) ? width + '' : '700');
    height = (!isNaN(height) ? height + '' : '500');
    windowName = (windowName == undefined || windowName == '' ? 'popup' : windowName);

    // Create it
    var popUp = window.open(url, windowName, 'width=' + width + ',height=' + height + ',scrollbars=no,resizable=no,menubar=no,toolbar=no,location=no');

    // Bring it to the front
    if (typeof popUp == 'object') { popUp.focus(); }

}  // popupNoScroll()

addLoadEvent(globalOnLoad);
