var gaCookies = {
source : '',
medium  : '', 
term    : '', 
content  : '', 
campaign  : '', 
campaignsegment  : '', 
numvisits : 0,
_uGC : function (l,n,s) {
	if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
	var i,i2,i3,c="-";
	i=l.indexOf(n);
	i3=n.indexOf("=")+1;
	if (i > -1) {
	i2=l.indexOf(s,i); 
	if (i2 < 0) { i2=l.length; }
	c=l.substring((i+i3),i2);
	}
	return c;
},
populateGAFields : function(z){
	this.source  = this._uGC(z, 'utmcsr=', '|');
	this.medium  = this._uGC(z, 'utmcmd=', '|');
	this.term    = this._uGC(z, 'utmctr=', '|');
	this.content = this._uGC(z, 'utmcct=', '|');
	this.campaign = this._uGC(z, 'utmccn=', '|');	
	
	var gclid = this._uGC(z, 'utmgclid=', '|');
	if (gclid !="-") {
	    this.source = 'google';
	    this.medium = 'cpc';
	}
	var csegment = this._uGC(document.cookie, '__utmv=', ';');
	if (csegment != '-') {
    var csegmentex = /[1-9]*?\.(.*)/;
    csegment    = csegment.match(csegmentex);
    csegment    = csegment[1];
	} else {
	  csegment = '(not set)';
	}
	this.campaignsegment = csegment;
	var a = this._uGC(document.cookie, '__utma=', ';');
	var aParts = a.split(".");
	this.numvisits = aParts[5];	
}
};

gaCookies.populateGAFields (gaCookies._uGC(document.cookie, '__utmz=', ';'));

var frmLeadsGeneratorURL = "//s3.amazonaws.com/ki.js/9914/1IW.js";
var _kiq = _kiq || [];
var dispLeadsgenerator = 0;

if (gaCookies.campaign === '(direct)')
{		
	dispLeadsgenerator = 0;
	_kiq.push(function(){
	var surveys = [19170,19170]; 
	var ab = new $KI.Cookie('ab_test_template', surveys[Math.floor(Math.random() * surveys.length)]);
	if(ab) { KI.show_by_id('survey', ab.get());} });	
}
else if (gaCookies.campaign === '(organic)')
{		
	if (gaCookies.term.match(/varumarket/)) 	   
	{ 
		dispLeadsgenerator = 0;
		_kiq.push(function(){
		var surveys = [19170,19170]; 
		var ab = new $KI.Cookie('ab_test_varumarke', surveys[Math.floor(Math.random() * surveys.length)]);
		if(ab) { KI.show_by_id('survey', ab.get());} });
 	} 
  else if (gaCookies.term.match(/behovssoken/)) 	          
	{ 		
		dispLeadsgenerator = 0;	
		_kiq.push(function(){
		var surveys = [19170,19170]; 
		var ab = new $KI.Cookie('ab_test_behov', surveys[Math.floor(Math.random() * surveys.length)]);
		if(ab) { KI.show_by_id('survey', ab.get());  } });
 	} 
}  
if (dispLeadsgenerator === 1)
{
	document.write(unescape("%3Cscript src='" + frmLeadsGeneratorURL + "' async='true' type='text/javascript'%3E%3C/script%3E"));
}
