﻿

//classe permettant de gérer un selecteur de date par drop down list
DropDownDate = function(prefix,suffixe,IDChampResultat){
    this.jour = document.getElementById(prefix+'jour'+suffixe);
    this.mois = document.getElementById(prefix+'mois'+suffixe);
    this.annee = document.getElementById(prefix+'annee'+suffixe);
    this.est_mode_tb_only = typeof(this.jour) == "undefined";
    this.resultat = document.getElementById(IDChampResultat);   
}
DropDownDate.prototype.SetJour = function(val){    
    this.selectValue('jour',val);       
}
DropDownDate.prototype.SetMois = function(val){    
    this.selectValue('mois',val);
}
DropDownDate.prototype.SetAnnee = function(val){
    this.selectValue('annee',val);
}
DropDownDate.prototype.selectValue   = function (nom_select, itemValue)
{
    var ListBox = this.GetControl(nom_select);
	if (ListBox)
	{
		var nbOptions = ListBox.options.length;

        for(i=0;i < nbOptions; i++)
        {
            if(ListBox[i].value == itemValue)
			{
	            ListBox[i].selected = "selected";
	            break;
            }
        }
	}
	else{
	    this.SetPartie(nom_select,itemValue);
	}
}
DropDownDate.prototype.MajResultat = function(){
    this.resultat.value=this.GetDate();
}
DropDownDate.prototype.GetDate = function(){
    return this.VerifTailleComposant('jour',this.GetValueJour())+"/"+this.VerifTailleComposant('mois',this.GetValueMois())+"/"+this.VerifTailleComposant('annee',this.GetValueAnnee());
}
DropDownDate.prototype.VerifTaille = function(){
    return this.GetDate().length == 10;
}
DropDownDate.prototype.IsDate = function(){    
        var aiDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
        var iDay = this.GetValueJour();
        var iMonth = this.GetValueMois();
        var iYear = this.GetValueAnnee();
            
        if (iDay < 1 || iMonth < 1 || iYear < 0)
			return 0;

        if (iMonth > 12)
			return 0;

        iYear += iYear < 100 ? iYear > 10 ? 1900 : 2000 : 0;
        aiDays[1] += (iYear % 4 ? 0 : iYear % 100 ? 1 : iYear % 400 ?
        iYear == 200 ? 1 : 0 : 1);

        return (iDay <= aiDays[iMonth - 1]);
            
}
DropDownDate.prototype.IsSupToday = function(){
    var today = new Date();
    return this.GetValueAnnee() > today.getFullYear() ||
           (this.GetValueAnnee() == today.getFullYear() && this.GetValueMois() > (today.getMonth() + 1)) ||
           (this.GetValueAnnee() == today.getFullYear() && this.GetValueMois() == (today.getMonth() + 1) && this.GetValueJour() > today.getDate()) ;
}
DropDownDate.prototype.SetDate = function (){
    if(this.resultat.value != ''){
        var infos = this.resultat.value.split('/');
        this.SetJour(infos[0]);
        this.SetMois(infos[1]);
        this.SetAnnee(infos[2]);
    }
}
DropDownDate.prototype.GetValueAnnee = function(){
   return this.GetIntValue('annee');
}
DropDownDate.prototype.GetValueMois = function(){
   return this.GetIntValue('mois'); 
}
DropDownDate.prototype.GetValueJour = function(){
   return this.GetIntValue('jour'); 
}
DropDownDate.prototype.GetIntValue = function(nom_select){
    var select = this.GetControl(nom_select);
    if(select){
        if(select.value == '')
            return -1;
        return parseInt(select.value,10);
    }
    else{
        return this.GetPartie(nom_select);
    }
}
DropDownDate.prototype.GetControl = function(nom){
    if(this.est_mode_tb_only){
        return false;
    }
    return this[nom];
}
DropDownDate.prototype.GetPartie = function (nom){
    var tab_split = this.resultat.value.split('/');
    switch(nom){
        case 'annee' : return parseInt(tab_split[2],10);
        case 'mois' : return parseInt(tab_split[1],10);
        case 'jour' : return  parseInt(tab_split[0],10);
    } 
    return '';
}
DropDownDate.prototype.SetPartie = function (nom,valeur){
    var annee = (nom != 'annee') ? this.GetPartie('annee') : valeur;
    var mois = (nom != 'mois') ? this.GetPartie('mois') : valeur;
    var jour = (nom != 'jour') ? this.GetPartie('jour') : valeur;
    this.resultat.value = this.VerifTailleComposant('jour',jour)+"/"+this.VerifTailleComposant('mois',mois)+"/"+this.VerifTailleComposant('annee',annee);
   
}
DropDownDate.prototype.VerifTailleComposant = function(nom,val){
val = val.toString();
    if(val.toString().trim() == '') return '';
    
    var nb_char = 0;
     switch(nom){
        case 'annee' : nb_char = 4;
        case 'mois' : nb_char = 2;
        case 'jour' :nb_char = 2;
    } 
    while(val.length < nb_char){
        val = '0'+val;
    }
    return val;
}


//classe permettant de gérer la selection d'un séjour via des DropDownDate
SelectSejour = function (prefix,IDChampResultatDeb,IDChampResultatFin){
    this.debut = new DropDownDate(prefix,'Arrivee',IDChampResultatDeb);
    this.fin = new DropDownDate(prefix,'Depart',IDChampResultatFin);
    this.debut.SetDate();
    this.fin.SetDate();
    this.SetEcouteur(this.debut.mois);
    this.SetEcouteur(this.debut.annee);
   
}
SelectSejour.prototype.SetEcouteur = function(list){
    if(list){
        list.selectSejour = this;
        list.onchange =AjusteDate;
    }
}
SelectSejour.prototype.VerifDate = function(){
        if ( !this.debut.VerifTaille() ||  !this.fin.VerifTaille() )
        {
			return 1;
        }
		else
		{

            if (!this.debut.IsDate())  return 6;
            if (!this.fin.IsDate())  return 7;            
	        if (!this.IsDateSupInf()) return 4;
	        if (!this.fin.IsSupToday()) return 5;
            
		}
		this.debut.MajResultat();
		this.fin.MajResultat();
		return -1;           
}
SelectSejour.prototype.IsDateSupInf = function(){
 return this.fin.GetValueAnnee() > this.debut.GetValueAnnee() ||
           (this.fin.GetValueAnnee() == this.debut.GetValueAnnee() &&  this.fin.GetValueMois() > ( this.debut.GetValueMois())) ||
           (this.fin.GetValueAnnee() == this.debut.GetValueAnnee() &&  this.fin.GetValueMois() == ( this.debut.GetValueMois()) && this.fin.GetValueJour() > this.debut.GetValueJour()) ;

    
				
          
}
SelectSejour.prototype.AjusteDates = function(){
    if(this.debut.annee.value != ""){
       if(this.fin.GetValueAnnee() < this.debut.GetValueAnnee()){
            this.fin.SetAnnee(this.debut.GetValueAnnee());
       }
    }
    if(this.debut.mois.value != ""){
       if(this.fin.GetValueMois() < this.debut.GetValueMois() && (this.debut.annee.value == "" || this.fin.GetValueAnnee() <= this.debut.GetValueAnnee())){
            
                this.fin.SetMois(this.debut.GetValueMois());
            
       }
    }
}
function AjusteDate(){
    this.selectSejour.AjusteDates();
}
