/*****************************************************************
fichier : calendar.js
	librairie utilisee pour : 
		afficher un calendrier
		inserer les "/" a la volee
		positionne la
	@author SROS
	@creation 2002
	@version 1.0

	@modification 29/04/2003 - DTHO
	@version 1.0a
*****************************************************************/
// texte par defaut
var txtCalendarMois=["Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin","Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Decembre"];
var txtCalendarJour = ["Di","Lu", "Ma", "Me", "Je", "Ve", "Sa"];
var txtCalendarMoisPrecedent="Mois Pr&eacute;c&eacute;dent";
var txtCalendarMoisSuivant="Mois Suivant";
var txtCalendarDateInvalide="Format de Date Invalide: <b>(0)</b>.<br>Le calendrier va se positionner <br>sur la <b>date du jour</b>."
var txtCalendarCalendrier="Calendrier"
//Gestion des couleurs
var couleurFondEcran="#F7F7F3"
var couleurFondMois="#339966"
var couleurFondJour="#99CC99"
var couleurFondNumeroJour="white"
var couleurFondNumeroJourWE="#CCCCCC"
var couleurFondDateCourante="#FFCC66"
var policeJour="black"
var policeJourAutreMois="gray"
var policeMois="white"
var choixPolice="tahoma, verdana"


/*****************************************************************
variable globale : chemin vers les ressources graphiques
*****************************************************************/
var pathImage="/newwebapp/images/";

/*************************************************************************
But positionne une date au 31/12/9999
	@param input nomChamp : champ a impacter (ex document.form.date)
	@author : SROS
*************************************************************************/
function calendar_datefin(nomChamp) {
	nomChamp.value = "31/12/9999";
}

//*************************************************************************
//
// Permet de compter les occurences dans une chaine de caractere.
// 
//
//@param sChaine : chaine de caractere dans laquelle on recherche.
//	 sCaractere : caractere recherche dans la chaine ch.	
//
// Exemple d'utilisation:
// var compte = calendar_compterOccurence(chaine,"/");
//
// auteur : Sylvain ROSSANO
//*************************************************************************
function calendar_compterOccurence(sChaine, sCaractere) {
	var nCpt = 0;
	for (i=0;i<sChaine.length;i++){
		if(sChaine.charAt(i) == sCaractere){
			nCpt = nCpt + 1 ;


		}
	}
	return nCpt;
}

//*************************************************************************
//
// Permet de generer automatiquement un masque de saisie correct pour une date
// en ne saisissant que les chiffres. A appliquer sur un champ input sur les 
// evenements onKeyUp et onKeyPress.
//
//@param obj : nom du champ input (date) auquel s'applique le masque	
//
// Exemple d'utilisation:
//
// onKeyUp="calendar_masqueSaisieDate(form.date)" 
// onKeyPress="calendar_masqueSaisieDate(form.date)"
//
// auteur : Sylvain ROSSANO
//*************************************************************************
function calendar_masqueSaisieDate(obj) {
	var sCh;
	var sChGauche, sChDroit;
	var nCompte;

	sCh = obj.value;
	sCh.toString();
	nCompte = calendar_compterOccurence(sCh,"/");
	if ( nCompte != 2){		
		//test char 2 is is an int
		var day = sCh.slice(1,2)		
		if(checkIfInt(day)){		
			if ( ( (sCh.slice(2,3)) != ("/") ) && (sCh.length >= 3) ){
				sChGauche = sCh.slice(0,2);
				sChDroit = sCh.slice(2);
				obj.value = sChGauche + "/" + sChDroit;
			}
		}else{
			sCh = "0"+sCh;
		}
		//test char 5 is is an int		
		var month = sCh.slice(4,5)
		if(checkIfInt(month)){
			if ( ( (sCh.slice(5,6)) != ("/") ) && (sCh.length >= 6) ){
				sChGauche = sCh.slice(0,5);
				sChDroit = sCh.slice(5);
				obj.value = sChGauche + "/" + sChDroit;
			}
		}	
	}
}

function checkIfInt(i){	
	if(i>=0 && i<=9){	
		return true;
	}
	return false;
}


//*************************************************************************
//
// Permet de generer automatiquement un masque de saisie correct pour une date
// en ne saisissant que les chiffres. A appliquer sur un champ input sur les 
// evenements onKeyUp et onKeyPress.
//
//@param obj : nom du champ input (heure) auquel s'applique le masque	
//
// Exemple d'utilisation:
//
// onKeyUp="calendar_masqueSaisieHeure(form.heure)" 
// onKeyPress="calendar_masqueSaisieHeure(form.heure)"
//
// auteur : Sylvain ROSSANO
//*************************************************************************
function calendar_masqueSaisieHeure(obj) {
	var sCh;
	var sChGauche, sChDroit;
	var nCompte;

	sCh = obj.value;
	sCh.toString();
	nCompte = calendar_compterOccurence(sCh,":");
	if ( nCompte != 1){
		if (sCh.length==4) {
			sChGauche = sCh.slice(0,2);
			sChDroit  = sCh.slice(2,4);
			obj.value = sChGauche+":"+sChDroit
		}
	}
}

