var op = navigator.userAgent.toLowerCase().indexOf('opera')> -1 ;
var ie = document.all && !op ;
var ie7 = ie && (navigator.userAgent.toLowerCase().indexOf("msie 7") != -1)
var ieold = ie && !ie7
var tab_type = new Array() ;
var tab_prix_a = new Array() ;
var tab_prix_b = new Array() ;
var tab_quantite = new Array() ;
var tab_prix = new Array() ;
var prix_rv = new Array() ;
var tab_colisage = new Array() ;
var tab_unite = new Array() ;
var option_extra__tab_id_supplement_article = new Array() ;
var tab_quantite_saisie = new Array() ;
var tab_niveau = new Array() ;

// if (!ie) alert('Not Internet Explorer !')
function popup(id_ext, gamme)
{
//	var nom_popup = id_ext ; // popups multiples
	var nom_popup = 'popup' ; // popup unique
	window.open('popup.php?id_ext='+id_ext+'&gamme='+gamme+'&referer=ok', nom_popup, 'height=500, width=400, scrollbars=yes, resizable=yes') ;
	return false; // car appelée sur le onClick d'un lien dont le href vaut #
}


if (ie)
{
	w_ie=20
	h_ie=20
}
else
{
	w_ie=0
	h_ie=0
}

var fenetre_image
function popup_image(objLien)
{
//	var nom_popup = id_ext ; // popups multiples
	var nom_popup = 'popup' ; // popup unique
	
	// récupérer les dimensions de la popup
	var image = objLien.getElementsByTagName('img')[0]
	var h = image.height
	var w = image.width
	if (h>w)
	{
		w = 600*w/h
		h = 600
	}
	else
	{
		h = 600*h/w
		w = 600
	}
	
	w += 20 +w_ie
	h += 20 +h_ie
	
	if (fenetre_image && !fenetre_image.closed)
	{
		fenetre_image.resizeTo(w+10, h+50)
		fenetre_image = window.open(objLien.href, nom_popup)
	}
	else
	{
		fenetre_image = window.open(objLien.href, nom_popup, 'height='+h+', width='+w+', scrollbars=yes, resizable=yes') ;
	}
	return false; // car appelée sur le onClick d'un lien qu'il faut utiliser seulement si javascript est inactif
}
/*
function popup_tombola(id_ext)
{
	window.open('popup_tombola.php?id_ext='+id_ext, id_ext, 'height=500, width=400, scrollbars=yes, resizable=yes') ;
	return false; // car appelée sur le onClick d'un lien dont le href vaut #
}
*/
function popup_correction(id_ext, id_panier)
{
	window.open('popup_correction.php?id_ext='+id_ext+'&id_panier='+id_panier, id_ext, 'height=500, width=400, scrollbars=yes, resizable=yes') ;
	return false; // car appelée sur le onClick d'un lien dont le href vaut #
}
/* Opera : double KO
Array.prototype.in_array = function(val) {
//	for (var i in this) //Opera KO
	for (var i=0 ; i<this.length ; i++)
	{
		if (this[i] == val) 
		{
			return true;
		}
	}
	return false;
};
*/
// var MAX_option_1 = 10 ;

function change_option_1(ce_select) 
{
	
	if (ce_select && ce_select.type =='select-one')
	{
		/*
		alert(ce_select.name);
		alert(ce_select.value);
		alert(ce_select.type);
		// 
	
	Préparation de l'appel de la procédure combinaisons_possibles(), puis de l'affichage de la zone correspondante (tarif, quantité, éventuellement option tertiaire)
	Exemple : select.id = "option-37-0", c'est le cas id_ext = 37, première option primaire (0).
	En pratique il y jusqu'à 3 options primaires, soit un indice de 0 à 2 
	*/
	// regexp = /option-(\d+)-(\d+)/ ;
		regexp = new RegExp('option-(\\w+)-(\\w+)','') ;
		resultat = regexp.exec(ce_select.id)
		if (resultat.length)
		{
			
			id_ext = RegExp.$1 ;
			num_option_primaire = Number(RegExp.$2) ; 
			//alert(ce_select.id+" || "+resultat+" || "+id_ext+" - "+num_option_primaire);
			combinaisons_possibles(id_ext, num_option_primaire) ;
			affiche_zone_article_interne(id_ext) ;
			
		}
		else
		{
			alert(ce_select.id) ;
		}
		
		controle_couleur_souple(ce_select.form) ;
	}
}

function change_option_1_precedent(ce_select) 
{
	/*
	Simplement exécuter le traitement change_option_1(ce_select) sur le select précédent s'il existe
	*/
	// regexp = /option-(\d+)-(\d+)/ ;
	regexp = new RegExp('option-(\\w+)-(\\w+)','') ;
	resultat = regexp.exec(ce_select.id)
	if (resultat.length)
	{
		id_ext = RegExp.$1 ;
		num_option = Number(RegExp.$2)-1 ;
		if (num_option > 0)
		{
			combinaisons_possibles(id_ext, num_option) ;
			// affiche_zone_article_interne(id_ext) ;
		}
	}
	else
	{
		alert(ce_select.id) ;
	}
}

