﻿var nMatches=0, nWins=0, nDraws=0, nLosses=0, nFor=0, nAgainst=0;
var bStrings_v_Strings=false;

var monthName = "somemonth";
var yearName = "someyear";
var numberOfDays = 0;       // days in the month
var endDay = 0;             // day of the week the last day of the month falls on 
var numBlankDays = 0;       // number of "blank" days at the start of the month

// Constant array of month names
var months=["monthzero", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

function InThePast(n)
{
	var today = new Date();
	var arrYear = matchArr[n][eCol.cDate].substring(6,10);
	var arrMonth = matchArr[n][eCol.cDate].substring(3,5);
	var arrDay = matchArr[n][eCol.cDate].substring(0,2);

    if(arrYear == String(today.getFullYear()))
    {
        if(arrMonth < ((y = String(today.getMonth()+1)).length < 2 ? "0" + y : y)) return true;
        
        if(arrMonth == ((y = String(today.getMonth()+1)).length < 2 ? "0" + y : y))
        {
            if(arrDay < ((z = String(today.getDate())).length < 2 ? "0" + z : z)) return true;
        }
    }
    else if(arrYear < String(today.getFullYear())) return true;
    
    return false;
}

function GetColour(n, ha)
{
	if(matchArr[n][eCol.cLeague].search(/Biggleswade/)==0) return "bg";
	if(matchArr[n][eCol.cLeague].search(/Friendly/)==0)    return "vl";
	if(matchArr[n][eCol.cLeague].search(/LDBL/)==0)  return "let";
	if(matchArr[n][eCol.cLeague].search(/Fletcher/)==0)    return "let";
	if(ha=="H")
	{
		if(matchArr[n][eCol.cLeague].search(/HABL Medley/)==0) return (((matchArr[n][eCol.cHome].search(/Strings B/)==0)||(matchArr[n][eCol.cHome].search(/Strings 2/)==0))?"mdb":"mda");
		if(matchArr[n][eCol.cLeague].search(/HABL Mens 4/)==0) return (((matchArr[n][eCol.cHome].search(/Strings B/)==0)||(matchArr[n][eCol.cHome].search(/Strings 2/)==0))?"m4b":"m4a");
		if(matchArr[n][eCol.cLeague].search(/SBL/)==0)         return ((matchArr[n][eCol.cHome].search(/Strings B/)==0)?"sb":"sa");
	}
	else
	{
		if(matchArr[n][eCol.cLeague].search(/HABL Medley/)==0) return (((matchArr[n][eCol.cAway].search(/Strings B/)==0)||(matchArr[n][eCol.cAway].search(/Strings 2/)==0))?"mdb":"mda");
		if(matchArr[n][eCol.cLeague].search(/HABL Mens 4/)==0) return (((matchArr[n][eCol.cAway].search(/Strings B/)==0)||(matchArr[n][eCol.cAway].search(/Strings 2/)==0))?"m4b":"m4a");
		if(matchArr[n][eCol.cLeague].search(/SBL/)==0)         return ((matchArr[n][eCol.cAway].search(/Strings B/)==0)?"sb":"sa");
	}
};

function PrintTableHead()
{
	document.write("<table style=\"width: 945px; border-collapse: collapse\" border=\"0\" cellpadding=\"1\">");
};

function PrintColumnHead(doTime, doLeague, doResults)
{
	document.write("<tr><th class=\"center\" style=\"width: 12%\">Date</th>");
	if(doTime) document.write("<th class=\"center\" style=\"width: " + (doResults ? 7 : 6) + "%\">Time</th>");
	if(doLeague) document.write("<th class=\"center\" style=\"width: 26%\">League</th>");
	document.write("<th class=\"center\" colspan=\"3\">Teams</th>");
	if(doResults) document.write("<th class=\"center\" style=\"width: 7%\">W/D/L</th><th class=\"center\" style=\"width: 6%\">F</th><th class=\"center\" style=\"width: 6%\">A</th>");
	document.write("</tr>");
};

function PrintColumnFoot(doTime, doLeague, doResults)
{
	document.write("<tr><td></td>");
	if(doTime)   document.write("<td></td>");
	if(doLeague) document.write("<td></td>");
	document.write("<td style=\"width: " + ((doLeague && doResults) ? 17 : 27) + "%\"></td>");
	document.write("<td style=\"width: 2%\"></td>");
	document.write("<td style=\"width: " + ((doLeague && doResults) ? 17 : 27) + "%\"></td>");
	if(doResults) document.write("<td class=\"right\" colspan=\"3\"><a href=\"#\">Return to top of page</a></td>");
	document.write("</tr></table>");
};

function PrintRowDetails(doTime, doLeague, doResults, n)
{
	document.write("<tr>");
	document.write("<td class=\"ucenter\">");
	if((matchArr[n][eCol.cID].substring(2,6) == "2004")||(matchArr[n][eCol.cID].substring(2,6) == "2005"))
	{
		document.write(matchArr[n][eCol.cDate] + "</td>");
	}
	else
	{
		document.write("<a href=\"javascript:void(0)\" rel=\"" + matchArr[n][eCol.cID] + "\" onclick=\"javascript:displayinwindow(document.getElementById(rel).innerHTML)\" style=\"text-decoration: none; color:#000000;\">" + matchArr[n][eCol.cDate] + "</a></td>");
	}

	if(doTime) document.write("<td class=\"ucenter\">" + matchArr[n][eCol.cTime] + "</td>");
	if(doLeague) document.write("<td class=\"u\">" + matchArr[n][eCol.cLeague] + "</td>");
	document.write("<td " + ((matchArr[n][eCol.cHome].search(/strings/i)==0) ? "style=\"font-weight: bold; text-align: right;\" class=\"" + GetColour(n, "H") + "\">" : "class=\"uright\">") + matchArr[n][eCol.cHome] + "</td>");
	document.write("<td class=\"ucenter\">v</td>");
	document.write("<td " + ((matchArr[n][eCol.cAway].search(/strings/i)==0) ? "style=\"font-weight: bold;\" class=\"" + GetColour(n, "A") + "\">" : "class=\"u\">") + matchArr[n][eCol.cAway] + "</td>");
	if(doResults)
	{
		if((matchArr[n][eCol.cHome].search(/strings/i)==0) && (matchArr[n][eCol.cAway].search(/strings/i)==0))
		{
			document.write("<td class=\"ucenter\">&nbsp;</td>");
			bStrings_v_Strings=true;
		}
		else
		{
			document.write("<td class=\"ucenter\">" + matchArr[n][eCol.cResult] + "</td>");
			switch(matchArr[n][eCol.cResult])
			{
			case "W": nWins++;   break;
			case "D": nDraws++;  break;
			case "L": nLosses++; break;
			}
		}
		document.write("<td class=\"ucenter\">" + matchArr[n][eCol.cFor] + "</td>");
		document.write("<td class=\"ucenter\">" + matchArr[n][eCol.cAgainst] + "</td>");
		
		if((matchArr[n][eCol.cFor] != "")&&(matchArr[n][eCol.cAgainst] != ""))
		{
			nFor+=parseInt(matchArr[n][eCol.cFor], 10);
			nAgainst+=parseInt(matchArr[n][eCol.cAgainst], 10);
		}
	}
	document.write("</tr>");
};

function PrintTableSummary()
{
	document.write("<tr><th class=\"center\" colspan=\"" + matchArr[0].length + "\">");
	document.write("Summary: Played " + nMatches + " (Won " + nWins + " Drawn " + nDraws + " Lost " + nLosses + "), Points For " + nFor + ", Points Against " + nAgainst + "</th></tr>");
	if(bStrings_v_Strings)
	{
		document.write("<tr><td class=\"center\" colspan=\"" + matchArr[0].length + "\">(P might not equal W+D+L as the Strings v Strings matches are not included in this calculation)</td></tr>");
	}
};

function RowIsDuplicate(c)
{
	return ((matchArr[c][eCol.cDate]==matchArr[c-1][eCol.cDate])
	&&      (matchArr[c][eCol.cTime]==matchArr[c-1][eCol.cTime])
	&&      (matchArr[c][eCol.cHome]==matchArr[c-1][eCol.cHome])
	&&      (matchArr[c][eCol.cAway]==matchArr[c-1][eCol.cAway]));
};

function DrawTable(season, league)
{
	var bShowTime=true;
	var bShowLeague=true;
	var bShowResults=true;
	var bEmptyTable=true;

	nMatches=0, nWins=0, nDraws=0, nLosses=0, nFor=0, nAgainst=0;
	bStrings_v_Strings=false;

	if(league!=null)
	{
		bShowLeague=false;
		document.write("<div class=\"w945\"><h1>" + league + "</h1></div>");
	}
	PrintTableHead();
	if((season==2004)||(season==2005)) bShowTime=false;
	PrintColumnHead(bShowTime, bShowLeague, bShowResults);
	for(var c=0; c<(matchArr.length-1); c++)
	{
		if((matchArr[c][eCol.cID].substring(2,6) == season.toString())&&(matchArr[c][eCol.cType]=="M"))
		{
			if((bShowLeague)
			||  ((!bShowLeague) && (matchArr[c][eCol.cLeague].search(league)==0)))
			{
				if((bShowLeague)&&(c>0)&&(RowIsDuplicate(c))) continue;
				PrintRowDetails(bShowTime, bShowLeague, bShowResults, c);
				if((matchArr[c][eCol.cFor] != "")&&(matchArr[c][eCol.cAgainst] != "")) nMatches++;
				bEmptyTable=false;
			}
		}
	}
	if (bEmptyTable) document.write("<tr><td colspan=\"8\">Awaiting fixture list.</td></tr>");
	else PrintTableSummary();
	PrintColumnFoot(bShowTime, bShowLeague, bShowResults);
};

// Determine the number of days in the month and the starting/ending dates
function DrawMonth(inputMonth, inputYear, inputDay)
{
	var x;
	var y;
	var leapYear = false;
	var matchIdx;
	monthName = months[inputMonth];
	yearName = inputYear.toString();
	numBlankDays = ((inputDay - 1) + 7) % 7;
		
	if(inputYear%4 == 0) leapYear = ((inputYear%100 != 0) ? true : ((inputYear%400 == 0) ? true : false));

	switch(inputMonth)
	{
	case 1: case 3: case 5: case 7: case 8: case 10: case 12: numberOfDays = 31; break;
	case 4: case 6: case 9: case 11: numberOfDays = 30; break;
	case 2: numberOfDays = ((leapYear) ? 29 : 28); break;
	}
		
	endDay = (numberOfDays + numBlankDays) % 7;

	//integer to keep track of how many days were added to the month
	cellCount = 0;

	document.write("<table style=\"page-break-before: always; width: 945px;\" border=\"1\" cellpadding=\"3\">");
	document.write("<tr><td class=\"boldcenter\" colspan=\"7\"><a id=\"" + (monthName.substring(0,3)).toLowerCase() + "\">" + monthName + " " + yearName + "</td></tr>");
	document.write("<tr>");
	document.write("<th class=\"center\" style=\"width: 14%\">Monday</th>");
	document.write("<th class=\"center\" style=\"width: 14%\">Tuesday</th>");
	document.write("<th class=\"center\" style=\"width: 14%\">Wednesday</th>");
	document.write("<th class=\"center\" style=\"width: 14%\">Thursday</th>");
	document.write("<th class=\"center\" style=\"width: 14%\">Friday</th>");
	document.write("<th class=\"center\" style=\"width: 15%\">Saturday</th>");
	document.write("<th class=\"center\" style=\"width: 15%\">Sunday</th>");
	document.write("</tr>");
	document.write("<tr class=\"cal\">");
		
	//prints out blank cells needed to start off month calendar
	for(var blanks = 0; blanks < numBlankDays; blanks++)
	{
		document.write("<td class=\"bcal\">&nbsp;</td>");
		cellCount++;
	}
		
	//prints out cells for each day of the month
	for(var days = 1; days <= numberOfDays; days++)
	{
		document.write("<td class=\"cal\">" + days);
		for(matchIdx=0; matchIdx<matchArr.length; matchIdx++)
		{
			if((matchArr[matchIdx][eCol.cDate].substring(6,10) == yearName)
			&&  (matchArr[matchIdx][eCol.cDate].substring(3,5) == ((x = String(inputMonth)).length < 2 ? "0" + x : x))
			&&  (matchArr[matchIdx][eCol.cDate].substring(0,2) == ((y = String(days)).length < 2 ? "0" + y : y)))
			{
				document.write("<br />" + matchArr[matchIdx][eCol.cTime] + " <a href=\"javascript:void(0)\" rel=\"" + matchArr[matchIdx][eCol.cID] + "\" onclick=\"javascript:displayinwindow(document.getElementById(rel).innerHTML)\">");
				if(matchArr[matchIdx][eCol.cType] == "M")
				{
					document.write(matchArr[matchIdx][eCol.cHome] + " v " + matchArr[matchIdx][eCol.cAway] + "</a>");
				}
				else
				{
					document.write(matchArr[matchIdx][eCol.cLeague] + "</a>");
				}
			}
		}
		document.write("</td>");
		if((++cellCount % 7 == 0) && (days < numberOfDays)) document.write("</tr><tr class=\"cal\">");
	}
		
	//prints out blank cells needed to end off month calendar
	while(cellCount++ % 7 != 0) document.write("<td class=\"bcal\">&nbsp;</td>");
	document.write("</tr></table>");
};

function showCalendar(inYear)
{
	var currentMonth = 7;
	var currentYear = inYear;
	var startDate = new Date(currentYear, currentMonth-1, 1);
	var startDay = startDate.getDay();
	var mIndex;

	// Draw shortcut links
	document.write("<div class=\"w945\"><p class=\"center\">");
	for(var index = 0; index < 12; index++)
	{
		if((mIndex = (7 + index)%12) == 0) mIndex = 12;
		document.write("[<a href=\"#" + (months[mIndex].substring(0,3)).toLowerCase() + "\">" + months[mIndex] + "</a>] ");
	}
	document.write("</p></div><br />");

	for(index = 0; index < 12; index++)
	{
		DrawMonth(currentMonth, currentYear, startDay);
		document.write("<div class=\"w945\"><p class=\"right\"><a href=\"#\">Return to top of page</a></p></div><p>&nbsp;</p>");

		if(currentMonth == 12) currentYear++;
		currentMonth = currentMonth%12 + 1;
		startDay = endDay + 1;
	}
};

function CreateDivs(season)
{
	var loop;
	var loop2;
	var venueFound;
	var resultArr = new Array();

	for(loop = 0; loop < (matchArr.length); loop++)
	{
		if(matchArr[loop][eCol.cID].substring(2,6) == season.toString())
		{
			document.write("<div id=\"" + matchArr[loop][eCol.cID] + "\" class=\"balloonstyle\">");
			document.write("<table>");
			if(matchArr[loop][eCol.cType] == "M")
			{
				document.write("<tr><td class=\"bold\" colspan=\"2\">" + matchArr[loop][eCol.cHome] + " v " + matchArr[loop][eCol.cAway] + "</td></tr>");
				document.write("<tr><td class=\"u\" colspan=\"2\">" + matchArr[loop][eCol.cLeague] + "</td></tr>");
				document.write("<tr><td style=\"width: 70px;\">Date:</td><td>" + matchArr[loop][eCol.cDate] + "</td></tr>");
				document.write("<tr><td>Time:</td><td>" + matchArr[loop][eCol.cTime] + "</td></tr>");
				document.write("<tr><td>Venue:</td><td>");
				venueFound=false;
				for(loop2 = 0; loop2 < venueArr.length; loop2++)
				{
					if(venueArr[loop2][0] == matchArr[loop][eCol.cVenue])
					{
						document.write(venueArr[loop2][1]);
						venueFound=true;
						break;
					}
				}
				if(!venueFound) document.write("Not confirmed");
				document.write("</td><td>");
				if(matchArr[loop][eCol.cPlayers]!="") document.write("<tr><td class=\"u\">Players:</td><td class=\"u\">" + matchArr[loop][eCol.cPlayers] + "</td></tr>");

				if(matchArr[loop][eCol.cResult]!="")
				{
					resultArr = (matchArr[loop][eCol.cScores]).split(dLimit);
					if(resultArr.length == 1) document.write("<tr><td class=\"u\" colspan=\"2\">Individual Results not confirmed</td></tr>");
					else
					{
						for(loop2 = 0; loop2 < (resultArr.length-2); loop2+=2) document.write("<tr><td>" + resultArr[loop2] + ":</td><td>" + resultArr[loop2+1] + "</td></tr>");
						document.write("<tr><td class=\"u\">" + resultArr[loop2] + ":</td><td class=\"u\">" + resultArr[loop2+1] + "</td></tr>");
					}
					document.write("<tr><td class=\"bold\" colspan=\"2\">Final Result: " + matchArr[loop][eCol.cHome] + " ");
					if(matchArr[loop][eCol.cHome].search(/Strings/) == 0) document.write(matchArr[loop][eCol.cFor] + " - " + matchArr[loop][eCol.cAgainst]);
					else document.write(matchArr[loop][eCol.cAgainst] + " - " + matchArr[loop][eCol.cFor]);
					document.write(" " + matchArr[loop][eCol.cAway] + "</td></tr>");
				}

				if(matchArr[loop][eCol.cNotes]!="") document.write("<tr><td>Notes:</td><td>" + matchArr[loop][eCol.cNotes] + "</td></tr>");
				document.write("</table></div>");
			}
			else
			{
				document.write("<tr><td class=\"ubold\" colspan=\"2\">" + matchArr[loop][eCol.cLeague] + "</td></tr>");
				document.write("<tr><td>Date:</td><td>" + matchArr[loop][eCol.cDate] + "</td></tr>");
				document.write("<tr><td>Time:</td><td>" + matchArr[loop][eCol.cTime] + "</td></tr>");
				document.write("<tr><td>Venue:</td><td>");
				venueFound=false;
				for(loop2 = 0; loop2 < venueArr.length; loop2++)
				{
					if(venueArr[loop2][0] == matchArr[loop][eCol.cVenue])
					{
						document.write(venueArr[loop2][1]);
						venueFound=true;
						break;
					}
				}
				if(!venueFound) document.write("Not confirmed");
				document.write("</td><td>");
				if(matchArr[loop][eCol.cNotes] != "") document.write("<tr><td>Notes:</td><td>" + matchArr[loop][eCol.cNotes] + "</td></tr>");
				document.write("</table></div>");
			}
		}
	}
};
