
function tableau() {
  this.length = tableau.arguments.length
  for (var i = 0; i < this.length; i++)
  this[i+1] = tableau.arguments[i]
}

var tablJour = new tableau("Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi");
var tablMois = new tableau("Janvier","F&eacute;vrier","Mars","Avril","Mai","Juin","Juillet","Ao&ucirc;t","Septembre","Octobre","Novembre","D&eacute;cembre");
var DateJour = new Date();

function date() {
  document.write(tablJour[(DateJour.getDay()+1)]," ",DateJour.getDate()," ");
  document.write(tablMois[(DateJour.getMonth()+1)]," ");
  if (!document.all) {
    document.write((DateJour.getFullYear()));
  } else {
    document.write(DateJour.getFullYear());
  }
  document.write("&nbsp;&nbsp;");
}

function dateShort() {
  document.write(tablJour[(DateJour.getDay()+1)]," ",DateJour.getDate()," ");
  document.write(tablMois[(DateJour.getMonth()+1)]);
}

function time() {
  document.write(DateJour.getHours(),":",DateJour.getMinutes());
}

function TestTime(strTime)
{
// format test้ 00:00:00
  re = /^(\d{1,2}):(\d{1,2}):?(\d{1,2})?$/;
  MyArray = re.exec(strTime);
  if (!MyArray) return false;
  iHou = MyArray[1];
  iMin = MyArray[2];
  if (MyArray.length > 3) {
    iSec = MyArray[3];
    if (iSec < 0 && iSec > 59) return false;
  }
  if (iHou < 0 || iHou > 23) return false;
  if (iMin < 0 && iMin > 59) return false;
  return true;
}

