msg=1;

function ongletClick(ong) {
	f=document.frm;
	if (onglet==1) {
		if (strtoint(f.montant.value)<=0) {
			jalert("Veuillez saisir le montant du prêt");
			f.montant.focus();
			return;
		}
		if (((strtoint(f.mens1p1.value)<=0)||(strtoint(f.dur1p1.value)<=0))
				&&
				((strtoint(f.mens1p2.value)<=0)||(strtoint(f.dur1p1.value)<=0)||(strtoint(f.dur1p2.value)<=0))) {
			jalert("Veuillez saisir au moins un autre prêt");
			f.mens1p1.focus();
			return;
		}
		if ((strtoint(f.menssouh.value)<=0)&&(strtoint(f.revmens.value)<=0)) {
			jalert("Veuillez saisir la mensualité souhaitée ou votre revenu mensuel");
			f.menssouh.focus();
			return;
		}
		if ((strtoint(f.menssouh.value)>0)&&(strtoint(f.revmens.value)>0)) {
			if (strtoint(f.menssouh.value)>strtoint(f.revmens.value)*.33) {
				jalert("Les mensualités souhaitées dépassent votre capacité de remboursement");
				f.menssouh.focus();
				return;
			}
		}
	}
	
	l="index.php?client="+client+"&tpl="+template+"&ong="+ong+"&mnn="+monnaie;
	l+="&mens1p1="+strtoint(f.mens1p1.value);
	l+="&mens1p2="+strtoint(f.mens1p2.value);
	l+="&mens2p1="+strtoint(f.mens2p1.value);
	l+="&mens2p2="+strtoint(f.mens2p2.value);
	l+="&mens3p1="+strtoint(f.mens3p1.value);
	l+="&mens3p2="+strtoint(f.mens3p2.value);
	l+="&mens4p1="+strtoint(f.mens4p1.value);
	l+="&mens4p2="+strtoint(f.mens4p2.value);
	l+="&dur1p1="+strtoint(f.dur1p1.value);
	l+="&dur1p2="+strtoint(f.dur1p2.value);
	l+="&dur2p1="+strtoint(f.dur2p1.value);
	l+="&dur2p2="+strtoint(f.dur2p2.value);
	l+="&dur3p1="+strtoint(f.dur3p1.value);
	l+="&dur3p2="+strtoint(f.dur3p2.value);
	l+="&dur4p1="+strtoint(f.dur4p1.value);
	l+="&dur4p2="+strtoint(f.dur4p2.value);

	l+="&mnt="+strtoint(f.montant.value);
	l+="&txi="+strtofloat(f.tauxInteret.value);
	l+="&txa="+strtofloat(f.tauxAssurance.value);

	l+="&menssouh="+strtoint(f.menssouh.value);
	l+="&revmens="+strtoint(f.revmens.value);
	l+="&txendet="+strtoint(f.txendet.value);

	if (onglet==1) {
		l+="&tpa="+f.typeAssurance.selectedIndex;
	} else {
		l+="&tpa="+f.typeAssurance.value;
	}
	
	if (onglet<4) {
		l+="&dur="+(f.duree.options[f.duree.selectedIndex].text);
	} else {
		l+="&dur="+f.duree.value;
	}
	
	location=l;
	return false;
}

function convertClick() {
	f=document.frm;
	monnaie=1-f.monnaie.value;
	f.monnaie.value=monnaie;
	coeff=(monnaie==1)?1/pariteEuro:pariteEuro;

	f.montant.value=inttostr(Math.round(strtoint(f.montant.value)*coeff));
	f.menssouh.value=inttostr(Math.round(strtoint(f.menssouh.value)*coeff));
	f.revmens.value=inttostr(Math.round(strtoint(f.revmens.value)*coeff));

	f.mens1p1.value=inttostr(Math.round(strtoint(f.mens1p1.value)*coeff));
	f.mens1p2.value=inttostr(Math.round(strtoint(f.mens1p2.value)*coeff));
	f.mens2p1.value=inttostr(Math.round(strtoint(f.mens2p1.value)*coeff));
	f.mens2p2.value=inttostr(Math.round(strtoint(f.mens2p2.value)*coeff));
	f.mens3p1.value=inttostr(Math.round(strtoint(f.mens3p1.value)*coeff));
	f.mens3p2.value=inttostr(Math.round(strtoint(f.mens3p2.value)*coeff));
	f.mens4p1.value=inttostr(Math.round(strtoint(f.mens4p1.value)*coeff));
	f.mens4p2.value=inttostr(Math.round(strtoint(f.mens4p2.value)*coeff));

	return ongletClick(onglet);
}

function mensChange() {
	f=document.frm;
	f.totalp1.value=inttostr(strtoint(f.mens1p1.value)+strtoint(f.mens2p1.value)+strtoint(f.mens3p1.value)+strtoint(f.mens4p1.value));
	calcMensAvLiss();
}

var tabLibMois=new Array("janv","févr","mars","avr","mai","juin","juil","août","sept","oct","nov","déc");

