
var compteur=1;
var total_capital = 0;
var solde_total = 0;
var capital_acc=0;
var interet_acc=0;
var langue = null;

function setLangue(lang)
{
	langue = lang;
}


function verification(){
if (window.document.calc.capital.value == 0 || window.document.calc.capital_2.value == ""){
	if(langue == "english" || langue == "en")
		alert("You must enter a cost for the property.");
	else
		alert("Vous devez entrer un coût pour la propriété.");
	window.document.calc.prix.focus();
}
else
{
	if (window.document.calc.periode.value=="" || window.document.calc.periode.value == " "){
		if(langue == "english" || langue == "en")
			alert("You must enter one period of damping.");
		else
			alert("Vous devez entrer une période d'amortissement");
		window.document.calc.periode.focus();
	}
	else
	{
		if (window.document.calc.taux.value=="" || window.document.calc.taux.value == " "){
		if(langue == "english" || langue == "en")
			alert("You must enter an interest rate");
		else			
			alert("Vous devez entrer un taux d'intérêts");
		window.document.calc.taux.focus();
		}
		else
		{
			calcul()
		}
	}
}
}


function place(that, maxlong,champcible2) {



var tour=0;

var trouve="false";

var temp = that.value;

var longueur = temp.length;

var pospoint=0;

var point='false';

var sous="";

var dollars="";



while(tour<longueur){

	caractere = temp.charAt(tour)

	if (caractere==","){

		avant = temp.substring(0,tour);

		apres = temp.substring(tour+1,longueur);

		temp = avant + "." + apres;

		point = 'true';

		pospoint = tour;

	}

	if (caractere==" "){

		avant = temp.substring(0,tour);

		apres = temp.substring(tour+1,longueur);

		temp=avant + apres;		

		tour=tour-1;

		longueur=longueur-1;

	}

	if (caractere=="."){

		point = 'true';

		pospoint = tour;

	}



	tour=tour+1;

};

if (point == 'true'){





	if (longueur == pospoint+1){

		sous = "";



		tour=0

		if (longueur>4){

			while (tour<longueur-4){

				dollars = dollars + temp.charAt(tour)

				tour=tour+1;

			}

			dollars = dollars + "" + temp.charAt(tour) + temp.charAt(tour+1) + temp.charAt(tour+2)

		}

		else

		{

			tour=0

			while (tour<longueur-1){

				dollars = dollars + temp.charAt(tour)

				tour=tour+1;

			}

		}

	}

	else

	{

		if (temp.charAt(pospoint+1) != "" ){

			sous = "." + temp.charAt(pospoint+1)



			if (temp.charAt(pospoint+2) != "" ){

			

				tour=0

				sous = sous + temp.charAt(pospoint+2)

				if (longueur>6){

					while (tour<longueur-6){

						dollars = dollars + temp.charAt(tour)

						tour=tour+1;

					}

					dollars = dollars + " " + temp.charAt(tour) + temp.charAt(tour+1) + 					temp.charAt(tour+2)

	

				}

				else

				{	

					tour=0

						while (tour<longueur-3){

						dollars = dollars + temp.charAt(tour)

						tour=tour+1;

						}

				}

			}

			else

			{

				sous = sous + "0"

				tour=0

				if (longueur>5){

					while (tour<longueur-5){

						dollars = dollars + temp.charAt(tour)

						tour=tour+1;

					}

					dollars = dollars + "" + temp.charAt(tour) + temp.charAt(tour+1) + 					temp.charAt(tour+2)	

				}

				else

				{

					tour=0

					while (tour<longueur-2){

						dollars = dollars + temp.charAt(tour)

						tour=tour+1;

					}

				}

			}

		}

	}

}

else

{

	sous = ""

	tour=0

	if (longueur>3){

		while (tour<(longueur-3)){

			dollars = dollars + temp.charAt(tour)

			tour=tour+1;

		}

		dollars = dollars + "" + temp.charAt(tour) + temp.charAt(tour+1) + temp.charAt(tour+2)

	}

	else

	{

		tour=0

		while (tour<longueur){

			dollars = dollars + temp.charAt(tour)

			tour=tour+1;

		}

	}	

	//calcul();
}



if (dollars==""){

	dollars="0";

}



var temp2 = dollars + sous;


if (champcible2=='paiement'){

	window.document.calc.paiement_2.value = temp2;

	//window.document.calc.paiement.value = temp;

}

if (champcible2=='depot'){

	

	if(window.document.calc.depot_2.value=='0'){
			
		temp='0';

	};

	//alert(temp2);
	//window.document.calc.depot_2.value = temp2;
	
	/*a = window.document.calc.prix.value;
	b = window.document.calc.depot_2.value;
	c = a - b;
	alert(a+ ' '+b);
	alert(c);
	*/
	
	window.document.calc.capital_2.value =parseFloat(window.document.calc.prix.value) - parseFloat(temp2);
	window.document.calc.capital.value = parseFloat(window.document.calc.capital_2.value);
	
	window.document.calc.depot_2.value = temp2;
	
	place(window.document.calc.capital_2,10,'capital');

}

if (champcible2=='capital'){

	window.document.calc.capital_2.value = temp2;

	window.document.calc.capital.value = temp;

}

if (champcible2=='prix'){

	window.document.calc.prix.value = temp2;

	window.document.calc.capital_2.value =temp - parseFloat(window.document.calc.depot_2.value);

	window.document.calc.capital.value = parseFloat(window.document.calc.capital_2.value);

	place(window.document.calc.capital_2,10,'capital');

}








}



