/******************************************************************************/
/***********************        calendrier              ***********************/
/******************************************************************************/
/***********************  dernière modif: 29/08/07 YLC  ***********************/
/******************************************************************************/

var date;
var destinationInput;
var currentDiv=null;

var selectedMonth = null;
var selectedYear = null;
//Création de la table contenant le calendrier pour un mois donné
//Le mois affiché est celui de la date en paramètre
//Ou du jour
function calendar(date1){
    //If no parameter is passed use the current date.
    if(date1 == null){
       date = new Date();
    }else{date=date1;}

    day = date.getDate();
    month = date.getMonth();
    year = date.getFullYear();

    months = new Array(
    'Janvier',
    'Février',
    'Mars',
    'Avril',
    'Mai',
    'Juin',
    'Juillet',
    'Aout',
    'Septembre',
    'Octobre',
    'Novembre',
    'Décembre');

    this_month = new Date(year, month, 1);
    next_month = new Date(year, month + 1, 1);

    //Find out when this month starts and ends.
    first_week_day = this_month.getDay()-1;
    if (first_week_day==-1) first_week_day=6
    days_in_this_month = Math.round((next_month.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24));

    calendar_html = '<table>';

    calendar_html += '<tr><td colspan="7" align="center" >'
    calendar_html += '<a href="#" onClick="calendrier_mois_precedent(date);return false;" title="Mois précédent"><img src="modules/gestion/images/fleche_precedente.jpg" border="0" align=left></a>';
    calendar_html += '<a href="#" onClick="calendrier_mois_suivant(date);return false;" title="Mois suivant"><img src="modules/gestion/images/fleche_suivante.jpg" border="0" align=right></a>'+ months[month] + ' ' + year +'</td></tr>';

    calendar_html += '<tr>';
    calendar_html += '<tr class="enteteSemaine"><td>Lun.</td><td>Mar.</td><td>Mer.</td><td>Jeu.</td><td>Ven.</td><td>Sam.</td><td>Dim.</td></tr>';

    //Fill the first week of the month with the appropriate number of blanks.
    for(week_day = 0; week_day < first_week_day; week_day++)
       {
       calendar_html += '<td class="joursVides"></td>';
       }

    week_day = first_week_day;
    for(day_counter = 1; day_counter <= days_in_this_month; day_counter++)
       {
        day_t=day_counter+"";
        if (day_t.length==1) day_t="0"+ day_counter;
        m=month+1
        month_t=(m)+"";
        if (month_t.length==1) month_t="0"+ month_t;
        date_counter=day_t +"/"+ month_t +"/"+ year
       week_day %= 7;

       if(week_day == 0)
          calendar_html += '</tr><tr>';
       //Do something different for the current day.
       if(day == day_counter && month == selectedMonth && year == selectedYear)
          calendar_html += '<td align="center" class="aujourdHui"><a href="" onClick="destinationInput.value=\''+ date_counter +'\';closeCalendrier();return false;">' + day_counter + '</a></td>';
       else
          calendar_html += '<td align="center" class="joursNormaux"><a href="" onClick="destinationInput.value=\''+ date_counter +'\';closeCalendrier();return false;"> ' + day_counter + '</a></td>';

       week_day++;
       }

    calendar_html += '</tr>';
    calendar_html += '</table>';

    //Display the calendar.
    return calendar_html;
}

//Reinitialise le calendrier au mois précédent de la date en paramètre
function calendrier_mois_precedent(ladate){
    if(ladate == null)
       ladate = new Date();

    day = ladate.getDate();
    month = ladate.getMonth();
    year = ladate.getFullYear();

    if (month>0){
        month--;
    }else{
        month=11;
        year--;
    }

    div_calendrier=document.getElementById("tableau_calendrier");
    if (div_calendrier!=null){
        div_calendrier.innerHTML=calendar(new Date(year,month,day));

    }
}

//Reinitialise le calendrier au mois suivant de la date en paramètre
function calendrier_mois_suivant(ladate){
    if(ladate == null)
       ladate = new Date();

    day = ladate.getDate();
    month = ladate.getMonth();
    year = ladate.getFullYear();

    if (month<11){
        month++;
    }else{
        month=0;
        year++;
    }

    div_calendrier=document.getElementById("tableau_calendrier");
    if (div_calendrier!=null){
        div_calendrier.innerHTML=calendar(new Date(year,month,day));
    }
}

//Creation du calendrier
//Recupération de la valeur de l'input d'appel
//Affichage du calendrier
function initCalendrier(objDest){

    date2=null;
    destinationInput=document.getElementById(objDest);
    
    ladate=destinationInput.value;
    if (ladate!="" && ladate.length==10){
        lemoi=ladate.substring(3,5);
        if (lemoi.charAt(0)=="0") lemoi=lemoi.substring(1,2);
        date2=new Date(ladate.substring(6,10),parseInt(lemoi)-1,ladate.substring(0,2));
        selectedMonth = date2.getMonth();
        selectedYear = date2.getFullYear();
    }
    div_calendrier=document.getElementById("tableau_calendrier");

    if (div_calendrier!=null){
        div_calendrier.innerHTML=calendar(date2);
        afficheDiv('calendrier','');
    }
}


//Fermeture du calendrier
function closeCalendrier(){
    afficheDiv('calendrier','none');
}

function afficheDiv(index,display){
    var obj=document.getElementById(index);
    if(obj!=null) {
        if (display==""){
            if (currentDiv!=null) afficheDiv(currentDiv,"none");
            currentDiv=index;
        }else currentDiv=null;
        //obj.style.left=(getXPosition()+10)+"px";
		//obj.style.top=(getYPosition()+10)+"px";
		obj.style.display=display;
        
    }
}

var _x;
var _y;
var isIE = document.all?true:false;
if (!isIE) document.addEventListener('mousemove',changeCoord, false);

function changeCoord(e){
	_x=e.clientX;
	_y=e.clientY;
}

function getXPosition(){
    if (isIE) _x = event.clientX+document.body.scrollLeft;
	return _x+document.documentElement.scrollLeft;
}
function getYPosition(){
    if (isIE) _y = event.clientY+document.body.scrollTop;
	return _y+document.documentElement.scrollTop;
}