//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #
//
//  w66-charter.js
//
//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


// Function to prime the date fields of the Charter Form. It will accept dates and populate
// the form with those dates, or it will populate based on the present date.


function NewEventPrimeDate( DateStart , DateEnd ) {

	var EventFutureYearRange = 3;
	var MonthNames = new Array( "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" );
	var OrderedMonthNames = new Array();
	var Today = new Date();
	var CurMonth = Today.getMonth();
	var CurYear = Today.getFullYear();
	var FromStart = 0;
	var DSFY;  // date start focus year
	var DSFM;  // date start focus month
	var DSFD;  // date start focus day
	var DEFY;  // date end focus year
	var DEFM;  // date end focus month
	var DEFD;  // date end focus day

	// Prime the month choices, starting with the current month.

	for( var i = 0 ; i < 12 ; i++ ) {
		if( MonthNames[i + CurMonth] ) {
			document.charter.eventstartmonth.options[i]  = new Option( MonthNames[ i + CurMonth ] , ( i + CurMonth ) , false , false );
			document.charter.eventendmonth.options[i]    = new Option( MonthNames[ i + CurMonth ] , ( i + CurMonth ) , false , false );
		} else {
			document.charter.eventstartmonth.options[i]  = new Option( MonthNames[ FromStart ] , ( FromStart ) , false , false );
			document.charter.eventendmonth.options[i]    = new Option( MonthNames[ FromStart ] , ( FromStart ) , false , false );
			FromStart++;
		}
	}


	// Prime the day. Will be overridden if the date is specified in the function call.

	document.charter.eventstartday.selectedIndex = ( Today.getDate() - 1 );
	document.charter.eventendday.selectedIndex = ( Today.getDate() - 1 );

	// Prime the year, starting with current and stretching into the future based on "EventFutureYearRange" value.

	for( var i = 0 ; i < EventFutureYearRange ; i++ ) {
		document.charter.eventstartyear.options[i]  = new Option( ( CurYear + i ) , ( CurYear + i ) , false , false );
		document.charter.eventendyear.options[i]    = new Option( ( CurYear + i ) , ( CurYear + i ) , false , false );
	}

	// If the DateStart is specified, go ahead and prime it with the date that was sent along.

	if( DateStart ) {

		DSFY = YearFromDate( DateStart );
		DSFM = MonthFromDate( DateStart );
		DSFD = DayFromDate( DateStart );

		for( var dsfm = 0 ; dsfm < document.charter.eventstartmonth.length ; dsfm++ ) {
			if( document.charter.eventstartmonth[dsfm].text == DSFM ) {
				document.charter.eventstartmonth.selectedIndex = dsfm;
			}
		}

		for( var dsfd = 0 ; dsfd < document.charter.eventstartday.length ; dsfd++ ) {
			if( document.charter.eventstartday[dsfd].text == DSFD ) {
				document.charter.eventstartday.selectedIndex = dsfd;
			}
		}

		document.charter.eventstartday.selectedIndex = ( DSFD - 1 );

		for( var dsfy = 0 ; dsfy < document.charter.eventstartyear.length ; dsfy++ ) {
			if( document.charter.eventstartyear[dsfy].text == DSFY ) {
				document.charter.eventstartyear.selectedIndex = dsfy;
			}
		}
	}


	// If the DateEnd is specified, go ahead and prime it with the date that was sent along.

	if( DateEnd ) {
		DEFY = YearFromDate( DateEnd );
		DEFM = MonthFromDate( DateEnd );
		DEFD = DayFromDate( DateEnd );

		for( var defm = 0 ; defm < document.charter.eventendmonth.length ; defm++ ) {
			if( document.charter.eventendmonth[defm].text == DEFM ) {
				document.charter.eventendmonth.selectedIndex = defm;
			}
		}

		document.charter.eventendday.selectedIndex = ( DEFD - 1 );

		for( var defy = 0 ; defy < document.charter.eventendyear.length ; defy++ ) {
			if( document.charter.eventendyear[defy].text == DEFY ) {
				document.charter.eventendyear.selectedIndex = defy;
			}
		}
	}

	if( navigator.appName == "Microsoft Internet Explorer" && parseInt( navigator.appVersion ) >= 4 ) {
		var DayNames = new Array( "sunday" , "monday" , "tuesday" , "wednesday" , "thursday" , "friday" , "saturday" );

		var StartDate = new Date();
		var EndDate = new Date();

		StartDate.setFullYear( document.charter.eventstartyear.value  );
		StartDate.setMonth(    document.charter.eventstartmonth.value );
		StartDate.setDate(     document.charter.eventstartday.value   );

		EndDate.setFullYear( document.charter.eventendyear.value   );
		EndDate.setMonth(    document.charter.eventendmonth.value );
		EndDate.setDate(     document.charter.eventendday.value   );

		document.all.startdow.innerText = DayNames[ StartDate.getDay() ];
		document.all.enddow.innerText = DayNames[ EndDate.getDay() ];

	} else {
		// Netscape won't handle innerText or other groovy parts of the DOM.
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function UpdateDOW() {
	if( navigator.appName == "Microsoft Internet Explorer" && parseInt( navigator.appVersion ) >= 4 ) {
		var DayNames = new Array( "sunday" , "monday" , "tuesday" , "wednesday" , "thursday" , "friday" , "saturday" );

		var StartDate = new Date();
		var EndDate = new Date();
		var RemoveDate = new Date();

		StartDate.setFullYear( document.charter.eventstartyear.value  );
		StartDate.setMonth   ( document.charter.eventstartmonth.value );
		StartDate.setDate    ( document.charter.eventstartday.value   );

		EndDate.setFullYear( document.charter.eventendyear.value   );
		EndDate.setMonth   ( document.charter.eventendmonth.value );
		EndDate.setDate    ( document.charter.eventendday.value   );

		document.all.startdow.innerText = DayNames[ StartDate.getDay() ];
		document.all.enddow.innerText = DayNames[ EndDate.getDay() ];

	} else {
		// Netscape is behind the times, sadly enough.
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function ValidateEventForm() {
	var Mesg = "";
	var Today = new Date();

	var TodayParsed = ZeroPad( "" + Today.getFullYear() , 4 );
	TodayParsed += ZeroPad( "" + ( parseInt( Today.getMonth() ) + 1 ) , 2 );
	TodayParsed += ZeroPad( "" + Today.getDate() , 2 );

	if( ! document.charter.headline.value ) {
		Mesg += "    No event headline was entered.    \n\n";
	}

	var EventStart = ZeroPad( "" + document.charter.eventstartyear.value , 4 );
	EventStart += ZeroPad( "" + ( parseInt( document.charter.eventstartmonth.value ) + 1 ) , 2 );
	EventStart += ZeroPad( "" + document.charter.eventstartday.value , 2 );

	var EventEnd = ZeroPad( "" + document.charter.eventendyear.value , 4 );
	EventEnd += ZeroPad( "" + ( parseInt( document.charter.eventendmonth.value ) + 1 ) , 2 );
	EventEnd += ZeroPad( "" + document.charter.eventendday.value , 2 );

	if( ( TodayParsed > EventStart ) || ( TodayParsed > EventEnd ) || ( TodayParsed > EventRemove ) ) {
		Mesg += "    Make sure all the dates are today or later.    \n\n";
	}

	if( ( EventStart <= EventEnd ) ) {
		// good.
	} else {
		Mesg += "    The dates are not correct.    \n\n";
		Mesg += "    Please make sure that the dates    \n";
		Mesg += "    entered are in chronological order.    \n";
	}


	//; --------------------------------------------------------------------

	var sty = ( document.charter.eventstartyear.value );
	var stm = ( document.charter.eventstartmonth.value );
	var std = ( document.charter.eventstartday.selectedIndex + 1 );
	var stdate = new Date( parseFloat( sty ) , parseFloat( stm ) , parseFloat( std ) , 0 , 0 , 0 , 0 );
	var Vs = 1;

	var eny = ( document.charter.eventendyear.value );
	var enm = ( document.charter.eventendmonth.value );
	var end = ( document.charter.eventendday.selectedIndex + 1 );
	var endate = new Date( parseFloat( eny ) , parseFloat( enm ) , parseFloat( end ) , 0 , 0 , 0 , 0 );
	var Ve = 1;

	if( parseFloat( std ) != stdate.getDate() ) {
		Vs = 0;
	}
	if( parseFloat( stm ) != stdate.getMonth() ) {
		Vs = 0;
	}
	if( parseFloat( end ) != endate.getDate() ) {
		Ve = 0;
	}
	if( parseFloat( enm ) != endate.getMonth() ) {
		Ve = 0;
	}
	if( Vs < 1 ) {
		Mesg += "    Start date is not valid. Please check a calendar.    \n\n";
	}
	if( Ve < 1 ) {
		Mesg += "    End date is not valid. Please check a calendar.    \n\n";
	}

	//; --------------------------------------------------------------------

	if( Mesg == "" ) {
		return true;
	} else {
		Mesg = "    ----------------------------------------------    \n\n    This event cannot be submitted.    \n\n" + Mesg;
		Mesg += "    ----------------------------------------------    \n\n";
		alert( Mesg );
		return false;
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


// Return just the year from the date sent in format: 2002-01-12 00:00:00

function YearFromDate( DateString ) {
	var EventDatePattern = /^(\d+)-(\d+)-(\d+)/;


	return DateString.match( EventDatePattern )[1];
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


// Return the name of the month from a date sent in format: 2002-01-12 00:00:00

function MonthFromDate( DateString ) {
	var EventDatePattern = /^(\d+)-(\d+)-(\d+)/;
	var MonthNum = DateString.match( EventDatePattern )[2];
	if     ( MonthNum ==  1 ) { return "January"  ; }
	else if( MonthNum ==  2 ) { return "February" ; }
	else if( MonthNum ==  3 ) { return "March"    ; }
	else if( MonthNum ==  4 ) { return "April"    ; }
	else if( MonthNum ==  5 ) { return "May"      ; }
	else if( MonthNum ==  6 ) { return "June"     ; }
	else if( MonthNum ==  7 ) { return "July"     ; }
	else if( MonthNum ==  8 ) { return "August"   ; }
	else if( MonthNum ==  9 ) { return "September"; }
	else if( MonthNum == 10 ) { return "October"  ; }
	else if( MonthNum == 11 ) { return "November" ; }
	else if( MonthNum == 12 ) { return "December" ; }
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


// Return just the day from the date sent in the format: 2002-01-12 00:00:00

function DayFromDate( DateString ) {
	var EventDatePattern = /^(\d+)-(\d+)-(\d+)/;
	return ( DateString.match( EventDatePattern )[3] );
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function VFCharter() {
	var Mesg = "";
	var Today = new Date();
	var GoodEmail = 1;
	var GoodPhone = 1;

	// validate that there's a name.

	if( ! document.charter.name.value ) {
		Mesg += "      + NAME field is empty          \n";
	}

	// validate that there's a phone number.

	if( ! document.charter.phone.value ) {
		GoodPhone = 0;
	}

	// validate that if there's an email entered that it looks valid...

	if( ( ! document.charter.email.value ) || ( ! document.charter.email.value.match( /^[\w\.-_]+@[^\.][\w\.]+$/ ) ) | ( ! document.charter.email.value.match( /\.[a-zA-Z]{2,}$/ ) ) ) {
		GoodEmail = 0;
	}

	if( GoodPhone + GoodEmail == 0 ) {
		Mesg += "      + PHONE and EMAIL fields can't BOTH be empty          \n";
	}

	if( ! document.charter.request.value ) {
		Mesg += "      + TRIP DETAILS field is empty          \n";
	}

	var TodayParsed = ZeroPad( "" + Today.getFullYear() , 4 );
	TodayParsed += ZeroPad( "" + ( parseInt( Today.getMonth() ) + 1 ) , 2 );
	TodayParsed += ZeroPad( "" + Today.getDate() , 2 );

	var EventStart = ZeroPad( "" + document.charter.eventstartyear.value , 4 );
	EventStart += ZeroPad( "" + ( parseInt( document.charter.eventstartmonth.value ) + 1 ) , 2 );
	EventStart += ZeroPad( "" + document.charter.eventstartday.value , 2 );

	var EventEnd = ZeroPad( "" + document.charter.eventendyear.value , 4 );
	EventEnd += ZeroPad( "" + ( parseInt( document.charter.eventendmonth.value ) + 1 ) , 2 );
	EventEnd += ZeroPad( "" + document.charter.eventendday.value , 2 );

	if( ( EventStart <= EventEnd ) ) {
		// good.
	} else {
		Mesg += "    The dates are not correct.    \n\n";
		Mesg += "    Please make sure that the dates    \n";
		Mesg += "    entered are in chronological order.    \n";
	}

	//; --------------------------------------------------------------------

	var sty = ( document.charter.eventstartyear.value );
	var stm = ( document.charter.eventstartmonth.value );
	var std = ( document.charter.eventstartday.selectedIndex + 1 );
	var stdate = new Date( parseFloat( sty ) , parseFloat( stm ) , parseFloat( std ) , 0 , 0 , 0 , 0 );
	var Vs = 1;

	var eny = ( document.charter.eventendyear.value );
	var enm = ( document.charter.eventendmonth.value );
	var end = ( document.charter.eventendday.selectedIndex + 1 );
	var endate = new Date( parseFloat( eny ) , parseFloat( enm ) , parseFloat( end ) , 0 , 0 , 0 , 0 );
	var Ve = 1;

	if( parseFloat( std ) != stdate.getDate() ) {
		Vs = 0;
	}
	if( parseFloat( stm ) != stdate.getMonth() ) {
		Vs = 0;
	}
	if( parseFloat( end ) != endate.getDate() ) {
		Ve = 0;
	}
	if( parseFloat( enm ) != endate.getMonth() ) {
		Ve = 0;
	}
	if( Vs < 1 ) {
		Mesg += "    Start date is not valid. Please check a calendar.    \n\n";
	}
	if( Ve < 1 ) {
		Mesg += "    End date is not valid. Please check a calendar.    \n\n";
	}

	//; --------------------------------------------------------------------

	if( Mesg == "" ) {
		return true;
	} else {
		Mesg = "    ----------------------------------------------    \n\n    This charter cannot be submitted.    \n\n" + Mesg;
		Mesg += "    ----------------------------------------------    \n\n";
		alert( Mesg );
		return false;
	}
}

//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #
//
//  Function to validate the charter content.

function VFCharterContent() {
	var Mesg = "";
	if( ! document.charter.headline.value ) {
		Mesg += "      + HEADLINE field is empty          \n";
	}
	if( ! document.charter.body.value ) {
		Mesg += "      + CHARTER PAGE BODY field is empty          \n";
	}
	if( ! document.charter.guest_email_body.value ) {
		Mesg += "      + EMAIL TO GUEST field is empty          \n";
	}
	if( Mesg ) {
		alert( "\n\n    The form cannot be submitted:          \n\n" + Mesg + "\n\n" );
		return false;
	} else {
		return true;
	}
}

//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function VFCharterPublic() {
	var DF = document.charter;
	var Mesg = "";
	var GoodEmail = 1;
	var GoodPhone = 1;
	var Today = new Date();

	// validate that there's a name.
	if( ! trim( DF.name.value ) ) { Mesg += "      Please fill in the NAME field.          \n"; }

	// validate that there's a phone number.
	if( ! trim( DF.phone.value ) ) { GoodPhone = 0; }

	// validate that if there's an email entered that it looks valid...
	var EMAIL = trim( DF.email.value );
	if( ( ! EMAIL.match( /^[\w\.-_]+@[^\.][\w\.]+$/ ) ) | ( ! EMAIL.match( /\.[a-zA-Z]{2,}$/ ) ) ) { GoodEmail = 0; }

	if( GoodPhone + GoodEmail == 0 ) { Mesg += "      Please fill in either the PHONE or EMAIL field.          \n"; }

	if( ! trim( DF.request.value ) ) { Mesg += "      Please fill in the TRIP DETAILS area.          \n"; }

	var TodayParsed = ZeroPad( "" + Today.getFullYear() , 4 );
	TodayParsed += ZeroPad( "" + ( parseInt( Today.getMonth() ) + 1 ) , 2 );
	TodayParsed += ZeroPad( "" + Today.getDate() , 2 );

	var EventStart = ZeroPad( "" + DF.eventstartyear.value , 4 );
	EventStart += ZeroPad( "" + ( parseInt( DF.eventstartmonth.value ) + 1 ) , 2 );
	EventStart += ZeroPad( "" + DF.eventstartday.value , 2 );

	var EventEnd = ZeroPad( "" + DF.eventendyear.value , 4 );
	EventEnd += ZeroPad( "" + ( parseInt( DF.eventendmonth.value ) + 1 ) , 2 );
	EventEnd += ZeroPad( "" + DF.eventendday.value , 2 );

	if( ( EventStart <= EventEnd ) ) {
		// good.
	} else {
		Mesg += "     The DEPARTURE and/or RETURN DATES are not correct.    \n\n";
		Mesg += "     Please make sure that the dates    \n";
		Mesg += "     entered are in chronological order.    \n";
	}

	//; --------------------------------------------------------------------

	var sty = ( DF.eventstartyear.value );
	var stm = ( DF.eventstartmonth.value );
	var std = ( DF.eventstartday.selectedIndex + 1 );
	var stdate = new Date( parseFloat( sty ) , parseFloat( stm ) , parseFloat( std ) , 0 , 0 , 0 , 0 );
	var Vs = 1;

	var eny = ( DF.eventendyear.value );
	var enm = ( DF.eventendmonth.value );
	var end = ( DF.eventendday.selectedIndex + 1 );
	var endate = new Date( parseFloat( eny ) , parseFloat( enm ) , parseFloat( end ) , 0 , 0 , 0 , 0 );
	var Ve = 1;

	if( parseFloat( std ) != stdate.getDate()  ) { Vs = 0; }
	if( parseFloat( stm ) != stdate.getMonth() ) { Vs = 0; }
	if( parseFloat( end ) != endate.getDate()  ) { Ve = 0; }
	if( parseFloat( enm ) != endate.getMonth() ) { Ve = 0; }
	if( Vs < 1 ) { Mesg += "    Start date is not valid.    \n\n"; }
	if( Ve < 1 ) { Mesg += "    End date is not valid.    \n\n";   }

	if( Mesg ) {
		alert( "\n\n    The form cannot be submitted:          \n\n" + Mesg + "\n\n" );
		return false;
	} else {
		DF.thing.value = "desmond";
		return true;
	}



}

//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #
//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #

