var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."),
	pageTracker;

function sleuth() {
	var cpcValue = "cpc - ", 
		userdef, 
		cpcregexp, 
		ref, 
		re, 
		searchq;
	
	function readCookie(name) {		// function to read cookie
		var nameRegex = new RegExp("(?:;\\s|^)" + name + "=([^;]+)"), nameValue;	// match name of cookie and store value in $1
		nameValue = nameRegex.exec(document.cookie);				
		return nameValue && nameValue[1];
	}
	
	function noPercent(x) {
		x = unescape(x);
		return x.replace(/\+/g, " ").replace(/(^ +| +$)/g, "");
	}
	
	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);
		}
	}
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function nsLink(nsTag, nsAtt, nsZall) {
	var i = 0,
	nsEle = document.getElementsByTagName(nsTag);
	
	for(i = 0; i < nsEle.length; i++) {
		if(nsEle[i][nsAtt].indexOf("netsuite.com") > -1) {
			nsEle[i][nsAtt] = pageTracker._getLinkerUrl(nsEle[i][nsAtt]) + '&' + nsZall;
		}
	}
}

function setCookie(name, value, expires, path, domain, overwrite) {
			// set time, it's in milliseconds
			var today = new Date();
			today.setTime(today.getTime());
			/*
				if the expires variable is set, make the correct 
				expires time, the current script below will set 
				it for x number of days, to make it for hours, 
				delete * 24, for minutes, delete * 60 * 24
			*/
			if (expires) {
				expires = expires * 1000 * 60 * 60 * 24;
			}
			else {
				expires = 365 * 24 * 60 * 60 * 1000;
			}
			var expires_date = new Date(today.getTime() + (expires));
			if(overwrite == true || !document.cookie.match('(^|;\\s)' + name)){
				document.cookie = name + "=" + value + ((expires) ? ";expires=" + expires_date.toGMTString() : "") + ((path) ? ";path=" + path: ";path=/") + ((domain) ? ";domain=" + domain: "");
			}
}

function storeQueryString() {
    var querystring = new Array;
    var q = String(document.location).split('?')[1];
    if (!q) return false;
    q = q.split('&');
    for (var i = 0; i < q.length; i++) {
        var o = q[i].split('=');
        querystring[o[0]] = o[1];
    }
    return querystring;
}

function runGA() {
	if (typeof(_gat) === "object") {
		pageTracker = _gat._getTracker("UA-1064919-3");
		pageTracker._setDomainName("bizactions.com");
		pageTracker._setAllowHash(false);
		pageTracker._setAllowLinker(true);
		pageTracker._trackPageview();
		sleuth();
		if (document.cookie.indexOf('BIZEMPLOYEE') != -1) {
			pageTracker._setVar('internal');	
		}
		var zall, zcookie, zsource, zmedium, zcampaign, zterm, zcontent, zag;
		
		if (location.search.match(/ga_campaign|ga_keyword|ga_adgroup/)) {
			q = storeQueryString();
			zsource = 'google';
			zmedium = 'cpc';
			zcampaign = q['ga_campaign'];
			zcontent = q['ga_adgroup'];
			zterm = q['ga_keyword'];
			zall = zsource + '-' + zmedium + '-' + zcampaign + '-' + zcontent + '-' +zterm;
			setCookie('__utmroi', zall,'','','bizactions.com', true);
			zall = 'zall=' + zall;
		} else if (document.cookie.indexOf('__utmz') !== -1 && (location.search.indexOf("utm") !== -1 || document.cookie.indexOf('__utmroi') === -1 )) {
			zcookie = readCookie('__utmz');	
			if (zcookie.indexOf("utmgclid=") !== -1) {
				zsource = 'google';
				zmedium = 'cpc';
			} else { 
				zcookie.indexOf("utmcsr=") !== -1 ? zsource = zcookie.match(/utmcsr=([^|;]*)/)[1] : zsource = 'none';
				zcookie.indexOf("utmcmd=") !== -1 ? zmedium = zcookie.match(/utmcmd=([^|;]*)/)[1] : zmedium = 'none';
			}
			zcookie.indexOf("utmccn=") !== -1 ? zcampaign = zcookie.match(/utmccn=([^|;]*)/)[1]: zcampaign = 'none';
			zcookie.indexOf("utmctr=") !== -1 ? zterm = zcookie.match(/utmctr=([^|;]*)/)[1] : zterm = 'none';
			zcookie.indexOf("utmcct=") !== -1 ? zcontent = zcookie.match(/utmcct=([^|;]*)/)[1] : zcontent = 'none';
			zall = zsource + '-' + zmedium + '-' + zcampaign + '-' + zcontent + '-' + zterm;
			setCookie('__utmroi', zall,'','','bizactions.com','');
			zall = 'zall=' + zall;
		} else if(document.cookie.indexOf('__utmroi') !== -1) {
			zall = 'zall=' + readCookie('__utmroi');
		} else{
			zall = "zall=not set";
		}
		
		nsLink("iframe", "src", zall);
		nsLink("a", "href", zall);
		
	} else {
        setTimeout(function () { 
		    runGA(); 
		}, 100);
    }
}

document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
runGA();
