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"));

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 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 noPercent(x)
{
	x = unescape(x);
	return x.replace(/\+/g," ").replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function sleuth()
{
		var sleuthTracker = _gat._getTracker('UA-1');
	if(document.location.search.indexOf("gclid")!=-1||document.location.search.indexOf("cpc")!=-1) {
		ref = document.referrer;
		re = /(\?|&)(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) {
			searchq[3] = noPercent(searchq[3]);
			sleuthTracker._setVar(searchq[3]);
		}
		else {
			sleuthTracker._setVar('Referral: ' + document.referrer);
		}
	}
}

function SuperCookie() {
    this.source = " ";
    this.medium = " ";
    this.campaign = " ";
    this.keyword = " ";
    this.timestamp = " ";
    this.adgroup = " ";
    this.custom = " ";

	this._getValue = function(){ return this.source + '|' + this.medium + '|' + this.campaign + '|' + this.keyword + '|' + this.timestamp + '|' + this.adgroup + '|' + this.custom;
	};

	this._setCookie = function(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 (!value) {
				value = this.source + '|' + this.medium + '|' + this.campaign + '|' + this.keyword + '|' + this.timestamp + '|' + this.adgroup + '|' + this.custom;
			}
			if(overwrite == true || !document.cookie.match('(^|;\\s)' + name)){
			document.cookie = name + "=" + value + ((expires) ? ";expires=" + expires_date.toGMTString() : "") + ((path) ? ";path=" + path: ";path=/") + ((domain) ? ";domain=" + domain: "");
			}
	};
	
	this._initData = function(type) {
		var cancel_campaign = false;
		var cancel_medium = false;
		var cancel_keyword = false;
		var cancel_source = false;
		var cancel_content = false;
		if (type && document.cookie.match('(^|;\\s)' + type) && type != '__utmz') {
			var c2 = readCookie(type);
			var ca2 = c2.split('|');
			this.medium = ca2[0];
			this.source = ca2[1];
			this.campaign = ca2[2];
			this.keyword = ca2[3];
			this.timestamp = ca2[4];
			this.adgroup = ca2[5];
			this.custom = ca2[6];
		}
		if (!type || type == 'both' || type == 'query') {
			var q = storeQueryString();
			if (q['utm_source']) {
				this.source = q['utm_source'];
				cancel_source = true;
			}
			if (q['utm_medium']) {
				this.medium = q['utm_medium'];
				cancel_medium = true;
			}
			if (q['utm_campaign']) {
				this.campaign = q['utm_campaign'];
				cancel_campaign = true;
			}
			if (q['utm_term']) {
				this.keyword = q['utm_term'];
				cancel_keyword = true;
			}
			if (q['utm_content']) {
				this.adgroup = q['utm_content'];
				cancel_content = true;
			}
			if (q['ga_campaign']) {
				this.campaign = q['ga_campaign'];
				cancel_campaign = true;
			}
			if (q['ga_keyword']) {
				this.keyword = q['ga_keyword'];
				cancel_keyword = true;
			}
			if (q['ga_ad_content']) {
				this.custom = q['ga_ad_content'];
			}
			if (q['ga_adgroup']) {
				this.adgroup = q['ga_adgroup'];
				cancel_content = true;
			}
		}
		if (!type || type == 'both' || type == '__utmz') {
			if (document.cookie.indexOf('__utmz') != -1) {
				var c2 = readCookie('__utmz'); //This gets the cookie
				var ca2 = c2.split('|'); //This splits the cookie into parts
				temp2 = ca2[0].substring(ca2[0].indexOf('utmcsr'));
				temp2 = temp2.substring(temp2.indexOf('utmgclid'));
				ca2[0] = temp2; // We then replace the item in the array with just the variable data									
				for (i = 0; i < ca2.length; i++) {
					temp3 = ca2[i]; //First, take each variable (ex. utmcsr=sourcename)
					temp4 = temp3.split('='); //Splits into an array, with temp4[0] = 'utmcsr, and temp4[1] = 'sourcename' using our above example
					if (temp4[0] == 'utmgclid') //Identifies the varaible and replaces appropriate items for Google Adwords Campaigns
					{
						if (!cancel_source) {
							this.source = 'google';
							cancel_source = true;
						}
						if (!cancel_medium) {
							this.medium = 'cpc';
							cancel_medium = true;
						}
						if (!cancel_campaign) {
							this.campaign = 'google';
							cancel_campaign = true;
						}
					}
					if (temp4[0] == 'utmcsr' && !cancel_source) {
						this.source = temp4[1];
					}
					if (temp4[0] == 'utmccn' && !cancel_campaign) {
						this.campaign = temp4[1];
					}
					if (temp4[0] == 'utmcmd' && !cancel_medium) {
						this.medium = temp4[1];
					}
					if (temp4[0] == 'utmctr' && !cancel_keyword) {
						this.keyword = temp4[1];
					}
					if (temp4[0] == 'utmcct' && !cancel_content) {
						this.adgroup = temp4[1];
					}
				}
			}
		}
	};

	this._setAll = function(src,med,camp,key,time,adgr,cust){
		var today = new Date();
		today.setTime(today.getTime()); 
		(src) ? this.source = src : this.source = " ";
		(med) ? this.medium = med : this.medium = " ";
		(camp) ? this.campaign = camp : this.campaign = " ";
		(key) ? this.keyword = key : this.keyword = " ";
		(time) ? this.timestamp = time : this.timestamp = today.getUTCFullYear() + '/' + (today.getUTCMonth() + 1) + '/' + today.getUTCDate();
		(adgr) ? this.adgroup = adgr : this.adgroup = " ";
		(cust) ? this.custom = cust : this.custom = " ";
	};

    this._setSource = function(x) {
        this.source = x;
    };
    this._setMedium = function(x) {
        this.medium = x;
    };
    this._setCampaign = function(x) {
        this.campaign = x;
    };
    this._setKeyword = function(x) {
        this.keyword = x;
    };
    this._setAdgroup = function(x) {
        this.adgroup = x;
    };
    this._setTimestamp = function(x) {
       if(x){ 
		this.timestamp = x;
	   }
	   else{
		var today = new Date();
		today.setTime(today.getTime()); 
	   	this.timestamp = today.getUTCFullYear() + '/' + (today.getUTCMonth() + 1) + '/' + today.getUTCDate();
	   }
    };
    this._setCustom = function(x) {
        this.custom = x;
    };
}
function runSC(){
	if (readCookie("__utmtrack") == null) {
		var sc = new SuperCookie();
      	sc._initData();
      	sc._setTimestamp();
      	sc._setCookie('__utmtrack','','','','e-zdogtraining.com','');
	}
	var page = document.location.pathname;
	 if (page == "/redirect_dtraining.php"){
		var sc = new SuperCookie();
      	sc._initData('__utmtrack');
      	scdata = sc._getValue();
		var url = window.location.toString();
		url.match(/\?(.+)$/);
  		var params = RegExp.$1;
  		var params = params.split("&");
  		var queryStringList = {};
  		for(var i=0;i<params.length;i++)
  		{
    		var tmp = params[i].split("=");
    		queryStringList[tmp[0]] = unescape(tmp[1]);
  		}
  		pageTracker._addTrans(queryStringList["orderId"],"",queryStringList["PayTotal_A"],"","","","","");
  		pageTracker._addItem(queryStringList["orderId"],"","","",queryStringList["PayTotal_A"],"1");
   		pageTracker._trackTrans();
	}
}
var scdata;
var pageTracker;
function runGA() {            
	if(typeof(_gat)=='object') {
		pageTracker = _gat._getTracker("UA-10518639-1");
		sleuth();		
		pageTracker._trackPageview();
		runSC();
	} else {
    	setTimeout("runGA();",100);
	}
} 
runGA();