function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}


function setCookie(name, value, expire) { 
	if (expire != null) { 
		  var today = new Date() 
		  var expires = new Date() 
		  expires.setTime(today.getTime() + 60*60*24*1000*expire) 
		  expire = expires 
	} 
		  document.cookie = name + "=" + escape(value) 
		  + ((expire == null) ? "" : ("; expires=" + expire.toGMTString())) 
} 


function traitement(chaine,b){
	var nom		=new Array();
	var fichier	=new Array();
	var poids	=new Array();
	var ico=new Array();
	var deb	=new Array();
	var fin = new Array();
	var pdf=new Array();
	var url=new String(chaine);
	tableau ="";
	strFichier = "";
	strPoids = "";
	strPDF = "";
	
	j = 0;
	k = 0;

	//------------ Traitement des lignes --------------
	// On scinde la chaine dans un tableau à n éléments
	nom=url.split("¤");
	for (i=1;i<nom.length;i++){
		if (i%2 != 0){
			fichier[k] = nom[i];
			// Extrait un pdf
			deb[k] = fichier[k].indexOf("*");
			fin[k] = fichier[k].lastIndexOf("*");
			pdf[k] = fichier[k].slice(deb[k],fin[k]);			
			pdf[k] = pdf[k].substr(1,fin[k]);
			// Extrait le nom
			fichier[k] = fichier[k].slice(0,deb[k]);
			k = k+1;
		}
		else{
			// Poids du fichier
			poids[j] = nom[i];
			j = j+1;
		}
	}


	// On vire les doublons-------------------------------
	for(i=0;i<pdf.length;i++){
			// On kill les doublons
			if(strPDF.indexOf(pdf[i]) == -1){
				strPDF 		= strPDF + pdf[i]+"¤";
				strFichier 	= strFichier + fichier[i]+ "¤";
				strPoids 	= strPoids + poids[i]+ "¤";
			}
	}
	
	fichier = strFichier.split("¤");
	pdf		= strPDF.split("¤");
	poids	= strPoids.split("¤");

	fichier.pop();
	pdf.pop();
	poids.pop();
	//-------------------------------------------------------

//----------Tableau HTML-------------------------------------
	nblignes = fichier.length;
	if (b == "ok"){	
		for (i=0;i<nblignes;i++){
			if (i%2 == 0){color="#FFFFCB";}
			else color="#FFFFFF";
				tableau = tableau + "<tr><td width=5% bgcolor='"+color+"'>&nbsp;<img src='images/pdf.gif' align='absmiddle'></td><td width=5% bgcolor='"+color+"'><input name=cb type='checkbox' value='#"+fichier[i]+"*"+pdf[i]+"*"+"¤"+poids[i]+"'></td><td width=75% bgcolor='"+color+"'><a href=\"javascript:OuvrePopup(\'pdf/"+strCatalogue+"/"+pdf[i]+"\','')\">"+fichier[i]+"</a></td><td width=15% bgcolor='"+color+"'><a href='download.asp?file=pdf/"+strCatalogue+"/"+pdf[i]+"&strCat="+strCatalogue+"'><img border='0' align='absmiddle' src='images/download.gif' width='15' height='16'></a>&nbsp;"+poids[i]+" Ko</td></tr>";
		}
		tableau = "<table width='100%' cellpadding=0 cellspacing=0 border=0>"+tableau+"</table>";
	}
	else{
		for (i=0;i<nblignes;i++){
			if (i%2 == 0){color="#FFFFCB";}
			else color="#FFFFFF";
				tableau = tableau + "¤"+fichier[i]+"*"+pdf[i]+"*"+"¤"+poids[i];
		}		
	}	
	return tableau;
//--------------------------------------------------------------------
}

function Checkall(nom_checkbox){
// Coche toutes les checkbox
	max2 = eval("document.docs."+nom_checkbox+".length");
	for(i=0;i<max2;i++){
		eval("document.docs."+nom_checkbox+"["+i+"].checked = true");
	}

// Si il y a une seule checkbox affichée (ex : qd search.asp affiche une seule page)
	if(document.docs.cb.value != null){
		document.docs.cb.checked = true;
	}
	
}

function getCookie(Name) { 
// Récupération de la valeur du cookie
          var search = Name + "=" 
          if (document.cookie.length > 0) { // si il existe au moins un cookie 
                    offset = document.cookie.indexOf(search) 
					if (offset != -1) { // si le cookie recherché existe 
                              offset += search.length ; 
                              //détermine la position de début de la valeur du cookie. 
                              end = document.cookie.indexOf(";", offset) 
                              // détermine la position de la fin de la valeur du cookie 
                              if (end == -1) 
                                        end = document.cookie.length 
                              return unescape(document.cookie.substring(offset, end)) 
					} 
          } 
}



