var events = new Array();
events[events.length] = new addEvent("Presentation Night Photos", "25 May 2010 21:05", "results0910");

function addEvent (eventName, eventTime, href)
{
	this.name = eventName;
	this.time = new Date(eventTime);
	this.href = href;
}

// sort into chronological order
function sortEvent(a,b)
{
  return (a.time.getTime() < b.time.getTime()) ? 1 : -1;
}

var months = new Array(" Jan"," Feb"," Mar"," Apr"," May"," Jun"," Jul"," Aug"," Sep"," Oct"," Nov"," Dec");
var tableColA = "#00F000> ";
var tableColB = "#80F000> ";
var currentCol = tableColA;

var today = new Date();
today = today.getTime();

var EntryDate = new Date("11 October 2009");  // start of season.  NB Easter April 4, 2010
var lastDate;

var weekNo = 0;

function addScore (tableName, teamA, teamB, scoreA, scoreB)
{
	tableName[teamA].played++;
	tableName[teamB].played++;
	tableName[teamA].legs = tableName[teamA].legs + scoreA;
	tableName[teamB].legs = tableName[teamB].legs + scoreB;
	scoreA > scoreB ? tableName[teamA].wins ++ : tableName[teamB].wins ++;			
}

function Fixture (fixtureType, fixtureDate, teamA, teamB, dScoreA, dScoreB, cScoreA, cScoreB, sScoreA, sScoreB)
{
// fixtureType : L=League K=Knockout T=knockoutTitle H=Holiday

	this.fixType = fixtureType;
	this.day = new Date(fixtureDate);
	this.day.setHours(20);
	this.homeTeam = teamA;
	this.awayTeam = teamB;
	this.homeDoublesScore = dScoreA;
	this.awayDoublesScore = dScoreB;
	this.homeSinglesScore = sScoreA;
	this.awaySinglesScore = sScoreB;
	this.homeCrocketScore = cScoreA;
	this.awayCrocketScore = cScoreB;
	
	this.relativeDate = this.day.getTime() < today ? 0 : 3;
	
	if (arguments.length > 3)
	{
		if (lastDate != fixtureDate.getTime())
		{
			weekNo++;
			lastDate = fixtureDate.getTime();
		}
		this.weekNo = weekNo;
	}
	else this.weekNo=0;
	
	if (dScoreA != null)
	{
		if (fixtureType == 'L')
		{
			addScore(doublesTable,teamA,teamB,dScoreA,dScoreB);
			addScore(singlesTable,teamA,teamB,sScoreA,sScoreB);
			addScore(crocketTable,teamA,teamB,cScoreA,cScoreB);
		}
		if (fixtureType == 'K')
		{
			addScore(cupTable,teamA,teamB,dScoreA,dScoreB);
		}
	}
}


function tableEntry (teamNo, played, wins, legs)
{
	this.team = teamNo;
	this.played = played;
	this.wins = wins;
	this.legs = legs;
}


// create an array for teams, this array will be cross referenced by other functions, so this is the only 
// part which needs to be modified when setting team names.
// Note: The following pairs of teams will not share a home venue
// 0+4, 1+5, 2+6, 3+7.    Pairing 2+6 should be used last as they play on the last match.
var teamName = new Array ();
teamName[teamName.length] = "British Legion";
teamName[teamName.length] = "Wheatsheaf Inn";
teamName[teamName.length] = "Duke of York";
teamName[teamName.length] = "Farmers Arms";
teamName[teamName.length] = "Prince of Wales";
teamName[teamName.length] = "Beauchamp Arms";

var Legion = 0;
var Beauch = 5;
var Wheats = 1;
var DukeoY = 2;
var Farmer = 3;
var Prince = 4;

function getTeamName (teamId)
{
   return (isNaN(teamId) ? teamId : teamName[teamId]);
  /*if (isNaN(teamId)) {return teamId}
  else return teamName[teamId]*/
}

var doublesTable = new Array ();
var singlesTable = new Array ();
var crocketTable = new Array ();
var cupTable = new Array ();

