
function cleanString(string) 
	{
	var decimalPoint = false;
	var tempString = "";

	for (var i=0; i<string.length; i++) 
		{
		if ((string.charAt(i)>="0")&&(string.charAt(i)<="9")) 
			{
			tempString=tempString+string.charAt(i);
			}
		if ((string.charAt(i)==".") && !decimalPoint) 
			{
			tempString=tempString+string.charAt(i);
			decimalPoint = true;
			}
		}
	return tempString;
	}

function roundToTwoDecimalPlaces(string) 
	{
	var tmp = "" + Math.round(string*100);
	return ((tmp.substring(0,tmp.length-2))+"."+(tmp.substring(tmp.length-2,tmp.length)));
	}

function formatP(string) 
	{
	var lengthString = string.length;

	if (lengthString==10) 
		{
		return ("$"+string.charAt(0)+","+string.substring(1,4)+","+string.substring(4,10));
		}
	if (lengthString==9) 
		{
		return ("$"+string.substring(0,3)+","+string.substring(3,9));
		}
	if (lengthString==8) 
		{
		return ("$"+string.substring(0,2)+","+string.substring(2,8));
		}
	if (lengthString==7) 
		{
		return ("$"+string.charAt(0)+","+string.substring(1,7));
		}
	if (lengthString<=6) 
		{
		return("$"+string);
		}
	}


function formati(string) 
	{
	return(string + "%");
	}


function amortize(form) 
	{
	var Q = form.P.value;
	for (j=1; j<=120; j++) 
		{
		var H = roundToTwoDecimalPlaces(Q*(Math.pow((1+eval(form.i.value/200)),(1/6))-1));
		var C = form.M.value-H;
		var R = Q-C;
		Q=R;

		if (j==12) 
			{
			form.oneyear.value = formatP(roundToTwoDecimalPlaces(R));
			}
		if (j==24) 
			{
			form.twoyear.value = formatP(roundToTwoDecimalPlaces(R));
			}
		if (j==36) 
			{
			form.threeyear.value = formatP(roundToTwoDecimalPlaces(R));
			}
		if (j==60) 
			{
			form.fiveyear.value = formatP(roundToTwoDecimalPlaces(R));
			}
		if (j==120) 
			{
			form.tenyear.value = formatP(roundToTwoDecimalPlaces(R));
			}
		}
	}

function calcMonthlyMortgagePayment(P,i,n) 
	{
	P=cleanString(P);
	i=cleanString(i);
	var M = P*((Math.pow((eval(1+(i/200))),(1/6))-1)/(1-(Math.pow((Math.pow((1+(i/200)),(1/6))),(-12*n)))));
	M = roundToTwoDecimalPlaces(M);
	M = formatP(M);
	return M;
	}

function calcMaximumMortgage(form) 
	{
	var income=(cleanString(form.income.value))*12;
	var heating=(cleanString(form.heating.value))*12;
	var loanOne=(cleanString(form.loanOne.value))*12;
	var loanTwo=(cleanString(form.loanTwo.value))*6;
	var propertyTax=(cleanString(form.propertyTax.value))*12;

	var gds = 0.32;
	var tds = 0.40;
	var rdefine = Math.pow((eval(1+((cleanString(form.interestRate.value))/200))),(1/6))-1;
	var purchase = Math.pow((1.0+rdefine),300);
	var gdsTotal = (gds*income)-propertyTax-heating;
	var tdsTotal = (tds*income)-propertyTax-heating-loanOne-loanTwo;
	if (gdsTotal<tdsTotal) 
		{
		var payment=gdsTotal/12;
		} 
	else 
		{
		var payment=tdsTotal/12;
		}
	var maxMortgage = ((payment*(purchase-1.0))/rdefine)/purchase;
	form.maximumMortgage.value=(formatP((roundToTwoDecimalPlaces(maxMortgage))));
	}
