var month_fr=new Array('Janvier', 'Fevrier', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre');
var day_fr=new Array('Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi');
var short_day_fr = [	'Dim', 'Lun', 'Mar', 'Me', 'Jeu', 'Ven', 'Sam'];

function isBissextile(annee) {
	if (annee%4==0 && annee %100!=0 || annee%400==0) return true; else return false;
}

function nb_01_01(day,month,year){
	nb_mois=new Array(13);
	nb_mois=[,0,31,59,90,120,151,181,212,243,273,304,334];
	j=eval(day) ; m=eval(month) ; a=eval(year);
	nb=nb_mois[m]+j-1 ;
	if (isBissextile(a) && m>2) nb++;
	return nb;
}

function nb_jour_annee(a1,a2){
	n=0;
	if (a1==a2) n=0 ; else
	for (i=a1; i<a2; i++) {n += 365; if (isBissextile(i)) n++}
	return n;
}

function getDayName(date){
	return day_fr[date.getDay()];
}

function validInDateAndOutDate(inDate,outDate){
  	//véfification du format de date
      if (!isValidDDSlashMMSlashYYYYDate(inDate) || isValidDDSlashMMSlashYYYYDate(outDate) ) {
         alert("Attention soit la date n'est pas correcte, soit elle n'est pas au format JJ/MM/AAAA");
		 return false;
      }
  	 //création d'un tableau ou je range la date début dans trois cases, si il y a des zero je les supprimme
   	deb = new Array(3); 
   if (inDate.substring(0,1)=="0"){
      deb[1]=parseInt(inDate.substring(1,2));
      }
   else {
      deb[1]=parseInt(inDate.substring(0,2));
      }
   if (datedebut.substring(3,4)=="0"){
      deb[2]=parseInt(inDate.substring(4,5));
      }
   else {
      deb[2]=parseInt(inDate.substring(3,5));
      }
   deb[3]=parseInt(inDate.substring(6,10));
   
   //si la datefin contient 1 on vient du formulaire dateresadebut donc on compare avec la date du jour
   //dans ce cas on récupère un format de date javascript, on le transforme et on le range dans un tableau
   //je ne transforme pas la variable datfin que je réutilise plus loin
   fin = new Array(3); 
   if (datefin.substring(0,1)=="0"){
         fin[1]=parseInt(outDate.substring(1,2));
      }
      else {
         fin[1]=parseInt(outDate.substring(0,2));
      }
      if (datefin.substring(3,4)=="0"){
         fin[2]=parseInt(outDate.substring(4,5));
      }
      else {
         fin[2]=parseInt(outDate.substring(3,5));
         }
      fin[3]=parseInt(outDate.substring(6,10));
      var message="la date de fin de réservation ne peut être infèrieure à la date de début";
   
     //comparaison des dates 

      if (deb[3]<fin[3]) {
         alert("date invalide la date de debut est apres la date de fin");
		 return false;
         }
      else {
         if ((deb[3]== fin[3]) && (deb[2]<fin[2])){
            alert(message); return false;
         }
         else {
            if ((deb[3]== fin[3]) && (deb[2]==fin[2]) && (deb[1]<fin[1])){
            alert(message); return false;
            }
         }
      }
      //si la date de réservation dépasse un an on demande une confirmation
      if (outDate!='1'){
         if ((deb[3]-fin[3])>1){
            if (confirm("attention, vous avez réservé pour plus d'un an, confirmer ?")) {
            return true;
            }
            else {
               return false;
            }
         }
      }
   return true;
   }

 function isValidDDSlashMMSlashYYYYDate(d) {
  var dateToVerify=d
  // rangement de la date dans des variables
  if (dateToVerify.substring(0,1)=="0"){
         var j=parseInt(dateToVerify.substring(1,2));
      }
      else {
         var j=parseInt(dateToVerify.substring(0,2));
      }
      if (dateToVerify.substring(3,4)=="0"){
         var m=parseInt(dateToVerify.substring(4,5));
      }
      else {
         var m=parseInt(dateToVerify.substring(3,5));
         }
      var a=parseInt(dateToVerify.substring(6,10));
   //si la longueur est différent de 10 , problème
     if (dateToVerify.length != 10) {
         return false;
   }
   //les caratères / ne sont pas aux endroits attendus
   else {
      if((dateToVerify.charAt(2) != '/') && (dateToVerify.charAt(5) != '/')) {
         return false;
      }
   }
   //l'année n'est pa un chiffre
   if (isNaN(a)) {
      return false;
    }
   //le mois n'est pas un chiffre ou n'est pas compris entre 0 et12
     if ((isNaN(m))||(m<1)||(m>12)) {
      return false;
    }
   //test si il s'agit d'une année bissextile pour accepter le 29/02
   if (((a % 4)==0 && (a % 100)!=0) || (a % 400)==0){
         if ((isNaN(j)) || ((m!=2) && ((j<1)||(j>31))) || ((m==2) && ((j<1)||(j>29)))) {
            return false;
        }
   }
    else {
         if ((isNaN(j)) || ((m!=2) && ((j<1)||(j>31))) || ((m==2) && ((j<1)||(j>28)))){
         return false;
      }
   }
   return true;
}




function buildDDSlashMMSlashYYYYDate(dateStr) {
	var year;
	var month;
	var day;

	var validDate = isValidDDSlashMMSlashYYYYDate(dateStr);

	if(validDate ){
		year = parseInt(dateStr.substring(6,10));
		
		month =parseInt(dateStr.substring(4,5));
		if (dateStr.substring(3,4)!="0"){
     		month= parseInt(dateStr.substring(3,5));
        }

		 day = parseInt(dateStr.substring(1,2));
		if (dateStr.substring(0,1) != "0"){
         	day = parseInt(dateStr.substring(0,2));
      	}

      	return new Date(year,month-1,day);
	}
}


function nbDays(inDate,outDate){
	return days_between(inDate, outDate);
}	



function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms);
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)

}

