// JavaScript Document

var PHOTO_SELEC=null;
var timer=null;
var check_content="";

function effacerContenu(elem) {
	elem.value="";
}


function annuler() {
	//window.location.href="index.php";
	document.getElementById("inscription-client").innerHTML="";
}

function annulerLogin() {
	new net.ContentLoader("needed/seconnecter.php",innerOut,null,document.getElementById("saisie-login"));
}
function seConnecter() {
	new net.ContentLoader("connexion.php",innerOut,null,document.getElementById("saisie-login"));
}
function validerLogin() {
	
	
	//alert(document.getElementById("login").value);
	//alert(document.getElementById("pass").value);
	var login=document.getElementById("login").value;
	var pass=document.getElementById("pass").value;
	
	if (login != ""  && pass != "" && login != "identifiant") {
		new net.ContentLoader("needed/checkLogin.php?login="+login+"&pass="+pass,loginOut,null,document.getElementById("saisie-login"));
	}
}

function sinscrire() {
	new net.ContentLoader("needed/inscription.php",innerOut,null,document.getElementById("inscription-client"));
}

function moncompte() {
	new net.ContentLoader("needed/monCompte.php",innerOut,null,document.getElementById("inscription-client"));
}

function checkUnique(elem) {
	//alert (elem.getAttribute("change"));
	if (elem.getAttribute("change")=="true") {
	  champs=elem.getAttribute("name").split("-");
	  champs=champs[1];
	  valeur=elem.value;
	  base=elem.getAttribute("base");
	  //alert("needed/checkUnique.php?base="+base+"&champs="+champs+"&valeur="+valeur);
	  new net.ContentLoader("needed/checkUnique.php?base="+base+"&champs="+champs+"&valeur="+valeur,uniqueOut,null,document.getElementById("inscription-client"));

	}
}

function checkFormat(elem,type,mini,maxi) {
  if (elem.value != "") {
	if ( (elem.value.length < mini) || (elem.value.length > maxi)) {
		alert("format incorrect");
		elem.focus();
	}
  }
}

function uniqueOut() {
	//alert (this.req.responseText);
	 if (this.req.responseText == 'NOK') 
	 {
		 alert ( 'Cet identifiant existe déjà');
		 //elem.value="";
		// document.getElementById("identifiant-client").focus();
	 }
}
function checkIdentPass(elem) {

	if (elem.value != document.getElementById("champs-pass").value ) {
		alert("les mots de passe saisis ne sont pas identiques");
		//document.getElementById("champs-pass").value="";
		elem.value="";
		//document.getElementById("champs-pass").focus();
	}
  
}

function validerInscriptionClient(form) {
	//alert (form);
	retour=true;
	champs=document.getElementsByTagName("input");
	for(i=0;i<champs.length;i++){
		//alert (champs[i].getAttribute("name")) ;
		if ( (champs[i].getAttribute("renseigner")=="oui") && (champs[i].value=="")){
			champsnom=champs[i].getAttribute("name");
			champsnom=champsnom.split("-");
			champsnom=champsnom[1];
			alert (champsnom+" non renseigné !");
			retour=false;
		}
	}
	
	if ( retour) {
	   login=document.getElementById("champs-login").value;
	   pass=document.getElementById("champs-pass").value;
	   email=document.getElementById("champs-email").value;
	   new net.ContentLoader("needed/creerClient.php?login="+login+"&pass="+pass+"&email="+email,clientOut,null,document.getElementById("inscription-client"));
	}
}

function modifierClient(form) {
	retour=true;
	champs=document.getElementsByTagName("input");
	for(i=0;i<champs.length;i++){
		//alert (champs[i].getAttribute("name")) ;
		if ( (champs[i].getAttribute("renseigner")=="oui") && (champs[i].value=="")){
			champsnom=champs[i].getAttribute("name");
			champsnom=champsnom.split("-");
			champsnom=champsnom[1];
			alert (champsnom+" non renseigné !");
			retour=false;
		}
	}
	
	if ( retour) {
	   login=document.getElementById("champs-login").firstChild.nodeValue;
	  // alert(document.getElementById("champs-login").firstChild.nodeValue);
	   pass=document.getElementById("champs-pass").value;
	   email=document.getElementById("champs-email").value;
	   new net.ContentLoader("needed/modifierClient.php?login="+login+"&pass="+pass+"&email="+email,clientOut,null,document.getElementById("inscription-client"));
	}
}