option_non_proposee = new RegExp('^([^\\(]+\\()*([^\\)]+)\\)*$') ; 
function combinaisons_possibles(id_ext, num_option) /* faire une fonction récurssive ? */
{
	/*
	Les options primaires sont affichées dans un certains ordre. Le choix sur la première option élimine certaines possibilités de choix dans les options primaires suivante. Cette fonction 'combinaisons_possibles()' met en forme ceci de façon à ce que le client ne se retrouve pas piégé avec des options toujours incompatibles.
	*/

	/*
	
	articles id_ext-x-y-z
	x déterminé par le select option-id_ext-x
	y par le select option-id_ext-y
	z par le select option-id_ext-z
	...
	-> nb_options = 3 (x, y, z)
	
	mettre en évidence les combinaisons encore possibles
	après que les premiers choix aient été effectués
	pour x-y donné on cherche les z possibles
	
	-- exemple --
	code articles 
		514-0-0-0
		514-0-2-1  x
		514-1-1-1
		514-1-2-0 <-
		514-1-2-1 <-
		514-2-2-2  x
		514-4-4-4
	pour x-y = 1-2, les code articles possibles sont 1-2-0, 1-2-1
	donc les valeurs de z possibles sont : 0, 1
	-> tab[z] = [0, 1]
	
		216-0-0-0
		216-0-0-1
		216-0-0-2
		216-0-1-0 <-
		216-0-1-1 <-
		216-1-0-0
		216-1-0-1
		216-1-1-0  x
	pour x-y = 0-1, les code articles possibles sont 0-1-0, 0-1-1
	donc les valeurs de z possibles sont : 0, 1
	-> tab[z] = [0, 1]
	
	ainsi dans le select-z les options misent en évidence seront 0 et 1
	*/
	
	// ce select concerne l'option y (ex : x-y-z)
	// num_option
	
	// la valeur de l'option y est (ex : y=2)(ex : y=1)
	ce_select = document.getElementById('option-'+id_ext+'-'+num_option) ;
	num_val_option = ce_select.options[ce_select.selectedIndex].value ;
	
	
	nom = 'quantite-'+id_ext 
	liste = document.getElementsByName(nom) ;
	
	/*
	morceaux = liste[0].id.split('-') ;
	
	// il y a 3 options possibles (ex : quantite, 514, x, y, z)(ex : quantite, 216, x, y, z)
	nb_options = morceaux.length - 2;
	*/
	nb_options = nombre_d_options(id_ext) ;
	
	
	// passage en revue des codes articles
	// $1 et $2
	// - part1/3 -
	// - par rapport aux select supérieurs - 
	
	indice = new Array() ;
//	texte_indice = 'indice[' ;
	for (j=0 ; j<nb_options ; j++) 
	{
		indice[j] = new Array() ;
//	texte_indice += j+' [' ;
		ce_select_options = document.getElementById('option-'+id_ext+'-'+j).options
		for (k=0 ; k<ce_select_options.length ; k++)
		{
			indice[j][ce_select_options[k].value] = k ;
//	texte_indice += ce_select_options[k].value+'=>'+k+', ' ;
		}
//	texte_indice += '], ' ;
	}
//	texte_indice += ']' ;
//	alert(texte_indice) ;
//	for (j=0 ; j<num_option ; j++) concat+= document.getElementById('option-'+id_ext+'-'+j).selectedIndex+'-' ; 
	concat='^' ;
	debut=num_option+1 ;
	for (j=0 ; j<num_option ; j++) 
	{
		ce_select = document.getElementById('option-'+id_ext+'-'+j)
		concat+= ce_select.options[ce_select.selectedIndex].value+'-' ; 
	}
	concat += num_val_option ;
	regexp = new RegExp (concat) ; 
	
	// alert(num_option+' '+concat) ;
	// alert(regexp.toString()) ;
/*
 *	Pour la mise en évidence des options possibles dans les selects inférieurs
 *	.1 Création du tableau et des expressions régulière de controle
 */
	// creation du tableau de tableaux de valeurs d'option (ex : tab[x] et tab[z])
	// creation du tableau de tableaux de valeurs d'options supérieures (ex : tab[z] et ...)
	tab = new Array() ;
	tab_regexp = new Array() ;
	for (j=debut ; j<nb_options ; j++ )
	{
		tab[j] = new Array() ;
		// - part2/3 -
		// - par rapport au dernier select modifié - tab_regexp[j] = new RegExp('(\\w+-){'+j+'}(\\w+)') ; 
		// - par rapport aux select supérieurs - 
		if (j-debut == 0) tab_regexp[j] = new RegExp(concat+'-()(\\w+)') ; // Netscape 7 ne supporte pas le 'quantifier' 0 : (\w+-){0}
		else tab_regexp[j] = new RegExp(concat+'-(\\w+-){'+(j-debut)+'}(\\w+)') ; 
		// alert(tab_regexp[j].toString()) ;
	}
	tab_option_p1 = new Array() ;
	regexp_option_p1 = new RegExp(concat+'((-\\w+)+)$') ; 
	
/*
 *	.2 Remplissage du tableau des options possibles de l'option j
 */	
	
//	controle_articles_compatibles = '' ;
//	controle_articles_compatibles += regexp.toString()+'\r\n' ;
	for (i=0 ; i< liste.length ; i++)
	{
		// extraction du code article
		texte = liste[i].id.substring( nom.length+1, liste[i].id.length)
		
		// si c'est un article avec la bonne valeur de l'option (ex : y==1)
/*
		if (regexp.exec(texte)) // && RegExp.$2==num_val_option) 
*/
		if (regexp_option_p1.test(texte)) // && RegExp.$2==num_val_option) 
		{
//	controle_articles_compatibles += texte+' - '+num_val_option+'\r\n' ;
			// creation du tableau de valeurs du premier article compatible avec les options déjà choisies
			tab_option_p1.push(texte) ; 

			for (j=debut ; j<nb_options ; j++ ) if (tab_regexp[j].exec(texte) && !in_array(RegExp.$2, tab[j])) 
			{
				tab[j].push(RegExp.$2) ;
			}

		}
		else
		{
//	controle_articles_compatibles += texte+'\r\n' ;
		}
	}
//	alert(controle_articles_compatibles) ; 
//	print_tab(tab_option_p1) ; 
	
	

	
/*
 *	.3 Mise en évidence des options possibles dans les selects inférieurs
 */	
//	 texte = '' ; 
	// - part3/3 -
	// - par rapport au dernier select modifié -  for (j=0 ; j<nb_options ; j++ )
	// - par rapport aux select supérieurs - 
	exp_reg = concat ;
	for (j=debut ; j<nb_options ; j++ )
	{
		if (ce_select = document.getElementById('option-'+id_ext+'-'+j))
		{
			tab_options = ce_select.options ;
			
/*
 *	Mise en forme 
 */
			for (o=0 ; o < tab_options.length ; o++ )
			{
				if (in_array(tab_options[o].value, tab[j]))
				{
					tab_options[o].style.color = '#000000' ;
					option_non_proposee.exec(tab_options[o].innerHTML) ;
					tab_options[o].innerHTML = RegExp.$2 ;
				}
				else 
				{
					tab_options[o].style.color = '#AAAAAA' ;
					option_non_proposee.exec(tab_options[o].innerHTML) ; 
					tab_options[o].innerHTML = '&nbsp;&nbsp;('+RegExp.$2+')' ;
				}
			}
				
/*
 *	Auto sélection d'une valeur possible
 */
			/*
 			if (tab[j].length > 0)
			{
				if (!in_array(tab_options[ce_select.selectedIndex].value, tab[j]))
				{
					tab_options[indice[j][tab[j][0]]].selected = true ;
					ce_select.selectedIndex = indice[j][tab[j][0]] ;
				}
			}
			*/
			depart = ce_select.selectedIndex ;
			for (o=depart+0 ; o < depart+tab_options.length ; o++ )
			{
				oo = o % tab_options.length ;
				exp_reg_tmp = exp_reg+'-'+tab_options[ce_select.selectedIndex].value+'(-|$)' ;
				
				if (tab_options[oo].selected && !regexp_in_array(exp_reg_tmp, tab_option_p1)) 
				{
				//	tab_options[o+1 % tab_options.length].selected = true ; 
					// Opera se retrouve avec ce_select.selectedIndex = -1 !
					indice_regexp = indice_regexp_in_array(exp_reg, tab_option_p1) 
					if (indice_regexp > -1)
					{
						tab_option_p1_split = tab_option_p1[indice_regexp].split('-');
						nouvel_indice = indice[j][tab_option_p1_split[j]] ;
						tab_options[oo].selected = false ; 
						ce_select.selectedIndex = nouvel_indice ;
						tab_options[nouvel_indice].selected = true ; 
					}
				}			
			}
			exp_reg += '-' + tab_options[ce_select.selectedIndex].value ;
//			texte += j+' '+exp_reg_tmp+' '+regexp_in_array(exp_reg_tmp, tab_option_p1)+'     ' ;
		}
	}
	//window.status=texte ;
}

