
var id_util = null;
var login = null;
var droits = null;

var valeur = LireCookie("vosges-sport");
if (valeur != null) {
	var pos = 1;
	var mot = "";
	
	for (i=0; i < valeur.length; i++) {
		if (valeur.charAt(i) == "," && mot != "") {
			switch (pos) {
			case 1:
				id_util = mot;
				break;
			case 2:
				login = mot;
				break;
			case 3:
				droits = mot;
				break;
			
			}
			pos++;
			mot = "";		
		} else {
			mot += valeur.charAt(i);
		}
	}
}

function getCookieVal(offset)
{
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function LireCookie(nom)
{
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}

function cookie_exist(nom)
{
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg)
			return true;
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0)
			break;
	}
	return false;
}

function EffaceCookie(nom)
{
	date=new Date;
	date.setFullYear(date.getFullYear()-1);
	document.cookie="vosges-sport=null; expires=" + date;
}

function deconnect()
{
	EffaceCookie("vosges-sport");
	document.location = "index.php";
}

function get_banniere ()
{
	if (id_util != null) {
		document.write(login);	
	} else {
		document.write('<a href="javascript: get_identification ()">membre</a>');
	}	
}

function get_fenetre_ident ()
{
	if (id_util == null) {
		document.write('<div id="transparent_bleu">');
		document.write('</div>');
		document.write('<form method="POST" name="ident" onSubmit="return entrer()" action="index.html">');
		document.write('<div id="identification">');
		document.write('<div class="haut">');
		document.write('<h1>Identification</h1>');
		document.write('<a href="nouveau_membre.html">Nouveau membre</a>');
		document.write('</div>');
		document.write('<div class="bas">');
		document.write('<div style="height: 150px">');
		document.write('<table cellpadding="0" cellspacing="0" border="0">');
		document.write('<tr>');
		document.write('<td height="30" align="right">Votre login :</td>');
		document.write('<td width="20" align="left" id="login_erreur">&nbsp;</td>');
		document.write('<td align="left"><input type="text" onKeyPress="ActionTouche(event)" name="login" maxlength="30" size="20"></td>');
		document.write('</tr>');
		document.write('<tr>');
		document.write('<td>&nbsp;</td>');
		document.write('<td>&nbsp;</td>');
		document.write('<td id="msg_erreur_login" class="message">&nbsp;</td>');
		document.write('</tr>');
		document.write('<tr>');
		document.write('<td height="30">Votre mot de passe :</td>');
		document.write('<td align="left" id="passw_erreur">&nbsp;</td>');
		document.write('<td><input type="password" name="passw" maxlength="30" size="20"></td>');
		document.write('</tr>');
		document.write('<tr>');
		document.write('<td>&nbsp;</td>');
		document.write('<td>&nbsp;</td>');
		document.write('<td id="msg_erreur_passw" class="message">&nbsp;</td>');
		document.write('</tr>');
		document.write('</table>');
					
		document.write('<DIV class="boutons">');
		document.write('<DIV onClick="entrer()" class="formga" style="WIDTH: 50%;">');
		document.write('<input type="submit" value="Entrer">');
		document.write('</DIV>');
		document.write('<DIV class="formdr" style="WIDTH: 50%; text-align:right">');
		document.write('<input type="button" onClick="fermer_identification()" value="Annuler">');
		document.write('</DIV>');
		document.write('</DIV>');
		document.write('</div>');
		document.write('</div>');
		document.write('</div>');
		document.write('</form>');
		document.write('<form method="POST" name="ident2" action="index.html">');
		document.write('<input type="hidden" name="resultat">');
		document.write('</form>');
	}
}

function init_identification () {

	document.getElementById("login_erreur").innerHTML = "";
	document.getElementById("msg_erreur_login").innerHTML = "";
	document.forms["ident"].login.style.background = "#FFFFFF";
	document.forms["ident"].login.focus();
	document.getElementById("passw_erreur").innerHTML = "";
	document.getElementById("msg_erreur_passw").innerHTML = "";
	document.forms["ident"].passw.style.background = "#FFFFFF";
}

function get_identification () {

	afficher_scroll("identification");
	init_identification ();
	document.getElementById("transparent_bleu").style.width = document.body.clientWidth + "px";
	document.getElementById("transparent_bleu").style.height = document.body.scrollHeight + "px";
	document.getElementById("transparent_bleu").style.visibility = "visible";
}

function fermer_identification () {

	effacer("identification");
	document.getElementById("transparent_bleu").style.visibility = "hidden";
}

