$(document).ready(function()
{
	// bouton submit
	$('#submit').click(function(event) {
		$('#contactform').submit();	
		return false;
	});		
	
	// touche entrée = alias vers le click submit
	if ($.browser.msie && $.browser.version < 8) {
		$('#contactform').keydown(keyFormValidation);
    }	
	else {
		$('#contactform').keypress(keyFormValidation);
	}
	
	$('#contactform').submit(function(event) {			
		validateForm($(this));		
		return false;
	});
});

var validateForm = function(form) {
	$('#messageBox').unbind('dialogbeforeclose');
	$.ajax({
		url: 'contact/perform?format=json',
		type: 'POST',
		dataType: 'json',
		data: form.serialize(),			
		beforeSend: function() {
			$('#loadingIndicator').text('Envoi du message en cours...');
			$('#loadingIndicator').dialog('open');
		},
		complete: function() {
							
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxErrorHandler(XMLHttpRequest, textStatus, errorThrown);
		},
		success: function(serverData, textStatus) {
			var response = serverData.response;
			if (response.success == false) {						
				if (response.errorType == 'inputError') {						
					setErrorsMessages(response);
				}
				setMessageBox(response.title, response.messages, 'mbError');				
			} else {				
				setMessageBox(response.title, response.messages, 'mbConfirm');
				if (response.redirect) {
					$('#messageBox').bind('dialogbeforeclose', function(event, ui) {
						//top.location = response.redirect;
						$(form).each(function() {
							this.reset();							
						});
						$('.tooltip-error').tooltip('close');
					});					
				}
			}				
			$('#loadingIndicator').dialog('close');
			$('#messageBox').dialog('open');
		}	
	}); // $.ajax()
};
