var tb = new Array();

function check( element, path_puce, id ) {
	if ( tb[element+'_'+id] ) {
		tb[element+'_'+id] = 0;
		//document.getElementById( 'hidden|' + element ).value = '';
		document.getElementById( 'chk|' + element + '_' + id).src = path_puce + 'chk_off.gif';
	}
	else {
	tb[element+'_'+id] = 1;
		//document.getElementById( 'hidden|' + element ).value = 1;
		document.getElementById( 'chk|' + element + '_' + id).src = path_puce + 'chk_on.gif';
	}
	var liste = new String();
	for(var v in tb) {
		if ( tb[v] == 1 &&  v.substring(v.lastIndexOf('_')+1) ==  id){
			liste += '-' + v.substring(0,v.lastIndexOf('_'));	
		}
	}
		document.getElementById( 'dg_docs'+ '_' + id).value = liste;	
}


function selectAll (path_puce, id) {
	var Liste_doc = document.getElementsByName("doc_"+id);	
	for(i = 0; i < Liste_doc.length; i++) {
		check( Liste_doc[i].value, path_puce , id );
	}
}


function ChangeDynamique(url, id, Atr, type ) {
	setAction(id, "");

	obj = document.getElementsByName( 'form_'+id )[0];
	child = obj.childNodes;
	for (i=0; i<child.length; i++) {
		if (child[i].value) {
			 Atr = Atr+"&"+child[i].name+"="+child[i].value;
		}
	}

	appel_doc(url, "TypeDynamique="+type+"&id="+id+"&"+ Atr, 'bloc_result_document_'+id);
}

function ChangeTri(url, id, dg_tri, dg_ordre) {
	Submit_Order(id, dg_tri, dg_ordre);
	ChangeDynamique(url, id, "", 'order');
}

function Submit_Order (id, dg_tri, dg_ordre) {
	setDg_Tri(id,dg_tri);
	setDg_Ordre(id,dg_ordre);
	setAction(id, "");
}

function setDg_Tri(id, dg_tri ) {
	document.getElementById( 'dg_tri_'+id ).value = dg_tri;
}

function setDg_Ordre(id, dg_ordre ) {
	document.getElementById( 'dg_ordre_'+id ).value = dg_ordre;
}

function setAction(id, action ) {
	document.getElementById( 'dg_multi_action_'+id ).value = action;
	
}


function multi_suppr(form) {
	 setAction( 'supprimer_document' );
	 form.submit();
}

function visu_bloc(element, picto ) {
	if (document.getElementById( element ) ) {
		if (document.getElementById( element ).style.display == "none") {
			document.getElementById( element ).style.display = "";
			document.getElementById( picto + '_on' ).style.display = "none";
			document.getElementById( picto + '_off' ).style.display = "";
		} else if (document.getElementById( element ).style.display == "") {
			document.getElementById( element ).style.display = "none"
			document.getElementById( picto + '_on' ).style.display = "";
			document.getElementById( picto + '_off' ).style.display = "none";
		}
	}
}

function afficher_bloc( nom, num, nb, nb_par_page, nb_docs, nb_num_page ) {
	for (j=1; j<nb+1; j++) {
		if ( j >= num-nb_num_page && j <= num+nb_num_page ) {
		//alert( 'ok'+j );
			document.getElementById( 'num_docGrid_'+j ).style.display = '';
		}
		else {
		//alert( 'nok'+j );
			document.getElementById( 'num_docGrid_'+j ).style.display = 'none';
		}
		tab = document.getElementsByName(nom+j);
		//alert( "h"+tab[1]);
		if ( j == num ) {
			for (i=0; i<tab.length; i++) {
			
				tab[i].style.display = '';
			}
			document.getElementById( 'num_docGrid_'+j ).style.color = '#333333';
		}
		else {
			for (i=0; i<tab.length; i++) {
				tab[i].style.display = 'none';
			}
			document.getElementById( 'num_docGrid_'+j ).style.color = '#999999';
		}
		debut = 1 + nb_par_page * ( num - 1 );
		fin = debut + nb_par_page - 1;
		if ( fin > nb_docs ) {
			fin = nb_docs;
		}
		document.getElementById( 'infos_pagination_doc_debut' ).innerHTML = debut;
		document.getElementById( 'infos_pagination_doc_fin' ).innerHTML = fin;
	}
	document.getElementById( 'memo_page' ).innerHTML = num;
}

function afficher_bloc_precedent( nom, nb, nb_par_page, nb_docs, nb_num_page ) {
	num = document.getElementById( 'memo_page' ).innerHTML - 1;
	if ( num > 0 ) {
		afficher_bloc( nom, num, nb, nb_par_page, nb_docs, nb_num_page );
	}
}

function afficher_bloc_suivant( nom, nb, nb_par_page, nb_docs, nb_num_page ) {
	num = document.getElementById( 'memo_page' ).innerHTML - (-1);
	if ( num < nb+1 ) {
		afficher_bloc( nom, num, nb, nb_par_page, nb_docs, nb_num_page );
	}
}

/* INFOBULLE */

