// JavaScript Document

function formatMoney(quoi, test)
{
	quoi = Math.round(quoi*100)/100;
	quoi = "" + quoi;
	mSplit = quoi.indexOf(".");
	if (mSplit==-1)
	{
		return(quoi+".00 $")
	}
	else
	{
		quoi = "" + Math.round(quoi*100)/100;
		mSplit = quoi.lastIndexOf(".");
		diff = quoi.length - mSplit;
		if (diff==2)
		{
			return(quoi+"0 $")
		}
		else
		{
			var money = (Math.round(quoi*100)/100);
			money+="";
			mSplit = money.indexOf(".");
			if (mSplit == -1)
			{
				return (money+".00 $")
			}
			
			return money+" $";
		}
	}
}


function cutDecimals(quoi)
{
	var taux = quoi;
	point = taux.indexOf(".")
	if (point!=-1)
	{
		place = taux.split(".");
		if (place[1].length>2)
		{
			place[1]=place[1].substr(0,2);
		}
		return (place[0]+"."+place[1]);
	}
	else
	{
		return (taux);
	}
}

function credCalcTemps()
{
	var paim, solde, taux;
	var tauxMonth, mInter, curMonth, content, premSolde, curInter, montantTotal;
	
	paim = document.getElementById("paiment").value;
	solde = document.getElementById("solde").value;
	taux = cutDecimals(document.getElementById("taux").value);
	document.getElementById("taux").value=taux;
	
	if (paim == "" || solde == "" || taux == "") { return; }
	
	if (paim*1>solde*1)
	{
		alert ("Your payment should not exceed the amount owing!");
		return;
	}
	
	if (paim<Math.max(10,solde*0.03) && solde!=paim)
	{
		alert ("Careful! You must make a minimum payment of  $ "+(formatMoney(Math.max(10,solde*0.03)))+".");
		return;
	}

	//if ((solde * (taux/100) / 12) >= Math.max(10,solde*0.03))
	if ((solde * (taux/100) / 12) >= paim)
	{
		alert ("Because of the high interest rate, you'll never pay off the balance on your credit card by making only the minimum payments.");
		return;
	}
	
	tauxMonth = taux/12;
	mInter = 0;
	
	curMonth = 1;
	content="";
	
	// Tu auras besoin de 
	content+="<div class='calcresult'>";
		content+="It will take   <month> month(s) to repay your debt (i.e <an> year(s)).  You will have paid  <dd2> <total> in interest charges.";
	content+="</div>";
	
	content+="<table width='100%' class='calcresult'>";
	content+="<tr><th>Months</th><th>Amount owing</th><th>Interest charges</th><th>Total amount owing</th><th>Payment</th><th>Balance</th></tr>";

	if (formatMoney(solde)!=formatMoney(paim))
	{
		while (solde>0 && curMonth<=300)
		{
			premSolde = solde;
			curInter = Math.round(0+solde*tauxMonth)/100;
			montantTotal = (solde*1 + curInter*1)*1;
			solde = montantTotal - paim;
			
			if (montantTotal<paim)
			{
				paim = montantTotal
				solde = montantTotal - paim;
			}
			
			content+="<tr";
			if (curMonth % 2 == 0){ content+=" class='alt' "; }
			content+="><td>"+curMonth+"</td><td nowrap>"+formatMoney(premSolde)+"</td><td nowrap >"+formatMoney(curInter)+"</td><td nowrap>"+formatMoney(montantTotal)+"</td><td nowrap>"+formatMoney(paim)+"</td><td nowrap>"+formatMoney(solde)+"</td></tr>";
			curMonth++;
			mInter += curInter*1;
		}
	}
	else
	{
			mInter=0;
			curInter=0;
			premSolde = solde;
			montantTotal = premSolde;
			solde = montantTotal - paim;
			
			if (montantTotal<paim)
			{
				paim = montantTotal
				solde = montantTotal - paim;
			}
			
			content+="<tr";
			if (curMonth % 2 == 0){ content+=" class='alt' "; }
			content+="><td>"+curMonth+"</td><td nowrap>"+formatMoney(premSolde)+"</td><td nowrap >"+formatMoney(curInter)+"</td><td nowrap>"+formatMoney(montantTotal)+"</td><td nowrap>"+formatMoney(paim)+"</td><td nowrap>"+formatMoney(solde)+"</td></tr>";
			curMonth++;

	}
	
	content+="</table>";
	
	//replace values in previous text
	content = content.replace("<month>", curMonth-1);
	toAn = (((curMonth-1)/12)*10)/10;
	toAn = roundNumber(toAn, 2);
	content = content.replace("<an>", toAn);
	
	content = content.replace("<total>",formatMoney(mInter));
	
	if (curMonth>=300)
	{
		content = content.replace("<dd1>", "d'au moins");
		content = content.replace("<dd2>", "au moins");
	}
	else
	{
		content = content.replace("<dd1>", "de");
		content = content.replace("<dd2>", "");
	}
	
	document.getElementById("result").style.display = "block"
	document.getElementById("result").innerHTML = content;
}

