//var IMG_URL = "/ffcbourgogne/img2/";
var IMG_URL = "/img2/";

// Retourne le nom du jour correspondant à la date
// asDateValue      : valeur de la date dans le formulaire principal
// asDateFormat     : format de la date : 'D' (date format : JJ/MM/AAAA),
//                                        'T' (datetime : JJ/MM/AAAA HH:MI:SS)
// asLanguageFormat : format de la date : 'FR' (JJ/MM/AAAA)
//                                        'US' (MM/JJ/AAAA)
// asLanguage       : Language des traductions : 'FR'
//                                               'GB'
// abSearch         : Recherche ou non (autorisation des > < =)
function getDayName(asDateValue, asDateFormat, asLanguageFormat, asLanguage, abSearch)
{
   if (asLanguage == "FR")
   {
      days = ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"];
   }
   else
   {
      days = ["Sunday", "Monday", "Tuesday", "Wenesday", "Thursday", "Friday", "Saturday"];
   }
   if (asDateValue == '')
   {
      return '';
   }
   if (eval(abSearch))
   {
      if (asDateValue.substr(0,2) == "<=" || asDateValue.substr(0,2) == ">=" || asDateValue.substr(0,2) == "<>")
      {
         asDateValue = asDateValue.substr(2,asDateValue.length-2);
      }
      else if (asDateValue.substr(0,1) == "<" || asDateValue.substr(0,1) == ">" || asDateValue.substr(0,1) == "=")
      {
         asDateValue = asDateValue.substr(1,asDateValue.length-1);
      }
   }
   if (asLanguageFormat == "US")
   {
      ldDate = new Date(asDateValue.substring(6,10), asDateValue.substring(0,2)-1, asDateValue.substring(3,5));
   }
   else
   {
      ldDate = new Date(asDateValue.substring(6,10), asDateValue.substring(3,5)-1, asDateValue.substring(0,2));
   }
   var lsDayName = '';
   if (ldDate.getDay() >= 0 && ldDate.getDay() <= 6)
   {
      lsDayName = days[ldDate.getDay()];
   }
   return lsDayName;
}


// Copie la date de début dans la date de fin
// asBeginDateField : nom du champ de la date de début dans le formulaire principal
function equalBeginDate(asBeginDateField)
{
   document.principal.elements[asBeginDateField+"End"].value = document.principal.elements[asBeginDateField].value;
   if (document.principal.elements[asBeginDateField+"DayName"])
   {
      document.principal.elements[asBeginDateField+"EndDayName"].value = document.principal.elements[asBeginDateField+"DayName"].value;
   }
}


/*******************************************************************************
                          Partie Affichage du calendrier
*******************************************************************************/


