var events = {	Concerts:
			[
				["2008-11-15", "6:45 call for 7:30pm", ""],
				["2008-12-13", "6:45 call for 7:30pm", ""],
				["2009-2-28 18:45", "6:45 call for 8:00pm", "Concert at HAMILTON PLACE"],
				["2009-3-28", "6:45 call for 7:30pm", "HPO concert - Masterworks HAMILTON PLACE"],
			 	["2009-5-2", "6:45 call for 7:30pm", "Concert - Mendelssohn"]
			],
		Rehearsals:
			[
				["2008-9-9", "7:30-10pm", ""],
			 	["2008-9-16", "7:30-10pm", ""],
			 	["2008-9-23", "7:30-10pm", ""],
			 	["2008-9-30", "7:30-10pm", ""],
			 	
			 	["2008-10-7", "7:30-10pm", ""],
			 	["2008-10-14", "7:30-10pm", ""],
			 	["2008-10-21", "7:30-10pm", ""],
			 	["2008-10-28", "7:00-10pm", ""],
			 	
			 	["2008-11-4", "7:30-10pm", ""],
			 	["2008-11-11", "7:30-10pm", ""],
			 	["2008-11-12", "7:30-10pm", ""],
			 	["2008-11-14", "7:30-10pm", "choir, soloists and orchestra"],
			 	["2008-11-15", "7:00-10pm", "concert - Vaughn Williams, Durufle"],
			 	["2008-11-18", "7:30-10pm", ""],
			 	["2008-11-25", "7:30-10pm", ""],
			 	
			 	["2008-12-2", "7:30-10pm", ""],
			 	["2008-12-9", "7:30-10pm", ""],
				["2008-12-13", "1:00-4pm", "choir, soloists and orchestra"],
			 	["2008-12-13", "6:45 call for 7:30pm", "concert - Messiah"],
			 	
			 	["2009-1-6", "7:30-10pm", ""],
			 	["2009-1-13", "7:30-10pm", ""],
			 	["2009-1-20", "7:30-10pm", ""],
			 	["2009-1-27", "7:30-10pm", ""],

			 	["2009-2-3", "7:30-10pm", ""],
			 	["2009-2-10", "7:30-10pm", ""],
			 	["2009-2-18", "7:30-10pm", "*"],
			 	["2009-2-24", "7:30-10pm", ""],
			 	["2009-2-25", "7:00-10pm", "choir and orchestra"],
			 	["2009-2-27 15:00", "3:00-5pm", "soloists and piano"],
			 	["2009-2-27 19:00", "7:00-10pm", "choirs, soloists and orchestra"],
			 	["2009-2-28 13:00", "1:00-4:00pm", "dress rehearsal HAMILTON PLACE"],
			 	
			 	["2009-3-3", "7:30-10pm", ""],
			 	["2009-3-10", "7:30-10pm", ""],
			 	["2009-3-17", "7:30-10pm", ""],
			 	["2009-3-24", "7:30-10pm", ""],
			 	["2009-3-27", "7:00-10pm", "HPO choir, soloists and orchestra HAM. PLACE"],
			 	["2009-3-31", "7:30-10pm", ""],

			 	["2009-4-7", "7:30-10pm", ""],
			 	["2009-4-14", "7:30-10pm", ""],
			 	["2009-4-21", "7:30-10pm", ""],
			 	["2009-4-28", "7:30-10pm", ""],
			 	["2009-4-29", "7:00-10pm", "choir, soloists and orchestra (TBC)"],

			 	["2009-5-1", "7:00-10pm", "choir, soloists and orchestra"],
			 	["2009-5-5", "6:00-10pm", "auditions"],
			 	["2009-5-12", "6:00-10pm", "auditions (TBC)"]
			] };

// --- Code follows - Do not change! ---
var COMMENT = 'Comment';
var CONCERT = 'Concert';
var FUTURE = 'Future';
var NONE = 'None';
var REHEARSAL = 'Rehearsal';

// initially, no filter is on
var filter = FUTURE;

function parseDate( dateAsText )
{
	var retVal = null;

	// separate date and (optional) time components
	var date_time = dateAsText.split( ' ' );

	if( date_time.length > 0 )
	{
		var year = 0;
		var month = 0;
		var day = 0;
		var hours = 0;
		var minutes = 0;
		var seconds = 0;

		var ymd = date_time[0].split( '-' );

		if( ymd.length == 3 )
		{
			year = ymd[0];
			month = ymd[1];
			day = ymd[2];

			if( date_time.length > 1 )
			{
				var hms = date_time[1].split( ':' );

				if( hms.length > 0 )
					hours = hms[0];

				if( hms.length > 1 )
					minutes = hms[1];

				if( hms.length > 2 )
					seconds = hms[2];
			}
		}

		retVal = new Date( year, month - 1, day, hours, minutes, seconds );
	}

	return retVal;
}