for (i = 0 ; i < teamName.length; i++)
{
	doublesTable[i] = new tableEntry(i,0,0,0);
	cupTable[i] = new tableEntry(i,0,0,0);
	singlesTable[i] = new tableEntry(i,0,0,0);
	crocketTable[i] = new tableEntry(i,0,0,0);

}

var fixtures = new Array ();
// fixtureType, fixtureDate, teamA, teamB, dScoreA, dScoreB, sScoreA, sScoreB
fixtures[fixtures.length] = new Fixture("H","22 September 2009","Annual General Meeting");
fixtures[fixtures.length] = new Fixture("H",EntryDate,"Cribbage Drive");
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Legion,Prince,9,6,1,2,2,3);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Beauch,DukeoY,8,7,3,0,3,2);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Wheats,Farmer,6,9,1,2,3,2);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,DukeoY,Legion,8,7,2,1,2,3);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Farmer,Beauch,6,9,2,1,4,1);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Prince,Wheats,7,8,3,0,0,5);
EntryDate.setDate(EntryDate.getDate() + 2);
fixtures[fixtures.length] = new Fixture("H",EntryDate,"Committee Meeting");
EntryDate.setDate(EntryDate.getDate() + 5);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Legion,Farmer,8,7,0,3,3,2);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Beauch,Wheats,9,6,2,1,2,3);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Prince,DukeoY,9,6,1,2,3,2);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("T",EntryDate,"Pairs Knockout Heats");
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Wheats,Legion,5,10,1,2,3,2);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Farmer,DukeoY,8,7,1,2,4,1);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Beauch,Prince,5,10,1,2,1,4);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Legion,Beauch,7,8,1,2,2,3);
fixtures[fixtures.length] = new Fixture("L",EntryDate,DukeoY,Wheats,4,11,1,2,3,2);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Prince,Farmer,4,11,3,0,2,3);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("T",EntryDate,"Pairs Knockout Final");
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Prince,Legion,9,6,1,2,1,4);
fixtures[fixtures.length] = new Fixture("L",EntryDate,DukeoY,Beauch,7,8,2,1,2,3);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Farmer,Wheats,5,10,2,1,4,1);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Legion,DukeoY,8,7,2,1,4,1);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Beauch,Farmer,7,8,2,1,4,1);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Wheats,Prince,6,9,2,1,4,1);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Farmer,Legion,6,9,1,2,1,4);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Wheats,Beauch,9,6,3,0,5,0);
fixtures[fixtures.length] = new Fixture("L",EntryDate,DukeoY,Prince,6,9,1,2,2,3);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("H",EntryDate,"Christmas Break");
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Legion,Wheats,8,7,2,1,4,1);
fixtures[fixtures.length] = new Fixture("L",EntryDate,DukeoY,Farmer,6,9,1,2,3,2);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Prince,Beauch,9,6,1,2,1,4);
EntryDate.setDate(EntryDate.getDate() + 2);
fixtures[fixtures.length] = new Fixture("H",EntryDate,"Committee Meeting");
EntryDate.setDate(EntryDate.getDate() + 5);
EntryDate.setDate(EntryDate.getDate() + 7);

fixtures[fixtures.length] = new Fixture("T",EntryDate,"Singles Knockout Heats");
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Legion,Prince,5,10,1,2,1,4);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Beauch,DukeoY,9,6,3,0,1,4);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Wheats,Farmer,9,6,1,2,1,4);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,DukeoY,Legion,4,11,0,3,4,1);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Farmer,Beauch,7,8,2,1,2,3);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Prince,Wheats,9,6,2,1,1,4);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("T",EntryDate,"Singles Knockout Final");
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Legion,Farmer,9,6,3,0,3,2);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Beauch,Wheats,9,6,0,3,4,1);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Prince,DukeoY,6,9,3,0,4,1);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Wheats,Legion,7,8,0,3,2,3);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Farmer,DukeoY,7,8,0,3,4,1);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Beauch,Prince,10,5,2,1,0,5);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Legion,Beauch,8,7,1,2,2,3);
fixtures[fixtures.length] = new Fixture("L",EntryDate,DukeoY,Wheats,7,8,1,2,4,1);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Prince,Farmer,5,10,2,1,3,2);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Beauch,Legion,10,5,3,0,2,3);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Wheats,DukeoY,10,5,2,1,2,3);
fixtures[fixtures.length] = new Fixture("L",EntryDate,Farmer,Prince,11,4,2,1,2,3);



