function formater(nombre, taille) {
	 if (isNaN(nombre)) return "";
	 var chaine = nombre.toString();
	 while (chaine.length < taille) chaine = "0" + chaine;
		  return chaine;
		}

function arrondir(nombre, decimales) {
	if (isNaN(nombre)) return "";
	if (decimales == 0) {
		var n = Math.round(nombre);
		return n.toString();
	 }
	else if (decimales > 0) {
			if (document.all) var undefined; // Netscape 4 ne connaît pas la constante "undefined"
			var p10 = Math.pow(10, decimales);
			var n = Math.round(nombre * p10);
			n = n.toString();
			var point = n.length - decimales;
			if (point >= 0) n = n.substring(0, point) + "." + n.substring(point);
			else n = "0." + formater(n,2);
		}
	else {
		var p10 = Math.pow(10, -decimales);
		var n = Math.round(nombre / p10) * p10;
		n = n.toString();
		}
	if (nombre.signe_decimal == undefined) return n;
	else return n.replace(".", nombre.signe_decimal);
}
var prix = 0;
var remise = 0;
var prixtotal = 0;

var prixPromo = 0;
var reduction = '';
var montantReduction = 0;
// var reducPromo = 0;
var typeReduction ='';
var montant = 0;
var lecode = '';
var prixCours = 0;

						
function calcul(){
	var bg1 = '#fafafa';
	var bg2 = '#B4B5BA';
	var c1 = '#364970';
	var c2 = '#B4B5BA';
	// prix = tabPrix[0][1];		
	prix = prixCours;
	remise = tabPrix[0][2];
	var result = prix-remise;
						
	//for (i=1; i<tabPrix.length; i++){

	if(tabPrix.length>1){
	var i = 1;
		if(document.getElementById('cb' + i).checked){
			prix = parseFloat(prix) + parseFloat(tabPrix[i][1]);
			prix =  arrondir(prix,2); 
			
			remise = remise + tabPrix[i][2];
			result = prix-remise;	
			document.getElementById ('tr' +i).style.color = c1;
			//document.getElementById ('tr' +i).style.textDecoration = 'none';
			}
		else{
			document.getElementById ('tr' +i).style.color = c2;
			//document.getElementById ('tr' +i).style.textDecoration = 'line-through';
			}
								
		if(remise<1){
			document.getElementById ('TRremise').style.color = c2;
			//document.getElementById ('TRremise').style.textDecoration = 'line-through';
			document.getElementById ('livraison').style.display = 'none';
				if(user==true){
					inscription(1);
				}
			}
		else {
			document.getElementById ('TRremise').style.color = c1;
			document.getElementById ('TRremise').style.textDecoration = 'none';
			document.getElementById ('livraison').style.display = 'block';
			affForm(0);
			}
		document.getElementById ('prix').innerHTML = arrondir(prix,2);
		document.getElementById ('remise').innerHTML = '-' + arrondir(remise,2);
		document.getElementById ('total').innerHTML = arrondir(result,2);
					
		//}
	}
		
//		document.getElementById('fp1').value = arrondir(result,2);
//		document.getElementById('fp2').innerHTML = arrondir(result,2) + '€';
	
		if(typeReduction==1 || typeReduction ==0){
			majPrixPromo(typeReduction,montant);
		}	
		if(montantReduction>0){
			document.getElementById('TDprixPromo1').innerHTML = 'Code de r&eacute;duction';
			document.getElementById('TDprixPromo2').innerHTML = reduction;
			document.getElementById('TDprixTotalPromo1').innerHTML = 'Prix r&eacute;duit';
			document.getElementById('TDprixTotalPromo2').innerHTML = arrondir(prixPromo,2);	 
		}
		else {
			document.getElementById('TDprixPromo1').innerHTML = '';
			document.getElementById('TDprixPromo2').innerHTML = '';
			document.getElementById('TDprixTotalPromo1').innerHTML = '';
			document.getElementById('TDprixTotalPromo2').innerHTML = '';	 		
		}
		document.getElementById('fp1').value = arrondir((result-montantReduction),2);
		document.getElementById('fp2').innerHTML = arrondir((result-montantReduction),2) + '€';		
	}

	
function getInfosPrix(){
	var div = document.getElementById('showInfosPrix');
		if(div.innerHTML.length< 10){
		var html = document.getElementById('infoPrix').innerHTML;
		div.innerHTML = html;
	}
	showDiv('showInfosPrix');
}
	