function in_array(needle, haystack)
{
	var found = false ;
	for (var i=0 ; i<haystack.length && !found ; i++)
	{
		found = (needle == haystack[i]) ; 
	}
	return found ;
}
function regexp_in_array(exp_reg, tab_texte)
{
	var regexp = new RegExp(exp_reg) ;
	var result = false;
	for (i=0 ; i<tab_texte.length && !result ; i++)
	{
		result = regexp.test(tab_texte[i]) ; 
	}
	return result ;
}
function indice_regexp_in_array(exp_reg, tab_texte)
{
	var regexp = new RegExp(exp_reg) ;
	var result = false;
	var k = -1 ;
	for (i=0 ; i<tab_texte.length && !result ; i++)
	{
		result = regexp.test(tab_texte[i]) ; 
		if (result) k = i;
	}
	return k ;
}
function print_tab(T)
{
	t = '[' ;
		for (i=0 ; i<T.length ; i++ )
		{
	t += i+' => '+T[i]+', ' ;
		}
	t += ']' ;
	alert(t) ; 
}
function print_tab_tab(M)
{
	t = ''
	for (i=0 ; i<M.length ; i++ )
	{
	t += 'tab['+i+'] = [' ;
	t += print_tab(M[i]) ;
	t += ']\r\n' ;
	}
	alert(t) ; 
}


