//pour avoir des combos sans initialisation '-' 
//indiquer dans la page appelante 	var no_date=true;


if(typeof(d_imagePath)!='undefined'){
		if(d_imagePath!=null){
				langue = d_imagePath;
		}else
			langue = "fr";
	}else
		langue = "fr";

//Initialisation de variables
var un_jour = 86400000;
var init_boucle=0;
// Vrifie si l'anne est bissextile. Actualise le tableau nombre_de_jour_par_mois[]
var nombre_de_jour_par_mois = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
function isBissextile(annee) {
	if (annee%4 == 0) nombre_de_jour_par_mois[1] = 29;
	else nombre_de_jour_par_mois[1] = 28;
}

function $(id){
	return document.getElementById(id);
}


// Dfinition de la fonction de cration d'objets
function Calendrier(date_arrivee,date_depart,date_min,date_max, nb_nuits, nb_nuits_min, nb_nuits_max) {

	this.date_arrivee = date_arrivee;
	this.date_depart = date_depart;
	this.date_min = date_min;
	this.date_max = date_max;
	this.nb_nuits = nb_nuits;
	this.nb_nuits_min = nb_nuits_min;
	this.nb_nuits_max = nb_nuits_max;
	
    // Dfinition de la mthode affichage_calendrier
    this.affichage_calendrier = function affichage_calendrier(tag_select){
		
		this.affichage_jour(tag_select.jour_arrivee,date_arrivee);
		this.affichage_mois(tag_select.mois_arrivee,date_arrivee);
		this.affichage_annee(tag_select.annee_arrivee,date_arrivee);

		//alert(date_arrivee.getDay());
		
		if($('form_date_jour')){//affichage du jour de la semaine, si le div form_date_jour existe
			$('form_date_jour').style.visibility="visible";
			$('form_date_jour').innerHTML = jourArray[langue][date_arrivee.getDay()]+"&nbsp;"+date_arrivee.getDate()+"&nbsp;"+moislongArray[langue][date_arrivee.getMonth()]+"&nbsp;"+date_arrivee.getFullYear();
		}
		
		if($('form_jour_semaine')){//affichage du jour de la semaine uniquement, si le div form_date_jour existe
			$('form_jour_semaine').style.visibility="visible";
			$('form_jour_semaine').innerHTML = jourArray[langue][date_arrivee.getDay()];
		}
		
		if (tag_select.jour_depart) {
        	this.affichage_jour(tag_select.jour_depart,date_depart);
			this.affichage_mois(tag_select.mois_depart,date_depart);
			this.affichage_annee(tag_select.annee_depart,date_depart);
		} else {
			if ($('date_depart')){
			$('date_depart').innerHTML = jourArray[langue][date_depart.getDay()] + " " + date_depart.getDate() + " " + moislongArray[langue][date_depart.getMonth()] + " " + date_depart.getFullYear()
			}
		}		
		
		this.affichage_nuits(tag_select);//Affichage des nuits
		
    }

//afichage des jours
	this.affichage_jour = function affichage_jour(tag_select,date_reference){
		
		date_reference = new Date(date_reference);
		//alert(date_reference);
		isBissextile(date_reference.getFullYear());
		
		var temp = date_reference.getDate() - 1;
		
		i=0;
		var a;
		for(a in tag_select.options){
			tag_select.options.a!=null?tag_select.options.a = null:0;
			i++;
		}
		/*
		var y=document.createElement('option');
		y.text = "-";
		y.value = "-";
			try{
				tag_select.add(y,null); // standards compliant
			}catch(err){
				b.ie?tag_select.add(y):c_log(err);
			}
		*/
		//alert(nombre_de_jour_par_mois[date_reference.getMonth()]);
		
		if(typeof(no_date)!='undefined' && no_date){
			tag_select.options[0] = new Option('-','-');
			init_boucle=1;
		}
		
		for (i=init_boucle;i<nombre_de_jour_par_mois[date_reference.getMonth()]+init_boucle;i++){
			try{
				tag_select.options[i] = new Option(1+i-init_boucle,1+i-init_boucle);
			}catch(err){
				b.ie?tag_select.add(y):c_log("ligne 90 : "+err);
			}
		}
		
		temp = Math.min(date_reference.getDate(),nombre_de_jour_par_mois[date_reference.getMonth()])-1+init_boucle;
		
		try{
			tag_select.options.selectedIndex = temp;
		}catch(err){
			c_log("ligne 95 : "+err);
		}
		
	}
	
//affichage des mois
	this.affichage_mois = function affichage_mois(tag_select,date_reference){
		date_reference = new Date(date_reference);
		compteur_mois=0;
		mois_selectionne = date_reference.getMonth() - 1;
		
		/*var y=document.createElement('option');
		y.text = "-";
		y.value = "-";
			try{
				tag_select.add(y,null); // standards compliant
			}catch(err){
				b.ie?tag_select.add(y):c_log(err);
			}
		*/
		if(typeof(no_date)!='undefined' && no_date){
			tag_select.options[0] = new Option('-','-');
			init_boucle=1;
		}
		if ((date_max-date_min)/un_jour>=335) {//on affiche tous les mois
			for (i=init_boucle;i<=11+init_boucle;i++) {
				if (i==date_reference.getMonth()) mois_selectionne = compteur_mois;
				var option = document.createElement
					tag_select.options[compteur_mois+init_boucle] = new Option(i+1-init_boucle,i+1-init_boucle);
					compteur_mois++;
			}
			
		} else if (date_max.getFullYear()==date_min.getFullYear()) {
		
			for (i=init_boucle;i<=11+init_boucle;i++) {
				
				if (i<=date_max.getMonth() && i>=date_min.getMonth()) {
					i==date_reference.getMonth()?mois_selectionne = compteur_mois:0;
					try{
						//tag_select.add(y,null);
						tag_select.options[compteur_mois+init_boucle] = new Option(i+1-init_boucle,i+1-init_boucle);
					}catch(err){
						c_log(" ligne 131 : "+err);
					}
					compteur_mois++;
				}
			}
			
		} else {
	
			for (i=init_boucle;i<=11+init_boucle;i++) {
				if ((i<=date_max.getMonth() && date_reference.getFullYear()==date_max.getFullYear()) || (i>=date_min.getMonth() && date_reference.getFullYear()==date_min.getFullYear())) {
					if (i==date_reference.getMonth()) mois_selectionne = compteur_mois;
					try{
						tag_select.options[compteur_mois+init_boucle] = new Option(i+1-init_boucle,i+1-init_boucle);
						//tag_select = new Option(i+1,i+1);
					}catch(err){
						c_log(" ligne 145 : "+err);
					}
					compteur_mois++;
				}
			}
			
		}
	
		/* include1 */
		try{
			tag_select.options.selectedIndex = mois_selectionne+(init_boucle*2);
		}catch(err){
			c_log("ligne 157 : "+err);
		}
		
	}
	
	
//gestion des annees
	this.affichage_annee = function affichage_annee(tag_select,date_reference){
		date_reference = new Date(date_reference);
		/*
		var y=document.createElement('option');
		y.text = "-";
		y.value = "-";
			try{
				tag_select.add(y,null); // standards compliant
			}catch(err){
				b.ie?tag_select.add(y):c_log(err);
			}
		*/
		if(typeof(no_date)!='undefined' && no_date){
			tag_select.options[0] = new Option('-','-');
			init_boucle=1;
		}
		for (i=0;i<=(date_max.getFullYear()-date_du_jour.getFullYear());i++)
			tag_select.options[i+init_boucle] = new Option(date_du_jour.getFullYear()+i,date_du_jour.getFullYear()+i);

		try{
			tag_select.options.selectedIndex = date_reference.getFullYear()-date_du_jour.getFullYear()+init_boucle;
		}catch(err){
			c_log("ligne 182 : "+err);
		}
	}
	
//affichage des nuits
	this.affichage_nuits = function affichage_nuits(tag_select){

		if(typeof(no_date)!='undefined' && no_date){
			init_boucle=1;
		}
		
		for (j=nb_nuits_max;j>(nb_nuits_max-nb_nuits_min);j--) 
			tag_select.nb_nuit.options[j] = null;
			
	
		for (k=0;k<=(nb_nuits_max-nb_nuits_min);k++) 
			tag_select.nb_nuit.options[k+init_boucle] = new Option(nb_nuits_min+k,nb_nuits_min+k);
    
    var n = nb_nuits-nb_nuits_min+Number(init_boucle);
       
    if(!isNaN(n) && n >= 0)
      tag_select.nb_nuit.options.selectedIndex = n;
    else
      tag_select.nb_nuit.options.selectedIndex = init_boucle;
	}
	

// Dfinition de la mthode mise_a_jour
	this.mise_a_jour = function mise_a_jour(tag_select,tagChange) {
		//affichage pour les nuits
		var select_nb_nuit=tag_select.nb_nuit.value;
		if(tag_select.nb_nuit.value=='-'){
			tag_select.nb_nuit.selectedIndex=1;
			select_nb_nuit='1';
		}
		var jour_arrivee=tag_select.jour_arrivee.value;
		if(tag_select.jour_arrivee.value=='-'){
			tag_select.jour_arrivee.selectedIndex=tag_select.jour_arrivee.selectedIndex+1;
		}
		var mois_arrivee=tag_select.mois_arrivee.value;
		if(tag_select.mois_arrivee.value=='-'){
			tag_select.mois_arrivee.selectedIndex=tag_select.mois_arrivee.selectedIndex+1;
		}
		var annee_arrivee=tag_select.mois_arrivee.value;
		if(tag_select.annee_arrivee.value=='-'){
			tag_select.annee_arrivee.selectedIndex=tag_select.annee_arrivee.selectedIndex+1;
		}
		if (tagChange=="nuit") this.date_depart = date_depart = new Date(date_arrivee.getFullYear(),date_arrivee.getMonth(),date_arrivee.getDate()+Number(select_nb_nuit));
		
		//affichage pour une date d'arrivee
		if (tagChange=="arrivee") {
	
		date_arrivee = new Date(Number(tag_select.annee_arrivee.value),tag_select.mois_arrivee.selectedIndex-1,Number(tag_select.jour_arrivee.value));
			this.date_arrivee = new Date(Number(tag_select.annee_arrivee.value),tag_select.mois_arrivee.selectedIndex-1,Number(tag_select.jour_arrivee.value));
		
			date_depart = new Date(date_arrivee.getFullYear(),date_arrivee.getMonth(),date_arrivee.getDate()+Number(select_nb_nuit));
			this.date_depart =	new Date(date_arrivee.getFullYear(),date_arrivee.getMonth(),date_arrivee.getDate()+Number(select_nb_nuit));
		
			if (date_depart>date_max) {
				date_depart = date_max;
				this.date_arrivee = date_arrivee = new Date(date_depart.getFullYear(),date_depart.getMonth(),date_depart.getDate()-Number(select_nb_nuit));
			}
		
			/*if (date_arrivee<date_min) {
				date_arrivee = date_min;
				this.date_depart = date_depart = new Date(date_arrivee.getFullYear(),date_arrivee.getMonth(),date_arrivee.getDate()+Number(select_nb_nuit));
			}*/
      
      if(document.location.href.indexOf('mercure.com') < 1){
        if (date_arrivee < date_min) {
          date_arrivee = new Date(Number(tag_select.annee_arrivee.value),tag_select.mois_arrivee.selectedIndex,Number(tag_select.jour_arrivee.value));
          if(date_arrivee < date_min){
            date_arrivee = new Date(Number(tag_select.annee_arrivee.value)+1,tag_select.mois_arrivee.selectedIndex-1,Number(tag_select.jour_arrivee.value));
          }
          if(date_arrivee>=date_max){
            date_arrivee = date_min;
          }
          
          this.date_depart = date_depart = new Date(date_arrivee.getFullYear(),date_arrivee.getMonth(),date_arrivee.getDate()+Number(select_nb_nuit));
        }
      }
			
		}
		
		//affichage pour une date de depart
		if (tagChange=="depart") {
			this.date_depart = date_depart = new Date(tag_select.annee_depart.value,Number(tag_select.mois_depart.value)-1,tag_select.jour_depart.value);
			this.date_arrivee = date_arrivee = new Date(date_depart.getFullYear(),date_depart.getMonth(),date_depart.getDate()-Number(select_nb_nuit));
			
			if (date_arrivee<date_min) {
				date_arrivee = date_min;
				this.date_depart = date_depart = new Date(date_arrivee.getFullYear(),date_arrivee.getMonth(),date_arrivee.getDate()+Number(select_nb_nuit));
			}
			
		}
		if($('date_depart')){
		$('date_depart').style.visibility="visible";
		}
		this.nb_nuits = nb_nuits = tag_select.nb_nuit.value;
		this.affichage_calendrier(tag_select);
	}
}

function c_log(err){
	if(console)
		console.log(err);
	//b.ie?alert(err):0;
}

function init_select(champs_select){
champs_select.options.selectedIndex = 0;
if($('date_depart')){
$('date_depart').style.visibility="hidden";
}
}

/* include1 */
/*for (i=0;i<=11;i++) {
			if (i<=date_max.getMonth() && i>=date_min.getMonth()) {
				if (i==date_reference.getMonth()) mois_selectionne = compteur_mois;
				tag_select.options[compteur_mois] = new Option(i+1,i+1);
				compteur_mois++;
			}
		}*/
