// JavaScript Document
//Read the holidays.xml file -------------
var xmlDoc=null;
if (window.XMLHttpRequest)
  {
  xmlDoc=new window.XMLHttpRequest();
  xmlDoc.open("GET","xml/holidays.xml",false);
  xmlDoc.send("");
  xmlDoc=xmlDoc.responseXML;
  }
// IE 5 and IE 6
else //if (ActiveXObject("Microsoft.XMLDOM"))
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load("xml/holidays.xml");
  }
// -----------------------------------------

if (xmlDoc!=null) // if the read was sucessfull...

document.write("<table border='0'>");
{
	var counter = 0; //need a counter because we are only going to write out the next seven events based on todays date
	var current = new Date();
	var months = ["zero","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
	var year = current.getFullYear();
	var x=xmlDoc.getElementsByTagName("date"); // total number of events in the xml doc
	
// loop thru the years (this year and next), then months (current thru 12 for this year - all for next year), then days (0 - 31), then all events: 
// this will sort the events by date even if they are out of sequence in the file - then write out the first seven. 

	for (y=year;y<year+2;y++)//loop thru the current and next year
	{
		if (y == year) // if we are in this year of the year loop
		{
			var month = current.getMonth()+1;	//add one becuase js returns zero for january
			for (m=month;m<13;m++) //loop thru this month thru the 12th month
			{
				for (d=0;d<32;d++) //loop thru 31 days
				{
					
					for (e=0;e<x.length;e++) //loop thru the number of events
					{
						if ( (xmlDoc.getElementsByTagName("date")[e].getAttribute("year") == y) || (xmlDoc.getElementsByTagName("date")[e].getAttribute("year") == "all") )
						//if the events year is = y or "all"
						{
							if (xmlDoc.getElementsByTagName("date")[e].getAttribute("month") == m)
							//if the events month is = m
							{
								if (xmlDoc.getElementsByTagName("date")[e].getAttribute("day") == d )
								//if the events day is = d
								{
									if ( (d==0) ||!(m==current.getMonth()+1) || (m==current.getMonth()+1) && (d >= current.getDate() ) )
									// if d is zero then continue OR if m is not the current month then continue OR if m is the current mond AND d is greater than today then continue
									{
										if (counter < 7) // only wirte out seven events
										{
											if (counter < 3) // the first three have special formating
											{
												document.write("<tr><td width=60>");
												document.write("<h1>"+months[(xmlDoc.getElementsByTagName("date")[e].getAttribute("month"))]);
                                        	    if (d !== 0 ) // dont write the zero
                                          		{
                                                	document.write(" "+ xmlDoc.getElementsByTagName("date")[e].getAttribute("day"));
                                           		}
												document.write("</h1></td><td width=180><h1>");
												document.write(xmlDoc.getElementsByTagName("event")[e].getAttribute("name"));
												document.write("</h1>");
												document.write("</td></tr>")
												counter++
											} else  // regular formating for the rest
											{
												document.write("<tr><td>");
												document.write("<p>"+months[(xmlDoc.getElementsByTagName("date")[e].getAttribute("month"))]);
                                            	if (d !== 0)// dont write the zero
                                            	{
                                            		document.write(" "+ xmlDoc.getElementsByTagName("date")[e].getAttribute("day"));
                                            	}
												document.write("</td><td>");
												document.write(xmlDoc.getElementsByTagName("event")[e].getAttribute("name"));
												document.write("</p>");
												document.write("</td></tr>")
												counter++
											}
										}
									}
								}
							}
						}
					}
				}
			}
		} else //  year has advanced 1 and we still have not written out seven events
		{
			var month = 1;
			for (m=month;m<13;m++) // loop thru all the months
			{
				for (d=0;d<32;d++) // loop thru 31 days
				{
					for (e=0;e<x.length;e++) // loop thru all the events
					{
						if (xmlDoc.getElementsByTagName("date")[e].getAttribute("month") == m)
						{
							if (xmlDoc.getElementsByTagName("date")[e].getAttribute("day") == d )
							{
								if (counter < 7) // only writing out seven events
								{
									if (counter < 3) // the first three get special formating
									{
										document.write("<tr><td width=55>");
										document.write("<h1>"+months[(xmlDoc.getElementsByTagName("date")[e].getAttribute("month"))]);
                                        if (d !== 0) // dont write the zero
                                        {
                                            document.write(" "+ xmlDoc.getElementsByTagName("date")[e].getAttribute("day"));
                                        }
										document.write("</h1></td><td width=185><h1>");
										document.write(xmlDoc.getElementsByTagName("event")[e].getAttribute("name"));
										document.write("</h1>");
										document.write("</td></tr>")
										counter++
									} else  // regular formating for the rest
									{
										document.write("<tr><td>");
										document.write("<p>"+months[(xmlDoc.getElementsByTagName("date")[e].getAttribute("month"))]);
                                        if (d !== 0) // dont write the zero
                                        {
                                            document.write(" "+ xmlDoc.getElementsByTagName("date")[e].getAttribute("day"));
										}
                                        document.write("</td><td>");
										document.write(xmlDoc.getElementsByTagName("event")[e].getAttribute("name"));
										document.write("</p>");
										document.write("</td></tr>")
										counter++
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
document.write("</table>");