function makeForUrl(vare,value,starter) {

	if (useTrendyURL) {
		if ((vare=="id") || (vare=="?id"))
			return value;
		else
			return ((starter)?'/':'')+vare+":"+value;
	} else
		return ((starter)?'&':'')+vare+"="+value;
}

function IE_CorrectAlpha_PNG(){
	for(i=0; i<document.images.length; i++){
		img    = document.images[i];
		imgExt  = img.src.substring(img.src.length-3, img.src.length);
		imgExt  = imgExt.toUpperCase();
		if (imgExt == "PNG"){
			imgID    = (img.id) ? "id='" + img.id + "' " : "";
			imgClass= (img.className) ? "class='" + img.className + "' " : "";
			imgTitle= (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			imgStyle= "display:inline-block;" + img.style.cssText;
			if (img.align == "left") { imgStyle = "float:left;"  + imgStyle; } else if (img.align == "right"){ imgStyle = "float:right;" + imgStyle; }
			if (img.parentElement.href)   { imgStyle = "cursor:hand;" + imgStyle; }       
			strNewHTML    = '<span '+imgID+imgClass+imgTitle+' style="width:'+img.width+'px; height:'+img.height+'px;'+imgStyle+';'+'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+img.src+'\', sizingMethod=\'scale\');"></span>';
			img.outerHTML = strNewHTML;
			i = i-1;
		}
	}
}
//if (navigator.userAgent.indexOf('MSIE') != -1) window.attachEvent("onload", IE_CorrectAlpha_PNG);


function validateForm() { //argument facultatif : nom du formulaire
	if (arguments.length>=1) {
		var x = document.forms[arguments[0]].elements;
		
	} else {
		var x = document.getElementsByTagName("input");
	}
	var msg='';
	var tmp='';
	var msgF='';
	var format=true;
	for (i = 0; i < x.length; i++) {	
		if ((x[i].title != '') && (x[i].title != null) && (x[i].style.display != 'none')) {
			if ((x[i].title.lastIndexOf(')') == x[i].title.length-1) || (x[i].title.lastIndexOf('(') == 0)) {
				tmp=x[i].title.substring(x[i].title.lastIndexOf('(')+1,x[i].title.lastIndexOf(')'));
				var vnospace=replace(x[i].value,' ','');
				if (((tmp=='INT') || (tmp=='MNT')) && (isNaN(vnospace))) {
					format=false;
					msgF=msg_formatnum;
				}
				if ((tmp=='CAR') && (!isNaN(x[i].value))) {
					format=false;
					msgF=msg_formatalphanum;
				}
				if (tmp=='DAT') {
					if (!isNaN(x[i].value)) {
						format=false;
						msgF=msg_formatdate;
					} else if (x[i].value.length!=10) {
						format=false;
						msgF=msg_formatdate;
					} else {
						if (
							   (isNaN(x[i].value.charAt(0)+x[i].value.charAt(1)))
							|| (x[i].value.charAt(2) != '/')
							|| (isNaN(x[i].value.charAt(3)+x[i].value.charAt(4)))
							|| (x[i].value.charAt(5) != '/')
							|| (isNaN(x[i].value.charAt(6)+x[i].value.charAt(7)+x[i].value.charAt(8)+x[i].value.charAt(9)))
							) {
								format=false;
								msgF=msg_formatdate;
							}
					}
				}
				if ((tmp == 'EML') && ((x[i].value.indexOf('@')<1) || (x[i].value.indexOf('@')==(x[i].value.length-1)))) {
					msgF=msg_formatemail;
					format=false;
				}
			} 
			if ((x[i].title.lastIndexOf('(') == 0) && (format==false) && ((x[i].value != null) && (x[i].value != ''))) {
				msg+=msg_champs+x[i].title.substring(x[i].title.lastIndexOf(')')+1,x[i].title.length)+msg_obligatoire+msgF.substring(3,msgF.length)+'\n';
			} 
			if (((x[i].value == null) || (x[i].value == '') || (format == false)) && (x[i].title.lastIndexOf('(') != 0)) {
				var z=x[i].title.length;
				if (x[i].title.lastIndexOf('(') != -1) {
					var z=x[i].title.lastIndexOf('(');
				}
				msg+=msg_champs+x[i].title.substring(0,z)+msg_obligatoire+msgF+'\n';
			}
		}
		format=true;
		msgF='';
	}
	if (msg) alert(msg_erreur+'\n'+msg);
	return (msg == '');
}
function DisableBtn(o) {
	//o.disabled=true;
}
function AJAXRequest(page,retfonc,methode,data) {
	var xhr_object = null;
	if(window.XMLHttpRequest) // Firefox
	   xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
	   xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else { // XMLHttpRequest non supporté par le navigateur
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	   return;
	}
	if (data=="")
		data=null;
	if(methode == "GET" && data != null) { 
	   page += "?"+data; 
	   data = null; 
	}
	xhr_object.open(methode, page, true);
	xhr_object.onreadystatechange = function() {
		if(xhr_object.readyState == 4) {
			var RetAjax=xhr_object.responseText;
			eval(retfonc+'(RetAjax);');
                }
	}
	if(methode == "POST")
	   xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.send(data);
}

function FindID(id) {
	if(document.layers) return document.layers[id];
	if(document.all && !document.getElementById) return document.all[id];
	if(document.all && document.getElementById) return document.getElementById(id);
	if(!document.all && document.getElementById) return document.getElementById(id);
}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;
    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;
    var newstr = string.substring(0,i) + by;
    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);
    return newstr;
}
function trim(string){ 
	return string.replace(/(^\s*)|(\s*$)/g,''); 
} 