function checkDate(iDay, iMonth, iYear) {
  if (iMonth < 1 || iMonth > 12) return false;
  if (iMonth == 2 && iDay > 28) {
    if (iYear%4 != 0) return false;
    if (iDay > 29) return false;
  } else
  if ((iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && iDay > 30) {
    return false;
  } else 
  if (iDay > 31) return false;
  return true;
}

function TestDate(strDate)
{
 // format test้ AAAA-MM-JJ HH:MM:SS
  re = /^(\d{4})-(\d{2})-(\d{2})\s?(\d{1,2}:\d{1,2}:\d{1,2})?$/;
  MyArray = re.exec(strDate);
  if (MyArray) {
    iDay = Number(MyArray[3]);
    iMonth = Number(MyArray[2]);
    iYear = Number(MyArray[1]);
    if (!checkDate(iDay, iMonth, iYear)) return false;
    if (MyArray[4]) if (!TestTime(MyArray[4])) return false;
    return true;
  }
// format test้ JJ/MM/AAAA
  re1 = /^(\d{1,2})[- \/](\d{1,2})[- \/](\d{2}|\d{4})$/;
  MyArray = re1.exec(strDate);
  if (MyArray) {
    iDay = Number(MyArray[1]);
    iMonth = Number(MyArray[2]);
    iYear = Number(MyArray[3]);
    if (!checkDate(iDay, iMonth, iYear)) return false;
    return true;
  }
  return false;
}

function SetDateToSql(strDate) {
  if (!TestDate(strDate)) return false;
  sDate = strDate;
  re1 = /^(\d{1,2})[- \/](\d{1,2})[- \/](\d{2}|\d{4})$/;
  MyArray = re1.exec(strDate);
  if (MyArray) {
    iYear = Number(MyArray[3]);
    if (iYear < 100) iYear+=2000;
    iMonth = Number(MyArray[2]);
    sMonth = (iMonth < 10) ? "0"+iMonth : String(iMonth);
    iDay = Number(MyArray[1]);
    sDay = (iDay < 10) ? "0"+iDay : String(iDay);
    sDate = String(iYear)+"-"+sMonth+"-"+sDay;
  }
  return sDate;
}

function TestDateFR(strDate)
{ // format test้ : date francaise JJ/MM/AAAA
  re1 = /^(\d{1,2})[- \/](\d{1,2})[- \/](\d{2}|\d{4})$/;
  MyArray = re1.exec(strDate);
  if (MyArray) {
    iDay = Number(MyArray[1]);
    iMonth = Number(MyArray[2]);
    iYear = Number(MyArray[3]);
    if (!checkDate(iDay, iMonth, iYear)) return false;
    return true;
  }
  return false;
}

function SetFRDateToSql(strDate) {
//Passe une date francaise 28/04/1977 en date 1977-04-28 - Modif Dam accepte JJMMAA(AA)
  //if (!TestDateFR(strDate)) return strDate;
  sDate = strDate;
  re1 = /^(\d{1,2})[- \/](\d{1,2})[- \/](\d{2}|\d{4})$/;
  MyArray = re1.exec(strDate);
  bFormatOK = false;
  if (MyArray) {
    iYear = Number(MyArray[3]);
    iMonth = Number(MyArray[2]);
    iDay = Number(MyArray[1]);
    bFormatOK = true;
  } else {
    re1 = /^(\d{2})(\d{2})(\d{2}|\d{4})$/;
    MyArray = re1.exec(strDate);
    if (MyArray) {
      iYear = Number(MyArray[3]);
      iMonth = Number(MyArray[2]);
      iDay = Number(MyArray[1]);
      bFormatOK = true;
    }
  }
  if (!bFormatOK) return strDate;
  if (!checkDate(iDay, iMonth, iYear)) return strDate;
  if (iYear < 100) iYear+=2000;
  sMonth = (iMonth < 10) ? "0"+iMonth : String(iMonth);
  sDay = (iDay < 10) ? "0"+iDay : String(iDay);
  sDate = String(iYear)+"-"+sMonth+"-"+sDay;
  return sDate;
}

function SetSqlDateToFrDat(strDate) {
//Passe une date sql courte 1977-04-28 en date francaise courte 28/04/1977
  if (!TestDate(strDate)) return strDate;
  sDate = strDate;
  re1 = /^(\d{4})[- \/](\d{1,2})[- \/](\d{2}|\d{1,2})$/;
  MyArray = re1.exec(strDate);
  if (MyArray) {
    iYear = Number(MyArray[1]);
    if (iYear < 100) iYear+=2000;
    iMonth = Number(MyArray[2]);
    sMonth = (iMonth < 10) ? "0"+iMonth : String(iMonth);
    iDay = Number(MyArray[3]);
    sDay = (iDay < 10) ? "0"+iDay : String(iDay);
    sDate = sDay+"/"+sMonth+"/"+String(iYear);
  }
  return sDate;
}

function SetFullSqlDateToFrDat(strDate) {
//Passe une date sql longue 1977-04-28 00:00:00 en date francaise courte 28/04/1977
  if (!TestDate(strDate)) return strDate;
  sDate = strDate;
  adat = (strDate.split(" "))[0];
  strDate=adat;  
  re1 = /^(\d{4})[- \/](\d{1,2})[- \/](\d{2}|\d{1,2})$/;
  MyArray = re1.exec(strDate);
  if (MyArray) {
    iYear = Number(MyArray[1]);
    if (iYear < 100) iYear+=2000;
    iMonth = Number(MyArray[2]);
    sMonth = (iMonth < 10) ? "0"+iMonth : String(iMonth);
    iDay = Number(MyArray[3]);
    sDay = (iDay < 10) ? "0"+iDay : String(iDay);
    sDate = sDay+"/"+sMonth+"/"+String(iYear);
  }
  return sDate;
}

function SetFullSqlDateToFullFrDat(strDate) {
//Passe une date sql longue 1977-04-28 00:00:00 en date francaise  28/04/1977 00:00:00
  if (!TestDate(strDate)) return strDate;
  sDate = strDate;
  adat = (strDate.split(" "))[0];
    aheure = (strDate.split(" "))[1];
  strDate=adat;  
  re1 = /^(\d{4})[- \/](\d{1,2})[- \/](\d{2}|\d{1,2})$/;
  MyArray = re1.exec(strDate);
  if (MyArray) {
    iYear = Number(MyArray[1]);
    if (iYear < 100) iYear+=2000;
    iMonth = Number(MyArray[2]);
    sMonth = (iMonth < 10) ? "0"+iMonth : String(iMonth);
    iDay = Number(MyArray[3]);
    sDay = (iDay < 10) ? "0"+iDay : String(iDay);
    sDate = sDay+"/"+sMonth+"/"+String(iYear)+" "+aheure;
  }
  return sDate;
}

function GetSqlDat(strDate){
//date sql -> Date sql sans l'heure
  adat = (strDate.split(" "))[0];
  return adat;
}

function GetSqlHeure(strDate){
//date sql (2000-28-04 09:12:25) -> heure (09:12:25)
  adat = (strDate.split(" "))[1];
  return adat;
}

function GetTime(strDate){
//date -> heure
  adat = (strDate.split(" "))[1];
  return adat;
}

function GetCurDate() {
//Date courante
  d = new Date();
  iYear = Number(d.getYear()); if (iYear < 100) iYear+=2000;
  iMonth = Number(d.getMonth())+1; sMonth = (iMonth < 10) ? "0"+iMonth : String(iMonth);
  iDay = Number(d.getDate());  sDay = (iDay < 10) ? "0"+iDay : String(iDay);
  iHour = Number(d.getHours());  sHour = (iHour < 10) ? "0"+iHour : String(iHour);
  iMn = Number(d.getMinutes());  sMn = (iMn < 10) ? "0"+iMn : String(iMn);
  iSec = Number(d.getSeconds());  sSec = (iSec < 10) ? "0"+iSec : String(iSec);
  df = String(iYear)+"-"+sMonth+"-"+sDay+" "+sHour+":"+sMn+":"+sSec;
  return df;
}

function AddTimeToDate(addTime) {
//addTime est une valeur en semaines
  dat = new Date();
  dateInMs = dat.getTime();
  dateInMs += (addTime*7*24*3600*1000) ;
  d = new Date(dateInMs)
  iYear = Number(d.getYear()); if (iYear < 100) iYear+=2000;
  iMonth = Number(d.getMonth())+1; sMonth = (iMonth < 10) ? "0"+iMonth : String(iMonth);
  iDay = Number(d.getDate());  sDay = (iDay < 10) ? "0"+iDay : String(iDay);
  iHour = Number(d.getHours());  sHour = (iHour < 10) ? "0"+iHour : String(iHour);
  iMn = Number(d.getMinutes());  sMn = (iMn < 10) ? "0"+iMn : String(iMn);
  iSec = Number(d.getSeconds());  sSec = (iSec < 10) ? "0"+iSec : String(iSec);
  df = String(iYear)+"-"+sMonth+"-"+sDay+" "+sHour+":"+sMn+":"+sSec;
  return df;
}
