
// A conveience function to show the error dialog box and reset the form properly
function showErrorDialogWithMessage(message) 
{
	$("#dialog-message").html(message);
	$('#form-error-dialog').dialog('open');
	$('#contact-submit-button').removeAttr("disabled");
	$('#contact-submit-button').removeClass("disabled");
	$('#spinner').addClass("hide");
};




$(document).ready(function() {
	//		DublinDogFoundation Donation Javascript Controller
	//		Dependencies: jQuery 1.3, jQuery UI
	//		This script manages page effects and form validation/collection.
	//		Code copyright 2010, Bryan D K Jones. All rights reserved.
	
	
	// Progressive Enhancement: hide the no-javascript message and display the form
	$('#contact-no-javascript').addClass("hide");
	$('#contact-form').removeClass("hide");
	
	
	// Enable "_blank" functionality
	$('a.new-window-link').attr("target", "_blank");
	
	
	/*Dialogs
	-----------------------------------------------------------------------------------------------*/
			
			$("#form-error-dialog").dialog({
						autoOpen: false,
						modal: true,
						width: 400,
						buttons: {
							Ok: function() {
								$(this).dialog('close');
							}
						}
			});

	
	
	
/* Heavy Lifting: Validate the form and call the server
------------------------------------------------------------------------------------------------------- */

	$('#contact-submit-button').click(function() 
	{	
			//immediately disable the button to prevent double submits
			$(this).attr("disabled", "disabled");
			$(this).addClass("disabled");
			$('#spinner').removeClass("hide");
			
			// ridername: make sure it's not blank
			alert($("#ridername").val());
			if( $("#ridername").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please enter the  <strong>Rider Name</strong>.");
				return;
			}
			// Rider Address: make sure it's not blank
			if( $("#address1").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please enter the  <strong>Address</strong>.");
				return;
			}
			// emergencycontacts: make sure it's not blank
			if( $("#emergencycontact").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please enter the  <strong>Emergency Contact</strong>.");
				return;
			}
			// emergencyphone: make sure it's not blank
			if( $("#emergencyphone").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please enter the  <strong>Emergency Phone #</strong>.");
				return;
			}
			// contactphone: make sure it's not blank
			if( $("#contactphone").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please enter the  Your <strong>Phone #</strong>.");
				return;
			}
			
			// Company: make sure it's not blank
			if( $("#company").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! We gotta know your company! Please enter your <strong>company</strong>.");
				return;
			}	
			// Address: make sure it's not blank
			if( $("#address").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! We gotta know where you live! Please enter your <strong>address</strong>.");
				return;
			}		
			// First name: make sure it's not blank
			if( $("#contact-name").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! We gotta know who you are! Please enter your <strong>Full Name</strong>.");
				return;
			}
			// contact phone: make sure it's not blank
			if( $("#contact-phone").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! We gotta know your contact number! Please enter your <strong>Contact number</strong>.");
				return;
			}
			// Validate Email Address
			var email = $('#contact-email').val();	
			var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			
			if ( email == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Whoops, you missed one! Please enter your <strong>email address</strong>.");
				return;
			} 
			else if (!filter.test(email)) 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Sorry, your email address doesn't seem valid. Please make sure you've typed it correctly. (An example of correct formatting is: john@mac.com)");
				return;
			}
			// acknowledge: make sure it's not blank
			if($('#acknowledge').attr('checked')==false)
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please Select any <strong>Acknowledgement</strong>.");
				return;
			}
			// Sponserships: make sure it's not blank
			if($('#sponsership1').attr('checked')==false && $('#sponsership2').attr('checked')==false && $('#sponsership3').attr('checked')==false && $('#sponsership4').attr('checked')==false && $('#sponsership5').attr('checked')==false && $('#sponsership6').attr('checked')==false && $('#sponsership7').attr('checked')==false && $('#sponsership8').attr('checked')==false && $('#sponsership9').attr('checked')==false) 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please Select any <strong>Sponsership</strong>.");
				return;
			}
			// player1: make sure it's not blank
			if( $("#player1").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please enter the <strong>Player1</strong>.");
				return;
			}
			// player2: make sure it's not blank
			if( $("#player2").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please enter the <strong>Player2</strong>.");
				return;
			}
			// player3: make sure it's not blank
			if( $("#player3").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please enter the <strong>Player3</strong>.");
				return;
			}
			// player4: make sure it's not blank
			if( $("#player4").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please enter the <strong>Player4</strong>.");
				return;
			}
			// Additional Players: make sure it's not blank
			if( $("#player5").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please enter the <strong>Additional Player</strong>.");
				return;
			}
			// Number of adult guests: make sure it's not blank
			if( $("#numberofguests").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey! Please enter the <strong>Number of adult guests joining for reception & dinner</strong>.");
				return;
			}
			
			// Comments: make sure it's not blank
			if( $("#contact-comments").val() == "") 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Hey now, you left the comments field blank. You gotta write <em>something</em>!");
				return;
			}
			
			
			// Human check: make sure the math adds up
			var vstring1 = $("#vnumber1").html();
			var vstring2 = $("#vnumber2").html();
			
			var vnumber1 = +vstring1;				// The unary operator converts the string to a number (it's the fastest way to do it)
			var vnumber2 = +vstring2;
			
			var proposedTotalString = $("#contact-verify").val();
			var proposedTotalNumber = +proposedTotalString;
			var actualTotal = vnumber1 + vnumber2;
			if( proposedTotalNumber != actualTotal ) 
			{
				showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> Whoops! You failed the human test. Please double check your math and try again.");
				return;
			}

			var emailnumber=0;
			emailnumber=$("#formvalue").val();
			
			// ******************************************************************************************************************
			// We passed all the validation tests, so gather the info to put it into the database

			var name = $('#contact-name').val();
			var email = $('#contact-email').val();
			var comments = $('#contact-comments').val();
			
			var company = $('#company').val();
			var address = $('#address').val();
			var phone = $('#contact-phone').val();
			var sponsership1 = $('#sponsership1').val();
			var sponsership2 = $('#sponsership2').val();
			var sponsership3 = $('#sponsership3').val();
			var sponsership4 = $('#sponsership4').val();
			var sponsership5 = $('#sponsership5').val();
			var sponsership6 = $('#sponsership6').val();
			var sponsership7 = $('#sponsership7').val();
			var sponsership8 = $('#sponsership8').val();
			var sponsership9 = $('#sponsership9').val();
			var player1 = $('#player1').val();
			var player2 = $('#player2').val();
			var player3 = $('#player3').val();
			var player4 = $('#player4').val();
			var player5 = $('#player5').val();
			var numberofguests = $('#numberofguests').val();
			
			
			//*****************************
			// DEBUGGING ONLY -- REMOVE THIS SECTION IN PRODUCTION CODE
			//alert("form validated successfully!");
			//*****************************
			
			
			
			//Make the call to the server script to process the order
			$.ajax ({
				type: "POST",
				url: "../server-scripts/contact-form-handler.php",		//NOTE: This needs to point to your server-side script
				dataType: "json",
				data: {
					'emailnumber' : emailnumber,
					'company' : company,
					'address' : address,
					'contactPhone' : phone,
					'titlesponsor' : sponsership1,
					'eaglesponsor' : sponsership2,
					'birdiesponsor' : sponsership3,
					'parsponsor' : sponsership4,
					'cartsponsor' : sponsership5,
					'holdsponsor' : sponsership6,
					'lunchsponsor' : sponsership7,
					'foursome' : sponsership8,
					'singelplayer' : sponsership9,
					'player1' : player1,
					'player2' : player2,
					'player3' : player3,
					'player4' : player4,
					'player5' : player5,
					'numberofguests' : numberofguests,
					'name' : name, 
					'email' : email, 
					'comments' : comments
					},
				success: function(result) 
				{
					if (result.returncode =="success") 
					{
						// Present thank-you content
						$('.form-ul').fadeOut("normal");
						setTimeout(function() { $('#contact-thank-you').fadeIn("normal"); }, 600);
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) 
				{
					showErrorDialogWithMessage("<span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span> We apologize, but something went wrong while we were trying to process your comments. Please try again in a few minutes or <a href='mailto:info@dublindogfoundation.org'>click here to email us</a>.");
				}
			});		
	});
		
});









