var d, c, nb, div, tab, param, d2, c2, div2, div3, a;
function choix_date(data){
	var choix_1 = document.getElementById(data);
	if(choix_1.style.backgroundColor == "rosybrown" || choix_1.style.backgroundColor == 'rgb(188, 143, 143)'){
		tab = data.split('-');
		if($('date_calendar_'+tab[3]).innerHTML != "" && $('date_calendar2_'+tab[3]).innerHTML != ""){
			var date_calendar_arr = $('date_calendar_'+tab[3]).innerHTML;
			var tab_date_calendar_arr = date_calendar_arr.split(' : ');
			var tab_date_calendar_arr_2 = tab_date_calendar_arr[1].split('/');
			var date_calendar_dep = $('date_calendar2_'+tab[3]).innerHTML;
			var tab_date_calendar_dep = date_calendar_dep.split(' : ');
			var tab_date_calendar_dep_2 = tab_date_calendar_dep[1].split('/');
			var date_calendar_arr_final = tab_date_calendar_arr_2[2]+'-'+tab_date_calendar_arr_2[1]+'-'+tab_date_calendar_arr_2[0]+'-'+tab[3];
			var date_calendar_dep_final = tab_date_calendar_dep_2[2]+'-'+tab_date_calendar_dep_2[1]+'-'+tab_date_calendar_dep_2[0]+'-'+tab[3];
			$('date_calendar_'+tab[3]).innerHTML = '';
			$('date_calendar2_'+tab[3]).innerHTML = '';
			d = document.getElementById('calendar_hidden'+tab[3]);
			c = document.getElementsByClassName(date_calendar_arr_final)[0];
			d.removeChild(c);

			d = document.getElementById('calendar_hidden'+tab[3]);
			c = document.getElementsByClassName(date_calendar_dep_final)[0];
			d.removeChild(c);
			
			div3 = document.getElementById('calendar_hidden_option'+tab[3]);
			c2 = document.getElementsByClassName('option')[0];
			div3.removeChild(c2);
			
			param = 'date1='+date_calendar_arr_final+'&date2='+date_calendar_dep_final+'&ch='+tab[3];
			new	Ajax('tab_jour_clear.php', { asynchronous: false, data: param, method: 'post', onComplete: _ClearCalendar }).request();

		}
		else{
		choix_1.style.backgroundColor = "#DEDEDE";
		d = document.getElementById('calendar_hidden'+tab[3]);
		c = document.getElementsByClassName(data)[0];
		d.removeChild(c);
		var couleur = document.getElementById('1-'+data);
		if(couleur.style.color == "red"){
		document.getElementById('bouton_'+tab[3]).style.display = "block";
		document.getElementById('bouton2_'+tab[3]).style.display = "none";
		}
		else{}
		div = document.getElementById('calendar_hidden'+tab[3]);
		div2 = document.getElementById('calendar_hidden_option'+tab[3]);
		nb = div.childNodes.length;
		$('date_calendar_'+tab[3]).innerHTML = '';
		$('date_calendar2_'+tab[3]).innerHTML = '';
		if(nb == 0){		
		}
		else{
			div3 = document.getElementById('calendar_hidden_option'+tab[3]);
			c2 = document.getElementsByClassName('option')[0];
			div3.removeChild(c2);
			div.firstChild.setAttribute('name', 'date1');
			var test = div.firstChild.getAttribute('value');
			var tab2 = test.split('-');
			
			$('date_calendar_'+tab2[3]).innerHTML = '';
			$('date_calendar2_'+tab2[3]).innerHTML = '';
			$('date_calendar_'+tab2[3]).innerHTML = arrive +' : ' + tab2[2] +'/'+ tab2[1] + '/' + tab2[0];
			
			if(document.getElementById('1-'+test).style.color == "red"){
				document.getElementById('bouton_'+tab2[3]).style.display = "none";
				document.getElementById('bouton2_'+tab2[3]).style.display = "block";
			}
			else{
				document.getElementById('bouton_'+tab2[3]).style.display = "block";
				document.getElementById('bouton2_'+tab2[3]).style.display = "none";
			}
		}
	}
	}else{
	tab = data.split('-');
	div = document.getElementById('calendar_hidden'+tab[3]);
	nb = div.childNodes.length;
	if(nb == 0){
	choix_1.style.backgroundColor = "rosybrown";
	d = document.getElementById('calendar_hidden'+tab[3]);
	if(document.all){
		c = document.createElement('<input type="hidden" name="date'+(nb+1)+'" class="'+data+'" value="'+data+'" />');
	}else{
		c = document.createElement('input');
		c.setAttribute('type', 'hidden');
		c.setAttribute('className', data);
		c.setAttribute('class', data);
		c.setAttribute('name', 'date'+(nb+1));
		c.setAttribute('value', data);
	}
	d.appendChild(c);
	tab = data.split('-');
	
	$('date_calendar_'+tab[3]).innerHTML = '';
	$('date_calendar_'+tab[3]).innerHTML = arrive +' : ' + tab[2] +'/'+ tab[1] + '/' + tab[0];
	
	var couleur = document.getElementById('1-'+data);
	if(couleur.style.color == "red"){
		document.getElementById('bouton_'+tab[3]).style.display = "none";
		document.getElementById('bouton2_'+tab[3]).style.display = "block";
	}
	else{}
	
	}
	else if(nb==1){
		choix_1.style.backgroundColor = "rosybrown";
		d = document.getElementById('calendar_hidden'+tab[3]);
		d2 = document.getElementById('calendar_hidden_option'+tab[3]);
	if(document.all){
		c = document.createElement('<input type="hidden" name="date'+(nb+1)+'" class="'+data+'" value="'+data+'" />');
		c2 = document.createElement('<input type="hidden" name="option" class="option" value="1" />');
	}else{
		c = document.createElement('input');
		c.setAttribute('type', 'hidden');
		c.setAttribute('className', data);
		c.setAttribute('class', data);
		c.setAttribute('name', 'date'+(nb+1));
		c.setAttribute('value', data);
		c2 = document.createElement('input');
		c2.setAttribute('type', 'hidden');
		c2.setAttribute('className', 'option');
		c2.setAttribute('class', 'option');
		c2.setAttribute('name', 'option');
		c2.setAttribute('value', 1);
	}
	d.appendChild(c);
	d2.appendChild(c2);
		var date1 = d.firstChild.getAttribute('value');
		var date2 = d.lastChild.getAttribute('value');
		param = "date1="+date1+"&date2="+date2;
		
		var cal = document.getElementById('calendar_hidden'+tab[3]).firstChild.getAttribute('value');
		var tab_cal = cal.split('-');
		var cal_value = tab_cal[0]+''+tab_cal[1]+''+tab_cal[2];
		if(cal_value < tab[0]+''+tab[1]+''+tab[2]){
			$('date_calendar_'+tab[3]).innerHTML = '';
			$('date_calendar_'+tab[3]).innerHTML = arrive +' : ' + tab_cal[2] +'/'+ tab_cal[1] + '/' + tab_cal[0];
			$('date_calendar2_'+tab[3]).innerHTML = '';
			$('date_calendar2_'+tab[3]).innerHTML = depart +' : ' + tab[2] +'/'+ tab[1] + '/' + tab[0];

		}
		else{
			$('date_calendar_'+tab[3]).innerHTML = '';
			$('date_calendar_'+tab[3]).innerHTML = arrive +' : ' + tab[2] +'/'+ tab[1] + '/' + tab[0];
			$('date_calendar2_'+tab[3]).innerHTML = '';
			$('date_calendar2_'+tab[3]).innerHTML = depart +' : ' + tab_cal[2] +'/'+ tab_cal[1] + '/' + tab_cal[0];

		}

		new	Ajax('tab_jour.php', { asynchronous: false, data: param, method: 'post', onComplete: _tableaudate }).request();
		var ch = tab[3];
		param = "date1="+date1+"&date2="+date2+"&ch="+ch;
		new	Ajax('tab_jour_4.php', { asynchronous: false, data: param, method: 'post', onComplete: _ChangeColor }).request();
	}
	else{}

	}
}
function _tableaudate(originalRequest){
	if(document.all){
		var tableau = (typeof(JSON) != 'undefined' ) ? new Array(JSON.parse(originalRequest)) : new Array(eval('('+originalRequest+')'));
	}else{
		var tableau = new Array(typeof(JSON) != 'undefined' ) ? JSON.parse(originalRequest) : eval('('+originalRequest+')');
	}
	if(document.all){
		for(a=1;a<tableau[0].length;a++){
		if(document.getElementById('1-'+tableau[0][a]+'-'+tableau[0][0]).style.color == "red"){
			document.getElementById('bouton_'+tableau[0][0]).style.display = "none";
			document.getElementById('bouton2_'+tableau[0][0]).style.display = "block";
		}
		else{}
		}
	}
	else{
		for(a=1;a<tableau.length;a++){
		if(document.getElementById('1-'+tableau[a]+'-'+tableau[0]).style.color == "red"){
			document.getElementById('bouton_'+tableau[0]).style.display = "none";
			document.getElementById('bouton2_'+tableau[0]).style.display = "block";
		}
		else{}
	}
	}
}
function _ChangeColor(originalRequest){
	if(document.all){
		var tableau = (typeof(JSON) != 'undefined' ) ? new Array(JSON.parse(originalRequest)) : new Array(eval('('+originalRequest+')'));
	}else{
		var tableau = new Array(typeof(JSON) != 'undefined' ) ? JSON.parse(originalRequest) : eval('('+originalRequest+')');
	}
	if(document.all){
		var ch = tableau[0][0];
		for(a=1;a<tableau[0].length;a++){
		document.getElementById(tableau[0][a]+'-'+ch).style.backgroundColor = "rosybrown";
		}
		document.forms['form'+ch].submit();
	}
	else{
		var ch = tableau[0];
		for(a=1;a<tableau.length;a++){
		document.getElementById(tableau[a]+'-'+ch).style.backgroundColor = "rosybrown";
		}
		document.forms['form'+ch].submit();
	}
}
function _ClearCalendar(originalRequest){
	if(document.all){
		var tableau = (typeof(JSON) != 'undefined' ) ? new Array(JSON.parse(originalRequest)) : new Array(eval('('+originalRequest+')'));
	}else{
		var tableau = new Array(typeof(JSON) != 'undefined' ) ? JSON.parse(originalRequest) : eval('('+originalRequest+')');
	}
	if(document.all){
		var ch = tableau[0][0];
		var couleur = "#DEDEDE";
		for(a=1;a<tableau[0].length;a++){
		var id_date = document.getElementById(''+tableau[0][a]+'-'+ch+'');
		id_date.style.backgroundColor = couleur;
		}
		document.getElementById('bouton_'+tableau[0][0]).style.display = "block";
		document.getElementById('bouton2_'+tableau[0][0]).style.display = "none";
		
	}
	else{
		var ch = tableau[0];
		var couleur = "#DEDEDE";
	
		for(a=1;a<tableau.length;a++){
		var id_date = document.getElementById(''+tableau[a]+'-'+ch+'');
		id_date.style.backgroundColor = couleur;
		}
		document.getElementById('bouton_'+tableau[0]).style.display = "block";
		document.getElementById('bouton2_'+tableau[0]).style.display = "none";
		
	}
	
}

