
function setDate() {
    this.dateField   = opener.dateField;  
    this.inDate      = dateField.value;
    
   
    // SET DAY MONTH AND YEAR TO TODAY'S DATE
    var now   = new Date();
    var day   = now.getDate();
    var month = now.getMonth();
    var year  = now.getYear();

	var dateFinal = this.inDate;
 
 
 	// SI LA DATE DE DEBUT EST RENSEIGNEE, ET PAS LA DATE DE FIN, METTRE POUR LA DATE DE FIN = DATE DE DEBUT
    if(opener.dateDebutField != null 
    	&& opener.dateDebutField.value.trim() != ""
    	&& dateFinal.trim() == ""){
     	 	dateFinal = opener.dateDebutField.value;
    }
 
    // IF A DATE WAS PASSED IN THEN PARSE THAT DATE
    if (dateFinal.trim() != '' && dateFinal.indexOf('/')) {
   
         var inDay   = dateFinal.substring(0,dateFinal.indexOf("/"));
            if (inDay.substring(0,1) == "0" && inDay.length > 1)
                inDay = inDay.substring(1,inDay.length);
            inDay = parseInt(inDay);
         var inMonth = dateFinal.substring(dateFinal.indexOf("/") + 1, dateFinal.lastIndexOf("/"));
            if (inMonth.substring(0,1) == "0" && inMonth.length > 1)
                inMonth = inMonth.substring(1,inMonth.length);
            inMonth = parseInt(inMonth);
	     var inYear  = parseInt(dateFinal.substring(dateFinal.lastIndexOf("/") + 1, dateFinal.length));

        if (inDay) {
            day = inDay;
        }
        if (inMonth) {
            month = inMonth-1;
        }
        if (inYear) {
            year = inYear;
        }
    }
             
    this.focusDay                           = day;
    document.calControl.month.selectedIndex = month;
    document.calControl.year.value          = year;
    displayCalendar(day, month, year);
}




function setToday() {
    // SET DAY MONTH AND YEAR TO TODAY'S DATE
    var now   = new Date();
    var day   = now.getDate();
    var month = now.getMonth();
    var year  = now.getYear();
    this.focusDay                           = day;
    document.calControl.month.selectedIndex = month;
    document.calControl.year.value          = year;
    displayCalendar(day, month, year);
}




function isFourDigitYear(year) {
    if (year.length != 4) {
        alert ("D&eacute;sol&eacute;, l'ann&eacute;e doit &ecirc;tre indiqu&eacute;e sous forme de 4 chiffres.");
        document.calControl.year.select();
        document.calControl.year.focus();
    }
    else {
        return true;
    }
}




function selectDate() {
    var year  = document.calControl.year.value;
    if (isFourDigitYear(year)) {
        var day   = 0;
        var month = document.calControl.month.selectedIndex;
        displayCalendar(day, month, year);
    }
}




function setPreviousYear() {
    var year  = document.calControl.year.value;
    if (isFourDigitYear(year)) {
        var day   = 0;
        var month = document.calControl.month.selectedIndex;
        year--;
        document.calControl.year.value = year;
        displayCalendar(day, month, year);
    }
}




function setPreviousMonth() {
    var year  = document.calControl.year.value;
    if (isFourDigitYear(year)) {
        var day   = 0;
        var month = document.calControl.month.selectedIndex;
        if (month == 0) {
            month = 11;
            if (year > 1000) {
                year--;
                document.calControl.year.value = year;
            }
        }
        else {
            month--;
        }
        document.calControl.month.selectedIndex = month;
        displayCalendar(day, month, year);
    }
}




function setNextMonth() {
    var year  = document.calControl.year.value;
    if (isFourDigitYear(year)) {
        var day   = 0;
        var month = document.calControl.month.selectedIndex;
        if (month == 11) {
            month = 0;
            year++;
            document.calControl.year.value = year;
        }
        else {
            month++;
        }
        document.calControl.month.selectedIndex = month;
        displayCalendar(day, month, year);
    }
}