function entrer () {

	var url = "admin/utilisateur/get_identification.php?login=" + document.forms["ident"].login.value + "&passw=" + document.forms["ident"].passw.value;
	init_identification ();
	makeRequest_identification(url, "resultat_identification");
	return false;
}

function makeRequest_identification(url, cible)
{
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (!http_request) {
		alert('Abandon :( Impossible de créer une instance XMLHTTP');
		return false;
	}
	http_request.onreadystatechange = function() { alertContents_identification(http_request, cible); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function alertContents_identification(http_request, cible)
{
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			switch (http_request.responseText) {
				case "erreur login":
					document.getElementById("login_erreur").innerHTML = "<img src='images/erreur_gris.gif'>";
					document.getElementById("msg_erreur_login").innerHTML = "Login inexistant<br>&nbsp;";
					document.forms["ident"].login.style.background = "#FFFDDC";
					document.forms["ident"].login.focus();
					break;
				case "erreur passw":
					document.getElementById("passw_erreur").innerHTML = "<img src='images/erreur_gris.gif'>";
					document.getElementById("msg_erreur_passw").innerHTML = "mot de passe inexact<br>&nbsp;";
					document.forms["ident"].passw.style.background = "#FFFDDC";
					document.forms["ident"].passw.focus();
					break;
				default:
					document.cookie="vosges-sport=" + http_request.responseText;
					document.location = "index.html";
					break;
			}
		} else {
			alert('Un problème est survenu avec la requête.');
		}
	}
}

function init_nouveau() {
	
	document.forms["ajout_utilisateur"].login.style.background = "#FFFFFF";
	document.forms["ajout_utilisateur"].passw.style.background = "#FFFFFF";
	document.forms["ajout_utilisateur"].passw2.style.background = "#FFFFFF";
	document.forms["ajout_utilisateur"].email.style.background = "#FFFFFF";
	document.forms["ajout_utilisateur"].sexe.style.background = "#FFFFFF";
	document.forms["ajout_utilisateur"].localite.style.background = "#FFFFFF";
	document.forms["ajout_utilisateur"].jour.style.background = "#FFFFFF";
	document.forms["ajout_utilisateur"].mois.style.background = "#FFFFFF";
	document.forms["ajout_utilisateur"].annee.style.background = "#FFFFFF";
	
	document.getElementById("login_erreur").innerHTML = "&nbsp;";
	document.getElementById("msg_erreur_login").innerHTML = "&nbsp;";
	document.getElementById("passw_erreur").innerHTML = "&nbsp;";
	document.getElementById("msg_erreur_passw").innerHTML = "&nbsp;";
	document.getElementById("passw2_erreur").innerHTML = "&nbsp;";
	document.getElementById("msg_erreur_passw2").innerHTML = "&nbsp;";
	
	document.getElementById("email_erreur").innerHTML = "&nbsp;";
	document.getElementById("msg_erreur_email").innerHTML = "&nbsp;";
	document.getElementById("sexe_erreur").innerHTML = "&nbsp;";
	document.getElementById("msg_erreur_sexe").innerHTML = "&nbsp;";
	document.getElementById("localite_erreur").innerHTML = "&nbsp;";
	document.getElementById("msg_erreur_localite").innerHTML = "&nbsp;";
	document.getElementById("dnais_erreur").innerHTML = "&nbsp;";
	document.getElementById("msg_erreur_dnais").innerHTML = "&nbsp;";
}

