
var currentPageTitle = "";

function addLinkEvents()
{
	// Keep a copy of the current page title as this can change when
	// document downloads take place for fundDoctables
	currentPageTitle = document.title;

	var extDoc = [".doc",".xls",".zip",".ppt", ".wmv", ".pdf", "getDocument.jsp"];

	var docDownloadTrackingFunction='pageTracker._trackPageview';

	var indexFound = -1;

	var splitResult = "";
	var currentHref = "";
	var fundDocName = "";
	var fundDocTitle = "";
	
	var as = document.getElementsByTagName("a");
	for(var i=0; i<as.length; i++) {
		var tmp = as[i].getAttribute("onclick");
		if ((tmp != null) && (tmp != undefined))
		{
			if (tmp.toString().indexOf(docDownloadTrackingFunction) > -1)
			{
				// Link already with onclick tracking event, fundDocTables would
				// have populated this server-side. In this case skip modifying link
				continue;
			}
		}

		// If link contains document to download under staticfiles or other web sites, then trackit
		for (var j=0; j<extDoc.length; j++) 
		{
			if (as[i].href.indexOf(extDoc[j]) != -1) 
			{
				// Track FundDocuments not in FundDocTables
				if (extDoc[j] == "getDocument.jsp")
				{			
					currentHref = as[i].href;
    				// Also report document title as for FundDocTables
					splitResult = currentHref.split("portal");	
					fundDocName = "/downloads/funddocs/" + siteName + "/" + channelName + "/" + as[i].innerHTML + "/portal" + splitResult[1] + "/" ;									
					fundDocTitle = "/downloads/funddocs/portal" + splitResult[1];									

					as[i].setAttribute("onclick",((tmp != null) ? tmp+";" : "") + "pageTracker.u.a.title='" +fundDocTitle + "';" + docDownloadTrackingFunction + "('" + fundDocName + "');" + "document.title=currentPageTitle;");									
				}
				else
				{
					currentHref = as[i].href.toLowerCase();
					// Track normal static files or links to pdfs in other sites
					if (currentHref.indexOf("staticfiles") != -1)
					{
						splitResult = currentHref.split("staticfiles");				
						as[i].setAttribute("onclick",((tmp != null) ? tmp+";" : "") + docDownloadTrackingFunction + "('/downloads/" + siteName + "/" + channelName + "/staticfiles" + splitResult[1]+ "');");
					}
					else
					{
						// Links to file not in staticfiles area sometimes linked to other sites like ir.schroders.com
						splitResult = currentHref.split("//");	
						as[i].setAttribute("onclick",((tmp != null) ? tmp+";" : "") + docDownloadTrackingFunction + "('/downloads/" + siteName + "/" + channelName + "/" + splitResult[1] + "');");				
					}
				}

				break;
			}
		}
	}	

}
