//********** LIBRAIRIE DE VERIFICATION DE FORMULAIRE **********//
var color  = '#FFAEAE';
var color_ok  = '#ffffff';

function verif_remplissage(champs) {
   var i = 0, j;
   for(j=0; j<champs.length; j++) {
      if(!champs[j].value && !i) {
		champs[j].style.background = color;
		var msg = verif_champs_obliga;
		alert(msg);
		  i = 0;
		  return false;
	  } else { champs[j].style.background = color_ok; }
   }
	return true;
}

function verif_remplissage2(champs, descr,lang) {
   var i = 0, j;
   for(j=0; j<champs.length; j++) {
      if(!champs[j].value && !i) {
		champs[j].style.background = color;
		var msg = verif_le_champs_obliga+descr;
		
		//if(lang!="fr"){
		//	var msg = "You did not complete the "+descr+" field !";
		//}
		alert(msg);
		  i = 0;
		  return false;
	  } else { champs[j].style.background = color_ok; }
   }
	return true;
}

function verif_liste(champ, descriptif) {
	if(champ.options[champ.selectedIndex].value == "") {
		alert(verif_champs_descriptif+descriptif);
		return false;
	}
	return true;
}

function verif_cases(champs, descriptif) {
	
	var i = 0; 
	var j = 0; 
	
	for(j=0; j<champs.length; j++)
		
		if(!champs[j].checked)
			i++;
	if(i==champs.length){
		alert(verif_champs_select+descriptif+"...");
		return false;
	}
	return true;
}

function verif_mail0(champ,lang) {
	var syntaxe_mail = new RegExp("^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\._]*@[\-a-zA-Z0-9\._]+[\.]{1}[a-zA-Z]{2,4}$", "");
	if(!syntaxe_mail.test(champ.value)) {
		champ.style.background = color;
		var msg = verif_champs_email;
		
		//if(lang!="fr"){
		//	var msg = "Syntax error in mail address";
		//}
		alert(msg);
		
		
		return false;
	}else { champ.style.background = color_ok; }
	return true;
}

function verif_mail(champ) {
	var syntaxe_mail = new RegExp("^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\._]*@[\-a-zA-Z0-9\._]+[\.]{1}[a-zA-Z]{2,4}$", "");
	if(!syntaxe_mail.test(champ.value)) {
		champ.style.background = color;
		var msg = verif_champs_email;
		alert(msg);
		
		
		return false;
	}else { champ.style.background = color_ok; }
	return true;
}

function verif_ze_mail(champ,lang) {
	var syntaxe_mail = new RegExp("^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\._]*@[\-a-zA-Z0-9\._]+[\.]{1}[a-zA-Z]{2,4}$", "");
	if(champ.value!=""){
		if(!syntaxe_mail.test(champ.value)) {
			champ.style.background = color;
			if((lang=="fr")||(lang=="")){
			var msg = verif_champs_email;
			}//else
//			if(lang!="fr"){
//				var msg = "Syntax error in mail address";
//			}
			alert(msg);
			
			return false;
		}
	}
	return true;
}

function verif_tel(champ, descriptif,lang) {
   var pilepoil = 0;
   // var syntaxe_tel = new RegExp("^[0-9.()+ ]*$", ""); // Syntaxe Internationale, pas de vérif poussée
	//var syntaxe1 = new RegExp("^0[1-68]{1}[.]{1}[0-9]{2}[.]{1}[0-9]{2}[.]{1}[0-9]{2}[.]{1}[0-9]{2}$", "");
  // if(!syntaxe1.test(champ.value)) {
		var syntaxe2 = new RegExp("^0[1-68]{1}[0-9]{8}$", "");
		if(!syntaxe2.test(champ.value)) {
			champ.style.background = color;
			if((lang=="fr")||(lang=="")){
			var msg = verif_champs_tel1+descriptif+verif_champs_tel2;
			}//else
			//if(lang!="fr"){
			//	var msg = "Syntax error in the "+descriptif;
			//}
			alert(msg);
			
			
			return false;
		}
	//}
	else pilepoil = 1;
	if(!pilepoil) {
		champ.value =	champ.value.substring(0, 2)+"."+champ.value.substring(2, 4)+"."+champ.value.substring(4, 6)+"."+
							champ.value.substring(6, 8)+"."+champ.value.substring(8, 10);
	}
	return true;
}

function verif_cp(champ) {
	var syntaxe_cp = new RegExp("^[0-9]{5}$", "");
	if(!syntaxe_cp.test(champ.value)) {
		
		champ.style.background = color;
		alert(verif_champs_code_postal);
		
		return false;
	}
	return true;
}

function verif_login_pwd(champ) {
	var syntaxe_login_pwd = new RegExp("^[a-zA-Z0-9]{4,}$", "");
	if(!syntaxe_login_pwd.test(champ.value)) {
		alert(verif_champs_login);
		return false;
	}
	return true;
}

function verif_pwd(champ) {
	var syntaxe_login_pwd = new RegExp("^[a-zA-Z0-9]{6,}$", "");
	if(!syntaxe_login_pwd.test(champ.value)) {
		champ.style.background = color;
		alert(verif_champs_password);
		
		return false;
	}
	return true;
}

function verif_2_pwd(champ1, champ2) {
	
	if((champ1.value)!=(champ2.value)) {
		alert(verif_champs_password_ident);
		return false;
	}
	return true;
}

function verif_dn(champ) {
	var syntaxe1 = new RegExp("^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$", "");
	if(!syntaxe1.test(champ.value)) {
			champ.style.background = color;
			alert(verif_champs_date_naissance);
			
			return false;
	}
	return true;
}

