/*
 * fonctions nécessaires au formulaire implémenté dans le .php du même nom
 */
var tauxfp1=(5/100);
var tauxfp2=(3.3/100);
var tauxfp3=(1.65/100);
var tauxfp4=(0.825/100);
var tva=(19.6/100);
var ec=183;
var fdd=91;
var tauxdde=(3.60/100);
var tauxtc=(1.20/100);
var tauxfa=(0.09/100);

var NumChars = "0123456789";
var DecChars = "0123456789.,";

function IsNumeric(Data) {
var It = true;
var c;
	for (var x = 0; x < Data.length; x++)
		if (It) {
			c = Data.charAt(x);
			It = ( NumChars.indexOf(c) != -1);
		}
		return It;
}

function IsDecimal(Data) {
var It = true;
var c;
	for (var x = 0; x < Data.length; x++)
		if (It) {
			c = Data.charAt(x);
			It = ( DecChars.indexOf(c) != -1);
		}
		return It;
}

function checkNumber(str)
{
		var str2="";
		for (var i = 0; i < str.length; i++) 
	{
				var ch = str.substring(i, i + 1);
				if (ch==",") 
		{
								ch=".";
		}
				str2=str2+ch;
		}
		return str2;
}

//Vérification format du montant de l'achat
function checkMontant(chaine)
{
	if(chaine!="")
	{
			if (IsDecimal(chaine) == false)
			{
		alert("Saisissez un montant numérique");
		document.form.mt.focus();
			
		var chaine2="";

		for (var i = 0; i < chaine.length; i++) 
		{
						var car = chaine.substring(i, i + 1);
			if (IsDecimal(car) == false)
			{
				car="";
			}
					chaine2=chaine2+car;
				}
			document.form.mt.value=chaine2;
			return chaine2;
			}
	}
}

function checkComma(chaine)
{
	var car = chaine.charAt(chaine.length-1);
	if ( car == ',' || car == '.' ) 
	{
		for (var i = 0; i < chaine.length-1; i++) 
		{
						var car2 = chaine.substring(i, i + 1);
			if (car2 == ',' || car2 == '.' )
			{
					document.form.mt.value=chaine.substring(0, chaine.length-1);
					return;
			}
		}
	}
}

function verifieMontant (achat)
{
	if(achat!="")
	{
		if (IsDecimal(achat) == false)
		{
			alert("Saisissez un montant numérique");
			document.form.mt.focus();
			return "";
		}
	}
}


function stop(){
	alert(
		"Calcul impossible sans valeur."
		+"\nVeuillez saisir une valeur numérique s'il vous plaît"
	);
}


