var erreur_inscription = "";

function CE_CRE(param){
	if(param == "show"){
		document.getElementById("phrase_club_CRE").innerHTML = "Précisez le nom de votre centre : ";
		document.getElementById("input_club_CRE").innerHTML = "<input type='text' name='nom_club_CRE' id='nom_club_CRE' />";
		document.getElementById("departement_club_CRE").innerHTML = "<select name='departement_CRE' id='departement_CRE'><option value='0'>D&eacute;partement</option><option value='1'>Calvados</option><option value='2'>Eure</option><option value='3'>Orne</option><option value='4'>Manche</option><option value='5'>Seine maritime</option></select>"
		document.getElementById("departement_club_CRE").innerHTML += "<input type='button' value='OK' onclick='recherche_club();' />";
		document.getElementById("bouton_valider_inscription").style.display = "none";
	}else{
		document.getElementById("phrase_club_CRE").innerHTML = "";
		document.getElementById("input_club_CRE").innerHTML = "";
		document.getElementById("departement_club_CRE").innerHTML = "";
		document.getElementById("bouton_valider_inscription").style.display = "block";
	}
}

var http_recherche_club =  new getHTTPObject();

function recherche_club(){
	var nom = document.getElementById("nom_club_CRE").value;
	var departement = document.getElementById("departement_CRE").value;
	if(departement==0){
		alert("Veuillez choisir un département");
		return false;
	}
	
	var envoi = "nom="+nom+"&departement="+departement;
	
	http_recherche_club.open("POST", "./recherche_club.php" , true);
	http_recherche_club.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	http_recherche_club.send(envoi);
	http_recherche_club.onreadystatechange = retour_recherche_club
}

function retour_recherche_club(){
	if(http_recherche_club.readyState == 4){
		var select_clubs = document.createElement("select");
			select_clubs.id = "club_CRE_trouve";
			select_clubs.name = "club_CRE_trouve";
		document.getElementById("statut_club_CRE").innerHTML = "";
		document.getElementById("statut_club_CRE").appendChild(select_clubs);
		
		var decoupage = http_recherche_club.responseText.split("||");
		for(var i=0;i<=parseInt(decoupage.length)-2;i++){
			var decoupage2 = decoupage[i].split(",");
			var id_di = decoupage2[0];
			var nom = decoupage2[1];
			var option = document.createElement("option");
				option.id = "club_"+id_di;
				option.value = id_di;
				option.innerHTML = nom;
			document.getElementById("club_CRE_trouve").appendChild(option);
		}
		document.getElementById("bouton_valider_inscription").style.display = "block";
	}
}

function check_info(obj,a_verif){
	var valeur = obj.value;
	var verif = document.getElementById(a_verif).value;
	erreur = "";
	if(valeur != verif){
		alert("Les champs doivent être similaires pour "+a_verif+" !");
		return 1;
	}
	if(a_verif == "mail"){
		var arobase = valeur.indexOf("@");
		var point = valeur.lastIndexOf(".");
		if((arobase < 3)||(point + 2 > valeur.length)||(point < arobase+3)){
			alert("L'adresse e-mail que vous avez renseigné est invalide");
			return 1;
		}
	}
}

var http_validation_inscription = new getHTTPObject();

function validation_inscription(){
	
	var login = document.getElementById("login").value;
	var mdp = document.getElementById("pass").value;
	var mail = document.getElementById("mail").value;
	var type_client = document.getElementById("type_client").value;
	if(type_client == 0){
		alert("Veuillez définir votre type d'adhésion");
		return false;
	}
	
	var tableau_verif = new Array("login","pass","mail");
	
	for(var i=0;i<=2;i++){
		if(check_info(document.getElementById(tableau_verif[i]+"_confirm"),tableau_verif[i])){
			return false;
		}
		if(document.getElementById(tableau_verif[i]+"_confirm").value == "" || document.getElementById(tableau_verif[i]).value == ""){
			alert("Tous les champs doivent être remplis pour pouvoir valider votre inscription");
			return false;
		}
	}
	var envoi = "login="+login+"&mdp="+mdp+"&mail="+mail+"&type_client="+type_client;
	
	if(type_client == 1){
		if(!window.confirm("Certifiez vous bien que vous êtes "+document.getElementById("club_"+document.getElementById("club_CRE_trouve").value).innerHTML)){
			return false;
		}else{
			envoi += "&centre="+document.getElementById("club_CRE_trouve").value;
			envoi += "&departement_centre="+document.getElementById("departement_CRE").value;
		}
	}
	
	var check_box = document.getElementById("newsletter_I").checked;
	if(check_box == true){
		envoi += "&newsletter_I=1";
	}
	
	http_validation_inscription.open("POST", "./admin_validation_inscription.php" , true);
	http_validation_inscription.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	http_validation_inscription.send(envoi);	
	http_validation_inscription.onreadystatechange = retour_validation_inscription
}