var timer;
var actuelle;
function MouseOver(id, ev)
{
	if ( actuelle != id && document.getElementById("Bulle_"+actuelle) ) {
		MouseOut2(actuelle);
		actuelle = id;
	}
	else {
		actuelle = id;
	}
	
	var Xdoc, Xfen, Ydoc, Yfen, htDiv, lgDiv, dX, dY;
	var Ybulle;
	var el=document.getElementById("Bulle_"+id);
	
	var ddE = document.documentElement;
	var dB = document.body; 
	
	//on affiche la boite de dialogue pour evaluer ses dimensions.
	if (el.style.display!="inline")
		el.style.display="inline";
	
	//hauteur et largeur de la bulle
	htDiv = el.offsetHeight;
	lgDiv = el.offsetWidth;
	
	//delta de la bulle sous la souris
	dY=10;
	dX=10;
	
	if (navigator.appName == "Microsoft Internet Explorer") {
		if( ddE && ddE.clientWidth)
			DocRef = ddE;
		else
			DocRef = dB;
		with( DocRef){
			ScrollLeft = scrollLeft;
			ScrollTop = scrollTop;
			larg = clientWidth;
			haut = clientHeight; 
		} 
	} else {
		ScrollLeft = window.pageXOffset;
		ScrollTop = window.pageYOffset;
		larg = (window.innerWidth) - 16;
		haut = (window.innerHeight);
	}
	
	//position de la souris :
	Xfen = ev.clientX;
	Xdoc = Xfen + ScrollLeft;
	
	Yfen = ev.clientY;
	Ydoc = Yfen + ScrollTop;
	
	/*larg = (window.innerWidth);
	haut = (window.innerHeight);
	if ( !larg )
	{
		larg = (document.body.clientWidth);
	}
	else {
		//suppression de la largeur de la scrollbar
		larg = larg - 16;
	}
	if ( !haut )
	{
		haut = (document.body.clientHeight);
	}*/
	
	//position de la bulle dans la page :
	if ((Xfen + lgDiv + dX) > larg) {
		//sscrollLeft = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
		left = ( larg + ScrollLeft - lgDiv ) + 'px';
    	el.style.left = left;
		}
	else {
		add =  Xdoc + dX;
		el.style.left = add+'px';
    }
    
	
	if ((Yfen + htDiv + dY) > haut)
	{
		Ybulle = (parseInt(haut) + parseInt(ScrollTop) - htDiv-5);
		if (parseInt(Ybulle)>parseInt(Ydoc))
		{
			ttop = Ybulle + 'px';
			el.style.top = ttop;
     	}
     	else
     	{
			ttop = Ydoc - htDiv - dY;
			el.style.top = ttop+ 'px';
     	}
	}
  	else
  	{
		ttop = Ydoc + dY - 5 ;
		el.style.top = ttop+ 'px';
  	}
	
	/*el.innerHTML = "";
	el.innerHTML += " haut= "+haut+" px ; larg= "+larg+" px<br>";
	el.innerHTML += " scrollTop= "+ScrollTop+" px ; ScrollLeft= "+ScrollLeft+" px<br>";
	el.innerHTML += " Xdoc= "+Xdoc+" px ; Ydoc= "+Ydoc+" px<br>";
	el.innerHTML+= " Xfen= "+Xfen+" px ; Yfen= "+Yfen+" px<br><br>";
	el.innerHTML+= " cond1= "+(Yfen + htDiv + dY)+"  ; cond1= "+haut+" <br><br>";
	el.innerHTML+= " cond3= "+haut +" "+ ScrollTop +" "+ htDiv+"  ; Ybulle= "+Ybulle+"  <br><br>";
	el.innerHTML += " Top = " + el.style.top + " px ; Left= " + el.style.left + " px<br><br><br>";*/
	
	KillTimer();
}

function KillTimer() {
	window.clearTimeout(timer);
}
 
function MouseOut(id)
{
	//on cache la bulle
	timer = setTimeout( "MouseOut2('"+id+"')", 200 );
	//MouseOut2();
}

function MouseOut2(id)
{
	//on cache la bulle
	document.getElementById("Bulle_"+id).style.display = "none";
}


function appel_doc(url, request, DivResult ) {

	var xhr=null;
    
    if (window.XMLHttpRequest) { 
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) 
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { response_ajax_doc(xhr, DivResult); };
    
	xhr.open("POST", url, true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send(request);
	
}

function response_ajax_doc(xhr, DivResult) {
	
	var div = document.getElementsByName(DivResult)[0];
	
	if(xhr.readyState == 4 && xhr.status == 200){
		var docXml = xhr.responseText;
		
		div.innerHTML = docXml;
		ajax_parseJs(div)
	} else {
		div.innerHTML = "<div style='text-align:center;'><img src='"+statique+"/img/loading.gif'></div>";
	}
}

function ajax_parseJs(obj)
{
	var scriptTags = obj.getElementsByTagName('SCRIPT');
	var string = '';
	var jsCode = '';
	for(var no=0;no<scriptTags.length;no++){	
		if(scriptTags[no].src){
	        var head = document.getElementsByTagName("head")[0];
	        var scriptObj = document.createElement("script");
	
	        scriptObj.setAttribute("type", "text/javascript");
	        scriptObj.setAttribute("src", scriptTags[no].src);  	
		}else{
			if(navigator.userAgent.indexOf('Opera')>=0){
				jsCode = jsCode + scriptTags[no].text + '\n';
			}
			else
				jsCode = jsCode + scriptTags[no].innerHTML;	
		}
		
	}

	if(jsCode)ajax_installScript(jsCode);
}


function ajax_installScript(script)
{		
    if (!script)
        return;		
    if (window.execScript){        	
    	window.execScript(script)
    }else if(window.jQuery && jQuery.browser.safari){ // safari detection in jQuery
        window.setTimeout(script,0);
    }else{        	
        window.setTimeout( script, 0 );
    } 
}	

