/*
 * [moodoo.js] - Tracking users on ParlyWeb
 *
 * Site usage tracking in *aggregate* to compensate for the IP address
 * mapping/proxying situation.
 *
 * See http://www.parliament.vic.gov.au/privacy.html to see the privacy
 * statement
 *
 * by Stephen Rosman [ webmaster@parliament.vic.gov.au |  +613 9651 8490 ]
 *
 * Version: 1.1 (21/10/2003)
 * - Redirection component modified for proper domain names
 *
 * Version: 1.0
 * - Initial version
 */

/************************* COOKIE ABSTRACTION *************************/
// Settings
PWCookieJarDomain = "parliament.vic.gov.au";
PWCookieJarArmageddon = new Date("January 19, 2038");
// Decided to make PWSession the life of the browser rather than
// a 15 minute lifespan - since the log analyser can do that anyway.
//PWCookieJarSessionTimeout = 15*60; // 15 minutes

// Object: PWCookie
var PWCookieRandValChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
// This is essentially encoding a random 64 bit number per character.
function PWCookieRandVal (length) {
	if (!length) { length = 8; }
	var value = "";
	for (var i=0; i<length; i++) {
		value += PWCookieRandValChars.charAt(Math.floor(Math.random() * PWCookieRandValChars.length));
	}
	this.value = value;
}
function PWCookieRemove () {
	// This won't remove a cookie with the same name but for a differently
	// specified domain. (Or Path?)  I suppose we could step up or down the
	// domain name/path of the page and set each one to null but that's a bit
	// extreme
	document.cookie = escape(this.name) + "=";
}
function PWCookieSave () {
		var cookie = 
			escape(this.name) + "=" +
			escape(this.value) + "; " +
			"Domain=" + PWCookieJarDomain + "; " +
			"Path=/"
		;
		if (this.expires) {
			cookie += "; Expires=" + this.expires;
		}
		if (this.maxage) {
			// cookie += "; Max-Age=" + this.maxage;
			// I would be nice if we could just set "Max-Age" for this cookie and
			// let the browser do it's thing but IE doesn't respect the max age
			// cookie property when set via jscript.
			var now = new Date();
			var expires = new Date(now.getTime() + (this.maxage * 1000));
			cookie += "; Expires=" + expires.toUTCString()
		}
		document.cookie = cookie;
		if (window.debug) { alert(cookie); }
}
function PWCookie (name, value) {
	this.name = name;
	if (value) {
		this.value = value;
	} else {
		this.randVal();
	}
}
// Wire up object
PWCookie.prototype.save = PWCookieSave;
PWCookie.prototype.remove = PWCookieRemove;
PWCookie.prototype.randVal = PWCookieRandVal;


// Object: CookieJar
function PWCookieJarDebug () {
	var info = "";
	info += "Rawcookie: "+this.rawcookie+"\n";
	for (var i=0; i<this.allCookies.length; i++) {
		info += "Cookie["+i+"]: name="+this.allCookies[i].name+" value="+this.allCookies[i].value+"\n";
	}
	return info;
}
function PWCookieJarAddCookie (cookie) {
	// Won't add the cookie if it already exists for this page.
	// We can't know the domain restriction of the original cookie
	// or it's expiry date so we can't just write cookies to the
	// browser willy-nilly since we could be changing someone elses
	// application.
	//
	// Since we want to store the cookies in two different ways - in
	// an array so we can itterate throught them and by name - we
	// need to link the cookie to the index number (ord) so we can
	// change both if we need to.
	if (eval("this.cookies."+cookie.name)) {
		var ord = eval("this.cookies."+cookie.name+".ord");
		eval("this.cookies."+cookie.name+"=cookie;");
		this.allCookies[ord] = cookie;
	} else {
		var ord = this.allCookies.length;
		this.allCookies[ord] = cookie;
		cookie.ord = ord;
		eval("this.cookies."+cookie.name+"=cookie;");
	}
}
function PWCookieJar (cookie) {
	this.cookies = new Object();		// Cookies accessable by name
	this.allCookies = new Array();		// All cookies for itteration

	// Cookie format is specified in RFC 2109? and a couple of other
	// places.
	this.rawcookie = cookie || "";
	var allcookiestrings = this.rawcookie.split("; ");
	for (var i=0; i<allcookiestrings.length; i++) {
		// separate name from value and add cookie object to ways of accessing
		// cookie info.  This wouldn't work if you already had a cookie jar
		// since it would create new cookies by index.
		var cs = allcookiestrings[i];
		var eq = cs.indexOf('=');
		var name = cs.substring(0, eq);
		var value = unescape(cs.substring(eq+1, cs.length));
		if (name && value) {
			var c = new PWCookie(name, value);
			c.ord = i;
			this.allCookies[this.allCookies.length] = c
			eval("this.cookies."+name+"=c");
		}
	}
}
// Wire up PWCookieJar
PWCookieJar.prototype.debug = PWCookieJarDebug;
PWCookieJar.prototype.addCookie = PWCookieJarAddCookie;



/************************* Moodoo *************************/
function PWMoodooInit () {
	if (window.debug) { alert('Initialising Moodoo...'); } 

	// Need to force users to real domain name so that cookie tracking works.
	// If we can't change users homepages to point to 'intranet.parlynet.vic.gov.au'
	// instead of '192.168.1.1' we'll have to redirect them once they get there.
	if (location.host) {
		if (
			location.hostname == "192.168.1.1" ||
			location.hostname.toLowerCase() == "intranet" ||
			location.hostname.toLowerCase() == "intranet.parlynet.vic.gov.au"
		) {
			location.hostname = "intranet.parliament.vic.gov.au";
		}
/* else if (
			location.hostname == "192.168.1.163"
		) {
			location.hostname == "library.parlynet.vic.gov.au":
		}
*/
	}


	window.PWCookieJar = new PWCookieJar(document.cookie);

	// Add tracking cookies if they don't already exist

	// Track users
	if (!window.PWCookieJar.cookies.PWBrowser) {
		if (window.debug) { alert('Creating Browser Cookie...'); }
		var browserCookie = new PWCookie("PWBrowser");
		browserCookie.expires = PWCookieJarArmageddon.toUTCString();
		window.PWCookieJar.addCookie(browserCookie);
		browserCookie.save();
	}
	// Track sessions
	if (!window.PWCookieJar.cookies.PWSession) {
		if (window.debug) { alert('Creating Session Cookie...'); }
		var sessionCookie = new PWCookie("PWSession");
		//sessionCookie.maxage = PWCookieJarSessionTimeout;
		window.PWCookieJar.addCookie(sessionCookie);
		sessionCookie.save();
	}
	/* else {
		if (window.debug) { alert('Touching Session Cookie...'); }
		with (window.PWCookieJar.cookies) {
			PWSession.maxage = PWCookieJarSessionTimeout;
			PWSession.save();
		}
	}*/
	// Track screen dimensions
	// We don't check for the existance of a cookie before creating
	// it since the overhead is probably higher than just creating it.
	if (screen && screen.width && screen.height) {
		if (window.debug) { alert('Creating Screen Cookie...'); }
		var screenCookie = new PWCookie("PWScreen", screen.width + "x" + screen.height);
		window.PWCookieJar.addCookie(screenCookie);
		screenCookie.save();
	}
}
PWMoodooInit();