// JavaScript Document

var valeur=0;

/*Récupérer l'élément de la liste*/
function voirSelection(liste)
{
     var numero = liste.selectedIndex;
     valeur = liste.options[numero].value;
}

function formaterNombre(fltValeur) {
	/* Initialisations */
	var strSepMilliers = " ";
	var strSepDecimal = ",";
	var intNbDecimales = 2;
	var strResultat = "";
	var strPartieEntiere = String(Math.floor(fltValeur));
 
	/* Decoupage par milliers */
	var intNbBlocs = parseInt(strPartieEntiere.length / 3);
	var intLongPremBloc = strPartieEntiere.length % 3;
	strResultat = strPartieEntiere.substr(0,intLongPremBloc);
	for (var intIdxBloc = 0; intIdxBloc < intNbBlocs; intIdxBloc++)
		strResultat += strSepMilliers + strPartieEntiere.substr(intIdxBloc*3+intLongPremBloc,3);
 
	/* Ajout de la partie décimale */
	var strPartieDecimale = "";
	if (fltValeur == Math.floor(fltValeur))
		while (strPartieDecimale.length < intNbDecimales)
			strPartieDecimale += "0";
	else
		strPartieDecimale = String(Math.round((fltValeur-Math.floor(fltValeur))*Math.pow(10,intNbDecimales)))
	strResultat += strSepDecimal + strPartieDecimale;
 
	/* Retour de la chaîne résultante */
	return strResultat;
}


/*Calculer le montant du crédit*/
  function main(formulaire)
  
    {
	var i;	
	var i1;
	var n;
	var Df;
	var a; 
	var i2;
	var rs1;
	
/*Vérifier le salaire*/
    var sal = formulaire.salaire.value; 
   if(isNaN(sal))
   {
   alert('Votre devez saisir un nombre');   
   document.forms["simulation"].elements["salaire"].focus(); 
   }
   else
   
   if (sal=="")
   {
   alert('Vous devez saisr votre salaire');   
   document.forms["simulation"].elements["salaire"].focus(); 
   }
 else	 
  if (sal<12000)
   {
   alert('Votre salaire doit être superieur à 12000 DA');   
   document.forms["simulation"].elements["salaire"].focus(); 
   }
 
 
   else
   {	
    /*Vérifier l'âge*/	
	
        var age = formulaire.age.value;
		
	   if (age=='')
		  {
		   alert('Votre devez mentionner votre âge'); 
		   document.forms["simulation"].elements["age"].focus();		   
		   }
		   else
		  
		   if(isNaN(age) || age < 18 || age > 70)
			   {
			   alert('Votre age doit se situer entre 18 et 70 ans !');
			   document.forms["simulation"].elements["age"].focus();
			   }
			else
			{
		  	n=70-age;
			if (n>30)
			n=30; 
 		      
			
	/*Calculer le taux d'intérêt*/
	if (valeur ==0)
	alert('Veuillez sélectionner un type de crédit');
	else
	{
	
	 if (valeur==1 || valeur==3 || valeur==4 || valeur ==9)
	  {
		 if (sal>12000 && sal<=90000)
			{
			i=0.01;
			i1=0.01/12;
			}
			else
			if(sal>90000 && sal<=180000)
			{
			i=0.03;
			i1=0.03/12;
			}
		}	
	  else
		 {
			i=0.0625;
			i1=0.0625/12;
		 }
	
/*Calcul du taux d'intérêt proportionnel*/		
			
			
		/*Calcul de l'annuité*/	
		if (sal<50000)
		a=0.3;
		else
		a=0.5;
      /*Calcul du différé*/
		   if (valeur==6 || valeur==8 || valeur==9)
		   Df=12;
		   		else
				Df=6;
      				
		/*Calculer le montant du crédit*/
		    var n1=n*12;
			var a1=a*sal;
			var x=Df-n1;
			var x1=Df/i1;
		    var y=1+i1;
			var y1=Math.pow(y,-Df);		
			var y2=Math.pow(y,x);
							   
		   {
		    var c=a1*y1/i1*(1-y2);
		    var rs = Math.round(c*100) / 100;
			var mens = Math.round(a1*100) / 100;
			var mens1= formaterNombre(mens);
			i2=i*100;				
       		rs1=formaterNombre(rs);
			 document.getElementById("montant").innerHTML=rs1 + " DA";
			 document.getElementById("taux").innerHTML=i2 + " %";
			 document.getElementById("mons").innerHTML=mens1 + " DA";
			} 
			}
			}
 }
 }
 

