var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

var ga_loc = location.hostname;

var pageTracker;

function readCookie(name) {		// function to read cookie
	var nameRegex = RegExp("(?:;\\s|^)" + name + "=([^;]+)");	// match name of cookie and store value in $1
	nameValue = nameRegex.exec(document.cookie);				
	if(nameValue) {	
		return nameValue[1];
	} else {
		return null;
	}
}

function noPercent(x) {
	x = unescape(x);
	return x.replace(/\+/g, " ").replace(/(^ +| +$)/g, "");
}

function sleuth() {
	var cpcValue = "cpc - ", userdef, cpcregexp, ref, re, searchq;
	if (location.search.indexOf("gclid") !== -1 || location.search.indexOf("cpc") !== -1) {	
		userdef = readCookie("__utmv");
		cpcregexp = new RegExp(/^cpc - /);
		if (!userdef || !cpcregexp.exec(userdef)) {
			ref = document.referrer;
			re = new RegExp(/(?:\?|&)(?:q|p|query|encquery|qt|terms|rdata|qs|wd|text|szukaj|k|searchExpr|search_for|string|search_query|searchfor)=([^&]+)/);
			searchq = re.exec(ref);
			if (searchq) { 
				cpcValue += noPercent(searchq[1]);
			} else {
				cpcValue += "Paid Referral: "; 
				cpcValue += ref;
			}
			pageTracker._setVar(cpcValue);
		}
	}
}

var ga_settek = RegExp(/settek/);

function runGA() {            
	if (typeof(_gat) === "object") {
		if (ga_settek.exec(ga_loc)){
			try {
				pageTracker = _gat._getTracker("UA-11932965-1");
				pageTracker._setDomainName(".settek.com");
				pageTracker._trackPageview();
				sleuth();
				var xlsdocs = document.getElementsByTagName('a');
				for (var i=0; i<xlsdocs.length; i++){
					if (xlsdocs[i].href.match(/\/downloads\/chainofcustody/)){
						xlsdocs[i].onclick = function() {
							pageTracker._trackPageview('/downloads/chainofcustody.xls');
							window.setTimeout(function() { location.href=xlsdocs[i].href;}, 300);
							}
					}
				}
			} catch (err) {}
		}
	} else {
		setTimeout(function () { 
			runGA(); 
		}, 100);
	}
}

runGA();
