//==
//========= FONCTIONS GENERALES ===============================================
//==



// Initialisations faite au chargement d'une page
function fpjs_initPage() {
	fpjs_msg(0);
	//gBlocMenu = new xDhtml('blcMenu');
	gBlocErreur = new xDhtml('blcErreur');
	//gObjEvent = new xEvent();
	fpjs_setSaisie();
}

// A exécuter à un changement de page
function fpjs_quitter() {
	if (gPopUpWin != null && !gPopUpWin.closed) gPopUpWin.close();
}

// Afficher/masquer les onglets
function fpjs_switchOnglet(sNom) {
	var Bloc = document.getElementById(sNom).style;
	var Puce = eval('document.puce_'+sNom);
	if (Bloc.display == 'none') {
		//Puce.src = 'images/puceOngletOuvert.gif';
		Puce.src = 'images/fc1_menugauche.gif';
		Bloc.display = 'block';
	}else{
		//Puce.src = 'images/puceOngletFermer.gif';
		Puce.src = 'images/fc1_menugauche.gif';
		Bloc.display = 'none';
	}
}

function fpjs_switchOnglet2(sNom) {
	var Bloc = document.getElementById(sNom).style;
	var Puce = eval('document.puce_'+sNom);
	if (Bloc.display == 'none') {
		//Puce.src = 'images/planMoins.gif';
		Puce.src = 'images/spacer.gif';
		Bloc.display = 'block';
	}else{
		//Puce.src = 'images/planPlus.gif';
		Puce.src = 'images/spacer.gif';
		Bloc.display = 'none';
	}
}

// Afficher/masquer tous les onglets
function fpjs_switchAllOnglets(bDisplay) {
	if (bDisplay) {
		var Display = 'block';
		//var imagePuce2 = 'images/puceOngletOuvert.gif';
		var imagePuce2 = 'images/fc1_menugauche.gif';
	}else{
		var Display = 'none';
		//var imagePuce2 = 'images/puceOngletFermer.gif';
		var imagePuce2 = 'images/fc1_menugauche.gif';
	}

	var Tags = document.getElementsByTagName('div');
	var iMax = Tags.length;
	for (var i = 0; i < iMax; i++) {
		if (Tags.item(i).id.indexOf('onglet') == 0) {
			var Puce2 = eval('document.puce_'+Tags.item(i).id);
			Puce2.src=imagePuce2;
			Tags.item(i).style.display = Display;
		}
	}
}


// Fenêtre popup pour download pièces jointes
// La fenêtre est affichée en dehors des limites de visibilité de l'écran
// Si download direct : bug : le formulaire n'est plus acessible avec JS ???
function fpjs_downloadDoc(sType, sUrl) {
	sUrl = '../commun/download.php?T=' + sType + '&F=' + sUrl;
	fpjs_popUp(sUrl,10,10,5000,5000);
}

// Affichage du menu quand on passe sur le logo
function fpjs_menuOver() {
	if (gBlocMenu.getVisible()) return;
	gBlocMenu.setVisible(1);
	if (gMenuTop == null) {
		gMenuTop = 0;//96; //gBlocMenu.getTop();
		gMenuLeft = 10; //gBlocMenu.getLeft();
		gMenuWidth = gBlocMenu.getWidth() + gMenuLeft;
		gMenuHeight = gBlocMenu.getHeight() + gMenuTop + 96;
	}
	gObjEvent.add('mousemove', fpjs_docOver);
}

// Gestion de la position de la souris pour afficher/masquer le menu
function fpjs_docOver(e) {
	var x = gObjEvent.pageX(e);
	var y = gObjEvent.pageY(e);
	if ((x < gMenuLeft) || (x > gMenuWidth) || (y < gMenuTop) || (y > gMenuHeight)) {
		gBlocMenu.setVisible(0);
		gObjEvent.add('mousemove', null);		
	}
}

// Affichage d'une fenêtre popup avec un calendrier perpétuel
function fpjs_calendrier(sDate, sNomZone) {
	if (gObjCalendrier == null) {
		gObjCalendrier = new oCalendrier('gObjCalendrier');
	}
	gObjCalendrier.setZone(sNomZone);
	gObjCalendrier.affiche(sDate);
}

