// Mthodes Javascript utiles pour l'ensemble du site

//
// Recherche d'une frame nome "fname" dans la frame "fin" et les toutes les frames filles
//	si "fin" est null recherche dans self
//
function MM_findFrm(fname,fin) { //v1.0
	if(!fin) fin=self
	var len=fin.frames.length
	var fout = null
	if(len>0){	fout=fin.frames[fname];
				if(!fout){for(var i=0;!fout&&i<len;i++){fout=MM_findFrm(fname,fin.frames[i])}}}
	return fout
}

function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

// Affiche ou cache une zone DHTML
function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// Positionne physiquement un objet dans la page HTML
function MM_MoveDiv(divId,left,top,width)
{
	var div=MM_findObj(divId)
	if (div && !width && (document.all || document.getElementById)) width=div.style.width
	if (div) {if ( document.all || document.getElementById){div.style.left=left;div.style.top=top;div.style.width=width;}
	else div.moveTo(left,top)}
}

function MM_displayObj(nom,bDisplay)
{
	document.getElementById(nom).style.display = (bDisplay==false)?"none":"inline";
}

// Affiche ou cache une zone DHTML (style display)
function MM_displayLayers() { 
  var i,p,v,obj,args=MM_displayLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj.style.display=v;}
    }
}

//////////////////////////////////////////////////////////////
//
//	Fonction d'affectation et rcupration de la valeur d'un control dans un formulaire
//
//
function MM_GetValEdit(form,control,def)
{
	var ret="";if(def) ret=def;
	if(form&&form[control]) ret=form[control].value;
	return ret;
}

function MM_SetValEdit(form,control,val)
{
	if(form&&form[control]) form[control].value=val;
}

function MM_GetValCheck(form,control,def)
{
	var ret="off";if(def) ret=def;
	if(form&&form[control]&&form[control].checked) ret="on";
	return ret;
}

// Affect  checked  false si val="" ou val="off"
function MM_SetValCheck(form,control,val)
{
	var bool=false
	if(val!=""&&val!="off")bool=true;
	if(form&&form[control])form[control].checked=bool
}
	
function MM_GetValRadio(form,control,def)
{
	var ret="";if(def) ret=def;
	var i=0,Elem,nbElem;
	if (form){Elem=form.elements; if(Elem) nbElem=Elem.length}
	
	if (nbElem)
	while(i < nbElem)
	{if(Elem[i].name==control && Elem[i].checked){ret=Elem[i].value;break;}i++;}
	return ret;
}

// Selectionne le radio qui a pour "name" "controle" et pour "value" "val"
// Si val=="" selectionne le radio "def"
function MM_SetValRadio(form,control,val,def)
{
	if (val=="") val=def;
	var i=0,Elem,nbElem;
	if (form){Elem=form.elements; if(Elem) nbElem=Elem.length}
	
	if (nbElem)
	while(i < nbElem)
	{if(Elem[i].name==control && Elem[i].value==val){Elem[i].checked=true;break;}i++;}
}

function MM_GetValSel(form,control,def)
{
	var ret="";if(def) ret=def;
	if (form&&form[control])
	{var iSel=form[control].selectedIndex;if(iSel>=0) ret=form[control].options[iSel].value}
	return ret;
}

// Selectionne l'option qui a pour "value" "val"
// Si val=="" selectionne l'option "def"
function MM_SetValSel(form,control,val,def)
{
	if (val=="") val=def;
	if (form&&form[control]&&form[control].options)
	{
		var i=0,Options=form[control].options;
		var nbOption=Options.length;
		while(i < nbOption)
		{if(Options[i].value==val){Options[i].selected=true;break;}i++;}
	}
}

//	
//	Conversion on/off vers bool
//	et bool vers on/off
//
//		valeur "on" et "true" par dfaut
//
function MM_OnOffToBool(strOnOff)
{
	if (strOnOff != null) if(strOnOff == "off") return false;     
	return true
}

