var TDataCal = [
 ["FR",
  ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"],
  ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Décembre"],
  ["Mois précédent", "Mois suivant", "Année précédente", "Année suivante"],
  "Veuillez vérifier le format de la date."
 ],
 ["EN",
  ["S", "M", "T", "W", "T", "F", "S"],
  ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"],
  ["Previous month", "Next month", "Previous year", "Next year"],
  "Please check date format."
 ]
];
TJour = TDataCal[0][1]
TMois = TDataCal[0][2]


function CalObj (Nom, NomDiv, Dat, BD, Opt, Menu) {
	this.Nom      = Nom
	this.Div      = LID(NomDiv)
	this.BD       = BD
	this.Menu     = Menu
	this.NchDeb   = "DatDeb"
	this.NchFin   = "DatFin"
	this.ColorSel = "FFFF77"  //FFEFEF
	this.Visible  = true
	this.nbAgMois = 0

	AddProperty (this, Opt)

	this.ChangeDate = function (D) {
		this.D  = D;  if (!D) this.D = new Date()
		this.D1 = new Date(this.D.getYear(),this.D.getMonth(),1)
		this.Draw();
	}
	this.Clear   = function () {
		if (this.Div)  Div.innerHTML=""
		if (this.D)  thid.D  = null;
		if (this.D1) thid.D1 = null;
	}
	this.DrawInit()
	this.ChangeDate(Dat)
}

CalObj.prototype.DrawInit = function() {
  var row, cell, S="", H, I, J
  var Nom = this.Nom
  var Div = this.Div;  if (!Div)  return
  Div.style.display = "block"

  H= "<TABLE ID=" + Nom + "Cal Objet=Calend border=0 class=WholeCalendar>"
  H+="  <TR>"
  H+="    <TD Action=MPreced style='width:8; font:10pt Wingdings 3; cursor:hand'>|</TD>"
  H+="    <TD ID=" + Nom + "TitCal class=TitleCal style='width:80%'></TD>    "
  if (this.Menu)  S = "<IMG Action=Tree SRC=/Applis/Pack/Img/Ico/Win.gif Alt=Contenu>"
  H+="    <TD Action=Tree style='width:20; cursor:hand'>" + S + "</TD>"
  H+="    <TD Action=MSuiv align=right style='width:8; font:10pt Wingdings 3; cursor:hand'>}</TD>"
  H+="  </TR>"
  H+="  <TR> <TD colspan=4>"
  H+="    <TABLE class=CalTable cellspacing=0 xborder=1> "
  //H+="      <COL WIDTH=40><COL WIDTH=40><COL WIDTH=40><COL WIDTH=40><COL WIDTH=40><COL WIDTH=40><COL WIDTH=40>"
  H+="      <TR height=18>"
  H+="          <TD class=DayTitle></TD>"
  H+="          <TD class=DayTitle></TD>"
  H+="          <TD class=DayTitle></TD>"
  H+="          <TD class=DayTitle></TD>"
  H+="          <TD class=DayTitle></TD>"
  H+="          <TD class=DayTitle></TD>"
  H+="          <TD class=DayTitle></TD></TR>"
  H+="      <tr xheight=40><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>"
  H+="      <tr xheight=40><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>"
  H+="      <tr xheight=40><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>"
  H+="      <tr xheight=40><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>"
  H+="      <tr xheight=40><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>"
  H+="      <tr xheight=40><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>"
  H+="    </TABLE> "
  H+="  </TR>"
  if (this.MenuB) {
  	var T = this.MenuB.split(",")
	  H+="  <TR xheight=8> <TD colspan=4 style='font:8pt Arial;'>"
	  H+="    <TABLE class=CalTable cellspacing=0 xborder=1><TR height=10>"
	  for (i=0; i<T.length; i++) {
	    H+="      <TD align=center Action=Menu Style='font:bold 8pt Arial; padding:0; cursor:pointer'>" + T[i] + "</TD>"
	  }
	  H+="    </TR></TABLE> "
	  H+="  </TD></TR>"
	}
  H+="</TABLE>"
  Div.innerHTML = H
  Div.setAttribute("NomObj",Nom)
  Div.onclick = CalOnClic
}