function credCalcMois()
{
	var solde, taux, mois;
	var tauxMonth, mInter, curMonth, content, premSolde, curInter, montantTotal, curInter, toAn;
	
	var paim = 0;	
	solde = document.getElementById("solde").value;	
	taux = cutDecimals(document.getElementById("taux").value);
	document.getElementById("taux").value=taux;
	mois = document.getElementById("mois").value;
	
	if (taux == "" || solde == "" || mois == ""){
		alert("Enter a number greater than 0.");
		return;
	} 
	
	if (solde*1<=0){
		alert("You have no balance to repay.");
		return;
	} 
	
	//Get fewest month number possible to pay back card.  if input month is below, alert and return!
	if (mois>getHighestMonth())
	{
		alert("The number of months entered is too high and does not meet minimum payment requirements.");
		return;
	}
	
	tauxMonth = taux/12;
	mInter = 0;
	
	curMonth = 1;
	content="";
	
	//     Tu auras besoin de 
	content+="<div class='calcresult'>";
	content+="You will have to pay  <paiment> per month to reach your objective.  You will have paid  <interet> in interest charges.";
		//content+="Tu auras besoin de <month> mois pour rembourser ton pr&ecirc;t au complet.  (soit <an> ann&eacute;e(s))  Tu auras alors pay&eacute; <total> en frais d'int&eacute;r&ecirc;t.";
	content+="</div>";
	
	content+="<table width='100%' class='calcresult'>";
	content+="<tr><th>Months</th><th>Amount owing</th><th>Interest charges</th><th>Total amount owing</th><th>Payment</th><th>Balance</th></tr>";
	
	if (taux!=0)
	{
		paim = solde*(taux/100)/12/(1-(1/Math.pow((1+(taux/100)/12),mois)));
	}
	else
	{
		paim = 0;
	}
	
	//paim = Math.round(paim*100)/100;
	if (mois!=1 && paim!=0)
	{
		while (solde>0)
		{
			//alert ("deb : "+solde)
			premSolde = solde;
			curInter = (0+solde*tauxMonth)/100;
			//curInter = Math.round(curInter*100)/100;
			montantTotal = (solde*1 + curInter*1);
			//montantTotal = Math.round(montantTotal*100)/100;
			solde = montantTotal - paim;
			
			if (montantTotal < paim)
			{
				paim = montantTotal
				solde = montantTotal - paim;
			}
			
			/*Fix the less than a cent bug*/
			if (solde<0.01)
			{
				solde=0;	
			}
			
			content+="<tr";

			if (curMonth % 2 == 0){ content+=" class='alt' "; }
			content+="><td>"+curMonth+"</td><td nowrap>"+formatMoney(premSolde)+"</td><td nowrap>"+formatMoney(curInter)+"</td><td nowrap>"+formatMoney(montantTotal)+"</td><td nowrap>"+formatMoney(paim)+"</td><td nowrap>"+formatMoney(solde)+"</td></tr>";
			curMonth++;
			mInter += curInter*1;
			//alert ("fin : "+solde)
		}
	}
	else
	{
			premSolde = solde;
			curInter = 0;
			montantTotal = premSolde;
			solde = 0;
			paim=montantTotal;
			content+="<tr";
			if (curMonth % 2 == 0){ content+=" class='alt' "; }
			content+="><td>"+curMonth+"</td><td nowrap>"+formatMoney(premSolde)+"</td><td nowrap>"+formatMoney(curInter)+"</td><td nowrap>"+formatMoney(montantTotal)+"</td><td nowrap>"+formatMoney(paim)+"</td><td nowrap>"+formatMoney(solde)+"</td></tr>";
			curMonth++;
			mInter += curInter*1;	}
	
	content+="</table>";
	
	//replace values in previous text
	//content = content.replace("<month>", curMonth-1);
	//toAn = (((curMonth-1)/12)*10)/10;
	//toAn = roundNumber(toAn, 2);
	content = content.replace("<paiment>", formatMoney(paim));
	
	content = content.replace("<interet>",formatMoney(mInter));
	
	document.getElementById("result").style.display = "block"
	document.getElementById("result").innerHTML = content;
}

