function create_form_emprunt_immobilier(prix)
{

	   var xhr_object = null;
	   if(window.XMLHttpRequest) // Firefox
		  xhr_object = new XMLHttpRequest();
	   else if(window.ActiveXObject) // Internet Explorer
		  xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	   else { // XMLHttpRequest non supporté par le navigateur
		  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		  return;
		}
	xhr_object.open("POST", "/emprunt_form.php", true);
   	xhr_object.onreadystatechange = function() 
	{
      if(xhr_object.readyState == 4) {eval(xhr_object.responseText); }
  	}
   xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   var data = "prix="+prix;
   xhr_object.send(data);
   CommonVoirAnnonceurImmobilier();
}


function ReadNumber(si) {
 var result="";
 for (var pi=0;pi<si.length;pi++) {
  var ci=si.charAt(pi);
  if ((ci!="0")||(result!=""))
   if (ci>="0" && ci<="9")
    result+=ci
   else if ((ci==",")||(ci=="."))
    result+="."
   else if ((ci=="-")&&(result==""))
    result+="-";
 }
 if (result=="") result="0";
 return result;
}

function strtoint(si) {
 return parseInt(ReadNumber(si));
}

function strtofloat(sf) {
 return parseFloat(ReadNumber(sf));
}

function inttostr(i) {
 var result="";
 i=Math.round(parseFloat(i));
 si=i.toString();
 if (isNaN(si)) si="0";
 bi = 0;
 for (pi=si.length-1;pi>=0;pi--) {
  ci = si.charAt(pi);
  if ((bi==0)&&(pi!=si.length-1)&&(ci!="-")) {
   result=ci+" "+result;
  } else {
   result=ci+result;
  }
  bi=(bi+1)%3;
 }
 return result;
}

function floattostr(f) {
 f=Math.round(f*10000)/10000;
 var sf = f.toString();
 var result = "";
 for (pi=0;pi<sf.length;pi++) {
  cf = sf.charAt(pi);
  if (cf=='.')
   result += ','
  else
   result += cf;
 }
 return result;
}



function number_format(src,nv) {
	nv=parseInt(nv,10);
	if (nv<0||nv>9) nv=0; 
	cm = Math.pow(10,nv);
	s = src.value;
	
	if (cm>1) {	
		f= strtofloat(s);
		f2= parseFloat(Math.round(f*cm)) / cm;
		i2= Math.floor(f2);
		v2= Math.round(cm * (f2-i2));
		sv2= "0000000000"+v2;
		sv2d=sv2.length-nv;
		sv2f=sv2.length;
		sv2= sv2.substring(sv2d,sv2f);
		
		sf = inttostr(i2)+","+sv2;
	} else {
		i=strtoint(s);
		sf=inttostr(i);
	}
	src.value=sf;
}	


