/*
 * fonctions nécessaires au formulaire implémenté dans le .php du même nom
 */
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.mtta.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.mtta.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.mtta.value=chaine.substring(0, chaine.length-1);
			    return;
			}
		}
	}
}

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


function calcul() 
{
	//Mise en variables des données saisies
	var vmtta= document.form.mtta.value;

	//Vérification de la saisie des champs obligatoires
	if (document.form.mtta.value==""){stop();return;}
	if (document.form.nblog.selectedIndex == 0)
		{alert('Vous devez sélectionner le nombre de logement');}
	
	//Déclaration variables locales
	var venp=0;
	
	//Calcul des émoluments du notaire sur frais proportionnels pour logement 1 à 10
	if (document.form.nblog.selectedIndex == 1)
	{
		if (vmtta < 3050)
			{venp = vmtta * (5/100);}
			
		if ((vmtta > 3049) && (vmtta < 6100))
			{venp = (vmtta * (3.30/100)) + 51.850;}
			
		if ((vmtta > 6099) && (vmtta < 16770))
			{venp = (vmtta * (1.65/100)) + 152.50;}
			
		if (vmtta > 16769)
			{venp = (vmtta * (0.825/100)) + 290.853;}
			
		document.form.enp.value=parseInt(venp);
	}


	//Calcul des émoluments du notaire sur frais proportionnels pour logement 10 à 25
	if (document.form.nblog.selectedIndex == 2)
	{
		if (vmtta < 3050)
		{venp = vmtta * (4/100);}
		
		if ((vmtta > 3049) && (vmtta < 6100))
			{venp = (vmtta * (2.64/100)) + 41.480;}
			
		if ((vmtta > 6099) && (vmtta < 16770))
			{venp = (vmtta * (1.32/100)) + 122;}
		
		if (vmtta > 16769)
			{venp = (vmtta * (0.66/100)) + 232.682;}
		
		document.form.enp.value=parseInt(venp);
	}


	//Calcul des émoluments du notaire sur frais proportionnels pour logement 25 à 100
	if (document.form.nblog.selectedIndex == 3)
	{
		if (vmtta < 3050)
		{venp = vmtta * (3.33333/100);}
		
		if ((vmtta > 3049) && (vmtta < 6100))
			{venp = (vmtta * (2.20/100)) + 34.567;}
			
		if ((vmtta > 6099) && (vmtta < 16770))
			{venp = (vmtta * (1.10/100)) + 101.667;}
		
		if (vmtta > 16769)
			{venp = (vmtta * (0.55/100)) + 193.902;}
		
		document.form.enp.value=parseInt(venp);
	}


	//Calcul des émoluments du notaire sur frais proportionnels pour logement 100 à 250
	if (document.form.nblog.selectedIndex == 4)
	{
		if (vmtta < 3050)
		{venp = vmtta * (2.50/100);}
		
		if ((vmtta > 3049) && (vmtta < 6100))
			{venp = (vmtta * (1.65/100)) + 25.925;}
			
		if ((vmtta > 6099) && (vmtta < 16770))
			{venp = (vmtta * (0.825/100)) + 76.250;}
		
		if (vmtta > 16769)
			{venp = (vmtta * (0.4125/100)) + 145.426;}
		
		document.form.enp.value=parseInt(venp);
	}


	//Calcul des émoluments du notaire sur frais proportionnels pour logement 250 à 500
	if (document.form.nblog.selectedIndex == 5)
	{
		if (vmtta < 3050)
		{venp = vmtta * (2.00/100);}
		
		if ((vmtta > 3049) && (vmtta < 6100))
			{venp = (vmtta * (1.32/100)) + 20.740;}
			
		if ((vmtta > 6099) && (vmtta < 16770))
			{venp = (vmtta * (0.66/100)) + 61;}
		
		if (vmtta > 16769)
			{venp = (vmtta * (0.33/100)) + 116.341;}
		
		document.form.enp.value=parseInt(venp);
	}


	//Calcul des émoluments du notaire sur frais proportionnels pour logement  500 à plus
	if (document.form.nblog.selectedIndex == 6)
	{
		if (vmtta < 3050)
		{venp = vmtta * (1.66667/100);}
		
		if ((vmtta > 3049) && (vmtta < 6100))
			{venp = (vmtta * (1.10/100)) + 17.283;}
			
		if ((vmtta > 6099) && (vmtta < 16770))
			{venp = (vmtta * (0.55/100)) + 50.833;}
		
		if (vmtta > 16769)
			{venp = (vmtta * (0.275/100)) + 96.951;}
		
		document.form.enp.value=parseInt(venp);
	}


	//Calcul de la tva sur émoluments proportionnel
	var tva=19.6/100;
	var vtvae1 = venp*tva;
	document.form.tvae1.value = parseInt(vtvae1);
	

	//Calcul émoluments du notaire sur formalités
	var venf = 152;
	document.form.enf.value = venf;
	
	//Calcul de la tva sur émoluments de formalité
	var vtvae2 = venf*tva;
	document.form.tvae2.value = parseInt(vtvae2);
	
	//Calcul des timbres fiscaux
	var vtf = 381;
	document.form.tf.value = vtf;
	
	//Calcul du salaire du conservateur
	var vsc = vmtta * 0.001;
	document.form.sc.value = parseInt(vsc);
	
	//Calcul de la taxe foncière
	//var vtpf = vmtta *0.00514214;
	var vmttaht = vmtta / 1.196;
	var vtpf = vmttaht * 0.006;
	document.form.tpf.value =parseInt(vtpf);
	
	//Affichage du total du tableau détail
	var vtdc=parseInt(venp+venf+vtvae1+vtvae2+vtf+vsc+vtpf);
	document.form.tdc.value=vtdc;

	
	//Affichage des pourcentages
	//Pourcentage émoluments proportionnels du notaire
	var vpenp = 0;
	vpenp = ((venp/vtdc)*100);
	document.form.penp.value = parseInt(vpenp);

	//Pourcentage tva sur émoluments proportionnels
	var vptvae1 = 0;
	vptvae1 = (vtvae1 / vtdc)*100 ;
	document.form.ptvae1.value = parseInt(vptvae1);
	
	//Pourcentage émoluments sur formalités
	var vpenf = 0;
	vpenf = (venf / vtdc)*100 ;
	document.form.penf.value = parseInt(vpenf);
	
	//Pourcentage tva sur émoluments de formalités
	var vptvae2 = 0;
	vptvae2 = (vtvae2 / vtdc)*100 ;
	document.form.ptvae2.value = parseInt(vptvae2);	
	
	//Pourcentage timbres fiscaux
	var vptf = 0;
	vptf = (vtf / vtdc)*100 ;
	document.form.ptf.value = parseInt(vptf);
	
	//Pourcentage salaire du conservateur
	var vpsc = 0;
	vpsc = (vsc / vtdc)*100 ;
	document.form.psc.value = parseInt(vpsc);
	
	//Pourcentage taxe de publicité foncière
	var vptpf = 0;
	vptpf = (vtpf / vtdc)*100 ;
	document.form.ptpf.value = parseInt(vptpf);
	
	//Pourcentage du total
	var vptdc = 0;
	vptdc = (vtdc / vtdc)*100 ;
	document.form.ptdc.value = parseInt(vptdc);
	
	//Pourcentage du total général estimé
	var vptge = 0;
	vptge = (vtdc / vtdc)*100 ;
	document.form.ptge.value = parseInt(vptge);
	
	//Affichage total général	
	document.form.tge.value=vtdc;
}


function annuler()
{
	document.form.mtta.value = "";
	document.form.nblog.selectedIndex  = "";
	document.form.enp.value = "";
	document.form.tvae1.value = "";
	document.form.enf.value = "";
	document.form.tvae2.value = "";
	document.form.tf.value = "";
	document.form.sc.value = "";
	document.form.tpf.value = "";
	document.form.tdc.value = "";
	document.form.tge.value = "";
	document.form.penp.value = "";
	document.form.ptvae1.value = "";
	document.form.penf.value = "";
	document.form.ptvae2.value = "";
	document.form.ptf.value = "";
	document.form.psc.value = "";
	document.form.ptpf.value = "";
	document.form.ptdc.value = "";
	document.form.ptge.value = "";
}
 
function stop()
{
	alert("Il manque des données.\rVeuillez saisir tous les champs s'il vous plaît.");
}

