function mainCalc(){


/*Inputs */    

var houseValue=parseFloat(loseCommas(document.getElementById("housevalue").value)); /*value of house */
var mortgageStartAmt=parseFloat(loseCommas(document.getElementById("mortgagestartamount").value)); /*mortgage needed*/
var mortgageYears=parseFloat(loseCommas(document.getElementById("mortgageyears").value)); /*mortgage years*/
var mortgageMonths=parseFloat(loseCommas(document.getElementById("mortgagemonths").value)); /*mortgage months - default=0*/
var mortgageRate=parseFloat(loseCommas(document.getElementById("mortgagerate").value));/*mortgage rate */
var mortgageRepayment=parseInt(document.getElementById("mortgagerepayment").value);/*0=repayment, 1 = interest only*/
var mortgageOverpayment=parseFloat(loseCommas(document.getElementById("mortgageoverpayment").value));/*overpayment amt £*/

/*Variable tidyup*/
mortgageRate=mortgageRate/100; /* make a %*/
var mortgageRateMonth=Math.pow((1+mortgageRate),(1/12))-1;/* obtain monthly rate*/
var mortgageMonthSum=mortgageMonths+(12*mortgageYears);/* convert into months*/
var monthlyPayment=mortgageStartAmt*(mortgageRateMonth*(Math.pow((1+mortgageRateMonth),mortgageMonthSum)))/(Math.pow((1+mortgageRateMonth),mortgageMonthSum)-1);
monthlyPayment=monthlyPayment+mortgageOverpayment;
if (mortgageRepayment==1){
    monthlyPayment=mortgageStartAmt*mortgageRateMonth;
}



/*Generate Monthly Table and populate first row*/
var month=1;
var year=1;
var principal=mortgageStartAmt;
var interest=principal*mortgageRateMonth;
var payment=monthlyPayment;
var newPrincipal=principal+interest-payment;
var capitalPaid=payment-interest;
var percentageCapitalPaid=1-(interest/payment);
var ltv=newPrincipal/houseValue;

var monthlyTable=new Array();
monthlyTable [(month-1)] = new Array(8);
monthlyTable [(month-1)][0]= month;
monthlyTable [(month-1)][1]= year;
monthlyTable [(month-1)][2]= principal;
monthlyTable [(month-1)][3]= interest;
monthlyTable [(month-1)][4]= payment;
monthlyTable [(month-1)][5]= newPrincipal;
monthlyTable [(month-1)][6]= capitalPaid;
monthlyTable [(month-1)][7]= percentageCapitalPaid;
monthlyTable [(month-1)][8]= ltv;
month=month+1;

/*loop code to populate monthly table*/
for (month=2;month<=(mortgageMonthSum);month=month+1)
{
monthlyTable [(month-1)] = new Array(8);
monthlyTable [(month-1)][0]= month;
monthlyTable [(month-1)][1]= (1+Math.floor(((month-1)/12)));
monthlyTable [(month-1)][2]= monthlyTable[month-2][5];
monthlyTable [(month-1)][3]= mortgageRateMonth*monthlyTable [(month-1)][2];
monthlyTable [(month-1)][4]= payment;
monthlyTable [(month-1)][5]= monthlyTable [(month-1)][2]+monthlyTable [(month-1)][3]-monthlyTable[(month-1)][4];
monthlyTable [(month-1)][6]= monthlyTable [(month-1)][4]-monthlyTable [(month-1)][3];
monthlyTable [(month-1)][7]= 1-(monthlyTable [(month-1)][3]/monthlyTable[(month-1)][4]);
monthlyTable [(month-1)][8]= monthlyTable [(month-1)][5]/houseValue;
}

/*Code to generate summary variables*/
var totalInterest=0;
var totalPayment=0;
for (var i = 0; i < monthlyTable.length; i++){
    totalInterest=totalInterest + monthlyTable[i][3];
    totalPayment=totalPayment + monthlyTable[i][4];
}



var xa=document.getElementById('summarytable').rows[1].cells;
var xb=document.getElementById('summarytable').rows[2].cells;
var xc=document.getElementById('summarytable').rows[3].cells;
var xd=document.getElementById('summarytable').rows[4].cells;
var xe=document.getElementById('summarytable').rows[5].cells;
var xf=document.getElementById('summarytable').rows[6].cells;
var xg=document.getElementById('summarytable').rows[7].cells;
var xh=document.getElementById('summarytable').rows[8].cells;

xa[1].innerHTML=commaFormatted(houseValue.toFixed(2));
xb[1].innerHTML=commaFormatted(mortgageStartAmt.toFixed(2));
xc[1].innerHTML=commaFormatted((mortgageRate*100).toFixed(2));
xd[1].innerHTML=mortgageYears+"&nbsp;years,&nbsp;"+mortgageMonths+"&nbsp;months";
xe[1].innerHTML=commaFormatted(monthlyPayment.toFixed(2));
xf[1].innerHTML=commaFormatted(totalInterest.toFixed(2));
xg[1].innerHTML=commaFormatted(mortgageStartAmt.toFixed(2));
xh[1].innerHTML=commaFormatted(totalPayment.toFixed(2));


// Declare variables and create the header, footer, and caption.
  var oTable = document.createElement("TABLE");
  var oTHead = document.createElement("THEAD");
  var oTBody0 = document.createElement("TBODY");
  var oTBody1 = document.createElement("TBODY");
  var oTFoot = document.createElement("TFOOT");
  var oCaption = document.createElement("CAPTION");
  var oRow, oCell;
  var i, j;

  // Declare stock data that would normally be read in from a stock Web site.
  var heading = new Array();

  heading[0] = "Month";
  heading[1] = "Year";
  heading[2] = "Principal";
  heading[3] = "Interest";
  heading[4] = "Payment";
  heading[5] = "New Principal";
  heading[6] = "Capital Paid";
  heading[7] = "% Capital Paid";
  heading[8] = "% LTV";
  

  // Insert the created elements into oTable.
  oTable.appendChild(oTHead);
  oTable.appendChild(oTBody0);
  oTable.appendChild(oTBody1);
  oTable.appendChild(oTFoot);
  oTable.appendChild(oCaption);
  
  // Set the table's border width and colors.
  oTable.border=1;
  
  
  // Insert a row into the header and set its background color.
  oRow = document.createElement("TR");
  oTHead.appendChild(oRow);

  
  // Create and insert cells into the header row.
  for (i=0; i<heading.length; i++)
  {
    oCell = document.createElement("TH");
    oCell.innerHTML = heading[i];
    oRow.appendChild(oCell);
  }
  
  // Insert rows and cells into bodies.
  for (i=0; i<monthlyTable.length; i++)
  {
    var oBody = (i<2) ? oTBody0 : oTBody1;
    oRow = document.createElement("TR");
    oBody.appendChild(oRow);
    
    oCell = document.createElement("TD");
    oCell.innerHTML = monthlyTable[i][0];
    oRow.appendChild(oCell);
    oCell = document.createElement("TD");
    oCell.innerHTML = monthlyTable[i][1];
    oRow.appendChild(oCell);
    oCell = document.createElement("TD");
    oCell.innerHTML = commaFormatted(monthlyTable[i][2].toFixed(2));
    oRow.appendChild(oCell);
    oCell = document.createElement("TD");
    oCell.innerHTML = commaFormatted(monthlyTable[i][3].toFixed(2));
    oRow.appendChild(oCell);
    oCell = document.createElement("TD");
    oCell.innerHTML = commaFormatted(monthlyTable[i][4].toFixed(2));
    oRow.appendChild(oCell);
    oCell = document.createElement("TD");
    oCell.innerHTML = commaFormatted(monthlyTable[i][5].toFixed(2));
    oRow.appendChild(oCell);
    oCell = document.createElement("TD");
    oCell.innerHTML = commaFormatted(monthlyTable[i][6].toFixed(2));
    oRow.appendChild(oCell);
    oCell = document.createElement("TD");
    oCell.innerHTML = commaFormatted(((monthlyTable[i][7])*100).toFixed(2));
    oRow.appendChild(oCell);
    oCell = document.createElement("TD");
    oCell.innerHTML = commaFormatted(((monthlyTable[i][8])*100).toFixed(2));
    oRow.appendChild(oCell);
      
    
  }
  
  
  // Create and insert rows and cells into the footer row.
  oRow = document.createElement("TR");
  oTFoot.appendChild(oRow);
  oCell = document.createElement("TD");
  oRow.appendChild(oCell);

  var Parent = document.getElementById("summarytablemonthly");
  while(Parent.hasChildNodes())
    {
   Parent.removeChild(Parent.firstChild);
    }
  
  var xx=document.getElementById("summarytablemonthly");
  // Insert the table into the document tree.
  xx.appendChild(oTable);
}
