
var reps,inter,tot,insts;
var reps_i,inter_i,tot_i,insts_i;
var months;

var res_temp="%20%20%20%20%20%20%20%20%3Ch1%3EMortgage%20Calculator%20%3C%2Fh1%3E%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%3Cp%3EMonthly%20Payments%3Cbr%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Cspan%20class%3D%22bold%22%3ERepayment%20%3C%2Fspan%3E%3C%2Fp%3E%3Ch3%3E%26pound%3BREYPAYMENT%3C%2Fh3%3E%0A%3Cdiv%20class%3D%22clearfloat%22%3E%3C%2Fdiv%3E%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%3Cp%3EMonthly%20Payments%3Cbr%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Cspan%20class%3D%22bold%22%3EInterest%20Only%20%20%3C%2Fspan%3E%3C%2Fp%3E%3Ch3%3E%26pound%3BPAYMENTS%3C%2Fh3%3E%0A%3Cdiv%20class%3D%22clearfloat%22%3E%3C%2Fdiv%3E%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%3Ca%20href%3D%22mortgage_pg.shtml%22%20class%3D%22blue_buttons_short%22%3EMore%20Information%3C%2Fa%3E%0A";
var mort_pg_res_temp="%20%20%20%20%20%20%3Cdiv%20class%3D%22two_middle_column%22%3E%0A%20%20%20%20%20%20%20%20%3Ch3%3ERepayment%20Mortgage%3C%2Fh3%3E%0A%20%20%20%20%20%20%20%20%3Cp%3EMonthly%20Repayment%3Cbr%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Cspan%20class%3D%22bold%22%3E%26pound%3BMONTHLY%3C%2Fspan%3E%3C%2Fp%3E%0A%20%20%20%20%20%20%20%20%3Cp%3EInterest%20Paid%3Cbr%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Cspan%20class%3D%22bold%22%3E%26pound%3BINTEREST%3C%2Fspan%3E%3C%2Fp%3E%0A%20%20%20%20%20%20%20%20%3Cp%3ENumber%20of%20Installments%3Cbr%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Cspan%20class%3D%22bold%22%3EMONTHS%3C%2Fspan%3E%3C%2Fp%3E%0A%20%20%20%20%20%20%20%20%3Cp%3ETotal%20Paid%3Cbr%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Cspan%20class%3D%22bold%22%3E%26pound%3BTOTAL%3C%2Fspan%3E%3C%2Fp%3E%0A%20%20%20%20%20%20%3C%2Fdiv%3E%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%3Cdiv%20class%3D%22two_middle_column%22%3E%0A%20%20%20%20%20%3Ch3%3EInterest%20Only%20Mortgage%3C%2Fh3%3E%0A%20%20%20%20%20%20%20%20%3Cp%3EMonthly%20Repayment%3Cbr%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Cspan%20class%3D%22bold%22%3E%26pound%3BMONTHLY_I%3C%2Fspan%3E%3C%2Fp%3E%0A%20%20%20%20%20%20%20%20%3Cp%3EInterest%20Paid%3Cbr%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Cspan%20class%3D%22bold%22%3E%26pound%3BINTEREST_I%3C%2Fspan%3E%3C%2Fp%3E%0A%20%20%20%20%20%20%20%20%3Cp%3ENumber%20of%20Installments%3Cbr%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Cspan%20class%3D%22bold%22%3EMONTHS%3C%2Fspan%3E%3C%2Fp%3E%0A%20%20%20%20%20%20%20%20%3Cp%3ETotal%20Paid%3Cbr%20%2F%3E%0A%20%20%20%20%20%20%20%20%3Cspan%20class%3D%22bold%22%3E%26pound%3BTOTAL_I%3C%2Fspan%3E%3C%2Fp%3E%0A%20%20%20%20%20%20%3C%2Fdiv%3E%0A%20%20%20%20%20%20%3Cdiv%20class%3D%22clearfloat%22%3E%3C%2Fdiv%3E%20%20%20%0A%0A%20%20%20%20%20%20%3Cp%3EThis%20mortgage%20calculation%20is%20provided%20as%20a%20guide%20to%20repayment%20amounts.%20Exact%20repayment%20amounts%20will%20be%20calculated%20by%20any%20chosen%20mortgage%20provider.%3C%2Fp%3E%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%3Ca%20href%3D%22JavaScript%3Awindow.print%28%29%3B%22%20class%3D%22blue_buttons%22%3EPrint%20Results%3C%2Fa%3E%0A";


function do_mortgage(f){
	var ret=calc_mortgage(f.amount.value,f.years.value,f.interest.value);
	if(! ret){
		return;
	}
	var m_section=document.getElementById("mortgage_calculator_mini");
	str=res_temp.replace("REYPAYMENT",commafy(reps.toFixed(2)));
	str=str.replace("PAYMENTS",commafy(reps_i.toFixed(2)));
	m_section.innerHTML=unescape(str);
}

function do_mortgage_page(f){
	var ret=calc_mortgage(f.amount.value,f.years.value,f.interest.value);
	if(! ret){
		return;
	}
	var m_section=document.getElementById("mort_res");
	str=mort_pg_res_temp.replace("MONTHLY_I",commafy(reps_i.toFixed(2)));
	str=str.replace("INTEREST_I",commafy(inter_i.toFixed(2)));
	str=str.replace("TOTAL_I",commafy(tot_i.toFixed(2)));
	
	str=str.replace("MONTHLY",commafy(reps.toFixed(2)));
	str=str.replace("INTEREST",commafy(inter.toFixed(2)));
	str=str.replace("TOTAL",commafy(tot.toFixed(2)));

	str=str.replace(/MONTHS/g,commafy(months));

	m_section.innerHTML=unescape(str);
}

function calc_mortgage (amount,years,interest) {
	amount=amount.replace(/[^\d\.]/g,"");
	years=years.replace(/[\D]/g,"");
	interest=interest.replace(/[^\d\.]/g,"");
//	alert(amount+" "+years+" "+interest);

	if(validate_inputs(amount, years, interest)){
		return false;
	}
	
	months = years * 12;
	interest = 1 + interest / 100 / 12;

	var total_amount = amount * interest;

	var sum_rate = 0;
	for (var x=1; x<=months; x++) {
		sum_rate = sum_rate + Math.pow(interest, x);
	}
	
	var monthly_repayment_int_only = (amount * ((interest - 1) * 12) * 100 / 12) /100;

  var monthly_repayment = total_amount * Math.pow(interest, months) / sum_rate;
	
	var total_paid = monthly_repayment * months;

	reps = round_num(monthly_repayment);
	inter = round_num(total_paid - amount);
	tot = round_num(total_paid);
	insts = round_num(months);

	reps_i = round_num(monthly_repayment_int_only);
	inter_i = round_num(monthly_repayment_int_only * months);
	tot_i = round_num(monthly_repayment_int_only * months);
	insts_i = round_num(months);
	
	return true;
}

function validate_inputs (amount, years, interest) {

	var err = new String();
	if (amount == '') {
	    err+="\n\tNo amount entered";
	}
	if (years == '') {
	    err+="\n\tHow many years for this mortgage?";
	}
	if (interest == '') {
	    err+="\n\tNo interest rate";
	}

	if (err.length > 0) {
	    alert("Error:-"+err);
			return true;
	}
	return false;
}

function round_num (n) {
	var result = Math.round(n*100)/100;
	return(result);
}

function commafy(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

