var _filetypes = /\.(zip|exe|pdf|doc|xls|ppt|mp(3|4)|eps|docx|rar|csv|gz|dmg|xml|ashx)$/i;

function extend_tracking() {
	var a = document.getElementsByTagName("a");
	
	for (var i = 0; i<a.length; i++) {
		var href = a[i].getAttribute("href");
		var onclick = a[i].getAttribute("onclick");
		var target = a[i].getAttribute("target");
		var prev_js = ((onclick != null) ? onclick + "; " : "");
		var page = location.pathname;
		
		if (href) {
			if ((href.match(/^#?https?\:/i)) && (!href.match(document.domain))) {
				var ext_link = href.replace(/^https?\:\/\//i, '').replace(/^#/,'');
				a[i].setAttribute("onclick", prev_js + "_gaq.push(['_trackEvent', 'External links', 'Click', '" + ext_link + "'])");
			} else if (href.match(/^mailto\:/i)) {
				var mail_link = href.replace(/^mailto\:/i, '');
				a[i].setAttribute("onclick", prev_js + "_gaq.push(['_trackEvent','Email', 'Click', '" + mail_link + "'])");					
			} else if (href.match(_filetypes)) { 
				var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				a[i].setAttribute("onclick", prev_js + "_gaq.push(['_trackEvent', 'Download', '" + extension + "', '" + href + "'])");				
			} 
		}
		
		if (onclick) {
			if ((/window\.print/i).exec(onclick) != null) {
				a[i].setAttribute("onclick", prev_js + "_gaq.push(['_trackEvent', 'Print', 'Click', '" + page + "'])");					
			} else if ((/TipAFriend/i).exec(onclick) != null) {
				a[i].setAttribute("onclick", prev_js + "_gaq.push(['_trackEvent', 'Tip a friend', 'Click', '" + page + "'])");									
			} else if ((/addthis_sendto/i).exec(onclick) != null) { 
				a[i].setAttribute("onclick", prev_js + "_gaq.push(['_trackEvent', 'Share', 'Click', '" + page + "'])");									
			} else if ((/validate\(\)/i).exec(onclick) != null) {
				if (page && page.match(/Newsletter/i)) {
					a[i].setAttribute("onclick", prev_js + "_gaq.push(['_trackEvent', 'Newsletter', 'Subscribe', '" + page + "'])");									
				}
			}
		}
	}
}

function add_load_event(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
    	window.onload = func;
	} else {
    	window.onload = function() {
      		if (oldonload) {
        		oldonload();
      		}
      		func();
    	}
  	}
}


add_load_event(function() { 
	if (typeof(_gaq) != "undefined") {
		extend_tracking();
				
		if(/\/search\.jsp/.test(location.pathname)){
			var searchterm='';
			searchterm='/?q='+document.getElementsByName('TO_SEARCH')[0].value;
			_gaq.push(['_trackPageview', location.pathname+searchterm]);

		}

		// code for iframe tracking
		if (/contentOID=(1074053658|1074053647|1074053627)/.test(location.href) && document.getElementsByTagName('iframe').length > 0){
			_gaq.push(function() {
			  var pageTracker = _gat._getTrackerByName();
			  var iframe = document.getElementsByTagName('iframe')[0];
			  iframe.src = pageTracker._getLinkerUrl(iframe.src);
			});
		}

		if (/http:\/\/www\.fiatbasis\.dk\/fiatfrontend\/RequestCarExchange\.aspx\?brandid=1/.test(location.href)){
			if (document.getElementById('divStep5')){
				_gaq.push(['_trackEvent','getanoffer','submit']);
			}
		}
		if (/http:\/\/www\.fiatbasis\.dk\/fiatfrontend\/RequestCarBrochure\.aspx\?requesttype=2\&brandid=1/.test(location.href)){
			if (document.getElementById('divStep3')){
				_gaq.push(['_trackEvent','testdrive','submit']);
			}
		}
	}
});

