// Concerne la validation de champs saisis dans un formulaire

function test_champ(champ,type) {
	return test_champ(champ,type,0) 
}

var styleBgDepart = '#ffffff';

// Teste:
// - 1 : si le champ est vide
// - 2 : si le champ contient un arobase (verif de l'adresse email)
// - 3 : si le champ commence par "06" (verif numéro de portable)
// - 4 : si le champ est un numérique
// - 5 : si le champ comporte la longueur voulue
// - 6 : si le champ contient un nombre de lettres minimum
// - 7 : si le champ date est vide (ici le parametre longueur est la valeur à tester : jour, mois ou annee)
// - 8 : si les 2 champs ont la même valeur (ici le parametre longueur est la deuxième valeur)
// - 9 : si le champ est checké (concerne les checkbox uniquement)
// - 10 : si le champ est un identifiant numérique correct
// - 11 : si le champ est une date au format jj/mm/aaaa
// - 12 : si le champ est une heure (format HH:MM)
// - 13 : si le champ est un alphanumérique
// - 14 : si le champ est un booleen (valeur true ou false)
// - 15 : si le champ est un booleen (valeur oui ou non)
function test_champ(champ,type,longueur) {	
  var ok = false;
  switch (type){
    case 1 : ok = test_vide(champ);
    		 break;
    case 2 : ok = test_pasemail(champ);
    		 break;
    case 3 : ok = test_pasmobile(champ);
    		 break;
    case 4 : ok = test_pasnumerique(champ);
    		 break;
    case 5 : ok = test_souslongueurmin(champ,longueur);
     	 	 break;
    case 6 : ok = test_depassevaleurmax(champ,longueur);
     	 	 break;
    case 7 : ok = test_date_vide(champ,longueur);
     	 	 break;
    case 8 : ok = test_pasegal(champ,longueur);
     	 	 break;
    case 9 : ok = test_pascheckbox(champ);	 
     	 	 break;
    case 10 : ok = test_pasidentifiant(champ);
     	 	  break;
    case 11 : ok = test_pasdate(champ);
     	 	  break;
    case 12 : ok = test_pasheure(champ);
     	 	  break;     	 	  
    case 13 : ok = test_pasalphanum(champ);
     	 	  break;
    case 14 : ok = test_pasbooleen(champ);
     	 	  break;     	 	  
    case 15 : ok = test_pasOuiNon(champ);
     	 	  break;     	 	       	 	  
    default : break;    
  }
//  if (champ.style) {
//    if (champ.style.backgroundColor) {
      if (champ.style.backgroundColor!="#ff8400") {
        styleBgDepart = champ.style.backgroundColor;
      }
//    }
//  }
  if (ok==true) {
//    if (champ.style) {
//      if (champ.style.backgroundColor) {
        champ.style.backgroundColor="#ff8400";
//      }
//    }
    champ.focus();
    return true;
  }
  else {
//    if (champ.style) {
//      if (champ.style.backgroundColor) {
        champ.style.backgroundColor=styleBgDepart;
//      }
//    }
    return false;
  }
}

function test_vide(champ) {  
  if (champ.length == null){
    if (champ.value.length == 0) return true;
    if (champ.value.length > 0) {
      for (var i = 0; i < champ.value.length; i++) {
        if (champ.value.substring(i, i + 1) != ' ') return false;
      }
    }
    return true;
  } else {   
    test_vide_radio(champ);
  }  
}

function test_pascheckbox(champ) {   
  return (!champ.checked);
}

function test_vide_radio(champ) {
  var radio_choice = false;
  for (counter=0; counter<champ.length; counter++){
    if (champ[counter].checked) radio_choice = true;       
  }
  if (!radio_choice) return true;
  return false;
}

// fonction particuliere : teste si la date est remplie
// - 1 : jour
// - 2 : mois
// - 3 : annee
function test_date_vide(champ, partiedate) {  
  if (champ.value.length == 0) return true;
  switch (partiedate){
    case 1 : return (champ.value == 'jj');             
    		 break;	 
    case 2 : return (champ.value == 'mm');
    		 break;	 
    case 3 : return (champ.value == 'aaaa') ;
    		 break;	 
    default : break;
  }
}

// fonction : teste si le champ est une date, de la forme jj/mm/aaaa
function test_pasdate(champ) {
	var jour;
	var mois;
	var annee;
	var bOk=false;
	bOk = (champ.value.substring(2,3)=="/") && (champ.value.substring(5,6)=="/");
	if (bOk) {
		jour = champ.value.substring(0,2);
		mois = champ.value.substring(3,5);
		annee = champ.value.substring(6,10);
		bOk = (!isNaN(jour)) && (!isNaN(mois)) && (!isNaN(annee));
		if (bOk) {
			return test_date_val(jour,mois,annee);
		}
		else
			return true;
	}
	else
		return true;
}

