var tbLibMois = new Array("","Janvier", "F&eacute;vrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "D&eacute;cembre");
var tbLibMoisCrt = new Array("","Janv", "F&eacute;v", "Mars", "Avr", "Mai", "Juin", "Jui", "Ao&ucirc;t", "Sep", "Oct", "Nov", "D&eacute;c");
var tbLibJour = new Array("Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi");
function jours(selectElem) 
{
	selectElem = $(selectElem);
	for (var i=1; i <= 31; i++) {
		var jOpt = new Element("option");//document.createElement("option");
		jOpt.text = i;
		jOpt.value = (i < 10 ? ("0" + i) : i);
		if (selectElem && jOpt) jOpt.injectInside(selectElem);
	}
}

function mois(mm) 
{
	var ch = "<option value='01'>Janvier</option><option value='02'>F&eacute;vrier</option><option value='03'>Mars</option><option value='04'>Avril</option><option value='05'>Mai</option><option value='06'>Juin</option><option value='07'>Juillet</option><option value='08'>Aout</option><option value='09'>Septembre</option><option value='10'>Octobre</option><option value='11'>Novembre</option><option value='12'>D&eacute;cembre</option>";
//	var ch = replaceSubstring(ch, "value='" + mm + "'", "value='" + mm + "' selected");
	document.writeln(ch);
}

function annees(aa) 
{	var ch = "<option value='06'>2006</option><option value='07'>2007</option>";
//	var ch = replaceSubstring(ch, "value='" + aa + "'", "value='" + aa + "' selected");
	document.writeln(ch);
}

function moisannees(libCrt){
	var ch="";
	theDate=new Date();
	theMonth=theDate.getMonth()+1;
	theYear=theDate.getFullYear();
	for (i=1;i<13 ;i++ )
	{
			theMois=theMonth;
			if(theMonth < 10) theMois="0"+theMonth;
			ch+="<option value='" + theMois + "!" + new String(theYear).substring(2,4) + "'>" + getLibMois(theMonth,libCrt) + " " + (libCrt ? new String(theYear).substring(2,4) : theYear) + "</option>";
			theMonth++;
			if(theMonth==13){
				theMonth=1;
				theYear++;
			}
	}
	document.writeln(ch);
}


function getLibMois(mm,libCrt)
{
		if(libCrt)
			return tbLibMoisCrt[mm];
		else
			return tbLibMois[mm];
}

function getLibJour(uneDate)
{
	return tbLibJour[uneDate.getDay()];
}

function selListe(theListe,valeur)
{
	if(theListe)
	{
		for(i=0;i<theListe.length;i++)
		{
			if(theListe.options[i].value==valeur)
				theListe.options[i].selected=true;
			else
				theListe.options[i].selected=false;
		}
	}
}

function Chargement(formu,page)
{
	/* Mise a jour des dates */
	theDate=new Date();
	theForm=document.forms[formu];

	theChaine=theDate.getDate();
	selListe(theForm.departj,theChaine);

	if((theDate.getMonth()+1)<10){
		theChaine="0"+(theDate.getMonth()+1)+"!"+new String(theDate.getFullYear()).substring(2,4);
	}else{
		theChaine=(theDate.getMonth()+1)+"!"+new String(theDate.getFullYear()).substring(2,4);
	}
	selListe(theForm.departma,theChaine);

	theChaine=theDate.getDate();
	selListe(theForm.retourj,theChaine);

	if((theDate.getMonth()+1)<10){
		theChaine="0"+(theDate.getMonth()+1)+"!"+new String(theDate.getFullYear()).substring(2,4);
	}else{
		theChaine=(theDate.getMonth()+1)+"!"+new String(theDate.getFullYear()).substring(2,4);
	}
	selListe(theForm.retourma,theChaine);

	datesVoyages(formu,page);
}


function datesVoyages(formu,page){

	Frm=document.forms[formu];
	tbMoisAnnee=Frm.departma.options[Frm.departma.options.selectedIndex].value.split("!");
	Frm.departm.value=tbMoisAnnee[0];
	Frm.departa.value=tbMoisAnnee[1];
	tbMoisAnnee=Frm.retourma.options[Frm.retourma.options.selectedIndex].value.split("!");
	Frm.retourm.value=tbMoisAnnee[0];
	Frm.retoura.value=tbMoisAnnee[1];
	//alert(tbMoisAnnee+"/"+tbMoisAnnee);

	document.getElementById("jour1").innerHTML =getLibJour(new Date('20' + Frm.departa.value,Frm.departm.value-1,Frm.departj.value));
	document.getElementById("jour2").innerHTML =getLibJour(new Date('20' + Frm.retoura.value,Frm.retourm.value-1,Frm.retourj.value));
	if(page==1){
	document.getElementById("jour12").innerHTML =getLibJour(new Date('20' + Frm.departa.value,Frm.departm.value-1,Frm.departj.value));
	document.getElementById("jour22").innerHTML =getLibJour(new Date('20' + Frm.retoura.value,Frm.retourm.value-1,Frm.retourj.value));
	document.getElementById("jour13").innerHTML =getLibJour(new Date('20' + Frm.departa.value,Frm.departm.value-1,Frm.departj.value));
	document.getElementById("jour23").innerHTML =getLibJour(new Date('20' + Frm.retoura.value,Frm.retourm.value-1,Frm.retourj.value));
	//document.getElementById("jour14").innerHTML =getLibJour(new Date('20' + Frm.departa.value,Frm.departm.value-1,Frm.departj.value));
	//document.getElementById("jour24").innerHTML =getLibJour(new Date('20' + Frm.retoura.value,Frm.retourm.value-1,Frm.retourj.value));
	}
	calculj2(Frm.departa,Frm.departm,Frm.departj,'depart');
	calculj2(Frm.retoura,Frm.retourm,Frm.retourj,'retour');
}

function changedateretour(formu,page){

	Frm=document.forms[formu];

	tbMoisAnneed=Frm.departma.options.selectedIndex;
	tbMoisAnneer=Frm.retourma.options.selectedIndex;
	tbjourd=Frm.departj.options.selectedIndex;
	tbjourr=Frm.retourj.options.selectedIndex;

	if(tbMoisAnneed == tbMoisAnneer){
		if(tbjourd > tbjourr){
		Frm.retourj.options.selectedIndex = tbjourd;
		}
	}

	if(tbMoisAnneed > tbMoisAnneer){
	Frm.retourma.options.selectedIndex = tbMoisAnneed;
		if(tbjourd > tbjourr){
		Frm.retourj.options.selectedIndex = tbjourd;
		}
	}
	datesVoyages(formu,page);
}

function calculj2(sSelectYear,sSelectMonth,sSelectDay,type)
{
	function AddElement(num)
	{
		nouvel_element = new Option(num,num,false,false);
		sSelectDay.options[num] = nouvel_element;
		sSelectDay.options[num].value = num;
	}
	
	var td = new Date();
	var ms = td.getMonth();
	
    var nMonthValue = "20" + sSelectYear.value + sSelectMonth.value;
    var sYear = nMonthValue.toString().substr(0,4);
    var sMonth = nMonthValue.toString().substr(4,6);
    var nDays = 31;
    switch (sMonth)
	{
        case "04":
        case "06":
        case "09":
        case "11":
			if(sSelectDay.options[28] == null)
			{
				AddElement('28');
			}
			if(sSelectDay.options[29] == null)
			{
				AddElement('29');
			}
            nDays = 30;
			sSelectDay.options[30] = null;
        break;
	    case "02":
            nDays = (sYear%4 == 0 ? 29: 28);
			for (var less = 30; less >= nDays; less --)
			{
				sSelectDay.options[less] = null;
			}
	    break;
		default:
			if(sSelectDay.options[28] == null)
			{
				AddElement('28');
			}
			if(sSelectDay.options[29] == null)
			{
				AddElement('29');
			}
			if(sSelectDay.options[30] == null)
			{
				AddElement('30');
			}
		break;
    }	
    for (var count=0; count<nDays; count++)
	{
		// LZ : respecter le format du jour sur deux chiffres obligatoirement
        sSelectDay.options[count].value = LZ(count + 1);
        sSelectDay.options[count].text = count + 1;  
    }
	return;
}

// home : page=1
function MAJDates2(sSelectMonth,sSelectDay,type,formu,formu2,formu3)
{
	sDayIndex = sSelectDay.selectedIndex;
	sMonthIndex = sSelectMonth.selectedIndex;
	document.forms[formu].elements[type+'j'].options[sDayIndex].selected = true;
	document.forms[formu].elements[type+'ma'].options[sMonthIndex].selected = true;
	document.forms[formu2].elements[type+'j'].options[sDayIndex].selected = true;
	document.forms[formu2].elements[type+'ma'].options[sMonthIndex].selected = true;
	//document.forms[formu3].elements[type+'j'].options[sDayIndex].selected = true;
	//document.forms[formu3].elements[type+'ma'].options[sMonthIndex].selected = true;
	changedateretour(formu,'1');
	changedateretour(formu2,'1');
	//changedateretour(formu3,'1');
}

// pas home : page=0
function recupchargement(formu,departj,departma,retourj,retourma)
{
	if(departj == ''){
	Chargement(formu,'0');}
	else {
	theForm=document.forms[formu];
	selListe(theForm.departj,departj);
	selListe(theForm.departma,departma);
	selListe(theForm.retourj,retourj);
	selListe(theForm.retourma,retourma);
	datesVoyages(formu,'0');
	}
}

