// ********************************************* //
// Overlay and AJAX methods for the contact page //
// ********************************************* //

// Initialises form fields and hides warning messages
function initialiseForm($this) {
	
	$('.dialog div.warning').hide();
	$('.dialog div.ajaxerror').remove();
	$('.dialog div.confirmation').hide();
	$('.dialog fieldset').show();
	
	$('.dialog input#sendTo').attr('value', $this.attr('href').replace('mailto:',''));
	
	$('.dialog input:text').val('');
	$('.dialog input:checkbox').attr('checked', false);
	$('.dialog textarea').val('');
	$('.dialog select').val('');
	
}

// Implements the jQuery UI dialog control
function emailOverlays() {
		
	//	Set dialog options and behaviour
	var dialogOptions = {
		'autoOpen': false,
		'draggable': false,
		'modal': true,
		'resizable': false,
		'width': 860
	};
	
	//	Setup form as dialog in overlay
	$('.dialog').dialog( dialogOptions );
	
	//	Bind mailto links with overlay actions
	$('a[href^=mailto:].overlay').each( function() {
		$(this).click( function() {
			initialiseForm($(this));
			$('.dialog').dialog( 'open' );
			return false;
		});
	});
	
}

//	Bind on load
$(document).ready( function() {
	emailOverlays();
});

// Callback function after validation that makes a AJAX request to a web method
function submitEnquiry($form) {
	// Create params to send to web method
	$params  = "{";
	$params += "    'sendAddress':'" + $('.dialog input#sendTo').attr('value') + "',";
	$params += "    'name':'" + $('.dialog input#name').val().replace("'", "\\'") + "',";
	$params += "    'emailAddress':'" + $('.dialog input#email').val() + "',";
	$params += "    'cc':'" + $('.dialog input#cc').attr('checked') + "',";
	$params += "    'address':'" + $('.dialog textarea#address').val().replace("'", "\\'") + "',";
	$params += "    'flightNumber':'" + $('.dialog input#flight').val().replace("'", "\\'") + "',";
	$params += "    'subject':'" + $('.dialog select#subject').val().replace("'", "\\'") + "',";
	$params += "    'enquiry':'" + $('.dialog textarea#enquiry').val().replace("'", "\\'") + "',";
	$params += "    'requestsResponse':'" + $('.dialog input#response').attr('checked') + "',";
	$params += "    'agreedTerms':'" + $('.dialog input#terms').attr('checked') + "'";
	$params += "}";
	
	$.ajax({
		type: "POST",
		contentType: "application/json; charset=utf-8",
		url: "/services/AjaxWebServices.asmx/ContactEnquirySubmit",
		data: $params,
		dataType: "json",
		success: function(results) {
			AjaxSucceeded(results);
		},
		error: function(xhr, status, error) {
			AjaxError();
		}
	});
}

// Successful AJAX response handling
function AjaxSucceeded(results) {
	if (!results.d) {
		AjaxError();
	} else {	
	    $('.dialog div.confirmation').slideDown();
	}
}

// Generic AJAX Error Handler
function AjaxError() {
	$('.dialog div.confirmation').hide();
	$error = '<div class="message warning ajaxerror"><h3>Sorry, there was a problem&hellip;</h3><p>It wasn\'t possible to send your enquiry. Please try again later, thank you.</p></div>';
	$('.dialog div.warning').after($error);
}