function credCalcMin()
{
	var solde, taux, pSolde;
	var paim, tauxMonth, mInter, curMonth, content, premSolde, curInter, montantTotal;
	solde = document.getElementById("solde").value;	
	pSolde = document.getElementById("solde").value;
	taux = cutDecimals(document.getElementById("taux").value);
	document.getElementById("taux").value=taux;
	
	if (taux == "" || solde == ""){
		alert("Entrez un nombre supérieur ou égal à 0.");
		return;
	}
	
	/*See if it is possible due to high interest*/
	if ((solde * (taux/100) / 12) >= Math.max(10,solde*0.03))
	{
		alert("Because of the high interest rate, you'll never pay off the balance on your credit card by making only the minimum payments.");
		return;
	}
	
	tauxMonth = taux/12;
	mInter = 0;
	
	curMonth = 1;
	content="";
	
	//     Tu auras besoin de 
	content+="<div class='calcresult'>";
		content+="It will take   <month> month(s) to repay your debt. (i.e <an> year(s)).  You will have paid  <dd2> <total> in interest charges";
	content+="</div>";
	
	content+="<table width='100%' class='calcresult'>";
	content+="<tr><th>Months</th><th>Amount owing</th><th>Interest charges</th><th>Total amount owing</th><th>Payment</th><th>Balance</th></tr>";
	
	while (solde>0 && curMonth<=300)
	{
		premSolde = solde;
		
		if (pSolde*1 != 1)
		{
			curInter = Math.round(0+solde*tauxMonth)/100;
		}
		else
		{
			curInter=0;
		}
		
		if(getHighestMonth()==1)
		{
			montantTotal = solde*1;
			curInter=0;
		}
		else
		{
			montantTotal = (solde*1 + curInter*1)*1;
		}		
		    //if (curMonth==1)

                        //{
                                    var paim = Math.max(10,solde*0.03);
                        //}

                        //else

                        //{
                                    //var paim = (solde*0.03);
                        //}


		paim = Math.round(paim*100)/100;
		if (paim<10)
		{
			paim = 10;	
		}
		
		solde = montantTotal - paim;
		
		if (montantTotal<paim)
		{
			paim = montantTotal
			solde = montantTotal - paim;
		}
		
		content+="<tr";
		if (curMonth % 2 == 0){ content+=" class='alt' "; }
		content+="><td>"+curMonth+"</td><td>"+formatMoney(premSolde)+"</td><td>"+formatMoney(curInter)+"</td><td>"+formatMoney(montantTotal)+"</td><td>"+formatMoney(paim)+"</td><td nowrap>"+formatMoney(solde)+"</td></tr>";
		curMonth++;
		mInter += curInter*1;
	}
	
	content+="</table>";
	
	//replace values in previous text
	content = content.replace("<month>", curMonth-1);
	toAn = (((curMonth-1)/12)*10)/10;
	toAn = roundNumber(toAn, 2);
	content = content.replace("<an>", toAn);
	
	content = content.replace("<total>",formatMoney(mInter));
	
	if (curMonth>=300)
	{
		content = content.replace("<dd1>", "d'au moins");
		content = content.replace("<dd2>", "au moins");
	}
	else
	{
		content = content.replace("<dd1>", "de");
		content = content.replace("<dd2>", "");
	}
	
	document.getElementById("result").style.display = "block"
	document.getElementById("result").innerHTML = content;
}






function interetComp()
{
	epargne = document.getElementById("epargne").value;
	pourc = document.getElementById("pourc").value;
	an = document.getElementById("an").value;
	
	pourc = pourc/100;
	
	if (epargne!="" && pourc!="" && an!="")
	{
		data1 = (epargne*(Math.pow((1+pourc),an)-1))/pourc
		document.getElementById("total").value = formatMoney(data1);
	}
}