function MM_BoolToOnOff(bBool)
{
	if (bBool != null){if (!bBool) return "off"}
	return "on";
}

//////////////////////////////////////////////////////////////
//
// Gestion de la tool Barre
//
var timeout_state
var timeout_secondName
function MM_SetToolBarreState(state,secondName)
{
	var href
	f=MM_findFrm("FrameClient",top)
	if (f)
		href=f.location.href
	
	var bSetToolBarreOK=false;
	
	ToolBarreFrame=MM_findFrm("ToolBarre",top)
	if (ToolBarreFrame)
	{
		if (ToolBarreFrame.MM_SetState)
		{
			ToolBarreFrame.MM_SetState(state,href,secondName)
			bSetToolBarreOK = true;
		}
	}
	
	if (bSetToolBarreOK == false)
	{
		timeout_state = state;
		timeout_secondName = secondName;
		window.setTimeout('MM_SetToolBarreState(timeout_state,timeout_secondName)',100);
	}
}

// Gestion de la Maximisation Minimisation
function MM_IsMinimized(FrameBase)
{
	var bMinimised=false
	if (
			window.name == "FrameList" 
		|| 	window.name == "FrameDocument"
	)
	{
		bMinimised = true
	}
	else
	{
		f = window.parent
		if (	f && f.name 
			&& ( f.name == "FrameList" || f.name == "FrameDocument"))
		{
			bMinimised = true
		}
	}

/*
	var etat="Unknow";
	if (bMinimised)	etat="Minimized";
	else etat="Maximized";
	alert("MM_IsMinimized " + window.name + " " + etat)
*/
	return bMinimised;
}

function MM_IsMaximized(FrameBase)
{
	return !MM_IsMinimized(FrameBase)
}

