<!-- $Header$ -->

<!-- Global Javascript File for the MagicMail Server -->
<!-- Hide from unsuspecting browsers
// ------------------------------------------------------------------------
// -- First section should be for javascript used in both admin and user
// ------------------------------------------------------------------------

// Function for adding events to HTML tags, ie add an on click event to a span
function addListener(elementId, eventName, eventHandler) {
    var target = document.getElementById(elementId);
    if (target) {
        if (target.addEventListener) {
            target.addEventListener(eventName, eventHandler, false);
        } else if (target.attachEvent) {
            target.attachEvent("on" + eventName, eventHandler);
        }
    }
}

// Function that will fade a Help element to nothing
function fadeOut(id, fadelevel, originalOpacity) {
    var target = document.getElementById(id);
    var newfadelevel = fadelevel;
    if (target) {
        if (newfadelevel > 0) {
            newfadelevel -= 10;
            if (document.all) {
                target.style.filter = 'alpha(opacity=' + newfadelevel + ')';
            } else {
                target.style.opacity = newfadelevel / 100;
            }
            setTimeout("fadeOut('" + id + "', " + newfadelevel +", " + originalOpacity + ")", 50);
        } else {
            hideId(id);
            if (document.all) {
                target.style.filter = 'alpha(opacity=' + originalOpacity + '90)';
            } else {
                target.style.opacity = originalOpacity / 100;
            }
        }
    }
}

// Show help without the need for a popup
function displayHelp(e, id) {
    var target = document.getElementById(id);
    var x = y = 0;
    if (target) {
        if (document.all) {
            y = event.clientY + document.body.scrollTop - 20;
            x = event.clientX + document.body.scrollLeft + 20;
        } else {
            x = e.pageX + 20;
            y = e.pageY - 20;
        }
        target.style.position="absolute";
        target.style.top = y + "px";
        target.style.left = x + "px";
        target.style.display = "block";
    }
}

// Hide help, though could be used for any html object with an id
function hideId(id) {
    var target = document.getElementById(id);
    if (target) {
        target.style.display = 'none';
    }
}

// Show a help file for a particular ID in a popup window
function showHelp(id) {
    var newUrl = '/show-help.php?rule=' + id;
    var helpwin = window.open(newUrl, 'mmruleshelp', 'width=450,height=450,status=yes,resizable=yes,scrollbars=yes');
    // if it's open in the background, the user may not know it, so focus it
    helpwin.focus();
}

// Show a desc for a particular Template in a popup window
function showHelpDesc(desc)
{
    var helpwin = window.open('', 'Help', 'width=450,height=250,resizable=yes,scrollbars=yes');

    // if it's open in the background, the user may not know it, so focus it
    helpwin.document.open();
    helpwin.document.write(desc);
    helpwin.document.close();
    helpwin.focus();
}

// ------------------------------------------------------------------------
// -- All functions next section should be for user_javascript.js
// ------------------------------------------------------------------------
function redirect_if_logged_on_as_user() {
    var l_url = document.location.href;

    if ((document.cookie.length > 0) && (document.cookie.indexOf('magicmail_user_auth=') >= 0)) {
        if (l_url.substr(0,l_url.indexOf(".html"))) {
            document.location.href = l_url.substr(0,l_url.indexOf(".html")) + ".php";
        }  else {
            document.location.href = "./index.php";
        }
    }
    return false;
};

function destination_if_logged_on_as_user(php_dest, action) {
        if ((document.cookie.length > 0)
          && (document.cookie.indexOf('magicmail_user_auth=') >= 0)) {
            if (! action) {
                dest_ext = '.php';
            } else {
                dest_ext = '.php?' + action;
            }
        } else {
                dest_ext =  '.html';
        }
        document.location.href = php_dest + dest_ext;
        return false;
};

// ------------------------------------------------------------------------
// -- All functions below here should be for admin_javascript.js
// ------------------------------------------------------------------------

function redirect_if_logged_on_as_admin() {
    var l_url = document.location.href;

    if ((document.cookie.length > 0) && (document.cookie.indexOf('magicmail_admin_auth=') >= 0)) {
        if (l_url.substr(0,l_url.indexOf(".html"))) {
            document.location.href = l_url.substr(0,l_url.indexOf(".html")) + ".php";
        } else {
            document.location.href = l_url + "index.php";
        }
    }

    return false;
};

function warnDelete() {
    if (! confirm("Warning!\nAre you sure you wish to delete?")) {
        return false;
    }
    return true;
}

// ------------------------------------------------------------------------
// When domain name length exceeds 26 characters length, it will show a warning message.
// ------------------------------------------------------------------------
function warnTextLength(field, maxlimit, msg) {
    if (field.value.length >= maxlimit) {
        return confirm(msg);
    }
    return true;
}
// --------------------------------------------------------------------------
//-->

// function to return the browser detected time zone GMT offset.
function detectTimeZone() {
    // NOTE: The below method (getTimezoneOffset) has been known to fail for
    // daylight savings times in the southern hemispehere for certain browsers.
    return ((new Date()).getTimezoneOffset() / 60 ) * -1;
}