// Gestion de la couleur de fond d'une liste au passage de la souris
function fpjs_listeRollOver(oObj, bOver) {
	var Style = oObj.style;
	if (bOver) {
		gCouleurLigne = Style.backgroundColor;		
		//Style.backgroundColor = '#CBF100';
		Style.backgroundColor = '#FEBE3A';
		Style.cursor = 'hand';
	} else {
		Style.backgroundColor = gCouleurLigne;
		Style.cursor = 'default';
	}	
}

// Positionnement de la souris sur une zone en erreur
function fpjs_erreurZone(sZone) {
	var E =	document.forms[0].elements[sZone];
	//E.style.backgroundColor = '#CBF100';
	E.style.backgroundColor = '#FEBE3A';
	E.focus();	
}

// Fenêtre popup pour aide dans recherche
function fpjs_aideRecherche() {
	sUrl = '../commun/aiderecherche.htm';
	fpjs_popUp(sUrl, 600, 500);
}

// Ouverture d'une fenêtre popup
// sUrl : url de la page affichée dans la fenêtre
// nLarge : largeur de la fenêtre. Si null = 3/4 de la largeur de l'écran
// nHaut : hauteur de la fene^tre. Si null = 3/4 de la hauteur de l'écran
// nLeft : position gauche de la fenêtre. Si null = centrage horizontal
// nTop : position haute de la fenêtre. Si null = centrage vertical
function fpjs_popUp(sUrl, nLarge, nHaut, nLeft, nTop) {
	if (gPopUpWin != null && !gPopUpWin.closed) gPopUpWin.close();
	if (nLarge == null) nLarge = (screen.width * 3) / 4;
	if (nHaut == null) nHaut = (screen.height * 3) / 4;
	if (nLeft == null) nLeft = ((screen.width - nLarge) / 2) - 10;
	if (nTop == null) nTop = ((screen.height - nHaut) / 2) - 30;
	var sOption = 'scrollbars,resizable,height=' + nHaut + ',width=' + nLarge
		+ ',left=' + nLeft + ',screenX=' + nLeft + ',top=' + nTop + ',screenY=' + nTop;
	gPopUpWin = window.open(sUrl, 'tbatgpop', sOption);
	gPopUpWin.focus();
}

// Gestion des redirections
function fpjs_redirect(sUrl, sParam) {            
	var F = document.forms[0];
	if (sUrl != null) {
		if (sUrl == 'X') {  
			// Redirection sur une page appelante
			// on remet les différents zones cachées de la page appelante
			// qui était stockée dans la zone X sous la forme
			// lib=url|ID|Z=valeur|Z=valeur|....
			sParam = F.X.value;
			var i = sParam.indexOf('|');
			sUrl = sParam.substring(sParam.indexOf('=') + 1, i);
			i = sParam.indexOf('|', i + 1);
			sParam = sParam.substring(i + 1);
		}	
		F.action = sUrl;
	}
	if (sParam != null) {
		// sParam est une chaînes de la forme
		// Z=valeur|Z=valeur|....
		// Z est le nom d'une zone de formulaire (1 seul caractère)
		// valeur est la valeur de la zone
		var Params = sParam.split('|');
		var iMax = Params.length;
		for (var i = 0; i < iMax; i++) {
			if (Params[i] != '') F.elements[Params[i].substring(0,1)].value = Params[i].substring(2);			
		}
	}			
	F.submit();
}
// Positionne le curseur dans la première zone de saisie d'un formulaire
function fpjs_setSaisie() {
	if (document.forms.length == 0) return;
	var E = document.forms[0].elements
	var iMax = E.length;
	for (var i = 0; i < iMax; i++) {
		if (E[i].disabled) continue;
		if (E[i].readOnly) continue;	
		var Type = E[i].type.toLowerCase();
		if (Type == 'hidden') continue;
		if (Type == 'button') continue;
		if (Type == 'submit') continue;
		if (Type == 'reset') continue;
		if (Type.indexOf('select') != -1) continue;
		try {
			E[i].focus();
		} catch(e) {
			continue;
		}
		return;
	}
}

