var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."),
	pageTracker;

function createCookie(name,value) {	// creates a 6 month cookie
	var date = new Date();
	date.setTime(date.getTime()+15768000000);
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}

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, cpcName = "exact_search_term", userdef, ref, re, searchq;
	if (location.search.indexOf("gclid") !== -1 || location.search.indexOf("cpc") !== -1) {	
		userdef = readCookie("__utmv");
		if (!userdef || userdef.indexOf("=exact_search_term=") === -1) {
			ref = document.referrer || "not set";
			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 {
				cpcName = "paid_referral"; 
				cpcValue = ref;
			}
			pageTracker._setCustomVar(1, cpcName, cpcValue, 1);
		}
	}
}

function runGA() {            	
	if (typeof(_gat) === "object") {
	    pageTracker = _gat._getTracker("UA-3145846-6");
	    pageTracker._setDomainName('.secondopinionnewsletter.com');
		sleuth();
		pageTracker._trackPageview();
	} else {
		setTimeout(function () { 
			runGA(); 
		}, 100);
	}
}

document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
setTimeout(function () {
	runGA();
}, 1);

