<!--
// ===================================================
//
// Module de gestion de la validation des formulaires
//
// ===================================================

function goToPage(PageToGo) {
	window.location.href = PageToGo ; 
}

String.prototype.trim = function() {
	return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

// ==============================================
// Teste si la liste d�roulante est vide
// ==============================================
function IsListEmpty(Liste) {
	var Chaine;
	Chaine = Liste.options[Liste.selectedIndex].value;
	Chaine = Chaine.toUpperCase()
	if ((Chaine == null) ||
		(Chaine == "") ||
		(Chaine == "NONE"))
	{     
	  return(true);
	}
	return(false);
}  

// ==============================================
// Vérifie si une liste est vide et 
// affiche les messages d'alerte
// ==============================================
function CheckList(Liste, Libelle) {
	if (IsListEmpty(Liste)) {
		alert('Vous devez saisir ' + Libelle + '.');
		Liste.focus()
		return(false);
	}
	return(true);
}

// ==============================================
// Teste si une chaine contient un float
// ==============================================
function IsFloat(Chaine) {
	var flt = 0;
	Chaine = Chaine.replace("," , ".");
	// Verifie si c'est un float
	flt = eval('Chaine')*1;
	if (isNaN(parseFloat(flt))) {
		return(false);
	}   
	return(true);
}

// ==============================================
// Teste si une chaine contient un integer
// ==============================================
function IsInteger(Chaine) {
	var intg = 0;
	// Verifie si c'est un integer
	intg = eval('Chaine')*1;
	if (isNaN(parseInt(intg))) {
		return(false);
	}   
	 // Verifie qu'il n'y a pas de séparateur décimal
	if (Chaine.indexOf(".") >= 0 || Chaine.indexOf(",") >= 0) {
		return(false);
	}
	return(true);
}

// ==============================================
// Teste si une chaine contient un nombre vide ou zéro
// ==============================================
function IsNumericEmpty(Chaine) {
	var Nbr = 0;
	Nbr = eval('Chaine')*1;
	if	((Nbr == 0) || (isNaN(Nbr))) {
		return(true);
	}
	return(false);
}

// ==============================================
// V�rifie si un input pour du float est valide et 
// affiche les messages d'alerte
// ==============================================
function CheckIsFloat(oInput, Libelle) {
	var Chaine = oInput.value
	if (! IsFloat(Chaine)) {
		alert(Libelle + ' doit être une valeur numérique.');
		oInput.focus();
		return(false);
	}   
	return(true);
}

// ==============================================
// V�rifie si un input pour du float est vide et valide et 
// affiche les messages d'alerte
// ==============================================
function CheckFloatInput(Chaine, Libelle) {
	if (! IsFloat(Chaine)) {
		alert(Libelle + ' doit être une valeur numérique.');
		return(false);
	}   
	if (IsNumericEmpty(Chaine)) {
		alert('Vous devez saisir ' + Libelle + '.');
		return(false);
	}
	return(true);
}

// ==============================================
// V�rifie si un input pour de l'integer est vide et valide et 
// affiche les messages d'alerte
// ==============================================
function CheckIsInteger(oInput, Libelle) {
	var Chaine = oInput.value
	if (! IsInteger(Chaine)) {
		alert(Libelle + ' doit être une valeur numérique entière.');
		oInput.focus();
		return(false);
	}   
	return(true);
}

// ==============================================
// Vérifie si un input pour de l'integer est vide et valide et 
// affiche les messages d'alerte
// ==============================================
function CheckIntegerInput(Chaine, Libelle) {
	if (! IsInteger(Chaine)) {
		alert(Libelle + ' doit être une valeur numérique entière.');
		return(false);
	}   
	if (IsNumericEmpty(Chaine)) {
		alert('Vous devez saisir ' + Libelle + '.');
		return(false);
	}
	return(true);
}

// ==============================================
// V�rifie si un input pour du text est vide et 
// affiche les messages d'alerte
// ==============================================
function CheckStringInput(InputText, Libelle) {
	var Chaine = InputText.value.trim()
	if (IsEmptyString(Chaine)) {
		alert('Vous devez saisir ' + Libelle + '.');
		InputText.focus()
		return(false);
	}   
	return(true);
}

// ==============================================
// V�rifie si une chaine est vide  
// ==============================================
	function IsEmptyString(Chaine) {
	if ((Chaine == '') ||
		(Chaine == null) ){
		return(true);
	}
	return(false);
}

// ==============================================
// Teste si une chaine contient un e-mail
// ==============================================
function IsEmail(emailStr) {
/*	The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. 
*/
	var emailPat=/^(.+)@(.+)$/;
	
/*	The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ]    
*/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
/*	The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed. 
*/
	var validChars="\[^\\s" + specialChars + "\]";
	
/*	The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. 
*/
	var quotedUser="(\"[^\"]*\")";
	
/*	The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. 
*/
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
/*	The following string represents an atom (basically a series of
	non-special characters.) 
*/
	var atom=validChars + '+';
	
/*	The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. 
*/
	var word="(" + atom + "|" + quotedUser + ")";
	
/*	The following pattern describes the structure of the user */
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
/*	The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. 
*/
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/*	Finally, let's start trying to figure out if the supplied address is
	valid. 
*/

/*	Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. 
*/
	var matchArray=emailStr.match(emailPat)
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. 
  */
	if (matchArray==null) {
		alert("Adresse Email invalide (verifiez les @ ou les .)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// See if "user" is valid 
	// user is not valid
	if (user.match(userPat)==null) {
		alert("Nom d'utilisateur invalide.");
		return false;
	}

/*	if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. 
*/
	var IPArray=domain.match(ipDomainPat);
	// this is an IP address
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Adresse IP invalide!");
				return false;
			}
		}
		return true;
	}

//	Domain is symbolic name
	var domainArray=domain.match(domainPat);
	if (domainArray==null) {
		alert("Nom de domaine invalide.");
		return false;
	}
/*	domain name seems valid, but now make sure that it ends in a
	three-letter word (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. 
*/
/* Now we need to break up the domain to get a count of how many atoms
   it consists of. 
*/
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	// the address must end in a two letter or three letter word.
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {
		alert("L'adresse doit se terminer par un domaine en trois lettres ou un pays en deux lettres.");
		return false;
	}

//	Make sure there's a host name preceding the domain.
	if (len<2) {
		alert("L'adresse Email n'as pas de nom d'hôte!");
		return false;
	}

//	If we've gotten this far, everything's valid!
	return true;
}