EntryDate.setDate(EntryDate.getDate() + 2);
fixtures[fixtures.length] = new Fixture("H",EntryDate,"Committee Meeting");
EntryDate.setDate(EntryDate.getDate() + 5);

fixtures[fixtures.length] = new Fixture("K",EntryDate,Prince,Legion,11,4);
fixtures[fixtures.length] = new Fixture("K",EntryDate,DukeoY,Beauch,9,6);
fixtures[fixtures.length] = new Fixture("K",EntryDate,Farmer,Wheats,8,7);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("K",EntryDate,Legion,DukeoY,8,7);
fixtures[fixtures.length] = new Fixture("K",EntryDate,Beauch,Farmer,11,4);
fixtures[fixtures.length] = new Fixture("K",EntryDate,Wheats,Prince,6,9);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("K",EntryDate,Farmer,Legion,5,10);
fixtures[fixtures.length] = new Fixture("K",EntryDate,Wheats,Beauch,8,7);
fixtures[fixtures.length] = new Fixture("K",EntryDate,DukeoY,Prince,8,7);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("H",EntryDate,"Easter Sunday");
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("K",EntryDate,Legion,Wheats,8,7);
fixtures[fixtures.length] = new Fixture("K",EntryDate,DukeoY,Farmer,8,7);
fixtures[fixtures.length] = new Fixture("K",EntryDate,Prince,Beauch,11,4);
EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("K",EntryDate,Beauch,Legion,6,9);
fixtures[fixtures.length] = new Fixture("K",EntryDate,Wheats,DukeoY,9,6);
fixtures[fixtures.length] = new Fixture("K",EntryDate,Farmer,Prince,6,9);

EntryDate.setDate(EntryDate.getDate() + 7);
fixtures[fixtures.length] = new Fixture("K",EntryDate,Prince,Legion,7,8);
EntryDate.setDate(EntryDate.getDate() + 2);
fixtures[fixtures.length] = new Fixture("H",EntryDate,"Committee Meeting");
//EntryDate = new Date("31 May 2009");
//fixtures[fixtures.length] = new Fixture("H",EntryDate,"Cribbage Drive at The Wheatsheaf");
EntryDate = new Date("23 May 2010");
fixtures[fixtures.length] = new Fixture("H",EntryDate,"Presentation Night at The Beauchamp");



/*function sortAlph(a,b)
{
  var teama = teamName[a.team].toLowerCase();
  var teamb = teamName[b.team].toLowerCase();
  
  
  if (teama < teamb) {return -1}
  if (teamb < teama) {return 1}
  return 0;
  
}

function sortLeg(a,b)
{
//  return (a.legs >= b.legs) ? -1 : 0;
  if (a.legs < b.legs) return 1;
  if (a.legs >= b.legs) return -1;
}

function sortWin(a,b)
{
  return (b.wins - a.wins);
}
*/

var tempEntry = new tableEntry ("A",0,0,0);


function sortAlpha (tableName)
{
for (i=0; i<tableName.length; i++)
{
	for (j=i+1; j<tableName.length; j++)
	{
		if (teamName[tableName[i].team] > teamName[tableName[j].team])
		{
			tempEntry = tableName[i];
			tableName[i] = tableName[j];
			tableName[j] = tempEntry;
		}
	}
}
}

function sortLegs(tableName)
{
for (i=0; i<tableName.length; i++)
{
	for (j=i+1; j<tableName.length; j++)
	{
		if (tableName[i].legs < tableName[j].legs)
		{
			tempEntry = tableName[j];
			for (m=j; m>i; m--)
			{
				tableName[m] = tableName[m-1];
			}
			tableName[i] = tempEntry;
			j--;
		}
	}
}
}
 
// sort doubles table by points, numerically
function sortWins(tableName)
{
for (i=0; i<tableName.length; i++)
{
	for (j=i+1; j<tableName.length; j++)
	{
		if (tableName[i].wins < tableName[j].wins)
		{
			tempEntry = tableName[j];
			for (m=j; m>i; m--)
			{
				tableName[m] = tableName[m-1];
			}
			tableName[i] = tempEntry;
			j--;
		}
	}
}
}


