
$(document).ready(
function() {
	// Date picker

	var today = new Date();
	//calcule date de fin -> 01/MM-1/YYYY+1
	var nbJours =31;
	var mois =today.getMonth()-1;
	if(mois<0){
	  mois =11;
	}
	var annee = today.getFullYear()+1;
	if(mois==1){//fevrier
	   nbJours=28;
	   if(annee%4==0){
	     nbJours=29;
	   }
	}else{
	     if(mois==3 || mois==5 || mois==8 || mois==10){//avril,juin,septembre et novembre
	       nbJours=30; 
	     }
	}
    var dateFin = new Date(annee,mois,nbJours) ;
    
	//Apply date picker with calendar on input click
	hideSelect = function(){
	if(document.all && parseInt(navigator.appVersion.split(";")[1].replace("MSIE","")) < 7)
		{
		allSelect = document.getElementsByTagName('SELECT');
		for(i=0;i<allSelect.length;i++)
		allSelect[i].style.visibility="hidden";
		}
	}
	
	showSelect = function(){
	//On teste si on est sous IE (document.all) et si la version est inférieur au 7 
	if(document.all && parseInt(navigator.appVersion.split(";")[1].replace("MSIE","")) < 7)
		{
		allSelect = document.getElementsByTagName('SELECT');
		for(i=0;i<allSelect.length;i++)
		allSelect[i].style.visibility="visible";
		}
	}	
		
	hideSelectDepart = function(){
	  if(document.all && parseInt(navigator.appVersion.split(";")[1].replace("MSIE","")) < 7){
		if(document.getElementById("rangeDepart")){	
		  document.getElementById("rangeDepart").style.visibility="hidden";
		  document.getElementById("rangeRetour").style.visibility="hidden";
		  document.getElementById("nbSen").style.visibility="hidden";
		  document.getElementById("nbENF").style.visibility="hidden";
		}
	  }
	}

	hideSelectRetour = function(){
		if(document.all && parseInt(navigator.appVersion.split(";")[1].replace("MSIE","")) < 7){
		  if(document.getElementById("rangeRetour")){	
		    document.getElementById("rangeRetour").style.visibility="hidden";
		    document.getElementById("nbSen").style.visibility="hidden";
		    document.getElementById("nbENF").style.visibility="hidden";
		    document.getElementById("nbEtud").style.visibility="hidden";
		  }
		}
	}
	
	$('input#dateDepart, input#dateRetour').datePicker({clickInput:true,renderCallback:hideSelect});
	
    $('input#dateDepart').dpSetStartDate(today.addDays(1).asString());	
	$('input#dateRetour').dpSetStartDate(today.addDays(1).asString());
	
	$('input#dateDepart').dpSetEndDate(dateFin.asString());
	$('input#dateRetour').dpSetEndDate(dateFin.asString());

	$('input#dateDepart')
	.bind(
		'dpDisplayed',
		function(e) {
			showSelect();			
			hideSelectDepart();
		}
	)
	.bind(
		'dpMonthChanged',
		function(e) {
			showSelect();
			hideSelectDepart();
		}
	)
	.bind(
		'dpClosed',
		function(e, selectedDates) {
			var d = selectedDates[0];
			if (d) {
				// dateRetour même jour que dateDepart +7  au minimum
				//$('input#dateRetour').dpSetStartDate(d.addDays(7).asString());
			}
			showSelect();			
		}
	)
	.bind(
		// when a date is selected update the SELECTs
		'dateSelected',
		function(e, selectedDate, $td, state) {		    
			updateSelects(selectedDate, "Depart");
			updateRetour(selectedDate,dateFin);
			
			//maj calendrier date retour
			var selectedDateRet = selectedDate.addDays(1);			
			var mSelected ;		    
		    var ySelected ;
			if(parseInt(selectedDateRet.getTime()) < parseInt(dateFin.getTime()) ){  
		       mSelected = selectedDateRet.getMonth();		    
		       ySelected = selectedDateRet.getFullYear();  	       
		       $('input#dateRetour').dpSetStartDate(selectedDateRet.asString());	      	
		    }else{     
		        mSelected = dateFin.getMonth();		    
		        ySelected = dateFin.getFullYear();
		        $('input#dateRetour').dpSetStartDate(dateFin.asString());		       	       	
		    }    
		    	        
		    $('input#dateRetour').dpSetDisplayedMonth(mSelected,ySelected);
		    	
		}
	);
	
	$('input#dateRetour')
	.bind(
		'dpDisplayed',
		function(e) {
			showSelect();			
			hideSelectRetour();
		}
	)
	.bind(
		'dpMonthChanged',
		function(e) {
			showSelect();
			hideSelectRetour();
		}
	)
	.bind(
		// when a date is selected update the SELECTs
		'dateSelected',
		function(e, selectedDate, $td, state) {
			updateSelects(selectedDate, "Retour");			
			showSelect();
		}
	)
	.bind(
		'dpClosed',
		function(e) {
			showSelect();			
		});

	$('select[@name=jjDepart], select[@name=mmDepart]')
	.bind(
		'change',
		function() {
			var j = $("select[@name=jjDepart]").val();
			var mm = $("select[@name=mmDepart]").val();
			var m = mm.substring(4,7);
			var y = mm.substring(0,4);
			if(m.charAt(0) == "0") {
				m = m.charAt(1);			
			}
			var d = new Date(parseInt(y), parseInt(m)-1, parseInt(j));						
			// set start and end date	       
            //un jour en milliseconde
            var  un_jour=1000*60*60*24 ;
            var todayDepart = new Date();            
            var diff = parseInt(d.getTime()) - parseInt(todayDepart.getTime());           
            if(diff>0){
               diff = Math.ceil(diff/un_jour);
            }else{
               diff = 1;
            }
                              
			$('input#dateDepart').dpSetStartDate(todayDepart.addDays(diff).asString());
			$('input#dateDepart').dpSetDisplayedMonth(m-1,y);
			var todayRetour = new Date();
			var todayRetourPlusSept=todayRetour.addDays(diff+1);	
			
			if(parseInt(dateFin.getTime()) > parseInt(todayRetourPlusSept.getTime())){	
	           $('input#dateRetour').dpSetStartDate(todayRetourPlusSept.asString());
	        }else{
	           $('input#dateRetour').dpSetStartDate(dateFin.asString());	          
	        }
	        $('input#dateRetour').dpSetDisplayedMonth(m-1,y);	        
	        updateRetour(d,dateFin);
		}
	);
	
	$('select[@name=jjRetour],select[@name=mmRetour]')
	.bind(
		'change',
		function() {
			var jRet = $("select[@name=jjRetour]").val();
			var mmRet = $("select[@name=mmRetour]").val();
			var mRet = mmRet.substring(4,7);
			var yRet = mmRet.substring(0,4);
			if(mRet.charAt(0) == "0") {
				mRet = mRet.charAt(1);			
			}
			
			var dRet = new Date(parseInt(yRet), parseInt(mRet)-1, parseInt(jRet));
			updateRetourRet(dRet);				
			var  un_jour=1000*60*60*24 ;
            var todayRet = new Date();            
            var diffRet = parseInt(dRet.getTime()) - parseInt(todayRet.getTime());           
            if(diffRet>0){
               diffRet = Math.ceil(diffRet/un_jour);               
            }else{
               diffRet = 7;
            }
			
	        $('input#dateRetour').dpSetStartDate(todayRet.addDays(diffRet).asString());
	        $('input#dateRetour').dpSetDisplayedMonth(mRet-1,yRet);
		}
	);
	
});

