/*
$Revision: 10 $
$Workfile: hr_site-tracker.js $
$Author: Bert $
$Modtime: 19 10 04 17:24 $ 
*/
var CookieName	= "HRsite";
var CookieQPName  = "hrmigvis";
var CookieExpiry  = new Date;
var CookiePath	= "/";
var CookieTimeout = 365 * 24 * 60 * 60 * 1000;
var DomainList	= new Array(".co.uk",".com", ".net"); 

//---------------------------------------------------------------------------------------------
// Get the domain
//---------------------------------------------------------------------------------------------
function GetDomain() {
	var DomainValue = null;
	var firstDot;
	var secondDot;
	var lastDot;
	var useHostname=document.location.hostname;

	if (useHostname != null) {
		var arIndex;
		for (arIndex = 0; (arIndex < DomainList.length) && (DomainValue == null); arIndex++) {
			var tldIndex = useHostname.lastIndexOf(DomainList[arIndex]);
			if (tldIndex > 0) {
				var nextDot = useHostname.lastIndexOf('.',tldIndex-1);
				if (nextDot >= 0) {
					DomainValue = useHostname.substring(nextDot);
				}
				else {
					DomainValue = "." + useHostname;
				}
			}
		}
	}

	return DomainValue;
}
//---------------------------------------------------------------------------------------------
// Base64 Style encoding
//---------------------------------------------------------------------------------------------
function EncodeString( Input ) {
	var TransChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	var Output	 = "";
	
	for (var i = 0; i < Input.length; i += 3) {
		var NumBytesLeft = Input.length - i;
		var Value		= 0;
		
		Value  = (Input.charCodeAt(i) << 16) & 0x00ff0000;
		Value |= (NumBytesLeft > 1)? (Input.charCodeAt(i + 1) << 8) & 0x0000ff00 : 0;
		Value |= (NumBytesLeft > 2)? Input.charCodeAt(i + 2) & 0x000000ff : 0;
	
		Output += TransChars.charAt((Value & 0x00fC0000) >> 18);
		Output += TransChars.charAt((Value & 0x0003f000) >> 12);
		Output += (NumBytesLeft > 1)? TransChars.charAt((Value & 0x00000fc0) >> 6) : '_';
		Output += (NumBytesLeft > 2)? TransChars.charAt((Value & 0x0000003f)) : '_';
	}
	
	return Output;
}

//---------------------------------------------------------------------------------------------
// Encode all the relevent detals into a string
//---------------------------------------------------------------------------------------------
function EncodeDetails( Format, TRIPSParameters, CookieValue ) {
	var Output = "";

	for (var i = 0; i < Format.length; i++) {

		var Data;
		
		switch (Format.charAt(i)) {

			case 'r': // referrer
					Data = document.referrer;
					break;
	
			case 'p': // Fake URL
					Data = TRIPSParameters;
					break;
	
			case 'd': // browser data
					Data = screen.availWidth + "x" + screen.availHeight + "x" + screen.colorDepth + "." + navigator.javaEnabled();
					if (navigator.plugins) {
						Data += "." + navigator.plugins.length;
					}
					break;
	
			case 'c': // cookie id
					Data = CookieValue;
					break;
	
			case 'u': // derived identifier
					Data = window.history.length + "." + (Math.random() * 1000) + "." + (new Date()).getTime();
					break;
					
			case 't': // timestamp to prevent caching of these requests
					Data = "t"+(new Date()).getTime();
					break;
		}
		Output += EncodeString(Data) + "*";
	}

	return Output;
}

//---------------------------------------------------------------------------------------------
// Set the document cookie
//---------------------------------------------------------------------------------------------
function SetCookie( Name, Value, Expiry, Path, Domain ) {
	var CookieDetails = Name + "=" + Value + "; expires=\"" + Expiry.toGMTString() + "\" " + 
			   ((Path) ? "; path=" + Path : "") + ((Domain) ? "; domain=" + Domain : "");
	
	document.cookie = CookieDetails;
}

//---------------------------------------------------------------------------------------------
// Obtain the value of the document cookie
//---------------------------------------------------------------------------------------------
function GetCookie(Name,DataSource,DataEndChar) {
	var Prefix = Name+"=";
	var Value  = null;
	var Begin  = DataSource.indexOf(Prefix);
	if ((Begin != -1) && (Name.length > 0)) {
		var End = DataSource.indexOf(DataEndChar,Begin);
		if (End == -1) End = DataSource.length;
		Value = DataSource.substring(Begin+Prefix.length,End);
	} 
	return Value;
}

//---------------------------------------------------------------------------------------------
// Send the details back to the server
//---------------------------------------------------------------------------------------------
function SendDetails( Prefix, TRIPSParameters, Format, CookieValue ) {

	var UsePageUrl = Prefix + "?" + TRIPSParameters;
	var Tracker = new Image();
	Tracker.src = Prefix + "?f=" + Format + "&d=" + EncodeDetails(Format, UsePageUrl, CookieValue);
	
}

//---------------------------------------------------------------------------------------------
// Submit a tracker request
//---------------------------------------------------------------------------------------------
function SubmitTracker(trackerUrl, TRIPSParameters) {
	
	// First, see if we passed a "transferred" cookie from a previous domain
	// this will override any existing cookie on this domain
	var PassedCookieValue=GetCookie(CookieQPName,document.URL,"&");
	
	var CookieExpiry = new Date;
	var CookieDomain = GetDomain();
		
	// Define the expiry
	CookieExpiry.setTime(CookieExpiry.getTime() + CookieTimeout);
	
	if (PassedCookieValue != null) {
		// Set the cookie using the passed value
		SetCookie(CookieName, PassedCookieValue, CookieExpiry, CookiePath, CookieDomain); 
	} else {
		var CookieValue  = GetCookie(CookieName,document.cookie,";");
		// If there was no cookie already - set using a new value
		if (CookieValue == null) {
			SetCookie(CookieName, EncodeDetails("u"), CookieExpiry, CookiePath, CookieDomain); 
		}
	}

	// reobtain the current cookie value - to make sure cookies are valid
	// this will either be (a) an existing cookie value (b) the transferred one or (c) a new one
	CookieValue = GetCookie(CookieName,document.cookie,";");
	
	// Act on whether the cookie was set
	if (CookieValue == null) {
		// No cookie / or failed to set one
		SendDetails(trackerUrl, TRIPSParameters, "pdrt"); // referrer, browser data, HR data, timestamp
	} else {
		// Send the cookie details
		SendDetails(trackerUrl, TRIPSParameters, "pcrt", CookieValue); // referrer, cookie identifier, HR data, timestamp
	}
}
