/* ====
This custom Javascript logs campaign variables for subsequent visitors
Made by: Gjalt Jellesma, contains parts of Brian Clifton's first-and-last-referrer script
=== */


function firstTime() {
           newVisitor = 0;
	   var myCookie = " " + document.cookie + ";";
	   var searchName = "__utmb=";
	   var startOfCookie = myCookie.indexOf(searchName)
           if (startOfCookie <= 1) {   // i.e. first time visitor
		  newVisitor = 1;
	   }
	}

function trackVisit() {
	var cookie_name = "__utmg";
	if(!cookieExist(cookie_name)) {
		setCookie(cookie_name, generateId(), '90');
	}

	var myCookie = getCookie(cookie_name);
	var today = new Date();

var dateString = today.getDate() + "-" + (1 + today.getMonth()) + "-" + today.getFullYear() + " "
+ today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();

	var info = myCookie + "|" + getVars() + "|" + dateString;

	if(newVisitor)
		pageTracker._trackPageview("/crossvisittracking/?qid=" + info);
}



// Check if cookie exists
function cookieExist(cookieName) {
	var exist = false;
	var tmpCookie = "" + document.cookie;
	
	if(tmpCookie.indexOf(cookieName) > -1)
		exist = true;

	return exist;
}

// Set cookie
function setCookie(cookieName, cookieValue, nDays) {
	var today = new Date();
	var expire = new Date();
	if(nDays == null || nDays == 0) nDays = 1;
	expire.setTime(today.getTime() + 3600000 * 24 * nDays);
	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
}

// Read cookie
function getCookie(cookieName) {
	var my_cookie = ""+document.cookie;
	var ind = my_cookie.indexOf(cookieName);
	if (ind == -1 || cookieName == "") 
		return ""; 
	var ind1 = my_cookie.indexOf(';', ind);
	if (ind1 == -1) 
		ind1 = my_cookie.length; 

	return unescape(my_cookie.substring(ind + cookieName.length + 1, ind1));
}

// Generate visitor ID
function generateId() {
	var randomnumber = Math.floor(Math.random()*1000);
	var currentTime = new Date();
	var month = currentTime.getMonth()+ 1
	var timeStamp = currentTime.getFullYear() + month + currentTime.getDate() + "-" + currentTime.getHours() + currentTime.getSeconds() + "-" + randomnumber;
	return(timeStamp);
}

function _uGC(l, n, s) {
	// used to obtain a value form a string of key=value pairs
	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;
}

function getVars() {
	var z = _uGC(document.cookie, "__utmz=", ";");
	
	urchin_source = _uGC(z,"utmcsr=", "|");
	urchin_medium = _uGC(z,"utmcmd=", "|");
	urchin_term = _uGC(z,"utmctr=", "|");
	urchin_content = _uGC(z,"utmcct=", "|");
	urchin_campaign = _uGC(z,"utmccn=", "|");
	var gclid = _uGC(z,"utmgclid=","|");
	if(gclid) {
		urchin_source = "google";
		urchin_medium = "cpc";
	}

	var data = urchin_source;
	data += "|" + urchin_medium;
	data += "|" + urchin_term;
	data += "|" + urchin_content;
	data += "|" + urchin_campaign;

	return data;
}