function writeTabPrix(){
	prixCours = arrondir(tabPrix[0][1],2);
	document.write ('<form id="formPrix">');
	document.write ('<div id="showInfosPrix"></div>');
	document.write ('<table cellspacing="0" id="tableFormPrix">');
	document.write ('<tr>');
	document.write ('<td>' + tabPrix[0][0] + '<br /><a href="javascript://" onclick="getInfosPrix();return false;" title="Informations" style="font-size:10px;">en savoir plus sur la durée</a></td><td class="prix" id="prixPremier">' + prixCours);
	document.write ('</tr>');	
	// offres durées supplémentaires
	try{
	if(tabPrix[0][5].length >10){
		var offSupp = tabPrix[0][5].split('/');
		var d1 = offSupp[0];
		var p1 = offSupp[1];
		var d2 = offSupp[2];
		var p2 = offSupp[3];
		var d3 = offSupp[4];
		var p3 = offSupp[5];
		var checked = 'checked="checked"';
		offSupp  = null;
	document.write ('<tr>');
	document.write ('<td>');
		document.write ('<input name="rbDuree" type="radio" id="radio1" value="' + d1 + '" ' + checked + ' onclick="changePrixDuree(1,' + d1 + ',' + p1 + ');" /> <span id="radioMois1" style="color:#ff9e23;"><strong>' + d1 + ' mois</strong></span> ' + arrondir(p1,2) + '&euro; ' );
		//if(d2>0){document.write ('<input name="rbDuree" id="radio2" type="radio" value="' + p2 + '" onclick="changePrixDuree(2,' + d2 + ','+ p2 + ');" /> <span id="radioMois2"><strong>' + d2 + ' mois</strong></span> ' + arrondir(p2,2) + '&euro; ');}
		//if(d3>0){document.write ('<input name="rbDuree" id="radio3" type="radio" value="' + p3 + '" onclick="changePrixDuree(3,' + d3 + ','+ p3 + ');" /> <span id="radioMois3"><strong>' + d3 + ' mois</strong></span> ' + arrondir(p3,2) + '&euro; ');}
		if(d2>0){			
			document.write ('<br /><strong>Tarifs pr&eacute;f&eacute;rentiels :</strong><br /><input name="rbDuree" id="radio2" type="radio" value="' + p2 + '" onclick="changePrixDuree(2,' + d2 + ','+ p2 + ');" /> <span id="radioMois2"><strong>' + d2 + ' mois</strong></span> ' + arrondir(p2,2) + '&euro; au lieu de <strike>' + arrondir((p1*d2)/d1,2) + '</strike> &euro;' + pourcentReduc(p1,p2,d1,d2));
			}
		if(d3>0){document.write ('<br /><input name="rbDuree" id="radio3" type="radio" value="' + p3 + '" onclick="changePrixDuree(3,' + d3 + ','+ p3 + ');" /> <span id="radioMois3"><strong>' + d3 + ' mois</strong></span> ' + arrondir(p3,2) + '&euro; au lieu de <strike>' + arrondir((p1*d3)/d1,2) + '</strike> &euro;' + pourcentReduc(p1,p3,d1,d3));}
	document.write ('</td>');
	document.write ('</tr>');
	d1=null;p1=null;d2=null;p2=null;d3=null;p3=null;checked=null;
	}
	}catch(e){e=null;}

	if(tabPrix.length>1){	
		//for (i=1; i<=tabPrix.length-1;i++){
			var i = 1;
			prix = prix + tabPrix[i][1];	
			remise = remise + tabPrix[i][2];
			document.write ('<tr id="tr' + i + '">');
			//document.write ('<td><input type="checkbox" name="cb" id="cb' + i + '" value="' + i + '" checked="checked" onclick="calcul();" /> ' + tabPrix[i][0] );
			document.write ('<td><img src="/img/casque.gif" alt="" /> <input type="checkbox" name="cb" id="cb' + i + '" value="' + i + '" onclick="calcul();" /> ' + tabPrix[i][0] );
			// infos article boutique
				if(tabPrix[i][3].charAt(0) == 'a')	{	
					var idArt = tabPrix[i][3].replace('a','') ;
					document.write('<small> (port inclus)</small> <sup style=""font-weight:16px"">*</sup>');
					document.write (' <a href="javascript://" title="Plus d\'informations" onclick="infosArticle(' + idArt + ',\'divInfosArticle\'); return false;"><img src="/img/i_infos.gif" alt="Plus d\informations" style="vertical-align:middle;" /></a><div id="divInfosArticle" style="display:none;position:absolute; left:50px; z-index:20; text-decoration:none;"></div>');
				}
			document.write ('</td>');
			document.write ('<td class="prix">' + arrondir(tabPrix[i][1],2) + ' </td>');
			document.write ('</tr>');						
	//		}
				
			document.write ('<tr>');
			document.write ('<td>&nbsp;</td><td id="prix" class="prix">' + arrondir(prix,2) + ' </td>');
			document.write ('</tr>');
			document.write ('<tr id="TRremise">');
			document.write ('<td><sup style=""font-weight:16px"">*</sup> Remise achat groupé (-' + remise + ' &euro;)</td><td id="remise" class="prix">-' + arrondir(remise,2) + ' </td>');
			document.write ('</tr>');
		
			document.write ('<tr>');
			document.write ('<td>Votre prix</td><td id="total" class="prix">' + arrondir((prix-remise),2) + '&euro;</td>');
			document.write ('</tr>');						
		}
		
			document.write ('<tr>');
			document.write ('<td id="TDprixPromo1"></td><td  id="TDprixPromo2" class="prix"></td>');
			document.write ('</tr>');	
			document.write ('<tr>');
			document.write ('<td id="TDprixTotalPromo1"></td><td  id="TDprixTotalPromo2" class="prix"></td>');
			document.write ('</tr>');			
			document.write ('</table></form>');			
			resetBox();
}

