// Services Functions
//#####################################################
	
// Services Variables

	var nbrYearMonthLinks = -1;
	var linkYearMonth = new Array();

	var nbrSvc = -1;
	var svcDateTimeCtl = new Array();
	var svcDateCtl = new Array();
	var svcDay = new Array();
	var svcInMonth = new Array();
	var svcInMonthN= new Array();
	var svcTime = new Array();
	var svcService = new Array();
	var svcLocation = new Array();
	var svcDetail = new Array();
	var svcLocationText = new Array();
	var svcWeek1 = new Array();
	var svcWeek2 = new Array();
	var svcWeek3 = new Array();
	var svcWeek4 = new Array();
	var svcWeek5 = new Array();

	var serviceMode = 'general';

	var curDateTimeCtl = '';
	var prevDateTimeCtl = '';
	var lastDateCtl = '';

	var serviceDayIndex = new Array('Daily', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');

//------------------------------------------------------------------------------------------------------------
//- SERVICE FUNCTIONS ----------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------
function setServiceMode(vMode)
{
	serviceMode = vMode;
}
//------------------------------------------------------------------------------------------------------------
function loadRegularServices()
{
	var htmlString = '<table width="'+getPageWidth()+'" border="0"><tbody><TR>';
//	htmlString += '<TD valign="top" align="left" class="page-title">Regular Services</TD>';
	htmlString += '<TR><TD>&nbsp;</TD></TR>';
	htmlString += '<TD valign="top" align="right"><A href="servicesmonth.html">Regular Services By Month</A>';
	if ( getSeason() != '' ) {
		htmlString += '<BR><BR><B>See also:</B> <A href="seasonalloc.html">Seasonal Services</A></TD>';
	}
	else {
		htmlString += '</TD>';
	}
	htmlString += '</TR><TR><TD>&nbsp;</TD></TR></tbody></table><BR>';
	document.write(htmlString);

// --------------
// Regular
	startServiceMonth('99', '9999', 'yes');
	writeServiceMonth();

// ---------------------------------------------------

}
//------------------------------------------------------------------------------------------------------------
function addServiceLink(vMonth, vYear)
{
	setThisYM((vYear*100)+(vMonth*1));
	if (isYMgeToday(getThisYM()) > 0) {
		nbrYearMonthLinks += 1;
		linkYearMonth[nbrYearMonthLinks] = vYear+vMonth;
	}
}
//------------------------------------------------------------------------------------------------------------
function writeServiceLink()
{
	var cMonth = '';
	var nMonth = '';
	var cYear = '';
	var yearMonth = '';
	var y = 0;
	var htmlString = '<table width="'+getPageWidth()+'" border="0"><tbody><TR>';
//	htmlString += '<TD colspan=2" valign="top" align="left" class="page-title">Regular Services</TD>';
	htmlString += '<TR><TD>&nbsp;</TD></TR>';
	htmlString += '<TR><TD width="30">&nbsp;</TD>';
	htmlString += '<TD align="left">Select Month &raquo;&raquo;&raquo;<TD align="center">';
	for (var i = 0; i <= nbrYearMonthLinks; i++) {	
		if (linkYearMonth[i] != 'undefined') {	
			yearMonth = linkYearMonth[i];
			cMonth = yearMonth.substring(4,6);
			nMonth = Number(cMonth);
			cYear = yearMonth.substring(0,4);
			if (y != 0) {
				htmlString += '&nbsp;&nbsp;&nbsp;&nbsp;';
			}
			
			htmlString += '<A href="#' + getShortMonth(nMonth-1)+cYear + '">' + getLongMonth(nMonth-1) + ' ' +cYear +'</A>';
			if (y < 3) {
				y += 1;
			} else {
				htmlString += '<BR>';
				y = 0;
			}
		}
	}
	htmlString += '</TD>';
	htmlString += '<TD valign="top" align="right"><A href="services.html">Non-Month Specific Regular Services</A>';
	if ( getSeason() != '' ) {
		htmlString += '<BR><BR><B>See also:</B> <A href="seasonalloc.html">Seasonal Services</A></TD>';
	}
	else {
		htmlString += '</TD>';
	}
	htmlString += '</TR><TR><TD>&nbsp;</TD></TR></tbody></table>';
	document.write(htmlString);
}
//------------------------------------------------------------------------------------------------------------
function startServiceMonth(vMonth, vYear, vReg)
{
	var htmlString = '';
	var nMonth = 0;
	svcDateTimeCtl.length = 0;
	svcDay.length = 0;
	svcInMonth.length = 0;
	svcInMonthN.length = 0;
	svcService.length = 0;
	svcLocation.length = 0;
	svcDetail.length = 0;
	nbrSvc = -1;
	
	setThisYM((vYear*100)+(vMonth*1));
	setThisMonth(vMonth);
	if (isYMgeToday(getThisYM()) > 0) {
	
		setDayInMonth(vMonth, vYear);
		if (vReg == 'yes') {
			addRegularServices();
		}
		
		if (getThisYM() != '999999') {
			nMonth = Number(vMonth);
			htmlString = '<BR><table width="300"><tbody>';
			htmlString += '<TR><TD class="service-month">';
			htmlString += '<A name="'+getShortMonth(nMonth-1)+vYear+'">Services for '+getLongMonth(nMonth-1)+' '+vYear+'</A>';
			htmlString += '</TD></TR></tbody></table>';
		}
		htmlString += '<BR><table width="'+getPageWidth()+'" cellspacing="1" cellpadding="5" border="1"><tbody>';
		htmlString += '<TR align="center" valign="bottom">';
		htmlString += '<TD colspan="2"></TD>';
		htmlString += '<TD class="service-header">Claverham</TD>';
		htmlString += '<TD class="service-header">Cleeve</TD>';
		htmlString += '<TD class="service-header">Kenn</TD>';
		htmlString += '<TD class="service-header">Kingston Seymour</TD>';
		htmlString += '<TD class="service-header">Yatton</TD>';
		document.write(htmlString);
		
	}
}
//------------------------------------------------------------------------------------------------------------
function addService(vDay, vInMonth, vTime, vService, vLocation, vDetail)
{
	if (isYMgeToday(getThisYM()) > 0) {
		nbrSvc += 1;
		
// Change In month to readable text and for sort purposes
		if ( (vInMonth == 'all') || (vInMonth == '0') ) {
			svcInMonth[nbrSvc] = '0';
			svcInMonthN[nbrSvc] = 0;
		}
		else {
			svcInMonthN[nbrSvc] = parseInt(vInMonth);
			if (vInMonth == '1') svcInMonth[nbrSvc] = '1st';
			else if (vInMonth == '2') svcInMonth[nbrSvc] = '2nd';
			else if (vInMonth == '3') svcInMonth[nbrSvc] = '3rd';
			else if (vInMonth == '4') svcInMonth[nbrSvc] = '4th';
			else if (vInMonth == '5') svcInMonth[nbrSvc] = '5th';
		}

// Change Time to 24 hours for sort purposes
		var separator = vTime.indexOf(':');
		var am = vTime.indexOf('am');
		var pm = vTime.indexOf('pm');
		var time24 = '';
		if (separator >= 0) {
			time24 = vTime.substring(0,separator);
			if (pm >= 0) {
				time24 = Number(time24)+12;
			} else if (time24 <= 9) {
				time24 = '0'+time24;
			}
			time24 += vTime.substring(separator+1,vTime.length);
		} else if (am > 0) {
			time24 = vTime.substring(0,am)+'00';
			if (time24 <= 9) {
				time24 = '0'+time24;
			}
		} else if (pm > 0) {
			time24 = Number(vTime.substring(0,pm))+12
			time24 += '00'
		}	
		
// Set sort Control
		svcDateTimeCtl[nbrSvc] = getServiceDayIndex(vDay)+svcInMonth[nbrSvc]+time24+'@'+nbrSvc;
		svcDateCtl[nbrSvc] = getServiceDayIndex(vDay)+svcInMonth[nbrSvc];

// Save Values
		svcDay[nbrSvc] = vDay;
		svcTime[nbrSvc] = vTime;
		svcService[nbrSvc] = vService;
		svcLocation[nbrSvc] = vLocation;
		svcDetail[nbrSvc] = vDetail;
	}
}

//------------------------------------------------------------------------------------------------------------
function writeServiceMonth()
{
	if (isYMgeToday(getThisYM()) > 0) {
		
		var curIndex = -1;
		
		svcDateTimeCtl.sort();
		curDateTimeCtl = '';
		prevDateTimeCtl = '';

// Load Location Columns with spaces
		for (var j=0; j<5; j++) {
			svcLocationText[j] = '&nbsp;';
		}

		for (var i=0; i<=nbrSvc; i++) {
			var separator = svcDateTimeCtl[i].indexOf('@');
			curDateTimeCtl = svcDateTimeCtl[i].substring(0,separator);

			if (curDateTimeCtl != prevDateTimeCtl) {
				if (i > 0) {
					outputService(curIndex);
					for (var j=0; j<5; j++) {
						svcLocationText[j] = '&nbsp;';
					}
				}
			}
			curIndex = svcDateTimeCtl[i].substring(separator+1,svcDateTimeCtl[i].length);
				
			if (svcLocation[curIndex] == 'Claverham') {
				svcLocationText[0] = svcService[curIndex];
				if (svcDetail[curIndex] != '') {
					svcLocationText[0] += '<BR><SPAN class="reference-text">'+svcDetail[curIndex]+'</SPAN>';
				}
			}
			if (svcLocation[curIndex] == 'Cleeve') {
				svcLocationText[1] = svcService[curIndex];
				if (svcDetail[curIndex] != '') {
					svcLocationText[1] += '<BR><SPAN class="reference-text">'+svcDetail[curIndex]+'</SPAN>';
				}
			}
			if (svcLocation[curIndex] == 'Kenn') {
				svcLocationText[2] = svcService[curIndex];
				if (svcDetail[curIndex] != '') {
					svcLocationText[2] += '<BR><SPAN class="reference-text">'+svcDetail[curIndex]+'</SPAN>';
				}
			}
			if (svcLocation[curIndex] == 'Kingston') {
				svcLocationText[3] = svcService[curIndex];
				if (svcDetail[curIndex] != '') {
					svcLocationText[3] += '<BR><SPAN class="reference-text">'+svcDetail[curIndex]+'</SPAN>';
				}
			}
			if (svcLocation[curIndex] == 'Yatton') {
				svcLocationText[4] = svcService[curIndex];
				if (svcDetail[curIndex] != '') {
					svcLocationText[4] += '<BR><SPAN class="reference-text">'+svcDetail[curIndex]+'</SPAN>';
				}
			}
			prevDateTimeCtl = curDateTimeCtl;
		}
		outputService(curIndex);
		var htmlString = '</tbody></table><BR><SPAN class="reference-text">**  Book of Common Prayer 1662</SPAN><BR><BR>';
		document.write(htmlString);
	}
}
//------------------------------------------------------------------------------------------------------------
function outputService(svcIndex)
{
	var thisDate = getDayInMonth(svcInMonthN[svcIndex], svcDay[svcIndex]);
	var outputLine = 1;
	if (getThisYM() != '999999') {
		if ( (svcInMonth[svcIndex] != '0') && ((thisDate == '') || (typeof thisDate == 'undefined')) ) {
			outputLine = 0;
		}
	}

	if (outputLine == 1) {
		var htmlString = '<TR align="center" valign="top"><TD width="120" align="left"';
		if (lastDateCtl != svcDateCtl[svcIndex]) {
			htmlString += ' class="service-day-time-cell">';
			// Daily 
			if ((getServiceDayIndex(svcDay[svcIndex]) == 0)) {
				htmlString += '<SPAN class="service-day">'+svcDay[svcIndex]+'</SPAN>';
			}
			// Not Daily, but all occurences 
			else if (svcInMonth[svcIndex] == '0') {
				htmlString += '<SPAN class="service-day">All '+svcDay[svcIndex]+'s</SPAN>';
			}
			// Not Daily, specific occurence
			else {
				htmlString += '<SPAN class="service-day">'+svcInMonth[svcIndex]+' '+svcDay[svcIndex]+'</SPAN>';
				if ( (thisDate != '') && (thisDate != 'undefined') && (getThisYM() != '999999')) {
					htmlString += '<BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="service-date">'+thisDate+'&nbsp;'+getLongMonth(getThisMonth()-1)+'</SPAN>';
				}
				else {
					outputLine = 1;
				}
			}
		} else {
			htmlString += '>&nbsp</TD>';
		}
		htmlString += '<TD width="60" align="left" class="service-time">';
		if (svcTime[svcIndex] != '') {
			htmlString += svcTime[svcIndex]+'</TD>';
		} else {
			htmlString += '&nbsp</TD>';
		}
		for (var i=0; i<5; i++) {
			htmlString += '<TD class="service-detail">'+svcLocationText[i]+'</TD>';
		}
		htmlString += '</TR>';
	document.write(htmlString);
	}
	lastDateCtl = svcDateCtl[svcIndex];
}
//------------------------------------------------------------------------------------------------------------
function setDayInMonth(vMonth, vYear)
{
	var nbrDaysInMonth = 31;
	if ( (vMonth == '04') || (vMonth == '06') || (vMonth == '09') || (vMonth == '11') ) {
		nbrDaysInMonth = 30;
	}
	else if (vMonth == '02') {
		if (isLeapYear(vYear)) nbrDaysInMonth = 29;
		else nbrDaysInMonth = 28;
	}
	
	var startDate = new Date();
	startDate.setFullYear(vYear);
	startDate.setMonth(vMonth-1);
	startDate.setDate('01');
	var thisDay = startDate.getDay();
	if (thisDay == 0) thisDay = 7;
	var thisWeek = 1;
	var thisCount = 1;
	
	svcWeek1.length = 0;
	svcWeek2.length = 0;
	svcWeek3.length = 0;
	svcWeek4.length = 0;
	svcWeek5.length = 0;
	for (var i=1; i<=nbrDaysInMonth; i++) {
		var dateValue = '';
		if ( (i == 1) || (i == 21) ||(i == 31) ) dateValue = i+'st';
		else if ( (i == 2) || (i == 22) ) dateValue = i+'nd';
		else if ( (i == 3) || (i == 23) ) dateValue = i+'rd';
		else dateValue = i+'th';
		
		switch (thisWeek) {
			case 1:
				svcWeek1[thisDay] = dateValue;
				break;
			case 2:
				svcWeek2[thisDay] = dateValue;
				break;
			case 3:
				svcWeek3[thisDay] = dateValue;
				break;
			case 4:
				svcWeek4[thisDay] = dateValue;
				break;
			case 5:
				svcWeek5[thisDay] = dateValue;
				break;
		}

		if (thisDay == 7) thisDay = 1;
		else thisDay += 1;

		if (thisCount == 7) {
			thisWeek += 1;
			thisCount = 1;
		}
		else thisCount += 1;
		
	}
}

//------------------------------------------------------------------------------------------------------------
function getDayInMonth(vWeek, vDay)
{
	var returnValue = '';
	var dayIndex = 0;
	for (var i=0; i<serviceDayIndex.length; i++) {
		if (serviceDayIndex[i] == vDay) {
			dayIndex = i;
			break;
		}
	}
	switch (vWeek) {
		case 1:
			returnValue = svcWeek1[dayIndex];
			break;
		case 2:
			returnValue = svcWeek2[dayIndex];
			break;
		case 3:
			returnValue = svcWeek3[dayIndex];
			break;
		case 4:
			returnValue = svcWeek4[dayIndex];
			break;
		case 5:
			returnValue = svcWeek5[dayIndex];
			break;
	}
	return returnValue;
}

//------------------------------------------------------------------------------------------------------------
function getServiceDayIndex(inDay)
{
	var returnValue = 0;
	for (var i=0; i<serviceDayIndex.length; i++) {
		if (inDay == serviceDayIndex[i]) {
			returnValue = i;
			break;
		}
	}
	return returnValue;
}

//------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------