//*************************************************************************
//
// Permet de generer la popup ou s'affiche le calendrier.
// a positionner sur l'évenement OnClick()
//
//@param str_target : nom du champ input 
// Exemple d'utilisation:
// onClick="calendar_show('document.formulaire.date');"
//
// auteur : Sylvain ROSSANO
//*************************************************************************

function kb_calendar_show(str_target, str_datetime) {
	if(event.keyCode==13) {
		calendar_show(str_target, str_datetime)
	}
	event.keyCode=0
	window.event.returnValue=false;
	return false;
}

//*************************************************************************
//
// Permet de generer la popup ou s'affiche le calendrier.
// a positionner sur l'évenement OnClick()
//
//@param str_target : nom du champ input 
// Exemple d'utilisation:
// onClick="calendar_show('document.formulaire.date');"
//
// auteur : Sylvain ROSSANO
//*************************************************************************

function calendar_show(str_target, str_datetime) {
	if (isUndefined(str_datetime)) {
		str_datetime=eval(str_target+".value")
	}
	var n_weekstart = 1; // jour de la semaine commance normalement a 0 ou 1

	var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : calendar_verifformat(str_datetime));
	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setMonth(dt_datetime.getMonth()-1);
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth()+1);
	
	
	if (dt_next_month.getMonth()>dt_datetime.getMonth()) {
		while(((dt_next_month.getMonth()-dt_datetime.getMonth())%12)!=1) {
			dt_next_month.setDate(dt_next_month.getDate()-1);
		}
	}
	if (dt_next_month.getMonth()<dt_datetime.getMonth()) {
		while(((dt_datetime.getMonth()-dt_next_month.getMonth())%12)!=11) {
			dt_next_month.setDate(dt_next_month.getDate()-1);
		}
	}
	if (dt_prev_month.getMonth()<=dt_datetime.getMonth()) {
		while(((dt_datetime.getMonth()-dt_prev_month.getMonth())%12)!=1) {
			dt_prev_month.setDate(dt_prev_month.getDate()-1);
		}
	}
	if (dt_prev_month.getMonth()>dt_datetime.getMonth()) {
		while(((dt_prev_month.getMonth()-dt_datetime.getMonth())%12)!=11) {
			dt_prev_month.setDate(dt_prev_month.getDate()-1);
		}
	}
	
	
	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);

	var str_target_protected="";
	for(var i=0; i<str_target.length; i++) {
		var tChar=str_target.charAt(i);
		switch(tChar) {
			case '\'':
			str_target_protected+="\\\'"
			break
			case '\"':
			str_target_protected+="\\\""
			break
			default:
			str_target_protected+=tChar
		}
	}
	
	// html generation du calendrier
	var str_buffer = new String (
		"<html>\n"+
		"<head>\n"+
		"<title>"+txtCalendarCalendrier+"</title>\n"+
		"</head>\n"+
		"<script language=JavaScript>\n"+
		"function gotoPrev() {\n"+
		"	window.opener.calendar_show('"+str_target_protected+"', '"+ calendar_formatage(dt_prev_month)+"');\n"+
		"}\n"+
		"function gotoNext() {\n"+
		"	window.opener.calendar_show('"+str_target_protected+"', '"+ calendar_formatage(dt_next_month)+"');\n"+
		"}\n"+
		"function gestionTouche() {\n"+
		"	switch(event.keyCode) {\n"+
		"		case 27: window.close();\n"+
		"	}\n"+
		"}\n"+
		"</script>\n"+
		"<body bgcolor="+couleurFondEcran+" onBlur=\"window.focus();\" onKeyDown=\"gestionTouche();\">\n"+
		"<table class=\"clsOTable\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
		"<tr><td bgcolor="+couleurFondMois+">\n"+
		"<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
		"<tr>\n	<td bgcolor="+couleurFondMois+"><a href=\"javascript:gotoPrev();\">"+
		"<img src=\""+pathImage+"calPrev.gif\" width=\"20\" height=\"20\" border=\"0\""+
		" alt=\""+txtCalendarMoisPrecedent+"\"></a></td>\n"+
		"	<td bgcolor="+couleurFondMois+" colspan=\"5\">"+
		"<font color="+policeMois+" face="+choixPolice+" size=\"2\">"
		+txtCalendarMois[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</font></td>\n"+
		"	<td bgcolor="+couleurFondMois+" align=\"right\"><a href=\"javascript:gotoNext();\">"+
		"<img src=\""+pathImage+"calNext.gif\" width=\"20\" height=\"20\" border=\"0\""+
		" alt=\""+txtCalendarMoisSuivant+"\"></a></td>\n</tr>\n"
	);

	var dt_current_day = new Date(dt_firstday);
	// affichage des titres jours de semaine
	str_buffer += "<tr>\n";
	for (var n=0; n<7; n++)
		str_buffer += "	<td bgcolor="+couleurFondJour+">"+
		"<font color="+policeMois+" face="+choixPolice+" size=\"2\">"+
		txtCalendarJour[(n_weekstart+n)%7]+"</font></td>\n";
	// affichage table calendrier
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		// affichage colonne
		str_buffer += "<tr>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (dt_current_day.getDate() == dt_datetime.getDate() &&
					dt_current_day.getMonth() == dt_datetime.getMonth())
					// affichage date courante
					str_buffer += "	<td bgcolor="+couleurFondDateCourante+" align=\"right\">";
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// jour de semaine
					str_buffer += "	<td bgcolor="+couleurFondNumeroJourWE+" align=\"right\">";
				else
					// affichage de jour de travail du mois courant 
					str_buffer += "	<td bgcolor="+couleurFondNumeroJour+" align=\"right\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// affichage de jour du mois courant 
					str_buffer += "<a href=\"javascript:window.opener."+str_target+
					".value='"+calendar_formatage(dt_current_day)+"'; window.close();\">"+
					"<font color="+policeJour+" face="+choixPolice+" size=\"2\">";
				else
					// affichage de jour des autres mois
					str_buffer += "<a href=\"javascript:window.opener."+str_target+
					".value='"+calendar_formatage(dt_current_day)+"'; window.close();\">"+
					"<font color="+policeJourAutreMois+" face="+choixPolice+" size=\"2\">";
				str_buffer += dt_current_day.getDate()+"</font></a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		// affichage pied de page de la colonne
		str_buffer += "</tr>\n";
	}
	//affichage pied de page calendrier
	str_buffer +=
		"</table>\n" +
		"</tr>\n</td>\n</table>\n" +
		"</body>\n" +
		"</html>\n";

	var vWinCal = window.open("", "Calendar",
		"width=200,height=215,status=yes,resizable=yes,menubar=no,titlebar=no,top=200,left=400");
	vWinCal.opener = self;
	vWinCal.focus();
	var calc_doc = vWinCal.document;
	calc_doc.write (str_buffer);
	calc_doc.close();
}