function setNextYear() {
    var year  = document.calControl.year.value;
    if (isFourDigitYear(year)) {
        var day   = 0;
        var month = document.calControl.month.selectedIndex;
        year++;
        document.calControl.year.value = year;
        displayCalendar(day, month, year);
    }
}




function displayCalendar(day, month, year) {       

    day     = parseInt(day);
    month   = parseInt(month);
    year    = parseInt(year);
    var i   = 0;
    var now = new Date();

    if (day == 0) {
        var nowDay = now.getDate();
    }
    else {
        var nowDay = day;
    }
    var days         = getDaysInMonth(month+1,year);
    var firstOfMonth = new Date (year, month, 1);
    var startingPos  = firstOfMonth.getDay();
    days += startingPos;

    // MAKE BEGINNING NON-DATE BUTTONS BLANK
    for (i = 0; i < startingPos; i++) {
        document.calButtons.elements[i].value = "   ";
    }

    // SET VALUES FOR DAYS OF THE MONTH
    for (i = startingPos; i < days; i++)  
    {
        document.calButtons.elements[i].value = i-startingPos+1;
        document.calButtons.elements[i].onClick = "returnDate"
    }

    // MAKE REMAINING NON-DATE BUTTONS BLANK
    for (i=days; i<42; i++)  {
        document.calButtons.elements[i].value = "   ";
    }

    // GIVE FOCUS TO CORRECT DAY
    document.calButtons.elements[focusDay+startingPos-1].focus();
}


// GET NUMBER OF DAYS IN MONTH
function getDaysInMonth(month,year)  {
    var days;
    if (month==1 || month==3 || month==5 || month==7 || month==8 ||
        month==10 || month==12)  days=31;
    else if (month==4 || month==6 || month==9 || month==11) days=30;
    else if (month==2)  {
        if (isLeapYear(year)) {
            days=29;
        }
        else {
            days=28;
        }
    }
    return (days);
}




// CHECK TO SEE IF YEAR IS A LEAP YEAR
function isLeapYear (Year) {
    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
        return (true);
    }
    else {
        return (false);
    }
}




// SET FORM FIELD VALUE TO THE DATE SELECTED
function returnDate(inDay)
{
    var day   = inDay;
    var month = (document.calControl.month.selectedIndex)+1;
    var year  = document.calControl.year.value;

    if ((""+month).length == 1)
    {
        month="0"+month;
    }
    if ((""+day).length == 1)
    {
        day="0"+day;
    }
    if (day != "   ") {
        dateField.value = day + "/" +month  + "/" + year;
        window.close()
    }
}


//----------------- TRAITEMENT DE L'HEURE -----------------

