﻿// Register hits from newsletters
	var NewsletterID = getParameterByName("NewsletterID");
	var SubscriberID = getParameterByName("SubscriberID");
	if (NewsletterID!='' && SubscriberID!='')
	{
		_gaq.push(['_trackEvent', 'Link from Newsletter', 'Newsletter: ' + NewsletterID, window.location.href]);
		_gaq.push(['_trackEvent', 'Link from Newsletter', 'Subscriber: ' + SubscriberID, window.location.href]);
	}
	
$(document).ready(function () 
{
	
	
    // Handle form abandonment rate
    $(':input').blur(function () {
        if($(this).val().length > 0)
	{
            _gaq.push(['_trackEvent', 'Form: ' + location.pathname + $(this).parents('form').attr('name'), 'input_exit', $(this).attr('name')]);
            //_gaq.push(['_trackEvent', 'input_exit', 'Form: ' + location.pathname + $(this).parents('form').attr('name'), $(this).attr('name')]); //Better structure
        } 
    });

	//loop though each anchor element
	$('a').each(function(){
	    
		var href = $(this).attr('href');
		var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
	    
		if (href) {
		//check for links starting with http or https, making sure that links to our own domain are excluded
		if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
			$(this).click(function() {
				var extLink = href.replace(/^https?\:\/\//i, '');
				_gaq.push(['_trackEvent', 'External', 'Click', extLink]);
			});
		}
		//check for links starting with mailto:
		else if (href.match(/^mailto\:/i)){
			$(this).click(function() {
				var mailLink = href.replace(/^mailto\:/i, '');
				_gaq.push(['_trackEvent', 'Email', 'Click',mailLink]);
			});
		}
		//check for links with file extension that match the filetypes regular expression:
		else if (href.match(filetypes)){
			$(this).click(function() {
				var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				_gaq.push(['_trackEvent', 'Download', 'Click - ' + extension, href]);
			});
		}
		}

	});
});

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