var updateRetour = function(selectedDate,dateFin) {		 
		 selectedDate = new Date(selectedDate);	
		 var selectedDatePlusSept = selectedDate.addDays(7);		
		 if(parseInt(dateFin.getTime()) < parseInt(selectedDatePlusSept.getTime())){	
		     updateSelects(dateFin, "Retour") ;		    
		 }else{
		     updateSelects(selectedDatePlusSept, "Retour") ;		    
		 }
}

//update Retour apres avoir modifié la date de retour
var updateRetourRet = function(selectedDate) {		 
		 selectedDate = new Date(selectedDate);		
		 updateSelectsRet(selectedDate) ;
}

var updateSelectsRet = function (selectedDate)	{
		selectedDate = new Date(selectedDate);
		var d = selectedDate.getDate()-1;
		var m = selectedDate.getMonth() + 1;
		var y = selectedDate.getFullYear();
		var aaaaMM = "";
		if(m < 10) {
			aaaaMM = String(y)+ String(0) + String(m);			
		} else {
			aaaaMM = String(y) + String(m);
		}
						
		updateJJRetour(aaaaMM);	
		($("select[@name=jjRetour]")[0]).selectedIndex = d;
		$("select[@name=mmRetour]").val(aaaaMM);			
}

var updateSelects = function (selectedDate, trajet)	{
		selectedDate = new Date(selectedDate);
		var d = selectedDate.getDate() - 1;
		var m = selectedDate.getMonth() + 1;
		var y = selectedDate.getFullYear();
		var aaaaMM = "";
		if(m < 10) {
			aaaaMM = String(y)+ String(0) + String(m);			
		} else {
			aaaaMM = String(y) + String(m);
		}
				
		($("select[@name=jj"+trajet+"]")[0]).selectedIndex = d;
		$("select[@name=mm"+trajet+"]").val(aaaaMM);
		
		// met à jour les derniers jours du mois dans la liste déroulante des jours		
		if(trajet!=null && trajet=="Depart"){
			updateJJDepart(aaaaMM);
		}else if(trajet!=null && trajet=="Retour"){
			updateJJRetour(aaaaMM);
		}
		($("select[@name=jj"+trajet+"]")[0]).selectedIndex = d;		
}


