/*********************************************************************************************
 * Appel une fonction au chargement de la page                                               *
 * Source :                                                                                  *
 * http://www.sitepoint.com/blogs/2004/05/26/closures-and-executing-javascript-on-page-load/ *
 *********************************************************************************************/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() { oldonload(); func(); };
	}
}

/**********************************************
 * Ajoute des / dans une chaine de caractères *
 **********************************************/
function addslashes(str) {
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}

/*********************************************
 * Supprime les / d'une chaine de caractères *
 *********************************************/
function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}

/************************
 * Fonctions Macromedia *
 ************************/
function MM_jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!==0){ d.MM_p[j]=new Image(); d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/*********************************************
 * Charge un message dans la barre de status *
 *********************************************/
function statusMsg(msg) {
	window.status = msg;
	return true;
}

/**************************
 * Appel perso de Lytebox *
 **************************/
function startLytebox(id, href, title, rel, rev) {
	var el		= document.getElementById(id);
	el.href		= href;
	el.title	= title;
	el.rel		= rel;
	if (rev !== '') { el.rev = rev; }
	if (rel !== '') {
		switch(rel) {
			case "lyteframe": myLytebox.start(el, false, true); break;
			case "lytebox": myLytebox.start(el, false, false); break;
			case "lyteshow": myLytebox.start(el, true, false); break;
		}
	}
}

/***************************************************
 * Affiche la description d'un élément dans un div *
 ***************************************************/
function setDescription(id, texte) {
	if (id !== '') {
		var id = document.getElementById(id);
		if (texte !== '') {
			id.innerHTML=texte;
			id.style.visibility = 'visible';
		} else {
			id.style.visibility = 'hidden';
		}
	}
}

/*********************
 * Popup personalisé *
 *********************/
function newWindow(src, title, mode, w, h) {
	var options = 'resizable=yes, location=yes, menubar=no, scrollbars=yes, status=yes, titlebar=yes, toolbar=no, dependent=yes, alwaysRaised=yes';
	if (mode == 'full') {
		window.open(src, title);
	}
	if (mode == 'popup') {
		window.open(src, title, 'width=' + w + ' , height=' + h + ', ' + options);
	}
}

/*****************************
 * Affiche / cache un calque *
 *****************************/
function switchLayer(id, mode) {
	if (id !== '') {
		var id = document.getElementById(id);
		if (id.style.display) {
			if (mode !== '') {
				if (mode == 'inline') {
					switch (id.style.display) {
						case 'inline': id.style.display = 'none'; break;
						case 'none': id.style.display = 'inline'; break;
					}
				}
				if (mode == 'block') {
					switch (id.style.display) {
						case 'block': id.style.display = 'none'; break;
						case 'none': id.style.display = 'block'; break;
					}
				}
			} else {
				switch (id.style.display) {
					case 'block': id.style.display = 'none'; break;
					case 'none': id.style.display = 'block'; break;
				}
			}
		} else {
			switch (id.style.visibility) {
				case 'visible': id.style.visibility = 'hidden'; break;
				case 'hidden': id.style.visibility = 'visible'; break;
			}
		}
	}
}
function switchLayerMulti(id, total, scriptaculous, effect) {
	if (id !== '') {
		if (scriptaculous == 1) {
			var id = document.getElementById(id);
			if (total !== '') {
				for (i=1; i<=total; i++) {
					if (effect !== '') { new Effect.toggle(id + i, effect); }
				}
			} else {
				if (effect !== '') {
					new Effect.toggle(id, effect);
				} else {
					switchLayer(id);
				}
			}
		} else {
			var id = document.getElementById(id);
			if (total !== '') {
				for (i=1; i<=total; i++) {
					switchLayer(id + i);
				}
			} else {
				switchLayer(id);
			}
		}
	}
}

/************************************
 * Recupère la résolution du client *
 ************************************/
function getScreenSize() {
	var returnString = "";
	returnString += "Taille d'écran : " + screen.width + " x " + screen.height + "\n";
	returnString += "Taille d'écran sans les barres : " + screen.availWidth + " x " + screen.availHeight + "\n";
	returnString += "Taille dispo dans le navigateur : " + document.body.clientWidth + " x " + document.body.clientHeight + "\n";
	alert(returnString);
}
function resizePage(path, min, med, max) {
	if (path !== '' && min !== '' && med !== '' && max !== '') {
		var pageSize = '';
		if (screen.width >= max) { pageSize = max; }
		else if (screen.width >= med) { pageSize = med; }
		else { pageSize = min; }
		document.write('<link rel="stylesheet" href="' + path + '/site-' + pageSize + '.css" type="text/css" \/>');
	} else {
		alert('Paramètres non définis !');
	}
}

/*******************************
 * Fonction d'ajout au favoris *
 *******************************/
function favoris(title, url) {
	if (navigator.appName != 'Microsoft Internet Explorer'){
		window.sidebar.addPanel(title,url,"");
	} else {
		window.external.AddFavorite(url,title);
	}
}

/****************************************************
 * Fonction de vérification du nombre de caractères *
 ****************************************************/
function checkLen(id, max, output) {
	if (id !== '' && max !== '') {
		var id = document.getElementById(id);
		var strLen = id.value.length;
		var output = (!output) ? document.getElementById('checkMax') : document.getElementById(output);
		if (strLen > max) {
			id.value = id.value.substring(0,max);
			charsLeft = max;
			output.style.color = 'red';
		} else {
			charsLeft = strLen;
			output.style.color = 'black';
		}
		var charsMax = max - charsLeft;
		output.innerHTML = charsMax;
	} else {
		alert('Vous devez définir une valeur max');
	}
}

/*********************************************
 * Fonction d'affichage message d'erreur sql *
 *********************************************/
function genErrSQL(err_num, err_msg, query_name, query) {
	if (err_num !== '' && query_name !== '' && query !== '') {
		var myAlert = '';
		myAlert += 'Erreur n° : ' + err_num + '\r\n';
		myAlert += 'Erreur Message :\n' + err_msg + '\r\n';
		myAlert += 'Requete [' +  query_name + '] :\n';
		myAlert += query;
		alert(myAlert);
	} else {
		alert('Paramètres non définis !');
	}
}