<!--

var da = (document.all) ? 1 : 0;
var pr = (window.print) ? 1 : 0;
var mac = (navigator.userAgent.indexOf("Mac") != -1);
var BIB = "/consultation/";
var CGIBIN = "/cgi-bin/";


// Fonctions de base pour la gestion des cookies
function ecritCookie(nom, valeur, fen) {
	if (fen) {
		parent.opener.document.cookie = nom + "=" + valeur;
	}
	else {
		parent.document.cookie = nom + "=" + valeur;
	}
}

function litValCookie (chaine, deb_val) {
	var fin_val = chaine.indexOf(";", deb_val);
	if (fin_val == -1)
		fin_val = chaine.length;
	return chaine.substring(deb_val, fin_val);
}

function litCookie(nom, fen) {
	var ch_nom = nom + "=";
	var ch_len = ch_nom.length;
	var ch_cookie = "";
	if (fen) {
		ch_cookie = parent.opener.document.cookie;
	}
	else {
		ch_cookie = parent.document.cookie;
	}
	var coo_len = ch_cookie.length;
	var i = 0;

	while (i < coo_len) {
		var j = i + ch_len;
		if (ch_cookie.substring(i, j) == ch_nom)
			return litValCookie(ch_cookie, j);
		i = ch_cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break;
	}

	return null;
}

function suppCookie(nom) {
	var exp = new Date();
	exp.setTime(exp.getTime()-1);
	parent.opener.document.cookie = nom + "=null; expires=" + exp.toGMTString();
}


// Fonctions spécifiques utilisant les cookies

function ajouteNotice(val, selection) {
//	var chaine = parent.gauche.document.cookie;
	var nomCookie = "liste_notices";
	var valCookie = litCookie(nomCookie);

	var ecrit = 0;

	if ( ! valCookie ) { // pas de notices mémorisées pour l'instant
		valCookie = val + '/';
		ecrit = 1;
	}
	else {
		//La notice est-elle déjà présente dans la liste ?
		var trouve = false;
		var tabNotices = valCookie.split("/");
		
		for (var i = 0; i < valCookie.length - 1; i++) {
			if (val == tabNotices[i]) {
				trouve = true;
				break;
			}
		}

		if ( trouve ) {
			if (selection) {
				return 0;
			}
			alert('Cette notice est déjà dans la liste');
		}
		else {
			valCookie += val + '/';
			ecrit = 1;
		}
	}

	if (ecrit) {
		ecritCookie(nomCookie, valCookie);
		if (selection) { // ajout à partir d'une sélection de notices
			return 1;
		}
		alert("Notice ajoutée au panier de notices");
	}
}


function ajouteNoticesSel(valSel) { // fonction permettant d'ajouter une sélection de notices
									// (à partir d'une page de résultats) au panier
	var tabNotices = valSel.split("/");
	var i = 0;
	var nbAj = 0;
	for (i=0;i<tabNotices.length;i++) {
		if (tabNotices[i]) {
			nbAj += ajouteNotice(tabNotices[i], 1);
		}
		else {
			break;
		}
	}

	var msg = "";
	if (nbAj) {
		if (i > nbAj) {
			msg = nbAj+" notice(s) ajoutée(s) et "+(i-nbAj)+" déjà présente(s) dans le panier";
		}
		else {
			msg = nbAj+" notice(s) ajoutée(s)";
		}
	}
	else {
		if (i < 1) {
			msg = "Aucune notice sélectionnée !";	
		}
		else {
			msg = "Aucune notice ajoutée (déjà présente(s) dans le panier) !";
		}
	}
	alert(msg);
}


function ajouteNumNotice(num,etat) {
	var chaine = parent.principale.document.monform.notices.value
	if (etat){
		chaine += num+"/";
	}
	else {
		chaine = enleveNotice(num, chaine);
	}
	//alert(chaine);
	parent.principale.document.monform.notices.value = chaine;
}


