// $Id: base.js 602 2009-01-20 10:13:09Z accult $

/* Imported from www.stat.ubc.ca */

/**
 * Global object that we use to hold all data used by javascript for this website
 */
var stat = new Object();

/**
 * Version of this stat object.
 * If version numbers do not match, then cookie is reset to
 * default values.
 */
stat.ver = 2;

/**
 * Switch for debugging
 */
stat.debug=false;
/* stat.debug=true; */

/**
 * The array of possible font sizes in font points
 */
stat.fontSizeOptions = new Array(5, 7, 10, 12, 14, 16, 18, 20, 22, 36);

/**
 * Set the default font size
 */
stat.defaultFontSizeIndex=2;
stat.fontSizeIndex=stat.defaultFontSizeIndex;

/**
 * Name of the cookie we use to store data
 */
stat.cookieName="stat";

/**
 * Days until the cookie for the saved settings will expire
 */
stat.cookieExpiresDays=365;

/**
 * List of (primitive) properties of stat object we want stored in cookie
 */
stat.storeList = new Array("ver", "fontSizeIndex");

/**
 * String used to split stored values in cookie
 */
stat.cookieCutter = ":::";

/**
 * Called when body tag opens
 * We don't use the usual onload event from the body tag
 * as we want to run this before body is rendered to stop
 * the page changing size before our eyes
 */
function doOnBodyOpen(){
	debug(doOnBodyOpen);
	loadCookie();
	setFontSize();
}

/**
 * Make the global font size one step larger.
 * stat.fontSizeOptions contains the possible font sizes (in font points)
 */
function doLarger() {
	debug(doLarger);

	if(stat.fontSizeIndex < stat.fontSizeOptions.length - 1) {
		stat.fontSizeIndex = parseInt(stat.fontSizeIndex) + 1;
		setFontSize();
	} else {
		alert("Sorry, cannot make font size any larger.");
	}
}

/**
 * Make the global font size one step smaller.
 * stat.fontSizeOptions contains the possible font sizes (in font points)
 */
function doSmaller(){
	debug(doSmaller);

	if(stat.fontSizeIndex > 0) {
		stat.fontSizeIndex = parseInt(stat.fontSizeIndex) - 1;
		setFontSize();
	} else {
		alert("Sorry, cannot make font size any smaller.");
	}
}

/**
 * Make the global font size the default size.
 * stat.fontSizeOptions contains the possible font sizes (in font points)
 */
function doDefault(){
	debug(doDefault);

	stat.fontSizeIndex = stat.defaultFontSizeIndex;
	setFontSize();
}

/**
 * Update the font size after stat.fontSize has been changed
 * and save new size to the cookies
 */
function setFontSize() {
	debug(setFontSize);
	
	if(stat.fontSizeIndex < 0) {
		log(setFontSize, "fontSizeIndex too small, resetting");
		stat.fontSizeIndex = 0;
	} else if(stat.fontSizeIndex > stat.fontSizeOptions.length){
		log(setFontSize, "fontSizeIndex too large, resetting");
		stat.fontSizeIndex = stat.fontSizeOptions.length - 1;
	}

	document.body.style.fontSize = stat.fontSizeOptions[stat.fontSizeIndex] + "pt";
	saveCookie();
}

/**
 * Load data into stat from the cookie
 */
function loadCookie(){
	debug(loadCookie);
	if(navigator.cookieEnabled){
		if(document.cookie.length>0) {
			var c_start = document.cookie.indexOf(stat.cookieName + "=");
			if (c_start != -1) {
				c_start = c_start + stat.cookieName.length + 1;
				var c_end = document.cookie.indexOf(";", c_start);
				if (c_end == -1) {
					c_end = document.cookie.length;
				}
				decodeData(unescape(document.cookie.substring(c_start,c_end)));
			} else {
				log(loadCookie, "No current cookie\n(cookieName not found)");
			}
		} else {
			log(loadCookie, "No current cookie\n(no cookie in document object)");
		}
	} else {
		log(loadCookie, "Cookies diabled");
	}
}

/**
 * Save data into cookie from the stat
 */
function saveCookie(){
	debug(saveCookie);
	if(navigator.cookieEnabled){
		var exdate = new Date();
		exdate.setDate(stat.cookieExpiresDays);
		document.cookie = stat.cookieName + "=" + escape(encodeData()) + ";expires=" + exdate + ";path=/";
	} else {
		log(saveCookie, "Cookies diabled");
	}
}

/**
 * Encodes the data from stat object listed in stat.storeList to a string
 * ready for saving in cookie.
 */
function encodeData(){
	debug(encodeData);
	var output = "";
	for(i=0; i<stat.storeList.length; i++){
		output += stat.storeList[i] + stat.cookieCutter + stat[stat.storeList[i]];
		if(i<stat.storeList.length-1){
			output += stat.cookieCutter;
		}
	}
	log(encodeData, output);
	return output;
}

/**
 * Decodes the data from cookie to stat object.
 */
function decodeData(dataString){
	debug(decodeData);
	var exploded = dataString.split(stat.cookieCutter);
	var output = "";
	var foundVer=false;

	for(i=0; i<exploded.length; i+=2){
		if(exploded[i] == "ver"){
			if(exploded[i+1] != stat.ver){
				return;
			}
			foundVer = true;
			break;
		}
	}

	if(foundVer){
		for(i=0; i<exploded.length; i+=2){
			stat[exploded[i]] = exploded[i+1];
			output += exploded[i] + " = " + exploded[i+1] + "\n";
		}
		log(decodeData, output);
	} else {
		log(decodeData, "Didn't find cookie version - so didn't load data");
	}
}

/**
 * Call this when entering a function.
 * Set stat.debug=true to display debug info
 * @param fn function object that is being entered
 */
function debug(fn){
	if(stat.debug){
		var output = fn.name + "(";
		for(i=0; i<fn.arguments.length; i++){
			output += fn.arguments[i];
			if(i<fn.arguments.length-1){
				output += ", ";
			}
		}
		output += ")";
		alert(output);
	}
}

/**
 * Call this to log progress (displayed when stat.debug=true)
 * @param fn function object doing the logging
 * @param mesg progress message
 */
function log(fn, mesg){
	if(stat.debug){
		alert(fn.name + ":\n" + mesg);
	}
}

/**
 * For testing layout on different sized monitors, this fixes the width
 * of the body tag.
 * @param width width to fix
 */
function fixBody(width) {
	document.body.style.width=width;
}

/**
 * Called when QuickLink changes, loads page
 * @param select element of the calling QuicklLink form
 */
function doQuickLink(item){
	debug(doQuickLink);
	window.location = item.value;
}

/**
 * Asks user if they want to submit the form,
 * then add param to the action of the form.
 */
function formAskSubmit(formName, param) {
	ask = confirm('Submit form data?');
	if(ask == true) {
		f = document.forms[formName];
		f.action = f.action + param;
		f.submit();
	} else {
		window.location = window.location + param
	}
}

/**
 * Confirms user wants to create
 */
function formConfirm(message, formName) {
	ask = confirm(message);
	if(ask == true) {
		document.forms[formName].submit();
	}
}

/**
 * Hides the second argument, and shows the first
 */
function show(itemShow, itemHide) {
	document.getElementById(itemHide).style.display = 'none';
	document.getElementById(itemShow).style.display = 'block';
}

/**
 * First argument should be a checkbox, second something that can be disabled.
 * If the checkbox is checked then the second item is disabled.
 */
function checkdisable(checkbox, itemDisable) {
		document.getElementById(itemDisable).disabled = (document.getElementById(checkbox).checked == true);
}
