var delaiGain = 8000; // Temporisation d'affichage du ticket gagnant gratté
var tempoPopUp = 4000; // Temporisation d'ouverture d'une pop-up
coregProfil = false;
erreur_parrainage = false;
function close_popup() {
	$('#gray_bg').css("visibility","hidden");
	$('#pop-up').css("visibility","hidden");
	$('#fermeture_pop-up').css("visibility","hidden");
	$('#pop-up').html("");
}
function open_popup_affichage(data) {
	$('#pop-up').show();
	$('#pop-up').html(data);

   $('#contenant_pop-up').css('opacity', '0');
   $('#contenant_pop-up').css('display' , 'block');
   
   $('#contenant_pop-up').animate({
    opacity: 0.8
  	}, 1000, function() {
    $('#contenant_pop-up').delay(tempoPopUp).animate({
    opacity: 0
    }, 1000, function() {
    	$('#contenant_pop-up').hide();
    });
  });


};
function cacher_popup() {
	$('#gray_bg').hide();
	$('#contenant_pop-up').hide();
	$('#pop-up').hide();
	$('#pop-up').html("");
	$("#gray_bg").attr({ onclick: ""});
};
function ouvrir_form_mdp() {
	$('#mdp_form').slideToggle('slow');
}
/*
function fermer_form_mdp() {
	$('#mdp_lien').show();
	$('#mdp_form').stop().hide();
}
*/
function ouvrir_form_coordonnees() {
	$('#coordonnees_lien').hide();
	$('#coordonnees_form').slideDown('slow');
}

function ouvrir_form_abonnement() {
	$('#abonnement_lien').hide();
	$('#abonnement_form').slideDown('slow');
}
function retourIcone() {
		$('#img_switch').attr("src", "../templates/default/images/arrow_left_12x12_white.png");
}
function retourIconeOut() {
		$('#img_switch').attr("src", "../templates/default/images/arrow_left_12x12_black.png");
}

function verifFormPass() {
	
	var erreurPass = false;
	var pass = $('#ancienPass');
	var nouveaupass = $('#nouveauPass');
	var nouveaupass2 = $('#nouveauPass2');
	
	$('.mess_erreur').hide();
	
	surligne(pass, true);
	surligne(nouveaupass, true);
	surligne(nouveaupass2, true);

	//verif bon mdp
	$.post('../controller/ajaxAuthentification.php', { auth_pass : pass.val()},
	function(data) {
		if (data == true) {
			if (nouveaupass.val().length == 0) {
				erreurPass = true;
				surligne(nouveaupass, false);
				$('#erreur_passNonValide2').show();
			}
			
			if (nouveaupass2.val().length == 0) {
				erreurPass = true;
				surligne(nouveaupass2, false);
				$('#erreur_passNonValide3').show();
			}
			
			if (!(verifPass(nouveaupass))) {
				erreurPass = true;
				surligne(nouveaupass, false);
				$('#erreur_passNonValide2').show();
			}
			
			if (!(verifPass(nouveaupass2))) {
				erreurPass = true;
				surligne(nouveaupass2, false);
				$('#erreur_passNonValide3').show();
			}
			
			
			if (nouveaupass.val() != nouveaupass2.val()) {
				erreurPass = true;
				surligne(nouveaupass, false);
				surligne(nouveaupass2, false);
				$('#erreur_differentsMdp').show();
			}

			if(!erreurPass) {
				$.post('../controller/ajaxEnregistreur.php', { page : "modifierPass", pass : nouveaupass.val()},
				function(data) {
					$('#bulle').hide();
					open_popup_affichage(data);
				});	

			}
		}
		else {
			surligne(pass, false);
			var erreur = true;
			$('#erreur_passFaux').show();
		}
	});
}


function envoyerMail(d, email) {
	$.post('../controller/ajaxEnvoyerMail.php', { mail : d, adresse : email},
	function(data) {
		open_popup_affichage(data)
	});
}

function changerMail() {
	if (verifEmail($('#email'))) {
	
		$.post('../controller/ajaxVerifEmailEnBase.php', { verifEmailEnBase : $("#email").val()},
			function(data) {
				if (data == "true") {
				}
				else {
					$.post('../controller/ajaxEnregistreur.php', { page: "modifierEmail", email : $('#email').val()},
					function(data) {
						envoyerMail("confirmationInscription", '');
						choixPage("profil");
						open_popup_affichage(data);
					}); 
				}
				 
			});
	}
	else {
		//erreur
	}
}
function montrerFormEmail() {
	$('#formEmail').slideDown('slow');
}

