function writeCSV(){
	document.write('\"Subject\",\"Start Date\",\"Start Time\",\"End Date\",\"End Time\",\"All Day Event\",\"Description\",\"Location\"<br>');
	//x=1;
  	for(x=1; x < 5; x++) {
		alert(x);
  		document.write('\"' + aryEventName[x] +  '\"' + ',' +  '\"' + writeCSVDate(aryEventDate[x]) +  '\"' + ',' +  '\"' + aryStartTime[x] +  '\"' + ',' +  '\"' + writeCSVDate(aryEndDate[x]) +  '\"' + ', ' +  '\"' + aryEndTime[x] +  '\"' + ', ' +  '\"' + aryBlnAllDay[x] +  '\"' + ', ' +  '\"' + aryEventText[x] +  '\"' + ',' +  '\"' + aryLocation[x] +  '\"');
  		document.write('<br>');
  	}
}

function writeCSVDate(strDate) {
	var strYear = strDate.substr(0, 4);
	var strMonth = strDate.substr(5, 2);
	var strDay = strDate.substr(8, 2);
	
	return  strYear + '/' + strMonth + '/' + strDay;
}

// This can handle n simltaneous events provided the divs are in the html
// Both numbers are passed by the page. The 2nd and higher calls take the 'i' from the preceeding call

function changeHeadline(aryEventDate, aryEventName, aryEventText, aryEventDisplayDays, intPassNumber) {
	var blnShowDebugg = false;
	
	var strDisplayThis = 'In ChangeHeadlines, \n';
	if (intPassNumber < 1 ) {blnShowDebugg = true; intPassNumber = intPassNumber *- 1;} 
	
	for (i = 1 ; i < aryEventDate.length ; i++) {
        strDisplayThis = strDisplayThis + '\n intPassNumber = ' + intPassNumber;
        strDisplayThis = strDisplayThis + '\n aryEventName[' + i + '] = ' + aryEventName[i];	
        //strDisplayThis = strDisplayThis + '\n aryEventText[' + i + '] = ' + aryEventText[i];	
        strDisplayThis = strDisplayThis + '\n aryEventDisplayDays[' + i + '] = ' + aryEventDisplayDays[i];	
        strDisplayThis = strDisplayThis + ' and aryEventDate[' + i + '] = ' + aryEventDate[i];
		strDisplayThis = strDisplayThis + '\n inDateRange [' + i + '] = ' + inDateRange(aryEventDisplayDays[i], aryEventDate, i);
        var strEventText = '<p><h3>' + aryEventName[i] + '</h3></p><p>' + aryEventText[i] + '</p>';
		//var intSaveI = i;
		//if (blnShowDebugg) {intSaveI = intSaveI * -1;}
		if (inDateRange(aryEventDisplayDays[i], aryEventDate, i, blnShowDebugg)) {

		   //write the new info in
		   if (intPassNumber == 1) {
		   	   	//alert('intPassNumber = 1');
		   	   	SpecialDayText1.innerHTML = strEventText;
		  	   	SpecialDayHR1.innerHTML = "<hr>";
		   	   	//show the div
		   	   	SpecialDayEntry1.style.visibility = 'visible';
		   	   	SpecialDayHR1.style.visibility = 'visible';
			} else if (intPassNumber == 2) {
		   	  	//alert('intPassNumber = 2');
		   	  	SpecialDayText2.innerHTML = strEventText;
		   	 	SpecialDayHR2.innerHTML = "<hr>";
		   	  	//show the div
		   	  	SpecialDayEntry2.style.visibility = 'visible';
		      		SpecialDayHR2.style.visibility = 'visible';
			} else if (intPassNumber == 3) {
		   	  	//alert('intPassNumber = 3');
		   	  	SpecialDayText3.innerHTML = strEventText;
		   	  	SpecialDayHR3.innerHTML = "<hr>";
		   	  	//show the div
		   	  	SpecialDayEntry3.style.visibility = 'visible';
		      		SpecialDayHR3.style.visibility = 'visible';
			} else if (intPassNumber == 4) {
		   	  	//alert('intPassNumber = 4');
		   	  	SpecialDayText4.innerHTML = strEventText;
		   	  	SpecialDayHR4.innerHTML = "<hr>";
		   	  	//show the div
		   	  	SpecialDayEntry4.style.visibility = 'visible';
		      		SpecialDayHR4.style.visibility = 'visible';
			} else {
		   	  	//alert('intPassNumber > 4');
		   	  	SpecialDayText5.innerHTML = strEventText;
		   	  	SpecialDayHR5.innerHTML = "<hr>";
		   	  	//show the div
		   	  	SpecialDayEntry5.style.visibility = 'visible';
		      		SpecialDayHR5.style.visibility = 'visible';
			}
		   intPassNumber = intPassNumber + 1;
		}
	}

	strDisplayThis = strDisplayThis + '\n i = ' + i;
	strDisplayThis = strDisplayThis + '\n intPassNumber = ' + intPassNumber;
	// turn on for debugging
	if (blnShowDebugg) {alert(strDisplayThis);}

	//return the index of the item found to update the intPassNumber
	return intPassNumber;
}