function calendar_show_double_target(str_target1, str_target2, str_datetime) {
	if (isUndefined(str_datetime)) {
		str_datetime=eval(str_target1+".value")
	}
	var n_weekstart = 1; // jour de la semaine commance normalement a 0 ou 1

	var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : calendar_verifformat(str_datetime));
	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setMonth(dt_datetime.getMonth()-1);
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth()+1);
	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);

	var str_target_protected="";
	for(var i=0; i<str_target1.length; i++) {
		var tChar=str_target1.charAt(i);
		switch(tChar) {
			case '\'':
			str_target_protected+="\\\'"
			break
			case '\"':
			str_target_protected+="\\\""
			break
			default:
			str_target_protected+=tChar
		}
	}
	
	// html generation du calendrier
	var str_buffer = new String (
		"<html>\n"+
		"<head>\n"+
		"<title>"+txtCalendarCalendrier+"</title>\n"+
		"</head>\n"+
		"<script language=JavaScript>\n"+
		"function gotoPrev() {\n"+
		"	window.opener.pv_calendar_show('"+str_target_protected+"', '"+ calendar_formatage(dt_prev_month)+"');\n"+
		"}\n"+
		"function gotoNext() {\n"+
		"	window.opener.pv_calendar_show('"+str_target_protected+"', '"+ calendar_formatage(dt_next_month)+"');\n"+
		"}\n"+
		"function gestionTouche() {\n"+
		"	switch(event.keyCode) {\n"+
		"		case 27: window.close();\n"+
		"	}\n"+
		"}\n"+
		"</script>\n"+
		"<body bgcolor="+couleurFondEcran+" onBlur=\"window.focus();\" onKeyDown=\"gestionTouche();\">\n"+
		"<table class=\"clsOTable\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
		"<tr><td bgcolor="+couleurFondMois+">\n"+
		"<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
		"<tr>\n	<td bgcolor="+couleurFondMois+"><a href=\"javascript:gotoPrev();\">"+
		"<img src=\""+pathImage+"calPrev.gif\" width=\"20\" height=\"20\" border=\"0\""+
		" alt=\""+txtCalendarMoisPrecedent+"\"></a></td>\n"+
		"	<td bgcolor="+couleurFondMois+" colspan=\"5\">"+
		"<font color="+policeMois+" face="+choixPolice+" size=\"2\">"
		+txtCalendarMois[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</font></td>\n"+
		"	<td bgcolor="+couleurFondMois+" align=\"right\"><a href=\"javascript:gotoNext();\">"+
		"<img src=\""+pathImage+"calNext.gif\" width=\"20\" height=\"20\" border=\"0\""+
		" alt=\""+txtCalendarMoisSuivant+"\"></a></td>\n</tr>\n"
	);

	var dt_current_day = new Date(dt_firstday);
	// affichage des titres jours de semaine
	str_buffer += "<tr>\n";
	for (var n=0; n<7; n++)
		str_buffer += "	<td bgcolor="+couleurFondJour+">"+
		"<font color="+policeMois+" face="+choixPolice+" size=\"2\">"+
		txtCalendarJour[(n_weekstart+n)%7]+"</font></td>\n";
	// affichage table calendrier
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		// affichage colonne
		str_buffer += "<tr>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (dt_current_day.getDate() == dt_datetime.getDate() &&
					dt_current_day.getMonth() == dt_datetime.getMonth())
					// affichage date courante
					str_buffer += "	<td bgcolor="+couleurFondDateCourante+" align=\"right\">";
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// jour de semaine
					str_buffer += "	<td bgcolor="+couleurFondNumeroJourWE+" align=\"right\">";
				else
					// affichage de jour de travail du mois courant 
					str_buffer += "	<td bgcolor="+couleurFondNumeroJour+" align=\"right\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// affichage de jour du mois courant 
					str_buffer += "<a href=\"javascript:window.opener."+str_target1+
					".value='"+calendar_formatage(dt_current_day)+"'; window.opener."+str_target2+
					".value='"+calendar_formatage(dt_current_day)+"'; window.close();\">"+
					"<font color="+policeJour+" face="+choixPolice+" size=\"2\">";
				else
					// affichage de jour des autres mois
					str_buffer += "<a href=\"javascript:window.opener."+str_target1+
					".value='"+calendar_formatage(dt_current_day)+"'; window.opener."+str_target2+
					".value='"+calendar_formatage(dt_current_day)+"'; window.close();\">"+
					"<font color="+policeJourAutreMois+" face="+choixPolice+" size=\"2\">";
				str_buffer += dt_current_day.getDate()+"</font></a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		// affichage pied de page de la colonne
		str_buffer += "</tr>\n";
	}
	//affichage pied de page calendrier
	str_buffer +=
		"</table>\n" +
		"</tr>\n</td>\n</table>\n" +
		"</body>\n" +
		"</html>\n";

	var vWinCal = window.open("", "Calendar",
		"width=200,height=215,status=no,resizable=no,menubar=no,titlebar=no,top=200,left=400");
	vWinCal.opener = self;
	vWinCal.focus();
	var calc_doc = vWinCal.document;
	calc_doc.write (str_buffer);
	calc_doc.close();
}

