var repaymentAmountChange = false;
var principalChange = false;
function calculate(formName){
  var paymentAmount;
  var principal;
  var interestRate;
  var paymentsFrequency;
  var ratePerPayment;
  var totalNumPayments;
  var term;
  var dailyInterest;
  var term;
  var months;
  var selectedtermIndex;
  var selectedMonthsIndex;
  var totalPayment;
  var interestPayment;
  var selectedMortgageType;
  var mortgageType;
  var selectedpaymentsFrequency;
  var frequency;
  
  selectedpaymentsFrequency = formName.paymentsFrequency.selectedIndex;
  paymentsFrequency = (eval(formName.paymentsFrequency.options[selectedpaymentsFrequency].value)); 
  selectedtermIndex = formName.term.selectedIndex;
  term = (eval(formName.term.options[selectedtermIndex].value)); 
  selectedMonthsIndex = formName.months.selectedIndex;
  months = (eval(formName.months.options[selectedMonthsIndex].value));
  term = term + (months/12); 
  interestRate = ((eval(formName.InterestRate.value))/100);
  principal = eval(formName.Principal.value);
  ratePerPayment = (interestRate/paymentsFrequency);
  totalNumPayments = (paymentsFrequency * term);
  compoundedAmount = principal;
  paymentAmount = formName.RepaymentAmount.value;
  selectedMortgageType = formName.mortgageType.selectedIndex;
  mortgageType = (eval(formName.mortgageType.options[selectedMortgageType].value)); 
  dailyInterest = ((Math.pow((1 + interestRate),(1/365.25)))-1)
  
  if (paymentsFrequency == 52){
    frequency = 1;
  }
  else{
    if (paymentsFrequency == 26){
      frequency = 2;
    }
    else{
      frequency = 4;
    }
  }
  // Table Interest Mortgage Calculation
  if (mortgageType=="0"){
    if(repaymentAmountChange == true){
      if (principalChange == true){
        if((principal * ratePerPayment) > paymentAmount){
          paymentAmount = ((ratePerPayment/(1-(1/((Math.pow((1+ratePerPayment),(paymentsFrequency * (25+(11/12))))))))) * principal);
          alert ("The minimum allowable repayment amount with this interest rate and principal is $" + (parseInt(paymentAmount)) + ".  This value will be entered into the Repayment Amount field for you.");
          formName.RepaymentAmount.value = "$" + (parseInt(paymentAmount));  
        }
        totalNumPayments = ((Math.log(paymentAmount/(paymentAmount-(principal*ratePerPayment))))/(Math.log(1+ratePerPayment)));
        term = (totalNumPayments / paymentsFrequency);
        term = eval(term);
        if(term > (25+(11/12))){
          alert("You cannot repay $" + (parseInt(principal)) + " in less than 26 term at " + (interestRate*100) + "% interest and a repayment amount of $" + parseInt(paymentAmount) + ".  Please try again.");
          principalChange = false;
          repaymentAmountChange = false;
          return;
        }
        else{
          if(term < 1){
            alert("With the values you have chosen the term is less than one year. Please try again.");
            principalChange = false;
            repaymentAmountChange = false;
            return;     
          }
          else{
            term = (parseInt(term)-1);
            months = ((term - parseInt(term))*12);
            months = parseInt(months);
            formName.term.options[term].selected = true;  
            formName.months.options[months].selected = true;
            totalPayment = paymentAmount * totalNumPayments;
            interestPayment = totalPayment - principal;
            principalChange = false;
            repaymentAmountChange = false;
          }
        }
      }
      else{
        principal = (paymentAmount/(ratePerPayment/(1-(1/((Math.pow((1+ratePerPayment),totalNumPayments)))))));
        formName.Principal.value = (parseInt(principal));
        totalPayment = paymentAmount * totalNumPayments;
        interestPayment = totalPayment - principal;
        repaymentAmountChange = false;
      }
    }
    else{
      paymentAmount = ((ratePerPayment/(1-(1/((Math.pow((1+ratePerPayment),totalNumPayments)))))) * principal);
      totalPayment = paymentAmount * totalNumPayments;
      interestPayment = totalPayment - principal;
      formName.RepaymentAmount.value = "$" + parseInt(paymentAmount);
    }
  }
  // Interest Only Calculation
  else{
    // Calculate Principal, Total Repayment Amount and Total Interest Repayment
    if (repaymentAmountChange == true){
      principal = ((paymentAmount*paymentsFrequency)/interestRate);
      formName.Principal.value = "$" + parseInt(principal);
      totalPayment = paymentAmount * totalNumPayments;
      interestPayment = totalPayment;
      repaymentAmountChange = false; 
    }
    // Calculate Average Repayment Amount, Total Repayment Amount and Total Interest Repayment
    else{
      paymentAmount = ((principal*interestRate)/paymentsFrequency);
      formName.RepaymentAmount.value = "$" + parseInt(paymentAmount);
      totalPayment = paymentAmount * totalNumPayments;
      interestPayment = totalPayment;
    }
  }
  formName.TotalInterestPayment.value = "$" + (parseInt(interestPayment));
  formName.TotalMortgagePayment.value = "$" + (parseInt(totalPayment));
}
function checkInput(argValue,fieldName,element) { 
  var alertText = " field.  Please enter a value in numeric digits.";
  var temp = "r";
  if (argValue.length == 0){    
    alert("You have not entered a value in the " + fieldName + alertText);
    element.value = 0;
    element.focus();
    return;
  }
  else{
   var n = 0;
   var j=0;
   var checkPoint = 0;
   while (n < argValue.length){
     if ((argValue.substring(n, n+1) >= "0") && (argValue.substring(n, n+1) <= "9")){
       temp += argValue.substring(n, n+1);
     }
     else{
       if((argValue.substring(n, n+1) == ".") && (checkPoint == 0)){
         checkPoint++;
         temp += argValue.substring(n, n+1);
       }
       else{
         j++;
       }
     }
     n++;
    }
  }
  
  if (j==n){
    alert("You have not entered any numeric values in the " + fieldName + alertText);
    element.value = 0;
    element.focus();
    return;
  }
  else{
    element.value = temp.substring(1,n+1);
    return;
  }
}
