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 false;
   }
else
   return false;
}

/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/

function round_decimals(original_number, decimals) {
   var result1 = original_number * Math.pow(10, decimals)
   var result2 = Math.round(result1)
   var result3 = result2 / Math.pow(10, decimals)
   return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

   // Convert the number to a string
   var value_string = rounded_value.toString()
   
   // Locate the decimal point
   var decimal_location = value_string.indexOf(".")

   // Is there a decimal point?
   if (decimal_location == -1) {
	  
	  // If no, then all decimal places will be padded with 0s
	  decimal_part_length = 0
	  
	  // If decimal_places is greater than zero, tack on a decimal point
	  value_string += decimal_places > 0 ? "." : ""
   }
   else {

	  // If yes, then only the extra decimal places will be padded with 0s
	  decimal_part_length = value_string.length - decimal_location - 1
   }
   
   // Calculate the number of decimal places that need to be padded with 0s
   var pad_total = decimal_places - decimal_part_length
   
   if (pad_total > 0) {
	  
	  // Pad the string with 0s
	  for (var counter = 1; counter <= pad_total; counter++) 
			value_string += "0"
	  }
   return value_string;
}

function makeArray(n){
   this.length = n;
   for (i=1;i<=n;i++){
   this[i]=0;
   }
   return this;
}

function DoCalculation() {
var interest = 5.0;

var app1 = document.forms['mortgage_calc'].elements[1].value * 1.0;
var app2 = document.forms['mortgage_calc'].elements[2].value * 1.0;
var total = (app1 + app2)*4.5;

document.forms['mortgage_calc'].elements[0].value = round_decimals(total,2);

var pay = new makeArray(30);
pay[0] = 856.07;
pay[1] = 438.71;
pay[2] = 299.71;
pay[3] = 230.2;
pay[4] = 188.71;
pay[5] = 161.05;
pay[6] = 141.34;
pay[7] = 126.6;
pay[8] = 115.17;
pay[9] = 106.07;
pay[10] = 98.64;
pay[11] = 92.49;
pay[12] = 87.31;
pay[13] = 82.89;
pay[14] = 79.08;
pay[15] = 75.77;
pay[16] = 72.87;
pay[17] = 70.30;
pay[18] = 68.03;
pay[19] = 66.00;
pay[20] = 64.17;
pay[21] = 62.53;
pay[22] = 61.04;
pay[23] = 59.69;
pay[24] = 58.46;
pay[25] = 57.33;
pay[26] = 56.30;
pay[27] = 55.36;
pay[28] = 54.49;
pay[29] = 53.68;
pay[30] = 52.96;
pay[31] = 52.24;
pay[32] = 51.52;
pay[33] = 50.8;
pay[34] = 50.08;

var interest_only = total * (interest/100.0) / 12.0;
var term = pay[document.forms['mortgage_calc'].elements[5].value - 1]; // 25 years=54.17
var repayment = (total * term) / 10000;

if (document.forms['mortgage_calc'].elements[4].value == 1) {
document.forms['mortgage_calc'].elements[3].value = round_decimals(interest_only,2);
}
else {
document.forms['mortgage_calc'].elements[3].value = round_decimals(repayment,2);
}

return;
}

function DoCalculation2() {
      var interest = 4.25;
   
      var app1 = document.forms['mortgage_calc2'].elements[1].value * 1.0;
      var app2 = document.forms['mortgage_calc2'].elements[2].value * 1.0;
      var total = 0;
      
      if (document.forms['mortgage_calc2'].elements[2].value == 0) {
   	  total = app1 * interest;	  
      }
      else {
   	  total = (app1 + app2) * 3.5;
      }
   
      document.forms['mortgage_calc2'].elements[0].value = round_decimals(total,2);
   
      var pay = new makeArray(30);
      pay[0] = 852.64;
      pay[1] = 435.36;
      pay[2] = 296.35;
      pay[3] = 220.01;
      pay[4] = 185.30;
      pay[5] = 157.59;
      pay[6] = 137.84;
      pay[7] = 123.06;
      pay[8] = 111.59;
      pay[9] = 102.44;
      pay[10] = 94.97;
      pay[11] = 88.77;
      pay[12] = 83.54;
      pay[13] = 79.08;
      pay[14] = 75.23;
      pay[15] = 71.87;
      pay[16] = 68.93;
      pay[17] = 66.32;
      pay[18] = 64;
      pay[19] = 61.92;
      pay[20] = 60.06;
      pay[21] = 58.37;
      pay[22] = 56.84;
      pay[23] = 55.45;
      pay[24] = 54.17;
      pay[25] = 53.01;
      pay[26] = 51.94;
      pay[27] = 50.95;
      pay[28] = 50.04;
      pay[29] = 49.19;
   
      var interest_only = total * (interest/100.0) / 12.0;
      var term = pay[document.forms['mortgage_calc2'].elements[5].value - 1]; // 25 years=54.17
      var repayment = (total * term) / 10000;
   
      if (document.forms['mortgage_calc2'].elements[4].value == 1) {
      document.forms['mortgage_calc2'].elements[3].value = round_decimals(interest_only,2);
      }
      else {
      document.forms['mortgage_calc2'].elements[3].value = round_decimals(repayment,2);
      }
   
      return;
}



