// Vérifie que le code postal est correct
function fnIsCodePostal(aValue, iNullAllowed) {
	//get length of string
	iLength = aValue.length;
	//check if null
	if (iLength == 0 && iNullAllowed == 1)
		return true;
	if (iLength != 5 && iNullAllowed != 1)
		return false;
	re = /^[0-9]*$/;
	iSearch = re.exec(aValue);
	if (iSearch == null)
		return false;
	return true;
}

// Vérifie que le format de l'heure est correct: hh/mm
function fnIsHour(str) {
	var ok = true;
	var h=(str.substring(0,2));
	var m=(str.substring(3,2));
	//Expression rationnelle : l'heure saisie peut avoir les formes : 08:30, 8:30
	re = /^[0-9]{2}:[0-9]{2}$/;
	iSearch = re.exec(str);
	if (iSearch == null){
		ok = false;
	}else{
		//Décomposition de l'heure
		mask_h = /^[0-9]{2}/;
		mask_m = /:[0-9]{2}$/;
		mask_m2 = /[0-9]{2}/;
		h = mask_h.exec(str);
		m = mask_m2.exec(mask_m.exec(str));

		if(!(h>=0 && h<=23)){
			ok = false;
		}
		if(!(m>=0 && m<=59)){
			ok = false;
		}
	}
	return ok;
}

// Vérifie que le format de la date est correct: jj/mm/aa
function fnIsDate(str){
	var filter=/^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(\d{2})$/i
	if (!filter.test(str)){
		testresults=false
	}else{testresults=true}
	return (testresults)
}

// Vérifie que la chaine correspond bien à une adresse email
function fnIsEmail(aValue)
{
	re = /^[a-zA-Z0-9_.\-]+\@[a-zA-Z0-9_.\-]+\.[a-zA-Z0-9_\-]+$/;
	iSearch = re.exec(aValue);
	if (iSearch == null)
		return false;
	return true;
}

// Vérifie qu'une chaine est bien composée de caractères alphanumériques
function fnIsAlphaNumeric(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	var ok = "yes";
	var temp;
	if (field.value.length == 0) {
		return false;
	}
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		return false;
   	}
   	return true;
}


function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;

}

function IsTelephone(sText)
{
   var ValidChars = "0123456789 -";
   var IsNumber=true;
   var Char;
   if (sText.length < 1) {
   		IsNumber = false;
	} else {
	   for (i = 0; i < sText.length && IsNumber == true; i++)
	      {
	      Char = sText.charAt(i);
	      if (ValidChars.indexOf(Char) == -1)
	         {
	         IsNumber = false;
	         }
	      }
	}
   return IsNumber;

}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)

	if (pos1==-1 || pos2==-1){
		alert("Attention au format : mm/jj/aaaa")
		return false
	}
	if (strMonth.length<1  || month<1 || month>12){
		alert("Veuillez saisir un mois valide")
		return false
	}
	 
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Veuillez saisir un jour valide")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Veuillez saisir une année sur 4 chiffres entre "+minYear+" et "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Veuillez saisir une date valide")
		return false
	}
return true
}
//-->