function pourcentReduc(p1,p2,d1,d2){
	try{
	//var p = p2 / ((d2/d1) *p1);
	var p = p2/(p1*(d2/d1));
	p = (1-p)*100;
	p = Math.round(p);

	p = ' soit <u>' + p + '% de réduction</u>';
	}catch(e){e=null;p='';}
	return(p);
}

function resetBox(){
	try{
		var box = document.getElementsByName('rbDuree');
		box[0].checked = true;
		box = document.getElementsByName('cb');
		for (var i = 0;i< box.length;i++){
			box[i].checked = false;
		}
	}catch(e){e=null;}
}
function changePrixDuree (r,d,p){
	document.getElementById('prixPremier').innerHTML = arrondir(p,2);
	for(var i=1; i<=3;i++){
		document.getElementById('radioMois' + i).style.color = '';
	}
	var rbId = 'radioMois' + r ;
	document.getElementById(rbId).style.color = '#ff9e23';
	rbId = null;
	prixCours = p;
	dureeChoisie = d;
	calcul();	
}

function infosArticle(id,div){

url = '/incV2/infoArticle.asp?id=' + id ;
d = document.getElementById(div);
if( d.style.display == 'none'){
	httpRequest(url,div);
	 d.style.display = 'block';
}
else {  d.style.display = 'none';}

}