CalObj.prototype.Draw = function() {
  var row, cell, S, I, J, L, C, rw, DD, DF, T, nbCarJ

  this.Div.style.display = (this.Visible) ? "block":"none"
  I = parseInt(this.Div.style.width)
  if (I>190) nbCarJ=3; else if (I>155) nbCarJ=2; else nbCarJ=1
  var Nom = this.Nom
  var D   = this.D;   J0 = D.getDate()
  var D1  = this.D1;  M1 = D1.getMonth()+1;  A1 = D1.getYear()
  var Div = this.Div

  PremJour = D1.getDay()
  NbJ = NbJMois(M1);  NbJP = NbJMois(M1-1)

  LID(Nom + "TitCal").innerHTML = TMois[M1-1] + " " + A1
  T = LTags(Div.childNodes[0].rows[1].cells[0],"TABLE")[0]

  for (cell=0; cell<=6; cell++) {
    CC = T.rows[0].cells[cell];  CC.innerHTML = TJour[cell].substr(0,nbCarJ)
  }
  PremDate = CalcDate(D1 , -PremJour, "J");  //alert(NbJP + ", " + PremDate)

  for (L=1, noJ=0; L<6; L++) {
	  for (C=0; C<7; C++) {
    	CC = T.rows[L].cells[C]
  	  DD = CalcDate(PremDate , noJ, "J");  J = DD.getDate();  M = DD.getMonth()+1;  A = DD.getYear()
  		if (M!=M1)  S = "CellOff";  else if (J==J0)  S = "CellSel";  else  S = "Cell"
  		CC.innerHTML = J
  		CC.style.backgroundColor = "";  CC.className = S
  		CC.setAttribute("Action","Jour");  CC.setAttribute("J",J);  CC.setAttribute("M",M);  CC.setAttribute("A",A);  CC.removeAttribute ("Cles")
  		CC.style.fontWeight="normal"
  		noJ++
  	}
  	//alert(T.rows[L].innerHTML + ", " + CC.outerHTML)
  }
  if (!this.BD)  return
  this.nbAgMois = 0
	for (var I=0; I<this.BD.nb; I++) {
		rw = this.BD[I]
		if (rw.DatDeb) {
			DD = new Date(InverseDate(rw.DatDeb));  D = DD
			if (rw.DatFin)  DF = new Date(InverseDate(rw.DatFin));  else  DF = DD
			while (D<=DF && D.getMonth()==this.D.getMonth()) {
			  CC = this.PointeJour(D);  if (!CC)  break
			  S = LX(CC,"Cles");  if (S)  Col = "FFFFCC";  else  Col = this.ColorSel
			  CC.style.backgroundColor = Col
			  CC.setAttribute ("title", rw.Lib)
			  CC.setAttribute ("Cles", AddStr (S, rw.Cle));  //alert(rw.DatDeb + ", " + D + ", " + Lout(CC) + ", " + this.ColorSel)
			  if (D==DD)  CC.style.fontWeight="bold"
			  D = CalcDate(D, 1, "J")
			  this.nbAgMois++
			}
		}
	}
  //for (row=1, noJ=0; row<=6; row++) {  for (cell=0; cell<=6; cell++) {  CC = T.rows(row).cells[cell]  }  }
}

CalObj.prototype.PointeJour = function(Dat) {
	var L, C
	if (typeof(Dat)=="string")  D = new Date(Dat)
  else {
  	if (Dat<32) D = new Date(this.D.getYear(), this.D.getMonth(), Dat);  else  D = Dat
  }
	J = D.getDate(); M = D.getMonth()+1;  A = D.getYear();    //alert(J + "/" + M + "/" + A)
  T = LTags(this.Div.childNodes[0].rows[1].cells[0],"TABLE")[0]
  for (L=1; L<6; L++) {
	  for (C=0; C<7; C++) {
    	CC = T.rows[L].cells[C]
	  	if (LX(CC,"J")==J && LX(CC,"M")==M)  return(CC)
	  }
  }
}

CalObj.prototype.FormatDate = function(Format) {  return FormatDate(this.D, Format)  }

CalObj.prototype.Show = function() {
	this.Visible = true
  this.Div.style.display="block"
}

CalObj.prototype.Hide = function() {
	this.Visible = false
  this.Div.style.display="none"
}



function CalOnClic(e) {
	var D
  var O  = (document.all) ? window.event.srcElement : e.target;  //alert(O.outerHTML)
  ObjCal = eval(LX(this,"NomObj"))
  var Action = LX(O,"Action")
  switch (Action) {
  	case "Jour":     D = new Date(LX(O,"A"), LX(O,"M")-1, LX(O,"J"));  break;
  	case "MPreced":  D = CalcDate(ObjCal.D, -1, "M");  break;
  	case "MSuiv":    D = CalcDate(ObjCal.D,  1, "M");  break;
  	case "Tree":
  	  if (ObjCal.Menu) {
			  WinFlo = new WinObj("WinFlo","","width=160| height=100| position=absolute| top=350| left=100| background=FAFAFF")
			  WinFlo.AffListe(ObjCal.Menu)
			}
		  break;
  }
	var Cles = LX(O,"Cles");  if (Cles)  ObjCal.Cles = Cles
	if (D)  ObjCal.ChangeDate(D)
	if (ObjCal.onCalClic)  ObjCal.onCalClic(Action, O)
 	//alert(this.outerHTML)
 	//var Obj = ElemParent(this,"O.getAttribute('Objet')=='Calend'");  if (!Obj)  return
}




function NbJMois(M, A) {
	if (M==0)  M=12
  TM = [31,29,31,30,31,30,31,31,30,31,30,31]
  if (M==2) {   // bissextile ou pas
	  if ((A % 100 != 0 && A % 4 == 0) || (A % 400 == 0)) { return 29; }
	  else { return 28; }
	}else{
	  return(TM[M-1])
  }
}

function DefWeekNum(dd) {
  // Renvoie le numero de la semaine correspondant a la date requise
	numd = 0;
	numw = 0;
	for (n=1; n<QueryMonth; n++) {
	  numd += MonthLength[n];
	}
	numd = numd + dd - (9 - DefDateDay(QueryYear,1,1));
	numw = Math.floor(numd / 7) + 1;

	if (DefDateDay(QueryYear,1,1) == 1) { numw++; }
	return numw;
}

function CalcDate(D, Nb, Quoi) {
	  //alert(D.getDay() + ", " + D.getDate() + ", " + D.getMonth() + ", " + D.getYear())
	J = D.getDate();  M = D.getMonth()+1;  A = D.getYear()
	switch (Quoi) {
		case "J":  J+=Nb;  break
		case "M":  M+=Nb;  if (M>12) { A+=1;  M-=12 };  break
		case "A":  A+=Nb;  break
	}
  return new Date(A, M-1, J)
}

function InverseDate(Dat) {
	var A, T
	if (!Dat)  return Dat
	TH = Dat.split(" ");  T = TH[0].split("/");  if (T.length<3)  return Dat
	A = T[2];  if (A<30)  A="20" + A
	D = A + "/" + T[1] + "/" + T[0];	if (TH.length>1) D+= " " + TH[1]
	return D
}

