// JavaScript Document

//ajoute une fonction getFrenchDate à l'objet Date
//cette fonction renvoie la date au format jj/mm/aaaa
Date.prototype.getFrenchDate = function() {
	var day = this.getDate();
	var month = this.getMonth()+1;
	var year = this.getFullYear();
	var str = (day<10)? '0'+day : ''+day;
	str += '/';
	str += (month<10)? '0'+month : ''+month;
	str += '/'+year;
	return str;
}

// fonction de trim
function trim(str) {
	return str.replace(/^\s*|\s*$/g,"");
}

// vérifie la validité d'un code postal
function isPostalCode(cp) {
	regexp = new RegExp("[0-9]{5}","gi");

	if((trim(cp).search(regexp) == -1) || (cp.length != 5)) {
		return false;
	}
	else {
		return true;
	}
}

//vérifie la validité d'une adresse mail saisie dans les formulaires
function isEmailAddress(email){
	re = /^[a-zA-Z0-9]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([_\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,3})+$/;
	chaine = new String(email);
	
	return re.test(chaine);
}

function getDaysInMonth(dateObject) {
	var month = dateObject.getMonth();
	var year = dateObject.getFullYear();
	/*
	 * Leap years are years with an additional day YYYY-02-29, where the year
	 * number is a multiple of four with the following exception: If a year
	 * is a multiple of 100, then it is only a leap year if it is also a
	 * multiple of 400. For example, 1900 was not a leap year, but 2000 is one.
	 */
	var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	if (month == 1 && year) {
		if ((!(year % 4) && (year % 100)) ||
			(!(year % 4) && !(year % 100) && !(year % 400))) { return 29; }
		else { return 28; }
	} else { return days[month]; }
}

//sDate : date sous forme de chaine au format jj/mm/aaaa (le séparateur peut être "/", "-" ou ".")
function checkDateValid(sDate) {
	var chaine = new String(sDate);
	if (chaine.match(/^([0-9]{2})[\/\-\.]?([0-9]{2})[\/\-\.]?([0-9]{4})$/)) {
		var d = chaine.split(/[\/\-\.]/g);
		var day = d[0];
		var month = d[1];
		var year = d[2];
		if (d && year!='' && month!='' && day!='') {
			if (year>1900 && year<2050) {
				if (month>0 && month<13) {
					var date = new Date();
					date.setFullYear(year);
					date.setMonth(month-1);
					if (day>0 && day<=getDaysInMonth(date)) {
						return true;
					}
				}
			}
		}
	}
	return false;
}

//vérifie que la date sDate est antérieure à la date sPastDate (sPastDate = date du jour par défaut)
//sDate : date sous forme de chaine au format jj/mm/aaaa (le séparateur peut être "/", "-" ou ".")
//sPastDate : date sous forme de chaine au format jj/mm/aaaa (le séparateur peut être "/", "-" ou ".")
function checkDatePast(sDate, sPastDate) {
	if (sPastDate==undefined) {
		var todayDate = new Date();
		sPastDate = todayDate.getFrenchDate();
	}
	var chaine = new String(sDate);
	var dateValide = false;
	if (chaine.match(/^([0-9]{2})[\/\-\.]?([0-9]{2})[\/\-\.]?([0-9]{4})$/)) {
		var d = chaine.split(/[\/\-\.]/g);
		var day = d[0];
		var month = d[1];
		var year = d[2];
		if (d && year!='' && month!='' && day!='') {
			if (year>1900 && year<2050) {
				if (month>0 && month<13) {
					var date = new Date();
					date.setFullYear(year);
					date.setMonth(month-1);
					if (day>0 && day<=getDaysInMonth(date)) {
						dateValide=true;
					}
				}
			}
		}
	}
	if (dateValide) { //si la date sDate est valide, on vérifie si sPastDate est valide
		dateValide = false;
		var chaine = new String(sPastDate);
		if (chaine.match(/^([0-9]{2})[\/\-\.]?([0-9]{2})[\/\-\.]?([0-9]{4})$/)) {
			var d = chaine.split(/[\/\-\.]/g);
			var dayPast = d[0];
			var monthPast = d[1];
			var yearPast = d[2];
			if (d && yearPast!='' && monthPast!='' && dayPast!='') {
				if (yearPast>1900 && yearPast<2050) {
					if (monthPast>0 && monthPast<13) {
						var date = new Date();
						date.setFullYear(yearPast);
						date.setMonth(monthPast-1);
						if (dayPast>0 && dayPast<=getDaysInMonth(date)) {
							dateValide=true;
						}
					}
				}
			}
		}
	}
	if (dateValide) { //si les deux dates passées en paramètres sont valides, on compare les deux dates
		dateValide = false;
		chaine = new String(year+month+day);
		var chainePast = new String(yearPast+monthPast+dayPast);
		if (chaine <= chainePast) {
			dateValide = true;
		}
	}
	return dateValide;
}


// fonction antispam email
function dolink(ad){
   link = 'mailto:' + ad.replace(/\.\..+t\.\./,"@");
   return link;
}