function affiche_zone_article_interne(id_ext)
{
	id_perso = id_article_personnalise(id_ext) ;
	ce_div = document.getElementById('zone-'+id_perso)
	
	//alert(id_ext);
	
	if (ce_div && ce_div.style.display == '')
	{
//		gestion_options_tertiaires(id_ext, id_perso)
	}
	else
	{
		nb_options = nombre_d_options(id_ext) ;
		
		gestion_options_tertiaires(id_ext, id_perso)
		
		if (nb_options > 0)
		{
			liste_tableaux = document.getElementsByTagName('div') ;
			for (i=0 ; i<liste_tableaux.length ; i++)
			{
				if (liste_tableaux[i].getAttribute('name') == id_ext) liste_tableaux[i].style.display = 'none' ;
			}
			
			if (ce_div) 
			{
	//			document.getElementById('quantite-'+id_perso).value = document.getElementById('qte-'+id_ext).value ;
				initialise_quantite(id_perso) ;
			
				document.getElementById('zone-'+id_perso).style.display = '' ;
				message_HTML('', 'alerte_formulaire-'+id_ext) ;
			}
			else
			{
				message_HTML('combinaison d\'options non disponible : '+id_perso, 'alerte_formulaire-'+id_ext) ;
			}
		}
		else
		{
			if (ce_div) ce_div.style.display = '' ;
			initialise_quantite(id_ext) ;
		}
	}
	return false; // car appelée sur le onClick d'un lien dont le href vaut #
}
function gestion_options_tertiaires(id_ext, id_perso)
{
	/* DEBUT Effacer les précédentes options 3 */
	if (Option_3_old = document.getElementById('Clone_Option_3-'+id_ext)) 
	{
		/* Clonage ne fonctionne pas complètement pour Opera : clone les valeurs initiales */
		Option_3_old.parentNode.removeChild(Option_3_old) ;
	}
	/* FIN Effacer les précédentes options 3 */
	
	
	/* DEBUT Copier les options 3 */
	if (document.getElementById('Option_3-'+id_perso)) 
	{
		/* Clonage ne fonctionne pas complètement pour Opera : clone seulement les valeurs initiales */
		Option_3 = document.getElementById('Option_3-'+id_perso).cloneNode(true);
		if (ie) Option_3.style.display = '' ; 
		else Option_3.setAttribute('style', 'display: ;') ;
		Option_3.setAttribute('id', 'Clone_Option_3-'+id_ext) ;
		if (ce_div_2 = document.getElementById('perso_'+id_ext))
		{
			ce_div_2.appendChild(Option_3);
		}
		else 
		{
			document.getElementById('form-'+id_ext).appendChild(Option_3);
		}
	}
	
//	if(document.getElementById('optionST-option_extra-'+id_perso+'-perso'))
	if(document.getElementById('optionST-option_extra-'+id_ext))
	{
		option_extra__html (document.getElementById('form-'+id_ext),1) ;
	}
	//option_extra__html (document.getElementById('form-'+id_ext)) ;
	/* FIN Copier les options 3 */
}
function valeur_select(id)
{
	return document.getElementById(id).options[document.getElementById(id).selectedIndex].value ;
}
function initialise_quantite(id_perso)
{
/*
	if (document.getElementById('quantite-'+id_perso).value == '')
	document.getElementById('quantite-'+id_perso).value = tab_quantite['p'+id_perso][0] ;
*/
	
	if (document.getElementById(prefixe_id_nbpl+id_perso))
	{
		tab_quantite['p'+id_perso][0] = ajuste_quantite_mini(tab_quantite['p'+id_perso][0], id_perso) ;
		tab_quantite_saisie['p'+id_perso] = ajuste_quantite_mini(tab_quantite_saisie['p'+id_perso], id_perso) ;
	tab_quantite_saisie['p'+id_perso] = Math.max( tab_quantite['p'+id_perso][0], tab_quantite_saisie['p'+id_perso]) ;
	document.getElementById('quantite-'+id_perso).value = tab_quantite_saisie['p'+id_perso] ;
		calcule_quantite(id_perso) ;
	}
	else
	{
		tab_quantite_saisie['p'+id_perso] = Math.max( tab_quantite['p'+id_perso][0], tab_quantite_saisie['p'+id_perso]) ;
		document.getElementById('quantite-'+id_perso).value = tab_quantite_saisie['p'+id_perso] ;
		calcule_prix(id_perso) ;
	}
}


/***********************************************************************************************/
var prefixe_id_nbpl = 'nbpl-'
// var prefixe_id_nbgr = 'option-'
// var suffixe_id_nbgr = '-3'
var ratio_grilles_plaque=0 ;
var temporisation_calcule_quantite = null ; /*!!*/
var TEMPORISATION=1000 ;
// var controle_tempo = 0
function temporise_calcule_quantite(id_perso) /*!!*/
{
	clearTimeout(temporisation_calcule_quantite) ;
//	window.status = controle_tempo++ ;
	if (document.getElementById(prefixe_id_nbpl+id_perso).value != '')
	{
		temporisation_calcule_quantite = setTimeout('calcule_quantite(\''+id_perso+'\', true)', TEMPORISATION) ;
	}
}