function DeleteCookie(CatList) {
	// Fait expirer un cookie
	if (getCookie("Sch") != null) {
		document.cookie="Sch=Sch;expires=Thu, 01-Jan-1970 00:00:01 GMT";    
	}
	location.reload();
}

function DeleteItemCookie(strItem) {
	if (getCookie("Sch") != null) {
			cookietmp = getCookie("Sch");
			cookietmp = cookietmp.replace(strItem, "");
			setCookie("Sch", cookietmp, 5);
	}
	location.reload();
}

function Check(){
// Détecte les checkbox cochées et retourne les valeurs
	valchecked ="";
	maxcheck = document.docs.cb.length;
	for(i=0;i<maxcheck;i++)
	  {
		if (document.docs.cb[i].checked)
		{
			val = document.docs.cb[i].value;
			// Valchecked est la valeur qui va être enregistrée dans le cookie
			valchecked = valchecked + val;
		}
	  }
	val = "";
	return valchecked;
}


function switchAll(CatList){
// Suppression des élements sélectionnés et enreg ds cookie
	val_switch = "";

	// aCatList[i] = Catalogue
	aCatList = CatList.split("¤");

	for(i=0;i<aCatList.length;i++){
		
		if (aCatList[i] != ""){
			nbmax =  eval("document.docs." + aCatList[i] + ".length");

			// On supprime les bonnes infos
			if(nbmax == null){
				// Cas où le checkbox est unique
				if(eval("document.docs." + aCatList[i] + ".checked") == true){
					eval("document.docs." + aCatList[i] + ".checked = false");
				}
				else{
					eval("document.docs." + aCatList[i] + ".checked = true");
				}
			}
			else{
				// On inverse les checkbox de type strCat
				for (var k=0;k<nbmax;k++){
				box = eval("document.docs." + aCatList[i] + "[" + k + "]");
				box.checked =! box.checked;
				}				
			}

			valchecked ="";
			maxcheck = eval("document.docs." + aCatList[i] +".length");
		
			// Stockage des checkbox à garder dans le ccokie
			if(nbmax == null){
				// Cas où le checkbox est unique			
				if (eval("document.docs."+  aCatList[i] + ".checked") ){
					val = eval("document.docs."+  aCatList[i] + ".value")
					// Valchecked est la valeur qui va être enregistrée dans le cookie
					valchecked = valchecked + val;					
				}
			}
			else
			{
				for(j=0;j<maxcheck;j++)
				  {
					if (eval("document.docs."+  aCatList[i] + "[j].checked") )
					{
						val = eval("document.docs."+  aCatList[i] + "[j].value")
						// Valchecked est la valeur qui va être enregistrée dans le cookie
						valchecked = valchecked + val;
					}
				  }
			}
		val_switch = valchecked;

		// On met à jour le cookie
		setCookie("Sch"+ aCatList[i] ,val_switch,5);

		}
	}

	location.reload();
}


function CheckCookie(val)
{
   if(val == "NAV"){
	   if (getCookie("Sch"+strCatalogue) != null){// Cookie existant + nouvelles valeurs
	   		strCurrent = document.navcat.panier.value;
			strValNav = traitement(strCurrent,"KO");
			val_exist = (getCookie("Sch"+strCatalogue));
			setCookie("Sch"+strCatalogue,val_exist+strValNav,5);						   
			
	   }
	   else{
	   		strValNav = document.navcat.panier.value;	   
			strValNav = traitement(strValNav,"KO");
			setCookie("Sch"+strCatalogue,strValNav,5);	   		
			
	   }		
   }
   else{
	   if (getCookie("Sch"+strCatalogue) != null){// Cookie existant + nouvelles valeurs
			// Valeur du cookie
			val_exist = (getCookie("Sch"+strCatalogue));
			// Valeur cochées
			val_check = Check();
			setCookie("Sch"+strCatalogue,val_exist+val_check,5);		
	   }
	   else{// Nouveau Cookies
			val_check = Check();
			setCookie("Sch"+strCatalogue,val_check,5);
	   }
   }
   
// Si il y a une seule checkbox affichée (ex : qd search.asp affiche une seule page)
   if(document.docs.cb.checked){
	   if (getCookie("Sch"+strCatalogue) != null){// Cookie existant + nouvelles valeurs
			// Valeur du cookie
			val_exist = (getCookie("Sch"+strCatalogue));
			// Valeur cochées
			val_check = document.docs.cb.value;
			setCookie("Sch"+strCatalogue,val_exist+val_check,5);		
	   }
	   else{// Nouveau Cookies
			val_check = document.docs.cb.value;
			setCookie("Sch"+strCatalogue,val_check,5);
	   }   	
   }
}