function calcul(){

capital = parseFloat(window.document.calc.capital.value);

taux = parseFloat(window.document.calc.taux.value);

periode = parseInt(window.document.calc.periode.value);

depot = parseFloat(window.document.calc.depot_2.value);



for (i=0; i < window.document.calc.frequence.length; i++)

	{  

	if(window.document.calc.frequence[i].selected==true)

		{

		frequence = window.document.calc.frequence[i].value

		}

	}

//taux = (taux / frequence) / 100;
taux = (taux) / 100;
np = (frequence * periode);

taux = parseFloat(taux);

//ericmorin
//taux = Math.pow(1+(taux/2),(1/6.0))-1;

var I = taux/2;
var i = Math.pow((1+I),2)-1;
var j = Math.pow((1+i),(1/frequence))-1;

taux = j;

capital = parseFloat(capital);

np = parseFloat(np);

q=Math.pow((1+taux),np);

paiement = ((capital * q)/(q-1)) * taux



window.document.calc.paiement_2.value = (Math.round(paiement*100))/100;

place(window.document.calc.paiement_2,10,'paiement');



//solde_total = Math.round(parseFloat(window.document.calc.paiement.value) * np *100)/100;



remplirtableau("debut");

}



function placetauxperiode(that,maxlong) {



var tour=0;

var trouve="false";

var temp = that.value;

var longueur = temp.length;

var pospoint=0;

var point='false';



while(tour<longueur){

	caractere = temp.charAt(tour)

	if (caractere==","){

		avant = temp.substring(0,tour);

		apres = temp.substring(tour+1,longueur);

		temp = avant + "." + apres;

		point = 'true';

		pospoint = tour;

	}

	if (caractere==" "){

		avant = temp.substring(0,tour);

		apres = temp.substring(tour+1,longueur);

		temp=avant + apres;		

		tour=tour-1;

		longueur=longueur-1;

	}

	if (caractere=="."){

		point = 'true';

		pospoint = tour;

	}



	tour=tour+1;

};



if (that.name =="taux"){

	window.document.calc.taux.value = temp

}

else

{

	window.document.calc.periode.value = temp

}

}



function remplirtableau(moment){



taux = parseFloat(window.document.calc.taux.value);

periode = parseInt(window.document.calc.periode.value);



for (i=0; i < window.document.calc.frequence.length; i++)

	{  

	if(window.document.calc.frequence[i].selected==true)

		{

		frequence = window.document.calc.frequence[i].value

		}

	}

taux = taux/100;

np = (frequence * periode);

var I = taux/2;
var i = Math.pow((1+I),2)-1;
var j = Math.pow((1+i),(1/frequence))-1;

taux = j;

var cpt;

var interet;

var capital;

var capital2;

var interet2;



if (moment=="debut"){

	compteur = 1;

	total_capital = 0;

	capital_acc=0;

	interet_acc=0;

	solde_total = parseFloat(window.document.calc.capital.value) ;

}


}



function rienecrire(){



window.document.calc.taux.focus();



}
