// Draw calendar

	var aEntries = [];
	var aMonths = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var aDaysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var iCurrentEntry = 0;
	var iDayCount = 0;
	var dNow = new Date();
	var prevCourse =0;
	var dToday = new Date();
	var dThisMonth;
	var iPageID;
	var iLID;


function drawCalendar(sCalendarID, iInstanceID) {

	document.write('<table border="0" cellpadding="3" cellspacing="1" class="cal_t" id="' + sCalendarID + '">');

	document.write('<tr><td class="cal_m" colspan="7">');
				
	document.write('<select name="calmonth" id="calmonth" onchange="newDate(this.value, \'m\');">');
	for (iLoop = 0; iLoop <= 11; iLoop++) {
		document.write('<option value="' + iLoop + '"' + ((iLoop == dToday.month) ? ' selected="selected"' : '') + '>' + aMonths[iLoop] + '</option>');
	}
	document.write('</select>');

	/* document.write('<select name="caldate" id="caldate" onchange="newDate(this.value, \'d\');">');
	for (iLoop = 1; iLoop <= aDaysInMonth[document.getElementById('calmonth').value]; iLoop++) {
		document.write('<option value="' + iLoop + '"' + ((iLoop == dToday.date) ? ' selected="selected"' : '') + '>' + iLoop + '</option>');
	}
	document.write('</select>'); */
	//alert("dToday.month = " + dToday.month);		
	//alert("dToday.year = " + dToday.year);
	//alert("dToday.day = " + dToday.day);

	document.write('<select name="calyear" id="calyear" onchange="newDate(this.value, \'y\');">');
	for (iLoop = (dNow.getFullYear() - 1); iLoop <= (dNow.getFullYear() + 2); iLoop++) {
		document.write('<option value="' + iLoop + '"' + ((iLoop == dToday.year) ? ' selected="selected"' : '') + '>' + iLoop + '</option>');
	}
	document.write('</select>');
	
	document.write('</td></tr>');
	
	// Write out calendar table
	
	document.write('<tr><th>Mo</th><th>Tu</th><th>We</th><th>Th</th><th>Fr</th><th>Sa</th><th>Su</th></tr>');
	document.write('<tr>');
	
	iDayCount = dThisMonth.day;
	
	if (iDayCount == 0) {						
		for (iLoop = 1; iLoop < 7; iLoop++) {
			document.write('<td>&nbsp;</td>');
		}
	} else if (iDayCount < 7) {
		for (iLoop = 1; iLoop < iDayCount; iLoop++) {
			document.write('<td>&nbsp;</td>');
		}
	}
		
	for (iLoop = 1; iLoop <= aDaysInMonth[document.getElementById('calmonth').value]; iLoop++) {
		if ((iDayCount % 7) == 1) { document.write('</tr><tr>'); }
		sID = dToday.year.toString() + addZeros((dToday.month + 1).toString(), 2) + addZeros(iLoop.toString(), 2);
		//alert("sID= " + sID);
		bHasEntry = (aEntries.join(",").indexOf(sID + '_') >= 0);
		//alert(sID + " - " + bHasEntry);
		document.write('<td id="cal_' + sID + '" class="cal_c' + (bHasEntry ? '_on' : '') + '"' + (bHasEntry ? ' onclick="toggleEntry(\'' + sID + '\');""' : '') + ')>' + iLoop + '</td>');
		iDayCount = iDayCount + 1
	}
	
	document.write('</tr>');
	document.write('<tr><td class="cal_f" colspan="7"><div style="position:relative;">');
	
	iPrevMonth = dToday.month - 1;
	iPrevYear = (iPrevMonth < 0) ? dToday.year - 1 : dToday.year;
	if (iPrevMonth < 0) { iPrevMonth += 12; }

	iNextMonth = dToday.month + 1
	iNextYear = (iNextMonth > 11) ? dToday.year + 1 : dToday.year;
	if (iNextMonth > 11) { iNextMonth -= 12; }

	(iPrevYear >= dNow.getFullYear() - 1) ? document.write('<a href="?id=' + iInstanceID + '&y=' + iPrevYear + '&m=' + iPrevMonth + '">&lt;&nbsp;' + aMonths[iPrevMonth] + '</a>') : document.write('&nbsp;');		
	if (iNextYear <= dNow.getFullYear() + 2) { document.write('<div class="cal_f_r"><a href="?id=' + iInstanceID + '&y=' + iNextYear + '&m=' + iNextMonth + '">' + aMonths[iNextMonth] + '&nbsp;&gt;</a></div>'); }
	document.write('</div></td></tr>');
	document.write('</table>');

}



