var items = new Array();
var numItems = 0;
var backURL = "";
var quotes = new Array(
	'empty',
	'angr_conq.gif',
	'apology.gif',
	'be_friend.gif',
	'belev_thnk.gif',
	'coststime.gif',
	'discourage.gif',
	'dont_die.gif',
	'education.gif',
	'fall.gif',
	'fin_task.gif',
	'fool_some.gif',
	'fork_yogi.gif',
	'goal.gif',
	'god_laff.gif',
	'godsgift.gif',
	'hard_tchr.gif',
	'heller.gif',
	'imagination.gif',
	'inferior.gif',
	'j_swift.gif',
	'judg_book.gif',
	'know_how.gif',
	'laotzu.gif',
	'lincoln.gif',
	'lotinlife.gif',
	'mistakes.gif',
	'nietzche.gif',
	'ntr_desire.gif',
	'onyourfeet.gif',
	'parents.gif',
	'piece_ofyourmind.gif',
	'prayers.gif',
	'prog_opt.gif',
	'rite2do.gif',
	'seek_truth.gif',
	'taken_seriously.gif',
	'think_ucan.gif',
	'turkish.gif',
	'wisernow.gif'
	);

function item(org, html)
{
	this.org = org;
	this.html = html;
}

function addItem(row, cols)
{
	i = (row-1) * 4;

	var org = cols[i+1].substr(7,96);
	pos = org.indexOf("<", 0);
	if (pos > 0)
		org = org.substring(0, pos);

	for (j=1; j <= numItems; j++)
	{
		if ( org == items[j].org )
		{
			alert(org + " has already been added to basket");
			return false;
		}
	}

	var html = cols[i+1] + cols[i+2] + cols[i+3] + cols[i+4];

	items[++numItems] = new item(org, html);
}

function removeItems()
{
	numItems = 0;
	redisplayItems();
}

function removeItem(row)
{
	for (i=row; i < numItems; i++)
	{
		items[i] = items[i+1];
	}

	numItems--;
	redisplayItems();
}

function displayItems()
{
	if (main.bottom.frames.length == 0)
		backURL = new String(window.main.bottom.location);
	else
	{
		backURL = new String(window.main.bottom.activity.location);
		pos = backURL.lastIndexOf("/", backURL.length);
		backURL = backURL.substring(pos, backURL.length);
	}

	redisplayItems();
}

function redisplayItems()
{
	var activity = "";

	if (numItems == 1)
		activity = " activity";
	else
		activity = " activities";

	caption = "You have selected " + numItems + activity;
	top.newTitle(caption, "");

	with (top.main.bottom.document)
	{
		open();
		write('<html>');
		write('\n<head>');
		write('\n<title>Display Basket</title>');
		write('<link href="/activities.css" rel=stylesheet type="text/css">');
		write('\n</head>');
		write('\n<body>');
		write('\n<table class="background" align="center" border="0" cellpadding="0" cellspacing="0" width="90%">');
		write('\n<tr><td align="center">');
		write('\n<hr width="90%">');

		if (numItems > 0)
		{
			write('\n<table border="2" bgcolor="#ffff99" align="center" cellpadding="3" cellspacing="0" bordercolor="red">');
			write('\n<tr bgcolor="darkkhaki">');
			write('\n<th>&nbsp</th>');
			write('\n<th>Activity</th>');
			write('\n</tr>');

			for (i=1; i <= numItems; i++)
			{
				write('\n<tr>');
				write('\n<td><a href="javascript:top.removeItem(' + i + ')">remove</a></td>');
				write('\n<td>' + items[i].org + '</td>');
				write('\n</tr>');
			}

			write('\n</table>');
		}

		write('\n<br>');
		write('\n<table align="center">');
		write('\n<tr>');

		if (numItems > 0)
		{
			write('\n<td class="link"><a class="link" href="javascript:top.printItems()">Print</a></td>');
			write('\n<td class="link">|</td>');
		}

		write('\n<td class="link"><a class="link" href="javascript:top.back()">Back to Activities</a></td>');
		write('\n<td class="link">|</td>');

		if (numItems > 0)
		{
			write('\n<td class="link"><a class="link" href="javascript:top.removeItems()">Clear Basket</a></td>');
			write('\n<td class="link">|</td>');
		}

		write('\n<td class="link"><a class="link" href="/home.html" target="main">Home</a></td>');
		write('\n</tr>');
		write('\n</table>');
		write('\n');
		write('\n<br>');
		write('\n</td></tr>');
		write('\n</table>');
		write('\n</body></html>');
		close();
	}
}

