//********** LIBRAIRIE DE VERIFICATION DE FORMULAIRE **********//
//**********    Copyright Lapoz pour e-Lixir 2005    **********//

function verif_remplissage(champs) {
   var i = 0, j;
   for(j=0; j<champs.length; j++) {
      if(!champs[j].value && !i) {
	     alert("Vous n'avez pas rempli tous les champs obligatoires !");
		  i = 1;
		  champs[j].focus();
		  return false;
	  }
   }
	return true;
}

function verif_liste(champ, descriptif) {
	if(champ.options[champ.selectedIndex].value == "") {
		alert("Vous devez indiquer "+descriptif+" !");
		return false;
	}
	return true;
}

function verif_cases(champs, descriptif) {
	var i = 0, j; 
	for(j=0; j<champs.length; j++)
		if(!champs[j].checked)
			i++;
	if(i==champs.length){
		alert("Vous devez cocher au moins "+descriptif+"...");
		return false;
	}
	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)) {
		alert("Erreur de syntaxe dans l'adresse mail !");
		return false;
	}
	return true;
}

function verif_tel(champ, descriptif) {
   var syntaxe_tel = new RegExp("^[0-9.()+ ]*$", "");
   if(!syntaxe_tel.test(champ.value)) {
		alert("Erreur de syntaxe dans le numéro de "+descriptif+" !");
		return false;
	}
	return true;
}

function verif_cp(champ) {
	var syntaxe_cp = new RegExp("^[a-zA-Z0-9]$", "");
	if(!syntaxe_cp.test(champ.value)) {
		alert("Erreur de syntaxe dans le code postal !");
		return false;
	}
	return true;
}

function verif_date(champ, type) {
	switch(type) {
		case "jj-mm-aaaa" :	var syntaxe_date = new RegExp("^[0-9]{2}-[0-9]{2}-[0-9]{4}$", ""); break;
		case "mm/aaaa" :		var syntaxe_date = new RegExp("^[0-9]{2}/[0-9]{4}$", ""); break;
	}
	if(!syntaxe_date.test(champ.value)) {
		alert("Erreur de syntaxe dans la date !");
		return false;
	}
	return true;
}

function verif_float(champ, descriptif) {
	var syntaxe_float = new RegExp("^[0-9.]*$", "");
	if(!syntaxe_float.test(champ.value)) {
		alert(descriptif+" doit être un nombre !");
		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_file(champ, type, ext) {
	var chaine = "";
	for(i=0; i<ext.length; i++)
		chaine += ext[i]+"|";
	var format_file = new RegExp("("+chaine.substr(0, (chaine.length-1))+")$", "");
	if(!format_file.test(champ.value)) {
		var alerte = "Vous devez indiquer pour "+type+" un fichier dans l'un des formats suivants :\n";
		for(i=0; i<ext.length; i++)
			alerte += ext[i].toUpperCase()+", ";
		alert(alerte.substr(0, (alerte.length-2)));
		return false;
	}
	return true;
}

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);
}