function verif_jour(champ) {
	if(isNaN(champ.value) || champ.value > 31 || champ.value < 1) {
		alert(verif_champs_jour);
		return false;
	}
	return true;
}

function verif_mois(champ) {
	if(isNaN(champ.value) || champ.value > 12 || champ.value < 1) {
		alert(verif_champs_jour);
		return false;
	}
	return true;
}

function verif_annee(champ) {
	if(isNaN(champ.value) || champ.value < 2005) {
		alert(verif_champs_jour);
		return false;
	}
	return true;
}

function verif_heure(champ) {
	if(isNaN(champ.value) || champ.value > 23 || champ.value < 0) {
		alert(verif_champs_heure);
		return false;
	}
	return true;
}

function verif_minute(champ) {
	if(isNaN(champ.value) || champ.value > 59 || champ.value < 0) {
		alert(verif_champs_minute);
		return false;
	}
	return true;
}

function verif_float(champ, descriptif, lang) {
	var syntaxe_float = new RegExp("^[0-9.]*$", "");
	if(!syntaxe_float.test(champ.value)) {
		if((lang=="fr")||(lang=="")){
			var msg = descriptif+verif_champs_float;
			//}else
			//if(lang!="fr"){
			//	var msg = descriptif+" must be a number !"
			}
		alert(msg);
		return false;
   }
	return true;
}

function verif_qte(champ, descriptif) {
	var syntaxe_float = new RegExp("^[0-9.]*$", "");
	if(!syntaxe_float.test(champ.value)||(champ.value == "")||(champ.value == 0)) {
		alert(descriptif);
		return false;
   }
	return true;
}

function verif_url(champ) {
	var syntaxe_url = new RegExp("^https?://*", "");
	if(!syntaxe_url.test(champ.value))
		champ.value = "http://" + champ.value;
}

function verif_tof(champ) {
	if(champ) {
		var format_tof = new RegExp("(jpg|jpeg)$", "");
		if(!format_tof.test(champ.value)) {
			alert(verif_champs_photo);
			return false;
		}
	}
}

function verif_image(champ, description) {
	if(champ) {
		var format_tof = new RegExp("^(.*)\.([(jpg)|(jpeg)|(gif)|(png)])$","i");
		if(!format_tof.test(champ.value)) {
			alert(description+verif_champs_image);
			return false;
		}
	}
}


	
	
function majuscules(champ) {
	champ.value = champ.value.toUpperCase();
}

function minuscules(champ) {
	champ.value = champ.value.toLowerCase();
}

function majFirst(champ) {
	var index;
	var tmpStr;
	var tmpChar;
	var preString;
	var postString;
	var strlen;
	
	tmpStr = champ.value.toLowerCase();
	strLen = tmpStr.length;
	
	if (strLen > 0)  {
		for (index = 0; index < strLen; index++)  {
			if (index == 0)  {
				tmpChar = tmpStr.substring(0,1).toUpperCase();
				postString = tmpStr.substring(1,strLen);
				tmpStr = tmpChar + postString;
			}
			else {
				tmpChar = tmpStr.substring(index, index+1);
				if ((tmpChar == " " || tmpChar == "-") && 
					  (tmpStr.substring(index+1, index+3) != "le" && tmpStr.substring(index+1, index+3) != "la" && 
						tmpStr.substring(index+1, index+3) != "du" && tmpStr.substring(index+1, index+3) != "de" && 
						tmpStr.substring(index+1, index+4) != "des" && tmpStr.substring(index+1, index+4) != "rue" && index < (strLen-1)))  {
					tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
					preString = tmpStr.substring(0, index+1);
					postString = tmpStr.substring(index+2,strLen);
					tmpStr = preString + tmpChar + postString;
				}
			}
		}
	}
	champ.value = tmpStr;
}

function majFirstOnly(champ) {
	champ.value = champ.value.substring(0, 1).toUpperCase() + champ.value.substring(1);
}

var regExpTVAIntracommunautaire=/^[A-Z]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}$/g;		// Accepte un numero de TVA Intra-communautaire. Ex : FR 02 254 254 254
var regExpNumeroSiret=/^[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{5}$/g;					// Accepte un numero SIRET. Ex : 254 254 254 12345
var regExpCodeApe=/^[0-9]{2}[ \.\-]?[0-9]{2} ?[a-zA-Z]$/g;								// Accepte un code APE. Ex : 25.45Z

//Les deux fonction suivantes servent à identifier si une chaine de caractère est compatible ou non avec une expression régulière passée en paramètre
function matchRegularExpression(valeur, regularExpression)
{
	var resultat = valeur.match(regularExpression);
	if(resultat!=null && resultat.length==1) return true;
	else return false;
}

function doesntMatchRegularExpression(valeur, regularExpression)
{
	if(matchRegularExpression(valeur, regularExpression)) return false;
	else return true;
}

function isNotTVAIntracommunautaire(valeur)
{
	//return doesntMatchRegularExpression(valeur, regExpTVAIntracommunautaire);
	return false;
}

function isNotNumeroSiren(valeur)
{
	return doesntMatchRegularExpression(valeur, regExpNumeroSiren);
}

function isNotNumeroSiret(valeur)
{
	return doesntMatchRegularExpression(valeur, regExpNumeroSiret);
}

function isNotCodeApe(valeur)
{
	return doesntMatchRegularExpression(valeur, regExpCodeApe);
}