function writeTabPrixMois(m1,m2,m3){
	prixCours = arrondir(tabPrix[0][1],2);
	document.write ('<form id="formPrix">');
	document.write ('<div id="showInfosPrix"></div>');
	document.write ('<table cellspacing="0" id="tableFormPrix">');
	document.write ('<tr>');
	document.write ('<td>' + tabPrix[0][0] + '<br /><a href="javascript://" onclick="getInfosPrix();return false;" title="Informations" style="font-size:10px;">en savoir plus sur la durée</a></td><td class="prix" id="prixPremier">' + prixCours);
	document.write ('</tr>');	
	// offres durées supplémentaires
	try{
	if(tabPrix[0][5].length >10){
		var offSupp = tabPrix[0][5].split('/');
		var d1 = offSupp[0];
		var p1 = offSupp[1];
		var d2 = offSupp[2];
		var p2 = offSupp[3];
		var d3 = offSupp[4];
		var p3 = offSupp[5];
		var checked = 'checked="checked"';
		offSupp  = null;
	document.write ('<tr>');
	document.write ('<td>');
		document.write ('<input name="rbDuree" type="radio" id="radio1" value="' + d1 + '" ' + checked + ' onclick="changePrixDuree(1,' + d1 + ',' + p1 + ');" /> <span id="radioMois1" style="color:#ff9e23;"><strong>' + d1 + ' mois</strong></span> <strong>' + arrondir(p1/m1,2) + '&euro; par mois</strong>' );
		document.write( ' <small>en un r&egrave;glement ' + arrondir(p1,2) + '&euro;</small> ');
		//if(d2>0){document.write ('<input name="rbDuree" id="radio2" type="radio" value="' + p2 + '" onclick="changePrixDuree(2,' + d2 + ','+ p2 + ');" /> <span id="radioMois2"><strong>' + d2 + ' mois</strong></span> ' + arrondir(p2,2) + '&euro; ');}
		//if(d3>0){document.write ('<input name="rbDuree" id="radio3" type="radio" value="' + p3 + '" onclick="changePrixDuree(3,' + d3 + ','+ p3 + ');" /> <span id="radioMois3"><strong>' + d3 + ' mois</strong></span> ' + arrondir(p3,2) + '&euro; ');}
		if(d2>0){document.write ('<br /><strong>Tarifs pr&eacute;f&eacute;rentiels :</strong><br /><input name="rbDuree" id="radio2" type="radio" value="' + p2 + '" onclick="changePrixDuree(2,' + d2 + ','+ p2 + ');" /> <span id="radioMois2"><strong>' + d2 + ' mois</strong></span> <strong>' + arrondir(p2/m2,2) + '&euro; par mois</strong> <small>en un r&egrave;glement ' + arrondir(p2,2) + '&euro; au lieu de <strike>' + arrondir((p1*d2)/d1,2) + '</strike> &euro;</small>');}
		if(d3>0){document.write ('<br /><input name="rbDuree" id="radio3" type="radio" value="' + p3 + '" onclick="changePrixDuree(3,' + d3 + ','+ p3 + ');" /> <span id="radioMois3"><strong>' + d3 + ' mois</strong></span> <strong>' + arrondir(p3/m3,2) + '&euro; par mois</strong> <small>en un r&egrave;glement ' + arrondir(p3,2) + '&euro; au lieu de <strike>' + arrondir((p1*d3)/d1,2) + '</strike> &euro;</small>');}
		else {document.write('<span id="radioMois3" style="display:none;" />');}
	document.write ('</td>');
	document.write ('</tr>');
	d1=null;p1=null;d2=null;p2=null;d3=null;p3=null;checked=null;
	}
	}catch(e){e=null;}

	if(tabPrix.length>1){	
		//for (i=1; i<=tabPrix.length-1;i++){
			var i = 1;
			prix = prix + tabPrix[i][1];	
			remise = remise + tabPrix[i][2];
			document.write ('<tr id="tr' + i + '">');
			//document.write ('<td><input type="checkbox" name="cb" id="cb' + i + '" value="' + i + '" checked="checked" onclick="calcul();" /> ' + tabPrix[i][0] );
			document.write ('<td><img src="/img/casque.gif" alt="" /> <input type="checkbox" name="cb" id="cb' + i + '" value="' + i + '" onclick="calcul();" /> ' + tabPrix[i][0] );
			// infos article boutique
				if(tabPrix[i][3].charAt(0) == 'a')	{	
					var idArt = tabPrix[i][3].replace('a','') ;
					document.write('<small> (port inclus)</small> <sup style=""font-weight:16px"">*</sup>');
					document.write (' <a href="javascript://" title="Plus d\'informations" onclick="infosArticle(' + idArt + ',\'divInfosArticle\'); return false;"><img src="/img/i_infos.gif" alt="Plus d\informations" style="vertical-align:middle;" /></a><div id="divInfosArticle" style="display:none;position:absolute; left:50px; z-index:20; text-decoration:none;"></div>');
				}
			document.write ('</td>');
			document.write ('<td class="prix">' + arrondir(tabPrix[i][1],2) + ' </td>');
			document.write ('</tr>');						
	//		}
				
			document.write ('<tr>');
			document.write ('<td>&nbsp;</td><td id="prix" class="prix">' + arrondir(prix,2) + ' </td>');
			document.write ('</tr>');
			document.write ('<tr id="TRremise">');
			document.write ('<td><sup style=""font-weight:16px"">*</sup> Remise achat groupé (-' + remise + ' &euro;)</td><td id="remise" class="prix">-' + arrondir(remise,2) + ' </td>');
			document.write ('</tr>');
		
			document.write ('<tr>');
			document.write ('<td>Votre prix</td><td id="total" class="prix">' + arrondir((prix-remise),2) + '&euro;</td>');
			document.write ('</tr>');						
		}
		
			document.write ('<tr>');
			document.write ('<td id="TDprixPromo1"></td><td  id="TDprixPromo2" class="prix"></td>');
			document.write ('</tr>');	
			document.write ('<tr>');
			document.write ('<td id="TDprixTotalPromo1"></td><td  id="TDprixTotalPromo2" class="prix"></td>');
			document.write ('</tr>');			
			document.write ('</table></form>');			
			resetBox();
}