function CheckCookieMode2(){
   strNumPage2 = strNumPage;
   if ( strNumPage2.length == 1 ) strNumPage2 = "00" + strNumPage2;
   if ( strNumPage2.length == 2 ) strNumPage2 = "0" + strNumPage2;
   if (getCookie("Sch"+strCatalogue) != null){// Cookie existant + nouvelles valeurs
		// Valeur du cookie
		val_exist = (getCookie("Sch"+strCatalogue));
		// Valeur cochées
		val_check = "*" + strCatalogue.toUpperCase() + strChapter + "P" + strNumPage2 + ".PDF";
		setCookie("Sch"+strCatalogue,val_exist+val_check,5);		
   }
   else{// Nouveau Cookies

		val_check = "*"+ strCatalogue.toUpperCase() + strChapter + "P" + strNumPage2 + ".PDF";
		setCookie("Sch"+strCatalogue,val_check,5);
   }   	

}

function OpenPopupCenter(url, titre, largeur, hauteur){
	var winl = (screen.width - largeur) / 2; 
	var wint = (screen.height - hauteur) / 2; 
	winprops = 'height=' + hauteur + ', width=' + largeur + ',top=' + wint + ',left=' + winl + ',scrollbars=1,resizable' ;
	win = window.open(url ,titre, winprops);
	win.focus();
}

function MsgPanier(){
	// On ouvre la fenêtre qui affiche le contenu de la sélection
	OpenPopupCenter("selection_information_" + document.getElementById("langue").value + ".asp", "", 300, 140);
}

function CheckCookieMode_1_Force(){ // Enregistrement des pages ds le cookie en mode 1 forcé
	// Init
	var objCb = document.forms["docs"].elements["cb"];
	var idCat = document.forms["recherche"].elements["cat"].value;
	if (objCb[0] == null){ // Une seule case à cocher

		// Init
		var strValCb = objCb.value
		var val_exist;
		
		// On extrait le fichier PDF de la chaine
		strValPdf = strValCb.slice(strValCb.indexOf("*")+1,strValCb.lastIndexOf("*"))
		if(objCb.checked){ // On traite que les cases cochées
		
			// On enregistre dans le cookie
			if (getCookie("Sch" + idCat) != null){// Cookie existant + nouvelles valeurs
				// On récupère la valeur du cookie
				val_exist = getCookie("Sch" + idCat);
				//Enreg ds le cookie
				setCookie("Sch" + idCat, val_exist + "*" + strValPdf, 5);		
			}
			else{// Nouveau Cookie
				setCookie("Sch" + idCat, "*" + strValPdf, 5);
			}
			
		}
	}else{// plusieurs cases à cocher
	
		for(var i=0;i<objCb.length;i++){ // On boucle sur les Cbox
			// Init
			var strValCb = objCb[i].value
			var val_exist;
			
			// On extrait le fichier PDF de la chaine
			strValPdf = strValCb.slice(strValCb.indexOf("*")+1,strValCb.lastIndexOf("*"))
					
			if(objCb[i].checked){ // On traite que les cases cochées
			
				// On enregistre dans le cookie
				if (getCookie("Sch" + idCat) != null){// Cookie existant + nouvelles valeurs
					// On récupère la valeur du cookie
					val_exist = getCookie("Sch" + idCat);
					//Enreg ds le cookie
					setCookie("Sch" + idCat, val_exist + "*" + strValPdf, 5);		
				}
				else{// Nouveau Cookie
					setCookie("Sch" + idCat, "*" + strValPdf, 5);
				}
				
			}
		}
	}
}

function AddPanier( val ){ 
	strCatalogue = "";
	if( confirm("Voulez-vous ajouter le document à la sélection ?") ){
		// Si il y a une seule checkbox affichée (ex : qd search.asp affiche une seule page)
		if (getCookie("Sch"+strCatalogue) != null ){// Cookie existant + nouvelles valeurs
			
			val_exist = (getCookie("Sch"+strCatalogue));
			
			if(val_exist != val){			
				// Valeur cochées
				val_check = val;
				setCookie( "Sch" + strCatalogue,val_exist + val_check,5 );
			}	
		}
		else{// Nouveau Cookies
			val_check = val;
			setCookie("Sch"+strCatalogue,val_check,5);
		}   	

	}
}

function AddPanierCatalogue( val, currentCatalogue ){ 
	// Si il y a une seule checkbox affichée (ex : qd search.asp affiche une seule page)
	if (getCookie("Sch" + currentCatalogue) != null){// Cookie existant + nouvelles valeurs
			// Valeur du cookie
			val_exist = (getCookie("Sch" + currentCatalogue));
			// Valeur cochées
			val_check = val;
			setCookie("Sch" + currentCatalogue, val_exist + val_check, 5);		
	}
	else{// Nouveau Cookies
			val_check = val;
			setCookie("Sch" + currentCatalogue, val_check, 5);
	}   	

}
