/*
AJAX FORMS JS (requires jQuery)
*/

// How long to display status messages (in milliseconds)
var messageDelay = 2000;

// Init the forms once the document is ready
$( init );

// Initialize any forms available on this page
function init() {
	// Make submitForm() the form submit handler for any matching forms
	$('#contactForm, #enquire1Form, #enquire2Form, #mlForm').submit( submitForm );
}

// Submit the form via Ajax
function submitForm() {
	var $contactForm = $(this);
	var formID = $contactForm.attr('id');

	// Are all the fields filled in?
	if (!checkForm(formID)) {

		// No; display a warning message and return to the form
		$('#incompleteMessage').fadeIn().delay(messageDelay).fadeOut();
		$contactForm.fadeOut().delay(messageDelay).fadeIn();

	} else {

		// Yes; submit the form to the PHP script via Ajax
		$('#sendingMessage').fadeIn();
		$contactForm.fadeOut();
		// (Keeping these separate for now)
		switch(formID){
			case 'contactForm':
				$.ajax({	url: $contactForm.attr( 'action' ) + "?ajax=true",
							type: $contactForm.attr( 'method' ),
							data: $contactForm.serialize(),
							success: submitFinishedContact	});
				break;
			case 'enquire1Form':
				$.ajax({	url: $contactForm.attr( 'action' ) + "?ajax=true",
							type: $contactForm.attr( 'method' ),
							data: $contactForm.serialize(),
							success: submitFinishedEnquire1	});
				break;
			case 'enquire2Form':
				$.ajax({	url: $contactForm.attr( 'action' ) + "?ajax=true",
							type: $contactForm.attr( 'method' ),
							data: $contactForm.serialize(),
							success: submitFinishedEnquire2	});
				break;
			case 'mlForm':
				$.ajax({	url: $contactForm.attr( 'action' ) + "?ajax=true",
							type: $contactForm.attr( 'method' ),
							data: $contactForm.serialize(),
							success: submitFinishedML	});
				break;			
		}
	}

	// Prevent the default form submission occurring
	return false;
}

// Check the form fields before submit
function checkForm(formID){
	var validated = false;
	
	switch(formID){
		case 'contactForm':
			if($('#contactName').val() && $('#contactEmail').val() && $('#contactMessage').val()){
				validated = true;
			}
			break;
		case 'enquire1Form':
			if($('#enquiry1Name').val() && $('#enquiry1Email').val() && $('#enquiry1Phone').val() && $('#enquiry1City').val() &&
				$('#enquiry1ArrivalDate').val() && $('#enquiry1DepartureDate').val() && $('#enquiry1NumAdults').val() && $('#enquiry1NumChildren').val() &&
				($('#enquiry1HotelGrade3').prop("checked") || $('#enquiry1HotelGrade4').prop("checked") || $('#enquiry1HotelGrade5').prop("checked")) && 
				($('#enquiry1AccomTypeHalf').prop("checked") || $('#enquiry1AccomTypeAll').prop("checked")) ){
				validated = true;
			}
			break;
		case 'enquire2Form':
			if($('#enquiry2Name').val() && $('#enquiry2Email').val() && $('#enquiry2Phone').val() && $('#enquiry2City').val() &&
				$('#enquiry2ArrivalDate').val() && $('#enquiry2DepartureDate').val() && $('#enquiry2NumAdults').val() && $('#enquiry2NumChildren').val() &&
				($('#enquiry2HotelGrade3').prop("checked") || $('#enquiry2HotelGrade4').prop("checked") || $('#enquiry2HotelGrade5').prop("checked")) && 
				($('#enquiry2AccomTypeHalf').prop("checked") || $('#enquiry2AccomTypeAll').prop("checked")) &&
				($('#enquiry2AccomTypePrivate').prop("checked") || $('#enquiry2AccomTypeLuxury').prop("checked")) &&
				$('#enquiry2NumRooms').val() &&
				($('#enquiry2BudgetB').prop("checked") || $('#enquiry2BudgetS').prop("checked") || $('#enquiry2BudgetL').prop("checked")) &&
				($('#enquiry2AreaN').prop("checked") || $('#enquiry2AreaE').prop("checked") || $('#enquiry2AreaS').prop("checked") || $('#enquiry2AreaW').prop("checked")) ){
				validated = true;
			}
			break;
		case 'mlForm':
			if($('#subscribeName').val() && $('#subscribeEmail').val() &&
				($('#mlInvestments').prop("checked") || $('#mlSpecials').prop("checked") || $('#mlNewsletter').prop("checked"))){
				validated = true;
			}
			break;			
		default:
			validated = true;
	}
	
	return validated;	
}

