TAB_CONTROLS = new Array();

var DateJour = new Date();
DATE_DU_JOUR = new Array();
DATE_DU_JOUR[0] = DateJour.getDate();
DATE_DU_JOUR[1] = DateJour.getMonth();
DATE_DU_JOUR[2] = DateJour.getFullYear();


//**************************************************************
//* Contrôle Calendrier
//**************************************************************

function CtrlCalendrier(largeur) {
    this.index = TAB_CONTROLS.length;
    TAB_CONTROLS[this.index] = this;
    var datedefaut = new Date();
    datedefaut.setDate(DATE_DU_JOUR[0]);
    datedefaut.setMonth(DATE_DU_JOUR[1]);
    datedefaut.setFullYear(DATE_DU_JOUR[2]);
    this.value = datedefaut;
    this.evenements = Array();
    this.premier = 0;
    this.largeur = largeur;
}

CtrlCalendrier.prototype.Add = function(control) {
    var tmp = new Date(Number(control.dateevn)); 
    var key = ChaineComplete("0000",tmp.getFullYear(),4)+ChaineComplete("00",tmp.getMonth()+1,2)+ChaineComplete("00",tmp.getDate(),2);
    if (this.evenements[key] == null) this.evenements[key]=control.value;
}

CtrlCalendrier.prototype.Construct = function() {
    var output = "";
    output += "<DIV id='TableauCalendrier_"+this.index+"'>";
    output += this.ConstructContenu();
    output += "</DIV>";
    return output;
}

CtrlCalendrier.prototype.ConstructContenu = function() {
    var output = "";
    var tmpmois = new Date(this.value.getFullYear(),this.value.getMonth(),this.value.getDate());
    output += "<TABLE CLASS='Calendrier_Tableau' border=0 cellpadding=1 cellspacing=0 width='"+this.largeur+"'>";
    output += "<TR>";
    output += "<TD align='left' width='14%' id='Prec_Calendrier_ctrl_"+this.index+"' Class='Calendrier_EntetePrec' ";
    output += " accessoryGroups='caltitre' onclick='javascript:TAB_CONTROLS["+this.index+"].Navigation(this.id);'>&lt;</TD>";
    output += "<TD accessoryGroups='caltitre' align='center' Class='Calendrier_Entete' colspan=5 ";
    output += " >" + MoisEnClair(tmpmois) + " " + tmpmois.getFullYear() + "</TD>";
    output += "<TD accessoryGroups='caltitre' align='right' width='14%' id='Suiv_Calendrier_ctrl_"+this.index+"' Class='Calendrier_EnteteSuiv' ";
    output += " onclick='javascript:TAB_CONTROLS["+this.index+"].Navigation(this.id);'>&gt;</TD></TR>";
    output += "<TR>";
    for(var i=0 ; i < 7 ; i++)
    {
	output += "<TD width='14%' accessoryGroups='caljour' Class='Calendrier_EnteteJours' align='center'";
	output += " >" + JourEnClair(i) + "</TD>";
    }
    output +="<TR>";
    var tmp = new Date(this.value.getFullYear(),this.value.getMonth(),this.value.getDate());
    var col = 0 ;
    var lig = 0;
    tmp.setMonth(tmp.getMonth())
    tmp.setDate(1);
    while(tmp.getMonth()==tmpmois.getMonth())
    {
	if (col==7)
	    {
		output += "</TR><TR>";
		col = 0 ;
		lig++;
	    }
	if (tmp.getDay()==col)
	    {
		if(tmp.getDate()==1)
        this.premier = col;
		if(this.evenements[ChaineComplete("0000",tmp.getFullYear(),4)+ChaineComplete("00",tmp.getMonth()+1,2)+ChaineComplete("00",tmp.getDate(),2)]!=null)
		    {
        output += "<TD align='center' accessoryGroups='calevenement' width='14%' id='Cell_Calendrier_ctrl_"+this.index+"_"+ChaineComplete("000",col,3)+"_"+ChaineComplete("000",lig,3)+"' Class='Calendrier_Existe'";
	output += "><A Class='Calendrier_Lien' href='"+this.evenements[ChaineComplete("0000",tmp.getFullYear(),4)+ChaineComplete("00",tmp.getMonth()+1,2)+ChaineComplete("00",tmp.getDate(),2)]+"'>"+tmp.getDate()+"</a></TD>";
		    }
		else
		    {
			output += "<TD align='center' accessoryGroups='calfond' width='14%' id='Cell_Calendrier_ctrl_"+this.index+"_"+ChaineComplete("000",col,3)+"_"+ChaineComplete("000",lig,3)+"' Class='Calendrier_NExiste'";
			output += ">"+tmp.getDate()+"</TD>";
		    }
		tmp.setDate(tmp.getDate()+1);
	    }
	else
	    {
		output += "<TD align='center' width='14%' id='Cell_Calendrier_ctrl_"+this.index+"_"+ChaineComplete("000",col,3)+"_"+ChaineComplete("000",lig,3)+"' Class='Calendrier_Blanc'";
		output += ">&nbsp;</TD>";
	    }
	col++;
    }
    while (col!=7)
    {
	output += "<TD align='center' width='14%' id='Cell_Calendrier_ctrl_"+this.index+"_"+ChaineComplete("000",col,3)+"_"+ChaineComplete("000",lig,3)+"' Class='Calendrier_Blanc'";
	output += ">&nbsp;</TD>";
	col++;
  }
    output += "</TR>";
    tmp.setDate(tmp.getDate()-1);
/*
    while (lig!=5)
    {
	output += "<TR>";
	col=0;
	lig++;
	while (col!=7)
	    {
		output += "<TD align='center' id='Cell_Calendrier_ctrl_"+this.index+"_"+ChaineComplete("000",col,3)+"_"+ChaineComplete("000",lig,3)+"' Class='Calendrier_Blanc'";
		output += ">&nbsp;</TD>";
		col++;
	    }
	output += "</TR>";
    }
*/
    output += "</TABLE>";

    return output;
}


CtrlCalendrier.prototype.Navigation = function(id)
{
  if (id.substr(0,4)=="Prec")
  {
    this.value.setMonth(this.value.getMonth()-1);
    this.Refresh();
  }
  else
  {
    this.value.setMonth(this.value.getMonth()+1);
    this.Refresh();
  }
}

CtrlCalendrier.prototype.Refresh = function ()
{
  document.all("TableauCalendrier_"+this.index).innerHTML=this.ConstructContenu();
}


//**************************************************************
//* Contrôle Evénement
//**************************************************************

function CtrlEvenement(date,lien) {
  this.dateevn = date
  this.value=lien;
}

CtrlEvenement.prototype.GetValue = function ()
{
 return this.value;
}


function MoisEnClair(valdate)
{
  return MOIS_ANNEE[valdate.getMonth()];
}

function JourEnClair(valdate)
{
  return JOUR_SEMAINE[valdate];
}

function ChaineComplete(char,string,longueur)
{
 var tmp = char + String(string);
 return tmp.substr((tmp.length-longueur),longueur);
}