function sortRecordsByDate( recordA, recordB )
{
	var dateA = parseDate( recordA[0] );
	var dateB = parseDate( recordB[0] );

	if( dateA && dateB )
		return dateA - dateB;

	return 0;
}

function displayRecords( tableId, filterId )
{
	var rows = document.getElementById( tableId ).tBodies[0].rows;
	var filter = document.getElementById( filterId ).value;

	var curMonthName = '';
	var prevMonthName = '';

	var now = new Date();

	for( var idx = 0, evenRow = false; idx < rows.length; ++idx )
	{
		// default: hide all rows
		rows[ idx ].style.display = 'none';

		// the (inclusive) filter will determine what to show
		if( filter == NONE )
		{
			rows[ idx ].style.display = '';	// no filter, so display = default = show
		}
		else
		{
			if( filter == FUTURE && rows[ idx ].eventDate > now )
				rows[ idx ].style.display = '';
			else if( filter == COMMENT && rows[ idx ].hasComment )
				rows[ idx ].style.display = '';
			else if( rows[ idx ].eventType == filter )
				rows[ idx ].style.display = '';
		}

		// for the VISIBLE rows:
		if( rows[ idx ].style.display == '' )
		{
			// if different month, then display month name
			rows[ idx ].cells[0].innerHTML = '';
			curMonthName = rows[ idx ].monthName;

			if( curMonthName != prevMonthName )
			{
				rows[ idx ].cells[0].innerHTML = curMonthName;
				prevMonthName = curMonthName;
			}

			rows[ idx ].className = ''

			// alternate rows have a different style
			if( evenRow )
				rows[ idx ].className += " alt";

			evenRow = !evenRow;

			if( rows[ idx ].eventDate < now )
			{
				rows[ idx ].className += " pastDate";
			}
			else
			{
				if( rows[ idx ].eventType == CONCERT )
					rows[ idx ].className += " concert";

				if( rows[ idx ].hasComment )
					rows[ idx ].className += " comment";
			}
		} // if VISIBLE row
	} // for each row
}

function getRecords()
{
	// We are going to join and sort (by date) the
	// two types of events, but I'll add another element to each
	// record to keep track of the type of event.
	var nElements;
	var concerts = events.Concerts;

	for( var idx = 0; idx < concerts.length; ++idx )
	{
		var nElements = concerts[idx].length;
		concerts[idx][nElements] = CONCERT;
	}

	var rehearsals = events.Rehearsals;

	for( var idx = 0; idx < rehearsals.length; ++idx )
	{
		nElements = rehearsals[idx].length;
		rehearsals[idx][nElements] = REHEARSAL;
	}

	// Concatinate and Sort events by date
	var ary = concerts.concat( rehearsals );
	ary.sort( sortRecordsByDate );

	return ary;
}

function addRecordsToTable( tableId, ary )
{
	// Add rows to table
	var tbdy = document.getElementById( tableId ).tBodies[0];

	var record, dt;
	var now = new Date();
	var previousMonth = -1
	var newRow, newCell;

	for( var idx = 0; idx < ary.length; ++idx )
	{
		record = ary[ idx ];
		dt = new Date( parseDate(record[0]) );

		if( dt )
		{
			newRow = tbdy.insertRow(-1);
			newRow.eventDate = dt;

			// keep track of event type
			if( record[3] == CONCERT )
			{
				newRow.eventType = CONCERT;

				if( record[2].length == 0 )
					record[2] = CONCERT;
			}
			else
			{
				// if not a concert, then must be a rehearsal
				newRow.eventType = REHEARSAL;
			}

			// Any rows with comments should standout
			newRow.hasComment = false;

			if( record[2].length != 0 )
				newRow.hasComment = true;

			newCell = newRow.insertCell(0);
				newRow.monthName = monthNames[ dt.getMonth() ];	// at display time, we'll decide whether to show month
			
			newCell = newRow.insertCell(1);
				newCell.innerHTML = dt.getDate();
			
			newCell = newRow.insertCell(2);
				newCell.innerHTML = dayNames[ dt.getDay() ];
			
			newCell = newRow.insertCell(3);
				newCell.innerHTML = record[1];
			
			newCell = newRow.insertCell(4);
				newCell.innerHTML = record[2];
		}

	}
}
