// JavaScript for GRPatrick website





function aChange(){

  form1.a.value = formatDecimal(form1.a.value);

  form1.c.value = form1.a.value - form1.b.value;

}



function bChange(){

  form1.b.value = formatDecimal(form1.b.value);

  form1.c.value = form1.a.value - form1.b.value;



}



function cChange(){

  form1.c.value = formatDecimal(form1.c.value);

  form1.a.value = form1.c.value.valueOf() + form1.b.value.valueOf();

}



function dChange(){

  form1.d.value = formatInteger(form1.d.value);

  if(form1.d.value > 80){form1.d.value = 80;}

}



function eChange(){

  form1.e.value = formatPercentage(form1.e.value);

}



function calculateMortgage(){

  var monthlyInterest = form1.e.value / 1200

  var months = form1.d.value * 12

  var loan = form1.c.value	

	

  var temp = 1 + monthlyInterest;

  var expon = 1;	

  for(i = 0; i < months; i++){

    expon = temp * expon;

  }

  expon = 1 / expon;

  expon = 1 - expon;

  expon = monthlyInterest / expon;

	

  var monthlyPayment = loan * expon;

  var totalInterest = 0;	

  var currentInterest;

  var yearInterest;

  var workValue = loan;

  

  for(i = 0; i < form1.d.value; i++){

    yearInterest = 0;			

    for(j = 0; j < 12; j++){

      currentInterest = workValue * monthlyInterest;

      temp = monthlyPayment - currentInterest;

      workValue = workValue - temp;

      yearInterest = yearInterest + currentInterest;

    }

    totalInterest = totalInterest + yearInterest;

			

  }

  var totalRepayment

  totalRepayment = loan - 0 + totalInterest;

  averageMonthly = totalRepayment/months;

  form1.total.value = formatDecimal(averageMonthly.toString());

}





function formatDecimal(input){

  var ValidChars = "0123456789";

  var Char;

  var output = "";

  var decimal = 0;

  for(i = 0; i < input.length; i++){

    Char = input.charAt(i);

    if(decimal == 0){

      if(Char == "."){

        decimal = 1;

        output = output + Char;

      }else if(ValidChars.indexOf(Char) != -1){

        output = output + Char;

      }

    }else if(decimal > 0 && decimal < 3){

      if(ValidChars.indexOf(Char) != -1){

        output = output + Char;

        decimal ++;

      }

    }

  }

  if(decimal == 2){

    output = output + "0"

  }

  if(decimal == 1){

    output = output + "00"

  }

  return output;  

}



function formatInteger(input){

  var ValidChars = "0123456789";

  var Char;

  var output = "";

  for(i = 0; i < input.length; i++){

    Char = input.charAt(i);

    if(ValidChars.indexOf(Char) != -1){

      output = output + Char;

    }

  }

  return output;  

}



function formatPercentage(input){

  var ValidChars = "0123456789";

  var Char;

  var output = "";

  var decimal = 0;

  var unit = 0;

  for(i = 0; i < input.length; i++){

    Char = input.charAt(i);

    if(decimal == 0){

      if(Char == "."){

        decimal = 1;

        output = output + Char;

      }else if(unit < 2){

        if(ValidChars.indexOf(Char) != -1){

          output = output + Char;

          unit++;

        }

      }

    }else if(decimal > 0 && decimal < 3){

      if(ValidChars.indexOf(Char) != -1){

        output = output + Char;

        decimal ++;

      }

    }

  }

  if(unit == 0){

    output = "0" + output

  }

  if(decimal == 2){

    output = output + "0"

  }

  if(decimal == 1){

    output = output + "00"

  }

  if(decimal == 0){

    output = output + ".00"

  }

  return output;  

}