// Content-type: text/javascript; charset=utf-8

// Variables globales
var NLCookie;
var AJAX_HANDLER = '/newsletter/ajax.php?a=';
var qs = new QueryString();
var LePrenom = '';

//****************************************************************************//
// Au chargement
jQuery(document).ready(function () {

//****************************************************************************//
// Affichage de l'invite d'inscription à la newsletter

	NLCookie = jQuery.cookies.get('rdp-newsletter');
	// L'invite n'a pas encore été affichée
	if ((!NLCookie || NLCookie.alreadyDisplayed < 1) && !(qs.get('v'))) {
		// Affichage de l'invite d'inscription à la newsletter
		showDisplayBox('/newsletter/invite-pop.html', 375, 185, { backgroundColor: '#FFFFFF' });

		// Création de l'objet à enregistrer dans le cookie
		NLCookie = {
				alreadyDisplayed: 0
		};

		updateCookie('rdp-newsletter', NLCookie);
	}

//****************************************************************************//
// Action de fermeture sur le bouton en haut à droite
	jQuery('#frmNewsletter_lnkClose').live('click', function () {
		// Suppression des éléments de la DisplayBox
		jQuery('#DisplayBox_BG').remove();
		jQuery('#DisplayBox').remove();
		// Mise à jour du cookie
		NLCookie.alreadyDisplayed = 1;
		updateCookie('rdp-newsletter', NLCookie);

		return false;
	});
// Action de fermeture sur le lien "Fermer" en fin de processus
	jQuery('#frmNewsletter_lnkCloseEnd').live('click', function () {
		// Suppression des éléments de la DisplayBox
		jQuery('#DisplayBox_BG').remove();
		jQuery('#DisplayBox').remove();
		// Mise à jour du cookie
		NLCookie.alreadyDisplayed = 1;
		updateCookie('rdp-newsletter', NLCookie);

		return false;
	});

//****************************************************************************//
// Post de l'adresse email
	jQuery('#frmNewsletter #btnSubmitEmail').live('click', function () { Inscription1(); return false; });
	jQuery('#subscribe #subbutton').live('click', function () { Inscription1(); return false; });

	function Inscription1 () {
		var sEmail = jQuery('#txtEmail').val();
		var iOptin = -1;
		if (jQuery('#radOptinOui').attr('checked')) iOptin = 1;
		if (jQuery('#radOptinNon').attr('checked')) iOptin = 0;

		if (sEmail == '' || sEmail == 'Votre adresse mail...') {
			showAlertBox("Merci d'entrer une adresse email avant de continuer.", 300, 150, "OK");
			return false;
		}
		else if (!isValidEmail(sEmail)) {
			showAlertBox('L\'adresse email que vous avez entrée n\'est pas valide, merci de la corriger avant de continuer.', 300, 150, "OK");
			return false;
		}
		if (iOptin == -1) {
			showAlertBox('Vous devez choisir si vous souhaitez être informé des dernières informations Revue du Poker.com.', 300, 150, "OK");
			return false;
		}

		jQuery.post(AJAX_HANDLER + 'email_deja_inscrit',
			{ 'email': sEmail },
			function (email_deja_inscrit, textStatus) {
//				alert('email_deja_inscrit:' + email_deja_inscrit);
				if (email_deja_inscrit == '1') {
					showAlertBox("L'adresse email que vous avez entrée a déjé été utilisée.", 300, 150, "OK");
				}
				if (email_deja_inscrit == '0') {
					NLCookie.email = sEmail;
					updateCookie('rdp-newsletter', NLCookie);
					jQuery.post(AJAX_HANDLER + 'inscription1',
						{ 'email': sEmail, 'optin': iOptin },
						function (inscription1) {
//							alert('inscription1:' + inscription1);
							if (inscription1 == '1') {
								showDisplayBox('/newsletter/invite-more.php', 375, 400, { backgroundColor: '#FFFFFF' });
							}
							if (inscription1 == '0' || inscription1 == 'ERR') {
								alert('ERREUR #1');
							}
						}
					);
				}
				if (email_deja_inscrit == 'ERR') {
					alert('ERREUR #2');
				}
			}
		);
	}

//****************************************************************************//
// Post des infos supplémentaires
	jQuery('#frmNewsletterMore #btnSubmit').live('click', function () { Inscription2(); return false; });

	function Inscription2 () {
		var mess_erreur = '';
		var sEmail = '';
		var sCivilite = jQuery('#selCivilite').val();
		var sNom = jQuery('#txtNom').val();
		var sPrenom = jQuery('#txtPrenom').val();
		LePrenom = sPrenom;
		var sDDNJour = jQuery('#selJour').val();
		var sDDNMois = jQuery('#selMois').val();
		var sDDNAnnee = jQuery('#selAnnee').val();
		var sAdresse = jQuery('#txtAdresse').val();
		var sCP = jQuery('#txtCP').val();
		var sVille = jQuery('#txtVille').val();
		var sPays = jQuery('#selPays').val();
		var sMobile = jQuery('#txtMobile').val();

		if (sCivilite == '') { mess_erreur += '<li>Civilité</li>'; }
		if (sNom == '') { mess_erreur += '<li>Nom</li>'; }
		if (sPrenom == '') { mess_erreur += '<li>Prénom</li>'; }
		if (sDDNJour == 'x' || sDDNMois == 'x' || sDDNAnnee == 'x')
			{ mess_erreur += '<li>&quot;Date de naissance&quot; non valide</li>'; }
		if (sAdresse == '') { mess_erreur += '<li>Adresse</li>'; }
		if (sCP == '') { mess_erreur += '<li>Code postal</li>'; }
		if (sVille == '') { mess_erreur += '<li>Ville</li>'; }
		if (sPays == '') { mess_erreur += '<li>Pays</li>'; }
		if (sMobile == '') { mess_erreur += '<li>N° de mobile</li>'; }

		if (mess_erreur == '') {
			NLCookie = jQuery.cookies.get('rdp-newsletter');
			sEmail = NLCookie.email;
			jQuery.post(AJAX_HANDLER + 'inscription2',
				{	'email': sEmail,
					'civilite': sCivilite,
					'nom': sNom,
					'prenom': sPrenom,
					'date_naissance': sDDNAnnee + '-' + sDDNMois + '-' + sDDNJour,
					'adresse': sAdresse,
					'cp': sCP,
					'ville': sVille,
					'pays': sPays,
					'mobile': sMobile
				},
				function (inscription2) {
//					alert('inscription2:' + inscription2);
					if (inscription2 == '1') {
						showDisplayBox('/newsletter/invite-end.html', 350, 150, { backgroundColor: '#FFFFFF' });
					}
					if (inscription2 == '0' || inscription2 == 'ERR') {
						alert('ERREUR #3');
					}
				}
			);
		} else {
			showAlertBox("Les champs suivants sont obligatoires :</p><ul>" + mess_erreur + "</ul><p>Merci de les renseigner avant de continuer.", 375, 375, "OK");
		}

		mess_erreur = '';
	}

//****************************************************************************//
// Validation d'une inscription à la newsletter

	if (qs.get('v')) {
		sCle = qs.get('v');
		jQuery.post(AJAX_HANDLER + 'valider_inscription', { 'cle': sCle },
			function (valider_inscription) {
//				alert('valider_inscription:[' + valider_inscription + ']');
				if (valider_inscription == '1') {
					showDisplayBox('/newsletter/valide-1.html', 350, 150, { backgroundColor: '#FFFFFF' });
				}
				else if (valider_inscription == '0') {
					showDisplayBox('/newsletter/valide-0.html', 350, 150, { backgroundColor: '#FFFFFF' });
				}
				else if (valider_inscription == '2') {
					showDisplayBox('/newsletter/valide-2.html', 350, 150, { backgroundColor: '#FFFFFF' });
				}
				else if (valider_inscription == 'ERR') {
					alert('ERREUR #4');
				}
				// Mise à jour du cookie
				NLCookie.alreadyDisplayed = 1;
				updateCookie('rdp-newsletter', NLCookie);
			}
		);
	}

//****************************************************************************//
// Désinscription de la newsletter

	if (qs.get('a') && qs.get('a') == 'rm' && qs.get('k')) {
		sCle = qs.get('k');
		jQuery.post(AJAX_HANDLER + 'desinscription', { 'cle': sCle },
			function (desinscription) {
//				alert('desinscription:[' + desinscription + ']');
				if (desinscription != 'ERR') {
					showDisplayBox('/newsletter/desinsc-0.html', 350, 150, { backgroundColor: '#FFFFFF' });
				}
				else {
					alert('ERREUR #5');
				}
				// Mise à jour du cookie
				NLCookie.alreadyDisplayed = 1;
				updateCookie('rdp-newsletter', NLCookie);
			}
		);
	}

});

function updateCookie (sCookieName, sCookieValue) {
	// Définition de la date d'expiration du cookie
	var expirationDate = new Date();
	expirationDate.setDate(expirationDate.getDate() + 1); // +1 jour

	// Création/mise-à-jour du cookie
	jQuery.cookies.set(sCookieName, sCookieValue, {expiresAt: expirationDate});
}