function apportChange() {
	notaireChange();
}
function dureeChange() {
	var d=getIndexValue('dusim');
	t=taux_interet_defaut[d];
	setValue('tain',floattostr(t));
  	calcMensualite();
  	plusvalueChange();
}
function calculNotaire()
{
	if(getValue('typebien')=='NEUF')
		setValue('franot',inttostr(calcul_frais_notaire(strtoint(getValue('monachat')),getValue('locabien'),getValue('typebien'),getValue('nblog'))));
	else
		setValue('franot',inttostr(calcul_frais_notaire(strtoint(getValue('monachat')),getValue('locabien'),getValue('typebien'),0)));
}
function achatChange() {
	calculNotaire();
	notaireChange();
}
function notaireChange() {
	apport=strtoint(getValue('monap'));
	achat=strtoint(getValue('monachat'));
	setValue('fragr',inttostr((achat-apport)*0.011));
	setValue('fragrmem',inttostr((achat-apport)*0.011));
	setValue('trav',inttostr(achat*0.03));
	setValue('travmem',inttostr(achat*0.03));
	calcMensualite();
}
function garantieChange() {
	calcMensualite();
}
function travauxChange() {
	
	calcMensualite();
}
function calcMensualite() {
	apport=strtoint(getValue('monap'));
	achat=strtoint(getValue('monachat'));
	notaire = strtoint(getValue('franot'));
	garantie=strtoint(getValue('fragr'));
	travaux=strtoint(getValue('trav'));
	setValue('coutotc',inttostr(achat+notaire+garantie+travaux));
	pretmontant = achat+notaire+garantie+travaux-apport;
	if (pretmontant>=0) {
		setValue('monpret',inttostr(pretmontant));
		pretduree = strtoint(getIndexValue('dusim'));
		prettaux = strtofloat(getValue('tain'));
		pretassur = strtofloat(getValue('taas'));
		mensualite=calcMens(pretmontant,pretduree,prettaux,pretassur);
		setValue('mens',inttostr(mensualite));
	}
	else
	{
		alert('Le montant du pret est negatif, veuillez modifier les valeurs saisies');
		setValue('monpret',0);
		setValue('franot',0);
		setValue('fragr',0);
		setValue('trav',0);
		setValue('mens',0);
	}
 	plusvalueChange();
}

function calctotalachat() {
 taxefoncs = getValue('utaxfon');
 if (taxefoncs==1)
  taxefonc = strtofloat(getValue('taxfon'));
 else
  taxefonc = strtoint(getValue('taxfon'));
 taxehabs = getValue('utaxha');
 if (taxehabs==1)
  taxehab = strtofloat(getValue('taxha'))
 else
  taxehab = strtoint(getValue('taxha'));
 entretien = strtofloat(getValue('depent'));
 if (getValue('udepent')==1)
  entretien*=12;
 chrgcour = strtofloat(getValue('chacr'));
 if (getValue('uchacr')==1)
  chrgcour*=12;
 plusvalue = strtofloat(getValue('pluvan'));
 plusvalue2 = strtofloat(getValue('pluvto'));

 // total mensualité
 duree=getValue('dusim');
 mensualite=strtoint(getValue('mens'));
 totmensualite = parseInt(mensualite*12*duree);
 setValue('totmens',inttostr(totmensualite));

 // plus-value & autres
 achat=strtoint(getValue('monachat'));
 vente=achat;
 achatchrg=0;
 chrgannuelle = entretien+chrgcour;
 if (taxefoncs==1) chrgannuelle+=vente*taxefonc/100
  else chrgannuelle+=taxefonc;
 if (taxehabs==1) chrgannuelle+=vente*taxehab/100
  else chrgannuelle+=taxehab;
 for (i=0; i<duree; i++) {
  achatchrg+=chrgannuelle;
  chrgannuelle*=(1+plusvalue/100)
 }
 vente*=(1+plusvalue2/100);
 setValue('charge',inttostr(achatchrg));
 setValue('valbfp',inttostr(vente));
 setValue('plusv',inttostr(vente-achat));
 apport=strtoint(getValue('monap'));
 // dépenses nettes
 achatdepnet = apport+totmensualite+achatchrg-vente;

 setValue('depnet',inttostr(achatdepnet));

}


function plusvalueChange() {
 plusvalue = strtofloat(getValue('pluvan')/100);
 duree=strtoint(getValue('dusim'));
 plusvalue2 = Math.round((Math.pow(1+plusvalue,duree)-1)*10000)/100;
 setValue('pluvto',floattostr(plusvalue2));
 setValue('pluvtomem',floattostr(plusvalue2));
 calctotalachat();
}


function ageChange() {
 document.frm.pretassur.value=document.frm.age.options[document.frm.age.selectedIndex].value;
}


function plusvalue2Change() {
 plusvalue2 = strtofloat(getValue('pluvto'))/100;
 duree=strtoint(getValue('dusim'));
 plusvalue = Math.round((Math.pow(1+plusvalue2,1/duree)-1)*10000)/100;
 setValue('pluvan',floattostr(plusvalue));
 setValue('pluvanmem',floattostr(plusvalue));
}

function calctotalloyer() {
	duree = getValue('dusim');
	apport = strtoint(getValue('monap'));
	loyer = strtoint(getValue('loymens'));
	loyer2 = strtoint(getValue('taxhaac'));
	loyer3 = strtoint(getValue('chaent'));
	if (getIndex('uchaent')==1)
		loyer3*=12;
	reeval = strtofloat(getValue('reval'));
	txplace = strtofloat(getValue('renpl'));
	epargne = strtoint(getValue('epamen'));

	loyerc = loyer;
	loyer2c = loyer2+loyer3;
	totalloyer = 0;
	totalloyer2 = 0;
	placement = apport;
	for (i=0; i<duree; i++) {
		totalloyer += loyerc*12;
		totalloyer2 += loyer2c;
		loyerc *= (1+reeval/100);
		loyer2c *= (1+reeval/100);
		placement += (placement+12*epargne/2)*(txplace/100)+12*epargne;
	}
	loyerdepense = totalloyer+totalloyer2-placement+apport+12*epargne*duree;
	setValue('totloy',inttostr(totalloyer));
	setValue('totcha',inttostr(totalloyer2));
	setValue('renpla',inttostr(placement-apport-epargne*12*duree));
	setValue('depnetlo',inttostr(loyerdepense));
}


var frprop;
function mep(v,t) {
 frprop = frprop+v*t/100;
}

function calcCoefPret(i,duree) {
	var p=1;
	for (var j=0; j<duree; j++) p*=(1+i);
	return p
}
function calcMens(mnt,dur,txi,txa) {
	mis=dur*12;
	txm=txi/1200;
	prw=calcCoefPret(txm,mis);
	result=mnt*(prw*txm/(prw-1)+txa/1200);
	return result;
}