// asDateField      : nom du champ dans le formulaire principal
// asDatevalue      : valeur de la date dans le formulaire principal
// asDateFormat     : format de la date : 'D' (date format : JJ/MM/AAAA),
//                                        'T' (datetime : JJ/MM/AAAA HH:MI:SS)
// asLanguageFormat : format de la date : 'FR' (JJ/MM/AAAA)
//                                        'US' (MM/JJ/AAAA)
// asLanguage       : Language des traductions : 'FR'
//                                               'GB'
// abSearch         : Recherche ou non (autorisation des > < =)
function showCalendar(asDatefield, asDatevalue, asDateFormat, asLanguageFormat, asLanguage, abSearch)
{
   var lsFieldDayName = "";
   var lsValueDayName;
   
   if (window.opener && window.opener.document.principal[asDatefield])
   {
      var lsId = window.opener.document.principal[asDatefield].id;
   }
   else if (window.document && window.document.principal[asDatefield])
   {
      var lsId = window.document.principal[asDatefield].id;
   }

   if ((window.document && window.document.principal[asDatefield+"DayName"])
       || (window.opener && window.opener.document.principal[asDatefield+"DayName"]))
   {
      lsFieldDayName = "javascript:window.opener.document.principal."+asDatefield+"DayName.value = ";
   }

   if (asLanguage == "FR")
   {
      var laMonths    = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"];
      var laDays      = ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"];
      var lnWeekStart = 1; // 0 -> Dimanche, 1 -> Lundi
      var trCalendar  = "Calendrier";
      var trPrevYear  = "Année précédente";
      var trPrevMonth = "Mois précédent";
      var trNextMonth = "Mois suivant";
      var trNextYear  = "Année suivante";
      var trTime      = "Heure :";
   }
   else
   {
      var laMonths    = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
      var laDays      = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
      var lnWeekStart = 0; // 0 -> Sunday, 1 -> Monday
      var trCalendar  = "Calendar";
      var trPrevYear  = "Previous year";
      var trPrevMonth = "Previous month";
      var trNextMonth = "Next month";
      var trNextYear  = "Next year";
      var trTime      = "Time:";
   }

   if (eval(abSearch))
   {
      if (asDatevalue.substr(0,2) == "<=" || asDatevalue.substr(0,2) == ">=" || asDatevalue.substr(0,2) == "<>")
      {
         asDatevalue = asDatevalue.substr(2,asDatevalue.length-2);
      }
      else if (asDatevalue.substr(0,1) == "<" || asDatevalue.substr(0,1) == ">" || asDatevalue.substr(0,1) == "=")
      {
         asDatevalue = asDatevalue.substr(1,asDatevalue.length-1);
      }
   }

   var ldDatevalue = (asDatevalue == null || asDatevalue =="" ?  new Date() : str2Date(asDatevalue, asLanguageFormat, asLanguage));
   var ldPrevYear  = new Date(ldDatevalue);
       ldPrevYear.setYear(ldDatevalue.getFullYear()-1);
   var ldPrevMonth = new Date(ldDatevalue);
       ldPrevMonth.setMonth(ldDatevalue.getMonth()-1);
   var ldNextMonth = new Date(ldDatevalue);
       ldNextMonth.setMonth(ldDatevalue.getMonth()+1)
   var ldNextYear  = new Date(ldDatevalue);
       ldNextYear.setYear(ldDatevalue.getFullYear()+1);
   var ldFirstDay  = new Date(ldDatevalue);
       ldFirstDay.setDate(1);
       ldFirstDay.setDate(1-(7+ldFirstDay.getDay()-lnWeekStart)%7);
   var liMonth;
   var liYear;
   var ldSelected;

   var lsSelectBuffer = "<select class=\"calendarListBox\" name=\"lbDate\" onChange=\"javascript:window.opener.showCalendar('"+asDatefield+"', this.options[this.selectedIndex].value + ' ' + document.cal.time.value, '"+asDateFormat+"', '"+asLanguageFormat+"', '"+asLanguage+"', 'false');\">\n";
   for (var nbMonths = -12 ; nbMonths <= 12 ; nbMonths++)
   {
      if (ldDatevalue.getMonth()+nbMonths < 0)
      {
         liMonth = 12 + (ldDatevalue.getMonth()+nbMonths);
         liYear = ldDatevalue.getFullYear()-1;
      }
      else if (ldDatevalue.getMonth()+nbMonths > 11)
      {
         liMonth = ldDatevalue.getMonth()+nbMonths-12;
         liYear = ldDatevalue.getFullYear()+1;
      }
      else
      {
         liMonth = ldDatevalue.getMonth()+nbMonths;
         liYear = ldDatevalue.getFullYear();
      }
      ldSelected = new Date(ldDatevalue);
      ldSelected.setMonth(ldDatevalue.getMonth()+nbMonths)
      lsSelectBuffer += "<option value=\""+date2D(ldSelected,asLanguageFormat)+"\"";
      if (nbMonths == 0)
      {
         lsSelectBuffer += " selected ";
      }
      lsSelectBuffer += ">"+laMonths[liMonth]+" "+liYear+"</option>\n";
   }
   lsSelectBuffer += "</select>\n";

   
   // Génération en HTML

   // Affichage de l'entête du calendrier
   var lsBuffer = new String (
      "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"+
      "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"fr\" xml:lang=\"fr\">\n"+
      "<head>\n"+
      "   <title>"+trCalendar+"</title>\n"+
      "   <style type=\"text/css\">\n"+
      "      .calendarBody { margin : 10px; background-color : #FFFFFF; }\n"+
      "      .calendarTable { background-color : #000000; border : 0px; width : 100%; }\n"+
      "      .intoCalendarTable { background-color : #000000; border : 0px; width : 100%; }\n"+
      "      .calendarImage { vertical-align : middle; }\n"+
      "      .calendarHeader { background-color : #F05A22; text-align : center; width : 14%; color : #000000; font-family : verdana, arial, sans-sherif; font-size : 10px; font-weight : bold; }\n"+
      "      .calendarListBox { font-family : verdana, arial, sans-sherif; font-size : 10px; }\n"+
      "      .currentDate { background-color : #FFB122; text-align : right; width : 14%; font-family : verdana, arial, sans-sherif; font-size : 10px; }\n"+
      "      .weekDate { background-color : #FFFFFF; text-align : right; width : 14%; font-family : verdana, arial, sans-sherif; font-size : 10px; }\n"+
      "      .weekendDate { background-color : #EEEEEE; text-align : right; width : 14%; font-family : verdana, arial, sans-sherif; font-size : 10px; }\n"+
      "      .monthDate { color : #000000; }\n"+
      "      .otherMonthDate { color : #888888; }\n"+
      "      .calendarFooter { background-color : #99CCFF; width : 14%; color : #FFFFFF; font-family : verdana, arial, sans-sherif; font-size : 10px; font-weight : bold; }\n"+
      "      .timeTextBox { font-family : verdana, arial, sans-sherif; font-size : 10px; margin-left : 10px; font-weight : normal; }\n"+
      "   </style>\n"+
      "</head>\n"+
      "<body class=\"calendarBody\">\n"+
         "<form name=\"cal\" action=\"\">\n"+
            "<table class=\"calendarTable\" align=\"center\" cellspacing=\"0\">\n"+
               "<tr>\n"+
                  "<td>\n"+
                     "<table class=\"intoCalendarTable\" cellspacing=\"1\" cellpadding=\"3\">\n"+
                        "<tr>\n"+
                           "<td colspan=\"7\" width=\"100%\">\n"+
                              "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">\n"+
                                 "<tr>\n"+
                                    "<td width=\"43\">\n"+
                                       "<a href=\"javascript:window.opener.showCalendar('"+asDatefield+"', '"+date2D(ldPrevYear,asLanguageFormat)+" '+document.cal.time.value, '"+asDateFormat+"', '"+asLanguageFormat+"', '"+asLanguage+"', 'false');\">"+
                                       "<img class=\"calendarImage\" src=\""+IMG_URL+"first.gif\" width=\"20\" height=\"15\" border=\"0\" alt=\""+trPrevYear+"\"/></a>\n"+
                                       //"&nbsp;"+
                                       "<a href=\"javascript:window.opener.showCalendar('"+asDatefield+"', '"+date2D(ldPrevMonth,asLanguageFormat)+" '+document.cal.time.value, '"+asDateFormat+"', '"+asLanguageFormat+"', '"+asLanguage+"', 'false');\">"+
                                       "<img class=\"calendarImage\" src=\""+IMG_URL+"previous.gif\" width=\"14\" height=\"15\" border=\"0\" alt=\""+trPrevMonth+"\"/></a>\n"+
                                    "</td>\n"+
                                    "<td align=\"center\">\n"+
                                       lsSelectBuffer+
                                    "</td>\n"+
                                    "<td align=\"right\" width=\"43\">\n"+
                                       "<a href=\"javascript:window.opener.showCalendar('"+asDatefield+"', '"+date2D(ldNextMonth,asLanguageFormat)+" '+document.cal.time.value, '"+asDateFormat+"', '"+asLanguageFormat+"', '"+asLanguage+"', 'false');\">"+
                                       "<img class=\"calendarImage\" src=\""+IMG_URL+"next.gif\" width=\"14\" height=\"16\" border=\"0\" alt=\""+trNextMonth+"\"/></a>\n"+
                                       //"&nbsp;"+
                                       "<a href=\"javascript:window.opener.showCalendar('"+asDatefield+"', '"+date2D(ldNextYear,asLanguageFormat)+" '+document.cal.time.value, '"+asDateFormat+"', '"+asLanguageFormat+"', '"+asLanguage+"', 'false');\">"+
                                       "<img class=\"calendarImage\" src=\""+IMG_URL+"last.gif\" width=\"20\" height=\"16\" border=\"0\" alt=\""+trNextYear+"\"/></a>\n"+
                                    "</td>\n"+
                                 "</tr>\n"+
                              "</table>\n"+
                           "</td>\n"+
                        "</tr>\n"
   );

   var ldCurrentDay = new Date(ldFirstDay);

   // Titres des jours de la semaine
   lsBuffer += "<tr>\n";
   for (var nbDays=0; nbDays<7; nbDays++)
   {
      lsBuffer += "<td class=\"calendarHeader\" width=\"14%\" align=\"center\" colspan=\"1\">\n"+
                       laDays[(lnWeekStart+nbDays)%7]+"\n";
                    "</td>\n";
   }
   // Tableau Calendrier
   lsBuffer += "</tr>\n";
   while (ldCurrentDay.getMonth() == ldDatevalue.getMonth() || ldCurrentDay.getMonth() == ldFirstDay.getMonth())
   {
      // Affichage d'une ligne
      lsBuffer += "<tr>\n";
      for (var nbDays=0; nbDays<7; nbDays++)
      {
         if (ldCurrentDay.getDate() == ldDatevalue.getDate() && ldCurrentDay.getMonth() == ldDatevalue.getMonth())
         {
            // Date courante
            lsBuffer += "<td class=\"currentDate\">";
         }
         else if (ldCurrentDay.getDay() == 0 || ldCurrentDay.getDay() == 6)
         {
            // Jours de week-end
            lsBuffer += "<td  class=\"weekendDate\">";
         }
         else
         {
            // Jours travaillés du mois courant
            lsBuffer += "<td class=\"weekDate\">";
         }

         lsValueDayName = '';
         if (lsFieldDayName != "")
         {
            lsValueDayName = lsFieldDayName + "'" + getDayName(date2D(ldCurrentDay,"FR"), "D", "FR", asLanguage) + "'";
         }

         if (ldCurrentDay.getMonth() == ldDatevalue.getMonth())
         {
            // Affichage des jours du mois courant
            lsBuffer += "<a class=\"monthDate\" href=\"javascript:window.opener.document.principal."+asDatefield+".value='"+getDatetime(ldCurrentDay, asDateFormat, asLanguageFormat, asLanguage)+"'; "+lsValueDayName+";window.close();\">";
         }
         else
         {
            // Affichage des jours des autres mois
            lsBuffer += "<a class=\"otherMonthDate\" href=\"javascript:window.opener.document.principal."+asDatefield+".value='"+getDatetime(ldCurrentDay, asDateFormat, asLanguageFormat, asLanguage)+"'; "+lsValueDayName+";window.close();\">";
         }

         lsBuffer += ldCurrentDay.getDate()+"</a>\n"+
                                                "</td>\n";
         ldCurrentDay.setDate(ldCurrentDay.getDate()+1);
      }
      lsBuffer += "</tr>\n";
   }

   // Affichage du pied du calendrier
   if (asDateFormat == 'T') // DateTime  affichage de l'heure
   {
      lsBuffer +=
                           "<tr>\n"+
                              "<td colspan=\"7\" class=\"calendarFooter\">\n"+
                                    trTime+" <input class=\"timeTextBox\" type=\"text\" name=\"time\" value=\""+date2T(ldDatevalue)+"\" size=\"8\" maxlength=\"8\" "+
                                    "onChange=\"javascript:window.opener.showCalendar('"+asDatefield+"', '"+date2D(ldDatevalue,"FR")+" '+document.cal.time.value, '"+asDateFormat+"', '"+asLanguageFormat+"', '"+asLanguage+"');\"/>"+
                              "</td>\n"+
                           "</tr>\n"+
                        "</table>\n";
   }
   else
   {
      lsBuffer +=     "</table>\n"+
                        " <input type=\"hidden\" name=\"time\" value=\""+date2T(ldDatevalue)+"\"/>";
   }
   lsBuffer +=
                     "</td>\n"+
                  "</tr>\n"+
               "</table>\n"+
            "</form>\n"+
         "</body>\n"+
      "</html>\n";

   if (asDateFormat == "T")
   {
      lsHeight = "232";
   }
   else
   {
      lsHeight = "204";
   }
   var loWinCal = window.open("", "Calendar","width=240,height="+lsHeight+",status=no,resizable=no,top=200,left=200,dependent=yes,alwaysRaised=yes");
   loWinCal.opener = self;
   var loCalendarDoc = loWinCal.document;
   loCalendarDoc.write (lsBuffer);
   loCalendarDoc.close();
}