// ==============================================
// V�rifie si un input pour un e-mail est vide et 
// affiche les messages d'alerte
// ==============================================
function CheckEmailInput(InputEmail, Libelle) {
	var Chaine = InputEmail.value;
			
	if (! CheckStringInput(InputEmail, Libelle)) return(false);

	if (! IsEmail(Chaine)) {
		InputEmail.focus()
		return(false);
	}
	return(true);
}

// Teste la validité d'un numéro de téléphone
function IsTelNum(sString)
{
	if (sString == "") return (true);

	var slenght = sString.length;
	var sregex = '[0-9()\\.\\+\\-\\s]{'+ slenght + '}';
	var re = new RegExp(sregex,"g");

	return re.test(sString);
}

// ==============================================
// Vérifie la validité d'un numéro de téléphone
// ==============================================
function CheckTelNum(Inputtel, Libelle) {
	var Chaine = Inputtel.value

	if (! IsTelNum(Chaine)) 
	{
		alert(Libelle + ' est incorrect.');
		Inputtel.focus()
		return(false);
	}

	return(true);
}
function ouvre_popup(page, widthVal, heightVal) {
	leftVal = (screen.width - widthVal) / 2  ;
	topVal = (screen.height - heightVal) / 2 ;
	
	openparam = 'menubar=no, status=no, directories=no, location=no, scrollbars=yes,  '
	openparam = openparam + 'width='	+ widthVal + ', '
	openparam = openparam + 'height='	+ heightVal + ', '
	openparam = openparam + 'top='		+ topVal + ', '
	openparam = openparam + 'left='		+ leftVal + ', '
	return window.open(page,"_blank",openparam);
}

//-->

