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;
			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() {            
	var checkoutform = document.getElementById('checkout'),
		onsubmitfunction,
		domainLinks = document.getElementsByTagName("a");
		
	if (typeof(_gat) === "object") {
		pageTracker = _gat._getTracker("UA-75292-1");
		pageTracker._setDomainName('alphacard.com');
		pageTracker._setAllowHash(false);
		pageTracker._setAllowLinker(true);
		pageTracker._addIgnoredRef("checkout.netsuite.com");
		sleuth();
		pageTracker._trackPageview();
		pageTracker._trackPageLoadTime();
		if (checkoutform) {
			onsubmitfunction = checkoutform.onclick;
			checkoutform.onclick = function() {
				onsubmitfunction();
				document.forms['cart'].elements['redirect'].value = pageTracker._getLinkerUrl(document.forms['cart'].elements['redirect'].value);
			}
		}
		if (domainLinks) {
			for (i = 0; i < domainLinks.length; i += 1) {
				if (domainLinks[i].href.indexOf("netsuite.com") !== -1) {
					domainLinks[i].onclick = function() {
						this.href = pageTracker._getLinkerUrl(this.href);
					}
				}
			}
		}
	
	
	} else {
		setTimeout(function () { 
			runGA(); 
		}, 100);
	}
}

//document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
document.write(unescape("%3Cscript src='/js/ga.js' type='text/javascript'%3E%3C/script%3E"));
runGA(); 


