﻿// JScript File
//var donateUrl = '';
function getFileName(prefix,str)
{
	if(endsWith(str,'/'))
		str = str.substr(0,str.length-1);
	if(str.indexOf('/')==-1)
		str = '/' + str;
	if(endsWith(prefix,"/"))
		prefix = prefix.substr(0,prefix.length-1);
	var index = str.lastIndexOf('/');
	return prefix + str.substr(index);
}
function endsWith(s, endsWith) {
    if ('string' != typeof endsWith) {
        throw('IllegalArgumentException: Must pass a ' +
            ' string to String.prototype.endsWith()');
    }
    var start = s.length - endsWith.length;
    return s.substring(start) == endsWith;
};


function addUrchin()
{
	var aTags = document.getElementsByTagName('a');
	var docTypes = new Array(2);
	docTypes[0] = '.pdf';
	docTypes[1] = '.doc';
	for(var i=0;i<aTags.length;i++)
	{
		for(var j=0;j<docTypes.length;j++)
		{
			if(endsWith(aTags[i].href,docTypes[j]))
			{
				// Replace the onclick event entirely
//				aTags[i].onclick = function(){ alert("Thankyou for downloading a Counsel and Care publication. As a charity we rely on donations for funding, if you find the publication useful and wish to make a donation please do so on the following page!"); urchinTracker(getFileName("/docs/",this.href)); window.open(this.href); window.location.href = donateUrl; return false; };
				// Add to the onclick event
//				addEventDirect(aTags[i], 'click', function(){ alert("Thankyou for downloading a Counsel and Care publication. As a charity we rely on donations for funding, if you find the publication useful and wish to make a donation please do so on the following page!"); urchinTracker(getFileName("/docs/",this.href)); window.open(this.href); this.href = getDonateUrl(this.href); })
				addEventDirect(aTags[i], 'click', function(){ urchinTracker(getFileName("/docs/",this.href)); window.open(this.href); this.href = getDonateUrl(this.href); })
//				addEventDirect(aTags[i], 'click', function(){ urchinTracker(getFileName("/docs/",this.href)); })
//				addEventDirect(aTags[i], 'click', function(){ this.href = getDonateUrl(this.href); urchinTracker(getFileName("/docs/",this.href)); })
				break;
			}
		}
	}
}

function getDonateUrl(href)
{
	// Buffer the donate url
	var str = donateUrl;
	
	// Trim any leading '/'
	while(endsWith(str,'/'))
		str = str.substr(0,str.length-1);
		
	// Add a leading '/'
	if(str.indexOf('/')==-1)
		str = '/' + str;
	
	// Construct the final url
	return str + "/?file=" + href;
}

function addEvent( objID, type, fn )
{ 
	addEvent(document.getElementById(objID),type,fn);
}
function addEventDirect( obj, type, fn )
{ 
	if ( obj.attachEvent )
	{ 
		obj['e'+type+fn] = fn; 
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
		obj.attachEvent( 'on'+type, obj[type+fn] ); 
	}
	else
		obj.addEventListener( type, fn, false ); 
} 