// Renvoi de la longueur des tableaux d'articles dans le memento
function CdeLongueurMemento()
{

	//alert("Début calcul longueur") ;

	var i=0 ;
	while ((CdeReferenceMemento[i]!="") && (CdeReferenceMemento[i]))
	{
		i+=1 ;
	}
	
	return i ;
}

// Ajout d'articles dans le memento électronique
function CdeAjoutMemento(Reference,Intitule,Prix,Quantite,URL)
{

	//alert("Dans la fonction d'ajout");
	if (Quantite == 0 )
	{
		alert("Saisissez une quantité pour ajouter cet article au memento") ;
	}
	else
	{

		var dejala = 0 ;
		var i = CdeLongueurMemento() ;

		for (var j=0 ; j<i ; j++)
		{

			//alert(CdeReferenceMemento[j]+"=="+Reference) ;
			//if (CdeReferenceMemento[j]==Reference)
			if (CdeURLMemento[j]==URL)
			{
				CdeQuantiteMemento[j]=(CdeQuantiteMemento[j]-Quantite+(2*Quantite)) ;
				dejala = 1 ;
			}

		}

		if (dejala == 0)
		{

			CdeReferenceMemento[i]=Reference ;
			CdeIntituleMemento[i]=Intitule ;
			CdePrixMemento[i]=Prix ;
			CdeQuantiteMemento[i]=Quantite ;
			CdeURLMemento[i]=URL ;

		}
		SauveMemento() ;
	}
	
}

// Suppression d'un article
function CdeSupprimeMemento(ASupprimer)
{

	var j = CdeLongueurMemento() ;

	//alert(j);
	for (var i = ASupprimer; i<j-1; i++)
	{

	//alert(i);
	CdeReferenceMemento[i]=CdeReferenceMemento[i+1];
	CdeIntituleMemento[i]=CdeIntituleMemento[i+1];

	//alert(CdeIntituleMemento[i]);
	CdePrixMemento[i]=CdePrixMemento[i+1] ;
	CdeQuantiteMemento[i]=CdeQuantiteMemento[i+1];
	CdeURLMemento[i]=CdeURLMemento[i+1];

	}

	CdeReferenceMemento[i]="";
	CdeIntituleMemento[i]="";
	CdePrixMemento[i]="";
	CdeQuantiteMemento[i]="";
	CdeURLMemento[i]="";
	SauveMemento() ;

}

// Sauvegarde du memento
function SauveMemento()
{

	var TexteCookie = "" ;
	var j = CdeLongueurMemento() ;
	
	for (var i = 0; i<j; i++)
	{
		//alert(i);
		//CdePrixMemento[i] = "&nbsp;" ;
		TexteCookie = TexteCookie + CdeReferenceMemento[i] + "|" + CdeIntituleMemento[i] + "|" + CdePrixMemento[i] + "|" + CdeQuantiteMemento[i] + "|" + CdeURLMemento[i] + "||" ;
		//alert (TexteCookie) ;
	}

	var expdate = new Date ();
	expdate.setTime (expdate.getTime() + (60 * 60 * 24 * 365));
	setCookie("mementoAtisreal BNPPI",TexteCookie,expdate) ;

	//alert (TexteCookie) ;
}

// Affichage du memento

function AfficheMemento()
{

	var i=0 ;
	var Total=0 ;
	var aucun_article ;
	var memento ;
	
	aucun_article = "True" ;
	memento = "False" ;
	bgcolor = "" ;
	bordercolor = "#336699" ;
	presence_prix = "False" ;
	
	// On test si au moins un article est dans le memento
	while ((CdeReferenceMemento[i] != "") && (CdeReferenceMemento[i]))
	{
		aucun_article = "False" ;
		if (CdePrixMemento[i] != "" && CdePrixMemento[i] != "&nbsp;" && CdePrixMemento[i] != " ")
		{
			presence_prix = "True" ;
			CdePrixMemento[i] = CdePrixMemento[i] + " &#8364;" ;
		}
		i+=1;
	}
	// Si au moins un article est dans le memento, on affiche le memento
	if (aucun_article == "False")
	{
		// bordercolor='"+bordercolor+"'
		document.write("<table class='memento_table'>") ;
		document.write("<tr>") ;
		document.write("<td class='StyleTableau1'>Réf.</td>") ;
		document.write("<td class='StyleTableau1'>Intitulé</td>") ;
		
		if (presence_prix == "True")
		{
			document.write("<td class='StyleTableau1'>Prix</td>") ;
		}
		document.write("<td class='StyleTableau1'>&nbsp;</td>") ;
		document.write("</tr>") ;

		i=0 ;
		while ((CdeReferenceMemento[i]!="") && (CdeReferenceMemento[i]))
		{
			
	   		document.write("<tr>");
	   		document.write("<td class='StyleTableau3'>"+CdeReferenceMemento[i]+"&nbsp;&nbsp;</td>");
	   		document.write("<td class='StyleTableau3'><a href=\"" + CdeURLMemento[i] + "\" class='StyleTableau3'>"+CdeIntituleMemento[i]+"</a>&nbsp;&nbsp;</td>");
			if (presence_prix == "True")
			{
	   			document.write("<td class='StyleTableau3'>"+CdePrixMemento[i]+"</td>");
			}
	   		document.write("<td class='StyleTableau3' style='vertical-align:bottom;text-align:center'>");
	   		document.write("&nbsp;<a href='javascript:CdeSupprimeMemento("+i+");location.reload();' class=\"StyleTableau3\">Supprimer</a>&nbsp;");
			document.write("</td>");		
	   		document.write("</tr>");
	
			i+=1;
		
		}
		document.write("</table>") ;
	}
	else
	{
		document.write("<p>Votre sélection est vide.</p>");
	}
}

// *************************************************************************
// Début du code proprement dit : lecture du memento dans les cookies et
// initialisation des différents tableaux

CdeReferenceMemento = new Array() ;
CdeIntituleMemento = new Array() ;
CdePrixMemento = new Array() ;
CdeQuantiteMemento = new Array() ;
CdeURLMemento = new Array() ;

//Récupération du memento en cookies
MementoString = getCookie("mementoAtisreal BNPPI") ;

//Remplissage du tableau avec les valeurs récupérées dans le cookie

re = /\|\|/ ;

if (MementoString)
{
	MementoStringArray = MementoString.split(re) ;
	//alert (MementoStringArray[0]) ;
	
	for ( i = 0; i < MementoStringArray.length; i++)
	{
		ItemString = MementoStringArray[i] ;
		//alert (ItemString) ;
		re = /\|/ ;
		ItemStringArray = ItemString.split(re) ;
		//alert (ItemStringArray[0]) ;
		CdeReferenceMemento[i] = ItemStringArray[0] ;
		CdeIntituleMemento[i] = ItemStringArray[1] ;
		CdePrixMemento[i] = ItemStringArray[2] ;
		CdeQuantiteMemento[i] = ItemStringArray[3] ;
		CdeURLMemento[i] = ItemStringArray[4] ;
		
	}
}

// *************************************************************************
// Suppréssion du cookie
function f_effacer_cookie()
{
	var expdate = new Date ();
	expdate.setTime (expdate.getTime());
	setCookie("mementoAtisreal BNPPI",null,expdate) ;
}
