//Dernière modification le 06/12/2007.

/* Fonctions présentes dans ce fichier:
   isEmpty(s)
   isInteger(s [,eok])
   isAlphabetic(s [,eok])
   isAlphanumeric(s [,eok])
   isDate (dd, mm, yyyy [,eok])
   isSecond(ss)
   isMinute(mm)
   isHour(mm)
   getStandardDate(dd, mm, yyyy [,strSeparateur])
   getStandardHour(ss, mm, hh)
   compareDate(date1, date2, sign)
   getSystemDate()
   getSystemTime()
   getCurrentYear()
   getCurrentMonth()
   getCurrentDay()
   getCurrentHours()
   getCurrentMinutes()
   getCurrentSeconds()
   checkURL(s)
   fillTime(time) 
   checkEmail(emailStr)
   */


// ----------------------------------
// isEmpty(s)
// ----------------------------------
// Retourne true si le champ s est vide.

function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}


// ----------------------------------
// isInteger(s [,eok])
// ----------------------------------
// Retourne true si tous les caractères du champ s sont des chiffres.

function isInteger(s) {
	if(isEmpty(s)) {
		if(isInteger.arguments.length == 1) {
			return (false);
		} else {
			return (isInteger.arguments[1] == true);
		}
	}

	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if((c < "0") || (c > "9")) return (false);
	}

	return (true);
}


// ----------------------------------
// isAlphabetic(s [,eok])
// ----------------------------------
// Retourne true si tous les caractères du champ s sont des lettres de l'alphabet.

function isAlphabetic(s) {
	if(isEmpty(s)) {
		if(isAlphabetic.arguments.length == 1) {
			return (false);
		} else {
			return (isAlphabetic.arguments[1] == true);
		}
	}

	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i).toLowerCase();
		if((c < "a") || (c > "z")) return (false);
	}

	return (true);
}


// ----------------------------------
// isAlphanumeric(s [,eok])
// ----------------------------------
// Retourne true si tous les caractères du champ s sont des chiffres ou des lettres de l'alphabet.

function isAlphanumeric(s) {
	if(isEmpty(s)) {
		if(isAlphanumeric.arguments.length == 1) {
			return (false);
		} else {
			return (isAlphanumeric.arguments[1] == true);
		}
	}

	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isInteger(c) && !isAlphabetic(c)) { return false; }
	}

	return (true);
}


// ----------------------------------
// isDate (dd, mm, yyyy [,eok])
// ----------------------------------
// Retourne true si la date est valide.

function isDate(dd, mm, yyyy) {
	if(isEmpty(dd) && isEmpty(mm) && isEmpty(yyyy)) {
		if(isDate.arguments.length == 3) {
			return (false);
		} else {
			return (isDate.arguments[3] == true);
		}
	}

	if (!isInteger(dd) || !isInteger(mm) || !isInteger(yyyy)) {	return (false);	}

	if (dd < 1 || dd > 31) { return (false); }
	if (mm < 1 || mm > 12) { return (false); }
	if ((yyyy.toString()).length != 4) { return (false); }
	if ((mm==4 || mm==6 || mm==9 || mm==11) && dd==31) { return (false); }

	if (mm == 2) {
		var isleap = (yyyy % 4 == 0 && (yyyy % 100 != 0 || yyyy % 400 == 0));
		if (dd > 29 || (dd==29 && !isleap)) {
			return false;
		}
	}

	return (true);
}


// ----------------------------------
// isSecond(ss)
// ----------------------------------
// Retourne true si le nombre de secondes est valide (0-59).

function isSecond(ss) {
	if (!isInteger(ss)) {	return (false);	}
	if (ss > 59)				{ return (false); }
	return (true);
}


// ----------------------------------
// isMinute(mm)
// ----------------------------------
// Retourne true si le nombre de minutes est valide (0-59).

function isMinute(mm) {
	if (!isInteger(mm)) {	return (false);	}
	if (mm > 59)				{ return (false); }
	return (true);
}


// ----------------------------------
// isHour(mm)
// ----------------------------------
// Retourne true si le nombre d'heures est valide (0-23).

function isHour(hh) {
	if (!isInteger(hh)) {	return (false);	}
	if (hh > 23)				{ return (false); }
	return (true);
}


// ----------------------------------------------
// getStandardDate(dd, mm, yyyy [,strSeparateur])
// ----------------------------------------------
// Retourne une date au format standard (yyyymmdd).
// Il est possible d'ajouter un caractère qui sépare chacune des 3 valeurs qui composent la date.

function getStandardDate(dd, mm, yyyy) {
	var sep = '';

	if(getStandardDate.arguments.length == 4) {
		sep = getStandardDate.arguments[3];
	}

	if (!isDate(dd, mm, yyyy)) {
		return (false);
	} else {
		if ((dd.toString()).length == 1) { dd = '0' + dd;	}
		if ((mm.toString()).length == 1) { mm = '0' + mm;	}

		return (yyyy.toString() + sep + mm.toString() + sep + dd.toString());
	}
}


