/*
 * SimpleModal newsletter 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: newsletter.js 39 2007-10-26 20:46:32Z emartin24 $
 *
 */

var newsletter = {
	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 (newsletter.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/newsletter.html',
							data: $('#modalContainer form').serialize() + '&action=enregistrer',
							dataType: 'html',
							complete: function (xhr) {
								$('#modalContainer .loading').fadeOut(200, function () {
									$('#modalContainer .title').html('Merci !');
									$('#modalContainer .message').html(xhr.responseText).fadeIn(200);
								});
							},
							error: newsletter.error
						});
					});
				});
			}
			else {
				if ($('#modalContainer .message:visible').length > 0) {
					$('#modalContainer .message div').fadeOut(200, function () {
						$('#modalContainer .message div').empty();
						newsletter.showError();
						$('#modalContainer .message div').fadeIn(200);
					});
				}
				else {
					$('#modalContainer .message').animate({
						height: '30px'
					}, newsletter.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 () {
		newsletter.message = '';

		var email = $('#modalContainer #email').val();
		if (!email) {
			newsletter.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)) {
				newsletter.message += "L'E-mail est invalide.";
				return false;
			}
		}
		return true;
	},
	showError: function () {
		$('#modalContainer .message').html($('<div class="error"></div>').append(newsletter.message)).fadeIn(200);
	}
};