/*******************************
 * Fonctions js d'ouverture de calendriers en ajax, couplé au contrôle date.
 * 
 * Nécéssite :
 *  - jquery.js
 *  - popups.js
 */
var Calendriers = function() {
	
	/**
	 * Ouvre le calendrier au niveau du contrôle date d'ID spécifié.
	 * @param string id identifiant du contrôle calendrier
	 * @param string min date minimale accessible
	 * @param string max date mamimale accessible
	 */
	var ouvrir = function(id, min, max) {
		var idBouton = id + '_bouton';
		var idPopup = id + '_popup';
		
		Popups.ouvrirPopup(idBouton, idPopup);

		var calPrefixe = id + '_cal_';
		
		var mois = getMoisCourant(id);
		
		$.ajax({
			url: 'ws/calendrier.php?prefixe=' + calPrefixe
					+ '&' + calPrefixe + 'date=' + mois
					+ '&date_min=' + min + '&date_max=' + max,
			success: function(html) {
				$('#' + idPopup).html(html);
			},
			error: function(xhr, status, error) {
				alert(status + ' ' + error);
			},
			cache: false
		});
	};

	/**
	 * Change le contenu du calendrier pour qu'il affiche le mois donnée.
	 * @param string calPrefixe préfixe défini pour le calendrier (finis par _cal_)
	 * @param mois à afficher, sous la forme MMAAAA
	 * @param string min date minimale accessible
	 * @param string max date mamimale accessible
	 */
	var setMois = function(calPrefixe, mois, min, max) {
		var idPopup = calPrefixe.replace(new RegExp('_cal_$'), '_popup');
		
		$.ajax({
			url: 'ws/calendrier.php?prefixe=' + calPrefixe
					+ '&' + calPrefixe + 'date=' + mois
					+ '&date_min=' + min + '&date_max=' + max,
			success: function(html) {
				$('#' + idPopup).html(html);
			},
			error: function(xhr, status, error) {
				alert(status + ' ' + error);
			}
		});
		
		return false;
	};

	/**
	 * Change la date courante et provoque un submit() du "form" contenant le contrôle.
	 * @param string calPrefixe préfixe défini pour le calendrier (finis par _cal_)
	 * @param mois à afficher, sous la forme JJMMAAAA
	 */
	var setJour = function(calPrefixe, jour) {
		var idPopup = calPrefixe.replace(new RegExp('_cal_$'), '_popup');
		var idTexte = calPrefixe.replace(new RegExp('_cal_$'), '_texte');
		
		jour = '' + jour + '!'; //Fix pour IE 7 afin d'empêcher une conversion implicite de string en int...
		
		$('#' + idTexte).val(
				jour.charAt(0) + jour.charAt(1)
				+ '/' + jour.charAt(2) + jour.charAt(3)
				+ '/' + jour.charAt(4) + jour.charAt(5) + jour.charAt(6) + jour.charAt(7));
		
		Popups.fermerPopup(idPopup);
		
		$('#' + idTexte).attr('form').submit();
		
		return false;
	};

	/**
	 * Retourne le mois actuellement sélectionné.
	 * @param string id identifiant du contrôle calendrier
	 * @return mois sous la forme MMAAAA
	 */
	var getMoisCourant = function(id) {
		var idTexte = id + '_texte';
		
		var texte = $('#' + idTexte).val();

		if (/^\d{8}$/.test(texte)) { // 30041986
			return texte.substr(2, 2) + texte.substr(4, 4);
		} else if (/^\d{2}[^\d]\d{2}[^\d]\d{4}$/.test(texte)) { // 30/04/1986
			return texte.substr(3, 2) + texte.substr(6, 4);
		} else if (/^\d{4}[^\d]\d{2}[^\d]\d{2}$/.test(texte)) { // 1986-04-30
			return texte.substr(5, 2) + texte.substr(0, 4);
		} else {
			var now = new Date();
			var mois = '' + (now.getMonth() + 1);
			if (mois < 10) mois = ' ' + mois;
			return mois + now.getFullYear();
		}
	};
	
	return {
		ouvrir: ouvrir,
		setMois: setMois,
		setJour: setJour
	};
	
}();
