// Javascript calculators to work out court stamp duty and mortgage repayments
// Written by Tony Alexandre, alext@jerseymail.co.uk, 2007-2009
// For Homelink, www.homelink.je
// Last updated 21/08/2009

function calculatestamps()
{

// set variables

var propertyprice=document.stampcalc.PropertyPrice.value ;
var amountborrowed=document.stampcalc.AmountBorrowed.value ;
var firsttimebuyer=document.stampcalc.FirstTimeBuyer.value ;
var purchasestamps=0;
var loanstamps=0;
var disbursements=321.06;
var grandtotal=0;

	if (firsttimebuyer ==1)
	{ // first time buyer selected
		//Stamps on Purchase
		if (propertyprice <=300000) {purchasestamps=0;}		
		if (propertyprice >300000 && propertyprice <= 400000) {purchasestamps=((propertyprice-300000)*0.01)+0;}
		if (propertyprice >400000 && propertyprice <= 500000) {purchasestamps=((propertyprice-300000)*0.02)+4000;}
		if (propertyprice >500000 && propertyprice <= 700000) {purchasestamps=((propertyprice-500000)*0.025)+8000;}
		if (propertyprice >700000) {purchasestamps=((propertyprice-700000)*0.03)+13000;}

		//Stamps on Loan
		if (amountborrowed <=300000) {loanstamps=0;}
		if (amountborrowed >300000 && amountborrowed <=400000) {loanstamps=Math.ceil(amountborrowed-300000)*0.0025;}
		if (amountborrowed >400000) {loanstamps=Math.ceil(amountborrowed*0.01)*0.5;}

	purchasestamps=purchasestamps+70;
	document.stampcalc.StampsOnPurchase.value=purchasestamps.toFixed(2);

	loanstamps=loanstamps+50;
	if (amountborrowed ==0) {loanstamps=0;}	
	document.stampcalc.StampsOnLoan.value=loanstamps.toFixed(2);
	
	document.stampcalc.Disbursement.value=disbursements.toFixed(2);
	grandtotal=purchasestamps+loanstamps+disbursements;
	document.stampcalc.TotalToPay.value=grandtotal.toFixed(2);
	
	}

	if (firsttimebuyer ==2)
	{ // not first time buyer selected

		if (propertyprice <=50000) {purchasestamps=(propertyprice*0.005);}
		if (propertyprice >50000 && propertyprice<=300000) {purchasestamps=((propertyprice-50000)*0.015)+250;}
		if (propertyprice >300000 && propertyprice <= 500000) {purchasestamps=((propertyprice-300000)*0.02)+4000;}
		if (propertyprice >500000 && propertyprice <= 700000) {purchasestamps=((propertyprice-500000)*0.025)+8000;}
		if (propertyprice >700000) {purchasestamps=((propertyprice-700000)*0.03)+13000;}

		//Stamps on Loan
		loanstamps=Math.ceil(amountborrowed*0.01)*0.5;
		
	//purchasestamps=purchasestamps+58;
	purchasestamps=purchasestamps+70;
	
	document.stampcalc.StampsOnPurchase.value=purchasestamps;

	loanstamps=loanstamps+50;
	 if (amountborrowed ==0) {loanstamps=0;}

	grandtotal=purchasestamps+loanstamps+disbursements;	
	
	//purchasestamps=purchasestamps.toFixed(2);
	//loanstamps=loanstamps.toFixed(2);
	//grandtotal=grandtotal.toFixed(2);
	//disbursements=disbursements.toFixed(2);	

	document.stampcalc.StampsOnLoan.value=loanstamps.toFixed(2);
	document.stampcalc.Disbursement.value=disbursements.toFixed(2);	
	document.stampcalc.TotalToPay.value=grandtotal.toFixed(2);
	document.stampcalc.StampsOnPurchase.value=purchasestamps.toFixed(2);
	}
}


function floor(number)
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}
capitalarray = new Array(50);
function recalculate()
{
  var minterest = document.mcal.interest.value / 12 / 100;   
  var ainterest = document.mcal.interest.value / 100;   
  var mterm = document.mcal.year.value / 100;    
  //var type = document.mcal.mortgagetype.value ;
  var type = 1;
  var typeb = 1;
  var originalcapital = document.mcal.capital.value ;  
  var base = 1;
  var base2 = 1;
  var mbase = 1 + minterest;
  var ybase = 1 + minterest*12;
  var tempa=0;

  var stduty = 0;
	
  for (i=0; i<document.mcal.year.value; i++)
  {base2 = base2 * ybase;}
    
  for (i=0; i<document.mcal.year.value * 12; i++)
  {base = base * mbase;}

  	if (type == 1)
  	{    //Repayment type mortgage
   
  		if (document.mcal.intcalc.value == 0) 
  		{           // Annual interest calculations
  		var monthlypayment = ((originalcapital * minterest*12 / ( 1 - (1/base2))/12));
  		document.mcal.monthlypayment.value = floor((originalcapital * minterest*12 / ( 1 - (1/base2))/12));
  
  		}else{      // Monthly or daily interest calculations
  		var monthlypayment = (originalcapital * minterest / ( 1 - (1/base)));
  		document.mcal.monthlypayment.value = floor(originalcapital * minterest / ( 1 - (1/base)));
  		}

  	document.mcal.totalpayments.value = floor(document.mcal.monthlypayment.value * document.mcal.year.value * 12) ;

  	}else{  //Interest only type mortgage
  	var monthlypayment = (originalcapital * minterest);
  	document.mcal.monthlypayment.value = floor(originalcapital * minterest);
  	document.mcal.totalpayments.value = floor(monthlypayment * document.mcal.year.value * 12) ;
  	}
  
  
  	//For calculating arrays
  
  	if (typeb == 1) // Annual interest calculations
  	{
  	// Annual calculations for amortization table
  	var capital = originalcapital;
  		for (i=0; i<document.mcal.year.value; i++)
  		{
  		capital = capital - (12 * monthlypayment) + (capital*ainterest);
  		capitalarray[i] = capital;
  		}
  	}else{
  	// Monthly/Daily calculations for amortization table
  
  	var capital = originalcapital;

  		for (i=0; i<document.mcal.year.value; i++)
  		{
  		capital = capital - (12 * monthlypayment) + (capital*ainterest);
  		capitalarray[i] = capital;
  		}
  	}

	
}


  