//' Software by DavidRegier.com. Unauthorized use of this code is prohibited
//' This code may be used and modified for use on the MauiConcierge.com domain only
//' Contains code that drives inquire.aspx form

/*

function checkField
function checkForm
function clearField
function getOtherOccasion
function initForm
function morphForm
function verifyEmail

*/

//--------------------------------------------------------------------------------------

function verifyEmail( Form, Email ) {

	Email.value = Email.value.replace( /\s/g, "" );
	var emailRE = /^[\w\.-]+[^\.-]@[\w\.-]+\.[a-zA-Z]{2,}$/;

	EmailArr = Email.value.split( "," );
	for( i in EmailArr ) {
		if( !emailRE.test( EmailArr[ i ] ) ) {
			alert( "Sorry but \"" + EmailArr[ i ] + "\" is an invalid e-mail address.\n\nHere's some examples of valid e-mail addresses:\n1. joe@hotmail.com\n2. sally_smith@aol.com\n3. jim.clark@co.la.ca.us\n4. ron-kent254@yahoo.com\n\nTIP: Valid e-mails contain 1 \"@\" character & end with .com, .net, .us, .ca, etc. etc." );
			Email.focus();
			Form.submit_button.disabled = false;
			return false;
		}
	}

	if( confirm ( "IMPORTANT: Please double check your e-mail address for accuracy\n\n  1) Click OK if \"" + Email.value + "\" is correct & this form will be submitted; OR\n  2) Click Cancel if you need to correct it\n\nIt's important because if you make a typo in your e-mail address we may be unable to contact you." ) )
		return true;
	else {
		Email.focus();
		Form.submit_button.disabled = false;
		return false;
	}
}
//---------------------------------------------------------------------------------------
function clearField( Field ) { if( Field.value == Field.defaultValue ) Field.value = ""; }
function checkField( Field ) { if( Field.value == "" ) Field.value = Field.defaultValue; }
//--------------------------------------------------------------------------------------

function getOtherOccasion( PullDown ) {

	var val = PullDown[ PullDown.selectedIndex ].value;

	if( val == "Other" )
		morphForm( "otherOccasionRow", 1, "Other_Occasion" );
	else
		morphForm( "otherOccasionRow", 0 );
}
//--------------------------------------------------------------------------------------

function morphForm( elem, display, focusField ) {
	document.getElementById( elem ).className = ( display ) ? "on" : "off";
	if( focusField )
		eval( "document.AspForm." + focusField + ".focus()" );
}

//---------------------------------------------------------------------------------------

function initForm() { // Called at end of page & onLoad

	var Form = document.AspForm;


	if( Form.Travel_Dates[ 0 ].checked ) {
		morphForm( 'exactDatesRow', 1 );
		morphForm( 'estimatedDatesRow', 0 );
	}

	if( Form.Travel_Dates[ 1 ].checked ) {
		morphForm( 'exactDatesRow', 0 );
		morphForm( 'estimatedDatesRow', 1 );
	}

	if( Form.Special_Occasion[ Form.Special_Occasion.selectedIndex ].value == "Other" )
		morphForm( 'otherOccasionRow', 1 );
	else
		morphForm( 'otherOccasionRow', 0 );

	if( Form.Accommodations[ 0 ].checked )
		morphForm( 'stayingAtRow', 1 );

	if( Form.Accommodations[ 1 ].checked )
		morphForm( 'stayingAtRow', 0 );

}

//--------------------------------------------------------------------------------------

function checkForm( Form ) {

	Form.submit_button.disabled = true;

	var Name = Form.Name;

	var missingInfo = "";
	var lineNumber = 0;
	var Missing = null;

	Name.value = Name.value.replace( /^\s*|\s*$/g, "" );

	if( Name.value.indexOf( "^_^" ) >= 0 ) {
		var testEmail = "david" + "@" + "DavidRegier" + ".com";
		alert( "Normal recipient is: " + DB.Contact.Email + "\n\nSetting recipient to: " + testEmail );
		Form.Test_Email.value = testEmail;
	}
	else
		Form.Test_Email.value = "";

	if( Name.value == Name.defaultValue ) {
		lineNumber++;
		missingInfo += "\n   " + lineNumber + ". Your Name";
		if( Missing == null ) Missing = Name;
	}

	var Sender_Email = Form.Sender_Email;
	if( Sender_Email.value == Sender_Email.defaultValue ) {
		lineNumber++;
		missingInfo += "\n   " + lineNumber + ". Your Email Address";
		if( Missing == null ) Missing = Sender_Email;
	}

	// Know exact travel dates
	if( Form.Travel_Dates[ 0 ].checked ) {
		if( !Form.Arrival_Date.value ) {
			lineNumber++;
			missingInfo += "\n   " + lineNumber + ". Your Arrival Date";
			if( Missing == null ) Missing = Form.Arrival_Date;
		}
		if( !Form.Departure_Date.value ) {
			lineNumber++;
			missingInfo += "\n   " + lineNumber + ". Your Departure Date";
			if( Missing == null ) Missing = Form.Departure_Date;
		}
	}

	if( missingInfo && Name.value != "^_^" ) {
		alert( "We're sorry but you failed to provide the following information:\n" + missingInfo + "\n\nYou must fill in these missing items to submit this inquiry..." );
		Form.submit_button.disabled = false;
		Missing.focus();
		return false;
	}
	else {

		// Clear non-required default values

		if( Form.Travel_Dates[ 0 ].checked ) // Know exact travel dates
			Form.Estimated_Dates.selectedIndex = 0;

		if( Form.Travel_Dates[ 1 ].checked ) { // Do not know exact travel dates
			Form.Arrival_Date.value = "";
			Form.Departure_Date.value = "";
		}

		// Special Occasion anything but Other
		if( Form.Special_Occasion[ Form.Special_Occasion.selectedIndex ].value != "Other" )
			Form.Other_Occasion.value = "";

		if( Form.Accommodations[ 1 ].checked ) // Not booked accommodations
			Form.Staying_At.value = "";

		var goodEmail = verifyEmail( Form, Form.Sender_Email );
		if( !goodEmail ) return false;

		var gmtOffSetHi = 10; // for Hawaii
		var Now = new Date();
		var gmtOffSetClient = Now.getTimezoneOffset() / 60;
		var timeDiff = gmtOffSetHi - gmtOffSetClient;

		var plural = ( Math.abs( timeDiff ) == 1 ) ? "" : "s"
		var theSign = ( timeDiff > 0 ) ? "+" : "";
		var forTime = theSign + timeDiff + " hour" + plural;

		Form.Sent.value = Now.toLocaleString() + " (client's time)";
		Form.Time_Difference.value = forTime;

		Form.redirect.value =  "default.aspx?client=" + escape( Name.value );
		return true;
	}
}