function toggle(div)
{
	$(div).slideToggle("fast");
}

// mise en cache des images lourdes
function preload()
{
	heavyImage1 = new Image();
	heavyImage2 = new Image();
	heavyImage1.src = "images/pillier-G.png";
	heavyImage2.src = "images/pillier-D.png";
}


function afficher(div)
{	
	// alert(document.getElementById(div).style.display);
	if(document.getElementById(div).style.display == 'none')
	{
		$("#tooltipMonde1").fadeOut("normal");
		$("#tooltipMonde2").fadeOut("normal");
		$("#tooltipMonde3").fadeOut("normal");
		$("#"+div).fadeIn("normal");
	}
}

function resetbulles()
{
	$("#tooltipMonde1").fadeOut("normal");
	$("#tooltipMonde2").fadeOut("normal");
	$("#tooltipMonde3").fadeOut("normal");
}

function SelectMonde(num)
{
	if(num == "suivant")
	{
		if(document.getElementById("inputOcean").value == 1) { num = 3; }
		else {num = 1*document.getElementById("inputOcean").value - 1}
	}
	if(num == "precedent")
	{
		if(document.getElementById("inputOcean").value == 3) { num = 1; }
		else {num = 1*document.getElementById("inputOcean").value + 1}
	}
	
	document.getElementById("monde3").style.color = "#aaa";
	document.getElementById("monde2").style.color = "#aaa";
	document.getElementById("monde1").style.color = "#aaa";
	if (num == 1)
	{
		document.getElementById("monde1").style.color = "#fff";
		document.getElementById("inputOcean").value="1";
		document.getElementById("conteneurInscription").innerHTML = '<a href="javascript:inscription();">Inscription</a>';
		ScrollSection('monde3-pane');
	}
	else if (num == 2)
	{
		document.getElementById("monde2").style.color = "#fff";
		document.getElementById("inputOcean").value="2";
		document.getElementById("conteneurInscription").innerHTML = '<a href="javascript:inscription();">Inscription sur ticket</a>';
		ScrollSection('monde2-pane');
	}
	else if (num == 3)
	{
		document.getElementById("monde3").style.color = "#fff";
		document.getElementById("inputOcean").value="3";
		document.getElementById("conteneurInscription").innerHTML = '<a href="javascript:inscription();">Inscription</a>';
		ScrollSection('monde1-pane');
	}
}

function cacheModale(test)
{
	$('#pillierG').animate({
		left: '370',
	}, 1000, function() {
		$('#pillierD').animate({opacity: '0'});
	});
	$('#pillierD').animate({
		right: '370',
		}, 1000, function() {
		$('#pillierG').animate({opacity: '0'});
	});
	$('#modale .centre').animate({
		opacity: '0',
		marginLeft: '453',
		width: '0',
	  }, 1000, function() { 
			if(test == "subForm") document.subForm.submit();
	  });
	}
function affModale(){
	$('#pillierG').animate({
		opacity: '100',
		left: '-40',
	}, 1000, function() { });
	  $('#pillierD').animate({
		opacity: '100',
		right: '-73',
	  }, 1000, function() { });
	$('#modale .centre').animate({
		opacity: '100',
		marginLeft: '0',
		width: '906',
	  }, 1000, function() { });
}

function inscription(){ document.location.href="inscription.php?w="+document.getElementById("inputOcean").value; }

function infoChange(color, size, alignment, text)
{
	$("#information").fadeOut("fast", function() {
		document.getElementById("information").innerHTML = text;
		document.getElementById("information").style.color = color;
		document.getElementById("information").style.fontSize = size;
		document.getElementById("information").style.textAlign = alignment;
	});
	$("#information").fadeIn("fast");
}