function nouveau() {
	
	var ok = true;
	
	init_nouveau();
	
	/* login */
	form = "ajout_utilisateur";
	champ = "login";
	txt_champ = get_txt_champ (form, champ);
			
	if (isVide(txt_champ)) {
		erreur (form, champ, "veuillez saisir un login");
		ok = false;
	} else if (txt_champ.length < 6) {
		erreur (form, champ, "votre login doit comporter au moins 6 caractères");
		ok = false;
	}
	/* password */
	champ = "passw";
	txt_champ = get_txt_champ (form, champ);
				
	if (isVide(txt_champ)) {
		erreur (form, champ, "veuillez saisir un mot de passe");
		ok = false;
	} else if (txt_champ.length < 6) {
		erreur (form, champ, "le mot de passe doit comporter au moins 6 caractères");
		ok = false;
	}
	/* password 2 */
	champ = "passw2";
	txt_champ2 = get_txt_champ (form, champ);
				
	if (txt_champ != txt_champ2) {
		erreur (form, champ, "confirmation incorrecte");
		ok = false;
	}
	/* email */
	champ = "email";
	txt_champ = get_txt_champ (form, champ);
				
	if (isVide(txt_champ)) {
		erreur (form, champ, "veuillez saisir votre adresse mail");
		ok = false;
	}
	/* sexe */
	champ = "sexe";
	txt_champ = get_txt_champ (form, champ);
		
	if (txt_champ == 0) {
		erreur (form, champ, "veuillez indiquer votre sexe");
		ok = false;
	}
	/* localite */
	champ = "localite";
	txt_champ = get_txt_champ (form, champ);
				
	if (isVide(txt_champ)) {
		erreur (form, champ, "veuillez saisir votre localité");
		ok = false;
	}
	/* jour */
	champ = "jour";
	txt_champ = get_txt_champ (form, champ);
		
	if (txt_champ == 0) {
		erreur (form, champ, "veuillez saisir votre date de naissance");
		ok = false;
	}
	/* mois */
	champ = "mois";
	txt_champ = get_txt_champ (form, champ);
		
	if (txt_champ == 0) {
		erreur (form, champ, "veuillez saisir votre date de naissance");
		ok = false;
	}
	/* année */
	champ = "annee";
	txt_champ = get_txt_champ (form, champ);
		
	if (txt_champ == 0) {
		erreur (form, champ, "veuillez saisir votre date de naissance");
		ok = false;
	}
	
	if (ok) {
		var url = "admin/utilisateur/get_ajout_util.php?";
		url += "login=" + document.forms["ajout_utilisateur"].login.value;
		url += "&passw=" + document.forms["ajout_utilisateur"].passw.value;
		url += "&email=" + document.forms["ajout_utilisateur"].email.value;
		url += "&sexe=" + document.forms["ajout_utilisateur"].sexe.value;
		url += "&localite=" + document.forms["ajout_utilisateur"].localite.value;
		url += "&jour=" + document.forms["ajout_utilisateur"].jour.value;
		url += "&mois=" + document.forms["ajout_utilisateur"].mois.value;
		url += "&annee=" + document.forms["ajout_utilisateur"].annee.value;
		makeRequest_nouveau(url);
	}
}

function erreur (form, champ, msg) {
	if (champ == "jour" || champ == "mois" || champ == "annee") {
		document.getElementById("dnais_erreur").innerHTML = "<img src='images/erreur.gif'>";
		document.getElementById("msg_erreur_dnais").innerHTML = msg + "<br>&nbsp;";
	} else {
		document.getElementById(champ + "_erreur").innerHTML = "<img src='images/erreur.gif'>";
		document.getElementById("msg_erreur_" + champ).innerHTML = msg + "<br>&nbsp;";
	}
	document.forms[form].elements[champ].style.background = "#FFFDDC";
	document.forms[form].elements[champ].focus();
}

function makeRequest_nouveau(url)
{
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (!http_request) {
		alert('Abandon :( Impossible de créer une instance XMLHTTP');
		return false;
	}
	http_request.onreadystatechange = function() { alertContents_nouveau(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function alertContents_nouveau(http_request)
{
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			switch (http_request.responseText) {
				case "votre email est incorrect":
					document.getElementById("email_erreur").innerHTML = "<img src='images/erreur.gif'>";
					document.getElementById("msg_erreur_email").innerHTML = "email incorrect<br>&nbsp;";
					document.forms["ajout_utilisateur"].email.style.background = "#FFFDDC";
					document.forms["ajout_utilisateur"].email.focus();
					break;
				case "login deja existant":
					document.getElementById("login_erreur").innerHTML = "<img src='images/erreur.gif'>";
					document.getElementById("msg_erreur_login").innerHTML = "Login déjà existant<br>&nbsp;";
					document.forms["ajout_utilisateur"].login.style.background = "#FFFDDC";
					document.forms["ajout_utilisateur"].login.focus();
					break;
				default:
					document.forms["ajout_utilisateur"].id_util.value = http_request.responseText;
					window.frames["iform"].document.forms[0].id_util.value = document.forms["ajout_utilisateur"].id_util.value;
					window.frames["iform"].document.forms[0].submit();
					var login = document.forms["ajout_utilisateur"].login;
					alert("bravo " + login + ", votre inscription a reussi, vous pouvez vous connecter et acceder a l'espace membre");
					document.location = "index.html";
					break;
			}
		} else {
			alert('Un problème est survenu avec la requête.');
		}
	}
}

function trim(s) {
    return s.replace(/^\s+/, '').replace(/\s+$/, '');
}

function isVide(txt_champ)
{
	if (trim(txt_champ) == "")
		return true;
	return false;
}

function get_txt_champ (form, champ) {
	return document.forms[form].elements[champ].value;
}
