function addTeam(teams, team)
{
	if (teams[team] == null)
	{
		teams[team]				=	new Object;
		teams[team].played	=	1;
	}
	else
	{
		teams[team].played++;
	}
}

function showGame(game,	displayState)
{
	if (displayState)
	{
		game.style.display					=	"";
		game.nextSibling.style.display	=	"";
	}
	else
	{
		game.style.display					=	"none";
		game.nextSibling.style.display	=	"none";
	}
}

function selectTeam()
{
	var	games			=	document.all.tags("h2"),	
			team			=	this.options[this.selectedIndex].innerText,
			teamExp		=	new RegExp,
			teamFound;
	
	if (team	== "All")
	{
		team	=	".*";
	}

	teamExp.compile(team);

	for (var gameIndex = 0; gameIndex < games.length; gameIndex++)
	{
		teamFound = games[gameIndex].innerText.match(teamExp) != null;

		showGame(games[gameIndex], teamFound);
	}
}

function initialise()
{
	var	h2s					=	document.all.tags("h2"),
			firstH2				=	h2s[0],
			sortedTeams			=	new Array,
			teamPara,
			teamSelect,
			teamSelectTable,
			teamSelectTBody,
			teamSelectRow,
			teamSelectCell,
			teamsExp				=	new RegExp,
			teams					=	new Object,
			thisOption;
			
	teamsExp.compile("(.*) V (.*) [[].*", "i");
	
	for (var	h2Index = 0; h2Index < h2s.length; h2Index++)
	{
		teamsExp.exec(h2s[h2Index].innerText);

		addTeam(teams, RegExp.$1);		
		addTeam(teams, RegExp.$2);		
	}
	
	for (teamIndex in teams)
	{
		sortedTeams[sortedTeams.length]	=	teamIndex;
	}
	
	sortedTeams.sort();
	
	teamSelectTable				=	document.createElement("table");
	teamSelectTBody				=	document.createElement("tbody");
	teamSelectRow					=	document.createElement("tr");
	teamSelectCell					=	document.createElement("td");
	teamSelectCell.innerText	=	"Show games for team";
	
	teamSelectRow.appendChild(teamSelectCell);
	teamSelectTBody.appendChild(teamSelectRow);
	teamSelectTable.appendChild(teamSelectTBody);
	
	teamSelectCell			=	document.createElement("td");
	teamSelect				=	document.createElement("select");
	teamSelect.name		=	"teamSelect";
	teamSelect.onchange	=	selectTeam;
	
	thisOption				=	document.createElement("option");
	thisOption.innerText	=	"All";
	thisOption.selected	=	true;
	teamSelect.appendChild(thisOption);
	
	for (var teamIndex = 0; teamIndex < sortedTeams.length; teamIndex++)
	{
		thisOption				=	document.createElement("option");
		thisOption.innerText	=	sortedTeams[teamIndex];
		teamSelect.appendChild(thisOption);
	}
	
	teamSelectCell.appendChild(teamSelect);
	teamSelectRow.appendChild(teamSelectCell);
	
	firstH2.insertAdjacentElement("BeforeBegin", teamSelectTable);
}

