/****************************************************
     addLinkerEvents-ga.js
     Author: Brian J Clifton
     Url: http://www.advanced-web-metrics.com
     This script is free to use as long as this info is left in
     
     Combined script for tracking external links, file downloads and mailto links
     
     All scripts presented have been tested and validated by the author and are believed to be correct
     as of the date of publication or posting. The Google Analytics software on which they depend is 
     subject to change, however; and therefore no warranty is expressed or implied that they will
     work as described in the future. Always check the most current Google Analytics documentation.

     Custom-made Edition
      - 2008/12/09 : works when "a" has no 'onclick' attribution

****************************************************/


// Only links written to the page (already in the DOM) will be tagged
// This version is for ga.js (July 15th 2008)


function addLinkerEvents() {
	var as = document.getElementsByTagName("a");
	var extTrack = ["www.fepc.or.jp"];
	var extDoc = [".doc",".xls",".exe",".zip",".pdf",".js",".asx"];
	
	for(var i=0; i<as.length; i++) {
		var flag = 0;
		var tmp = as[i].getAttribute("onclick");

		if (tmp != null) {
		  tmp = String(tmp);
		  if (tmp.indexOf('urchinTracker') > -1 || tmp.indexOf('_trackPageview') > -1) continue;
    		}

		for (var j=0; j<extTrack.length; j++) {
			if (as[i].href.indexOf(extTrack[j]) == -1 && as[i].href.indexOf('google-analytics.com') == -1) {
				flag++;
			}
		}
		
		//Outbound Link
		if (flag == extTrack.length && as[i].getAttribute('onclick') == null){
			var splitResult = as[i].href.split("//");
			as[i].setAttribute("onclick", "pageTracker._trackPageview('/outbound/" +splitResult[1]+ "');"+((tmp != null) ? tmp+";" : ""));
		}			

		//Download
		for (var j=0; j<extDoc.length; j++) {
			if (as[i].href.indexOf(extTrack[0]) != -1 && as[i].href.indexOf(extDoc[j]) != -1 && as[i].getAttribute('onclick') == null) {
				var splitResult = as[i].href.split(extTrack[0]);
 var Result = splitResult[1].replace('__icsFiles/afieldfile/','file');
 var Result = Result.replace(/\d{4}\/\d{2}\/\d{2}/,'');
 var Result = Result.replace('about_us/pr/kaiken/file','news/file');
 var Result = Result.replace('library/data/demand/file','news/file');
 var Result = Result.replace('library/data/hatsujuden/file','news/file');
				as[i].setAttribute("onclick",((tmp != null) ? tmp+";" : "") + "pageTracker._trackPageview('" +Result+ "');");
				//alert(Result)
				break;
			}
		}

		//Mail
		if (as[i].href.indexOf("mailto:") != -1  && as[i].href.indexOf("gashbug@google.com") == -1 ) {
			var splitResult = as[i].href.split(":");
			as[i].setAttribute("onclick",((tmp != null) ? tmp+";" : "") + "pageTracker._trackPageview('/mailto/" +splitResult[1]+ "');");
			//alert(splitResult[1])
			break;
		}
	}
}
Event.observe(window, 'load', addLinkerEvents, false);