function loginOut() {
	alert (this.req.responseText);
	this.element_out.innerHTML=this.req.responseText;
}


function clientOut() {
	
	if (this.req.responseText!="NOK") {
		this.element_out.innerHTML="";
		alert ( this.req.responseText);
		this.element_out.innerHTML="";
	} else {
		alert ("erreur d'enregistrement ! ");
	}
}


function getElementsBySelector(selector){
//Extracted from nifty.js
	var i,selid="",selclass="",tag=selector,f,s=[],objlist=[];
	if(selector.indexOf(" ")>0){  //descendant selector like "tag#id tag"
		s=selector.split(" ");
		var fs=s[0].split("#");
		if(fs.length==1) return(objlist);
		f=document.getElementById(fs[1]);
		if(f) return(f.getElementsByTagName(s[1]));
		return(objlist);
	}
	if(selector.indexOf("#")>0){ //id selector like "tag#id"
		s=selector.split("#");
		tag=s[0];
		selid=s[1];
    }
	if(selid!=""){
		f=document.getElementById(selid);
		if(f) objlist.push(f);
		return(objlist);
    }
	if(selector.indexOf(".")>0){  //class selector like "tag.class"
		s=selector.split(".");
		tag=s[0];
		selclass=s[1];
    }
	var v=document.getElementsByTagName(tag);  // tag selector like "tag"
	if(selclass=="")
		return(v);
	for(i=0;i<v.length;i++){
		if(v[i].className.indexOf(selclass)>=0)
			objlist.push(v[i]);
    }
	return(objlist);
}

function ajusterColonne() {
	  
	  document.getElementById("content").style.height = document.body.clientHeight*0.9+'px';
	  /*document.getElementById("content").style.height = 800+'px';*/
	  return true;
}

function laDate() {
  var dateDuJour = new Date();
  var jour = dateDuJour.getDate();
  var nomjour = jourTexte(dateDuJour.getDay());
  var mois = dateDuJour.getMonth();
  var nommois =moisTexte(mois);
  var annee = dateDuJour.getFullYear();
  
  var today = nomjour+","+jour+" "+nommois+" "+annee;
  return today;
 }
 
function jourTexte(j)   {
  var joursemaine="";
  switch (j) {
    case 1 : joursemaine = "Lundi";
	break;
	case 2 : joursemaine = "Mardi";
	break;
	case 3 : joursemaine = "Mercredi";
	break;
	case 4 : joursemaine = "Jeudi";
	break;
	case 5 : joursemaine = "Vendredi";
	break;
	case 6 : joursemaine = "Samedi";
	break;
	case 0 : joursemaine = "Dimanche";
	break;
  }
  
  return joursemaine;
}

function moisTexte(m)   {
  var mois="";
  switch (m) {
    case 0 : mois = "Janvier";
	break;
    case 1 : mois = "Février";
	break;
	case 2 : mois = "Mars";
	break;
	case 3 : mois = "Avril";
	break;
	case 4 : mois = "Mai";
	break;
	case 5 : mois = "Juin";
	break;
	case 6 : mois = "Juillet";
	break;
	case 7 : mois = "Août";
	break;
	case 8 : mois = "Septembre";
	break;
	case 9 : mois = "Octobre";
	break;
	case 10 : mois = "Novembre";
	break;
	case 11 : mois = "Décembre";
	break;
  }
  
  return mois;
}

function formater_date(une_date) {
	tab_date = une_date.split('-',3)
	tab_date.reverse();
	return tab_date[0]+'-'+tab_date[1]+'-'+tab_date[2];
}

function zoomVignette(img) {
  
  if ( img.getAttribute("width") > img.getAttribute("height") ) {
	  img.style.width="60px";
	  img.style.height="40px";
  }
  else {
	  img.style.width="40px";
	  img.style.height="60px";
  }
}

function unzoomVignette(img) {
	 if ( PHOTO_SELEC != img ) {
	 if ( img.getAttribute("width") > img.getAttribute("height") ) {
	  img.style.width="30px";
	  img.style.height="20px";
     }
     else {
	  img.style.width="20px";
	  img.style.height="30px";
     }
	 }
}

