// This file requires formatDate.js

function toRfcDate( jsDate )
{
		if(!jsDate) {
			jsDate = new Date();
		}
		var year = jsDate.getFullYear();
		var month = jsDate.getMonth() + 1;
		if (month < 10) {
			month = "0" + month.toString();
		}
		var date = jsDate.getDate();
		if (date < 10) {
			date = "0" + date.toString();
		}
		// because this is a date picker and not a time picker, we treat time
		// as zero
		return year + "-" + month + "-" + date + "T00:00:00";
}


// returns the date string for the event date embedded the google calendar
// event summary.  The event summary looks like:
//	"When: Nov 11, 2006<br><br>Event Status: confirmed"
//
function getDatesFromSummary( summary, withYear )
{ 
	if ( withYear == null )
		withYear = false;

	str = new String( summary );
	if ( withYear )
	{
		return str.match( /(Sun|Mon|Tue|Wed|Thu|Fri|Sat) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9][0-9]?, [0-9][0-9][0-9][0-9]/g );
	}
	else
		return str.match( /(Sun|Mon|Tue|Wed|Thu|Fri|Sat) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9][0-9]?/g );
}
	

function getWhenFromSummary( summary, singleDayFormat, multiDayFormat, multiMonthFormat, multiYearFormat )
{
	if ( singleDayFormat == null )
		singleDayFormat = "D M j";
	if ( multiDayFormat == null )
		multiDayFormat = new Array( "M j", "j" );
	if ( multiMonthFormat == null )
		multiMonthFormat = new Array( "M j", "M j" );
	if ( multiYearFormat == null )
		multiYearFormat = new Array( "M j", "M j" );

	when = getDatesFromSummary( summary, true );
	from = new Date( when[0] );	

	// when contains one or two dates. 
	if ( when.length < 2 )
		return from.formatDate( singleDayFormat );
	else
	{
		to = new Date( when[1] );
		if ( ( from.getMonth() == to.getMonth() ) &&
                     ( from.getYear() == to.getYear() )
		)
			// the end date is in the same year/month
			return from.formatDate( multiDayFormat[0] ) + "-" + to.formatDate( multiDayFormat[1] );
		else
			// the end date is in a different month.
			// return the ending month as well.
			if ( from.getYear() == to.getYear() )
				// the end date is in the same year
				return from.formatDate( multiMonthFormat[0] ) + "-" + to.formatDate( multiMonthFormat[1] );
			else
				// the end date in a different year.
				return from.formatDate( multiYearFormat[0] ) + "-" + to.formatDate( multiYearFormat[1] );
	}
}
