// JavaScript Document

// 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-Z]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z]+([_\.-]?[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;
}

// fonction antispam email
function dolink(ad){
   link = 'mailto:' + ad.replace(/\.\..+t\.\./,"@");
   return link;
}