//This bit of JavaScript generates a table showing the programme for the year.
//Passed dates are shown pale. The next meeting is highlighted.

	u = document.URL;                     //URL of current document
	dirs = u.split("/");                  //Break out the parent directories
	page = dirs[dirs.length - 1];       //Last element is the document name

//Function to return the month name
function monthofYear(mon)
{
	switch( mon )
	{
		case 0: s = "January"; break;
		case 1: s = "February"; break;
		case 2: s = "March"; break;
		case 3: s = "April"; break;
		case 4: s = "May"; break;
		case 5: s = "June"; break;
		case 6: s = "July"; break;
		case 7: s = "August"; break;
		case 8: s = "September"; break;
		case 9: s = "October"; break;
		case 10: s = "November"; break;
		case 11: s = "December"; break;
		default: s = "Unknownmonth"
	}
	return s;
}

//Function to return the day in ordinal form
function dayofMonth(day)
{
	if (day > 3 & day < 21)
	{
		return day + "th"
	}
	switch( day % 10 )
	{
		case 0: s = day + "th"; break;
		case 1: s = day + "st"; break;
		case 2: s = day + "nd"; break;
		case 3: s = day + "rd"; break;
		case 4: s = day + "th"; break;
		case 5: s = day + "th"; break;
		case 6: s = day + "th"; break;
		case 7: s = day + "th"; break;
		case 8: s = day + "th"; break;
		case 9: s = day + "th"; break;
		default: s = "Unknownday"
	}
	return s;
}

//The programme for 2008-09 - date
var progDate = new Array(	"Sat, 20 Sep 2008",
							"Sat, 18 Oct 2008", 
							"Fri, 14 Nov 2008", 
							"Sat, 13 Dec 2008", 
							"Sat, 17 Jan 2009", 
							"Sat, 21 Feb 2009", 
							"Sat, 21 Mar 2009", 
							"Sun, 22 Mar 2009", 
							"Sat, 18 Apr 2009", 
							"Sat, 16 May 2009", 
							"Sat, 20 Jun 2009",
							"Sat, 19 Sep 2009");

//The programme for 2008-09 - date range
var progDate2 = new Array(	"",
							"", 
							" - 15th (Friday / Saturday)", 
							"", 
							"", 
							"", 
							"", 
							" (Sunday)", 
							"", 
							"", 
							"", 
							"");

//The programme for 2008-09 - notes
var progSup = new Array(	"",
							"",
							"&nbsp;<sup><small>1</small></sup>",
							"&nbsp;<sup><small>2</small></sup>",
							"",
							"",
							"", 
							"", 
							"",
							"",
							"",
							"");

//The programme for 2008-09 - conductors
var progCond = new Array(
							"Pamela Flanagan",
							"Pamela Flanagan",
							"Mary Tyers",
							"Marion Mullen, Pamela Flanagan",
							"Pamela Flanagan, Marion Mullen",
							"Pamela Flanagan",
							"Pamela Flanagan, Marion Mullen",
							"&nbsp;",
							"Marion Mullen, Pamela Flanagan",
							"Marion Mullen, Pamela Flanagan",
							"Marion Mullen, Pamela Flanagan",
							"&nbsp;"
						);

//The programme for 2008-09 - comments
var progComm = new Array(
							"AGM",
							"(With music 'bring-and-buy' in aid of great bass fund.)",
							"Recorder&nbsp;Weekend",
							"&nbsp;",
							"&nbsp;",
							"&nbsp;",
							"&nbsp;",
							"<A HREF=\"./Playing_day_advance.html\">One-to-a-part day</A>",
							"&nbsp;",
							"&nbsp;",
							"&nbsp;",
							"AGM"
						);

//The programme for 2008-09 - footnotes
var progFootNote = new Array(
							"Replaces the monthly Saturday meeting. Contact Pat Flanagan ((01) 833 7869) for details.",
							"One week early, to accommodate Christmas."
							);

var i;	//control variable for loop
var thisMonthDone = false;

t = new Date();
tcomp = new String(t.getYear() * 10000 + t.getMonth() * 100 + t.getDate())		//Today's date for comparing with programme dates