function calcul_mensual(choix_taux)
{ 
	var f=document.form_mensual;
	var bien,apport,duree,rapport,ta,tm,mens,index;

	apport = strtoint(f.apport.value);
	bien = strtoint(f.bien.value);
	duree = parseInt(f.duree.options[f.duree.selectedIndex].text,10);
	tau = strtofloat(f.taux.value);
	
	tauxDD = new Array(25);
	tauxVA = new Array(25);
	Diff = new Array(25);
	tauxDD[0]=5;
	tauxVA[0]=3.35;
	tauxDD[1]=7;
	tauxVA[1]=3.40;
	tauxDD[2]=12;
	tauxVA[2]=3.80;
	tauxDD[3]=15;
	tauxVA[3]=3.95;
	tauxDD[4]=20;
	tauxVA[4]=4.10;
	tauxDD[5]=25;
	tauxVA[5]=4.25;
	if(choix_taux==0 || tau==0) {
	  for (i=0;i<tauxVA.length;i++) Diff[i]= (tauxDD[i] - duree);
		i=0;
		while(Diff[i] < 0){i++;}
		index=i;
		ta = tauxVA[index] / 100;
		//tm = Math.pow(1+ta,(1/12)) - 1;
		tau = tauxVA[index];
		f.taux.value = tau;
		number_format(f.taux,2);

	} else {
		tau = strtofloat(f.taux.value);
		ta = tau / 100;
	}
	
	//-- verif
	if ( bien<=0 || bien>=9999999 ){
		alert("Entrez une valeur de bien correcte !");
		//f.bien.value="";
		f.bien.focus();
		return;
	}
	if ( tau<=0 || tau>=15 ){
		alert("Le taux indiqué est incorrect !");
		//f.taux.value="";
		f.taux.focus();
		return;
	}
		
	tm = ta / 12;
	mens = (bien-apport) * (tm) / (1 - Math.pow(1/(1+tm),12*duree));
	mens = Math.round(mens);
	
	s_mens = inttostr(mens);
	s_emprunt = inttostr(bien-apport);
	s_cout = inttostr(mens*12*duree-bien+apport);
	
	if (tau>0){
		if(document.All) {
			ctl_montantmensual.innerHTML="<b>"+s_mens+"</b> &euro;/mois";
			ctl_montantemprunt.innerHTML="<b>"+s_emprunt+"</b> &euro;";
			ctl_couttotal.innerHTML="<b>"+s_cout+"</b> &euro;";
		}
		else {
			document.getElementById("ctl_montantmensual").innerHTML = "<b>"+s_mens+"</b> &euro;/mois";
			document.getElementById("ctl_montantemprunt").innerHTML = "<b>"+s_emprunt+"</b> &euro;";
			document.getElementById("ctl_couttotal").innerHTML = "<b>"+s_cout+"</b> &euro;";

		}
	}
}

function calculer_capcite(choix_taux)
{
	//-- Initialisation
	var f=document.form2_capacite;
	var apport,duree,rapport,ta,tm,mens,index,capa;
	
	mens = strtoint(f.mensualite.value);
	apport = strtoint(f.apport.value);
	duree = parseInt(f.duree.options[f.duree.selectedIndex].text,10);	
	tau = strtofloat(f.taux.value);
	

	
	//-- Tableau dynamique
	tauxDD = new Array(25);
	tauxVA = new Array(25);
	Diff = new Array(25);
	tauxDD[0]=5;
	tauxVA[0]=3.35;
	tauxDD[1]=7;
	tauxVA[1]=3.40;
	tauxDD[2]=12;
	tauxVA[2]=3.80;
	tauxDD[3]=15;
	tauxVA[3]=3.95;
	tauxDD[4]=20;
	tauxVA[4]=4.10;
	tauxDD[5]=25;
	tauxVA[5]=4.25;
	if(choix_taux==0 || tau==0) {
				
		//-- Recherche du taux annuel	moyen du marché
		for (i=0;i<tauxVA.length;i++) Diff[i]= (tauxDD[i] - duree);
		i=0;
		while(Diff[i] < 0){i++;}
		index=i;
		ta = tauxVA[index] / 100;
		tm = Math.pow(1+ta,(1/12)) - 1;
		
		tau=tauxVA[index];
		f.taux.value = tau;
		number_format(f.taux,2);

	} else {
		ta = tau / 100;
	}
	ta = tau/100;
	//-- Verifications
	if ( mens<=0 || mens>=10000 ){
		alert("Veuillez enter le montant des mensualités entre 0 et 10000€");
		//f.mensualite.value="";
		f.mensualite.focus();
		return;
	}
	if ( tau<=0 || tau>=15 ){
		alert("Le taux indiqué est incorrect !");
		//f.taux.value="";
		f.taux.focus();
		return;
	}
	
	tm = ta / 12;
	capa = mens * (1 - Math.pow(1/(1+tm),12*duree)) / tm + apport;
	capa = Math.round(capa);
	
	//-- Affichage résultat
	if (tau>0){		
		s_emprunt = inttostr(capa-apport);
		s_capa = inttostr(capa);
		s_cout = inttostr(mens*12*duree-capa+apport);
		ctl2_emprunt.innerHTML="<b>"+s_emprunt+"</b> €";
		ctl2_capacite.innerHTML="<b>"+s_capa+"</b> €";
		ctl2_couttotal.innerHTML="<b>"+s_cout+"</b> €";
	}

}

function CloseEmprunt()
{
document.getElementById("immobilier_annonceur").innerHTML="";
}