function changeImage(img) {
	chemin=img.src.split("/");
	//chemin=chemin[chemin.lenght-1]+chemin[chemin.lenght-2]+chemin[chemin.lenght-3];
	fichier=chemin[chemin.length-1];
	
	fichier=fichier.split("vig_");
	fichier=fichier[1];
	newsrc=chemin[chemin.length-3]+"/"+chemin[chemin.length-2]+"/"+fichier;
	
	if ( (PHOTO_SELEC != img) && (PHOTO_SELEC != null) ) {
		 if ( PHOTO_SELEC.getAttribute("width") > PHOTO_SELEC.getAttribute("height") ) {
			PHOTO_SELEC.style.width="30px";
	        PHOTO_SELEC.style.height="20px";
		 }
		 else{
		   PHOTO_SELEC.style.width="20px";
	       PHOTO_SELEC.style.height="30px";
		 }
		 PHOTO_SELEC.style.borderColor="#FFFFFF";
		 
		 
	}
	img.style.borderColor="#FF0000";
	PHOTO_SELEC=img;
	
	
	if ( img.getAttribute("width") > img.getAttribute("height") ) {
		document.getElementById("zone_photo").style.width="615px";
		document.getElementById("zone_photo").style.height="415px";
	}
	else {
		document.getElementById("zone_photo").style.width="415px";
		document.getElementById("zone_photo").style.height="615px";
	}
	
	document.getElementById("album_img").src=newsrc;
	
}

function changeImageClient(img) {
	chemin=img.src.split("/");
	//alert("chemin :"+chemin);
	//chemin=chemin[chemin.lenght-1]+chemin[chemin.lenght-2]+chemin[chemin.lenght-3];
	fichier=chemin[chemin.length-1];
	
	//fichier=fichier.split("vig_");
	//fichier=fichier[1];
	//alert("fichier :"+fichier);
	newsrc=chemin[chemin.length-3]+"/"+chemin[chemin.length-2]+"/"+fichier;
	//alert(newsrc);
	if ( (PHOTO_SELEC != img) && (PHOTO_SELEC != null) ) {
		 if ( PHOTO_SELEC.getAttribute("width") > PHOTO_SELEC.getAttribute("height") ) {
			PHOTO_SELEC.style.width="30px";
	        PHOTO_SELEC.style.height="20px";
		 }
		 else{
		   PHOTO_SELEC.style.width="20px";
	       PHOTO_SELEC.style.height="30px";
		 }
		 PHOTO_SELEC.style.borderColor="#FFFFFF";
		 
		 
	}
	img.style.borderColor="#FF0000";
	PHOTO_SELEC=img;
	
	
	if ( img.getAttribute("width") > img.getAttribute("height") ) {
		document.getElementById("zone_photo").style.width="615px";
		document.getElementById("zone_photo").style.height="415px";
	}
	else {
		document.getElementById("zone_photo").style.width="415px";
		document.getElementById("zone_photo").style.height="615px";
	}
	
	document.getElementById("album_img").src=newsrc;
	
}

function validerInscription (elem) {
	
	if (document.form_race.email.value != "") {
	tab_box=getElementsBySelector("input.selectrace");
	
	var races = "";
	for (i=0;i < tab_box.length;i++ ){
		if(tab_box[i].checked) {
			races+=tab_box[i].value+"-";
			tab_box[i].checked=false;
		}
	}
	
	LOADER_HTTP_METHOD="POST";
	corps="liste_race="+races+"&email="+document.form_race.email.value;
	document.form_race.email.value = "";
	check_content=document.getElementById("checkbox").innerHTML;
	new net.ContentLoader ("validerInscription.php",innerOut,null,document.getElementById("checkbox"));
	LOADER_HTTP_METHOD="GET";
	}
	else {
		alert ("Vous devez indiquez une adresse email !");
	}
	
	timer=setTimeout('reAfficheInscription()',5000);
}

function removeMe() {
	if (document.form_race.email.value != "") {
	  LOADER_HTTP_METHOD="POST";
	  corps="email="+document.form_race.email.value;
	  document.form_race.email.value = "";
	  check_content=document.getElementById("checkbox").innerHTML;
	  new net.ContentLoader ("removeMe.php",innerOut,null,document.getElementById("checkbox"));
	  LOADER_HTTP_METHOD="GET";
	  timer=setTimeout('reAfficheInscription()',5000);
	}
	else {
		alert ("Vous devez indiquez une adresse email !");
	}
}

function reAfficheInscription() {
	document.getElementById("checkbox").innerHTML=check_content;
	clearTimeout(timer);
}