// document.write("page is " +page);
if ((page == "index.html") | (page == ""))	// Check whether index page or programme page
{
	//Find the date of the next meeting and write it to the front page
	document.write("<h3>The next meeting will be ");
	for (i=0; i<progDate.length; i++)
	{
		d = new Date(progDate[i]);													//Next programme date
		dcomp = new String(d.getYear() * 10000 + d.getMonth() * 100 + d.getDate())	//Format date for comparison
		day=dayofMonth(d.getDate());												//Format date for display
		mon=monthofYear(d.getMonth());
		year=d.getYear();
		year = (year < 1000) ? year + 1900 : year;									// correct the year if returned as offset from 1900

		if (dcomp >= tcomp)
			{
				//Next date
				document.write("on " + day + progDate2[i] + " " + mon + " " + year + ". " + progComm[i] + ".");
				thisMonthDone = true;
				break;
			}
	}
	if (!thisMonthDone)	//All the dates are passed!
	{
		document.write("next year!");
	}
	document.write("</h3>");
}
else
{
	//Generate HTML table for programme of meetings
	document.write("					<TABLE width=\"100%\" BORDER=\"1\" ALIGN=\"CENTER\">");
	
	//Table headers
	document.write("						<TR>");
	document.write("							<TH Width=\"35%\">Date</TH>");
	document.write("							<TH Width=\"35%\">Conductor</TH>");
	document.write("							<TH Width=\"30%\">Meeting</TH>");
	document.write("						</TR>");
	
	//Build HTML table from programme arrays
	for (i=0; i<progDate.length; i++)
	{
		d = new Date(progDate[i]);													//Next programme date
		dcomp = new String(d.getYear() * 10000 + d.getMonth() * 100 + d.getDate())	//Format date for comparison
		day=dayofMonth(d.getDate());												//Format date for display
		mon=monthofYear(d.getMonth());
	
		if (progCond[i] == "")
		{
			//Special entry (no conductor) - merge cells
			if (dcomp < tcomp)
			{
				//Date passed - write it in grey
				document.write("<TR><TD><font color=\"gray\">", mon + " " + day + progDate2[i] + progSup[i] + "</font></TD><TD colspan=\"2\"><font color=\"gray\">" + progComm[i] +"</font></TD></TR>");
			}
			else
				if (thisMonthDone)
				{
				//Future date, not next - normal
				document.write("<TR><TD>", mon + " " + day + progDate2[i] + progSup[i] + "</TD><TD colspan=\"2\">" + progComm[i] +"</TD></TR>");
				}
				else
				{
					//Next date - highlight
					document.write("<TR><TD><strong>", mon + " " + day + progDate2[i] + progSup[i] + "</strong></TD><TD colspan=\"2\"><strong>" + progComm[i] +"</strong></TD></TR>");
					thisMonthDone = true;
				}
		}
		else 
		{
			//Normal entry
			if (dcomp < tcomp)
			{
				//Date passed - write it in grey
				document.write("<TR><TD><font color=\"gray\">", mon + " " + day + progDate2[i] + progSup[i] + "</font></TD><TD><font color=\"gray\">" + progCond[i] + "</font></TD><TD><font color=\"gray\">" + progComm[i] +"</font></TD></TR>");
			}
			else
				if (thisMonthDone)
				{
				//Future date, not next - normal
				document.write("<TR><TD>", mon + " " + day + progDate2[i] + progSup[i] + "</TD><TD>" + progCond[i] + "</TD><TD>" + progComm[i] +"</TD></TR>");
				}
				else
				{
					//Next date - highlight
					document.write("<TR><TD><strong>", mon + " " + day + progDate2[i] + progSup[i] + "</strong></TD><TD><strong>" + progCond[i] + "</strong></TD><TD><strong>" + progComm[i] +"</strong></TD></TR>");
					thisMonthDone = true;
				}
		}
	}
	document.write("					</TABLE>");
	
	// Write footnotes
	thisMonthDone = false;
	var footNoteIndex = 0;
	document.write("									<OL>");
	for (i=0; (i<progDate.length)&(footNoteIndex<progFootNote.length); i++)
	{
		d = new Date(progDate[i]);													//Next programme date
		dcomp = new String(d.getYear() * 10000 + d.getMonth() * 100 + d.getDate())	//Format date for comparison
	
		if (dcomp < tcomp)
		{
			//Date passed - write it in grey
			if (progSup[i] !== "")
			{
				//Footnote entry
				document.write("<font color=\"gray\"><LI> " + progFootNote[footNoteIndex] + "</font>");
				footNoteIndex++;
			}
		}
		else
			if (thisMonthDone)
			{
				//Future date, not next - normal
				if (progSup[i] !== "")
				{
					//Footnote entry
					document.write("<LI> " + progFootNote[footNoteIndex]);
					footNoteIndex++;
				}
			}
			else
			{
				//Next date - highlight
				if (progSup[i] !== "")
				{
					//Footnote entry
					document.write("<LI><strong> " + progFootNote[footNoteIndex] + "</strong>");
					footNoteIndex++;
				}
				thisMonthDone = true;
			}
	}
	document.write("									</OL>")
};
	