$(document).ready(function() {
	$('#contactForm form').bind('submit', function(e) {
		$.post('/form/contact/format/json', $('#contactForm form').serialize(), function(msg){
			var error = false;
			$('#messages').remove();
			$('ul.errors').remove();
			if (msg.errors != undefined)
			{
				$.each(msg.errors, function(a,b) 
				{
					if (b.length > 0)
					{
						error = true;
						if (b == 'isEmpty')
							$('#' + a).after('<ul class="errors"><li>Please provide a value</li></ul>');
						if (b == 'emailAddressInvalidFormat')
							$('#' + a).after('<ul class="errors"><li>Please provide a correct email address</li></ul>');
					}
				});
				
				if (error)
				{
					$('h1').before('<div id="messages"><p>There are errors in your form below</p><div style="clear: both;"></div></div>');
				}
			}
			if (msg.message != undefined)
			{
				$('h1').before('<div id="messages"><p>' + msg.message + '</p><div style="clear: both;"></div></div>');
			}
		});
		
		e.preventDefault();
	});
});
