function js_check_form(form)
{
	if(!IsNumeric(form.mopr.value) || form.mopr.value <= 0 || form.mopr.value > mon_max_pret || form.mopr.value=='')
	{
		alert("Le montant du prêt doit-être un nombre\ncompris entre 1 et "+mon_max_pret+" euros");
		return false;
	}
	if(!IsNumeric(form.tain.value) || form.tain.value <= 0 || form.tain.value > taux_max_int)
	{
		alert("Le montant du taux d'intérêt doit-être un nombre\ncompris entre 0 (exclu) et "+taux_max_int+" %");
		return false;
	}
	if(!IsNumeric(form.taas.value) || form.taas.value < 0 || form.taas.value > taux_max_ass)
	{
		alert("Le montant du taux d'assurance doit-être un nombre\ncompris entre 0 et "+taux_max_ass+" %");
		return false;
	}
	return true;
}
function open_help(aide)
{
	eval("document.getElementById('help_"+aide+"').style.display ='block'");
	if(aide!=1)	document.getElementById('help_1').style.display ='none';
}

function close_help(aide)
{
	eval("document.getElementById('help_"+aide+"').style.display ='none'");
	if(aide!=1) document.getElementById('help_1').style.display ='block';
}


function IsNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}

function getValue(id)
{
	return eval("document.getElementById('"+id+"').value");
}

function setValue(id, valeur)
{
	eval("document.getElementById('"+id+"').value = '"+valeur+"'");
}

function saveNotaire(){
	var saveNotaire = getValue('notaire');
}

function reinitNotaire(){
	var oldNotaire = saveNotaire;
	var newNotaire = getValue('notaire');
	var calculDiffNotaire = getValue('mopr') - oldNotaire + newNotaire;
	setValue('mopr',calculDiffNotaire);
}

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 calculNotaire()
{
		setValue('notaire',inttostr(calcul_frais_notaire(strtoint(getValue('mopr')),'france','ANCIEN',0)));
}

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 lectnb(snbre,signe) {
	var s=snbre.toString();
	var sign="";
	var nbre="";
	var virg=0;
  for (var p=0; p<s.length; p++) {
		var c=s.charAt(p);
		if(!((c>="0" && c<="9") || c=="-" || c=="+" || c=="." || c=="," || c==" "))
			return "";
		if ((c>="1" && c<="9") || ((c=="0")&&(nbre!=""))) nbre+=c;
		if (((c==",")||(c==".")) && (virg==0)) {
			nbre+=".";
			virg++;
		}
		if ((c=="-")&&(nbre=="")&&(signe==1)) sign="-";
	}
	return (nbre=="")?"0":(sign+nbre);
}

function strtoint(snbre,signe) {
	if (strtoint.arguments.length<2) signe=0;
	return Math.round(parseFloat(lectnb(snbre,signe)));
}

function strtofloat(snbre,signe) {
	if (strtofloat.arguments.length<2) signe=0;
	return Math.round(parseFloat(lectnb(snbre,signe))*10000)/10000;
}

function inttostr(inbre,afficherZero) {
	if (inttostr.arguments.length<2) afficherZero=1;
	inbre=Math.round(parseFloat(inbre));
	var sign="";
	if (inbre<0) {
		sign="-";
		inbre=-inbre;
	}
	var s=inbre.toString();
	var snbre="";
	for (var p=0; p<s.length; p++) {
		var c=s.charAt(s.length-p-1);
		if ((p!=0)&&(p%3==0)) {
			snbre=c+" "+snbre;
		} else {
			snbre=c+snbre;
		}
	}
	if ((snbre=="0")&&(afficherZero==0)) snbre=""; else snbre=sign+snbre;
	return snbre;
}


function changeApport(apportPerso , pretEM){
	var appCalC = document.getElementById(apportPerso).value;
	if(appCalC == '') appCalC = 0;
	document.getElementById(pretEM).value = prixInit-appCalC;
	achatChange();
}