function calcul(){
	achat=document.form.mt.value;
	fphonoht=document.form.fphonoht.value;
	fphonottc=document.form.fphonottc.value;
	fpb1=document.form.fpb1.value;
	fpm1=document.form.fpm1.value;
	fpb2=document.form.fpb2.value;
	fpm2=document.form.fpm2.value;
	fpb3=document.form.fpb3.value;
	fpm3=document.form.fpm3.value;
	fpb4=document.form.fpb4.value;
	fpm4=document.form.fpm4.value;
	tfd=document.form.tfd.value;
	tadde=document.form.tadde.value;
	tatc=document.form.tatc.value;
	tafa=document.form.tafa.value;
	tta=document.form.tta.value;
	tge=document.form.tge.value;	
	if (document.form.mt.value==""){
		stop();
		return;
	}
	if (achat<3049){
		fpb1 = achat - 0;
	}
	else{
		fpb1 = 3049 - 0;
	}
	document.form.fpb1.value = fpb1;
	fpm1 = fpb1 * tauxfp1;
	document.form.fpm1.value = parseInt(fpm1);
	if (achat>6098) {
		fpb2 = 6098 - 3049;
	}
	else
	if (achat>3049) {
		fpb2 = achat - 3049;
	}
	else {
		fpb2 = 0;
	}
	document.form.fpb2.value = fpb2;
	fpm2 = fpb2 * tauxfp2;
	document.form.fpm2.value = parseInt(fpm2);

	if (achat>16769) {
		fpb3 = 16769 - 6098;
	}
	else
	if (achat>6098) {
		fpb3 = achat - 6098;
	}
	else {
		fpb3 = 0;
		fpb4 = 0;
	}
	document.form.fpb3.value = fpb3;
	fpm3 = fpb3 * tauxfp3;
	document.form.fpm3.value = parseInt(fpm3);

	if (achat>16769) {
		fpb4 = achat - 16769;
		document.form.max4.value = achat;
	}
	else {
		fpb4 = 0;
	}
	document.form.fpb4.value = fpb4;
	fpm4 = fpb4 * tauxfp4;
	document.form.fpm4.value = parseInt(fpm4);
	fphonoht= fpm1 + fpm2 + fpm3 + fpm4;
	fphonottc=fphonoht+(fphonoht*tva);
	document.form.fphonoht.value = parseInt(fphonoht);
	document.form.fphonottc.value = parseInt(fphonottc);
	document.form.ec.value=ec;
	document.form.fdd.value=fdd;
	tha=ec+fdd;
	document.form.tha.value=tha;
	
	if (achat<=30489) {
		tfd = 152;
	}
	else 
	if (achat<=76224) {
		tfd = 229;
	}
	else
	if (achat<=152449) {
		tfd = 381;
	}
	else
	if (achat<=228673) {
		tfd = 457;
	}
	else {
		tfd = 686;
	}
	document.form.tfd.value = tfd;
	tadde=achat*tauxdde;
	document.form.tadde.value = parseInt(tadde);
	tatc=achat*tauxtc;
	document.form.tatc.value = parseInt(tatc);
	tafa=achat*tauxfa;
	document.form.tafa.value = parseInt(tafa);
	tta=tadde+tatc+tafa;
	document.form.tta.value = parseInt(tta);
	tge=fphonottc+tha+tfd+tta;
	document.form.tge.value = parseInt(tge);
	
	document.form.rfp.value=parseInt(fphonottc);
	document.form.rha.value=parseInt(tha);
	document.form.rfd.value=parseInt(tfd);
	document.form.rta.value=parseInt(tta);
	rt=fphonottc+tha+tfd+tta;
	document.form.rt.value=parseInt(rt);

	pfp=fphonottc/rt*100;
	document.form.pfp.value=parseInt(pfp);
	pha=tha/rt*100;
	document.form.pha.value=parseInt(pha);
	pfd=tfd/rt*100;
	document.form.pfd.value=parseInt(pfd);
	pta=tta/rt*100;
	document.form.pta.value=parseInt(pta);
	pt=pfp+pha+pfd+pta;
	document.form.pt.value=Math.round(pt);


}

function annuler()
{
	document.form.mt.value ="";
	document.form.rfp.value = "";
	document.form.rha.value = "";
	document.form.rfd.value = "";
	document.form.rta.value = "";
	document.form.rt.value = "";
	document.form.pfp.value = "";
	document.form.pha.value = "";
	document.form.pfd.value = "";
	document.form.pta.value = "";
	document.form.pt.value = "";
	document.form.fpb1.value = "";
	document.form.fpb2.value = "";
	document.form.fpb3.value = "";
	document.form.fpb4.value = "";
	document.form.max4.value = "";
	document.form.fpm1.value = "";
	document.form.fpm2.value = "";
	document.form.fpm3.value = "";
	document.form.fpm4.value = "";
	document.form.fphonoht.value = "";
	document.form.fphonottc.value = "";
	document.form.ec.value = "";
	document.form.fdd.value = "";
	document.form.tha.value = "";
	document.form.tfd.value = "";
	document.form.tadde.value = "";
	document.form.tatc.value = "";
	document.form.tafa.value = "";
	document.form.tta.value = "";
	document.form.tge.value = "";
	
	
}

