var domainMatch = "cbb.dk";
var setDomain = ".cbb.dk";
var campaign = "skampagne1";

function getDomain(str){   
	if (str == null || str.length == 0)      
		return "";    
	str = str.replace("http://","").toLowerCase();    
	str = str.replace("https://","").toLowerCase();    
	var i = str.indexOf("/");   
	if (i > -1)      
		str = str.substring(0, i);    
	var parts = str.split('.');    
	var len = parts.length;    
	if (len < 3)      
		return str;    
	var lastPart = parts[len-1];   
	var secondPart;    
	secondPart = parts[len-2];    
	var two = 2;    
	if (lastPart == "uk" && secondPart == "co")      
		++two;    
	if (len >= 0)      
		return parts.splice(len-two, two).join('.');    
	return "";
}

function queryparam( name ){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null ) return "";  
	else return results[1];
}

function referrerqueryparam( name ){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( document.referrer );  
	if( results == null ) return "";  
	else return results[1];
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function GetSource ()
{
	return getDomain(document.referrer);
}

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{

  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path ) {
        cookie_string += "; path=" + escape ( path );
  }
  else {
	cookie_string += "; path=/";
  }

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}


function check_cookie (mon, svalue, prefix, blankval) {

	var current_date = new Date;
	var future_date = current_date;
	var yearadd = 0;
	var cookie_year ;
	var cookie_month;
	var cookie_day ;
	
	future_date.Month = current_date.getMonth() + mon;
	if (future_date.Month > 11) {
		yearadd = 1;
		
		future_date.Month = future_date.Month - 12;
	}
	
	cookie_year = future_date.getFullYear ( ) + yearadd;
	cookie_month = future_date.Month;
	cookie_day = future_date.getDate ( );
	if (cookie_day > 28) {
		cookie_day = 28;
	}
	
	
	if ( ! get_cookie ( prefix + "path" + mon) )
	{
		set_cookie ( "first" + prefix + mon, svalue,  cookie_year, cookie_month, cookie_day);
		set_cookie ( prefix + "path" + mon, "/BOL/" + svalue + "/EOL/",  cookie_year, cookie_month, cookie_day);
		set_cookie ( "last" + prefix + mon, svalue,  cookie_year, cookie_month, cookie_day);
	}
	else
	{
		var lastsource = get_cookie ( "last" + prefix + mon);
		if (currentsource != blankval)
			lastval = svalue;
		var sourcepath = get_cookie ( prefix + "path" + mon);
		set_cookie ( prefix + "path" + mon, sourcepath.replace("/EOL/","") + "/" + svalue + "/EOL/",  cookie_year, cookie_month, cookie_day);
		set_cookie ( "last" + prefix + mon, lastval,  cookie_year, cookie_month, cookie_day);

	}
	  
}


var orgSource = GetSource();
var refSource = orgSource;
var setSource = "false";



if (orgSource.indexOf(domainMatch ) < 0 ) //not internal
{
	if (window.location.href.indexOf("/info/") >= 0 ) //focus pages
	{
		setSource = "true";
	}
}




pageTracker._setAllowAnchor(true); 
pageTracker._setDomainName(setDomain);


if (setSource == "true") {

	var term = "";
	var source = "";
	var medium = "";
	var content = "";
	
	if (orgSource.indexOf("google") >= 0 ) {
		term = referrerqueryparam("q");
		source = "google";
		medium = "organic";
	}
	else if (orgSource.indexOf("bing") >= 0 ) {
		term = referrerqueryparam("q");
		source = "bing";
		medium = "organic";
	}
	else if (orgSource.indexOf("yahoo") >= 0 ) {
		term = referrerqueryparam("p");
		source = "yahoo";
		medium = "organic";
	}
	else {
		source = orgSource;
		medium = "referral";
		content = document.referrer;
	}

	// _setCustomVar(slot, name, value, scope {visitor, session, page })  	
	// Challenge: A new visit to these pages will override the term
	
	
	
	var hash = "utm_source=" + source + "&utm_medium=" + medium + "&utm_campaign=" + campaign + "&utm_term=" + term + "&utm_content=" + content;
	//Add parameters to URL
	window.location.hash = hash;
	
	if(pageTracker._setCustomVar) { 
		pageTracker._setCustomVar(5, "InfoVisitor", term, 1); 
	} 

}

try {
	pageTracker._trackPageview();
} catch(err) {}

if (! get_cookie ( "firstimpression" ) )
{
	var currentsource = GetSource();
	//check_cookie(1, currentsource, "source", "DIRECT");
	//check_cookie(6, currentsource, "source", "DIRECT");
	
	var term = "none";
	
	if (currentsource.indexOf("google") >= 0 ) {
		term = referrerqueryparam("q");
	}
	else if (currentsource.indexOf("bing") >= 0 ) {
		term = referrerqueryparam("q");
	}
	else if (currentsource.indexOf("yahoo") >= 0 ) {
		term = referrerqueryparam("p");
	}
	else {
		term = "none";
	}

	//check_cookie(1, term, "keyword", "none");
	check_cookie(6, term, "keyword", "none");
	set_cookie ( "firstimpression", "no" );
}
else { }

if (window.location.href == "https://www.cbb.dk/cbb?cmd=paymentcomplete&type=NP") {
	var sPath = get_cookie("keywordpath6");
	pageTracker._trackEvent("Keywords", "NP-KeywordPath", sPath);
}
else if (window.location.href == "https://www.cbb.dk/cbb?cmd=paymentcomplete&type=NN") {
	var sPath = get_cookie("keywordpath6");
	pageTracker._trackEvent("Keywords", "NN-KeywordPath", sPath);
}



	

