// User Tracking Module
// Matt Worrall, The OutRight Company UK Limited

// define global attributes
// these may vary from site to site
var hSite = "coverdirect";
var hServ = "www.outrightplc.co.uk";
var uID = "";
var hRefURL = "";
var strLog = "";
var maxlen = 2000;
var today = new Date();
var zero_date = new Date(0,0,0);
today.setTime(today.getTime() - zero_date.getTime());
var tDate = new Date(today.getYear(),today.getMonth(),today.getDate(),0,0,0);
var eDate = new Date(tDate.getTime() + (30 * 86400000));
var isSecure = false;

function trackUser(hURL) {
	// hURL is only sent when we need to force a unique URL
	// ie if it is hidden by IPL etc.
	// if not, we can get it on the fly
	if (hURL == null) {
		hURL = escape(document.URL);
	}
	// use value of hURL to determine if need to connect via SSL
	if (hURL.substr(0,5) == "https") {
		isSecure = true;
	}
	// get referring URL
	hRefURL = escape(document.referrer);
	// check for cookie
	// ie they've been here before
	if (!getCookie("uID")) {
		setCookie("uID",uniqueID(),eDate,"/");
	}
	if (isSecure) { strLog = "https://"; } else { strLog = "http://"; }
	strLog += hServ;
	strLog += "/WebTracking/Tracker.aspx?hSite="+hSite+"&hURL="+hURL+"&hUserID="+getCookie("uID")+"&hRefURL="+hRefURL;
	var reqlen = strLog.length;
  	if (reqlen>maxlen) strLog = strLog.substring(0,maxlen);
	var tImg = new Image();
	tImg.src = strLog;
}

function getCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function setCookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function delCookie(name,path,domain) {
    if (getCookie(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function uniqueID() {
	var randnum = 0
	for (i=0;i<3;i++) {
		randnum = Math.round(Math.random()*9);
		uID += randnum.toString();
	}
	uID += today.getTime();
	return uID;
}