/**
 * Escape special chars in JavaScript
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function jsEscape(text){
    var re=new RegExp("\\\\","g");
    text=text.replace(re,"\\\\");
    var re=new RegExp("'","g");
    text=text.replace(re,"\\'");
    re=new RegExp('"',"g");
    text=text.replace(re,'&quot;');
    re=new RegExp("\\\\\\\\n","g");
    text=text.replace(re,"\\n");
    return text;
}
function addEvent( obj, type, fn ) {
//John Resig : http://ejohn.org/projects/flexible-javascript-events/
	if ( obj.attachEvent ) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	} else
		obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
//John Resig : http://ejohn.org/projects/flexible-javascript-events/
	if ( obj.detachEvent ) {
		obj.detachEvent( 'on'+type, obj[type+fn] );
		obj[type+fn] = null;
	} else
		obj.removeEventListener( type, fn, false );
}

/*Refonte des liens*/
addEvent(window,'load',AddClass4Link);
function AddClass4Link() {
	/*INIT*/
	var DefaultTarget='_blank'; /* Cible par defaut des lien externe */
	var UnchangedLink='ignore'; /* Les liens aillant cette classe seront ignoré */
	/* Classe par defaut pour chaque type de lien */
	var Classe=new Array();
	Classe['ExternalLink']='externallink';
	Classe['EmailLink']='emaillink';
	Classe['FileLink']='pjlink';
	Classe['InternalLink']='internallink';
	Classe['AnchorLink']='anchor';
	Classe['JavascriptLink']='';
	/* liste des extention de fichier concidéré comme lien de telechargement */
	var FileExt=new Array('exe','zip');
	
	/*Traitement*/
	var x = document.getElementsByTagName("a");
	for (i = 0; i < x.length; i++) {
		var AnchorLink = new RegExp("#.*","i"); /*HREF Qui contient # suivit d'un mot*/
		var EmailLink = new RegExp("mailto:","i"); /*HREF Qui contient mailto: */
		var JavascriptLink = new RegExp("javascript:","i"); /*HREF Qui contient javascript: */
		/*Lien ignoré*/
		if (x[i].className.indexOf(UnchangedLink)!=-1)
			continue;
		/*Cas des liens ancre*/
		else if (AnchorLink.test(x[i].href))
			x[i].className+=' '+Classe['AnchorLink'];
		/*Cas des liens email*/
		else if (EmailLink.test(x[i].href))
			x[i].className+=' '+Classe['EmailLink'];
		/*Cas des liens javascript*/
		else if (JavascriptLink.test(x[i].href))
			x[i].className+=' '+Classe['JavascriptLink'];
		/*Cas des liens interne*/
		else if (x[i].href.indexOf(window.location.host)!=-1)
			x[i].className+=' '+Classe['InternalLink'];
		/*Cas des liens externe*/
		else if (x[i].href.indexOf(window.location.host)==-1) {
			x[i].className+=' '+Classe['ExternalLink'];
			x[i].target=DefaultTarget;
		}
		/*Traitement exterieur des lien vers les fichiers. De cette façon, on peu cumuler lien interne + fichier ou lien externe + fichier */
		for (j=0;j<FileExt.length;j++) {
			var FileLink = new RegExp("\."+FileExt[j],"i"); /*HREF Qui contient .ext ou ext est une extension sus-citée */
			if (FileLink.test(x[i].href))
				x[i].className+=' '+Classe['FileLink'];
		}
		
	}
}