function setDateHeure() {
    this.dateField   = opener.dateField;
    this.heureField  = opener.heureField;
    
    this.inDate      = dateField.value;
    this.inTime      = heureField.value;

    // SET DAY MONTH AND YEAR TO TODAY'S DATE
    var now   = new Date();
    var day   = now.getDate();
    var month = now.getMonth();
    var year  = now.getYear();

	var dateFinal = this.inDate;
	var heureFinal = this.inTime;
	

	// SI LA DATE DE DEBUT EST RENSEIGNEE, ET PAS LA DATE DE FIN, METTRE POUR LA DATE DE FIN = DATE DE DEBUT
    if(opener.dateDebutField != null 
    	&& opener.dateDebutField.value.trim() != ""
    	&& dateFinal.trim() == ""){
     	 	dateFinal = opener.dateDebutField.value;
    }
    
    
    // IF A DATE WAS PASSED IN THEN PARSE THAT DATE
    if (dateFinal.trim() != '' && dateFinal.indexOf('/')) {
    	 var inDay   = dateFinal.substring(0,dateFinal.indexOf("/"));
            if (inDay.substring(0,1) == "0" && inDay.length > 1)
                inDay = inDay.substring(1,inDay.length);
            inDay = parseInt(inDay);
         var inMonth = dateFinal.substring(dateFinal.indexOf("/") + 1, dateFinal.lastIndexOf("/"));
            if (inMonth.substring(0,1) == "0" && inMonth.length > 1)
                inMonth = inMonth.substring(1,inMonth.length);
            inMonth = parseInt(inMonth);
	     var inYear  = parseInt(dateFinal.substring(dateFinal.lastIndexOf("/") + 1, dateFinal.length));

        if (inDay) {
            day = inDay;
        }
        if (inMonth) {
            month = inMonth-1;
        }
        if (inYear) {
            year = inYear;
        }
    }
    this.focusDay                           = day;
    document.calControl.month.selectedIndex = month;
    document.calControl.year.value          = year;
    
    
    // TRAITEMENT DE L'HEURE / MINUTES
    var heure   = setHeureToday();
    var minutes = setMinutesToday();
    
     // SI L'HEURE DE DEBUT EST RENSEIGNEE, ET PAS L'HEURE DE FIN, METTRE POUR L'HEURE DE FIN = HEURE DE DEBUT
    if(opener.heureDebutField != null 
    	&& opener.heureDebutField.value.trim() != ""
    	&& heureFinal.trim() == ""){
     	 	heureFinal = opener.heureDebutField.value;
    }
    
    
    // IF L'HEURE EST SAISIE : PARSE 
    if (heureFinal.trim() != '' &&  heureFinal.indexOf(':')) {
        var inHeure   = heureFinal.substring(0, heureFinal.indexOf(":"));
        if (inHeure.substring(0,1) == "0" && inHeure.length > 1)
            inHeure = inHeure.substring(1,inHeure.length);
        inHeure = parseInt(inHeure);
        
        var inMinutes  = heureFinal.substring(heureFinal.lastIndexOf(":") + 1, heureFinal.length);
  		if (inMinutes.substring(0,1) == "0" && inMinutes.length > 1)
                inMinutes = inMinutes.substring(1,inMinutes.length);
            inMinutes = parseInt(inMinutes);
            
        if (inHeure < 24) {
            heure = inHeure;
        }
        if (inMinutes < 60) {
            minutes = inMinutes;
        }
    }
 
    document.calControl.heure.value 		= heure;
    document.calControl.minutes.value       = minutes;
           
    displayCalendar(day, month, year, heure, minutes);
}



// Récupère l'heure d'aujourd'hui
function setHeureToday()
{	
	var now   = new Date();
    var heure;  
   			
 	var heureInt = now.getHours();
	if (heureInt < 10)
		heure = "0" + parseInt(heureInt,10);
	else	
		heure = heureInt;
			
	return heure;	
}			
	
// Récupère les minutes d'aujourd'hui
function setMinutesToday()
{
	var now   = new Date();
    var minutes;
	
	var minuteInt = now.getMinutes();
	if (minuteInt < 10)
		minutes = "0" + now.getMinutes();
	else
		minutes = minuteInt;
		
	return minutes;	
}	

// Récupère les secondes d'aujourd'hui
function setSecondes()
{
	var now   = new Date();
    var secondes;
			
	var secondInt = now.getSeconds();	
	if (secondInt < 10)
		secondes = "0" + secondInt;
	else		
		secondes = secondInt;
	 
	return secondes;	
}



function setTodayHeure() {
    // SET DAY MONTH AND YEAR TO TODAY'S DATE
    var now   = new Date();
    var day   = now.getDate();
    var month = now.getMonth();
    var year  = now.getYear();
    this.focusDay                           = day;
    document.calControl.month.selectedIndex = month;
    document.calControl.year.value          = year;
    
    document.calControl.heure.value 		= heure;
   	document.calControl.minutes.value       = minutes;
   		
    displayCalendar(day, month, year, heure, minutes);
}



function setPreviousYearHeure() {
    var year  = document.calControl.year.value;
    if (isFourDigitYear(year)) {
        var day   = 0;
        var month = document.calControl.month.selectedIndex;
        year--;
        document.calControl.year.value = year;
        
        document.calControl.heure.value 		= heure;
   		document.calControl.minutes.value       = minutes;
        
        displayCalendar(day, month, year, heure, minutes);
    }
}