//checks to see if an event is in the date range
// -ive i gets the debug stuff

function inDateRange(intDaysBeforeEvent, aryEventDate, i, blnShowDebugg) {
	var strDisplayThis = 'In inDateRange ' + i + ':'
	var blnInDateRange = false;
	//var blnShowDebugg = false;
	//alert(i);
	//if (i < 0 ) {blnShowDebugg = true; i = i *- 1;} 


	//this is the number of days to go back to have the event show on the front page.
	var objCurrentDate = new Date();
	var objTargetDate = new Date();
	strDisplayThis = strDisplayThis + '\n i = ' + i + ':'
	strDisplayThis = strDisplayThis + '\n intDaysBeforeEvent = ' + intDaysBeforeEvent;
	strDisplayThis = strDisplayThis + '\n aryEventDate[i].substring(5,7) = ' + aryEventDate[i].substring(5,7);
	
	//get dates from the 'list'
	objTargetDate.setFullYear(parseInt(aryEventDate[i].substring(0,4)));
	strDisplayThis = strDisplayThis + '\n Date() = ' + Date();
	strDisplayThis = strDisplayThis + '\n objTargetDate.getFullYear() = ' + objTargetDate.getFullYear();
	strDisplayThis = strDisplayThis + '\n objCurrentDate.getFullYear() = ' + objCurrentDate.getFullYear();

	//ADJUSTS FOR OFF BY months
	objTargetDate.setMonth(returnParsedDates(aryEventDate[i], 5, 7)-1);
	strDisplayThis = strDisplayThis + '\n correct? objTargetDate.getMonth() = ' + objTargetDate.getMonth();

	objTargetDate.setDate(aryEventDate[i].substring(8,10));

	strDisplayThis = strDisplayThis + '\n objTargetDate.getDate() = ' + objTargetDate.getDate();
	strDisplayThis = strDisplayThis + '\n objCurrentDate.getDate() = ' + objCurrentDate.getDate();
	
	strDisplayThis = strDisplayThis + '\n objCurrentDate.getMonth() = ' + objCurrentDate.getMonth();

	//   /1000/60/60/24 reduces milliseconds/minutes/hours/days 
	strDisplayThis = strDisplayThis + '\n objCurrentDate.valueOf() = ' + objCurrentDate.valueOf();
	strDisplayThis = strDisplayThis + '\n objTargetDate.valueOf() = ' + objTargetDate.valueOf();
	strDisplayThis = strDisplayThis + '\n objTargetDate - objTargetDate = ' + (objCurrentDate.valueOf() - objTargetDate.valueOf());
	strDisplayThis = strDisplayThis + '\n objCurrentDate.valueOf() = ' + objCurrentDate.valueOf()/1000/60/60/24;
	strDisplayThis = strDisplayThis + '\n objTargetDate.valueOf() = ' + objTargetDate.valueOf()/1000/60/60/24;
	var intDifference = (objTargetDate.valueOf() - objCurrentDate.valueOf())/1000/60/60/24;
	strDisplayThis = strDisplayThis + '\n intDifference = ' + intDifference;
	strDisplayThis = strDisplayThis + '\n intDaysBeforeEvent = ' + intDaysBeforeEvent;

	if (intDifference >= 0 && intDifference < intDaysBeforeEvent) {
	   blnInDateRange = true;
	}

  	strDisplayThis = strDisplayThis + '\n blnInDateRange = ' + blnInDateRange;
	if (blnShowDebugg) {alert(strDisplayThis);}

	return blnInDateRange;
}


// calendar functions start here
var strCurrentMonthColor = '#FFDD00';
var strNextMonthColor = '#FFE479';
var strOccupiedColor = '#B4D8BA';
var strVacantColor = 'cornsilk';
var strTodayColor = '#FF9364';
var strSpiritColor = 'cornsilk';
var strNoSchoolColor = 'darkgreen';

var intOutOfMonth = 1;
var intOccupied = 2;
var intVacant = 3;
var intCalenderLength = 34;