function printItems()
{
	if (window.print)
		newWindow=window.open("/activities/printItems.html","printItems", "scrollbars=1 width=750");
	else
		alert("Sorry, your browser doesn't support the Print feature.");
}

function submitWindow(name, height)
{
	var width = 760;
	if (width > screen.availWidth)
		width = screen.availWidth;

	var banner = screen.height - screen.availHeight - 28;

	if (height + 28 > screen.availHeight)
		height = screen.availHeight - 28;

	var top =  ((screen.availHeight - height - 28) / 2) + banner;
	var left = (screen.availWidth  - width)  / 2;

	var html = name + ".html"
	newWindow=window.open(html, name, "scrollbars=1 top=" + top + " left=" + left + " width=" + width + " height=" + height);
}

function back()
{
	if (backURL.substring(0, 1) == "/")
	{
		abc = backURL.substring(1, backURL.length);
		newPage( abc );
	}
	else
		window.main.bottom.location = backURL;
}

function writeRows(cols)
{
	var row = 0;

	for (i=1; i < cols.length; i += 4)
	{
		with (main.bottom.activity.document)
		{
			row++
			write('\n<tr>');
			write('<td><a onMouseOver="return over()" href="javascript:addItem(' + row + ',cols)" onMouseOut="out()">add</a></td>')

			for (j=0; j < 4; j++)
			{
				write('\n' + cols[i+j]);
			}

			write('\n</tr>');
		}
	}
}

function newTitle(text, titleParent)
{
	main.bottom.document.title = text;
	main.document.title = text;

	with (main.title.document)
	{
		open();
		write('<html>');
  		write('\n<head>');
  		write('\n<title>do not use</title>');
  		write('\n<link rel="stylesheet" href="/title.css" type="text/css">');
  		write('\n</head>');
  		write('\n<body>');
  		write('\n<table class="background" align="center" border="0" cellpadding="0" cellspacing="0" width="90%">');
  		write('\n<tr><td class="title" align="center" height="30">');
  		write('\n' + titleParent + text);
  		write('\n</td></tr>');
  		write('\n</table>');
  		write('\n</body>');
  		write('</html>');
  		close();
	}
}

function newQuote()
{
	var random = Math.floor(Math.random() * quotes.length);
	if (random == 0) random = 1;
	if (random == quotes.length) random = random - 1;

	var src = "/quotes/" + quotes[random];

	with (main.bottom.document)
	{
		write('\n<center><img src="' + src + '" border="0"></center>');
	}
}

function newPage(page)
{
	page = "/activities/page/" + page;

	with (main.bottom.document)
	{
		open();
		write('<html>');
		write('\n<head>');
		write('\n<title>do not use</title>');
		write('\n<frameset border="0" frameborder="0" rows="*,30">');
		write('\n	<frame name="activity" src="' + page + '" scrolling="auto" marginheight="0" marginwidth="0"></frame>');
		write('\n	<frame name="footer"   src="/activities/footer.html" scrolling="auto" marginheight="0" marginwidth="0"></frame>');
		write('\n</frameset>');
		write('\n</head>');
		write('</html>');
		close();
	}
}

function newFooter(text)
{
	with (main.bottom.footer.document)
	{
		open();
		write('<html>');
		write('\n<head>');
		write('\n<link rel="stylesheet" href="/activities.css" type="text/css">');
		write('\n</head>');
		write('\n<body>');
		write('\n<table border="2" bgcolor="#ffff99" align="center" cellpadding="0" cellspacing="0" bordercolor="#cc0000" width="100%">');
		write('\n<tr bgcolor="darkkhaki">');
		write('\n<td width="20">');
		write('\n<a href="http://sm6.sitemeter.com/stats.asp?site=sm6teenergy2" target="_top">');
		write('\n<img src="http://sm6.sitemeter.com/meter.asp?site=sm6teenergy2" alt="Site Meter" border=0 height="20" width="20"></a>');
		write('\n</td>');
		write('\n<td align="center">');
		write('\n' + text);
		write('\n</td></tr></table>');
		write('\n</body>');
		write('</html>');
		close();
	}
}

function gotoTop()
{
	window.main.bottom.activity.location = "#top";
	window.main.bottom.activity.focus();
}

function newSearch(form)
{
	var sel = form.search;
	var page = sel.options[sel.selectedIndex].value;
	form.search.selectedIndex = 0;
	this.focus();

	if (page == "")
		return false;

	newPage(page);
}

function newAnchor(form)
{
	var sel = form.search;
	var page = sel.options[sel.selectedIndex].value;
	form.search.selectedIndex = 0;
	this.focus();

	if (page == "")
		return false;

	var ref = new String(window.main.bottom.location.pathname) + page;
	window.main.bottom.location = ref;
}