/*http://www.quirksmode.org/dom/toc.html*/
function getElementsByTagNames(list,obj)
{
	if (!obj) var obj = document;
	var tagNames = list.split(',');
	var resultArray = new Array();
	for (var i=0;i<tagNames.length;i++)
	{
		var tags = obj.getElementsByTagName(tagNames[i]);
		for (var j=0;j<tags.length;j++)
		{
			resultArray.push(tags[j]);
		}
	}
	var testNode = resultArray[0];
	if (!testNode) return resultArray;
	if (testNode.sourceIndex)
	{
		resultArray.sort(function (a,b) {
				return a.sourceIndex - b.sourceIndex;
		});
	}
	else if (testNode.compareDocumentPosition)
	{
		resultArray.sort(function (a,b) {
				return 3 - (a.compareDocumentPosition(b) & 6);
		});
	}
	return resultArray;
}

function createTOC()
{
	if (toc=='no') return;
	if (top.bugRiddenCrashPronePieceOfJunk) return;
	var x = document.body.childNodes;
	var y = document.createElement('div');
	y.id = 'toc';
	var a = y.appendChild(document.createElement('span'));
	a.onclick = showhideTOC;
	a.innerHTML = msg_toc;
	var z = y.appendChild(document.createElement('div'));
	z.onclick = showhideTOC;
	var toBeTOCced = getElementsByTagNames('h1,h2,h3,h4');
	if (toBeTOCced.length < 2) return;

	for (var i=0;i<toBeTOCced.length;i++)
	{
		var tmp = document.createElement('a');
		tmp.innerHTML = toBeTOCced[i].innerHTML;
		tmp.href = '#link' + i;
		tmp.className = 'page';
		z.appendChild(tmp);
		if (toBeTOCced[i].nodeName == 'H4')
			tmp.className += ' indent';
		var tmp2 = document.createElement('a');
		tmp2.id = 'link' + i;
		/*if (toBeTOCced[i].nodeName == 'H2')
		{
			tmp.innerHTML = 'Top';
			tmp.href = '#top';
			tmp2.id = 'top';
		}*/
		toBeTOCced[i].parentNode.insertBefore(tmp2,toBeTOCced[i]);
	}
	document.body.insertBefore(y,document.body.childNodes[0]);
}
var TOCstate = 'none';
function showhideTOC()
{
	TOCstate = (TOCstate == 'none') ? 'block' : 'none';
	document.getElementById('toc').lastChild.style.display = TOCstate;

}