// Positionne le curseur sur une zone en erreur
function fpjs_focusErreur(sNomZone) {
	try {
		document.forms[0].elements[sNomZone].focus();
	} catch(e) {
		return;
	}
}

// Contrôle de saisie des formulaires
function fpjs_verifForm(F, OKs) {
	var E = F.elements
	for (Nom in OKs) {
		if (!OKs[Nom][0].test(E[Nom].value)) {
			alert (OKs[Nom][1]);
			E[Nom].focus();
			return false;
		}	
	}
	return true;
}

function fpjs_verifDate(laZone) {
	var Reg = /^\d{2}\/\d{2}\/\d{4}$/
	if (!laZone.match(Reg)) return -1

	var JJ = laZone.substring(0,2)
	var MM = laZone.substring(3,5)
	var AA = laZone.substring(6)

	if (AA < 2001) return -2
	if (MM < 1 || MM > 12) return -2
	if (JJ < 1 || JJ > 31) return -2
	if (JJ > 30 && (MM == 4 || MM == 6 || MM == 9 || MM == 11)) return -2
	if (MM == 2) {
		Reg = ((AA % 4 == 0) && ( (!(AA % 100 == 0)) || (AA % 400 == 0) ) ) ? 29 : 28
		if (JJ > Reg) return -2
	}
	return 1
}

// Arrondi un nombre
function fpjs_arrondi(Nombre,Decimales) {
  if (!Number(Nombre)) return 0;
  if (Nombre == 0 || Decimales < 1) return 0
  var Multiplie = Math.pow(10,Decimales)
  Nombre = (Nombre * Multiplie) + 0.5
  Nombre = Math.floor(Nombre)
  Nombre = Nombre / Multiplie
  return Nombre
}

// Renvoie un nombre formaté (2 décimales)
function fpjs_formatNombre(Nombre) {
	if (!Number(Nombre)) return '0.00';
	if (Nombre < 0) return Nombre;
	Nombre = '' + (Nombre * 100);
	if (Nombre < 10) Nombre = '00' + Nombre
	else if (Nombre < 100) Nombre = '0' + Nombre
	return Nombre.substring(0, Nombre.length - 2)+ '.'+Nombre.substring(Nombre.length - 2);
}

// Affichage message dans la barre de status
function fpjs_msg(nMsg) {
	if (nMsg == gLastMsg) return true;
	if (!isNaN(nMsg)) {
		var sMsg = gMsg[nMsg];
		gLastMsg = nMsg;
	} else {
		var sMsg = nMsg;
		gLastMsg = null;
	}
	window.status = sMsg;
	return true;
}

//==
//========= OBJETS ============================================================
//==
// Spécial NS 4
function xGetBloc(oDoc, Nom) {
	for (var i = 0; i < oDoc.layers.length; i++) {
		if (oDoc.layers[i].id == Nom) {
			return oDoc.layers[i]
		} else {
			var Bloc = xGetBloc(oDoc.layers[i].document, Nom)
			if (Bloc != null) return Bloc
		}
	}
	return null
}

/**
* Objet xDHTML - Gestion des blocs
*/
function xDhtml(NomBloc) {
	// Propriétés
	if (document.layers) {
		this.xMontrer = 'show'; this.xCacher = 'hide'
		this.xUnite = ''
	} else {
		this.xMontrer = 'visible'; this.xCacher = 'hidden'
		this.xUnite = 'px'
	}
	if (document.getElementById) {
		this.leBloc = document.getElementById(NomBloc)
		//this.leStyle = this.leBloc.style
	} else  if (document.all) {
		this.leBloc = document.all[NomBloc]
		this.leStyle = this.leBloc.style
	} else if (document.layers) {
		this.leBloc = this.leStyle = xGetBloc(document, NomBloc)
	} else {
		this.leBloc = this.leStyle = null
	}

	return this;
}

xDhtml.prototype.setVisible = function(Quoi) { 
	if (!this.leStyle) return
	if (Quoi == 0) this.leStyle.visibility = this.xCacher
	else if (Quoi == 1) this.leStyle.visibility = this.xMontrer
	else if (Quoi == 2) {
		if (this.leStyle.visibility == this.xMontrer) this.leStyle.visibility = this.xCacher
		else this.leStyle.visibility = this.xMontrer
	}
}