sortAlpha(doublesTable);
sortAlpha(singlesTable);
sortAlpha(crocketTable);
sortAlpha(cupTable);

sortLegs(doublesTable);
sortLegs(singlesTable);
sortLegs(crocketTable);
sortLegs(cupTable);

sortWins(doublesTable);
sortWins(cupTable);

/*
singlesTable.sort(sortAlph);
crocketTable.sort(sortAlph);
doublesTable.sort(sortAlph);
cupTable.sort(sortAlph);


singlesTable.sort(sortLeg);
crocketTable.sort(sortLeg);
doublesTable.sort(sortLeg);
cupTable.sort(sortLeg);


doublesTable.sort(sortWin);
doublesTable.sort(sortWin);
cupTable.sort(sortWin);
*/

function displayTable(tableName,tableTitle,wins)
{
	var p = document;
	
	p.write('<p align=center><strong>' + tableTitle + '</strong></p>');
	
	p.write('<table border="1" class="crib_para" cellpadding="2" cellspacing="0" width="100%" title="' + tableTitle + '" bgcolor=' + tableColA);
	p.write('<thead>');
	p.write('<tr>');
	p.write('<td align="left"><strong>Team</strong></td>');
	p.write('<td width="10%" align="center"><strong>P</strong></td>');
	if (wins==true)
	{
		p.write('<td width="10%" align="center"><strong>W</strong></td>');
	}
	p.write('<td width="10%" align="center"><strong>Legs</strong></td>');
	if (wins==true)
	{
		p.write('<td width="10%" align="center"><strong>Pts</strong></td>');
	}
	p.write('</tr>');
	p.write('</thead><tbody>');
	for (i = 0 ; i < tableName.length; i++)
	{
		if (teamName[tableName[i].team] == "Bye") {continue;}
		p.write('<tr><td nowrap>' + getTeamName(tableName[i].team) + '</td>');
		p.write('<td align="center">' + tableName[i].played + '</td>');
		if (wins==true)
		{
			p.write('<td align="center">' + tableName[i].wins + '</td>');
		}
		p.write('<td align="center">' + tableName[i].legs + '</td>');
		if (wins==true)
		{
			p.write('<td align="center">' + (2 * tableName[i].wins) + '</td>');
		}
		p.write('</tr>');
	}  
	p.write('</tbody></table>');
}


function display_tables(doubles, crocket, singles)
{
	var p = document;
	p.write('<center>');
	p.write('<table class="crib_para" border="0" cellspacing="0" cellpadding="0">');
	p.write('<tr>');

	p.write('<td align="center">');
	displayTable(doubles,"League",true);
	p.write('</td>');

	if (crocket != null)
	{
		p.write('<td width="5%">&nbsp;&nbsp;&nbsp;</td>');

		p.write('<td nowrap align="center">');
		displayTable(crocket,"Crocket Memorial");
		p.write('</td>');
	}

	p.write('<td width="5%">&nbsp;&nbsp;&nbsp;</td>');

	p.write('<td nowrap align="center">');
	displayTable(singles,"Ken Long");
	p.write('</td>');

	p.write('</tr>');
	p.write('</table>');
	
	p.write('</center>');
	
//	window.top.document.body.cols = "*,1";
}
//-->
var thisDay, lastday;
function newLine (dateObj,weekNo)
{
	thisDay = dateObj.getTime();
	if (lastday != thisDay) // date has changed, change the background colour
	{
		currentCol = (currentCol == tableColA ? tableColB : tableColA);
		document.write('<TR id=' + dateObj.getTime() + ' bgcolor=' + currentCol + '<TD vAlign=top>&nbsp;');
		if (weekNo > 0) document.write( weekNo);
		document.write('</TD><TD valign=top nowrap align="right">' + dateObj.getDate() + months[dateObj.getMonth()] + '</TD>');

	}
	else
	{
		document.write('<TR bgcolor=' + currentCol + '<TD>&nbsp</TD><TD>&nbsp</TD>');
	}
	lastday = thisDay;
}