//////////////////////////////////////////////////////////////
//
// Gestion retaillage sous Netscape
//
function MM_reloadPage(init) 
{  	//reloads the window if Nav4 resized
	// alert("MM_reloadPage " + window.name)
	if (init==true) with (navigator) 
	{
		if ((appName=="Netscape")&&(parseInt(appVersion)==4)) 
		{
			document.MM_pgW=innerWidth; 
			document.MM_pgH=innerHeight; 
			onresize=MM_reloadPage; 
		}
	} 
	else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

//////////////////////////////////////////////////////////////
//
// Dplacement sur une ancre avec sauvegarde dans l'historique
//
function MM_GotoAnchor(strTargetID, frame)
{
	if (!frame) frame = self
	if (strTargetID && frame)
	{
		/*
			 alert(
				"location " + window.location + "\n" +
				"location.hash " + window.location.hash + "\n" +
				"location.hostname " + window.location.hostname + "\n" +
				"location.pathname " + window.location.pathname + "\n" +
				"location.protocol " + window.location.protocol + "\n" +
				"location.search " + window.location.search + "\n"
				)
		*/
		frame.window.location.hash = strTargetID;
	}
}

//////////////////////////////////////////////////////////////
//
// Aide et didacticiel
//

function MM_AfficherDidacticiel()
{
	MM_openBrWindow("aide/index.htm",'aide','menubar=no,scrollbars=yes,resizable=yes,width=800,height=600')
}

function MM_AfficherAide(file)
{
	path="aide/"
	fullPtahFile=path + 'frameaide.html'
	
	if (file)
	{
		fullPtahFile+= "#" + file
	}
	
	MM_openBrWindow(fullPtahFile,'aide','menubar=no,scrollbars=yes,resizable=yes,width=800,height=600')
}

//////////////////////////////////////////////////////////////
//
//	Gestion des statistiques de recherche sur les tables
//
var arrayStat=new Array()

	
function getOffsetStatRech(FormRech)
{
	var iOffsetStatRech=0
	if (FormRech)
	{
		type_rech=MM_GetValEdit(FormRech,"TYPE_RECH")
		toc_id=MM_GetValEdit(FormRech,"TOC_ID_SEL")
		switch (toc_id)
		{
			case '2' :  // CGI
			case '3' :  // LPF
				if (type_rech=="TI")
				{
					iOffsetStatRech=0;
				}
				if (type_rech=="REFDOC")
				{
					iOffsetStatRech=0;
				}
				break;
			case '5' :  //BOI
			case '6' :
				if (type_rech=="TI")
				{
					iOffsetStatRech=0;
				}
				if (type_rech=="REFDOC")
				{
					iOffsetStatRech=0;
				}
				break;
			case '8' :  // DB
				if (type_rech=="TI")
				{
					iOffsetStatRech=0;
				}
				if (type_rech=="REFDOC")
				{
					iOffsetStatRech=0;
				}
				break;
			case '10' : // PF
				if (type_rech=="TI")
				{
					iOffsetStatRech=1;
				}
				if (type_rech=="REFDOC")
				{
					iOffsetStatRech=0;
				}
				break;
			case '11' : // PDOC
				if (type_rech=="TI")
				{
					iOffsetStatRech=0;
				}
				if (type_rech=="REFDOC")
				{
					iOffsetStatRech=0;
				}
				break;
		}
		// alert("getOffsetStatRech type_rech=" + type_rech + " toc_id=" + toc_id + " iOffsetStatRech=" + iOffsetStatRech);
	}
	return iOffsetStatRech;
};

//"                                 !"#$&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
//"                                               0123456789       ABCDEFGHIJKLMNOPQRSTUVWXYZ      abcdefghijklmnopqrstuvwxyz									  AAAAAAACEEEEIIIIDNOOOOO  UUUUY  aaaaaaaceeeeiiiinooooo         "
var strTabChar=		""
var strTabCharAppauvri=	"AAAAAAACEEEEIIIIDNOOOOO  UUUUY  aaaaaaaceeeeiiiinooooo"

function MM_AppauvrirAvecAccent(strTexte, bTraiteAccents)
{
	return MM_Appauvrir(strTexte, false)
}

function MM_Appauvrir(strTexte, bTraiteAccents)
{
	var iLen=strTexte.length
	var strTextAppauvri="";
	
	if( bTraiteAccents == null )
	{
		bTraiteAccents=true
	}

	for (i=0;i<iLen;i++)
	{	
		var c=strTexte.charAt(i)
		var cAppauvri=c;
		if (
				(c < '0')
			||	(c > '9' && c < 'A')
			||	(c > 'Z' && c < 'a')
			||	(c > 'z' && c < ''))
		{
			cAppauvri=' '
		}
		else 
		{
			if (bTraiteAccents == true)
			{
				// Supression des caractres accentus
				var iFind=strTabChar.indexOf(c);
				if (iFind >=0) cAppauvri=strTabCharAppauvri.charAt(iFind);
			}
		}
		strTextAppauvri+=cAppauvri;		
	}
	strTextAppauvri=ltrim(strTextAppauvri)
	strTextAppauvri=rtrim(strTextAppauvri)
	return strTextAppauvri.toUpperCase();
}

//Function to trim the space in the left side of the string
function ltrim ( s ){
  var sAvant=s;
  var iLen=s.length
  var i=0
  while (i<iLen)
  {	
  	if (s.charAt(i) != ' ') break;
  	i++
  }
  s=s.substring(i);
  
  // alert("ltrim : sAvant : " + sAvant + "\ns      : '" + s + "'")
  return s;
}

//Function to trim the space in the rigth side of the string
function rtrim ( s ){
  var sAvant=s;
  var iLen=s.length
  var i=iLen-1
  while (i>=0)
  {	
  	if (s.charAt(i) != ' ') break;
  	i--
  }
  s=s.substring(0,i+1);
  
  // alert("rtrim : sAvant : " + sAvant + "\ns      : '" + s + "'")
  return s;
}