function writeCellDate(objElement, intIndex) {
	var strInnerText = '<strong>' + intIndex + '</strong>';
	objElement.innerHTML = strInnerText;
}
function changeInfoNames() {
	//alert(changeInfoNames);
	// allow the constants to set the bg colors, not needed but nice
	var dteToday = new Date();
	var objChangeColor = getObject('today');
	objChangeColor.style.background = strTodayColor;
	objChangeColor.innerHTML = '&nbsp;&nbsp;Today&nbsp;&nbsp;';
	var objChangeColor = getObject('thisMonth');
	objChangeColor.style.background = strCurrentMonthColor;
	//alert(dteToday.getMonth());
	objChangeColor.innerHTML = '&nbsp;&nbsp;' + getMonthName(dteToday.getMonth()) + '&nbsp;&nbsp;';
	var objChangeColor = getObject('nextMonth');
	objChangeColor.style.background = strNextMonthColor;
	objChangeColor.innerHTML = '&nbsp;&nbsp;' + getMonthName(dteToday.getMonth()+1) + '&nbsp;&nbsp;';
	
	//update the month names
	
	
	return 1;
}

function getRcObject(intCellNum) {
	//alert(intCellNum);
	var objRowColumn = getObject('Cell' + (intCellNum));
	return objRowColumn;
}
function loadCalender() {
	//getDay of current year
	var objCurrentDay = new Date(); 
	var intCurrentWeekDay = objCurrentDay.getDay();
	var intCurrentDayDate = objCurrentDay.getDate();
	var intDeltaDays = intCurrentDayDate - intCurrentWeekDay;
	// these get +1 and 2 so they work with the listings which start with 1
	var intCurrentMonth = objCurrentDay.getMonth()+1;
	var intNextMonth = objCurrentDay.getMonth()+2;
	var intCurrentYear = objCurrentDay.getYear();
	var aryDates = new Array();
	
	//set it to R1CN
	var objFirstDay = getRcObject(intCurrentWeekDay);
	writeCellDate(objFirstDay, intCurrentDayDate);
	aryDates[intCurrentWeekDay] = intCurrentDayDate;
	
	//alert('first day');
	//add the rest of the month's days
	var intLastCellUsed = 0;
	// The + 2 is for fixing off by one
	alert(getNumDaysInMonth(objCurrentDay.getMonth()));
	//alert(intCurrentWeekDay);
	for(i = (intCurrentWeekDay); i <= getNumDaysInMonth(objCurrentDay.getMonth())-intCurrentDayDate+2; i++) {
		objDay = getRcObject(i);
		var strID = objDay.id;
		intLastCellUsed = strID.substr(4);
		writeCellDate(objDay, i+intDeltaDays);
		objDay.style.background = strCurrentMonthColor;
		aryDates[i] = i+intDeltaDays;
		alert(i);
	}
	alert(i+intDeltaDays);
	
	//alert('now to end of current month');
	//fill in month's first days
	for(i = parseInt(intLastCellUsed); i >= 0; i--) {
		objDay = getRcObject(i);
		var strID = objDay.id;
		writeCellDate(objDay, i+intDeltaDays);
		objDay.style.background = strCurrentMonthColor;
		aryDates[i] = i+intDeltaDays;
	}
	//alert('now to begin current month');
	
	/*get the month's special days and fill in 
		loop thru and match the date/day. no need to ask if special
		color the day
	*/

	//fill in the next month's days
	var intDate = 1;
	
	for(i = parseInt(intLastCellUsed)+1; i <= intCalenderLength; i++) {
		var objDay = getRcObject(i);
		objDay.style.background = strNextMonthColor;
		writeCellDate(objDay, intDate);
		aryDates[i] = intDate;
		intDate++;
	}
	//alert('now next month');
	
	//get the month's special days and fill in 
	//alert('before loop');
	for(i=0; i <= aryDates.length; i++) {
		//does intCurrentMonth match the main list item?
		//does aryDates[i] match the main list item?
		// to do - fix for when year is not same, January
		var dteTestThisNow = intCurrentYear + ',' + convertOneDigitToTwoString(intCurrentMonth) + ',' + convertOneDigitToTwoString(aryDates[i]-1);
		var dteTestThisNext = intCurrentYear + ',' + convertOneDigitToTwoString(intNextMonth) + ',' + convertOneDigitToTwoString(aryDates[i]-1);
		for(x=0; x <= aryEventDate.length; x++) {
			if (dteTestThisNow == aryEventDate[x] || dteTestThisNext == aryEventDate[x]) {
		   		 //Write that sucker!
				 //kludge - -1 here moves the display cells back one to the correct place
		   		 var objCell = getObject('Cell' + (i-1));
		   		 var strDisplay = '<strong>' + (aryDates[i-1]) + '</strong> <br>';
		   		 strDisplay = strDisplay + '\n' + aryEventName[x] + '';
		   		 //strDisplay = strDisplay + '  ' + aryEventText[x];
		   		 objCell.title = aryEventName[x];
		   		 objCell.innerHTML = strDisplay;
		   		 objCell.style.background = strOccupiedColor;
				 //alert(strDisplay);
				 break;
			}
		} 
		 
	}
	//alert('special days');
	
	var objFirstDay = getRcObject(intCurrentWeekDay);
	objFirstDay.style.background = strTodayColor;
}
