
function formatNumber(n,r,ds,ts){
	nbr = String(n).split('.');
	// Gestion des decimal
	if(nbr[1] == undefined || nbr[1].length == 0) nbr[1] = '00';
	else if(nbr[1].length == 1) nbr[1] = nbr[1]+'0';
	else if(nbr[1].length > 2)  nbr[1] = Number(nbr[1].substr(0,2)+'.'+nbr[1].substr(2)).toFixed(0);
	// Gestion des millier
	//nbr[0] = nbr[0].replace(/([0-9])([0-9]{3})$/,'$1'+ts+'$2');
	return nbr[0]+ds+nbr[1];
}

function calcul(id){
	if(id > 0)
	{
		QTE  = parseInt(document.getElementById('qte_'+id).value);
		if(isNaN(QTE)) QTE = 0;
		
		if(QTE == 0)
			document.getElementById('qte_'+id).form.submit();
	
		PRIX = document.getElementById('prix_'+id).innerHTML;
		PRIX = parseFloat(PRIX.replace(',','.'));
		
		document.getElementById('total_'+id).innerHTML = formatNumber((QTE*PRIX),2,',',' ') + '&nbsp;&euro;';
	}
	/*
	// Tableau des Frais de port
fdp = [
0,
[5.1,6.15,6.95,7.55,8.8,8.8,9.6,9.6,11.45,11.45,11.45,13.3,18.05,18.05,18.05,],
[7.6,11.4,15.7,20,24.3,28.6,32.9,37.2,41.5,45.8,50.1,71.6,93.1,114.6,136.1,],
[8.7,13.9,25.3,36.7,48.1,59.5,70.9,82.3,93.7,105.1,116.5,176,233,292.5,352,],
[14.8,14.8,16.3,19.3,23,26.7,30.4,34.1,37.8,41.5,45.2,51.2,57.2,63.2,69.2,],
[17.8,17.8,19.6,23.8,28,32.2,36.4,40.6,44.8,49,53.2,63.7,74.2,84.7,95.2,],
[20.5,20.5,27,36,45.5,53.9,62.3,70.7,79.1,87.5,95.9,120.9,145.9,175.9,205.9,],
[23.7,23.7,35.7,47.7,59.7,71.7,83.7,95.7,107.7,119.7,131.7,166.7,201.7,236.7,271.7,],
];
	*/
	TT        = document.getElementById('affPanier').getElementsByTagName('td');
	NBR       = TT.length;
	total     = 0
	poidTotal = 0;
	panier    = '';
	for(i=0; i < NBR ; i++){
		if(TT[i].id && TT[i].id.indexOf('total_') == 0 && TT[i].id != 'total_ok'){
			tmp = TT[i].innerHTML;
			tmp = parseFloat(tmp.replace(',','.'));
			total += tmp;
		}
		if(TT[i].className && TT[i].className == 'qte'){
			IN = TT[i].getElementsByTagName('input');
			qte = IN[0].value;
			id  = IN[0].id.substr(4);
			panier += id+':'+qte+'|';
			poidTotal += (IN[1].value * qte);
		}
	}
	/*
	poidTotal += (poidTotal * 10) / 100;
	
	if      (poidTotal < 1000)  pt = 0;
	else if (poidTotal < 2000)  pt = 1;
	else if (poidTotal < 3000)  pt = 2;
	else if (poidTotal < 4000)  pt = 3;
	else if (poidTotal < 5000)  pt = 4;
	else if (poidTotal < 6000)  pt = 5;
	else if (poidTotal < 7000)  pt = 6;
	else if (poidTotal < 8000)  pt = 7;
	else if (poidTotal < 9000)  pt = 8;
	else if (poidTotal < 10000) pt = 9;
	else if (poidTotal < 15000) pt = 10;
	else if (poidTotal < 20000) pt = 11;
	else if (poidTotal < 25000) pt = 12;
	else if (poidTotal < 30000) pt = 13;
	else                        pt = 14;
	*/
	pays = 1;
	/*
	base = fdp[pays][pt] * Math.ceil(poidTotal / 30000); 
	
	// Participation
	if     (total < 90)  chg = 0;
	else if(total < 180) chg = (total * 5)  / 100;
	else                 chg = (total * 10) / 100;
	
	base -= chg;
		
	if (base <= 0) base = 0;
	
	document.getElementById('port_ok').innerHTML  = formatNumber(base,2,',',' ') + '&nbsp;&euro;';
	*/
	document.getElementById('total_ok').innerHTML = formatNumber(total,2,',',' ') + '&nbsp;&euro;';
	document.cookie = 'panier='+panier;
	document.cookie = 'pays='+pays;
}

function suppElem(id){
	document.getElementById('qte_'+id).value = '0';
	calcul(id)
}

function iniCalc(){
	if(document.getElementById('affPanier'))
	{
		INPUT = document.getElementById('affPanier').getElementsByTagName('input');
		NBR = INPUT.length;
	
		for(i=0; i < NBR; i++){
			if(INPUT[i].id.indexOf('qte_') == 0){
				INPUT[i].onkeyup = function(){
					ID = this.id.substr(4);
					calcul(ID);
				}
			} else if (INPUT[i].type == "image"){
				INPUT[i].onclick = function(){
					ID = this.name.substring(5,this.name.lastIndexOf(']'));
					suppElem(ID)
				}
			}
		}
		/*
		SELECT = document.getElementById('zone_fdp');
		SELECT.onchange = function(){
			this.form.submit();
		}
		*/
	}
}