// Handle the contact form Ajax response
function submitFinishedContact( response ) {
	response = $.trim( response );
	$('#sendingMessage').fadeOut();
	
	//Perform conversion tracking
	ppcConversion();

	if ( response == "success" ) {

		// Form submitted successfully:
		// 1. Display the success message
		// 2. Clear the form fields
		// 3. Redisplay the form
		$('#successMessage').fadeIn().delay(messageDelay).fadeOut();
		$('#contactName, #contactEmail, #contactSubject, #contactMessage').val( "" );
		
		//Can redisplay form:
		$('#contactForm').delay(messageDelay+500).fadeIn();
		//Or perhaps close the contact panel?

	} else {

		// Form submission failed: Display the failure message,
		// then redisplay the form
		$('#failureMessage').fadeIn().delay(messageDelay).fadeOut();
		$('#contactForm').delay(messageDelay+500).fadeIn();
	}
}

// Handle the first enquiry form Ajax response
function submitFinishedEnquire1( response ) {
	response = $.trim( response );
	$('#sendingMessage').fadeOut();
	
	//Perform conversion tracking
	ppcConversion();

	if ( response == "success" ) {

		// Form submitted successfully:
		// 1. Display the success message
		// 2. Clear the form fields
		// 3. Redisplay the form
		$('#successMessage').fadeIn().delay(messageDelay).fadeOut();
		
		$('#enquiry1Name, #enquiry1Email, #enquiry1Phone, #enquiry1City').val( "" );
		$('#enquiry1ArrivalDate, #enquiry1DepartureDate, #enquiry1NumAdults, #enquiry1NumChildren').val( "" );
		$('#enquiry1HotelGrade3, #enquiry1HotelGrade4, #enquiry1HotelGrade5').prop("checked", false);
		$('#enquiry1AccomTypeHalf, #enquiry1AccomTypeAll').prop("checked", false);
		$('#enquiry1Message').val( "" );
		
		$('#enquire1Form').delay(messageDelay+500).fadeIn();

	} else {

		// Form submission failed: Display the failure message,
		// then redisplay the form
		$('#failureMessage').fadeIn().delay(messageDelay).fadeOut();
		$('#enquire1Form').delay(messageDelay+500).fadeIn();
	}
}

// Handle the second (combination) enquiry form Ajax response
function submitFinishedEnquire2( response ) {
	response = $.trim( response );
	$('#sendingMessage').fadeOut();
	
	//Perform conversion tracking
	ppcConversion();

	if ( response == "success" ) {

		// Form submitted successfully:
		// 1. Display the success message
		// 2. Clear the form fields
		// 3. Redisplay the form
		$('#successMessage').fadeIn().delay(messageDelay).fadeOut();
		
		$('#enquiry2Name, #enquiry2Email, #enquiry2Phone, #enquiry2City').val( "" );
		$('#enquiry2ArrivalDate, #enquiry2DepartureDate, #enquiry2NumAdults, #enquiry2NumChildren').val( "" );
		$('#enquiry2HotelGrade3, #enquiry2HotelGrade4, #enquiry2HotelGrade5').prop("checked", false);
		$('#enquiry2AccomTypeHalf, #enquiry2AccomTypeAll').prop("checked", false);
		$('#enquiry2Message').val( "" );
		
		$('#enquiry2AccomTypePrivate, #enquiry2AccomTypeLuxury').prop("checked", false);
		$('#enquiry2NumRooms').val( "" );
		$('#enquiry2BudgetB, #enquiry2BudgetS, #enquiry2BudgetL').prop("checked", false);
		$('#enquiry2AreaN, #enquiry2AreaE, #enquiry2AreaS, #enquiry2AreaW').prop("checked", false);
		$('#enquiry2Message2').val( "" );
		
		$('#enquire2Form').delay(messageDelay+500).fadeIn();

	} else {

		// Form submission failed: Display the failure message,
		// then redisplay the form
		$('#failureMessage').fadeIn().delay(messageDelay).fadeOut();
		$('#enquire2Form').delay(messageDelay+500).fadeIn();
	}
}

// Handle the mailing list form Ajax response
function submitFinishedML( response ) {
	response = $.trim( response );
	$('#sendingMessage').fadeOut();
	
	//Perform conversion tracking
	ppcConversion();

	if ( response == "success" ) {

		// Form submitted successfully:
		// 1. Display the success message
		// 2. Clear the form fields
		// 3. Redisplay the form
		$('#successMessage').fadeIn().delay(messageDelay).fadeOut();
		$('#subscribeName, #subscribeEmail').val( "" );
		$('#mlInvestments, #mlSpecials, #mlNewsletter').prop("checked", false);
		
		$('#mlForm').delay(messageDelay+500).fadeIn();

	} else {

		// Form submission failed: Display the failure message,
		// then redisplay the form
		$('#failureMessage').fadeIn().delay(messageDelay).fadeOut();
		$('#mlForm').delay(messageDelay+500).fadeIn();
	}
}

//Conversion tracking
function ppcConversion() {
	var iframe = document.createElement('iframe');
	iframe.style.width = '0px';
	iframe.style.height = '0px';
	document.body.appendChild(iframe);
	iframe.src = baseURL + '/conversion_tracking.html';
}


