var link = new Object();
link.external = 5;
link.track = 2;
link.newWindow = 4;
link.email = 6;

function trackLink(href,parameters) {
        var parameterText = '';
        var parameterSeparator = '&';
        for (var key in parameters) {
                parameterText += parameterSeparator + key + '=' + parameters[key];
        }
        return 'http://ofxspo.pajmc.com/cgi-bin/rr.cgi/' + href + parameterText;
}
function externalLink(href,linkText) {
        if (!linkText || linkText == '') {
                linkText = href;
        }
        return '/services/mgi2/library/jsp/leavingpage.jsp?externalUrl=' + escape(href) + '&linkText=' + escape(linkText);
}

function makeAbsoluteLink(href) {
        // Already absolute ...
        if (/^\w*:\/\//.exec(href)) {
                return href;
        }

        // Relative to document root ...
        if (/^\//.exec(href)) {
                return location.protocol + '//' + location.host + href;
        }

        // Relative URL ...
        var path = navigation.currentPage;

        if (/^(.*)\/[^\/]*/.exec(path)) {
                        path = RegExp.$1;       
        }

        var dummy;
        // href has ../xx and path has xx/xx
        while (/^\.\.\/.*/.exec(href) && /.*\/.*/.exec(path)) {

                // chop ../ from front href
                dummy = /^\.\.\/(.*)/.exec(href);
                href = RegExp.$1;
                
                //      chop /xxxx off end path
                dummy = /^(.*)\/[^\/]*/.exec(path);
                path = RegExp.$1;
        }
        return path + '/' + href;
}

function transformLink(href,flags,linkText,parameters)
{
	if (!flags) 
	{
		flags = 0;
	}

	
	//flag 6 is an email link
	if (flags == 6 ) 
	{
		return href ;
	}
	else
	{
		if (flags & (link.external & ~link.newWindow)) 
		{
			//href = escape(href);
			//href = makeAbsoluteLink(href);
			href = externalLink(href,linkText);
		}
		else
		{
			if (flags & link.track) 
			{
				// a bug in netscape 4.6 can't open links with 
				// spaces in the name (some PDFs contain spaces) 
				href = escape(href);
				href = makeAbsoluteLink(href);
				href = trackLink(href,parameters);
			}
		};
	};
	return href;
}

function linkTo(href,flags,linkText,parameters)
{
	href = transformLink(href,flags,linkText,parameters);
	if (flags & link.newWindow)
	{
		window.open(href);
	} 
	else 
	{
		location.href = href;
	};
}
