/*
	core javascript functions

	copyright 1999-2005 Robin Parmar (except as noted)
	use at will but retain this note
	licensed under GPL
*/


// ============================
// core DHTML object  
// returns the named object with useful properties
function getObj(name) {
	// parameters
	this.name		= name;

	// properties
	this.obj		= null;
	this.style		= null;

	// methods
	this.findPosX	= findPosXObj;
	this.findPosY	= findPosYObj;
	this.write		= writeObj;
	this.show		= showObj;
	this.hide		= hideObj;
	this.block		= blockObj;
	this.none		= noneObj;

	// initialise
	if (document.getElementById) {			 		// nice proper DOM level 1
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	} else if (document.all) {						// DOM level 0 like IE 5
		this.obj = document.all[name];
		this.style = document.all[name].style;
	} else if (document.layers) {					// crap, it's Netscape 4
		this.obj = getNN4Obj(document, name);
		this.style = this.obj;
	}
}

// show object
function showObj() {
	this.style.visibility = 'visible';
}

// hide object
function hideObj() {
	this.style.visibility = 'hidden';
}

// set display to none
function noneObj() {
	this.style.display = 'none';
}

// set display to block 
function blockObj() {
	this.style.display = 'block';
}

// write html to layer
function writeObj(text) {
	if (document.layers) {
		text = '<p>' + text + '</p>';
		this.obj.document.open();
		this.obj.document.write(text);
		this.obj.document.close();
	} else {
		this.obj.innerHTML = text;
	}
}

// recursive function used by above for Netscape 4
function getNN4Obj(obj, name) {
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++) {
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getNN4Obj(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

// determines x position of object
function findPosXObj() {
	var x = 0;
	var obj = this.obj;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			x += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		x += obj.x;
	}
	return x;
}

// determines y position of object
function findPosYObj() {
	var y = 0;
	var obj = this.obj;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			y += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		y += obj.y;
	}
	return y;
}


// ============================
// DHTML 

var DHTML = (document.getElementById || document.all || document.layers);

// adds a page onload event to any that may already exist:
//     addLoadEvent(nameOfFunction);
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		};
	}
}


// ============================
// miscellaneous

// returns a random number between 1 and x
function getRandom(x) {
     y = Math.round((x * Math.random()) + .5);
     return y;
}

// jump to previous file in history
function previousFile(jump){
	if (self!=null && !self.closed) {
		if (!jump)
			jump=1
		self.history.go(-1 * jump);
	}
}


// ============================
// date ticker
// derived from www.dynamicdrive.com

// return string representing current date and time
function getDate(){
	var dayarray=new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

	var montharray=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

	var x = new Date()
	var y = x.getYear()
	var d = x.getDay()
	var m = x.getMonth()
	var h = x.getHours()
	var n = x.getMinutes()
	var s = x.getSeconds()
	var dayofm = x.getDate()
	var nooner = "AM"

	if (y < 1000) y += 1900
	if (dayofm<10) dayofm = "0" + dayofm
	if (h>=12) nooner = "PM"
	if (h>12) h = h - 12
	if (h==0) h = 12
	if (n<=9) n = "0" + n
	if (s<=9) s = "0" + s

	var text = dayarray[d] + ", " + montharray[m] + " " + dayofm+ ", " + y + " " + h + ":" + m + ":" + s + " " + nooner

	return text;
}

// write current date to specified layer
function displayDate(layer) {
	if (DHTML)
		writeLayer(layer, getDate());
}

// every second, update current date to specified layer
function refreshDate(layer) {
	if (DHTML)
		setInterval("displayDate(layer)", 1000);
}


// ============================
// cookies

function setCookie(ourCookie, ourValue) {
	// escape value
	ourValue = escape(ourValue);

	// good indefinitely
	var ourDate = new Date("December 31, 2023");
	ourDate = "expires=" + ourDate.toGMTString() + ";";

	// any path on this domain
	var ourPath = "path=/;";

	// set cookie
	document.cookie = ourCookie + "=" + ourValue + ";" + ourDate + ourPath;
}

function killCookie(ourCookie) {
	// get value
	var ourValue = getCookie(ourCookie);

	if(ourValue) {
		// no good any more
		var ourDate = new Date("December 31, 1970");
		ourDate = "expires=" + ourDate.toGMTString() + ";";

		// any path on this domain
		var ourPath = "path=/;";

		// set cookie
		document.cookie = ourCookie + "=" + ourValue + ";" + ourDate + ourPath;
	}
}

function getCookie(ourCookie) {
	var bigCookie, firstChar, lastChar, retValue;

	// get entire cookie string (bunch of name=value pairs)
	bigCookie = document.cookie;

	// find start of "name"
	firstChar = bigCookie.indexOf(ourCookie);

	// initialize return value
	retValue = false;

	// if found 
	if(firstChar != -1) {
		// skip to after "="
		firstChar += ourCookie.length + 1;

		// find end of "value"
		lastChar = bigCookie.indexOf(";", firstChar);

		// last cookie case
		if(lastChar == -1) {
			lastChar = bigCookie.length;
		}

		// extract "value"
		retValue = bigCookie.substring(firstChar, lastChar);
		retValue = unescape(retValue);
	}
	return retValue;
}

