function verif_nb_tot(ch)//ok
{
	var nb_max=eval('nb_max_'+ch);
	var tot_tmp=1;
	for(var i=0;i<lescategories.length;i++)
	{
		if(eval('cat_mere_'+lescategories[i])!=3)
			tot_tmp+=parseInt(document.getElementById('ch_'+ch+'_cat_'+lescategories[i]).value);
	}
	if(tot_tmp <= nb_max) return true; else return false;
}
function setChambre(id_ch)
{//alert('setChambre');
	var montant_tot_tmp=0;
	var marge_tot_tmp=0;
	var request=0;
	var promo="";
	var erreur=0;
	var tabtmp; var tabtmp2; var ind; var tmp; var tmp2;
	
	for(var i=0; i<lescategories.length; i++)
	{
		montant_tot_tmp+=parseInt(eval("montant_ch_"+id_ch+"_cat_"+lescategories[i]));
		marge_tot_tmp+=parseInt(eval("marge_ch_"+id_ch+"_cat_"+lescategories[i]));
		if(eval("request_ch_"+id_ch+"_cat_"+lescategories[i])==1) request=1;
		if(eval("erreur_ch_"+id_ch+"_cat_"+lescategories[i])==1) {erreur=1;}
		
		if(eval("promo_ch_"+id_ch+"_cat_"+lescategories[i])!="")
		{
			tabtmp=eval("promo_ch_"+id_ch+"_cat_"+lescategories[i]);
			tabtmp=tabtmp.split('|');
			for(ind=0;ind<tabtmp.length;ind++)
			{
				tmp=tabtmp[ind];
				if(tmp!="")
				{
					tabtmp2=tmp.split('!');
					tmp2=tabtmp2[0]+"!"+tabtmp2[1]+"!";
					if(promo.indexOf(tmp,0)==-1)
						promo+=tmp+"|";
				}
			}		
		}
	}
	
	//var nb_ch=parseInt(document.getElementById("quantite_ch_"+id_ch).value);
	document.getElementById("total_ch_"+id_ch).value=montant_tot_tmp;
	document.getElementById("marge_ch_"+id_ch).value=marge_tot_tmp;
	document.getElementById("request_ch_"+id_ch).value=request;
	document.getElementById("promo_ch_"+id_ch).value=promo;
	//alert('total:'+montant_tot_tmp+' marge:'+marge_tot_tmp+' request:'+request+' promo:'+promo);
	
	if(erreur==0) {document.getElementById('check_ch_'+id_ch).disabled=false;}
	else {document.getElementById('check_ch_'+id_ch).disabled=true;}
}

function getTarifCat(id_ch, id_cat, dt_debut, dt_fin)
{
	var qte_cat=parseInt(document.getElementById('ch_'+id_ch+'_cat_'+id_cat).value);
	//alert(id_ch+" "+id_cat+" "+qte_cat+" "+dt_debut+" "+dt_fin+" "+plus);
	
	if(qte_cat>0)
	{
		var xhr_object = null;
		
		if(window.XMLHttpRequest) // Firefox
		  xhr_object = new XMLHttpRequest();
		else if(window.ActiveXObject) // Internet Explorer
		  xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
		else { // XMLHttpRequest non supporté par le navigateur
		  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		  return;
		}
			
		xhr_object.open("POST", "ajax_hebergement.php", true);
		
		xhr_object.onreadystatechange = function() {
		  if(xhr_object.readyState == 4)
		  {
		  	eval(xhr_object.responseText);
		  	
		  	if(pb==0)
		  	{
			  	eval("montant_ch_"+id_ch+"_cat_"+id_cat+"="+new_prix);
					eval("marge_ch_"+id_ch+"_cat_"+id_cat+"="+new_marge);
					eval("request_ch_"+id_ch+"_cat_"+id_cat+"="+new_request);
					eval("promo_ch_"+id_ch+"_cat_"+id_cat+"='"+new_promo+"'");
					eval("erreur_ch_"+id_ch+"_cat_"+id_cat+"=0");
				}
				else
				{
					if(pb==-3) document.getElementById("mess_ch_"+id_ch).innerHTML+="les dates de séjour correspondent à 0 nuit!";
					if(pb==-2) document.getElementById("mess_ch_"+id_ch).innerHTML+="cette chambre n'est pas dispo pour les dates saisies";
					if(pb==-1) document.getElementById("mess_ch_"+id_ch).innerHTML+="cette chambre n'est pas disponible pour "+qte_cat+" "+eval("nom_cat_"+id_cat)+"(s)";
					eval("montant_ch_"+id_ch+"_cat_"+id_cat+"=0");
					eval("marge_ch_"+id_ch+"_cat_"+id_cat+"=0");
					eval("request_ch_"+id_ch+"_cat_"+id_cat+"=0");
					eval("promo_ch_"+id_ch+"_cat_"+id_cat+"=''");
					eval("erreur_ch_"+id_ch+"_cat_"+id_cat+"=1");
				}
				setChambre(id_ch);
		  }
		}
		
		var qte_ch=parseInt(document.getElementById("quantite_ch_"+id_ch).value);
	
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		var data = "action=calculTarif&chambre="+id_ch+"&nb_ch="+qte_ch+"&id_cat="+id_cat+"&nb_pers="+qte_cat+"&date_debut="+dt_debut+"&date_fin="+dt_fin;
		xhr_object.send(data);//alert (data);
	}
	else
	{
		//soustraire le mt de cette cat
		eval("montant_ch_"+id_ch+"_cat_"+id_cat+"=0");
		eval("marge_ch_"+id_ch+"_cat_"+id_cat+"=0");
		eval("request_ch_"+id_ch+"_cat_"+id_cat+"=0");
		eval("promo_ch_"+id_ch+"_cat_"+id_cat+"=''");
		eval("erreur_ch_"+id_ch+"_cat_"+id_cat+"=0");
		setChambre(id_ch);
	}
}

function reqTarif(id_ch, id_cat, dt_debut, dt_fin)
{
	document.getElementById("mess_ch_"+id_ch).innerHTML="";
	if(id_cat!=0)
	{
		getTarifCat(id_ch, id_cat, dt_debut, dt_fin);
	}
	else
	{
		for(var i=0; i<lescategories.length; i++)
		{
			if(!getTarifCat(id_ch, lescategories[i], dt_debut, dt_fin)) break;
		}
	}
}
function modQteCat(plus, id_ch, id_cat, dt_debut, dt_fin)
{
	var champs=document.getElementById('ch_'+id_ch+'_cat_'+id_cat);
	
	if(plus==1 && eval('cat_mere_'+id_cat)!=3 && !verif_nb_tot(id_ch))
	{
		document.getElementById("mess_ch_"+id_ch).innerHTML="Le maximum de personnes pour cette chambre a été atteint. Les bébés n'étant pas pris en compte.";
		return;
	}
	if(plus==-1 && parseInt(champs.value)==0) return;
	champs.value=parseInt(champs.value)+plus;
	reqTarif(id_ch, id_cat, dt_debut, dt_fin);
}

function modQteCh(plus, id_ch, dt_debut, dt_fin)
{
	var ob=document.getElementById("quantite_ch_"+id_ch);
	var qte=parseInt(ob.value);
	if(plus==-1 && qte==1) return;
	ob.value=qte+plus;
	qte=parseInt(ob.value);
	if(qte==0)
	{
		document.getElementById("total_ch_"+id_ch).innerHTML="<a class='cl_texteorange'><b>0 €</b></a>";
		return;
	}
	
	reqTarif(id_ch, 0, dt_debut, dt_fin);
}