xDhtml.prototype.getVisible = function () {
	if (this.leStyle) return (this.leStyle.visibility == this.xMontrer)
	else return 0
}

xDhtml.prototype.getTop = function () {
	if (this.leStyle) return parseInt(this.leStyle.pixelTop)
	else return 0
}

xDhtml.prototype.getLeft = function () {
	if (this.leStyle) return parseInt(this.leStyle.pixelLeft)
	else return 0
}

xDhtml.prototype.getWidth = function () {
	if (document.getElementById || document.all) return this.leBloc.scrollWidth
	if (document.layers) return this.leBloc.document.width
	return 0
}

xDhtml.prototype.getHeight = function () {
	if (document.getElementById || document.all) return this.leBloc.scrollHeight
	if (document.layers) return this.leBloc.document.height
	return 0
}

xDhtml.prototype.moveTo = function (nX,nY) {
	if (this.leStyle) {
		this.leStyle.left = nX + this.xUnite
		this.leStyle.top = nY + this.xUnite
	}
}

/**
* Objet xEvent - Gestion des événements
*/
function xEvent() {
	// Déterminer le navigateur
	this.I5 = this.I4 = this.IE = false;
	this.N4 = this.N6 = this.NS = false
	if (document.all && document.getElementById) this.I5 = true
	else if (document.all) this.I4 = true
	else if (document.layers) this.N4 = true
	else if (document.getElementById) this.N6 = true
	this.IE = this.I4 || this.I5
	this.NS = this.N4 || this.N6
	
	return this
}   

xEvent.prototype.add = function(Nom, Fonction) {
	if (this.IE) {
		eval('document.on' + Nom + '= Fonction')
	} else if (this.N6) {
		document.addEventListener(Nom ,Fonction ,true)
	} else if (oE.N4) {
		eval('document.captureEvents(Event.' + Nom.toUpperCase() + ')')
		eval('document.on' + Nom + ' = Fonction')
	}
}

xEvent.prototype.pageX = function(e) {
	if (this.IE) return event.clientX + document.body.scrollLeft
	else if (this.NS) return e.pageX
	return 0;
}
xEvent.prototype.pageY = function(e) {
	if (this.IE) return event.clientY + document.body.scrollTop
	else if (this.NS) return e.pageY
	return 0;
}   
xEvent.prototype.offsetX = function(e) {
  if (this.IE) return event.offsetX 
  else if (this.N6) return e.offsetX
  else if (this.N4) return e.layerX
  return 0;
}
xEvent.prototype.offsetY = function(e) {
  if (this.IE) return event.offsetY 
  else if (this.N6) return e.offsetY
  else if (this.N4) return e.layerY
  return 0;
}   

/**
* Objet oCalendrier - Calendrier perpétuel
*/

function oCalendrier(sNomObjet) {
	this.NomObjet = sNomObjet;
	this.Zone = null;
	this.Fenetre = null;
	this.Mois = 0;
	this.Annee = 0;
	this.JourEnCours = 0;
	this.MoisEnCours = 0;
	this.AnEnCours = 0;
	var d = new Date()
	this.AnneePivot = d.getFullYear();
	this.NomMois = new Array('Janv','Fév','Mars','Avr','Mai','Juin','Juil','Aout','Sept','Oct','Nov','Déc');
	this.NbJour = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	//this.NomJour = new Array('Dim','Lun','Mar','Mer','Jeu','Ven','Sam');
	this.NomJour = new Array('L','M','M','J','V','S','D');
	
	return this;
}

oCalendrier.prototype.setZone = function(sNomZone) {
	this.NomZone = sNomZone;
}

oCalendrier.prototype.click = function(nJour) {
	if (this.NomZone != null) {
		this.NomZone.value = ( (nJour < 10) ? '0':'' ) + nJour + '/'
							+ ( ((this.Mois + 1) < 10) ? '0':'' ) + (this.Mois + 1) + '/'
							+ this.Annee;
	}
	this.Fenetre.close();
}