// ----------------------------------
// getStandardHour(ss, mm, hh)
// ----------------------------------
// Retourne une heure au format standard (hhmmss).

function getStandardHour(ss, mm, hh) {
	if (!isSecond(ss) || !isMinute(mm) || !isHour(hh)) {
		return (false);
	} else {
		if ((ss.toString()).length == 1) { ss = '0' + ss;	}
		if ((mm.toString()).length == 1) { mm = '0' + mm;	}
		if ((hh.toString()).length == 1) { hh = '0' + hh;	}

		return (hh.toString() + mm.toString() + ss.toString());
	}
}


// ----------------------------------
// compareDate(date1, date2, sign)
// ----------------------------------
// Comparaison de deux dates au format standard.
// Le champ sign détermine le type de comparaison, et peut avoir
// les valeurs suivantes: "<", "<=", ">", ">=", "=".

function compareDate(date1, date2, sign) {
	if (date1.length!=8) { return (false); }
	if (date2.length!=8) { return (false); }

	switch (sign) {
		case '<' :
			if (date1 >= date2) { return (false); }
			break;
		case '<=' :
			if (date1 > date2)  { return (false); }
			break;
		case '>' :
			if (date1 <= date2) { return (false); }
			break;
		case '>=' :
			if (date1 < date2)  { return (false); }
			break;
		case '=' :
			if (date1 != date2) { return (false); }
			break;
		default :
			return (false);
			break;
	}

	return (true);
}


// ----------------------------------
// getSystemDate()
// ----------------------------------
// Retourne la date système au format standard (yyyymmdd).

function getSystemDate() {
	return (getStandardDate(getCurrentDay(), getCurrentMonth(), getCurrentYear()));
}


// ----------------------------------
// getSystemTime()
// ----------------------------------
// Retourne l'heure système au format standard (hhmm).

function getSystemTime() {
	var hh = getCurrentHours();
	var mm = getCurrentMinutes();
	var ss = getCurrentSeconds();
	return (hh.toString() + mm.toString() + ss.toString());
}


// ----------------------------------
// getCurrentYear()
// ----------------------------------
// Retourne l'année courante.

function getCurrentYear() {
	var today = new Date();
	today = today.getYear();
	return (today < 1900 ? 1900 + today : today);
}


// ----------------------------------
// getCurrentMonth()
// ----------------------------------
// Retourne le mois de l'année courante (01-12).

function getCurrentMonth() {
	var today = new Date();
	today = today.getMonth() + 1;
	return (today < 10 ? '0' + today : today);
}


// ----------------------------------
// getCurrentDay()
// ----------------------------------
// Retourne le jour du mois courant (01-31).

function getCurrentDay() {
	var today = new Date();
	today = today.getDate()
	return (today < 10 ? '0' + today : today);
}


// ----------------------------------
// getCurrentHours()
// ----------------------------------
// Retourne l'heure du jour courant (00-23).

function getCurrentHours() {
	var today = new Date();
	today = today.getHours();
	return (today < 10 ? '0' + today : today);

}


// ----------------------------------
// getCurrentMinutes()
// ----------------------------------
// Retourne le nombre de minutes de l'heure courante (00-59).

function getCurrentMinutes() {
	var today = new Date();
	today = today.getMinutes();
	return (today < 10 ? '0' + today : today);
}


// ----------------------------------
// getCurrentSeconds()
// ----------------------------------
// Retourne le nombre de secondes de la minute courante (00-59).

function getCurrentSeconds() {
	var today = new Date();
	today = today.getSeconds();
	return (today < 10 ? '0' + today : today);
}


// ----------------------------------
// checkURL(s)
// ----------------------------------

function checkURL(s) {
   var result = s;
   if(result.indexOf("http://") != 0 && result.indexOf("ftp://") != 0) {
      result = "http://" + result;
   }
   return result;
}


// ----------------------------------
// fillTime(time)
// ----------------------------------
// Verifie que le nombre de caractères encodés pour une heure, une minute ou une seconde est bien formatté sur 2 chiffres
// et rempli ces dernières au cas ou elles ne le seraient pas

function fillInteger(inti, longl) {
	if(!isInteger(inti.value, true)) { return false; }

	for(i = inti.value.length; i < longl; i++) {
		inti.value = "0" + inti.value;
	}

	return true;
}

// ----------------------------------
// checkEmail(emailStr)
// ----------------------------------
// Check basique qui verifie si l'adresse email entrée est correcte

//====================
function checkEmail(emailStr) {
//====================
 var re_email = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+[\.]([a-z0-9-]+){2}$/ ;
 return re_email.test(emailStr);
}

