// Validation d'un nombre entier positif
function isInteger(number) {
	return (number.toString().search(/^[0-9]+$/) == 0);
}

// Validation d'une URL
function isUrl(url) {
	var regexp = /^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i;
	return regexp.test(url);
}

// Validation d'une adresse email
function isEmail(email) {
	var regexp = /^[a-zA-Z0-9][a-zA-Z0-9._-]*@([a-zA-Z][a-zA-Z0-9_\-]*\.)*[a-z0-9]+[a-z0-9\-]+\.[a-z]{2,6}$/;
	return regexp.test(email);
}

// Validation d'une date
function checkDate(day, month, year, complete) {
	var result = false;
	
	// Si on demande une date complète (jour, mois et année)
	if (complete == 1) {
		if (day.length == 0 && month.length == 0 && year.length == 0) {
			result = true;
		}
		else {
			if (isInteger(day) == true && day >= 1 && day <= 31) {
				if (isInteger(month) == true && month >= 1 && month <= 12) {
					if (isInteger(year) == true && year >= 1900 && year <= 2100) {
						// On retranche 1 à month car javascript compte les mois de 0 à 11
						var testDate = new Date();
						month = month - 1;
						testDate.setFullYear(year, month, day);
						
						if (testDate.getDate() == day && testDate.getMonth() == month && testDate.getFullYear() == year) {
							result = true;
						}
					}
				}
			}
		}
	}
	else {
		if (day.length != 0 && month.length != 0 && year.length != 0) {
			if (isInteger(day) == true && day >= 1 && day <= 31) {
				if (isInteger(month) == true && month >= 1 && month <= 12) {
					if (isInteger(year) == true && year >= 1900 && year <= 2100) {
						// On retranche 1 à month car javascript compte les mois de 0 à 11
						var testDate = new Date();
						month = month - 1;
						testDate.setFullYear(year, month, day);
						
						if (testDate.getDate() == day && testDate.getMonth() == month && testDate.getFullYear() == year) {
							result = true;
						}
					}
				}
			}
		}
		else if (month.length != 0 && year.length != 0) {
			if (isInteger(month) == true && month >= 1 && month <= 12) {
				if (isInteger(year) == true && year >= 1900 && year <= 2100) {
					result = true;
				}
			}
		}
		else if (year.length != 0) {
			if (isInteger(year) == true && year >= 1900 && year <= 2100) {
				result = true;
			}
		}
		else if (day.length == 0 && month.length == 0 && year.length == 0) {
			result = true;
		}
	}
	
	return result;
}

// Validation d'un temps (ou d'une heure) au format hh:mm ou hh:mm:ss
function checkTime(time) {
	var result = false;
	
	if (time.length == 5 && time.substring(2, 3) == ':') {
		var list = time.split(':');
		var hour = list[0];
		var minute = list[1];
		
		if (isInteger(hour) && hour >= 0 && hour <= 23 && isInteger(minute) && minute >= 0 && minute <= 59) {
			result = true;
		} 
	}
	else if (time.length == 8 && time.substring(2, 3) == ':' && time.substring(5, 6) == ':') {
		var list = time.split(':');
		var hour = list[0];
		var minute = list[1];
		var second = list[2];
		
		if (isInteger(hour) && hour >= 0 && hour <= 23 && isInteger(minute) && minute >= 0 && minute <= 59 && isInteger(second) && second >= 0 && second <= 59) {
			result = true;
		}
	}
	
	return result;
}