oCalendrier.prototype.affiche = function(nQuoi, sNomZone) {
	if (nQuoi == -1) {
		this.Mois --;
		if (this.Mois < 0) {
			this.Mois = 11;
			this.Annee --;
		}
	} else if (nQuoi == -2) {
		this.Mois ++;
		if (this.Mois > 11) {
			this.Mois = 0;
			this.Annee ++;
		}
	} else if (nQuoi == null) { // change mois ou annee
		var f = this.Fenetre.document.forms[0];
		this.Mois = parseInt(f.selMois.options[f.selMois.selectedIndex].value);
		this.Annee = parseInt(f.selAnnee.options[f.selAnnee.selectedIndex].value);
	} else if (nQuoi == '' || nQuoi == 0) {  //Init sur la date du jour
		var d = new Date()
		this.MoisEnCours = d.getMonth()
		this.AnEnCours = d.getFullYear()
		this.JourEnCours = d.getDate()
		this.Annee = this.AnEnCours;
		this.Mois = this.MoisEnCours;
	} else {  // recoit une date aaaammjj
		this.AnEnCours = parseInt(nQuoi.substring(0,4));
		this.MoisEnCours = parseInt(nQuoi.substring(4,6)) - 1;
		this.JourEnCours = parseInt(nQuoi.substring(6));
		this.Annee = this.AnEnCours;
		this.Mois = this.MoisEnCours;
	}
	if (sNomZone != null) this.NomZone = sNomZone;
	this.ouvreFenetre();
	this.afficheFenetre();
}

oCalendrier.prototype.ouvreFenetre = function() {
	if (this.Fenetre == null || this.Fenetre.closed) {
		var sOption = 'resizable, width=160,height=160';
		nLeft = ((screen.width - 160) / 2) - 10;
		nTop = ((screen.height - 160) / 2) - 30;
		sOption += ',left=' + nLeft + ',screenX=' + nLeft;
		sOption += ',top=' + nTop + ',screenY=' + nTop;
		this.Fenetre = window.open('', 'tbatgcal', sOption);
	}
	this.Fenetre.focus();
}