function retour_validation_inscription(){
	if(http_validation_inscription.readyState == 4){
		document.getElementById("total").innerHTML = http_validation_inscription.responseText;
	}
}

var http_log_client = new getHTTPObject();

function log_client(login,mdp){
//alert("");	
var envoi_log_client = "login="+login+"&mdp="+mdp;
	
	http_log_client.open("POST", "./log_client.php" , true);
	http_log_client.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	http_log_client.send(envoi_log_client);	
	http_log_client.onreadystatechange = retour_log_client
}

function retour_log_client(){
	if(http_log_client.readyState == 4){
//alert(http_log_client.responseText);		
ouvrir(http_log_client.responseText);
	}
}

var http_MOD_infos_simples = new getHTTPObject();

function MOD_infos_simples(){
	var tableau_verif = new Array("login","pass","mail");
	for(var i=0;i<=2;i++){
		if(check_info(document.getElementById(tableau_verif[i]+"_confirm"),tableau_verif[i])){
			return false;
		}
	}
		
	var envoi_MOD_infos_simples = "login="+document.getElementById("login").value+"&mdp="+document.getElementById("pass").value+"&mail="+document.getElementById("mail").value;
		envoi_MOD_infos_simples += "&id_di="+document.getElementById("id_personne").value;
		//envoi_MOD_infos_simples += "&id_newsletter="+document.getElementById("id_newsletter").value;
		
	if(document.getElementById("newsletter_OK").checked == true){
		envoi_MOD_infos_simples += "&newsletter=1";
	}
	
	http_MOD_infos_simples.open("POST","./modifications_infos_simples.php",true);
	http_MOD_infos_simples.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	http_MOD_infos_simples.send(envoi_MOD_infos_simples);
	http_MOD_infos_simples.onreadystatechange = retour_MOD_infos_simples
}

function retour_MOD_infos_simples(){
	if(http_MOD_infos_simples.readyState == 4){
		if(http_MOD_infos_simples.responseText == 0){
			alert("Une erreur est survenue");
		}else{
			var decoupage = http_MOD_infos_simples.responseText.split("|");
			log_client(decoupage[0],decoupage[1]);
		}
	}
}

var http_modification_club_new = new getHTTPObject();

function modification_club_new(id_club,dep_club){
	var envoi = "departement="+dep_club+"&numero="+id_club;
		var inputs_all = document.getElementById("modification_du_club").getElementsByTagName("input");
		var select_all = document.getElementById("modification_du_club").getElementsByTagName("select");
		for(var i=0;i<=parseInt(inputs_all.length)-1;i++){
			if(inputs_all[i].type == "text"){
				envoi += "&"+inputs_all[i].id+"="+escape(inputs_all[i].value);
			}
			if(inputs_all[i].type == "checkbox"){
				if(inputs_all[i].checked == true){
					envoi += "&"+inputs_all[i].id+"="+escape(inputs_all[i].getAttribute("name"));
				}else{
					envoi += "&"+inputs_all[i].id+"=";
				}
			}
		}
		for(var j=0;j<=parseInt(select_all.length)-1;j++){
			envoi += "&"+select_all[j].id+"="+select_all[j].value;
		}
		//alert(envoi);
		
		http_modification_club_new.open("POST","./modification_club_traitement.php",true);
		http_modification_club_new.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		http_modification_club_new.send(envoi);
		http_modification_club_new.onreadystatechange = retour_modification_club_new
}

function retour_modification_club_new(){
	if(http_modification_club_new.readyState == 4){
		var decoupage = http_modification_club_new.responseText.split("||");
			alert(decoupage[0]);
			log_client(decoupage[1],decoupage[2]);
	}
}

function valid(id,e){
	if(window.event) // IE
		keynum = e.keyCode
	else if(e.which) // Netscape/Firefox/Opera
		keynum = e.which
	if(keynum==13){
		log_client(document.getElementById("login_B").value,document.getElementById("password_B").value);
	}
}