var txt1='Le nom est obligatoire';
var txt2='Le prénom est obligatoire';
var txt3='Le mot de passe est invalide';
var txt4='5 caractères minimum';
var txt5='Les mots de passe ne correspondent pas';
var txt6='L\'adresse est obligatoire';
var txt7='La ville est obligatoire';
var txt8='Le code postal est obligatoire';
var txt9='Le code postal est invalide';
var txt10='L\'identifiant est invalide';
var txt11='2 caractères minimum';
var txt12=' est déjà utilisé';
var txt13='L\'adresse mail est invalide';
var txt14=' est disponible!';
var txt15=' n\'est pas disponible';
var txt16='Le téléphone est obligatoire';
var txt17='La date de naissance est obligatoire';
var txt18='La date de naissance est invalide';
var txt19='Le téléphone est obligatoire';
var txt20='Le téléphone est invalide';

var pagereq = 'verifIdMailInscrire.php?';
var xmlhttp = null;
var formulaire;
var nom;
var nomEtat;
var prenom;
var prenomEtat;
var identifiant;
var identifiantEtat;
var passwd;
var passwdEtat;
var passwdConfirm;
var passwdConfirmEtat;
var mail;
var mailEtat;
var date_naissance;
var date_naissanceEtat;
var adresse;
var adresseEtat;
var cp;
var cpEtat;
var ville;
var villeEtat;
var tel;
var telEtat;
var submitBouton;
var identifiantFocus = false;

var cacheIdentifiant = {};
var cacheMail = {};

String.prototype.trim = new Function("return this.replace(/^\\s+|\\s+$/g,'')");
String.prototype.isEmpty = new Function("var x = this.trim(); if (x.length == 0) { return true; } else { return false; }");

function checkIdentifiant(identifiant) {
  var filter = /^([a-zA-Z0-9_\.])+$/;
  if (filter.test(identifiant)) return true;
  else return false;
}

function checkAlnumName(nom) {
  var filter = /^([a-zA-Z0-9_\.\-])+\s?$/;
  if (filter.test(nom)) return true;
  else return false;
}

function checkNumName(num) {
  var filter = /[0-9]+\s*$/;
  if (filter.test(num)) return true;
  else return false;
}

function checkMail(mail) {
  var filter = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (filter.test(mail)) return true;
  else return false;
}

function checkDate(dat)
{
	var filter = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
  if (!filter.test(dat)) return false;
  else {
	  var tmpTab=dat.split("/");
		var year=tmpTab[2];
		var month=tmpTab[1];
		var day=tmpTab[0];
		// month argument must be in the range 1 - 12
	  month = month - 1;  // javascript month range : 0- 11
	  var tempDate = new Date(year,month,day);
	  if ( (year == tempDate.getFullYear()) && (month == tempDate.getMonth()) && (day == tempDate.getDate()) )
	      return true;
	  else
	     return false;
  }
}

function creerXmlhttp() {
  var xhttp = null;
  try {
    xhttp = new ActiveXObject("Msxml2.XMLHTTP")
  } catch(e) {
    try {
      xhttp = new ActiveXObject("Microsoft.XMLHTTP")
    } catch (x) {
      xhttp = null
    }
  }

  if (!xhttp && typeof XMLHttpRequest != "undefined") {
    xhttp = new XMLHttpRequest()
  }

  return xhttp;
}
																					
function execreq(champs, valeur) {
  if (xmlhttp && xmlhttp.readyState != 0) {
    xmlhttp.abort()
  }

  xmlhttp = creerXmlhttp();
  if (xmlhttp) {
    lurl = pagereq + "champs=" + champs + "&valeur=" + valeur;
    xmlhttp.open("GET", lurl, true);
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4) {
      	var res=xmlhttp.responseText;
      	var etat;      	var tabl;      	var txtEr;      	var txtSu;
      	
      	if(champs=="login") {etat=identifiantEtat; tabl=cacheIdentifiant; txtEr= valeur + txt15; txtSu= valeur + txt14;}
      	else if(champs=="mail") {etat=mailEtat; tabl=cacheMail; txtEr= valeur + txt12; txtSu="";}
      	
      	if(!res)
      	{
      		setSuccess(etat, txtSu);
      		tabl[valeur]=true;
      		return true;
      	}
      	else
      	{
      		setError(etat, txtEr);
      		tabl[valeur]=false;
      		return false;
      	}
      }
    }
    xmlhttp.send(null);
  }
  return false;
}



function de(n) { return document.getElementById(n); }

function cacheDom() {
  formulaire = de('formulaire');
  nom = de('nom');
  nomEtat = de('nomEtat');
  prenom = de('prenom');
  prenomEtat = de('prenomEtat');
  identifiant = de('identifiant');
  identifiantEtat = de('identifiantEtat');
  passwd = de('passwd');
  passwdEtat = de('passwdEtat');
  passwdConfirm = de('passwdConfirm');
  passwdConfirmEtat = de('passwdConfirmEtat');
  mail = de('mail');
  mailEtat = de('mailEtat');
  date_naissance = de('date_naissance');
  date_naissanceEtat = de('date_naissanceEtat');
  adresse = de('adresse');
  adresseEtat = de('adresseEtat');
  cp = de('cp');
  cpEtat = de('cpEtat');
  ville = de('ville');
  villeEtat = de('villeEtat');
  tel = de('tel');
  telEtat = de('telEtat');
  submitBouton = de('signupsubmit');
}

function cacheImages() {
  var errorImage = new Image();
  var successImage = new Image();
  errorImage.src = "images/portail/cross.png";
  successImage.src = "images/portail/tick_small.png";
}