oCalendrier.prototype.afficheFenetre = function() {
	//if (this.Annee == this.AnEnCours && this.Mois == this.MoisEnCours) var CeJour = this.JourEnCours;
	//else var CeJour = -1;
	var CeJour = this.JourEnCours;
	var Html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
		+ '<html><head><title>Calendrier</title>'
		+ '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'
		+ '<style type="text/css">'
		+ 'a {text-decoration: none; color: #000}'
		+ '.caltable {border-bottom: 1px #000 solid;border-right: 1px #000 solid;}'
		+ '.caltd, .calautremois, .caljour, .calcejour, .calweekend {font: 11px Verdana, sans-serif; border-top: 1px #000 solid; border-left: 1px #000 solid; padding: 2px}'
		+ '.calautremois {color: #666666;}'
		+ '.caljour {background-color: #E4FB95}'
		+ '.calcejour {background-color: #cbf100}'
		+ '.calweekend {background-color: #cccccc}'
		+ '.big {font: bold 17px Verdana}'
		+ 'select, option {font: 8pt Verdana, sans-serif; background-color: #E4FB95; }'
		+ '</style>'
		+ '<script language="javascript">'
		+ 'function fpjs_rollOver(oObj, bOver) {'
		+ '	var Style = oObj.style;'
		+ '	if (bOver) {'
		+ '		gCouleurLigne = Style.backgroundColor;'
		+ '		Style.backgroundColor = "#CBF100";'
		+ '	} else {'
		+ '		Style.backgroundColor = gCouleurLigne;'
		+ '	}'
		+ '}'
		+ '</sc'
		+ 'ript></head><body><form>'
		+ '<table cellspacing="0" border="0" cellpadding="0" align="center"><tr><td class="big">'
		+ '<a href="javascript:opener.' + this.NomObjet + '.affiche(-1)">&lt;</a></td>'
		+ '<td align="center" nowrap><select name="selMois" '
		+ 'onchange="opener.' + this.NomObjet + '.affiche(null)">';

	for (var i = 0; i < 12; i++) {
		Html += '<option value="' + i +'"';
		if (i == this.Mois) Html += ' selected';
		Html += '>' + this.NomMois[i] + '</option>';
	}
	Html += '</select><select name="selAnnee" '
		+ 'onchange="opener.' + this.NomObjet + '.affiche(null)">';

	if (this.Annee < this.AnneePivot) var Debut = this.Annee - 10;
	else var Debut = this.AnneePivot - 10;
	if (this.Annee > this.AnneePivot) var Fin = this.Annee + 11;
	else var Fin = this.AnneePivot + 11;
	for (var i = Debut; i < Fin; i++) {
		Html += '<option value="' + i +'"';
		if (i == this.Annee) Html += ' selected';
		Html += '>' + i + '</option>';
	}
	Html += '</select></td>'
		+ '<td class="big"><a href="javascript:opener.' + this.NomObjet + '.affiche(-2)">&gt;</a></b></td>'
		+ '</td></tr>';

	// Ligne 2 : le calendrier
	var Debut = new Date(this.Annee,this.Mois,1);
	Debut = Debut.getDay() - 1;
	if (Debut < 0) Debut = 6
	// test année bisex
	this.NbJour[1] = 28;
	if (((this.Annee % 4 == 0) && (this.Annee % 100 != 0)) || (this.Annee % 400 == 0)) this.NbJour[1] = 29; 
	var Colonne = 0;
	var DernierMois = this.Mois - 1;
	if (DernierMois < 0) DernierMois = 11;
	
	Html +='<tr><td align="center" colspan="3">'
		+ '<table cellspacing="0" class="caltable">'
		+'<tr bgcolor="#FFFF99">';
		
	for (i=0; i<7; i++) {
		Html += '<td class="caltd"><b>' + this.NomJour[i] +'</b></td>';
	}
	Html += '</tr>'
		+ '<tr align="center">';
		
	// Derniers jours du mois précédent
	for (i = 0; i < Debut; i++, Colonne++){
		Html += '<td class="calautremois">' + (this.NbJour[DernierMois] - Debut + i + 2) + '</td>';
	}
	// Jours du mois
	for (i = 1; i <= this.NbJour[this.Mois]; i++, Colonne++) {
		if (i == CeJour) Html += '<td class="calcejour" ';
		else if(Colonne < 5) Html += '<td class="caljour" ';
		else Html += '<td class="calweekend" ';
		
		Html += 'onmouseover="fpjs_rollOver(this, true)" '
			+ 'onmouseout="fpjs_rollOver(this, false)">'
			+ '<a href="javascript:opener.' + this.NomObjet + '.click(' + i + ')">'
			+ i +'</a></td>';
		if (Colonne == 6) {
			Html += '</tr><tr align="center">';
			Colonne = -1;
		}
	}
  
	// Premiers jours du mois suivant
	if (Colonne > 0) {
		for (i = 1; Colonne < 7; i++, Colonne++){
			Html += '<td class="calautremois">' + i + '</td>';
		}
	}

	Html += '</tr></table>'
		+ '</td></tr></table></form></body></html>';

	with(this.Fenetre.document) {
		open()
		write(Html);
		close();
	}	
}

//==
//========= INITIALISATIONS ===================================================
//==
var gPopUpWin = null;
var gPopUpUrl = '';
var gLastMsg = '-1';
var gCouleurLigne = null;
var gBlocMenu = null;	// div contenant le menu
var gBlocErreur = null;  // div contenant les messages d'erreurs
var gObjEvent = null;
var gMenuTop = null;
var gMenuLeft = null;
var gMenuWidth = null;
var gMenuHeight = null;
var gObjCalendrier = null;

// Messages types
var gMsg = new Array();
gMsg[0] = "";
gMsg[1] = "Trier la liste suivant cette colonne";
gMsg[2] = "Modifier ou supprimer l'élément";
gMsg[3] = "Saisir des critères de recherche";
gMsg[4] = "Liste des éléments trouvés";
gMsg[5] = "Saisir des informations";
gMsg[6] = " ";
gMsg[7] = "Afficher/masquer le menu";
gMsg[8] = "Calendrier perpétuel";
gMsg[9] = "Cliquez pour vous positionner sur l'erreur";

onload = fpjs_initPage;
onunload = fpjs_quitter;