// Javascript functions

var _outline = "";
var _section = "";
var _thispage = "";
var _pageparams = new Array;

function w(s) { document.write(s); }
function wl(s) { document.writeln(s); }
function wlbr(s) { document.writeln(s+'<br>'); }

// function
function decode(str)
		{
		var i;

		while ((i = str.indexOf('%')) > 0)
			{
			var s1, s2, s3

			s1 = str.substring(0, i)
			s2 = hexChar(str.substring(i+1, i+3))
			s3 = str.substring(i+3)

			str = ""+s1+s2+s3;
			}

		return str;
		}


// Work out _thispage
function getPageInfo()
		{
		var i, j;
		
		i = document.URL.indexOf('?', i);

		if (i < 0) i = document.URL.length;
		_thispage = document.URL.substring(0, i);

		if (_thispage == "" || _thispage.charAt(_thispage.length-1) == '/') _thispage += "index.html";


		// If we have a parameter it is the category
		if ((i = document.URL.indexOf("?")) >= 0)
			{
			var pstr = document.URL.substring(i+1);
			var param = "";

			while ((i = pstr.indexOf("&")) > 0)
				{
				addParam(decode(pstr.substr(0, i)));
				pstr = pstr.substr(i+1);
				}

			addParam(decode(pstr));
			}
		}


function addParam(str)
		{
		var p = new Object;
		var i = str.indexOf('=');

		if (i < 0)
			{
			p.name = "";
			p.value = str;
			}
		else
			{
			p.name = str.substring(0, i);
			p.value = str.substring(i+1);
			}

		_pageparams[_pageparams.length] = p;
		}

function getParam(name)
		{
		var i;

		for (i=0;i<_pageparams.length;i++)
			{
			if (_pageparams[i].name == name) return _pageparams[i].value;
			}

		return "";
		}



function redirectpage(url, wait)
		{
		startpage("Redirecting you to the correct page .... please wait");

		wl('<meta HTTP-EQUIV="Refresh" CONTENT="'+wait+';URL=http:'+url+'">');
		wl('If the page fails to appear please <a href="'+url+'">click here</a>');
		}


//--------------------------------------------------------------------------------
// Routines for working with the selectable table
var _linkList = new Array;
var _categoryList = new Array;
var _nLinks = 0;
var _nCats = 0;

// find the named category object or create it.
function findCategory(name)
	{
	var found = false;
	var category;
	var i;

	for (var i=0;i<_nCats;i++)
	    {
	    category = _categoryList[i];
	    if (category.name == name)
		{
		found = true;
		break;
		}
	    }

	if (!found) return null;
	
	return category;
	}

function findOrCreateCategory(name)
	{
	var cat;

	cat = findCategory(name);
	if (cat != null) return cat;

	cat = _categoryList[_nCats++] = new Object;
	cat.name = name;
	cat.nobjs = 0;
	cat.objarray = new Array;

	return cat;
	}


function addToCategory(category, obj)
	{
	var	cat;

	cat = findOrCreateCategory(category);
	cat.objarray[cat.nobjs++] = obj;
	}


function addTableEntry(type, category, s1, s2, s3)
	{
	var	lobj = new Object;

	lobj.type = type;
	lobj.s1 = s1;
	lobj.s2 = s2;
	lobj.s3 = s3;

	addToCategory("All Categories", lobj);
	addToCategory(category, lobj);

	_linkList[_nLinks++] = lobj;
	}

function hexChar(hc)
	{
	var	zc = "0".charCodeAt(0)
	var	n1 = hc.charCodeAt(0) - zc;
	var	n2 = hc.charCodeAt(1) - zc;

	return String.fromCharCode(n1*16+n2)
	}

