// JavaScript Document

function addToCaddie (eltype, id) {
	var el = id.substr(13); //pour sauter "serp_element_" et ne garder que le chiffre qui suit
	var found = false;
	for (var i=0; i<Panier[eltype].length; i++) {
		if (Panier[eltype][i]['id']==el) {
			found = true;
			break;
		}
	}
	if (!found) ajoutPanier(eltype, el, Login);
}


function deleteFromCaddie (typlist,id) {
	suppPanier(typlist, id, Login);
}


function afficherOneListe (typlist) {
	// on supprime le contenu de liste actuelle du DOM
	var liste, noeud, eltype, nb;
	liste = document.getElementById('panier_'+typlist+'s');
	while (noeud=liste.firstChild) {
		liste.removeChild(noeud);
	}
	
	// on remplit la liste ul
	for (i=0; i<Panier[typlist].length; i++) {
		var li, a, h4, p, img, x, str, contenu;
		
		// chaque li va contenir 1 img, 2 p et la croix pour supprimer
		li = document.createElement('li');

		// img
		img = document.createElement('img');
		img.setAttribute('src',Panier[typlist][i]['vignette']);
		img.setAttribute('alt',Panier[typlist][i]['nom']);
		img.setAttribute('width','53');
		img.setAttribute('height','53');
		img.setAttribute('class',''); //FF
		img.setAttribute('className',''); //IE
		li.appendChild(img);
		
		// la img dans a
		a = document.createElement('a'); 
		a.setAttribute('href',Panier[typlist][i]['lienfiche']);
		a.appendChild(img);
		li.appendChild(a);
		
		// h4
		h4 = document.createElement('h4');
		contenu = document.createTextNode(Panier[typlist][i]['nom']);
		h4.appendChild(contenu);
		li.appendChild(h4);
		
		// le h4 dans a
		a = document.createElement('a'); 
		a.setAttribute('href',Panier[typlist][i]['lienfiche']);
		a.appendChild(h4);
		li.appendChild(a);
		
		// p
		p = document.createElement('p');
		str = Panier[typlist][i]['info'];
		contenu = document.createTextNode(str);
		p.appendChild(contenu);
		li.appendChild(p);

		// la x pour fermer
		x = document.createElement('img');
		x.setAttribute('src',Root+'design/picto_del.gif');
		x.setAttribute('alt','Del');
		x.setAttribute('width','9');
		x.setAttribute('height','9');
		x.setAttribute('class','x_close'); //FF
		x.setAttribute('className','x_close'); //IE
		
		// la x dans a
		a = document.createElement('a'); 
		a.setAttribute('href','#');
		//a.setAttribute('onclick',"deleteFromCaddie('etablissement',"+Panier[typlist][i]['id']+")"); 
		//cette ligne est ok mais ne fonctionne pas sous ie, donc les 3 lignes de modif qui suivent 
		a.i = i;
		a.typlist = typlist;
		a.onclick = function(){deleteFromCaddie(typlist,Panier[this.typlist][this.i]['id']);};
		a.appendChild(x);
		li.appendChild(a);
		
		// on ajoute le li construit dans le ul
		liste.appendChild(li);
	}
}


function afficherPanier () {
	//
	// pour les etablissements
	//
	afficherOneListe('etablissement');
	remplacerContenu("panier_etab_nb", Panier['etablissement'].length);
	
	//
	// pour les incentives
	//
	//afficherOneListe('incentive');
	//remplacerContenu("panier_incent_nb", Panier['incentive'].length);
	
	//
	// pour la nav user
	//
	remplacerContenu("nb_selections", Panier['etablissement'].length + Panier['incentive'].length);
}


function stylePanier (etat) {
	var pan = document.getElementById('form_validation_panier');
	var ind_choisir = document.getElementById('indication_choisir');
	var ind_glisser = document.getElementById('indication_glisser');
	var ind_deposer = document.getElementById('indication_deposer');
	
	switch (etat) {
		case 'choisir':
			pan.style.backgroundImage = "url("+Root+"design/fond_bas_panier.jpg)";
			ind_choisir.style.display = 'block';
			ind_glisser.style.display = 'none';
			ind_deposer.style.display = 'none';
			break;
		case 'glisser':
			pan.style.backgroundImage = "url("+Root+"design/fond_bas_panier_o.jpg)";
			ind_choisir.style.display = 'none';
			ind_glisser.style.display = 'block';
			ind_deposer.style.display = 'none';
			break;
		case 'deposer':
			pan.style.backgroundImage = "url("+Root+"design/fond_bas_panier_o.jpg)";
			ind_choisir.style.display = 'none';
			ind_glisser.style.display = 'none';
			ind_deposer.style.display = 'block';
			break;
	}
}