function setPreviousMonthHeure() {
    var year  = document.calControl.year.value;
    if (isFourDigitYear(year)) {
        var day   = 0;
        var month = document.calControl.month.selectedIndex;
        if (month == 0) {
            month = 11;
            if (year > 1000) {
                year--;
                document.calControl.year.value = year;
            }
        }
        else {
            month--;
        }
        document.calControl.month.selectedIndex = month;
        
        document.calControl.heure.value 		= heure;
    	document.calControl.minutes.value       = minutes;
    
        displayCalendar(day, month, year, heure, minutes);
    }
}




function setNextMonthHeure() {
    var year  = document.calControl.year.value;
    if (isFourDigitYear(year)) {
        var day   = 0;
        var month = document.calControl.month.selectedIndex;
        if (month == 11) {
            month = 0;
            year++;
            document.calControl.year.value = year;
        }
        else {
            month++;
        }
        document.calControl.month.selectedIndex = month;
        
        document.calControl.heure.value 		= heure;
    	document.calControl.minutes.value       = minutes;
    
        displayCalendar(day, month, year, heure, minutes);
    }
}




function setNextYearHeure() {
    var year  = document.calControl.year.value;
    if (isFourDigitYear(year)) {
        var day   = 0;
        var month = document.calControl.month.selectedIndex;
        year++;
        document.calControl.year.value = year;
        
        document.calControl.heure.value 		= heure;
    	document.calControl.minutes.value       = minutes;
    
        displayCalendar(day, month, year, heure, minutes);
    }
}


function displayCalendar(day, month, year, heure, minutes) {       

    day     = parseInt(day);
    month   = parseInt(month);
    year    = parseInt(year);
    var i   = 0;
    var now = new Date();

    if (day == 0) {
        var nowDay = now.getDate();
    }
    else {
        var nowDay = day;
    }
    var days         = getDaysInMonth(month+1,year);
    var firstOfMonth = new Date (year, month, 1);
    var startingPos  = firstOfMonth.getDay();
    days += startingPos;

    // MAKE BEGINNING NON-DATE BUTTONS BLANK
    for (i = 0; i < startingPos; i++) {
        document.calButtons.elements[i].value = "   ";
    }

    // SET VALUES FOR DAYS OF THE MONTH
    for (i = startingPos; i < days; i++)  
    {
        document.calButtons.elements[i].value = i-startingPos+1;
        document.calButtons.elements[i].onClick = "returnDate"
    }

    // MAKE REMAINING NON-DATE BUTTONS BLANK
    for (i=days; i<42; i++)  {
        document.calButtons.elements[i].value = "   ";
    }

    // GIVE FOCUS TO CORRECT DAY
    document.calButtons.elements[focusDay+startingPos-1].focus();
    
    // Affiche les heures / minutes
    document.calControl.heure.value = heure;
    document.calControl.minutes.value = minutes; 
}
				

// SET FORM FIELD VALUE TO THE DATE SELECTED
function returnDateHeure(inDay)
{
    var day     = inDay;
    var month   = (document.calControl.month.selectedIndex)+1;
    var year    = document.calControl.year.value;
    var heure   = document.calControl.heure.value;
    var minutes = document.calControl.minutes.value;

	// traitement heures / minutes
    if (("" + heure).length == 1)
   		 {heure = "0" + heure;}
    //si l'heure > 23
   if (heure > 23)
    	 {heure = "00";}
   
    if (("" + minutes).length == 1)
    	{minutes = "0" + minutes;}
     //si les minutes > 59
   if (minutes > 59)
    	 {minutes = "00";}	
    	
    heureField.value = heure + ":" + minutes;
   
    if ((""+month).length == 1)
    	{month="0"+month;}
    if ((""+day).length == 1)
    	{day="0"+day;}
    if (day != "   ") {
        dateField.value = day + "/" +month  + "/" + year;
        
        window.close()
    }
    
    
}

// fin new




// -->