function calcule_quantite(id_perso, tempo) /*!!*/
{
	ratio_grilles_plaque =  nb_grilles(id_article_externe(id_perso)) ;
	
	// si nulle
		if (tempo) 
		{
			// récupère la dernière valeur saisie pour cet article.
			nb_pl = document.getElementById(prefixe_id_nbpl+id_perso).value ;
			if (nb_pl == 0 )
			{
				nb_pl = ajuste_quantite_colisage(tab_quantite['p'+id_perso][0]*tab_unite['p'+id_perso]/ratio_grilles_plaque, 1, 1) ;

			}
			affecte_quantite_saisie(id_perso, nb_pl) ;
		}
		else
		{
			nb_pl = tab_quantite_saisie['p'+id_perso] *tab_unite['p'+id_perso] /ratio_grilles_plaque ;
		}
	
//	nb_gr = tab_colisage['p'+id_perso] ; // *  ;
	nb_pl = ajuste_quantite_colisage(nb_pl, 1, 1) ;
	affecte_nbpl(id_perso, nb_pl) ;
	affecte_quantite(id_perso, nb_pl) ;
	calcule_prix(id_perso) ;
}
function affecte_quantite(id_perso, nb_pl)
{
	document.getElementById('quantite-'+id_perso).value = tab_quantite_saisie['p'+id_perso] ;
}
function affecte_quantite_saisie(id_perso, nb_pl)
{
	tab_quantite_saisie['p'+id_perso] = nb_pl * ratio_grilles_plaque / tab_unite['p'+id_perso] ;
}
function affecte_nbpl(id_perso, nb_pl)
{
	document.getElementById(prefixe_id_nbpl+id_perso).value = nb_pl ;
}

function ajuste_quantite_mini(quantite, id_perso)
{
/*
Effectue la transformation suivante :
la qte_mini de 1 (pour une série) est transformée en fonction du nombre de grille par plaques
nb_gr	qte_mini
	1	1.000
	2	1.000
	3	0.999
	4	1.000
	5	1.000
	6	0.996
	8	1.000
	9	0.999
	10	1.000
	12	0.996
	15	0.990
	16	0.992
	18	0.990
	20	1.000
	25	1.000
	30	0.990
	36	0.972
*/
	nb_gr = nb_grilles(id_article_externe(id_perso)) ;
	unite = tab_unite['p'+id_perso] ;
	return Math.floor(quantite *unite /nb_gr) *nb_gr  /unite;
}
function ajuste_quantite_colisage(quantite, colisage, unite, arrondi)
{
// ATTENTION : fonction truquée. le cas inférieur a été mis en place pour proposer 166 plaques de 6 grilles, afin de rester dans une série de 1000 grilles. Or le pb est que la quantité minimale est 1000 grilles, une quantite inférieure n'étant pas commandable.
// Test en repassant à l'arrondi superieur systématique. Les calculs seront alors opérationnnels. La quantité minimale est modifiée... (par exemple pour proposer 166 plaques de 6 grilles, soit 996 grilles, ce qui correspond à une série entière)

	quantite = quantite*unite ;
	if (!(quantite%colisage == 0))
	{
		switch(arrondi)
		{/*
		case 'inferieur' :
			quantite = Math.ceil(quantite*unite/colisage)*colisage ;
			break ;*/
		case 'superieur' :
		default :
			quantite = Math.ceil(quantite/colisage)*colisage ;
		}
	}
	return quantite/unite ;
}


function change_quantite (id_perso, qte)
{
	document.getElementById('quantite-'+id_perso).value = qte ;
	temporise_calcule_prix(id_perso) ;
}

function _20060420_change_quantite (id_perso, id_prix)
{
	document.getElementById('quantite-'+id_perso).value = tab_quantite['p'+id_perso][id_prix] ;
	temporise_calcule_prix(id_perso) ;
}

var temporisation_calcule_prix = null ;
function temporise_calcule_prix(id_perso)
{
	clearTimeout(temporisation_calcule_prix) ;
//	window.status = controle_tempo++ ;
	if (document.getElementById('quantite-'+id_perso) && document.getElementById('quantite-'+id_perso).value != '')
	{
		// tab_quantite_saisie['p'+id_perso] = document.getElementById('quantite-'+id_perso).value ;
		temporisation_calcule_prix = setTimeout('calcule_prix(\''+id_perso+'\', true)', TEMPORISATION) ;
	}
}