function obligation()
{
	var echeance = document.getElementById("echeance").value;
	var taux = document.getElementById("taux").value;
	var duree = document.getElementById("duree").value;
	var montant = document.getElementById("montant").value;
	
	/*if (duree*1 < 0.083)
	{
		duree=0.083;
		document.getElementById("duree").value = duree;
	}*/
	
	if (echeance!="" && taux!="" && duree!="" && montant!="")
	{
		document.getElementById("box11").value = formatMoney(echeance*(taux/100));
		document.getElementById("box12").value = taux;
		document.getElementById("box13").value = formatMoney(echeance);
		
		document.getElementById("box21").value = formatMoney((echeance-montant)/duree);
		document.getElementById("box22").value = formatMoney(echeance);
		document.getElementById("box23").value = formatMoney(montant);
		document.getElementById("box24").value = formatMoney(echeance-montant);
		
		document.getElementById("box31").value = duree;
		document.getElementById("box32").value = formatMoney(echeance-montant);
		document.getElementById("box33").value = duree;
		document.getElementById("box34").value = formatMoney((echeance-montant)/duree);
		
		document.getElementById("box41").value = formatMoney((echeance*(taux/100))+((echeance-montant)/duree));
		document.getElementById("box42").value = formatMoney((echeance*(taux/100)));
		document.getElementById("box43").value = formatMoney((echeance-montant)/duree);
		
		document.getElementById("box51").value = formatMoney(montant);
		
		document.getElementById("box61").value = roundNumber((((echeance*(taux/100))+((echeance-montant)/duree))/montant)*100,2);
		document.getElementById("box62").value = formatMoney(((echeance*(taux/100))+((echeance-montant)/duree)));
		document.getElementById("box63").value = formatMoney(montant);
		document.getElementById("box64").value = roundNumber((((echeance*(taux/100))+((echeance-montant)/duree))/montant)*100,2);
		
		
			document.getElementById("box65").value = roundNumber((((echeance*(taux/100))+((echeance-montant)/duree))/montant)*100,2);
		document.getElementById("box66").value = formatMoney(((echeance*(taux/100))+((echeance-montant)/duree)));
		document.getElementById("box67").value = formatMoney(montant);
		document.getElementById("box68").value = roundNumber((((echeance*(taux/100))+((echeance-montant)/duree))/montant)*100,2);
	
	}
}

function obligation1()
{
	var interet= 0;
	var valeur = 0;
	var marche= 0;
	var echeance = 0;
	var J2 = 0;
	var J3 = 0;
	var E3 = 0;
	var E5 = 0;
	var E7 = 0;
	var E9 = 0;
		
	var final = 0;
	
	interet = document.getElementById("interet").value;
	valeur = document.getElementById("valeur").value;
	marche = document.getElementById("marche").value;
	echeance = document.getElementById("echeance").value;
	
	interet = interet/100;
	marche = marche/100;
	
	if (interet=="0")
	{
		var J2 = 1/(1+marche);
		var J3 = Math.pow(J2,echeance);
		var E3 = interet*1;
		var E5 = valeur*1;
		var E7 = (marche*1)+0.000000000000000001;
		var E9 = echeance*1;
		final = E3*(E5*(1-J3))/E7+(E5/(Math.pow(1+E7,E9)));
		document.getElementById("rez").value = formatMoney(final);		
	}
	
	if (interet!="" && valeur!="" && marche!="" && echeance!="")
	{

		var J2 = 1/(1+marche);
		var J3 = Math.pow(J2,echeance);
		var E3 = interet*1;
		var E5 = valeur*1;
		var E7 = marche*1;
		var E9 = echeance*1;
		final = E3*(E5*(1-J3))/E7+(E5/(Math.pow(1+E7,E9)));
		document.getElementById("rez").value = formatMoney(final);	
	}
}

