// load and restore page if the root page is not valid
function breakOut() {
	var rootURL = 'http://user.fundy.net/morris/';
	if (location.protocol == 'http:' && (self == top || !parent.IsFrameset)) {
		if (document.getElementsByTagName('body')) {
			document.getElementsByTagName('body')[0].style.display = 'none';
		}
		// self.location = rootURL + 'redirect.html?' + self.location.href.substring(location.href.lastIndexOf('/') + 1);
		location.replace(rootURL + 'redirect.html?' + self.location.href.substring(location.href.lastIndexOf('/') + 1));
	}
}

// function to execute when a page is loaded
function stdOnLoad() {
	removeLinkBox();
	preloadImages();
}

// remove link outline - removes link bounding boxes on <a> and <area> links in IE4+
function removeLinkBox() {
	if (document.all) {
		for (var i in document.links) {
			document.links[i].onfocus = document.links[i].blur;
		}
	}
}

// cache mouseover images
function preloadImages() {
	if (!parent.preloadComplete) {
		newImage('images/poll/bullet1.gif');
		newImage('images/interface/nexto.gif');
		newImage('images/interface/backo.gif');
		newImage('images/interface/topo.gif');
		newImage('images/tutorials/numbers/vote0.gif');
		newImage('images/tutorials/numbers/vote1.gif');
		newImage('images/tutorials/numbers/vote2.gif');
		newImage('images/tutorials/numbers/vote3.gif');
		newImage('images/tutorials/numbers/vote4.gif');
		newImage('images/tutorials/numbers/vote5.gif');
		parent.preloadComplete = true;
	}
}

// create new images for cache
var imageCache = new Array;
function newImage(img) {
	var media = new Image;
	media.src = img;
	imageCache[imageCache.length] = media;
}

// change an image
function changeImages(id, url) {
	document.images[id].src = url;
}

// evaluate next and back pages for news section
var npage, ppage;
function newsPage() {
	var pagenumber = location.href.match(/[\/\?\\]news(\d\d)(\d\d)\./);

	nextpage();
	prevpage();

	// next page = previous month
	function nextpage() {
		var year = parseInt(pagenumber[1], 10);
		var month = parseInt(pagenumber[2], 10);

		if (year == 0 && month == 1) return;

		month--;
		if (month == 0) {
			month = 12;
			year--;
		}
		npage = 'news' + (year + 100).toString().substr(1) + (month + 100).toString().substr(1) + '.shtml';
		document.getElementById("n1").style.visibility = 'visible';
		// document.getElementById("n2").style.visibility = 'visible';
	}

	// previous page = next month
	function prevpage() {
		var year = parseInt(pagenumber[1], 10);
		var month = parseInt(pagenumber[2], 10);

		if (year >= currentyear && month >= currentmonth) return;

		month++;
		if (month == 13) {
			month = 1;
			year++;
		}
		ppage = 'news' + (year + 100).toString().substr(1) + (month + 100).toString().substr(1) + '.shtml';
		document.getElementById("b1").style.visibility = 'visible';
		// document.getElementById("b2").style.visibility = 'visible';
	}
}

// get last modified date of document
function date_lastmodified() {
	var date = new Date(document.lastModified);
	var d = date.getDate();
	var m = date.getMonth() + 1;
	var y = date.getYear() % 100;

	d = d < 10 ? '0' + d : d;
	m = m < 10 ? '0' + m : m;
	y = y < 10 ? '0' + y : y;

	return d + '-' + m + '-' + y;
}

// close pop-up image windows
function closeImageWindow() {
	if (ImageWindow != null && !ImageWindow.closed) ImageWindow.close();
}

// open a new, formatted image window
var ImageWindow = null;
function openImage(imageURL, imageTitle, imageWidth, imageHeight) {
	// check if window is open: then close
	closeImageWindow();

	// create an Image Window
	ImageWindow = openCenteredWindow('', 'ImageWindow' + imageWidth + '_' + imageHeight, imageWidth, imageHeight, 'no');
	// write to, and format, ImageWindow
	ImageWindow.document.open();
	ImageWindow.document.write('<html><head><title>' + imageTitle + '</title></head><body onclick="window.close();" style="margin: 0px; background-color: #000000;"><table cellspacing="0" cellpadding="0" style="width: 100%; height: 100%;"><tr><td style="text-align: center; vertical-align: middle;"><img src="' + imageURL + '" alt="" style="border: 0px;" /></td></tr></table></body></html>');
	ImageWindow.document.close();
}

// open a content window (with consideration for screen size)
function openCenteredWindow(contentURL, contentTitle, contentWidth, contentHeight, scrollBars) {
	// center window on screen
	var winLeft = (screen.width - 1 - contentWidth) / 2;
	var winTop = (screen.height - 1 - contentHeight) / 2;

	if (winLeft < 0) {
		scrollBars = 'yes';
		winLeft = 10;
		contentWidth = screen.width - 30;
	}
	if (winTop < 0) {
		scrollBars = 'yes';
		winTop = 10;
		contentHeight = screen.height - 100;
	}

	var NewWindow = window.open(contentURL, contentTitle, 'scrollbars=' + scrollBars + ', width=' + contentWidth + ', height=' + contentHeight + ', top=' + winTop + ', left=' + winLeft + ', screenX=' + winLeft + ', screenY=' + winTop);
	NewWindow.focus();
	return NewWindow;
}

// rate a page
function rate(id, rating) {
	openCenteredWindow('/morris/cgi-bin/rate.pl?cmd=vote&id=' + id + '&vote=' + rating, 'Thanks', '200', '100', 'no');
}

// vote on a poll
function poll(id, vote) {
	document.location = '/morris/cgi-bin/poll.pl?cmd=vote&id=' + id + '&vote=' + vote;
}

// void function
function dummy() {}