function calcule_prix(id_perso, tempo)
{

var bracelet = new Array('202','670','632','200','631','199','672','629','671','720','771','201','399','791');  /* T3WCOM */

	id_ext = id_article_externe(id_perso) ;	
	if (tempo)	qte = document.getElementById('quantite-'+id_perso).value ;
	else qte = tab_quantite_saisie['p'+id_perso] ;
	
	
	if (isNaN(qte))
	{
			alert('Combinaison d\'options non disponible') ;
	}
	else
	{
		if (qte < tab_quantite['p'+id_perso][0]) 
		{
			if (document.getElementById(prefixe_id_nbpl+id_perso))
			{
				nb_pl = ajuste_quantite_colisage(tab_quantite['p'+id_perso][0]*tab_unite['p'+id_perso]/ratio_grilles_plaque, 1, 1) ;
				affecte_nbpl(id_perso, nb_pl) ;
				message_HTML('Quantité minimale '+nb_pl , 'alerte-'+id_perso) ;
			}
			else
			{
				message_HTML('Quantité minimale '+tab_quantite['p'+id_perso][0], 'alerte-'+id_perso) ;
			
			// document.getElementById('prix-'+id_perso).innerHTML = '-' ;
			document.getElementById('quantite-'+id_perso).value = tab_quantite_saisie['p'+id_perso] = qte = tab_quantite['p'+id_perso][0] ;
			}
		}
		else 
		{
			message_HTML('', 'alerte-'+id_perso) ;
		}
		
		if (tab_colisage['p'+id_perso]>=1) 
		{
			qte = ajuste_quantite_colisage (qte, tab_colisage['p'+id_perso], tab_unite['p'+id_perso]) ;
			document.getElementById('quantite-'+id_perso).value = qte ;
		}
		else if (document.getElementById(prefixe_id_nbpl+id_perso))
		{
			ratio_grilles_plaque = nb_grilles(id_ext) ;
			nb_pl = ajuste_quantite_colisage(qte*tab_unite['p'+id_perso]/ratio_grilles_plaque, 1, 1) ;
			affecte_nbpl(id_perso, nb_pl) ;
			affecte_quantite(id_perso, nb_pl) ;
		}
	
		id_ext = id_article_externe(id_perso) ;
		document.getElementById('qte-'+id_ext).value = qte ;
		/*
		for (i=0 ; i<document.getElementsByName('quantite-'+id_ext).length ; i++)
			document.getElementsByName('quantite-'+id_ext)[i].value = qte ;
		*/
		
		nb_qte = tab_quantite['p'+id_perso].length ;
		nb_prix = tab_prix['p'+id_perso].length ;
		switch (tab_type['p'+id_perso])
		{
		case 'abaque' :
			if (nb_qte > 1) // si prix dégressif
			{
				i = 0 ;
				while ( i < nb_qte && qte >= tab_quantite['p'+id_perso][i] )
				{
					i++ ;
				}
				
				//alert(i+" - "+nb_qte);
				
				if (i==nb_qte && nb_qte==nb_prix-1)
				{
					qte_max = tab_quantite['p'+id_perso][nb_qte-1] ;
					prix_max = tab_prix['p'+id_perso][nb_qte-1] ;
					prix_supp = tab_prix['p'+id_perso][nb_prix-1] ;
					prix_total = qte_max * prix_max + (qte - qte_max) * prix_supp  +   qte * option_extra__supplement_prix(id_ext, id_perso) ;
					//alert("if");
				}
				else
				{
					//alert("else");
					/* 3wcom */
					if(in_array(id_ext,bracelet))
					{
						prix_total = (qte * (tab_prix['p'+id_perso][i-1]))+ option_extra__supplement_prix(id_ext, id_perso);
					}
					else
					{
						prix_total = qte * (tab_prix['p'+id_perso][i-1] + option_extra__supplement_prix(id_ext, id_perso));
					}
					
				}
			}
			else
			{
				//alert("ELSE");
				prix_total = qte * (tab_prix['p'+id_perso][0] + option_extra__supplement_prix(id_ext, id_perso)) ;
			}
			break;
		case 'affine' :
			prix_total = qte * (tab_prix_a['p'+id_perso] + option_extra__supplement_prix(id_ext, id_perso)) + tab_prix_b['p'+id_perso] ;
			
			break;
		}
		
		debut = (tab_unite['p'+id_perso]!=1)?('=<b>'+qte*tab_unite['p'+id_perso]+'</b>,'):'' ;
		
		document.getElementById('prix-'+id_perso).innerHTML = debut+' &nbsp; prix <b>'+format(prix_total)+'</b>' ; /* formater */
		if (prix_rv && prix_rv['p'+id_perso]) document.getElementById('prix_rv-'+id_perso).innerHTML = format(qte * prix_rv['p'+id_perso] * tab_unite['p'+id_perso] - prix_total) ; /* formater */
		if (document.getElementById('alerte-niveau-'+id_perso)) message_niveau(id_perso) ;
	}
}