// Transforme une date string au format date
// asDateValue : valeur de la date en string
// asLanguageFormat : format de la date : 'FR' (JJ/MM/AAAA)
//                                        'US' (MM/JJ/AAAA)
// asLanguage       : Language des traductions : 'FR'
//                                               'GB'
function str2Date(asDatevalue, asLanguageFormat, asLanguage)
{
   var reDate = /^(\d+)\/(\d+)\/(\d+)\s+(\d+)\:(\d+)\:(\d+)$/;
   if (!reDate.exec(asDatevalue))
   {
      var reDate = /^(\d+)\/(\d+)\/(\d+)$/;
      if (!reDate.exec(asDatevalue))
      {
         if (asLanguage == "FR")
         {
            var trError     = "Format date heure invalide :";
         }
         else
         {
            var trError     = "Invalid Datetime format:";
         }
         alert(trError + asDatevalue);
         return new Date();
      }
      if (asLanguageFormat == "FR")
      {
         return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1));
      }
      else
      {
         return (new Date (RegExp.$3, RegExp.$1-1, RegExp.$2));
      }
   }
   if (asLanguageFormat == "FR")
   {
      return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6));
   }
   else
   {
      return (new Date (RegExp.$3, RegExp.$1-1, RegExp.$2, RegExp.$4, RegExp.$5, RegExp.$6));
   }
}


