﻿/// <reference path="jquery-1.3.1-vsdoc.js" />

function docWrite(str) {
    // Useful if you need to write a swf file, that needs to work in IE.
    document.write(str);
}

function PopUp(url, features, name) {
    // Attempt to pop up a new window.  If blocked, redirect to that page.
    var newwin = window.open(url, name, features);
    if (!newwin) { document.location = url; }
}

function closePopUp() {
    try {
        var winopener = window.opener;
        if (winopener) {
            // Make sure popup blocker is not enabled, before attempting window.close();
            if (winopener.location != window.location) {
                window.close();
                return 
            }
        }
        if (history.length > 0) {
            // PopUp blocker detected, and history exists, try to just go back.
            // Note that history.length starts at 1 in Firefox, but in Firefox, 
            // the previous step has probably already closed this window.
            history.go(-1);
        } else {
            // PopUp blocker detected, but no history exists, try to close current window.
            var winself = window.open('', '_self', '');
            window.close();
        }
    } catch (e) {
    }
}

function mcmsEml() {
    $(document).ready(function() {
        $('.mcmsEml').each(function() {
            if (this.href)
                this.href = this.href.replace(/\.at\./, '@').replace(/.*#/, 'mailto:');
            this.innerHTML = this.innerHTML.replace(/\.at\./, '@');
        }); 
    }); 
}

var mcmsRegSiteRoot, mcmsRegSecureSiteRoot, mcmsRegFileRoot;
function mcmsInitGALinks(sSiteRoot,sSecureSiteRoot,sFileRoot) {
    // Google Analytics helper.
    if (sSiteRoot) mcmsRegSiteRoot = new RegExp('^' + sSiteRoot.toLowerCase() + '.*$');
    if (sSecureSiteRoot) mcmsRegSecureSiteRoot = new RegExp('^' + sSecureSiteRoot.toLowerCase() + '.*$');
    if (sFileRoot) mcmsRegFileRoot = new RegExp(sFileRoot.toLowerCase());
    $(document).ready(function() {
        $("a[href*=':'],a.mcmsEml").click(function() {
            try {
                if (this.href && _gaq && !$(this).attr('gatracked')) {
                    var sHref = this.href.toLowerCase();
                    $(this).attr('gatracked', true);
                    if (/^https?:\/\/.*/.test(sHref)) {
                        if (mcmsRegSiteRoot.test(sHref) || mcmsRegSecureSiteRoot.test(sHref)) {
                            // internal link...
                            if (mcmsRegFileRoot.test(sHref)) {
                                _gaq.push(['_trackEvent','download','click',sHref]);
                            }
                        } else {
                            _gaq.push(['_trackEvent','outbound','click',sHref]);
                        }
                    } else if (mcmsRegFileRoot.test(sHref)) {
                        _gaq.push(['_trackEvent','download','click',sHref]);
                    } else if (/mailto:/.test(sHref)) {
                        _gaq.push(['_trackEvent','mailto','click',sHref.substr(7, sHref.length)]);
                    }
                }
            } catch (err) { }
        });
    }); 
}

function mcmsOnEndRequest(sender, args) {
    // Display a pop-up message if there are any unhandled server-side errors processing an AJAX Request.
    var err = args.get_error();
    if (err != null) {
        args.set_errorHandled(true);
        var s = err.message;
        s = s.substr(s.indexOf(':') + 2, s.length);
        
        alert(s);
    }
}

