﻿// Objet de gestion des erreurs



var err_shownErrorField = null;
var err_lastShownMessageId = null;

// Constructeur
function errorManager(defaultBoxTitle, defaultBoxButtonLabel) {
	if(!err_showDiv) { alert("Erreur: les functions de dhtml.js est requis pour l'utilisation de errorManager"); return null; }
	this.errors = new Array(); // Objets d'erreur
	this.length = 0; // Nombre d'erreurs
	this.texts = new Object();
	
	// Textes par défaut
	this.texts.defaultTitle = defaultBoxTitle;
	this.texts.defaultButtonLabel = defaultBoxButtonLabel;
	
	// Création du background de popup
	if(!document.getElementById('popupBG')) document.write('<div id="popupBG" class="popupBG" style="display:none; position:fixed;"></div>');
	if(!document.getElementById('messageBox')) document.write('<div id="messageBox" class="popupMessageBox" style="position:fixed;"><table width="100%" style="height:100%" cellpadding="0" cellspacing="0" border="0"><tr><td align="center" valign="middle"><table cellpadding="0" cellspacing="0" border="0" class="messageBoxContentTable"><tr><td id="popupMessageBoxTitle" class="popupMessageBoxTitle">' + this.texts.defaultTitle + '</td></tr><tr><td id="popupMessageBoxBody" class="popupMessageBoxBody"></td></tr><tr><td id="popupMessageBoxFooter" class="contentSubOptions"><a href="javascript:err_closeErrorMessage()" id="popupMessageBoxButton" name="popupMessageBoxButton">' + this.texts.defaultButtonLabel + '</a></td></tr></table></td></tr></table></div>');
	
	// Méthodes de l'objet
	this.setText = err_setText;
	this.showErrorMessage = err_showErrorMessage;
	this.closeErrorMessage = err_closeErrorMessage;
	this.showMessageBox = err_showMessageBox;
	this.closeMessageBox = err_closeMessageBox;
	this.addError = err_addError;
	this.removeError = err_removeError;
}



// Texte custom
function err_setText(txtID, newText) {
	this.texts[txtID] = newText;
}



// Affichage d'un message d'erreur
function err_showErrorMessage(msgOrId, title, buttonLabel) {
	if(this == window || this == document) { 
		if(typeof(msgOrId) != "number") {
			err_showDiv("popupBG","block");
			alert(msgOrId);
			err_hideDiv("popupBG");
		} else {
			alert("Error: the showErrorMessage method must be called from an instance of errorManager (" + msgOrId + ").");
		}
		return;
	}
	if(typeof(msgOrId) == "number") {
		err_shownErrorField = this.errors[msgOrId].field;
		if(this.errors[msgOrId].title) title = this.errors[msgOrId].title;
		if(this.errors[msgOrId].buttonLabel) buttonLabel = this.errors[msgOrId].buttonLabel;
		msgOrId = this.errors[msgOrId].message;
	}
	err_showDiv("popupBG","block");
	
	// Textes
	if(title) document.getElementById("popupMessageBoxTitle").innerHTML = title;
	else document.getElementById("popupMessageBoxTitle").innerHTML = this.texts.defaultTitle;
	document.getElementById("popupMessageBoxBody").innerHTML = msgOrId;
	if(buttonLabel) document.getElementById("popupMessageBoxButton").innerHTML = buttonLabel;
	else document.getElementById("popupMessageBoxButton").innerHTML = this.texts.defaultButtonLabel;
	
	err_showDiv("messageBox","block");
}

function err_closeErrorMessage() {
	err_hideDiv("popupBG");
	err_hideDiv("messageBox");
	if(err_shownErrorField != null) err_shownErrorField.focus();
	err_shownErrorField == null;
}



// Affichage d'une boîte de message personnalisée
function err_showMessageBox(boxId) {
	err_showDiv("popupBG","block");
	err_setStyleProp(err_getDiv(boxId), "position", "absolute");
	err_setStyleProp(err_getDiv(boxId), "zIndex", "101");
	err_lastShownMessageId = boxId;
	err_showDiv(boxId,"block");
}

function err_closeMessageBox(boxId) {
	err_hideDiv("popupBG");
	if(boxId) err_hideDiv(boxId);
	else err_hideDiv(err_lastShownMessageId); // Auto.
}
	
		
		
// Ajout d'une erreur
function err_addError(errorField, message, title, buttonLabel) {
	var placeAtIndex=this.errors.length;
	for(var i=0; i<this.errors.length; i++) {
		if(this.errors[i].field == errorField) {
			placeAtIndex = i;
			break;
		}
	}
	if(title == null || title == undefined) title = this.texts.defaultTitle;
	if(buttonLabel == null || buttonLabel == undefined) buttonLabel = this.texts.defaultButtonLabel;
	this.errors[placeAtIndex] = new Object();
	this.errors[placeAtIndex].field = errorField;
	this.errors[placeAtIndex].message = message;
	this.errors[placeAtIndex].title = title;
	this.errors[placeAtIndex].buttonLabel = buttonLabel;
	errorField.isErrorous = true;
	this.length = this.errors.length;
}

// Retrait d'une erreur
function err_removeError(errorField) {
	for(var i=0; i<this.errors.length; i++) {
		if(this.errors[i].field == errorField) {
			this.errors.splice(i,1);
			break;
		}
	}
	errorField.isErrorous = false;
	this.length = this.errors.length;
}




/* Fonctions DHTML, contenues pour éviter les confilts */
	
	// Affichage d'un div
	function err_showDiv(id, displayVal){
		if(displayVal == null || displayVal == "" || displayVal == undefined) displayVal = "inherit"; // Défaut
		err_setStyleProp(err_getDiv(id), "display", displayVal);
	}
	
	// Masquage d'un div
	function err_hideDiv(id){
		err_setStyleProp(err_getDiv(id), "display", "none");
	}

	
	// ----- Gestion CSS
	function err_setStyleProp(obj, propName, newVal) {
		if(!obj) return;
		try {
			if(!obj.style) obj[propName] = newVal; // Old browser
			else obj.style[propName] = newVal; // Browser récent
		} catch(e) {
		}
	}
	
	// Retourne la référence au layer (div) demandé
	function err_getDiv(id){
		if(document.getElementById) return document.getElementById(id);
		if(document.layers) return document.layers[id];
		if(document.all) return document.all[id];
		return null;
	}