function test_date(champJour,champMois,champAnnee) {
  return test_date_val(champJour.value, champMois.value, champAnnee.value);
}
function test_date_val(jour,mois,annee) {
  if (annee<=1900) return true;
  var d2=new Date(annee,mois-1,jour);
  j2=d2.getDate();
  m2=d2.getMonth()+1;
  a2=d2.getYear();  
  if (a2<=100) {a2=1900+a2}
  if ( (jour!=j2)||(mois!=m2)||(annee!=a2)) return true;
  return false;
}

// Teste de la majorité
function test_majorite(jour,mois,annee) {	
	var d=new Date(annee.value,mois.value,jour.value);
	var m=new Date();
	var diffannee=0;
	var diffmois=0;
	var diffjour=0;	
	if (d.getTime()>m.getTime()) return true;	
	diffannee = (m.getFullYear()-d.getFullYear());
	if (diffannee>18) return false;
	if (diffannee==18){
	  diffmois = (m.getMonth()+1-d.getMonth()); 
	  if (diffmois>0) return false;
	  if (diffmois==0){
	    diffjour = (m.getDate()-d.getDate()); 
    	if (diffjour>=0) return false;
      }
    }  	
	return true;
}

function test_pasemail(champ) {
  var arobase = false;
  if (champ.value.length > 0) {
    for (var i = 0; i < champ.value.length; i++) {
      if (champ.value.substring(i, i + 1) == '@'){
        arobase = true; 	
      }
      if (arobase==true){
		if (champ.value.substring(i, i + 1) == '.')	return false;
	  }	
    }
  }
  return true;
}

function test_pasmobile(champ) {
  if (champ.value.length == 0) return false;
  if (champ.value.length > 0) {
  	if (champ.value.substring(0, 2) != '06') return true;
  }
  return false;
}

function test_pasnumerique(champ) {
  if (champ.value) {//champ
    len = champ.value.length;
    val = champ.value;
  }
  else {//string
    len = champ.length;
    val = champ;
  }
  if (len == 0) return false;
  if (len > 0) {
	  if (isNaN(val)) return true;
  }		  
  return false;
}        
 
function test_souslongueurmin(champ,longueur) {
  if (champ.value.length > 0) {
  	if (champ.value.length < longueur) return true;
  }	
  return false;
}

function test_depassevaleurmax(champ,max) {
  if (champ.value.length > 0) {
  	if (champ.value > max) return true;
  }	
  return false;
}

function test_pasegal(champ,longueur) {
  if (champ.value==longueur.value) return false;   
  return true;
}

function test_pasidentifiant(champ) {
	// Teste si identifiant ou email
  if (test_pasemail(champ)) {
      if (champ.value.substring(0,1) != 'M') return true;
      if (test_souslongueurmin(champ,9)) return true; 	
  }	
  return false;
}

function test_pasheure(champ) {
  // Teste si champ est de la forme HH:MM
  if (champ.value.length == 0) return false;
  if (champ.value.length != 5) return true;
  if (champ.value.length > 0) {    
  	if (champ.value.substring(2,3) != ':') return true;
  	var heure = champ.value.substring(0,2);
  	var minute = champ.value.substring(3,5);
  	if (test_pasnumerique(heure)) return true;
  	if (test_pasnumerique(minute)) return true;
  	if (heure>=24) return true;
  	if (minute>=60) return true;
  }
  return false;
}

function test_pasalphanum(champ) {
  var reg = new RegExp("^[a-zA-Z0-9][a-zA-Z0-9\-]*$","g");
  return (!(champ.value=="" || reg.test(champ.value)));
}

function test_pasbooleen(champ) {
  return (!(champ.value=="true" || champ.value=="True" || champ.value=="false" || champ.value=="False"));
}

function test_pasOuiNon(champ) {   
  return (!((champ.value=="oui") || (champ.value=="Oui") || (champ.value=="non") || (champ.value=="Non")));
}

function trimChampLeft(champ) {
	while(champ.value.length>0 && (
			(champ.value.charAt(0)==' ') || (champ.value.charAt(0)=='\r') || (champ.value.charAt(0)=='\n') || (champ.value.charAt(0)=='\t')) ) {
		champ.value=champ.value.substring(1);
	}
	return champ;	
}
function trimChampRight(champ) {
  while(champ.value.length>0 && (
			(champ.value.charAt(champ.value.length-1)==' ') || (champ.value.charAt(champ.value.length-1)=='\r') || (champ.value.charAt(champ.value.length-1)=='\n') || (champ.value.charAt(champ.value.length-1)=='\t')) ) {
		champ.value=champ.value.substring(0,champ.value.length-1);
	}
	return champ;
}
function trimChamp(champ) {
	return trimChampLeft(trimChampRight(champ));
}