function setSuccess(o, m) {
  o.innerHTML = '<span class="success_msg">' + (m == null ? '&nbsp;' : m) + '</span>';
  o.className = 'success';
}

function setError(o, m) {
  o.innerHTML = '<span class="error_msg">' + (m == null ? '&nbsp;' : m) + '</span>';
  o.className = 'error';
}

function doActions() {
	return (nom.onkeyup()
				  && prenom.onkeyup()
				  && identifiant.onkeyup()
				  && passwd.onkeyup()
				  && passwdConfirm.onkeyup()
				  && mail.onkeyup()
				  && date_naissance.onkeyup()
				  && adresse.onkeyup()
				  && cp.onkeyup()
				  && ville.onkeyup()
				  && tel.onkeyup());
}

function init() {
  if (de('nom') === null)
  {
    return false;
  }
  cacheDom();
  cacheImages();
  
  nom.onkeyup = checkNom;
  prenom.onkeyup = checkPrenom;
  identifiant.onkeyup = checkUser;
  identifiant.onfocus = focusIdentifiant;
  passwd.onkeyup = checkPasswd;
  passwdConfirm.onkeyup = checkPasswdConfirm;
  mail.onkeyup = checkMail0;
  date_naissance.onkeyup = checkDate_naissance;
  adresse.onkeyup = checkAdresse;
	cp.onkeyup = checkCp;
	ville.onkeyup = checkVille;
	tel.onkeyup = checkTel;
	formulaire.onsubmit = checkForm;
}

function checkForm() {
  if (doActions()) return true;
  else return false;
}

function checkUser() {
  if (!checkIdentifiant(identifiant.value)) {
   setError(identifiantEtat, txt10);
   return false;
  } else if (identifiant.value.trim().length < 2) {
   setError(identifiantEtat, txt11);
   return false;
  }
  
	if (cacheIdentifiant[identifiant.value]==null) {
    return execreq("login", identifiant.value);
  } else {
    if (cacheIdentifiant[identifiant.value]) {
      setSuccess(identifiantEtat, identifiant.value + txt14);
      return true;
    } else {
      setError(identifiantEtat, identifiant.value + txt15);
      return false;
    }
  }
}

function checkNom() {
  if (!nom.value.isEmpty()) {
    setSuccess(nomEtat);
    return true;
  } else {
	  setError(nomEtat, txt1);
    return false;
  }
}

function checkPrenom() {
  if (!prenom.value.isEmpty()) {
    setSuccess(prenomEtat);
    return true;
  } else {
    setError(prenomEtat, txt2);
    return false;
  }
}

function focusIdentifiant() {
  if (!identifiantFocus) {
  	identifiantFocus = true;
    var fullName = nom.value.toLowerCase() + '.' + prenom.value.toLowerCase();
    if (fullName.length>1 && checkAlnumName(fullName))
    {
        identifiant.value = fullName;
        identifiant.select();
        checkUser();
    }
  }
  return false;
}

function checkPasswd() {
  if (passwd.value.trim().length == 0) {
    setError(passwdEtat, txt3);
    return false;
  } else if (passwd.value.trim().length < 5) {
    setError(passwdEtat, txt4);
    return false;
  } else {
    setSuccess(passwdEtat);
    return true;
  }
}

function checkPasswdConfirm() {
  checkPasswd();
  if (passwdConfirm.value.trim().length==0) {
    setError(passwdConfirmEtat, txt3);
    return false;
  } else if (passwdConfirm.value.trim().length < 5) {
    setError(passwdConfirmEtat, txt4);
    return false;
  }

  if (passwd.value == passwdConfirm.value) {
    setSuccess(passwdConfirmEtat);
    setSuccess(passwdEtat);
    return true;
  } else {
    setError(passwdConfirmEtat, txt5);
    return false;
  }
}

function checkMail0() {
  if (!checkMail(mail.value)) {
   setError(mailEtat, txt13);
   return false;
  }
  
	if (cacheMail[mail.value]==null) {
    return execreq("mail", mail.value);
  } else {
    if (cacheMail[mail.value]) {
      setSuccess(mailEtat);
      return true;
    } else {
      setError(mailEtat, mail.value + txt12);
      return false;
    }
  }
}

function checkAdresse() {
  if (!adresse.value.isEmpty()) {
    setSuccess(adresseEtat);
    return true;
  } else {
	  setError(adresseEtat, txt6);
    return false;
  }
}

function checkCp() {
  if (checkNumName(cp.value)) {
    setSuccess(cpEtat);
    return true;
  } else {
	  if (cp.value.isEmpty()) setError(cpEtat, txt8);
	  else setError(cpEtat, txt9);
    return false;
  }
}

function checkVille() {
  if (!ville.value.isEmpty()) {
    setSuccess(villeEtat);
    return true;
  } else {
	  setError(villeEtat, txt7);
    return false;
  }
}

function checkDate_naissance() {
	if (date_naissance.value.isEmpty()) {
		setError(date_naissanceEtat, txt17); return false;
	}
  else
	{
	  if (checkDate(date_naissance.value)) {
	  	setSuccess(date_naissanceEtat); return true;
	  }
	  else {
	  	setError(date_naissanceEtat, txt18); return false;
		}
	}
}

function checkTel() {
  if (checkNumName(tel.value)) {
    setSuccess(telEtat);
    return true;
  } else {
	  if (tel.value.isEmpty()) setError(telEtat, txt19);
	  else setError(telEtat, txt20);
    return false;
  }
}
