var ns4=document.layers;
var ie4=document.all;
var ns6=document.getElementById&&!document.all;

//
function AfficheDate(nObjet,choix) {
if(ie4) { // Internet explorer
	eval(nObjet).style.visibility = (choix==1?'visible':'hidden');
} else if(ns4) { // Netscape 4.x
	document.eval(nObjet).visibility = (choix==1?'show':'hidden');
} else if(ns6) { // Netscape 6 (mozilla)
	var divns6 = document.getElementsByTagName("span")
	divns6[nObjet].style.visibility = (choix==1?'visible':'hidden');
}
}
// Mettre à jour les cochages des notices de la page
function MajPage(nom) {
	liste=LireCookie(nom);
	if (!liste) liste = ",";
	var total = liste.replace(/[^,]/gi,"");
	total = eval(total.length - 1);
	document.getElementById("panierb").innerHTML=total;
	document.getElementById("panierh").innerHTML=total;
	for (var i = 0; i < document.SELECT.elements.length; i++) {
		if (document.SELECT.elements[i].type == 'checkbox') {
			if ( document.SELECT.elements[i].checked ) {
				var chx = document.SELECT.elements[i].value;
				var j = liste.indexOf(","+chx+",");
				if ( j == -1 ) {
					document.SELECT.elements[i].checked = false;
					document.getElementById("TR"+chx).className="sel_of"; 
				}
			}	
		}
	}
}
// Rejet d'un élément de la sélection
function EnlevePanier(nom,chx) {
	var j = liste.indexOf(","+chx+",");
	if ( liste == ","+chx+"," ) {
		liste = ",";
	} else if ( j == 0 ) {
		liste = liste.substr(chx.length+1);
	} else {
		liste = liste.substr(0,j)+liste.substr(j+chx.length+1);
	}
	EcrireCookie(nom,liste);
}
// Rejet d'un élément de la sélection
function Effacer(nom) {
	i=document.selection.panier.options.selectedIndex;
	if (i < 0)return;
	chx = document.selection.panier.options[i].value;
	document.selection.panier.options[i]=null;
	var j = liste.indexOf(","+chx+",");
	if ( liste == ","+chx+"," ) {
		liste = ",";
	} else if ( j == 0 ) {
		liste = liste.substr(chx.length+1);
	} else {
		liste = liste.substr(0,j)+liste.substr(j+chx.length+1);
	}
	EcrireCookie(nom,liste);
	var total = liste.replace(/[^,]/gi,"");
	total = eval(total.length - 1);
	if ( total == 0 ) {
		window.location.href = "index.php" 
	} else {
		if ( i > total-1) i=total-1;
		document.selection.panier.options.selectedIndex = i;
		document.getElementById("aide3").innerHTML="Panier : "+total+" fiche(s)";
	}
}
// Guide
function Guide(AIDE,CHP) {
	var MSG = AIDE;
	document.getElementById(CHP).innerHTML=MSG;
}
// Ouverture d'une fenetre POPUP
function OuvrirFenetre(URL,LgX,LgY,Nom) {
	PosX = (screen.width) ? (screen.width-LgX)/2 : 0; 
	PosY = (screen.height) ? (screen.height-LgY)/2 : 0; 
	if ( !Nom ) Nom = "popup";
	msg=open("wait_fr.html",Nom,"location=no,status=no,toolbar=no,directories=no,resizable=yes,menubar=no,scrollbars=yes,width="+LgX+",height="+LgY+",left="+PosX+",top="+PosY);
	msg.location.href=URL;
	msg.focus();
}
// SELECTION
function EcrireCookie(nom, valeur) {
	var date=new Date;
	date.setMonth(date.getMonth()+6);
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : date;
	var path=(argc > 3) ? argv[3] : "/";
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
 ((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
 ((path==null) ? "" : ("; path="+path))+
 ((domain==null) ? "" : ("; domain="+domain))+
 ((secure==true) ? "; secure" : "");
}
function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function LireCookie(nom) {
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}
function CaseCocher(CLE,nom) {
	if ( liste.indexOf(","+CLE+",") != -1 ) {
		document.write('<input onClick="Cochage(\'select'+CLE+'\',\''+nom+'\');" type="checkbox" name="select'+CLE+'" value="'+CLE+'" checked>');
		document.getElementById("TR"+CLE).className="sel_on"; 
	} else {
		document.write('<input onClick="Cochage(\'select'+CLE+'\',\''+nom+'\');" type="checkbox" name="select'+CLE+'" value="'+CLE+'">');
		document.getElementById("TR"+CLE).className="sel_of"; 
	}
}
function Cochage(CTL,nom) {
	var chx = document.SELECT.elements[CTL].value;
	var flg = false;
	var i = liste.indexOf(","+chx+",");
	if ( document.SELECT.elements[CTL].checked ) {
		if ( i == -1 ) {
			liste = liste+chx+",";
			flg = true;
		}
		document.getElementById("TR"+chx).className="sel_on"; 
	} else {
		
		if ( i != -1 ) {
			if ( liste == ","+chx+"," ) {
				liste = ",";
			} else if ( i == 0 ) {
				liste = liste.substr(chx.length+1);
			} else {
				liste = liste.substr(0,i)+liste.substr(i+chx.length+1);
			}
			flg = true;
		}
		document.getElementById("TR"+chx).className="sel_of"; 
	}
	if ( flg ) EcrireCookie(nom,liste);
	var total = liste.replace(/[^,]/gi,"");
	total = eval(total.length - 1);
	document.getElementById("panierh").innerHTML=total;
	document.getElementById("panierb").innerHTML=total;
	modif=true;
}
// 
function CocherTout(nom) {
	for (var i = 0; i < document.SELECT.elements.length; i++) {
		if (document.SELECT.elements[i].type == 'checkbox') {
			if ( !document.SELECT.elements[i].checked ) {
				document.SELECT.elements[i].checked = true;
				Cochage(document.SELECT.elements[i].name,nom);
			}	
		}
	}
}
// 
function DecocherTout(nom) {
	for (var i = 0; i < document.SELECT.elements.length; i++) {
		if (document.SELECT.elements[i].type == 'checkbox') {
			if ( document.SELECT.elements[i].checked ) {
				document.SELECT.elements[i].checked = false;
				Cochage(document.SELECT.elements[i].name,nom);
			}	
		}
	}
}
// Message de validation
function Message(Total,Maxdoc) {
	if ( Total > Maxdoc) {
		if (!confirm("Seuls les "+Maxdoc+" premiers documents sur "+Total+" seront traités. Voulez vous continuer ?")) return false;
	}
	return true;
}
// Formulaire POST
function OuvrirPost(URL,LgX,LgY) {
	PosX = (screen.width) ? (screen.width-LgX)/2 : 0; 
	PosY = (screen.height) ? (screen.height-LgY)/2 : 0; 
	document.traitement.action = URL;
	msg=window.open("wait_fr.html","popup","location=no,status=no,toolbar=no,directories=no,resizable=yes,menubar=no,scrollbars=yes,height="+LgX+",width="+LgY+",left="+PosX+",top="+PosY);	
	document.traitement.target = "popup";
	document.traitement.submit();
	msg.focus();
}
// Composition requête
function ComposeRequete(CTL,REQ,NUM,CLR) {
	var chp = document.formhist.quest.value;
	var req = document.formhist.where.value;
	var clr = document.formhist.requete.value;
	switch ( CTL ) {
		case 0:	// #
		var reg = /[0-9]$|\)$/;
		if ( reg.exec(chp) != null ) {
			chp=chp+' et ';
			clr=clr+' et ';
			req=req+' AND ';
		}
		document.formhist.quest.value=chp+'#'+NUM;
		document.formhist.requete.value=clr+CLR;
		document.formhist.where.value=req+REQ
		break;
		
		case 1:	// OU
		if ( chp == "" ) return;
		var reg = /[0-9]$|\)$/;
		if ( reg.exec(chp) == null ) return;
		document.formhist.quest.value=chp+' ou ';
		document.formhist.requete.value=clr+' ou ';
		document.formhist.where.value=req+' OR ';
		break;
		
		case 2:	// ET
		if ( chp == "" ) return;
		var reg = /[0-9]$|\)$/;
		if ( reg.exec(chp) == null ) return;
		document.formhist.quest.value=chp+' et ';
		document.formhist.requete.value=clr+' et ';
		document.formhist.where.value=req+' AND ';
		break;
		
		case 3:	// SAUF
		if ( chp == "" ) {
			document.formhist.quest.value='Tout sauf ';
			document.formhist.requete.value='Tout sauf ';
			document.formhist.where.value='NOT ';
		} else {
			var reg = /[0-9]$|\)$/;
			if ( reg.exec(chp) == null ) return;
			document.formhist.quest.value=chp+' sauf ';
			document.formhist.requete.value=clr+' sauf ';
			if ( req == "" ) {
				document.formhist.where.value='NOT ';
			} else {
				document.formhist.where.value=req+' AND NOT ';
			}
		}
		break;
		
		case 4:	// (
		var reg = /[0-9]$/;
		if ( reg.exec(chp) != null ) {
			chp=chp+' et ';
			clr=clr+' et ';
			req=req+' AND ';
		}
		document.formhist.quest.value=chp+'(';
		document.formhist.requete.value=clr+'(';
		document.formhist.where.value=req+'('
		break;
		
		case 5:	// )
		if ( chp == "" ) return;
		var reg = /[0-9]$/;
		if ( reg.exec(chp) == null ) return;
		if ( chp.indexOf("(") == -1 ) return;
		var i = 0;
		var j = chp.indexOf('(');
		while (j != -1) {
			i++;
			j = chp.indexOf('(',++j);
		}
		var k = 0;
		var j = chp.indexOf(')');
		while (j != -1) {
			k++;
			j = chp.indexOf(')',++j);
		}
		if ( k >= i ) return;
		document.formhist.quest.value=chp+')';
		document.formhist.requete.value=clr+')';
		document.formhist.where.value=req+')'
		break;
	}
}
function LanceRequete() {
	var tmp = "";
	var ope = "";
	var chp = document.formhist.quest.value;
	if ( chp == "" ) return;
	i = 0;
	var j = chp.indexOf('(');
	while (j != -1) {
		i++;
		j = chp.indexOf('(',++j);
	}
	var k = 0;
	var j = chp.indexOf(')');
	while (j != -1) {
		k++;
		j = chp.indexOf(')',++j);
	}
	if ( i != k ) {
		alert("Nombre de parenthèses incorrect.");
		return;
	}
	var reg = /[0-9]$|\)$/;
	if ( reg.exec(chp) == null ) {
		alert("Syntaxe incorrecte.");
		return;
	}
	document.formhist.submit();
}