// var option_extra__tab_article = new Array('215', '216', '396', '398',   '199', '200', '202', '629', '631', '632', '670', '672') ; 
var option_extra__tab_article_carton = new Array('215', '216', '396', '398', '723') ; 
var option_extra__tab_article_bracelet = new Array('199', '200', '202', '629', '631', '632', '670', '671', '672','720','771','201','399','791') ;  /* T3WCOM */
var option_extra__tab_article_loterie = new Array('680', '686', '690', '675', '688') ; 
/* id_externe  */
/*215	CARPRE	216	CARTRA	396	PLAPRE	398	PLATRA	199	BRACON	200	BRSTLA	202	EBT___	629	BRTEXT	631	BRLALA	632	BRSTMI	670	EBS__1	672	EBRH_1*/
function option_extra__supplement_prix(id_ext, id_perso)
{	
	option_extra__tab_id_supplement_article["199"]=4;
	option_extra__tab_id_supplement_article["200"]=4;
	option_extra__tab_id_supplement_article["202"]=5;
	option_extra__tab_id_supplement_article["629"]=4;
	option_extra__tab_id_supplement_article["631"]=4;
	option_extra__tab_id_supplement_article["632"]=4;
	option_extra__tab_id_supplement_article["670"]=8;
	option_extra__tab_id_supplement_article["671"]=4; 
	option_extra__tab_id_supplement_article["672"]=4;
	option_extra__tab_id_supplement_article["771"]=4;
	option_extra__tab_id_supplement_article["201"]=4;
	option_extra__tab_id_supplement_article["399"]=5;
	option_extra__tab_id_supplement_article["791"]=5;
	
	option_extra__tab_id_supplement_article["720"]=5; /* T3WCOM */

	supplement = 0 ;
	if(	in_array(id_ext, option_extra__tab_article_carton) || 
			in_array(id_ext, option_extra__tab_article_bracelet) || 
			in_array(id_ext, option_extra__tab_article_loterie))
	{
		id_tab_opt = id_ext ;
		//alert(option_extra__tab_id_supplement_article[id_tab_opt]);
		
		/*if (id_ext == id_perso) id_tab_opt = 'p'+id_perso ;
		else */
		
		switch(option_extra__tab_id_supplement_article[id_tab_opt])
		{
		case 1 :
			switch (id_ext)
			{
			case '723' :
				supplement = 14.25 ; // 13.5 ;
				break ;
				
			case '216' :
			default :
				ce_form = document.getElementById('form-'+id_ext) ;
				texte_option = ce_form['option_1[]'][0][ce_form['option_1[]'][0].selectedIndex].innerHTML ;
				if (texte_option.indexOf('euille') > -1)
				{
					supplement = 7.35 ; // 7 ;
				}
				else
				{
					if (texte_option.indexOf('igide') > -1 || texte_option.indexOf('pais') > -1)
					{
						supplement = 14.25 ; // 13.5 ;
					}
					else
					{
						supplement = 0 ;
					}
				}
				break ;
			}
			break ;
			
		case 2 :
			supplement = 18.5 ; // 17.85 ; // 17 ;
			break ;
			
		case 3 :
			supplement = 25.5 ; // 24.65 ; // 23.5 ;
			break ;
			
			
		// bracelet	
		case 4 :
			var retour = id_ext+"-1-1-0-1";
			if(id_perso==retour || id_perso==id_ext) {supplement = 38.27;}	 // 38.272 ;
			break ;
				
		case 5 :
			var retour = id_ext+"-1-1-0-1";
			if(id_perso==retour || id_perso==id_ext) {supplement = 43.06;} // 38.272 ;
			break ;
				
			
		// loterie
		case 6 :
			supplement = 9.90 ;
			break ;
			
		case 7 :
			supplement = 9.90 ;
			break ;
			
		case 8 :
			var retour = id_ext+"-1-0-0-1";
			if(id_perso==retour || id_perso==id_ext) {supplement = 43.06;} // 38.272 ;
			break ;	
		
		case 9 :
			supplement = 43.06 ;
			break ;
		
		case 0 :
		default :
			supplement = 0 ;
		}
	}
	if (cette_zone = document.getElementById('prix-unitaire-'+id_perso)) cette_zone.innerHTML = format(tab_prix['p'+id_perso][0]+supplement) 
//	else window.status = 'prix-unitaire-'+id_perso ;

	return supplement ;
/*	ce_form = document.getElementById('form-'+id_ext) ;
	// 216 CARTRA,  398 PLATRA,  215 CARPRE,  398 PLAPRE,  ...
	if (ce_form['option_2[texte]'] && ce_form['option_2[texte]'].parentNode && in_array(id_ext, option_extra__tab_article))
	{
		nb_logo = compte(ce_form, 'option_2[logo][]') ;
		switch(nb_logo)
		{
		case 1 :
			supplement = 17 ;
			break ;
		case 2 :
			supplement = 23.5 ;
			break ;
		default :
			texte_option = ce_form['option_1[]'][0][ce_form['option_1[]'][0].selectedIndex].innerHTML ;
			if (texte_option.indexOf('euille'))
			{
				supplement = 7 ;
			}
			else
			{
				if (texte_option.indexOf('igide') || texte_option.indexOf('pais'))
				{
					supplement = 13.5 ;
				}
				else
				{
					supplement = 0 ;
				}
			}
		}
	}
	else 
	{
		supplement = 0 ;
	}
*/
}

//desuet
function _compte(noeud_parent, nom_enfant)
{
	cpt = 0 ;
	tab_candidat = document.getElementsByName(nom_enfant)
	alert(tab_candidat.length)
	for (i=0 ; i<tab_candidat.length ; i++)
	{
		if (lignee(noeud_parent, tab_candidat[i])) cpt++ ;
	}
/*
	for (i=0, candidat=noeud_parent.firstChild  ; i<noeud_parent.childNodes.length ; i++, candidat=candidat.nextSibling)
	{
		if (candidat.name == nom_enfant) cpt++ ;
	}
*/
	return cpt;
}
//desuet
function _lignee(noeud_parent, noeud_enfant)
{
	intermediaire = noeud_enfant ;
	while(intermediaire = intermediaire.parentNode)
	{
		if (intermediaire == noeud_parent) return true ;
	}

	return false;
}

