// 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 = 'LP-UniqueID';
TrackerSessionCookieName = 'LP-UniqueSesh';

Main();

//----------------------------------
function Main() {
	PrepareTrackingCookies();
	SendbackPageArriving();
}


//----------------------------------
function PrepareTrackingCookies() {
	TrackCook = scanForCookie();
	if (TrackCook == null) {
		SetNewTrackerCookie();
	}
	
	TrackCook = scanForSessionCookie();
	//window.status="Sesh: "+TrackCook;
	if (TrackCook == null) {
		SetNewTrackerSessionCookie();
	}
}

function SendbackPageArriving() {
	document.write('<div style="position:absolute; width:2px;height:2px;">')
	title = escape(document.title);
	refer = escape(document.referrer);
	URL = escape(location.href); 	// document.URL
	UniqueID = getCookie(TrackerCookieName);
	
	base = "<img alt='Loanspage.co.uk for fast loans!' src='/perl/DreamsOfFreedom/page-track.pl";
	
	if (title != "") {
		base = base + "?PageTitle=" + title;
	} else {
		base = base + "?PageTitle=-unknown-";
	}
	if (UniqueID) {
		base = base + "&UniqueID=" + UniqueID;
	}
	if (refer != "") {
		base = base + "&Referrer=" + refer;
	}
	if (URL != "") {
		base = base + "&URL=" + URL;
	}
	
	base = base + "' width='2' height='2'>"
	
	document.write(base);
	
	document.write('</div>');
}

//----------------------------------

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)) + 'j';

	SetOurCookie(cookie);
}

function SetNewTrackerSessionCookie() {
	Today = new Date();
	
	// Add 10 years to the cookie...
	Tomorrow = Today.valueOf() + (1000 * 60 * 60 * 24 * 365 * 10);
	Tomorrow = new Date(Tomorrow);
	// Format of ID will be YYYYMMDDHHMMSS.4-Digit-RandomNumber
	// 25 characters
	
	// Sept 02 - new format = YYYYMMDDHHSSMMXXj
	cookie = "99" + Num(Today.getFullYear()-2000) + Num(Today.getMonth()+1) + Num(Today.getDate()) + Num(Today.getHours()) + Num(Today.getMinutes()) + Num(Today.getSeconds());
	cookie = cookie + Num(Math.round(Math.random() * 99));

	setCookie(TrackerSessionCookieName, cookie, Tomorrow, '/');
}

function parseUrl1(data) {
    var e=/((http|https|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?/;

    if (data.match(e)) {
        return  {url: RegExp['$&'],
                protocol: RegExp.$2,
                host:RegExp.$3,
                path:RegExp.$4,
                file:RegExp.$6,
                hash:RegExp.$7};
    }
    else {
        return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
    }
}

function SetOurCookie (cookie) {
	var expire = new Date('July 1, 2099 23:00:00');
	var Domain = null;
	
	// Root of this website
	if (document.URL.indexOf('loanspage.co.uk') > -1)
		Domain = "loanspage.co.uk";
	else if (document.URL.indexOf('loanspage.com') > -1)
		Domain = "loanspage.com";
	else if (document.URL.indexOf('loanspage.net') > -1)
		Domain = "loanspage.net";
	else if (document.URL.indexOf('loanspage.biz') > -1)
		Domain = "loanspage.biz";
	else if (document.URL.indexOf('loanspage.tv') > -1)
		Domain = "loanspage.tv";
	else {
		var Data = parseUrl1(document.URL);
		Domain = Data.host;
	}
		
	setCookie(TrackerCookieName, cookie, new Date('July 1, 2099 23:00:00'), '/', ((Domain)?"."+Domain:null));	
}

function scanForCookie() {
	// Is there a tracking number in the URL?
	// http://www.loanspage.co.uk?2002122716473329a&otherparam=x&othereparam2=y
	// The tracking number is the 2002122716473329a value ... so extract this....
	
	var parambegin = document.URL.indexOf('?');
	var cookie = '';
	if (parambegin != -1){
		var paramend = document.URL.indexOf("&");
		if (paramend == -1)
			paramend = document.URL.length;
			
		cookie = document.URL.substring(parambegin+1, paramend);
		
		//window.status = cookie;
		
		if (cookie.substr(0,3) == '200') {
			SetOurCookie(cookie);
		} else {
			//cookie = null;
			cookie = getCookie(TrackerCookieName);
		}
	} else {
		cookie = getCookie(TrackerCookieName);
	}
	return cookie;
}

function scanForSessionCookie() {
	// Is there a cookie in the URL?
	cookie = getCookie(TrackerSessionCookieName);
	return cookie;
}


function ResetCookie() {
	SetTrackerCookie();
}
function UniqueIDTest () {
	x = getCookie(TrackerCookieName);
	document.write("Cookie name is " + TrackerCookieName + "<br>UniqueID = " + x);
}
function DeleteUniqueIDCookie() {
	delCookie(TrackerCookieName);
}


