

function isEmpty(champ) {
	return (champ == null) || (champ == '');
}

var RE_mail=new RegExp(".+@.+\\..+","i");

function checkEmail(str) {
	if(!RE_mail.test(str)){
		return false;
	}
	return true;
}


var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[1] = "- Le champ 'telephone' est obligatoire\n";
telNumberErrors[2] = "- Le champ 'telephone' ne doit pas contenir de code pays\n";
telNumberErrors[3] = "- Le champ 'telephone' doit contenir 10 chiffre\n";
telNumberErrors[4] = "- Le champ 'telephone' est invalide\n";
telNumberErrors[5] = "- Le champ 'telephone' ou 'portable' est obligatoire\n";
telNumberErrors[6] = "- Le champ 'portable' ne doit pas contenir de code pays\n";
telNumberErrors[7] = "- Le champ 'portable' doit contenir 10 chiffre\n";
telNumberErrors[8] = "- Le champ 'portable' est invalide\n";
function checkTelephone(telephoneNumber) {

  // test telephone vide
  var telnum = telephoneNumber + " ";
  if (telnum.length == 1)  {
     telNumberErrorNo = 1;
     return false;
  }
  telnum.length = telnum.length - 1;
  
  return checkPhone(telephoneNumber, false);

}

function checkCellPhone(errorNumber, isCellPhone) {

  //Variable 0 pour le téléphone et 4 pour le portable.
  var phone = 0;
  if(isCellPhone) phone = 4;
  
  return errorNumber + phone;

}

function checkPhone(telnum, isCellPhone) {
  
  // ont interdit les codes pays
  var exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telnum) == true) {
     telNumberErrorNo = checkCellPhone(2, isCellPhone);
     return false;
  }
  
  // on supprime les espaces
  while (telnum.indexOf(" ")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
  }
  
  // on supprime les tiret
  while (telnum.indexOf("-")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
  }  

  // on supprime les points
  while (telnum.indexOf(".")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf(".")) + telnum.slice (telnum.indexOf(".")+1)
  }  
  
  // on test s'il y a seulement des chiffres
  exp = /^[0-9]{10}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = checkCellPhone(3, isCellPhone);
     return false;
  }
  
  // le premier chiffre est il un 0 ?!
  exp = /^0[0-9]{9,10}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = checkCellPhone(4, isCellPhone);
     return false;
  }

  // 01 || 02 || 03 || 04 || 05 || 06 || 08 || 09
  if (telnum.charAt(1) != '1' && telnum.charAt(1) != '2' && telnum.charAt(1) != '3' && telnum.charAt(1) != '4' && telnum.charAt(1) != '5' && telnum.charAt(1) != '6' && telnum.charAt(1) != '8' && telnum.charAt(1) != '9') {
     telNumberErrorNo = checkCellPhone(4, isCellPhone);
     return false;
  }
  
  // le numero semble bon
  return telnum;


}

function checkCodePostal(codePostal) {

  // on test s'il n'y a que 5 caract?res
  if (codePostal.length != 5)  {
     return false;
  }
  
  // on test s'il y a seulement des chiffres
  exp = /^[0-9]{5}$/
  if (exp.test(codePostal) != true) {
     return false;
  }
  
  return true;
}



function checkForm() {
	var errors = '';
	
	if (document.forms[0].nom != null && isEmpty(document.forms[0].nom.value)) errors += '- Le champ \'nom\' est obligatoire\n';
	if (document.forms[0].prenom != null && isEmpty(document.forms[0].prenom.value)) errors += '- Le champ \'prenom\' est obligatoire\n';
	if (document.forms[0].rue != null && isEmpty(document.forms[0].rue.value)) errors += '- Le champ \'rue\' est obligatoire\n';	
	if (document.forms[0].code_postal != null && isEmpty(document.forms[0].code_postal.value)) errors += '- Le champ \'code postal\' est obligatoire\n';			
	else if (document.forms[0].code_postal != null && !checkCodePostal(document.forms[0].code_postal.value)) errors += '- Le champ \'code postal\' est invalide\n';	
	if (document.forms[0].ville != null && isEmpty(document.forms[0].ville.value)) errors += '- Le champ \'ville\' est obligatoire\n';
	if (document.forms[0].telephone_fixe != null && !checkTelephone(document.forms[0].telephone_fixe.value)) errors += telNumberErrors[telNumberErrorNo];
    
	if (errors == '') {
		return true;
	} else {
		alert('Envoi impossible :\n\n' + errors);
		return false;
	}
}

function checkForm4Formations(form) {
	if (form==null) {
		form = document.forms[0];
	}
	
	var errors = '';
	
	if (form.nom != null && isEmpty(form.nom.value)) errors += '- Le champ \'nom\' est obligatoire\n';
	if (form.prenom != null && isEmpty(form.prenom.value)) errors += '- Le champ \'prenom\' est obligatoire\n';
	if (form.code_postal != null && form.code_postal.value.length > 0 && !checkCodePostal(form.code_postal.value)) errors += '- Le champ \'code postal\' est invalide\n';		
	if (form.email != null && isEmpty(form.email.value)) { errors += '- Le champ \'email\' est obligatoire\n'; }
	else if (!checkEmail(form.email.value)) errors += '- Le champ \'email\' est invalide\n'; 
	if (form.telephone_fixe != null && !checkTelephone(form.telephone_fixe.value)) errors += telNumberErrors[telNumberErrorNo];
    
	if (errors == '') {
		return true;
	} else {
		alert('Envoi impossible :\n\n' + errors);
		return false;
	}
}

function checkThisForm(form) {
	if (form==null) {
		form = document.forms[0];
	}
	
	var errors = '';
	
	if (form.nom != null && isEmpty(form.nom.value)) errors += '- Le champ \'nom\' est obligatoire\n';
	if (form.prenom != null && isEmpty(form.prenom.value)) errors += '- Le champ \'prenom\' est obligatoire\n';
	if (form.code_postal != null && form.code_postal.value.length > 0 && !checkCodePostal(form.code_postal.value)) errors += '- Le champ \'code postal\' est invalide\n';		
	if (form.email != null && isEmpty(form.email.value)) { errors += '- Le champ \'email\' est obligatoire\n'; }
	else if (!checkEmail(form.email.value)) errors += '- Le champ \'email\' est invalide\n'; 
	if((form.telephone_fixe==null || isEmpty(form.telephone_fixe.value)) && (form.telephone_mobile==null || isEmpty(form.telephone_mobile.value))) {
		errors += telNumberErrors[5];
	} else {
		if (form.telephone_fixe!=null && !isEmpty(form.telephone_fixe.value) && !checkPhone(form.telephone_fixe.value, false)) errors += telNumberErrors[telNumberErrorNo];
		if (form.telephone_mobile!=null && !isEmpty(form.telephone_mobile.value) && !checkPhone(form.telephone_mobile.value, true)) errors += telNumberErrors[telNumberErrorNo];
	}
	
	if (errors == '') {
		return true;
	} else {
		alert('Envoi impossible :\n\n' + errors);
		return false;
	}
}