function addDays(date,nb){ //can be < 0
	 // The number of milliseconds in one day
     var ONE_DAY = 1000 * 60 * 60 * 24;
	 
	  // Convert date to milliseconds
      var date_ms = date.getTime();
	  
	  var total_ms = date_ms + ONE_DAY * parseInt(nb);
	  
	  
	 var theDate = new Date(total_ms);
	return theDate;
}

function getDDSlashMMSlashYYYYFromDate(theDate){
	var dd = theDate.getDate();
	var mm = theDate.getMonth()+1; // 0 based
	var yy = theDate.getYear();
	if (yy < 1000) yy +=1900; // Y2K fix
	
	if (parseInt(mm) < 10){
		mm = "0" + mm;
	}
	if (parseInt(dd) < 10){
		dd = "0" + dd;
	}
	
	return dd + "/" + mm + "/" + yy;
}

  //On suppose que la date entrée a été validée auparavant
  //au format dd/mm/yyyy
  function getDate(strDate){	  
	day = strDate.substring(0,2);
	month = strDate.substring(3,5);
	year = strDate.substring(6,10);
	d = new Date();
	d.setDate(day);
	d.setMonth(month);
	d.setFullYear(year); 
	return d;  
  }

	  //Retourne:
	  //   0 si date_1 == date_2
  	  //   1 si date_1 > date_2
	  //  -1 si date_1 < date_2	  
	  function compareDatesObjects(date_1, date_2){
	    diff = date_1.getTime() - date_2.getTime();
	    return ( diff == 0 ? diff : diff / Math.abs(diff) );
	  }
	  
	 
	
	  // Format the date to output.
	function ds_format_date(d, m, y) {
		// 2 digits month.
		m2 = '00' + m;
		m2 = m2.substr(m2.length - 2);
		// 2 digits day.
		d2 = '00' + d;
		d2 = d2.substr(d2.length - 2);
		// DD/MM/YYYY
		return d2 + '/' + m2 + '/' + y;
	}


	function isValidDate(dateStr) {
		// Checks for the following valid date formats:
		// DD/MM/YY   DD/MM/YYYY   DD-MM-YY   DD-MM-YYYY
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
		var matchArray = dateStr.match(datePat); // is the format ok?
		if (matchArray == null) {
			alert("La date n'est pas valide");
			return false;
		}
		day = matchArray[1]; // parse date into variables
		month = matchArray[3];
		year = matchArray[4];
		if (month < 1 || month > 12) { // check month range
			alert("Le mois doit être compris entre 1 et 12");
			return false;
		}
		if (day < 1 || day > 31) {
			alert("Les jours doivent être compris entre 1 et 31");
			return false;
		}
		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
			alert("Ce mois n'a pas 31 jours!")
			return false;
		}
		if (month == 2) { // check for february 29th
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day>29 || (day==29 && !isleap)) {
				alert("Le mois de février " + year + " n'a pas " + day + " jours!");
				return true;
			}
		}
		return false;
	}
	

