// JavaScript functions for Croydon Sisters website

function FormattedDate(d)
// Returns date and time in format: Day dd Month yyyy hh:mm
{
	// d is passed as a string, so we need to re-create a date object:
	var d_o = new Date(d);

	var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
	var months = ["January","February","March","April","May","June","July","August","September","October","November","December"]

	var datestring = days[d_o.getDay()] + " " + d_o.getDate() + " " + months[d_o.getMonth()] + " " +
		d_o.getFullYear() + " " + d_o.getHours() + ":";

	if(d_o.getMinutes() < 10)
		datestring += "0";

	datestring += d_o.getMinutes();

	return datestring;
}

function getNextDateOccurrence(dayofweek,hour,minute)
// Find the next time that it will be the dayofweek, hour and minute specified
{
	var now = new Date();
	var nextOccurrence = new Date();
	var millisecsinday = 86400000;

	if(now.getDay() == dayofweek)
	{
		if(now.getHours() < hour)
		{
			nextOccurrence.setHours(hour);
			nextOccurrence.setMinutes(minute);
			return nextOccurrence;
		}
		else if (now.getHours() == hour)
			if(now.getMinutes < minute)
			{
				nextOccurrence.setMinutes(minute);
				return nextOccurrence;
			}
	}

	// increment the day until it matches required dayofweek
	do
	{
		nextOccurrence.setTime(nextOccurrence.getTime() + millisecsinday);
	}
	while (nextOccurrence.getDay() != dayofweek);

	nextOccurrence.setHours(hour);
	nextOccurrence.setMinutes(minute);

	return nextOccurrence;
}