// load_xml_doc.js
// contains one borrowed function from w3schools.com
function loadXMLDoc(dname)
{
	var xmlDoc;
	
	// code for IE
	if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc=document.implementation.createDocument("","",null);
	}
	
	else
	{
		alert('Your browser cannot handle this script');
	}
	xmlDoc.async=false;
	xmlDoc.load(dname);
	return(xmlDoc);
}

// Written by SGB, 5-17-07
// helper function to compare dates
// returns the greatest date
// or zero if they're equal
function greatestDate(date1, date2)
{
	if (date1.getTime() > date2.getTime())
		return date1;
	else if (date1.getTime() < date2.getTime())
		return date2;
	else
		return 0;
}

// Written by SGB, 5-17-07
function displayWeekly()
{
	var sYear, sMonth, sDay;
	var today, sDate, eDate;
	var weeksOut = 2; //determines how many weeks out we show sessions
	var displaysLeft = weeksOut;
	// load the xml document
	var weeklyXML = loadXMLDoc("/cmbase/wrap/theme1/text/weeklyCamps.xml");
	
	var sessions = weeklyXML.getElementsByTagName('session');
	var soldOut = weeklyXML.getElementsByTagName('soldOut');
	var core1 = weeklyXML.getElementsByTagName('core1');
	var core2 = weeklyXML.getElementsByTagName('core2');
	var core3 = weeklyXML.getElementsByTagName('core3');
	var core4 = weeklyXML.getElementsByTagName('core4');
	
	// init today
	today = new Date(Date());  
	
	// remove 3 days from the today date,
	// so that it flips to the next week on thursday
	today.setTime(today.getTime() - 259200000);
	
	for(i = 0; i < sessions.length; i++)
	{
		// check the date of the session
		if (displaysLeft > 0)
		{
			// check if the session is sold out
			if (soldOut[i].childNodes[0].nodeValue != "Y")
			{
				// init session variables
				sDate = String(sessions[i].attributes.getNamedItem("date").value);
				sYear = sDate.slice(0, 4);
				sMonth = sDate.slice(4, 6);
				sDay = sDate.slice(6);
				
				sYear = sYear.valueOf();
				sMonth = sMonth.valueOf() - 1; // correcting for zero start
				sDay = sDay.valueOf();
				
				sDate = new Date(sYear, sMonth, sDay, 0, 0, 0, 0);
				
				// calculate the end date of the camp
				// which is just 4 days after the start
				eDate = new Date(sDate);
				eDate.setTime(sDate.getTime() + 345600000);
				
				if (greatestDate(sDate, today) == sDate)
				{
					// write the session info
					document.write("<strong>" + (sDate.getMonth() + 1) + "/" + sDate.getDate() + " - " + (eDate.getMonth() + 1) + "/" + eDate.getDate() + ":</strong><br />");
					document.write(core1[i].childNodes[0].nodeValue + ", " + core2[i].childNodes[0].nodeValue + ", ");
					
					// if there's a 4th core this week
					if (core4[i].childNodes[0].nodeValue != "N/A")
					{
						document.write(core3[i].childNodes[0].nodeValue + ", and " + core4[i].childNodes[0].nodeValue + ".");
					}
					// if there's not
					else
					{
						document.write("and " + core3[i].childNodes[0].nodeValue + ".");
					}
					
					document.write("<br />");
					// subtract one from displaysLeft
					displaysLeft--;
				}
			}
		}
	}
}