function fieldFill(id, type)
{
	if(type == "error") document.getElementById(id).innerHTML = "<img src='images/inscription-error.png' alt='ko' />";
	else if(type == "ok") document.getElementById(id).innerHTML = "<img src='images/inscription-tick.png' alt='ok' />";
}
function fieldCheck(name, monde)
{
	if(name == "nom") { 
		if(document.subForm.nom.value.length > 25) { fieldFill("tdnom", "error"); } else { fieldFill("tdnom", "ok"); }
	}
	else if(name == "prenom"){ 
		if(document.subForm.prenom.value.length > 25) { fieldFill("tdprenom", "error"); } else { fieldFill("tdprenom", "ok"); }
	}
	else if(name == "pseudo"){
		if(document.subForm.pseudo.value.match(/^[a-zA-Z0-9_-]{3,18}$/) == null) { fieldFill("tdpseudo", "error"); }
		else {
			// requete ajax pour vérifier la dispo du pseudo
			$.get("inscription.php?action=pseudoCheck&w="+monde+"&pseudo="+document.subForm.pseudo.value, function(retour) {
				// si le pseudo existe, on met une erreur
				if(retour == "OK") {
					fieldFill("tdpseudo", "error"); 
					alert("Ce pseudo est déjà utilisé");
					}
				else if(retour == "KO") { fieldFill("tdpseudo", "ok"); }
			});
		}
	}
	else if(name == "mail") {
		if (document.subForm.mail.value.match(
			/^[a-zA-Z0-9][a-zA-Z0-9._-]*@([a-zA-Z][a-zA-Z0-9_\-]*\.)*[a-z0-9]+[a-z0-9\-]+\.[a-z]{2,6}$/
			) == null) { fieldFill("tdmail", "error"); }
		else {
			// la requete ajax pour tester si le mail est bien libre
			$.get("inscription.php?action=mailCheck&w="+monde+"&mail="+document.subForm.mail.value, function(retour) {
				// si le pseudo existe, on met une erreur
				if(retour == "OK") {
					fieldFill("tdmail", "error"); 
					alert("Cette adresse mail est déjà utilisée"); 
					}
				else if(retour == "KO") { fieldFill("tdmail", "ok"); }
			});
		}
	}
	else if(name == "pass") {
		if(document.subForm.pass.value.length < 6) { fieldFill("tdpass", "error"); }
		else { fieldFill("tdpass", "ok"); }
	}
	else if(name == "pass_c") {
		if(document.subForm.pass_c.value != document.subForm.pass.value
			|| document.subForm.pass_c.value.length < 6) { fieldFill("tdpass_c", "error"); }
		else { fieldFill("tdpass_c", "ok"); }
	}
	else if(name == "parrain")
	{
		if(document.subForm.parrain.value == "") { fieldFill("tdparrain", "ok"); }
		else {
			// la requete ajax pour tester si le existe bien
			$.get("inscription.php?action=parrainCheck&w="+monde+"&parrain="+document.subForm.parrain.value, function(retour) {
				// si le parrain existe, c'est ok
				if(retour == "KO") {
					fieldFill("tdparrain", "error"); 
					alert("Ce membre n'existe pas dans ce monde"); 
					}
				else if(retour == "OK") { fieldFill("tdparrain", "ok"); }
			});
		}
	}
	
}
function validationInscription(monde)
{
	if(document.subForm.nom.value.length > 25) {
		infoChange("#fcc", "large", "center", "Votre nom est trop long");
		return false;
	}
	if(document.subForm.prenom.value.length > 25) {
		infoChange("#fcc", "large", "center", "Votre prénom est trop long");
		return false;
	}
	if(document.subForm.pseudo.value.match(/^[a-zA-Z0-9_-]{3,18}$/) == null) {
		infoChange("#fcc", "large", "center", "Erreur dans votre pseudo");
		return false;
	}
	if(document.subForm.pass.value.length < 6) { 
		infoChange("#fcc", "large", "center", "Votre mot de passe est trop court");
		return false;
	}
	if(document.subForm.pass_c.value != document.subForm.pass.value
		|| document.subForm.pass_c.value.length < 6) {
		infoChange("#fcc", "large", "center", "Votre mot de passe est mal confirmé");
		return false;	
	}
	if (document.subForm.mail.value.match(
			/^[a-zA-Z0-9][a-zA-Z0-9._-]*@([a-zA-Z][a-zA-Z0-9_\-]*\.)*[a-z0-9]+[a-z0-9\-]+\.[a-z]{2,6}$/
			) == null) { 
		infoChange("#fcc", "large", "center", "Votre E-mail est incorrect");
		return false;	
	}
	if(document.subForm.charte.checked == false) {
		infoChange("#fcc", "large", "center", "Vous devez accepter la charte pour vous inscrire");
		return false;	
	}
	// un peu de jquery...
	if ($.firstLoadingPseudo != false)
	{
		$.get("inscription.php?action=pseudoCheck&w="+monde+"&pseudo="+document.subForm.pseudo.value, function(retour) {
			if(retour == "OK") { alert("Ce pseudo est déjà utilisé"); document.subForm.pseudo.focus(); }
			else if(retour == "KO"){
				$.firstLoadingPseudo = false;
				validationInscription(monde);
			}
		});
		return false;
	}
	// encore un peu...
	else if ($.firstLoadingMail != false)
	{
		$.get("inscription.php?action=mailCheck&w="+monde+"&mail="+document.subForm.mail.value, function(retour) {
			if(retour == "OK") { alert("Cette adresse mail est déjà utilisée"); document.subForm.mail.focus(); }
			else if(retour == "KO"){
				$.firstLoadingMail = false;
				validationInscription(monde);
			}
		});
		return false;
	}
	// et encore un peu...
	else if ($.firstLoadingParrain != false)
	{
		if(document.subForm.parrain.value == "") {
			$.firstLoadingParrain = false;
			validationInscription(monde); 
		}
		else 
		{
			$.get("inscription.php?action=parrainCheck&w="+monde+"&parrain="+document.subForm.parrain.value, function(retour) {
				if(retour == "KO") { alert("Ce membre n'existe pas dans ce monde"); document.subForm.parrain.focus(); }
				else if(retour == "OK"){
					$.firstLoadingParrain = false;
					validationInscription(monde);
				}
			});
		}
		return false;
	}
	else { cacheModale("subForm"); }
	return false;
}

