// Track our visitors through the site. Helps us know where someone came from and how well our service works.

// NB - include the "cookies.js" library.
TrackerCookieName = 'CCS-UniqueID';

function Num(x) {
	if (x < 10) {
		return '0' + String(x);
	} else {
		return String(x);
	}
}

function SetNewTrackerCookie() {
	Today = new Date();
	// Format of ID will be YYYYMMDDHHMMSS.4-Digit-RandomNumber
	// 25 characters
	
	// Sept 02 - new format = YYYYMMDDHHSSMMXXj
	cookie = Num(Today.getFullYear()) + Num(Today.getMonth()+1) + Num(Today.getDate()) + Num(Today.getHours()) + Num(Today.getMinutes()) + Num(Today.getSeconds());
	cookie = cookie + Num(Math.round(Math.random() * 99));

	SetOurCookie(cookie);
}

function SetOurCookie (cookie) {
	// Root of this website
	if (document.URL.indexOf('chichester-counselling-services.com') > -1) {
		domain = '.chichester-counselling-services.com';
	} else if (document.URL.indexOf('ccs-counselling.com') > -1) {
		domain = '.ccs-counselling.com';
	} else {
		domain = null;
	}
	//window.status='Domain is: ' + domain;
	//window.status="Setting cookie to " + cookie +", Domain to "+domain;
	
	setCookie(TrackerCookieName, cookie, new Date('July 1, 2099 23:00:00'), '/', domain);

}

function scanForCookie() {
	// Is there a cookie in the URL?
	parambegin = document.URL.indexOf('?');
	cookie = '';
	if (parambegin != -1){
		cookie = document.URL.substring(parambegin+1, document.URL.length);
		if (cookie.substr(0,3) == '200') {
			SetOurCookie(cookie);
		} else {
			//cookie = null;
			cookie = getCookie(TrackerCookieName);
		}
	} else {
		cookie = getCookie(TrackerCookieName);
	}
	return cookie;
}

function TrackVisitor() {
	TrackCook = scanForCookie();
	if (TrackCook == null) {
		SetNewTrackerCookie();
	}
}

function ResetCookie() {
	SetTrackerCookie();
}
function UniqueIDTest () {
	x = getCookie(TrackerCookieName);
	document.write("Cookie name is " + TrackerCookieName + "<br>UniqueID = " + x);
}
function DeleteUniqueIDCookie() {
	delCookie(TrackerCookieName);
}

TrackVisitor();