function nb_grilles(id_ext) 
{
/*
quand on peut choisir la quantité de grille acheté non pas par grille mais par plaques, le select contenant le nombre de grilles est *forcément le dernier* possédant l'attribut name = 'option_1[]'.
*/
	derniere_option = nombre_d_options(id_ext)-1 ;
	dernier_select = document.getElementById('option-'+id_ext+'-'+derniere_option) ;
	
	return dernier_select.options[dernier_select.selectedIndex].innerHTML ;
}

function est_id_perso (id_perso)
{
	return id_perso.indexOf('-')>0 ;
}
function id_article_externe (id_perso)
{
	id_perso = id_perso.toString() ; // sinon la chaine '456' est interprétée comme un chiffre dans indexOf
	if (est_id_perso(id_perso)) id_ext = id_perso.substring(0, id_perso.indexOf('-')) ;
	else id_ext = id_perso ;
	
	return id_ext ;
}
function id_article_personnalise(id_ext)
{
	nb_options_MAX = 10 ;
	i=0 ;
	id_perso = id_ext.toString() ;
	while (i<nb_options_MAX && document.getElementById('option-'+id_ext+'-'+i))
	{
		if (document.getElementById('option-'+id_ext+'-'+i).type == 'select-one')
		{
			id_perso += '-'+valeur_select('option-'+id_ext+'-'+i) ;
		}
		else
		{
			id_perso += '-0' ;
		}
		i++ ;
	}
	return id_perso.toString() ;
}
function nombre_d_options(id_ext)
{
	return id_article_personnalise(id_ext).split('-').length - 1 ; // 123-a-b-c ->  4-1 ->  3
}
function envoyer(id_perso) 
{
	id_ext = id_article_externe(id_perso)
	perso_vue = (perso_vue = document.getElementById('perso_vue_'+id_ext))?(perso_vue.value == 'true'):true ;
	if (document.getElementById('qte-'+id_ext))
	{
	
		perso_ok = true;
		custom_elt = document.getElementById('custom_'+id_perso)
		if ( typeof(custom_elt) != "undefined" ) 
		if(custom_elt && custom_elt.value == '1')
		{
			perso_ok = false;
		}
		
		qte = document.getElementById('qte-'+id_ext).value ;
		erreur_niveaux = (document.getElementById('alerte-niveau-'+id_perso))?(delta_niveaux(id_perso)!=0):false ;
		quantite_ok = !(qte < tab_quantite['p'+id_perso][0] || isNaN(qte) || erreur_niveaux) ;
		/* Vérification que les options demandées sont possible - 
		inutile pour certaine car on ne peut alors pas expédier le formulaire */
		form = document.getElementById('form-'+id_ext) ;
		option_visuel_ok = (form.elements['option_2[version_ticket]'] && form.elements['option_2[ticket]'])?verif_visuel(form):true ;
		option_couleur_souple_ok = (form.id == 'form-216')?verif_couleur_souple(form):true ;
		
		form_valide = quantite_ok && option_visuel_ok && option_couleur_souple_ok && perso_ok ;
		if ( !form_valide )
		{
			alerte = 'Corrigez vos options\r\n' ;
			if (!quantite_ok) alerte += 'Vous ne pouvez pas commander cette quantité\r\n' ;
			if (!perso_ok) alerte += 'Vous n\'avez pas personnalisé votre produit\r\n' ;
			if (erreur_niveaux) 
			{
				alerte += 'Elle ne correspond pas à la répartition par niveaux,\n' ;
				alerte += 'modifiez la personnalisation.\r\n' ;
			}
			if (!option_visuel_ok) alerte += message_erreur_visuel+"\r\n" ;
			if (!option_couleur_souple_ok) alerte += message_erreur_couleur_souple+"\r\n" ;
			alert(alerte);
			affiche_personnalisation(id_ext, 'ouvre')
		}
		else
		{	
			if (!perso_vue)
			{
				if(confirm('Voulez vous personnaliser \r\ncet article ?'))
					affiche_personnalisation(id_ext, 'ouvre')
				else
					document.getElementById('form-'+id_ext).submit() ;
			}
			else
			{
				document.getElementById('form-'+id_ext).submit() ;
			}
		}
	}
	else alert('Erreur fonction envoyer :'+id_perso+' - '+id_ext) ;
}

function message_HTML(texte, id)
{
	if (texte.length > 0) document.getElementById(id).innerHTML = texte+'<br />' ;
	else document.getElementById(id).innerHTML = texte ;
}

var regexp_decimales = /^(.*\d+,)(\d*)(.*)$/ ; 
function format(valeur)
{
	valeur = Number(valeur).toFixed(2).replace('.', ',')+'&nbsp;<span class="euro">&euro;</span>&nbsp;' ;
	resultat = regexp_decimales.exec(valeur) ;
	if (resultat && resultat.length)
	{
		valeur = RegExp.$1+'<span class="decimales">'+RegExp.$2+'</span>'+RegExp.$3 ;
	}	
	return valeur ;
}

function imprimer() 
{
/*	style_impression(true) ; */
	window.print() ; 
	return false ;
}

function _espion(repere)
{
// alert(repere+' : '+tab_quantite_saisie['p'+id_perso])
}