$('document').ready(function() {
	$('.handle').click(function() { 
		$('#feedback-overlay').show();
		return false;
	});
	$('#feedback-overlay').click(function() {
		$('#feedback-overlay').hide();
	});
	$('#contact-close').click(function() {
		$('#feedback-overlay').hide();
		return false;
	});
	$('.stop-click').click(function(event) {
		event.stopPropagation();
	});
	
	$('#overlay-contact').submit(function() {
		submitContactForm();
		return false;
	});
	
	/* This also does validation on the page based contact form */
	$('#contact-inline').validate();
});


/* Call ajax server script to post form content */

function submitContactForm()
{
	var scriptPath = $("#overlay-contact input:hidden[name=rootPath]").val() + '/ajax/contact-form.php';

	$.ajax({
		  type: 'POST',
		  url: scriptPath,
		  data: $("#overlay-contact").serialize(),
		  success: function(data) {
				showContactResult(data);
			},
			beforeSend: function() { return $('#overlay-contact').validate().form(); }
		});
}

function showContactResult(res)
{
	if(res == "SUCCESS")
	{
		$('#message-holder').html("<h3>Thank you</h3>\n<p>Your enquiry has been sent to the RSP team. Please click the <strong>X</strong> above to return to the site or <a href=\"#\" id=\"show-contact-form\">you can send another message</a>.</p>");
	}
	else
	{
		var errors = res.replace('ERROR', '');
		$('#message-holder').html("<h3>Sorry...</h3>\n<p>There has been a problem sending your enquiry:</p><ul class=\"error\">" + errors + "</ul><p>Please <a href=\"#\" id=\"show-contact-form\">try again</a>.</p>\n<p>Alternatively, email <a href=\"mailto:support@rsp.ac.uk\" class=\"lowlight\">support@rsp.ac.uk</a> or telephone <span class=\"highlight\">0845 257 6860</span>.</p>");
	}
	$('#form-holder').hide();
	$('#message-holder').show();
	$('#show-contact-form').click(function() {
		$('#message-holder').hide();
		$('#form-holder').show();
		return false;
	});
}




