function initdate1(j,m,y){
	m=m+1;
	jo=j;
	mo=m;
	j=""+j;
	m=""+m;
	y=""+y;
	if(j.length==1){j="0"+j;}
	if(m.length==1){m="0"+m;}	
	document.forms.resa.datededebut.value = j+"/"+m+'/'+y;
	document.forms.resa.datedefin.value = "";
	document.forms.resa.nbnuit.value = "";
	document.forms.resa.nbnuit2.value = "";
	document.forms.resa.mo2.value = m;
	document.forms.resa.an2.value = y;
	document.getElementById('moncalque').innerHTML=affiche_date(mo,y,jo);
	document.getElementById('moncalques').innerHTML=affiche_date2(mo,y,jo);
	document.getElementById('dateentiere1').innerHTML=affdate(mo,y,jo);
	document.getElementById('dateentiere2').innerHTML="";
}

function affiche_date(m,y,d){
	var date=new Date();
	var nomjour=new Array("Di","Lu","Ma","Me","Je","Ve","Sa");
	var nbjours = Array (31,0,31,30,31,30,31,31,30,31,30,31);
	date.setDate(1);
	date.setMonth(m-1);
	date.setYear(y);
	var mois=parseInt(date.getMonth())+1;
	// test bissecstile & seculaire
	var motan=date.getFullYear();
	var choix="";
	if (motan-(parseInt(motan/4)*4)==0){choix="bi";}
	if (motan-(parseInt(motan/4)*4)==0&&motan-(parseInt(motan/100)*100)==0) {choix="";} 
	if (motan-(parseInt(motan/4)*4)==0&&motan-(parseInt(motan/400)*400)==0) {choix="bi";}
	if(choix=="bi"){nbjours[1]=29;}
	if(choix!="bi"){nbjours[1]=28;}
	// fin du test bissecstile & seculaire

	//Début calendrier

	var valid="";
	var now=new Date();
	nowan=now.getFullYear();
	nowmois=now.getMonth();
	nowjour=now.getDate();
	var AStyleNomJour = "border-left: #CAC9C8 1px solid;border-top: #CAC9C8 1px solid;border-right: #333333 1px solid;border-bottom: #333333 1px solid;font-size:9pt;text-align:center;color:black;font-family: Arial Verdana;cursor:pointer;padding:1px;z-index:99";
	var AStyleJourOK = "border-left: #CAC9C8 1px solid;border-top: #CAC9C8 1px solid;border-right: #333333 1px solid;border-bottom: #333333 1px solid;background-color:#00FF00;font-size:9pt;text-align:center;color:black;font-family: Arial Verdana;cursor:pointer;padding:1px;z-index:99";
	var AStyleJourKO = "border-left: #CAC9C8 1px solid;border-top: #CAC9C8 1px solid;border-right: #333333 1px solid;border-bottom: #333333 1px solid;background-color:#FF0000;font-size:9pt;text-align:center;color:white;font-family: Arial Verdana;cursor:pointer;padding:1px;z-index:99";
	var AStyleJourSelected = "border-left: #CAC9C8 1px solid;border-top: #CAC9C8 1px solid;border-right: #333333 1px solid;border-bottom: #333333 1px solid;background-color:#7951AE;font-weight: bold;font-size:9pt;text-align:center;color:white;font-family: Arial Verdana;cursor:pointer;padding:1px;z-index:99";
	txt="<table border='1px' width='165px'>";
	txt+="<tr>";
	var datefausse=1;
	for(i=0;i<nomjour.length; i++)
	{
		txt+="<td style='"+AStyleNomJour+"'>"+nomjour[i]+"</td>";
	}
	txt+="</tr>";
	for(i=0;i<6;i++)
	{
		txt+="<tr>";
		for(y=0;y<7;y++)
		{
			txt+="<td";
			if(i==0)
			{
				if(date.getDay()!=y){txt+="  >";}
				else
				{
					//TEST SUR L'ANNEE
					if(nowan>date.getFullYear())
					{
						txt+=" style='"+AStyleJourKO+"'>";
						txt+=date.getDate();
						date.setDate(parseInt(date.getDate())+1);
						datefausse+=1;
					}else if(nowan==date.getFullYear())
					{
						//TEST SUR LE MOIS
						if (nowmois>date.getMonth())
						{
							txt+=" style='"+AStyleJourKO+"'>";
							txt+=date.getDate();
							date.setDate(parseInt(date.getDate())+1);
							datefausse+=1;
						}else if (nowmois==date.getMonth())
						{
							//TEST JOUR
							if(nowjour>date.getDate()){
								txt+=" style='"+AStyleJourKO+"'>";
								txt+=date.getDate();
								date.setDate(parseInt(date.getDate())+1);
								datefausse+=1;
							}else{
								// restriction par logement
								valid=true;
								valid=traitrest(date);
								

								if(valid==false){txt+=" style='"+AStyleJourKO+"'>";}
								else if(date.getDate()==d){
									txt+=" style='"+AStyleJourSelected+
									"' onMouseOver=\"this.style.backgroundcolor='#777777'\" onMouseOut=\"this.style.backgroundcolor='#7951AE'\" onClick='initdate1("+date.getDate()+","+date.getMonth()+","+date.getFullYear()+")'>";
								}
								else{
									txt+=" style='"+AStyleJourOK+
									"' onMouseOver=\"this.style.backgroundcolor='#777777'\" onMouseOut=\"this.style.backgroundcolor='#00FF00'\" onClick='initdate1("+date.getDate()+","+date.getMonth()+","+date.getFullYear()+")'>";
								}
								txt+=date.getDate();
								date.setDate(parseInt(date.getDate())+1);
								datefausse+=1;
							}
						}else{
							// restriction par logement
							valid=true;
							valid=traitrest(date);
							

							
							if(valid==false){txt+=" style='"+AStyleJourKO+"'>";}
							else if(date.getDate()==d){
								txt+=" style='"+AStyleJourSelected+
								"' onMouseOver=\"this.style.backgroundcolor='#777777'\" onMouseOut=\"this.style.backgroundcolor='#7951AE'\" onClick='initdate1("+date.getDate()+","+date.getMonth()+","+date.getFullYear()+")'>";
							}
							else{
									txt+=" style='"+AStyleJourOK+
								"' onMouseOver=\"this.style.backgroundcolor='#777777'\" onMouseOut=\"this.style.backgroundcolor='#00FF00'\" onClick='initdate1("+date.getDate()+","+date.getMonth()+","+date.getFullYear()+")'>";
							}
							txt+=date.getDate();
							date.setDate(parseInt(date.getDate())+1);
							datefausse+=1;
						}
					}else{
					// restriction par logement
					valid=true;
					valid=traitrest(date);
								

					
					if(valid==false){txt+=" style='"+AStyleJourKO+"'>";}
					else if(date.getDate()==d){
						txt+=" style='"+AStyleJourSelected+
						"' onMouseOver=\"this.style.backgroundcolor='#777777'\" onMouseOut=\"this.style.backgroundcolor='#7951AE'\" onClick='initdate1("+date.getDate()+","+date.getMonth()+","+date.getFullYear()+")'>";
					}
					else{
						txt+=" style='"+AStyleJourOK+
						"' onMouseOver=\"this.style.backgroundcolor='#777777'\" onMouseOut=\"this.style.backgroundcolor='#00FF00'\" onClick='initdate1("+date.getDate()+","+date.getMonth()+","+date.getFullYear()+")'>";
					}
					txt+=date.getDate();
					date.setDate(parseInt(date.getDate())+1);
					datefausse+=1;
					}
				}
			}
			else
			{
				if (datefausse<=nbjours[mois-1])
				{
					if(nowan>date.getFullYear())
					{
						txt+=" style='"+AStyleJourKO+"'>";
						txt+=date.getDate();
						date.setDate(parseInt(date.getDate())+1);
						datefausse+=1;
					}else if(nowan==date.getFullYear())
					{
						//TEST SUR LE MOIS
						if (nowmois>date.getMonth())
						{
							txt+=" style='"+AStyleJourKO+"'>";
							txt+=date.getDate();
							date.setDate(parseInt(date.getDate())+1);
							datefausse+=1;
						}else if (nowmois==date.getMonth())
						{
							//TEST JOUR
							if(nowjour>date.getDate()){
								txt+=" style='"+AStyleJourKO+"'>";
								txt+=date.getDate();
								date.setDate(parseInt(date.getDate())+1);
								datefausse+=1;
							}else{
								// restriction par logement
								valid=true;
								valid=traitrest(date);
								

								if(valid==false){txt+=" style='"+AStyleJourKO+"'>";}
								else if(date.getDate()==d){
									txt+=" style='"+AStyleJourSelected+
									"' onMouseOver=\"this.style.backgroundcolor='#777777'\" onMouseOut=\"this.style.backgroundcolor='#7951AE'\" onClick='initdate1("+date.getDate()+","+date.getMonth()+","+date.getFullYear()+")'>";
								}
								else{
									txt+=" style='"+AStyleJourOK+
									"' onMouseOver=\"this.style.backgroundcolor='#777777'\" onMouseOut=\"this.style.backgroundcolor='#00FF00'\" onClick='initdate1("+date.getDate()+","+date.getMonth()+","+date.getFullYear()+")'>"; 
								}  
								txt+=date.getDate();
								date.setDate(parseInt(date.getDate())+1);
								datefausse+=1;
							}
						}else{
							// restriction par logement
							valid=true;
							valid=traitrest(date);
								

							if(valid==false){txt+=" style='"+AStyleJourKO+"'>";}
							else if(date.getDate()==d){
								txt+=" style='"+AStyleJourSelected+
								"' onMouseOver=\"this.style.backgroundcolor='#777777'\" onMouseOut=\"this.style.backgroundcolor='#7951AE'\" onClick='initdate1("+date.getDate()+","+date.getMonth()+","+date.getFullYear()+")'>";
							}
							else{
								txt+=" style='"+AStyleJourOK+
								"' onMouseOver=\"this.style.backgroundcolor='#777777'\" onMouseOut=\"this.style.backgroundcolor='#00FF00'\" onClick='initdate1("+date.getDate()+","+date.getMonth()+","+date.getFullYear()+")'>";
							}
							txt+=date.getDate();
							date.setDate(parseInt(date.getDate())+1);
							datefausse+=1;
						}
					}else{
					// restriction par logement
					valid=true;
					valid=traitrest(date);
								

					if(valid==false){txt+=" style='"+AStyleJourKO+"'>";}
					else if(date.getDate()==d){
						txt+=" style='"+AStyleJourSelected+
						"' onMouseOver=\"this.style.backgroundcolor='#777777'\" onMouseOut=\"this.style.backgroundcolor='#7951AE'\" onClick='initdate1("+date.getDate()+","+date.getMonth()+","+date.getFullYear()+")'>";
					}
					else{
						txt+=" style='"+AStyleJourOK+
						"' onMouseOver=\"this.style.backgroundcolor='#777777'\" onMouseOut=\"this.style.backgroundcolor='#00FF00'\" onClick='initdate1("+date.getDate()+","+date.getMonth()+","+date.getFullYear()+")'>";
					}
					txt+=date.getDate();
					date.setDate(parseInt(date.getDate())+1);
					datefausse+=1;
					}
				}
				else{txt+="  >";}
			}
			txt+="</td>";
		}
		txt+="</tr>";
	}
	txt+="</table>";

	return(txt);
}




function changeforfait(j,m,y){
	m=m+1;
	jo=j;
	mo=m;
	j=""+j;
	m=""+m;
	y=""+y;
	if(j.length==1){j="0"+j;}
	if(m.length==1){m="0"+m;}	
	document.forms.resa.datededebut.value = "";
	document.forms.resa.datedefin.value = "";
	document.forms.resa.nbnuit.value = "";
	document.forms.resa.nbnuit2.value = "";
	document.forms.resa.mo2.value = m;
	document.forms.resa.an2.value = y;
	document.forms.resa.mo.value = m;
	document.forms.resa.an.value = y;
	document.getElementById('moncalque').innerHTML=affiche_date(mo,y,jo);
	document.getElementById('moncalques').innerHTML="";
	document.getElementById('dateentiere1').innerHTML="";
	document.getElementById('dateentiere2').innerHTML="";
}