// Transforme une date string au format date
// adDatevalue      : date à transformer
// asLanguageFormat : format de la date : 'FR' (JJ/MM/AAAA)
//                                        'US' (MM/JJ/AAAA)
function date2D(adDatevalue, asLanguageFormat)
{
   if (asLanguageFormat == "FR")
   {
      return (new String (
            (adDatevalue.getDate() < 10 ? "0" : "")+adDatevalue.getDate()+
            "/"+
            ((adDatevalue.getMonth()+1) < 10 ? "0" : "")+(adDatevalue.getMonth()+1)+
            "/"+
            adDatevalue.getFullYear()));
   }
   else
   {
      return (new String (
            ((adDatevalue.getMonth()+1) < 10 ? "0" : "")+(adDatevalue.getMonth()+1)+
            "/"+
            (adDatevalue.getDate() < 10 ? "0" : "")+adDatevalue.getDate()+
            "/"+
            adDatevalue.getFullYear()));
   }
}

// Transforme une date string au format heure (HH:MI:SS)
// adDatevalue      : date à transformer
function date2T(adDatevalue)
{
   return (new String (
         (adDatevalue.getHours() < 10 ? "0" : "")+adDatevalue.getHours()+
         ":"+
         (adDatevalue.getMinutes() < 10 ? "0" : "")+adDatevalue.getMinutes()+
         ":"+
         (adDatevalue.getSeconds() < 10 ? "0" : "")+adDatevalue.getSeconds()));
}

// Transforme une date string au format date
// adDatevalue      : date à transformer
// asLanguageFormat : format de la date : 'FR' (JJ/MM/AAAA HH:MI:SS)
//                                        'US' (MM/JJ/AAAA HH:MI:SS)
function date2DT(adDatevalue, asLanguageFormat)
{
   return (new String (
         date2D(adDatevalue, asLanguageFormat)+" "+
         date2T(adDatevalue)));
}


// Appelle les fonctions qui transforment une date en string au format

function getDatetime(adDatevalue, asDateFormat, asLanguageFormat)
{
    if (asDateFormat == 'T')
    {
       return date2DT(adDatevalue, asLanguageFormat);
    }
    if (asDateFormat == 'D')
    {
       return date2D(adDatevalue, asLanguageFormat);
    }
}