function detecterEvenementLogin() {
	$('#auth_email').blur(function() {
		var regex = /^[a-zA-Z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$/;
		if(!regex.test($("#auth_email").val ()))
		{
			$('#erreur_adresseEmailLogin').show ();
			erreur = true;
		}
		else
		{
			$('#erreur_adresseEmailLogin').hide ();
		}
	});
	$('#auth_pass').blur(function() {
		if (($('#auth_pass').val().indexOf("<")>=0)||($('#auth_pass').val().indexOf(">")>=0)||($('#auth_pass').val().length < 6 )) {
			$('#erreur_passLogin').show ();
			erreur = true;
		}
		else {
			$('#erreur_passLogin').hide ();
		}
	});
} 
function detecterEvenement() {

//	$('.case').css('display','none');
	$('#FirstName').blur(function() {
		verifPrenom($('#FirstName'));
	});
	$('#LastName').blur(function() {
		verifNom($('#LastName'));
	});
	$('#email').blur(function() {
		if (verifEmail($('#email'))) {
			verifEmailEnBase($('#email'));
		}
	});
	$('#Pass').blur(function() {
		verifPass($('#Pass'));
	});
	$('#ancienPass').blur(function() {
		verifPassBis($('#ancienPass'));
	});

	$('#Profession').blur(function() {
		verifProfession($('#Profession'));
	});
	$('#ZipCode').blur(function() {
		verifZip($('#ZipCode'));
	//	completerAvecZip($('#ZipCode'));
		
	});
	$('#pays').blur(function() {
		verifPays($('#pays'));
	});
	
	$('#annee').blur(function() {
		if (verifPresenceAge()) {
			if (verifJourMois()) {
				verifJourFevrier();
			}
		}
	});
	$('#Address').blur(function() {
		verifAdresse($('#Address'));
	});
	$('#City').blur(function() {
		verifVille($('#City'));
	});
	$('#PhoneNumber').blur(function() {
		verifTel($('#PhoneNumber'));
	});
	$('#EmailFilleul_1').one("focus", function() {
		$('#EmailFilleul_1').val("");
		$('#EmailFilleul_1').css('color','black');
	});
	$('#EmailFilleul_2').one("focus", function() {
		$('#EmailFilleul_2').val("");
		$('#EmailFilleul_2').css('color','black');
	});
	$('#EmailFilleul_3').one("focus", function() {
		$('#EmailFilleul_3').val("");
		$('#EmailFilleul_3').css('color','black');
	});
	$('#EmailFilleul_4').one("focus", function() {
		$('#EmailFilleul_4').val("");
		$('#EmailFilleul_4').css('color','black');
	});
	$('#EmailFilleul_5').one("focus", function() {
		$('#EmailFilleul_5').val("");
		$('#EmailFilleul_5').css('color','black');
	});
	$('#EmailFilleul_6').one("focus", function() {
		$('#EmailFilleul_6').val("");
		$('#EmailFilleul_6').css('color','black');
	});
	$('#message_box_par').one("focus", function() {
		$('#message_box_par').val("");
		$('#message_box_par').css('color','black');
	});
	$('.champsMessage textarea').one("focus", function() {
		$('.champsMessage textarea').val("");
		$('.champsMessage textarea').css('color','black');
	});
	
	$('#EmailFilleul_1').blur(function() {
		if (verifEmail2($(this)))
		{
			$('#erreur_adresseEmail1').hide ();
			if (verifEmailEnBase2($(this)))
			{
				$('#erreur_emailEnBase1').show ();
			}
			else
			{
				$('#erreur_emailEnBase1').hide ();
			} 
		}
		else
		{
			$('#erreur_adresseEmail1').show ();
		}
	});
	$('#EmailFilleul_2').blur(function() {
		if (verifEmail2($(this)))
		{
			$('#erreur_adresseEmail2').hide ();
			if (verifEmailEnBase2($(this)))
			{
				$('#erreur_emailEnBase2').show ();
			}
			else
			{
				$('#erreur_emailEnBase2').hide ();
			}
		}
		else
		{
			$('#erreur_adresseEmail2').show ();
		}
	});
	$('#EmailFilleul_3').blur(function() {
		if (verifEmail2($(this)))
		{
			$('#erreur_adresseEmail3').hide ();
			if (verifEmailEnBase2($(this)))
			{
				$('#erreur_emailEnBase3').show ();
			}
			else
			{
				$('#erreur_emailEnBase3').hide ();
			}
		}
		else
		{
			$('#erreur_adresseEmail3').show ();
		}
	});
	$('#EmailFilleul_4').blur(function() {
		if (verifEmail2($(this)))
		{
			$('#erreur_adresseEmail4').hide ();
			if (verifEmailEnBase2($(this)))
			{
				$('#erreur_emailEnBase4').show ();
			}
			else
			{
				$('#erreur_emailEnBase4').hide ();
			}
		}
		else
		{
			$('#erreur_adresseEmail4').show ();
		}
	});
	$('#EmailFilleul_5').blur(function() {
		if (verifEmail2($(this)))
		{
			$('#erreur_adresseEmail5').hide ();
			if (verifEmailEnBase2($(this)))
			{
				$('#erreur_emailEnBase5').show ();
			}
			else
			{
				$('#erreur_emailEnBase5').hide ();
			}
		}
		else
		{
			$('#erreur_adresseEmail5').show ();
		}
	});
	$('#EmailFilleul_6').blur(function() {
		if (verifEmail2($(this)))
		{
			$('#erreur_adresseEmail6').hide ();
			if (verifEmailEnBase2($(this)))
			{
				$('#erreur_emailEnBase6').show ();
			}
			else
			{
				$('#erreur_emailEnBase6').hide ();
			}
		}
		else
		{
			$('#erreur_adresseEmail6').show ();
		}
	});
	
	$('.btnDelGodson').unbind ('click');
	
	$('.btnDelGodson').bind ('click', function () {
		img = $(this);
		$.post('../controller/ajaxFilleul.php', { email : $(this).attr('id').substr(7)},
			function(data) {
				mess = data.substr (4);
				code = data.substr (0,3);
				if (code == 200)
				{
					img.unbind ('click');
					img.parent('div').fadeOut ('fast', function () {
						img.parent('div').html ('');
					});
				}
				
				open_popup_affichage(mess);
		});
	});
	
	$('.pasInscritBtn').unbind ('click');
	$('.pasInscritBtn').bind ('click', function () {
		$(this).unbind ();
		envoyerMail('invitationFilleul', $(this).attr('id').substr(8));
		$(this).fadeOut ('fast', function () {
			$(this).html ('');
			$(this).removeClass('pasInscritBtn');
		});
					
	});	
	
	$('#montrezOiOpen').unbind ('click');
	$('#montrezOiOpen').bind ('click', function () {
		if ($('#contenuParrainage:visible').length)
		{
			montrerOi ();
		}
		else
		{
			montrerParrainage ();
		}
	});
	

}
function ajouterChamps() {
	var bool = true;
	var z = 1;
	while ((bool)&&(z<7)) {
		if ($('#blockEmailFilleul' + z).css("display") == "none") {
			$('#blockEmailFilleul' + z).css("display","block");
			bool = false;
		}
		else {
			z = z+1;
		}	
	}
}
function verifPrenom(champ)
{
	var regex = /^[a-zA-Zéèàìòùôêçüöäë '-]{3,}$/i;
	var val = champ.val();
	if(!regex.test(val))
	{
		surligne(champ, false);
		$('#erreur_prenom').show();
		return false;
	}
	else
	{
		surligne(champ, true);
		$('#erreur_prenom').hide();
		return true;
	}
}
function verifNom(champ)
{
	var regex = /^[a-zA-Zéèàìòùôêçüöäë '-]{3,}$/i;
	var val = champ.val();
	if(!regex.test(val))
	{
		surligne(champ, false);
		$('#erreur_nom').show();
		return false;
	}
	else
	{
		surligne(champ, true);
		$('#erreur_nom').hide();
		return true;
	}
}
function verifEmail(champ)
{
	var regex = /^[a-zA-Z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$/;
	var val = champ.val();
	if(!regex.test(val))
	{
		surligne(champ, false);
		$('#erreur_adresseEmail').show();
		return false;
	}
	else
	{
		surligne(champ, true);
		$('#erreur_adresseEmail').hide();
		return true;
	}
}
function verifPass(champ) {
	if ((champ.val().indexOf("<")>=0)||(champ.val().indexOf(">")>=0)||(champ.val().length < 6 )) {
		surligne(champ, false);
		$('#erreur_passNonValide').show();
		return false;
	}
	else {
		surligne(champ, true);
		$('#erreur_passNonValide').hide();
		return true;
	}
}
function verifPassBis(champ) {
	if ((champ.val().indexOf("<")>=0)||(champ.val().indexOf(">")>=0)||(champ.val().length < 6 )) {
		surligne(champ, false);
		$('#erreur_passNonValide').show();
		return false;
	}
	else {
		surligne(champ, true);
		$('#erreur_passNonValide').hide();
		return true;
	}
}
// J'ai été obligée de faire ça pour pallier aux problèmes que génèrent
// les closures. emailEnBase est donc une variable globale accessible partout.
// A utiliser avec précaution.
emailEnBase = false;
function funct(data,champ) {
		if (data == "true") {
			surligne(champ, false);
			emailEnBase = false;
			$('#erreur_emailEnBase').show();
			return emailEnBase;
		}
		else {
			surligne(champ, true);
			emailEnBase = true;
			$('#erreur_emailEnBase').hide();
			return emailEnBase;
		}
}

function verifEmailEnBase2(champ) {

	answer = undefined;
	$.ajax({
		type: "POST",
		url: "../controller/ajaxVerifEmailEnBase2.php",
		async: false,
		data: "verifEmailEnBase="+champ.val(),
		success: function(data){
			if (data == "true")
			{
				surligne(champ, false);
				answer = true;
			}
			else
			{
				surligne(champ, true);
				answer = false;
			}
		}
	});
	return answer;
}

function verifEmailEnBase(champ) {
	$.post('../controller/ajaxVerifEmailEnBase.php', { verifEmailEnBase : champ.val()},
	function(data) {
		funct(data, champ);
	});
	return emailEnBase;
}
function verifEmail2(champ)
{
	var regex = /^[a-zA-Z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$/;
	var val = champ.val();
	if(!val)
	{
		surligne(champ, true);
		return true;
	}
	else if(!regex.test(val))
	{
		surligne(champ, false);
		return false;
	}
	else
	{
		surligne(champ, true);
		return true;
	}
}

function verifAdresse(champ)
{
	var regex = /^[a-zA-Z0-9éèàìòùôûêâîçüöäë. ,_-]{6,}$/i;
	var val = champ.val();
	if(!regex.test(val))
	{
		surligne(champ, false);
		$('#erreur_adressePostale').show();
		return false;
	}
	else
	{
		surligne(champ, true);
		$('#erreur_adressePostale').hide();
		return true;
	}
}


function verifPays(champ)
{
/*	var regex = /^[a-zA-Zéèàìòùôêç-]{3,}$/;
	if(!regex.test(champ.val()))
	{
		surligne(champ, false);
		return false;
	}
	else
	{
		surligne(champ, true);
		return true;
	}*/
	surligne(champ, true);
	return true;
}

function verifCountry(champ)
{
/*	var regex = /^[a-zA-Zéèàìòùôêç-]{3,}$/;
	if(!regex.test(champ.val()))
	{
		surligne(champ, false);
		return false;
	}
	else
	{
		surligne(champ, true);
		return true;
	}*/
	surligne(champ, true);
	return true;
}

function verifZip(champ)
{
	var val = champ.val();
	if(val.length <5)
	{
		surligne(champ, false);
		$('#erreur_codePostal').show();
		return false;
	}
	else
	{
		surligne(champ, true);
		$('#erreur_codePostal').hide();
		return true;
	}
	return true;
}

function verifTel(champ)
{
	var regex = /^((0[67]([-. ]?[0-9]{2}){4})|(6[0-9]{2}([-. ]?[0-9]{3}){2}))$/;	
	var val = champ.val();
	if(!regex.test(val))
//	if(!regex.test(val) && val != "")	// On vire le cas où on met un numéro nul
	{
		surligne(champ, false);
		$('#erreur_telephone').show();
		return false;
	}
	else
	{
		surligne(champ, true);
		$('#erreur_telephone').hide();
		return true;
	}
}
function verifNumTel() {
	if (verifTel($('#PhoneNumber'))) {
		$.post('../controller/ajaxEnregistreur.php', { page: "modifierNumTel", tel : $('#PhoneNumber').val()},
			function(data) {
			//	open_popup_affichage(data);
			//	choixPage('parrainageFinal');
				choixPage ('jeu');
			});
	}
}

function detecterTelFourni() {
	$.post('../controller/ajaxTelephone.php', {},
		function(data) {
			if (data === "false") {
				$('#num_tel').css("display", "block");
			}
		});
}

function verifProfession(champ)
{
	var val = champ.val();
	if(val == "Def")
	{
		surligne(champ, false);
		$('#erreur_profession').show();
		return false;
	}
	else
	{
		surligne(champ, true);
		$('#erreur_profession').hide();
		return true;
	}
}
function verifPresenceAge() {
	var jour = $('#jour');	// jour
	var mois = $('#mois'); // mois
	var annee = $('#annee'); // année
	if (jour.val() && mois.val() && annee.val()) {
		surligne(jour, true);
		surligne(mois, true);
		surligne(annee, true);
		$('#erreur_jourFevrier').hide();
		$('#erreur_jourMois').hide();
		$('#erreur_date').hide();
		return true;
	}
	if(!jour.val()) {
		surligne(jour, false);
		$('#erreur_date').show();
	}
	if (!mois.val()) {
		surligne(mois, false);
		$('#erreur_date').show();
	}
	if (!annee.val()) {
		surligne(annee, false);
		$('#erreur_date').show();
	}
	return false;
}
function verifJourMois() {
	var jour = $('#jour');	// jour
	var mois = $('#mois'); // mois
	var annee = $('#annee'); // année
	if (jour.val() == 31 && (mois.val()==4 | mois.val()==6 | mois.val() ==9 | mois.val() ==11 )) {
		surligne(jour, false);
		surligne(mois, false);
		surligne(annee, false);
		$('#erreur_jourMois').show();
		return false;
	}
	else {
		surligne(jour, true);
		surligne(mois, true);
		surligne(annee, true);
		$('#erreur_jourMois').hide();
	}
	return true;
}
function verifJourFevrier() {
	var jour = $('#jour');	// jour
	var mois = $('#mois'); // mois
	var annee = $('#annee'); // année
	if ((jour.val() == 31 | jour.val() == 30) && (mois.val()==2)) {
		surligne(jour, false);
		surligne(mois, false);
		surligne(annee, false);
		$('#erreur_jourFevrier').show();
		return false;
	}
	else {
		surligne(jour, true);
		surligne(mois, true);
		surligne(annee, true);
		$('#erreur_jourFevrier').hide();
		return true;
	}
}
function passeOubli(d) {
	if (verifEmail($("#oubli_email"))) {
		$.post('../controller/ajaxVerifEmailEnBase.php', { verifEmailEnBase : $("#oubli_email").val()},
		function(data) {
			if (data == "true") {
				$.post('../controller/ajaxAfficheurConnexion.php', { connex_etape : d, email : $("#oubli_email").val()},
					function(data) {
						open_popup_affichage(data);
					});
			}
			else {
				open_popup_affichage(data);
			}
		});
	}
}

verifEnCours = false;

function verifForm(action)
{
	if(!verifEnCours)
	{
		verifEnCours = true;
		var erreur = false;
		//var tabErreur = new Array();

	
		var title = $('[name=title]');
		if (!(title.length == 0)) {
			if ((!($("#Mlle").is(':checked')) && !($("#M").is(':checked')) && !($("#Mme").is(':checked')))) {
				erreur = true;
				$("#erreur_civilite").show();
			}
			else {
				$("#erreur_civilite").hide();
			}
		}
		var firstName = $('#FirstName');
		if (!(firstName.length == 0)) {
			if (!verifPrenom(firstName)) {
				erreur = true;
			}
		}
		var lastName = $('#LastName');
		if (!(lastName.length == 0)) {
			if (!verifNom(lastName)) {
				erreur = true;
			}
		}
		var email = $('#email');
		if (!(email.length == 0)) {
			if (!verifEmail(email)) {
				erreur = true;
			}
		}
		var pass = $('#Pass');
		if (!(pass.length == 0)) {
			if (!verifPass(pass)) {
				erreur = true;
			}
		}
		var ancienpass = $('#ancienPass');
		var nouveaupass = $('#nouveauPass');
		var nouveaupass2 = $('#nouveauPass2');
		if (!(ancienpass.length == 0)) {
			if (ancienpass.val().length == 0) {
				if ((nouveaupass.val().length != 0)||(nouveaupass2.val().length != 0)) {
					erreur = true;
					$("#erreur_passNonRenseigne").show();
				}
			}
		}

		var profession = $('#Profession');
		if (!(profession.length == 0)) {
			if (!verifProfession(profession)) {
				erreur = true;
			}
		}	
		if (!($('#jour').length == 0)) {
			if (verifPresenceAge()) {
				if (verifJourMois()) {
					if (!verifJourFevrier()) {
						erreur = true;
					}
				}
				else {
					erreur = true;
				}
			}
			else {
				erreur = true;
			}
		}
	
		var zip = $('#ZipCode');
		if (!(zip.length == 0)) {
			if (!verifZip(zip)) {
				erreur = true;
			}
		}

		var adresse = $('#Address');
		if (!(zip.length == 0)) {
			if (!verifAdresse(adresse)) {
				erreur = true;
			}
		}
		var pays = $('#Country');
		if (!(pays.length == 0)) {
			if (!verifPays(pays)) {
				erreur = true;
			}
		}	
		var tel = $('#PhoneNumber');
		if (!(tel.length == 0)) {
			if (!verifTel(tel)) {
				erreur = true;
			}
		}	
		//teste si ancienpass existe	Pas sur qu'il y est besoin de redonner l'autorisation de revalider le form
		if (!(ancienpass.length == 0)) {
			if (!(ancienpass.length == 0)) {
				if (!((ancienpass.val().indexOf("<")>=0)||(ancienpass.val().indexOf(">")>=0))) {
					if (!(nouveaupass.val().length == 0)) {
						if (verifPassBis(nouveaupass)) {
							$("#passNonValide").hide();
							if (!(nouveaupass2.val().length == 0)) {
								if (verifPassBis(nouveaupass2)) {
									$("#passNonValide").hide();
									if (!(nouveaupass.val() == nouveaupass2.val())) {
										erreur = true;
										//tabErreur.push("differentsMdp");
										$("#erreur_differentsMdp").show();
									}
									else {
										$("#erreur_differentsMdp").hide();
									}
								}
								else {
									erreur = true;
									//tabErreur.push("passNonValide");
									$("#erreur_passNonValide").show();
								}
							}
						}
						else {
							erreur = true;
							//tabErreur.push("passNonValide");
							$("#erreur_passNonValide").show();
						}
					}
				}
			}
		}
		
		// Test de vérification mot de passe
		if ((!(ancienpass.length == 0))&&!(ancienpass.val().length == 0)) {
			//if (!(ancienpass.val().length == 0)) {
				if (!((ancienpass.val().indexOf("<")>=0)||(ancienpass.val().indexOf(">")>=0))) {
					$.post('../controller/ajaxAuthentification.php', { auth_email : $("#email").val(), auth_pass : ancienpass.val()},
					function(data) {
						if (data == true) {
							surligne(ancienpass, true);
							$("#erreur_passFaux").hide();
						}
						else {
							surligne(ancienpass, false);
							var erreur = true;
							//tabErreur.push("passFaux");
							$("#erreur_passFaux").show();
						/* Erreur à afficher*/
						}
						
						if (erreur) {
						}
						else {
							$.post('../controller/ajaxVerifEmailEnBase.php', { verifEmailEnBase : $('#email').val()},
							function(data) {
								// s'il n'y a pas de champ email, ça ne pose pas de probleme.
								funct(data, $('#email'));
							
								var fields = $(":input").serializeArray();
								if (action == "modifier") {
									$.post('../controller/ajaxEnregistreur.php', { page : "modifier", d : fields},
									function(data) {
										$('#bulle').hide();
										choixPage('');
									});			
								}
								else {
									$.post('../controller/ajaxEnregistreur.php', { d : fields},
									function(data) {
										$('#bulle').hide();
										choixPage('');
									});
								}
							});
						}
						verifEnCours = false;
					});
				}
				else
				{
					verifEnCours = false;
				}
		}
		
		else {
			if (erreur) {
				verifEnCours = false;
			}
			else {
				$.post('../controller/ajaxVerifEmailEnBase.php', { verifEmailEnBase : $('#email').val()},
				function(data) {
					// s'il n'y a pas de champ email, ça ne pose pas de probleme.
					funct(data, $('#email'));
				
					var fields = $(":input").serializeArray();
					if (action == "modifier") {
						$.post('../controller/ajaxEnregistreur.php', { page : "modifier", d : fields},
						function(data) {
							$('#bulle').hide();
							choixPage('');
						});			
					}
					else {
						$.post('../controller/ajaxEnregistreur.php', { d : fields},
						function(data) {
							$('#bulle').hide();
							choixPage('');
						});
					}
					verifEnCours = false;
				});
			}
		}
		
		
	}
}
function verifCoreg() {
	// On récupère la liste des ids des co-regs
	var tab = $('#champ_hidden').val().split('|');
	
	var tabid = new Array();
	var tabvaleur = new Array();
	
	for (var i = 0; i<tab.length; i++)
	{
		tabid.push(tab[i]);
		
		if($('#choix_'+tab[i]).is(':checked')) 
		{
			tabvaleur.push('Oui');
		}
		else 
		{
			tabvaleur.push('Non');
		}
	}

	$.post('../controller/ajaxEnregistreur.php', { page:'coregistration', coregId : tabid, coregReponse : tabvaleur},
		function(data) {
			if(coregProfil)
			{
				open_popup_affichage(data);
				$('#coregistration').fadeOut ('fast', function () { $('#coregistration').html (''); });
			}
			else
			{
				$('#bulle').hide();
				choixPage('');
			}
		});
}
function verifParrainage() {
	var fields = new Array();
	var tab = new Array();
	var val = "";
	var bool = false;
	var email = $('[class=EmailFilleul]');
	for(i = 0;i<email.length;i++) {
		var z = i+1;
		val = $('#EmailFilleul_' + z).val();
		bool = false;
		for(j =0; j<tab.length; j++) {
			if ((val==tab[j])&&(val)) {
				bool = true;
			}
		}
		if (!bool) {
			fields[i] = val;
			tab[i] = val;
			
			$.post('../controller/ajaxParrainage.php', { d : val},
			function(data) {
					erreur_parr(data)
			});
		}
		
	}

	if (!erreur_parrainage) {
		envoyerData(fields);
	}

}

function erreur_parr(bool) {
	if (!erreur_parrainage) {
		erreur_parrainage=bool;
	}
}
function verifParrainageFinal() {

	var emails = new Array ();
	var fields = $('.EmailFilleul');
	var regex = /^[a-zA-Z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$/;
	
	fields.each (function () {
		if ($(this).val ())
		{
			if(regex.test ($(this).val ()))
			{
			//	alert ($(this).val ());
				emails.push ($(this).val ());
			}
		}
	});
	$.post('../controller/ajaxEnregistreur.php', { page : "parrainage" , d : emails, mess : $('#message_box_par').val ()},
		function(data) {
			$('#bulle').hide();
			open_popup_affichage(data);
		//	choixPage('');
		});
}

// Méthode ne semblant plus présenter le moindre intérêt
function envoyerData(fields) {
	$.post('../controller/ajaxEnregistreur.php', { page : "parrainage" , d : fields},
		function(data) {
			$('#bulle').hide();
			open_popup_affichage(data);
			choixPage('');
		});
}

// Méthode ne semblant plus présenter le moindre intérêt
function envoyerDataFinal(fields) {
	$.post('../controller/ajaxEnregistreur.php', {page : "parrainageFinal", d : fields},
		function(data) {
			open_popup_affichage(data);	// On envoie la bulle
			choixPage('parrainageFinal');
		});
}
function surligne(champ, ok)
{
	var champCase = $('#case_' + champ.attr('id'));
    if(!ok)
	{
		champ.css("border-color","#ff2600");
		champ.css("background-color","#ffffff");
		$('#case_' + champ.attr('name')).css('background',"url('../templates/default/images/img_verif_nok.png') no-repeat");
	}
    else
    {
		champ.css("background-color","#CCCCCC");
		champ.css("border-color","#CCCCCC");
		if (champ.val ())
		{
			$('#case_' + champ.attr('name')).css('background',"url('../templates/default/images/img_verif_ok.png') no-repeat");
		}
		else
		{
			$('#case_' + champ.attr('name')).css('background',"none");
		}
	}

}
function verifLogin() {
	var erreur = false;
	
	$('#erreur_login').hide ();
	
	var regex = /^[a-zA-Z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$/;
	if(!regex.test($("#auth_email").val ()))
	{
		$('#erreur_adresseEmailLogin').show ();
		erreur = true;
	}
	else
	{
		$('#erreur_adresseEmailLogin').hide ();
	}
	
	
	
	if (($('#auth_pass').val().indexOf("<")>=0)||($('#auth_pass').val().indexOf(">")>=0)||($('#auth_pass').val().length < 6 )) {
		$('#erreur_passLogin').show ();
		erreur = true;
	}
	else {
		$('#erreur_passLogin').hide ();
	}
	
	if (!erreur)
	{
		$.post('../controller/ajaxAuthentification.php', { auth_email : $("#auth_email").val(), auth_pass : $("#auth_pass").val()},
			function(data) {
				if (data == "1") {
					choixPage('profil');
				}
				else {
					$('#erreur_login').show ();
				}
		});
	}
}

function verifAncienPass(champ) {
	if (!((champ.val().indexOf("<")>=0)||(champ.val().indexOf(">")>=0))) {
		$.post('../controller/ajaxAuthentification.php', { auth_email : $("#email").val(), auth_pass : champ.val()},
		function(data) {
			if (data == true) {
				surligne(champ, true);
				return true;
			}
			else if (data == false) {
				surligne(champ, false);
				return false;
			/* Erreur à afficher*/
			}
			else {
			}
		});
	}
}

function verifGagnant(d) {
	var result = "false";
	if (d == true) 
	{
		result = "true";
	}
	$.post('../controller/ajaxVerifGagnant.php', { gagnant : result},
		function(data) {
			if (d) {
				// faire un modal
				open_popup_affichage(data);
				setTimeout(function() {
                choixPage('profil');
            }, delaiGain);				
			}
			else {
				var nbpr = $('#nbPartiesRestantes').html();
				$('#nbPartiesRestantes').empty();
				$('#nbPartiesRestantes').append(nbpr);
				if (nbpr != 1) {
					$('#lien').empty();
					$('#lien').append(data);
				}
				else {
					$('#nbPartiesRestantes').empty();
					$('#messageResultat').empty();
					$('#messageResultat').append(data);
					$('#lien').empty();
					$('#lien').append("<a class=\"choixPage\" href=\"\" onclick=\"choixPage('parrainageFinal');return false;\">Cliquez ici</a>");
					$('#lien').css("margin-top","20px");
					$('#lien').css("margin-left","80px");
				}
			}
	});	
} 

function choixPagePasFrom() {
$.post('../controller/ajaxEnregistreur.php', { },
		function(data) {
			$('#bulle').hide();
			choixPage('');
		});
}


function addMessage(message)
{
	document.getElementById('bulle').style.display = "block";
	document.getElementById('erreurForm').innerHTML = message;
}

function decr(mess)
{
	var nb = jQuery('#nbPartiesRestantes').html () - 1;
	jQuery('#nbPartiesRestantes').html (nb);
}

function won(message)
{
	verifGagnant(true);
}

function lost(mess)
{
	verifGagnant(false);
}

function sendToActionScript(value) {
        thisMovie("ExternalInterfaceExample").sendToActionScript(value);
}

function isReady() {
	return jsReady;
}
function verifParrainageEncore() {
var fields = new Array();
fields[0] = $(':checked').val();

}

function montrerOi() {
	$('#contenu_oi').slideDown('slow');
	$('#contenuParrainage').slideUp('slow');
	$("#oi_wrapper:hover").css("color","black");
	
	$('#montrezOiOpen').html ("Fermez votre carnet d'adresse");
}

function montrerParrainage() {
	$('#parrainage_wrapper .lien_ouvrir').empty();
	$('#contenuParrainage').slideDown('slow');
	$('#contenu_oi').slideUp('slow');
	$('#montrezOiOpen').html ("Invitez votre carnet d'adresse");
}

/**
 * Fonction permettant de supprimer le panier choisi, et de revenir sur la page home
 */
function changerPanier () {
	$.post('../controller/ajaxChoixPanier.php', {},
function(data) {
		
		if (data == 1)
		{
			choixPage ('home');
		}
	});
}