//*************************************************************************
//
// lorsqu'on tape une date dans le champ input et que l'on clique sur le calendier
// si la date n'est pas au bon format ..message d'erreur et le calendrier 
// s'ouvre a la date du jour 
//
//@param str_datetime : valeur du champ input
//
// Exemple d'utilisation:
// calendar_verifformat(form.date.value)
//
// auteur : Sylvain ROSSANO
//*************************************************************************
function calendar_verifformat (sDatetime) {
	var sReDate = /^(\d+)\/(\d+)\/(\d+)$/;
	var sJourMois = sDatetime.split("/");
	var bFormatIncorrect = false ;

	if ( sJourMois[0] > 31 || sJourMois[1] > 12 ){
		bFormatIncorrect = true;
	}

	if (!sReDate.exec(sDatetime)||(bFormatIncorrect)) {
		dialog_alert(txtCalendarDateInvalide.replace("(0)", sDatetime));
		bFormatIncorrect = false;
		return (new Date());


	}
	else {
	return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1));
	}
}


//*************************************************************************
//
// formate la date pour la mettre dans le champ input
// --> rajoute le 0 devant si le mois ou le jour est inferieur a 10
// --> rajoute le / entre les jour,mois,annee
// --> test si on est au dessu de 31/12/9999..et bloque tout sinon..
//
//@param dt_datetime : date
//
// Exemple d'utilisation:
// var dt_prev_month = new Date;
// calendar_formatage (dt_prev_month)
//
// auteur : Sylvain ROSSANO
//*************************************************************************

function calendar_formatage (dt_datetime) {
	if (dt_datetime.getFullYear() >= 10000) {
		return "31/12/9999";
	} else {
		return (new String (
			(dt_datetime.getDate()<10?"0"+dt_datetime.getDate():dt_datetime.getDate())+"/"+(dt_datetime.getMonth()+1<10?"0"+(dt_datetime.getMonth()+1):dt_datetime.getMonth()+1)+"/"+dt_datetime.getFullYear()));
	}
}
