var _gaq = _gaq || [];
var _ua = '';
var _filetypes = /\.(zip|exe|pdf|doc|xls|ppt|mp(3|4)|eps|docx|rar|csv|gz|dmg|xml|ashx)$/i;

//-------------------------------
// MANAGING ONLOAD EVENTS
//-------------------------------
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();
    }
  }
}

//------------------------------
// ADDING TRACKING TO LINKS
//------------------------------
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 + "'])");
				}
			}
		}
	}
}

add_load_event(extend_tracking);


//--------------------------------
// ADDING CUSTOMVAR CUSTOMER LEVEL
//--------------------------------
function customer_level(){
	if (/^https:\/\/www\.one\.com\/admin\/frontpage\.do/.test(window.location)){
		if (document.getElementById('membership_type')) { 
			_gaq.push(['_setCustomVar',
			1,														// This custom var is set to slot #1. 
			'Customertype',											// The 2nd-level name for your online content categories.
			document.getElementById('membership_type').innerHTML,	// Sets the value of "Customertype"
			1														// Sets the scope to visitor-level.
			]);
		}
		if (document.getElementById('black')) { 
			_gaq.push(['_setCustomVar',
			1,														// This custom var is set to slot #1. 
			'Black',												// The 2nd-level name for your online content categories.
			document.getElementById('black').innerHTML,				// Sets the value of "Customertype"
			1														// Sets the scope to visitor-level.
			]);
		}
	}
}
add_load_event(customer_level);
