
//**************Natural Search/Universal Tracker Plugin***************
//********************************************************************
var atlasSearch = new atlasSearchObject();
//Configuration Settings


//Click Tag settings - these tags should be provided by your Account Manager

atlasSearch.engines['google'].clickTag = 'clk.atdmt.com/9QP/go/167214768/direct/01/';
atlasSearch.engines['yahoo'].clickTag = 'clk.atdmt.com/9QP/go/167214900/direct/01/';
atlasSearch.engines['live'].clickTag = 'clk.atdmt.com/9QP/go/167214899/direct/01/';
atlasSearch.engines['bing'].clickTag = 'clk.atdmt.com/9QP/go/167214582/direct/01/';
atlasSearch.engines['ask'].clickTag = 'clk.atdmt.com/9QP/go/167214572/direct/01/';
atlasSearch.engines['aol'].clickTag = 'clk.atdmt.com/9QP/go/167214145/direct/01/';

//Action Tag - this action tag will capture extended natural search data and universal referral data if configured below

atlasSearch.actionTag = 'ParasolNaturalSearchTracker';

//Paid Search parameter - to distinguish paid search from natural search, a parameter must be present to indicate a paid link.  Please refer to your Account manager for details of how this should be set 

atlasSearch.paidSearchParam = 'atlassearch=paid';

//Brand Terms array - keywords can be categorised into brand vs non-brand by adding brand terms to the array below

atlasSearch.brandTerms = ['Calculating tax','Contractor','Employee expenses','Parasol','Parasolgroup.co.uk','Parasolit','PAYE','Calculator','Umbrella'];

//Universal tracking allows a generic action tag to be placed on every landing page.  Use the exclusion list to exclude referring domains you do not want to track

atlasSearch.universalTracking = 'false';
atlasSearch.universalTrackingExclusionList = ['example1.com'];



//*****************Natural Search/Universal Tracker Plug-in Code - DO NOT MODIFY*******************
//*************************************************************************************************
//*************************************************************************************************
//*************************************************************************************************

if(atlasSearch.referrerURL != '')
{atlasSearch.referrerInfo.referrerType = 'Non-search';}

if(atlasSearch.locationURL.indexOf(atlasSearch.paidSearchParam) != -1 )
{atlasSearch.referrerInfo.paid = 'paid';}

updateReferrerInfo(atlasSearch);

if(atlasSearch.referrerInfo.referrerType == 'Search' && atlasSearch.referrerInfo.paid == 'non_paid' && atlasSearch.referrerInfo.atlasClickURL)
{
	var atlassearchclickRequest = new Image();
	atlassearchclickRequest.src = atlasSearch.protocol+atlasSearch.referrerInfo.atlasClickURL+'?href='+atlasSearch.protocol+'image.atdmt.com/images/pixel.gif';	
}

if(atlasSearch.referrerInfo.referrerType == 'Search' && atlasSearch.referrerInfo.paid == 'non_paid')
{
	var atlassearchactionRequest = new Image();
	atlassearchactionRequest.src = atlasSearch.protocol+'switch.atdmt.com/action/'+atlasSearch.actionTag+'/v3/atc1.'+atlasSearch.referrerInfo.referrerType+'/atc2.'+atlasSearch.referrerInfo.referrerDetail+'/atc3.'+atlasSearch.referrerInfo.paid+'/atc4.'+atlasSearch.referrerInfo.keywordType+'/keyword.'+escape(atlasSearch.referrerInfo.keyword)+'/fullReferrer.'+escape(atlasSearch.referrerURL)+'/landingPageURL.'+escape(atlasSearch.locationURL);
}
else if(atlasSearch.universalTracking == 'true')
{
	if(window.location.hostname != '')
	{atlasSearch.universalTrackingExclusionList.push(window.location.hostname);}
	
	atlasSearch.referrerhostname = atlasSearch.referrerURL;

	if(atlasSearch.referrerhostname.indexOf('?') != -1)
	{atlasSearch.referrerhostname = atlasSearch.referrerhostname.substring(0,atlasSearch.referrerhostname.indexOf('?'))}

	atlasSearch.requestRequired = 'true';

	for(i=0;i<atlasSearch.universalTrackingExclusionList.length;i++)
	{
		if(atlasSearch.referrerhostname.indexOf(atlasSearch.universalTrackingExclusionList[i]) != -1)
		{atlasSearch.requestRequired = 'false';}
	}

	if(atlasSearch.requestRequired == 'true')
	{
		var atlassearchactionRequest = new Image();
		atlassearchactionRequest.src = atlasSearch.protocol+'switch.atdmt.com/action/'+atlasSearch.actionTag+'/v3/atc1.'+atlasSearch.referrerInfo.referrerType+'/atc2.'+atlasSearch.referrerInfo.referrerDetail+'/atc3.'+atlasSearch.referrerInfo.paid+'/atc4.'+atlasSearch.referrerInfo.keywordType+'/keyword.'+escape(atlasSearch.referrerInfo.keyword)+'/fullReferrer.'+escape(atlasSearch.referrerURL)+'/landingPageURL.'+escape(atlasSearch.locationURL);
    }
}