function decompte(timestamp, tsActuel, div)
{
    var div = document.getElementById(div);

    var SECONDE = 1;
    var MINUTE = 60*SECONDE;
    var HEURE = 60*MINUTE;
    var JOUR = 24*HEURE;

    this.decremente = function()
    {
        var tsRestant = timestamp - tsActuel;
        if (tsRestant >= 0)
        {
            tsActuel++;
            
            var j = ((parseInt(tsRestant / JOUR) < 10) ? parseInt(tsRestant / JOUR) : parseInt(tsRestant / JOUR));
            var h = ((parseInt((tsRestant % JOUR) / HEURE) < 10) ? "0"+parseInt((tsRestant % JOUR) / HEURE) : parseInt((tsRestant % JOUR) / HEURE));
            var m = ((parseInt(((tsRestant % JOUR) % HEURE) / MINUTE) < 10) ? "0"+parseInt(((tsRestant % JOUR) % HEURE) / MINUTE) : parseInt(((tsRestant % JOUR) % HEURE) / MINUTE));
            var s = ((parseInt((((tsRestant % JOUR) % HEURE) % MINUTE) / SECONDE) < 10) ? "0"+parseInt((((tsRestant % JOUR) % HEURE) % MINUTE) / SECONDE) : parseInt((((tsRestant % JOUR) % HEURE) % MINUTE) / SECONDE));

            div.innerHTML = parseInt(j)+"j - "+parseInt(h)+"h"+m+"m"+s+"s";
        }
        else
        {
            div.innerHTML = "...";
        }
    }
}
function refresh()
{
    decompte.decremente();
    setTimeout(refresh, 1000);
}

function fadeOutIn(id) {
    $("#" + id).fadeOut("slow", function(){
        fadeInOut(id);
    });
} function fadeInOut(id) {
    $("#" + id).fadeIn("slow", function(){
        fadeOutIn(id);
    });
}
