// JavaScript Document 
// --------------------------------------------
// calcul de base
// ---------------------------------------------


function calcul() 
{
var tt=0;
var tta=0;
var deduc=0;

var CGS=0;
var AssSoc=0;
var CRDS=0;

var montant = document.getElementById("montant");

/* assurances sociales*/
AssSoc=new Number(arrondir(montant.value*.0085,0));
document.getElementById("assursoc").innerHTML =AssSoc + " €";
tt=tt+AssSoc;
deduc=deduc+AssSoc;

/* CGS */
CGS=new Number(arrondir(montant.value*.075*.97,0));
document.getElementById("cgs").innerHTML =CGS+" €";
tt=tt+ CGS;
deduc=deduc+new Number(arrondir(montant.value*.051*.97,0));


/* CRDS */
CRDS=new Number(arrondir(montant.value*.005*.97,0));
document.getElementById("crds").innerHTML =CRDS+" €";
tt=tt+CRDS;
document.getElementById("totalart").innerHTML =tt+" €";

/* diffuseur */
TTdiffuseur=new Number(arrondir(montant.value*.01,0));
document.getElementById("difus").innerHTML =TTdiffuseur +" €";
tt=tt+TTdiffuseur;

/* total */
document.getElementById("total").innerHTML =tt +" €";
document.getElementById("deduc").innerHTML ="(dont "+deduc+" € "+" sont fiscalement déductibles)";

}

// --------------------------------------------
// fonction arrondir un nombre
// --------------------------------------------
function arrondir(pnumber,decimals) 
{ 
var strNumber = new String(pnumber); 
strNumber=strNumber.replace(",",".");
var arrParts = strNumber.split('.'); 
var intWholePart = parseInt(arrParts[0],10); 
var strResult = ''; 
if (isNaN(intWholePart)) 
intWholePart = '0'; 
if(arrParts.length > 1) 
{ 
var decDecimalPart = new String(arrParts[1]); 
var i = 0; 
var intZeroCount = 0; 
while ( i < String(arrParts[1]).length ) 
{ 
if( parseInt(String(arrParts[1]).charAt(i),10) == 0 ) 
{ 
intZeroCount += 1; 
i += 1; 
} 
else 
break; 
} 
decDecimalPart = parseInt(decDecimalPart,10)/Math.pow(10,parseInt(decDecimalPart.length-decimals-1)); 
Math.round(decDecimalPart); 
decDecimalPart = parseInt(decDecimalPart)/10; 
decDecimalPart = Math.round(decDecimalPart); 

//If the number was rounded up from 9 to 10, and it was for 1 'decimal' 
//then we need to add 1 to the 'intWholePart' and set the decDecimalPart to 0. 

if(decDecimalPart==Math.pow(10, parseInt(decimals))) 
{ 
intWholePart+=1; 
decDecimalPart="0"; 
} 
var stringOfZeros = new String(''); 
i=0; 
if( decDecimalPart > 0 ) 
{ 
while( i < intZeroCount) 
{ 
stringOfZeros += '0'; 
i += 1; 
} 
} 
decDecimalPart = String(intWholePart) + "." + stringOfZeros + String(decDecimalPart); 
var dot = decDecimalPart.indexOf('.'); 
if(dot == -1) 
{ 
decDecimalPart += '.'; 
dot = decDecimalPart.indexOf('.'); 
} 
var l=parseInt(dot)+parseInt(decimals); 
while(decDecimalPart.length <= l) 
{ 
decDecimalPart += '0'; 
} 
strResult = decDecimalPart; 
} 
else 
{ 
var dot; 
var decDecimalPart = new String(intWholePart); 

decDecimalPart += '.'; 
dot = decDecimalPart.indexOf('.'); 
var l=parseInt(dot)+parseInt(decimals); 
while(decDecimalPart.length <= l) 
{ 
decDecimalPart += '0'; 
} 
strResult = decDecimalPart; 
} 
return strResult; 
} 