function durChange() {
	f=document.frm;

	d1=strtoint(f.dur1p1.value);
	d2=strtoint(f.dur1p2.value);
	if (d1) {
		dm=currentmonth+d1;
		da=currentyear+parseInt((dm-1)/12);
		dm=(dm-1)%12+1;
		f.ech1p1.value=tabLibMois[dm-1]+"-"+da;
		if (d2) {
			dm=currentmonth+d1+d2;
			da=currentyear+parseInt((dm-1)/12);
			dm=(dm-1)%12+1;
			f.ech1p2.value=tabLibMois[dm-1]+"-"+da;
		}
	}

	d1=strtoint(f.dur2p1.value)
	d2=strtoint(f.dur2p2.value)
	if (d1) {
		dm=currentmonth+d1;
		da=currentyear+parseInt((dm-1)/12);
		dm=(dm-1)%12+1;
		f.ech2p1.value=tabLibMois[dm-1]+"-"+da;
		if (d2) {
			dm=currentmonth+d1+d2;
			da=currentyear+parseInt((dm-1)/12);
			dm=(dm-1)%12+1;
			f.ech2p2.value=tabLibMois[dm-1]+"-"+da;
		}
	}

	d1=strtoint(f.dur3p1.value)
	d2=strtoint(f.dur3p2.value)
	if (d1) {
		dm=currentmonth+d1;
		da=currentyear+parseInt((dm-1)/12);
		dm=(dm-1)%12+1;
		f.ech3p1.value=tabLibMois[dm-1]+"-"+da;
		if (d2) {
			dm=currentmonth+d1+d2;
			da=currentyear+parseInt((dm-1)/12);
			dm=(dm-1)%12+1;
			f.ech3p2.value=tabLibMois[dm-1]+"-"+da;
		}
	}

	d1=strtoint(f.dur4p1.value)
	d2=strtoint(f.dur4p2.value)
	if (d1) {
		dm=currentmonth+d1;
		da=currentyear+parseInt((dm-1)/12);
		dm=(dm-1)%12+1;
		f.ech4p1.value=tabLibMois[dm-1]+"-"+da;
		if (d2) {
			dm=currentmonth+d1+d2;
			da=currentyear+parseInt((dm-1)/12);
			dm=(dm-1)%12+1;
			f.ech4p2.value=tabLibMois[dm-1]+"-"+da;
		}
	}

	calcMensAvLiss();
}

function calcMensAvLiss() {
	f=document.frm;
	montant=strtoint(f.montant.value);
	duree=strtoint(f.duree.options[f.duree.selectedIndex].text);
	tauxInteret=strtofloat(f.tauxInteret.value);
	tauxAssurance=strtofloat(f.tauxAssurance.value);
	typeAssurance=(onglet==1)?f.typeAssurance.selectedIndex:f.typeAssurance.value;
	if ((montant>0)&&(tauxInteret>0)) {
		if (typeAssurance==0) {
			mensualiteHorsAssurance=calcMens(montant,duree,tauxInteret,0);
			mensualiteAssurance=montant*tauxAssurance/1200;
			mensualiteAssuranceComprise=mensualiteHorsAssurance+mensualiteAssurance;
		} else {
			mensualiteAssuranceComprise=calcMens(montant,duree,tauxInteret,tauxAssurance);
		}
		if (onglet==1) f.mensavliss.value=inttostr(mensualiteAssuranceComprise);
	} else {
		if (onglet==1) f.mensavliss.value="";
	}
	if (onglet==1) calcMensCalc();
}

function dureeChange() {
	f=document.frm;
	var s=f.duree.options[f.duree.selectedIndex].text;
	var d=strtoint(s);
	if (d<dureeMini) d=dureeMini;
	if (d>dureeMaxi) d=dureeMaxi;

	var t=eval("f.tauxInteret"+d+".value");
	f.tauxInteret.value=floattostr(t);

	calcMensAvLiss();
}

function menssouhChange() {
	calcMontDisp();
}

function txendetChange() {
	f=document.frm;
	if (strtoint(f.txendet.value)>33) {
		jalert("Le taux d'endettement ne doit pas excéder 33%");
		f.txendet.focus();
	} else {
		calcMensCalc();
	}
}

function calcMensCalc() {
	f=document.frm;
	f.menscalc.value=inttostr(strtoint(f.revmens.value)*strtoint(f.txendet.value)/100);
	calcMontDisp();
}

function calcMontDisp() {
	f=document.frm;
	menssouh=strtoint(f.menssouh.value);
	menscalc=strtoint(f.menscalc.value);
	var mensmax=0;
	if (menscalc>0) {
		if (menssouh>0) {
			mensmax=Math.min(menssouh,menscalc);
		} else {
			mensmax=menscalc;
		}
	} else {
		if (menssouh>0) {
			mensmax=menssouh;
		} else {
		}
	}
	if ((menscalc>0)||(menssouh>0)) {
		if ((mensmax<strtoint(f.totalp1.value))&&(msg)) {
			jalert("Mensualité insuffisante");
			msg=0;
		}
	}
	f.disp.value=inttostr(Math.max(mensmax-strtoint(f.totalp1.value),0));
}

function calcCoefPret(i,duree) {
	return Math.pow(1+i,duree);
}

function calcMens(mnt,dur,txi,txa) {
	mis=dur*12;
	if(typeAssurance==1) {
		txm=(txi+txa)/1200;
		prw=calcCoefPret(txm,mis);
		result=mnt*prw*txm/(prw-1);
	} else {
		txm=txi/1200;
		prw=calcCoefPret(txm,mis);
		result=mnt*(prw*txm/(prw-1)+txa/1200);
	}
	return result;
}

