/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2007 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 39 2007-10-26 20:46:32Z emartin24 $
 *
 */

var contact = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.content.fadeIn(200, function () {
					$('#modalContainer #name').focus();
				});
				// resize the textarea for safari
				if ($.browser.safari) {
					$('#modalContainer textarea').attr({
						cols: '37',
						rows: '8'
					});
				}
			});
		});
	},
	show: function (dialog) {

		$('#modalContainer .formulaireEnvoyer').click(function (e) {
			
			e.preventDefault();
			
			// validate form
			if (contact.validate()) {

				$('#modalContainer .message').fadeOut(function () {
					$('#modalContainer .message').removeClass('error').empty();
				});
				$('#modalContainer .title').html('Envoi en cours ...');
				$('#modalContainer form').fadeOut(200);
				$('#modalContainer .content').animate({
					height: '80px'
				}, function () {
					$('#modalContainer .loading').fadeIn(200, function () {
						$.ajax({
							url: '/tools/contact.html',
							data: $('#modalContainer form').serialize() + '&action=envoyer',
							dataType: 'html',
							complete: function (xhr) {
								$('#modalContainer .loading').fadeOut(200, function () {
									$('#modalContainer .title').html('Merci !');
									$('#modalContainer .message').html(xhr.responseText).fadeIn(200);
								});
							},
							error: contact.error
						});
					});
				});
			}
			else {
				if ($('#modalContainer .message:visible').length > 0) {
					$('#modalContainer .message div').fadeOut(200, function () {
						$('#modalContainer .message div').empty();
						contact.showError();
						$('#modalContainer .message div').fadeIn(200);
					});
				}
				else {
					$('#modalContainer .message').animate({
						height: '30px'
					}, contact.showError);
				}
				
			}
		});
	},
	close: function (dialog) {
		dialog.content.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.remove(dialog);
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		contact.message = '';
		if (!$('#modalContainer #name').val()) {
			contact.message += 'Le nom est obligatoire. ';
		}

		if ($('#modalContainer #typeFormulaire').val() != "rappel") {
			var email = $('#modalContainer #email').val();
			if (!email) {
				contact.message += "L'E-mail est obligatoire. ";
			}
			else {
				// Regex from: http://regexlib.com/REDetails.aspx?regexp_id=599
				var filter = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/;
				if (!filter.test(email)) {
					contact.message += "L'E-mail est invalide.";
				}
			}
		} else {
			if (!$('#modalContainer #tel').val()) {
				contact.message += 'Le num&eacute;ro de t&eacute;l&eacute;phone est obligatoire.';
			}
		}
		
		if ($('#modalContainer #typeFormulaire').val() == "contact") {
			if (!$('#modalContainer #message').val()) {
				contact.message += 'Le message est obligatoire.';
			}
		}

		if (contact.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	showError: function () {
		$('#modalContainer .message').html($('<div class="error"></div>').append(contact.message)).fadeIn(200);
	}
};