function obligation2()
{
	if (document.getElementById("age1").value=="")
		document.getElementById("age1").value=0;
	if (montant1 = document.getElementById("montant1").value=="")
		document.getElementById("montant1").value=0;
	if (rendement1 = document.getElementById("rendement1").value=="")
		document.getElementById("rendement1").value=0;
	
	if (age2 = document.getElementById("age2").value=="")
		document.getElementById("age2").value=0;
									   
	if (montant2 = document.getElementById("montant2").value=="")
		document.getElementById("montant2").value=0;
	if (rendement2 = document.getElementById("rendement2").value=="")
		document.getElementById("rendement2").value=0;
	//_CHANGED______
	var age1 = Math.round(document.getElementById("age1").value);
		document.getElementById("age1").value = age1
	//--____________
	var montant1 = document.getElementById("montant1").value;
	var rendement1 = document.getElementById("rendement1").value;
	
	//_CHANGED______
	var age2 = Math.round(document.getElementById("age2").value);
		document.getElementById("age2").value
	//--____________
	var montant2 = document.getElementById("montant2").value;
	var rendement2 = document.getElementById("rendement2").value;
	
	var lastVal = 0;
	var lastVal2 = 0;
	
	//_ADDED______
	if (age1 > 64 || age2 > 64){
		alert("The age you enter should be less than or equal to 64.");
		return;
	}
	
	var i=1;
	var content="";
	if (age2<age1 && age2!="")
	{
		alert ("For this test, you must start investing at the same time or AFTER the 1st test.");
	}
	else
	{
		if (age1!="" && montant1!="" && rendement1!="")
		{
			content+="<table width='49.5%' class='calcresult'>";
	content+="<tr><th colspan='4'>My investment, example 1</th></tr><tr><th>Age</th><th>Annual investment</th><th>After...</th><th>End of year value </th></tr>";
	
		nSolde = montant1;
		totInvest = montant1;
			while (age1<66)
			{
				nSolde = (+totInvest*1)*(1+rendement1/100);		
				totInvest = montant1*1+nSolde*1;
				content+="<tr";
				if (age1 % 2 == 0){ content+=" class='alt' "; }
				content+="><td>"+age1+"</td><td>"+montant1+"</td><td>"+i+"year(s)</td><td>"+formatMoney(nSolde)+"</td></tr>";
				age1++;
				i++;
				lastVal = formatMoney(nSolde);
			}
	
		content+="</table>";
		}
		
		if (age2!="" && montant2!="" && rendement2!="")
		{
			content+="<table width='49.5%' class='calcresult'>";
	content+="<tr><th colspan='4'>My investment, example 2</th></tr><tr><th>Age</th><th>Annual investment</th><th>After...</th><th>End of year value</th></tr>";
		i=1;
		nSolde = montant2;
		totInvest = montant2;
		age1 = document.getElementById("age2").value;
			while (age1<66)
			{
				content+="<tr";
				if (age1 % 2 == 0){ content+=" class='alt' "; }
				content+="><td"
				
				//if (age1<age2)
				//{
					//content+=" colspan='4'>&nbsp;</td></tr>";
				//}
				//else
				//{
					nSolde = (+totInvest*1)*(1+rendement2/100);		
					totInvest = montant2*1+nSolde*1;
					content+=">"+age1+"</td><td>"+montant2+"</td><td>"+i+"year(s)</td><td>"+formatMoney(nSolde)+"</td></tr>";
					i++;
					lastVal2 = formatMoney(nSolde);
				//}
				age1++;
				
				//totInvest+=montant1;
			}
	
		content+="</table>";		
		}
		
		//recap
		
		val1 = ((66-document.getElementById("age1").value)*montant1)*1;
		val2 = ((66-document.getElementById("age2").value)*montant2)*1;
		
		content+="<table class='calcresult' style='clear: both;'>";
			content+="<tr><td colspan='2'>The magic of interest compound</td></tr>";
			content+="<tr><td>1. In example 1, the total investment amount is: </td><td nowrap>"+formatMoney(val1)+"</td></tr>";
			content+="<tr><td>2. In example 1, the total amount available to me at age 65 will be: </td><td nowrap>"+lastVal+"</td></tr>";
			if (age2!="" && montant2!="" && rendement2!=""){
				content+="<tr><td>3. In example 2, the total investment amount is: </td><td nowrap>"+formatMoney(val2)+"</td></tr>";
				content+="<tr><td>4. In example 2, the total amount available to me at age 65 will be: </td><td nowrap>"+lastVal2+"</td></tr>";
			}
		content+="</table>";
		
		document.getElementById("result").style.display = "block";
		document.getElementById("result").innerHTML = content;
		
	}
}

function getHighestMonth()
{
	var solde, taux;
	var paim, tauxMonth, mInter, curMonth, content, premSolde, curInter, montantTotal;
	solde = document.getElementById("solde").value;	
	taux = document.getElementById("taux").value;	
	
	tauxMonth = taux/12;
	mInter = 0;
	
	curMonth = 1;
	
	while (solde>0)
	{
		
		premSolde = solde;
		curInter = Math.round(0+solde*tauxMonth)/100;
		montantTotal = (solde*1 + curInter*1)*1;

        var paim = Math.max(10,solde*0.03);


		paim = Math.round(paim*100)/100;
		if (paim<10)
		{
			paim = 10;	
		}
		
		solde = montantTotal - paim;
		
		if (montantTotal<paim)
		{
			paim = montantTotal
			solde = montantTotal - paim;
		}
		
		curMonth++;
		mInter += curInter*1;
	}
	

	return(curMonth-1);
}



function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber;
}

// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789.").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return true;
   }
else
   return false;
}
