String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}

function trackExternalLink(link, domain) {
	return function() {
	  try {
		_gaq.push(['_trackEvent', 'Outbound Links', domain, link]);
		setTimeout('document.location = "' + link + '"', 100); // short delay ensures proper tracking.
	  }catch(err){}
	}
}
function trackStaticFiles(link) {
	return function() {
		link = link.replace(/^https?:\/\/([-\w\.]+)+(:\d+)?/, '');
		_gaq.push(['_trackPageview', link]);
	}
}
// Add a tracking script to links that aren't normally tracked.
var links = document.getElementsByTagName('A');
for (var i = 0; i < links.length; i++)
{
	var url = links[i].href;
	// if the link is definiately external. (Verify the link does not contain the website's domain name!)
	if ((url.startsWith('http://') || url.startsWith('https://')) && url.indexOf(window.location.hostname) == -1)
	{
		// Parse out the domain name that's in the href.
		var matches = url.match(/^https?:\/\/([-\w\.]+)+(:\d+)?/);
		if (matches) 
			links[i].onclick = trackExternalLink(links[i].href, matches[1]);
	}
	// if the link is to an internal document that can not be embedded with a GA script.
	else if (url.endsWith('.pdf') || url.endsWith('.doc') || url.endsWith('.txt'))
		links[i].onclick = trackStaticFiles(links[i].href);
}