function atlasSearchObject()
{
	this.actionTag = '';
	this.protocol = protocolFinder();
	this.paidSearchParam = '';
	this.brandTerms = new Array();
	this.referrerInfo = {'referrerType':'DirectType','referrerDetail':'na','paid':'non_paid','keywordType':'na','keyword':'na'};
	this.universalTracking = '';
	this.universalTrackingExclusionList = new Array();
	this.locationURL = window.location + '';
	this.referrerURL = document.referrer + '';
	this.engines = new Array();
	this.engines['google'] = {'name':'Google Search','referrerURLSubstring':'.google.','keywordStart':'q=','keywordEnd':'&'};
	this.engines['yahoo'] = {'name':'Yahoo Search','referrerURLSubstring':'search.yahoo.co','keywordStart':'p=','keywordEnd':'&'};
	this.engines['live'] = {'name':'MSN Live','referrerURLSubstring':'search.live.co','keywordStart':'q=','keywordEnd':'&'};
	this.engines['bing'] = {'name':'Bing Search','referrerURLSubstring':'bing.co','keywordStart':'q=','keywordEnd':'&'};
	this.engines['ask'] = {'name':'Ask.com','referrerURLSubstring':'ask.com/web?q=','keywordStart':'q=','keywordEnd':'&'};
	this.engines['aol'] = {'name':'AOL Search','referrerURLSubstring':'search.aol.co','keywordStart':'query='};
}
function updateReferrerInfo(atlasSearchObjectInstance)
{
	for (var i in atlasSearchObjectInstance.engines)
	{
	    var selectedEngine = atlasSearchObjectInstance.engines[i];
	    //alert('Testing Engine ' + selectedEngine.name + '<br>'); 	
    	if(atlasSearchObjectInstance.referrerURL.indexOf(selectedEngine.referrerURLSubstring) != -1 )
	    {
		    atlasSearchObjectInstance.referrerInfo.referrerType = 'Search';
		    atlasSearchObjectInstance.referrerInfo.referrerDetail = selectedEngine.name;
		    var keywordString = 'unknown - not provided by engine';
		    if(selectedEngine.keywordStart)
		    {
			    if(atlasSearchObjectInstance.referrerURL.indexOf(selectedEngine.keywordStart) != -1)
			    {keywordString = atlasSearchObjectInstance.referrerURL.substring(atlasSearchObjectInstance.referrerURL.indexOf(selectedEngine.keywordStart)+selectedEngine.keywordStart.length);}
		    }
				
		    if(selectedEngine.keywordEnd)
		    {
			    if(keywordString.indexOf(selectedEngine.keywordEnd) != -1)
			    {keywordString = keywordString.substring(0,keywordString.indexOf(selectedEngine.keywordEnd));}
		    }
		
		    if(keywordString != 'unknown - not provided by engine')
		    {
		        atlasSearchObjectInstance.referrerInfo.keywordType = 'Non_Brand';
		        for(j=0;j<atlasSearchObjectInstance.brandTerms.length;j++)
	            {
	                var brand_term = atlasSearchObjectInstance.brandTerms[j];
		            if(keywordString.indexOf(brand_term) != -1)
		            {atlasSearchObjectInstance.referrerInfo.keywordType = 'Brand';}
	            }
		    
		    }

		    atlasSearchObjectInstance.referrerInfo.keyword = keywordString;
		    atlasSearchObjectInstance.referrerInfo.atlasClickURL = selectedEngine.clickTag;
		
		//alert('SUCCESS! - Engine: ' + referrerInfo.engineName + ' keyword:' + referrerInfo.keyword + '<br>');	
	    } 	
	}
}

function protocolFinder()
{
	var protocol = 'http://';
	if(window.location.protocol == 'https:')
	{protocol = 'https://';}

	return protocol;
}