// Miscellaneous functions

function addZeros(sInt, iNo) {
	sInt = sInt.toString();
	while (sInt.length < iNo) {
		sInt = '0' + sInt;
	}
	return sInt;
}

// Set up some initial variables
function initCalendar(id, iYear, iMonth, iDay) {
	
	// Establish what dates we're working with
	//alert("year, month, day: " + iYear + ", " + iMonth + ", " + iDay);
	dToday = new Date(iYear,  iMonth,  iDay);
	dThisMonth = new Date(iYear,  iMonth, 1);
	iPageID = id
	////iLID = iLocationID
	dToday.year = dToday.getFullYear();
	dToday.month = dToday.getMonth();
	dToday.date = dToday.getDate();
	dToday.day = dToday.getDay();
	dThisMonth.day = dThisMonth.getDay();

	
	// Check for leap year
	
	var sDividedYear = (dToday.year / 4).toString();
	aDaysInMonth[1] = (sDividedYear.indexOf('.') >= 0) ? 28 : 29;
	
	var sCentury = dToday.year.toString().charAt(2) + dToday.year.toString().charAt(3);
	if (sCentury == '00') {
		sDividedYear = (dToday.year / 400).toString();
		aDaysInMonth[1] = (sDividedYear.indexOf('.') >= 0) ? 28 : 29;
	}
	

}

// Miscellaneous functions that incorporate CF data

function newDate(iValue, sType) {
	
	var iMonth = document.getElementById('calmonth').value;
	var iYear = document.getElementById('calyear').value;
	
	switch (sType) {
	
		case 'y':
			iYear = iValue;
			break;
			
		case 'm':
			iMonth = iValue;
			break;
			
	}

	window.location.href = '?id=' + iPageID + '&y=' + iYear + '&m=' + iMonth + '#calendar';
	
}

function toggleEntry(sID) {
	
	var id
	
	//Reset title
	document.getElementById('calendarDate').innerHTML = '';
	
	//Reset all previous dates
	for(i=0;i<aEntries.length;i++) {
		
		id = aEntries[i];
		dDate = id.substring(0,id.indexOf('_'));
	
		if (document.getElementById('cal_' + dDate))
		{			
			document.getElementById('cal_' + dDate).className = 'cal_c_on';						
			document.getElementById('entry_' + id).className = 'cal_e';
			document.getElementById('entry_0').className = 'cal_e_on';
		}
		
	}
	
	//Show only valid ones
	for(i=0;i<aEntries.length;i++) {
	
		if (aEntries[i].indexOf(sID) != -1) {
			
			id = aEntries[i];
			dDate = id.substring(0,id.indexOf('_'));
			
			document.getElementById('calendarDate').innerHTML = convertUDate(dDate);

			document.getElementById('cal_' + sID).className = 'cal_c_over';
			document.getElementById('entry_' + id).className = 'cal_e_on';
			document.getElementById('entry_0').className = 'cal_e';
			iCurrentEntry = id;
			
		}
	}
}

function convertUDate(date)
{
	var d = new Date();
	d.setFullYear(date.substring(0,4), date.substring(4,6) - 1, date.substring(6,8));
	return d.toLocaleDateString();
}