function setActiveStyleSheet(title) {
/*
On passe un a un les balise <link>,
Pour chaque balise  dont 
	1- l'attribut "rel" contient le mot "style"  
	2- l'attribut "title" existe
on active la balise link et, si le parametre "title" est égal a la variable title, on désactive la balise.
*/
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}
function getActiveStyleSheet() {
/*
On passe un a un les balise <link>,
Pour LA balise  dont 
	1- l'attribut "rel" contient le mot "style"  
	2- l'attribut "title" existe
	3- Qui n'est pas désactivé
on renvoie le title de cette balise.
Si pas de coorespondance, on renvoie null
*/
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}
function getPreferredStyleSheet() {
/*
On passe un a un les balise <link>,
Pour LA balise  dont 
	1- l'attribut "rel" contient le mot "style" 
	2-l'attribut "rel" ne contient pas le mot "alt"
	2- l'attribut "title" existe
on renvoie le title de cette balise,
si pas de corespondance, on renvoie null
*/
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}
window.onload = function(e) {
  
	/*
	Au chargement de la page, récupere le CSS par defaut et l'active.
	Si ce css était mémorisé par cookie, cela permetrait de rétablir le style choisie en derneir par l'internaute
	*/
	var title = getCookie("style") ; 
	
	if (title=='')
		title=getPreferredStyleSheet();

	ModeContrast(((title=="normal")?false:true),'accesslink',title);
	
	restaureFontSize();
  
}
 

function ModeContrast(actif,obj,style) {

	if (!FindID(obj)) return;

	setActiveStyleSheet(style);
	
	/*save en cookie*/
	exp = new Date();
	exp.setDate(exp.getDate+1);
	setCookie("style",style,exp);
	
	if (actif) {
		FindID(obj).innerHTML=msg_css_normal;
		FindID(obj).href=replace(FindID(obj).href,'contraste','normal');
		FindID(obj).href=replace(FindID(obj).href,'true','false');
	} else {
		FindID(obj).innerHTML=msg_css_contraste;
		FindID(obj).href=replace(FindID(obj).href,'normal','contraste');
		FindID(obj).href=replace(FindID(obj).href,'false','true');
	}

} 


function ChangeSizeTxt(sens) {
	var b=document.getElementsByTagName("body");
	var siz=parseInt(b[0].style.fontSize);
	
	var fontSizeMin = 12;
	var max=25;
	
	if (sens=='+') {
		siz++;
		if (siz>max) siz=max;
		b[0].style.fontSize=siz+'px';
	} else {
		siz--;
		if (siz<fontSizeMin) siz=fontSizeMin;
		b[0].style.fontSize=siz+'px';
	}
	
	/*save en cookie*/
	exp = new Date();
	exp.setDate(exp.getDate+1);
	setCookie("fontsize",siz,exp);
}
function restaureFontSize() {
	var siz = getCookie("fontsize") ; 
	var b=document.getElementsByTagName("body");
	if (siz!='')
		b[0].style.fontSize=siz+'px';
}

function setCookie(nom, valeur, expiration) {
	document.cookie = nom + "=" + escape(valeur) + ((expiration == null) ? "" : ("; expires=" + expiration.toGMTString()));
}
function getCookie(nom) {
	var recherche = nom+ "=";
	if (document.cookie.length > 0) { 
		offset = document.cookie.indexOf(recherche) ;
		if (offset != -1) { // si le cookie existe
			offset += recherche.length;
			// position du début
			fin = document.cookie.indexOf(";",offset) ;
			// position de la fin 
			if (fin == -1) 
				fin= document.cookie.length;
			return unescape(document.cookie.substring(offset, fin));
		} 
	}
	
	return '';
}

function noaccent(chaine) {
  temp = chaine.replace(/[àâä]/gi,"a");
  temp = temp.replace(/[éèêë]/gi,"e");
  temp = temp.replace(/[îï]/gi,"i");
  temp = temp.replace(/[ôö]/gi,"o");
  temp = temp.replace(/[ùûü]/gi,"u");
  temp = temp.replace(/[ç]/gi,"c");
  temp = temp.replace(/[\/\[\]\\\{\}\(\)]/gi,"");
  return temp
}

/*Recherche*/
function Search(l) {
	if (document.getElementById('txt_search').value.length<3) {
		alert(msg_search_short);
		return false;
	}
	
	document.location.href=l+makeForUrl('act','search',true)+makeForUrl('string',escape(noaccent(document.getElementById('txt_search').value),true));
}
