var NB_MAX_ELEMENT=1000;
var STRING_ALERT="Vous ne pouvez plus ajouter de résultat !";


// Function FixeCookie
// pour créer ou modifier un cookie
function SetCookie (nom,valeur,expire,path,domaine,securise) {
document.cookie = nom + "=" +valeur + ((expire) ? "; expires=" + expire.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domaine) ? "; domain=" + domaine : "") + ((securise) ? "; secure" : "");
}

// Function FixeCookie - pour créer ou modifier un cookie
function FixeCookie (nom) {
	document.cookie = nom + "=@" +";PATH=/";
}

// Function RazCookie - pour reinitialiser un cookie
function RazCookie (nom) {
	FixeCookie(nom);
}

// Function SupprCookie - Supprime un Cookie
function SupprCookie (nom) {
	if (GetValeurCookie(nom)) {
		document.cookie = nom + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

// Function ValeurCookie - Utiliser par GetValeurCookie
function ValeurCookie (Pos) {
	var endstr = document.cookie.indexOf (";", Pos);
	
	if (endstr == -1)

	endstr = document.cookie.length;

	return document.cookie.substring (Pos, endstr);
}

// Function GetValeurCookie - pour récupérer la valeur d'un cookie
function GetValeurCookie (nom) {
	var cookielength = document.cookie.length;
	var arg = nom + "=";
	var arglength = arg.length;
	var i = 0;
	
	while (i < cookielength) {
		var j = i + arglength;
		if (document.cookie.substring(i, j) == arg)
		return ValeurCookie (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	
	return null;
}

// Change la valeur du cookie
function ChgtValeurCookie(nomcook,action,index) {
	var recherche = nomcook + "=";
	var pre_val = "";
	var suite = index + "@";
	
	if (document.cookie) {
		var offset = document.cookie.indexOf(recherche);
		
		if (offset != -1) {
			offset += recherche.length;
			var fin = document.cookie.indexOf(";",offset);
			
			if ((fin == -1) || (fin == ''))
				fin = document.cookie.length;
				
			pre_val = document.cookie.substring(offset,fin);
			
			if (action == "AJOUT") {
				// Limite de NB_MAX_ELEMENT selection...
				//if (CompteNbElt(nomcook) >= NB_MAX_ELEMENT) {
					//alert(STRING_ALERT);
					//return false;
				//} else {
				if (AlreadyInList(pre_val, "@"+suite) == false)
					pre_val = pre_val + suite;
				//}
			}
			
			if (action == "SUPPR") {
				if (AlreadyInList(pre_val, "@"+suite) == true) {
					//alert("Avant SupprValeur :" + pre_val);
					pre_val=SupprValeur(pre_val, suite);
					//alert("Apres SupprValeur :" + pre_val);
				}
			}
			document.cookie = nomcook + "=" + pre_val + ";PATH=/";
			return true;
		} else {
			document.cookie = nomcook + "=" + suite + ";PATH=/";
			return true;
		}
	} else {
		document.cookie = nomcook + "=" + suite + ";PATH=/";
		return true;
    }
}

// Verifie si valeur est present dans la chaine
function AlreadyInList(chaine, valeur) {
	//alert("AlreadyInList IN  - cherche " + valeur + " dans " + chaine);
	var offset = chaine.indexOf(valeur);
	if (offset != -1) {
		//alert("Already in List");
		return true;
	} else {
		//alert("Not in List");
		return false;
	}
}

// Supprime la valeur de la chaine
function SupprValeur(chaine, valeur) {
	var newChaine = "";
	
	var debut = chaine.indexOf(valeur);
	var fin = debut + valeur.length;
		
	newChaine = chaine.substring(0,debut);
	newChaine = newChaine + chaine.substring(fin, chaine.length);
	return newChaine;
}

// Nombre d'element stocké dans le cookie
function CompteNbElt(nomcook) {
	
	var tableau = SplitCookie(nomcook);
	var NbElt = 0;
	
	if (tableau != null)
		NbElt = tableau.length;
		
	return NbElt;	
}

function SplitCookie(nomcook) {
	var chaine = GetValeurCookie(nomcook);
	var reg=new RegExp("[@]+", "g");
	
	//alert("Chaîne d'origine : " + chaine);
	
	if (chaine.charAt(0) == "@") 
		chaine = chaine.substring(1,chaine.length);
	
	if (chaine.charAt(chaine.length-1) == "@") 
		chaine = chaine.substring(0,chaine.length-1);
	
	//alert("Chaîne a spliter : " + chaine);
	
	if (chaine.length > 0) {
		return chaine.split(reg);
	}
	return null;
}