/**
 * Fonctions javascript / ajax du calendrier
 * @author Mathieu MARFISI
 */
  
	/**
	 * Fonction pour creation de l'objet XMLRequest AJAX
	 */
	function calendrierDateInputCreateXHR() {
	    
	    var xhr;
	    
	    try {
	    	xhr = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (err) {
	    	try {
	    		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	    	} catch(err) {
	    		xhr = false;
	    	}
	    }
	    
	    if(!xhr && typeof XMLHttpRequest != 'undefined') {
	    	xhr = new XMLHttpRequest();
	    }
	    
	    return xhr;
	}
 
  /**
   * Affiche ou ferme le calendrier suivant l'état initial
   * @param champs Le nom du champs date input
   * @param langue La langue du calendrier (ex : FR, UK ect..)
   */
	function calendrierDateInputShow(champs,langue) {
					
			var calendrier = document.getElementById(champs + '_CAL');
			var AUTORISE_DATE_PASSE = document.getElementById(champs + '_AUTORISE_DATE_PASSE');
			var FORMAT_DATE = document.getElementById(champs + '_FORMAT_DATE');
			
			if(calendrier.style.visibility ==  'visible') {
				calendrier.style.visibility =  'hidden';
			} else {
				// Requete AJAX
				var xhr = calendrierDateInputCreateXHR();
				xhr.onreadystatechange = function() {
						if(xhr.readyState == 4){    
		        
			        var response = xhr.responseText;
			        var html = new String();
			      
			        // Récupération du code HTML
			        html = response;
					    calendrier.className = "calendrierDateInput";
			        calendrier.innerHTML = html;
		    		}
				};
	 			xhr.open('GET', 'calendrier_di/calendrier.ajax.php?champs=' + champs + '&langue=' + langue
	 						+ '&autorise_date_passe=' + AUTORISE_DATE_PASSE.value + '&format_date=' + FORMAT_DATE.value);
			  xhr.send(null);
				calendrier.style.visibility =  'visible';
			}				
	}
	
	/**
	 * Action l'on selectionne une date dans le calendrier, 
	 * remplit le champs input et ferme le calendrier
	 * @param champs Le nom du champs date input
	 * @param dateSelectionne La date que l'on a selectionnée
	 */
	function calendrierDateInputSelectDate(champs,dateSelectionne) {
			var calendrier = document.getElementById(champs + '_CAL');
			var dateInput = document.getElementById(champs + '_ID');
			dateInput.value = dateSelectionne;
			calendrier.style.visibility =  'hidden';	
	}
	
	/**
	 * Action pour changer de mois dans le calendrier
	 * @param champs Le nom du champs date input
	 * @param langue La langue du calendrier (ex : FR, UK ect..)
	 * @param d La mois suivant ou precedent sous la forme YYYYMM
	 */
	function calendrierDateInputChangeMois(champs,langue, d) {
		
		  var calendrier = document.getElementById(champs + '_CAL');
		  var AUTORISE_DATE_PASSE = document.getElementById(champs + '_AUTORISE_DATE_PASSE');
			var FORMAT_DATE = document.getElementById(champs + '_FORMAT_DATE');
			
		  // Requete AJAX
		  var xhr = calendrierDateInputCreateXHR();
	   	xhr.onreadystatechange = function() {
						if(xhr.readyState == 4){    
		        
			        var response = xhr.responseText;
			        var html = new String();
			      
			        // Récupération du code HTML
			        html = response;
					    calendrier.className = "calendrierDateInput";
			        calendrier.innerHTML = html;
		    		}
				};
			xhr.open('GET', 'calendrier_di/calendrier.ajax.php?champs=' + champs + '&langue=' + langue + '&autorise_date_passe=' 
									+ AUTORISE_DATE_PASSE.value + '&format_date=' + FORMAT_DATE.value + '&d=' + d);
		  xhr.send(null);
	}
	
	/**
	 * Pour ferme le calendrier
	 * @param champs Le nom du champs date input
	 */
	function  calendrierDateInputFermer(champs) {
	  var calendrier = document.getElementById(champs + '_CAL');
		calendrier.style.visibility =  'hidden';
	}	