function effaceNoticesSelec() {
	var noticesSel = 0;
	var fini = 0;
	var nomCookie = "liste_notices";
	var valCookie = litCookie(nomCookie, 1);

	if (valCookie) {
		var chaine = parent.principale.document.monform.notices.value;
		//alert(chaine);
		if (chaine.length > 0){
			noticesSel = 1;
			//alert("il faut enlever les notices "+chaine); 
			var chaine2 = valCookie;
			//alert("dans le cookie "+chaine2); 
			while (!fini){
				s = chaine.indexOf("/");
				if (s>0){
					num = chaine.substring(0, s)
					chaine = enleveNotice(num,chaine);
					//alert("chaine "+chaine);
					chaine2 = enleveNotice(num,chaine2);
					//alert("chaine2 "+chaine2);
				}
				else {
					fini = 1;
					//alert('fini');
				}
			}

			if (chaine2.length == 0) { //revient à "vider"
				//alert("il faut supprimer tout");
				var rep = false;
				rep = confirm("Voulez-vous supprimer toutes les notices du panier ?");
				if (rep) { 
					suppCookie(nomCookie);
					parent.document.location = "about:blank";
					parent.window.close();
				}
				else {
					return;
				}
			}
			else {
				ecritCookie(nomCookie, chaine2, 1);
				//alert(parent.opener.document.cookie);
			}
		}
	}

	if (noticesSel) {
		var chaineCookie = "";
		var nomCookie = "liste_notices";
		var valCookie = litCookie(nomCookie, 1);
		chaineCookie = nomCookie + "=" + valCookie;
		if (document.contexte.format.value == "ISBD")
			parent.principale.document.location = CGIBIN + "panier_ISBD.pl?" + chaineCookie;
		else
			parent.principale.document.location = CGIBIN + "panier.pl?" + chaineCookie;
	}
	else {
		alert("vous n'avez pas sélectionné de notice");
	}
}


function enleveNotice (n,c) {
	var re = /\d/;
	var aux = c;
	var trouve = 0;
	var pos = -1;

	while (!trouve) {
		pos = aux.indexOf(n, pos+1);
		var carAp = aux.charAt(pos+n.length); //caractère de la liste situé juste après la chaine cherchée
		//alert ("après "+carAp);
		if (carAp.match(re)) { //notice incluse dans une autre
			//alert("ce n'était pas la bonne notice");
			continue;
		}
		else { //bonne notice
			aux = c.substring(0, pos)+ c.substring(pos+n.length+1, c.length);
			c = aux;
			trouve = 1;
		}
	}

	return c;
}


function effaceCookie() {
	var nomCookie = "liste_notices";
	//var valCookie = litCookie(nomCookie, 1);
	//alert("le cookie est :"+chaine);

	var rep = false;
	rep = confirm("Voulez-vous supprimer toutes les notices du panier ?");
	if (rep) {
		suppCookie(nomCookie);
		parent.document.location = "about:blank";
		parent.window.close();
	}
}


function quitter() {
	if (parent.principale.document.monform.notices.value) {
		var rep = false;
		rep = confirm("voulez-vous effacer les notices sélectionnées?");
		if (rep) {
			effaceNoticesSelec();
		}
	}
	parent.window.close();
}



function formulaireAdresse() {
//	if (parent.opener.document.cookie) {
		var optFen="dependant=yes,scrollbars=no,resizable=no,height=300,width=400,top=50,left=100";
		var fen_form = open(BIB + "formAdresse.html","fen_form",optFen);
/*	}
	else {
		alert("Il n'y a aucune notice !");
	}
*/
}


function imprimePanier() {
//	var nomMach = parent.principale.document.monform.nomMachine.value;
//	if (nomMach) { alert("Vous êtes sur la machine " + nomMach); }
	if (navigator.appName.match(/netscape/i)) {
		parent.principale.print();
	}
	else {
		parent.principale.focus();
		window.print();
	}
}

function changeFormat() {
	var nomCookie = "liste_notices";
	var chaineCookie = nomCookie + "=" + litCookie(nomCookie, 1);

	with (document.contexte.format) {
		if (value == "ISBD") {
			parent.principale.document.location = CGIBIN + 'panier.pl?' + chaineCookie;
			value = "TAB";
		}
		else {
			parent.principale.document.location = CGIBIN + 'panier_ISBD.pl?' + chaineCookie;
			value = "ISBD";
		}
	}
}

//-->

//-->