function putTable(showcats, style, url)
	{
	var nCols = 1;
	var col, category, lobj, catno, l;
	var param, i;

	// If we have a parameter it is the category
	param = findParam("category");
	if (param == "") param = "All Categories";


	if (showcats)
	    {
	    w('<P CLASS=SmallLink>')
	    for (catno=0;catno<_nCats;catno++)
		{
		category = _categoryList[catno]
		if (category.name != param) 
		    {
		    w('<A CLASS=SmallLink')
		    w(' HREF="'+url+'?category='+category.name+'">')
		    }
		else w('<span class=CurrentSmallLink>')
		w(category.name)
		if (category.name != param) 
		    {
		    w('</A>')
		    }
		else w('</span>')
		w('&nbsp;&nbsp;&nbsp;')
		}
	    }

	if (style == 0) return;

	wl('');
	for (catno=0;catno<_nCats;catno++)
	    {
	    var ws = "WIDTH=100%"

	    category = _categoryList[catno]
	    if ((param != "All Categories" && category.name != param) || category.name == "All Categories") continue;

	    wl('<A NAME="'+category.name+'"></A><H4>'+category.name)
	    if (showcats && param == "All Categories") wl('&nbsp;&nbsp;<A HREF="#top" CLASS=SmallLink>(Back to top)</A>')
	    wl('</H4>')
	    
	    wl('<P>')
	    if (style == 1) wl('<table border=1 cellpadding=3 cellspacing=0>')
	    else if (style == 2) wl('<ul>');

	    col = 0;

	    for (l=0;l<category.nobjs;l++)
		{
		lobj = category.objarray[l]

		if (lobj.type == 1)
		    {
		    // Acronyms
		    w('<tr>');
		    w('<th valign=center align=left>'+lobj.s1+'</th>')
		    w('<td valign=center align=left>'+lobj.s2+'</td>')
		    wl('</tr>');
		    }
		else if (lobj.type == 2)
		    {
		    // Links
		    wl('<li><a href="'+lobj.s1+'">'+lobj.s2+'</a>');
		    }
		}
	    if (style == 1) wl('</table>')
	    else if (style == 2) wl('</ul>');
	    }
	}

//------------------------------------------------------------
function putPicture(title, ref, bigref, bigsize, cap1, cap2, pre, post)
	{
	var alt = title;

	wl(pre);
	wl('<table border=0 vspace=0 hspace=0 cellpadding=0 cellspacing=0><tr><td align=center>');
	if (cap1 != "") wl('<i>'+cap1+'</i><p>');
	if (bigref != "")
	    {
	    alt = "Click to change";
	    if (bigsize != "") alt += " to "+bigsize+" image";
	    w('<a href="'+bigref+'">');
	    }
	wl('<IMG border=0 SRC="'+ref+'">');
	if (bigref != "") w('</a>');
	if (cap2 != "") wl('<p><i>'+cap2+'</i>');
	wl('</td></tr></table>');
	wl(post);
	}





//------------------------------------------------------------
var qNo = 0;
var qDB = new Array();


// Get current cookie setting
// Add an entry to the 'database' (mDB)
function addQuote(text, author)
	{
	qDB[qNo] = new Object;
	qDB[qNo].text = text;
	qDB[qNo].author = author;
	qDB[qNo].type = 1;
	qNo++;
	}

// Add an entry to the 'database' (mDB)
function addRule(text, title)
	{
	qDB[qNo] = new Object;
	qDB[qNo].text = text;
	qDB[qNo].title = title;
	qDB[qNo].type = 3;
	qNo++;
	}

// Add an entry to the 'database' (mDB)
function addShort(text)
	{
	qDB[qNo] = new Object;
	qDB[qNo].text = text;
	qDB[qNo].type = 2;
	qNo++;
	}

// Write an entry
function putEntry(qe, sep)
	{
	if (qe.type == 1) w("<LI>");
	else if (qe.type == 3)
	    {
	    w("<FONT COLOR=\"#000080\"><I>"+qe.title+"</I></FONT><UL>");
	    }
	w(qe.text);
	if (qe.type == 1)
	    {
	    if (qe.author != "") w("<BR><I><FONT COLOR=\"#000080\">~ "+qe.author+"</FONT></I>");
	    w("<P>");
	    }
	else if (qe.type == 2 && sep) wl("<HR>");
	else if (qe.type == 3) wl("</UL>");
	}

// do all the work
function showAll(title, pre, post)
	{
	startpage(title, "special");

	// cycle through each entry in the outline array
	w(pre);
	for (var i=0;i<qDB.length;i++) putEntry(qDB[i], true);
	wl(post);
	}


// do all the work
function showRandom(title, pre, post, shownum, newpage)
	{
	// cycle through each entry in the outline array
	var	l = qDB.length;
	var	i = Math.floor(Math.random() * l);
	var	tstr = title;

	if (shownum) 
	    {
	    if (title != "") tstr += " ";
	    tstr += "#"+(i+1);
	    }

	if (newpage) startpage(tstr, "special");
	else w('<b>'+tstr+'</b><br>');

	w(pre);
	putEntry(qDB[i], false);
	wl(post);
	}

