// JavaScript Document - Mortgage Calculator Functions


<!-- Date Script -->
d = new Array(
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
);
m = new Array(
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
);

today = new Date();
day = today.getDate();
year = today.getYear();

if (year < 2000)    // Y2K Fix, Isaac Powell
year = year + 1900; // http://onyx.idbsu.edu/~ipowell

//end = "th";
if (day==1 || day==21 || day==31) end="st";
if (day==2 || day==22) end="nd";
if (day==3 || day==23) end="rd";
//day+=end;


function Loan() {
var housePrice;
var IR;
var years;
var monthPay;
var month;
var timetype;
var interestRate;

//validate the incoming variables

  //Principle Loan Balance Checks

  if (document.calcform.price.value == "")
   {
      alert("Principal Loan Balance is required");
      return;
   }
  if (NumericCheck(document.calcform.price.value)=="-1")
   {
      alert("Principal Loan Balance is not numeric");
      return;
   }

  //Annual Interest Rate Checks

  if (document.calcform.interest.value == "")
   {
      alert("Annual Interest Rate is required");
      return;
   }

  ///Amortization Length in Years Checks

  if (document.calcform.time.value == "")
   {
      alert("Amortization Length is required");
      return;
   }

  //Monthly Prepayment Check

  if (NumericCheck(document.calcform.monthprepmt.value)=="-1")
   {
      alert("Monthly Principal Prepayment Amount is not numeric");
      return;
   }

  //Annual Prepayment Check

  if (NumericCheck(document.calcform.annualprepayt.value)=="-1")
   {
      alert("Annual Principal Prepayment Amount is not numeric");
      return;
   }

  //One Time Prepayment Check

  if (NumericCheck(document.calcform.oneprepay.value)=="-1")
   {
      alert("One-Time Principal Prepayment Amount is not numeric");
      return;
   }

  //One Time Prepayment Check

  if (NumericCheck(document.calcform.pymon.value)=="-1")
   {
      alert("One-Time Principal Prepayment Month is not numeric");
      return;
   }
      
  if (document.calcform.pymon.value != "" && document.calcform.oneprepay.value == "") 
   {
      alert("One-Time Principal Prepayment Month is required, since One-Time Principal Prepayment Amount has been entered");
      return;
   }

  if (document.calcform.pymon.value == "" && document.calcform.oneprepay.value !== "") 
   {
      alert("One-Time Principal Prepayment Amount is required, since One-Time Principal Prepayment Month has been entered");
      return;
   }
   


housePrice = parseFloat(document.calcform.price.value);
IR = parseFloat(document.calcform.interest.value);
years = parseFloat(document.calcform.time.value);
timetype = document.calcform.duration[document.calcform.duration.selectedIndex].value;
month = years;
interestRate = IR/1200;

if (timetype == "Yrs") 
{
    month = years * 12;
}

negmonth =- 1 * month;
var bottom = 1 - (Math.pow(interestRate+1,negmonth));
var top = interestRate;
var mid = top / bottom;
monthPay = (housePrice * mid);
document.calcform.monthPay.value = eval(monthPay);
document.calcform.month.value = month;
}

function NumericCheck(cnumber) 
{
  var oneChar = "";
  var decimalFlag = 0;
  checkZero = 0;
  for (var i = 0; i < cnumber.length; i++) 
  {
      oneChar = cnumber.charAt(i);
      if ((oneChar < "0" || oneChar > "9") && (oneChar != ",") && (oneChar != ".") && (oneChar != "$") ) 
      {
         return "-1";
      }
      if ((oneChar == "$") || (oneChar == ",")) 
      {
          cnumber = cnumber.substring(0,i) + cnumber.substring((i + 1),cnumber.length);
          i--;
      }
      if (oneChar == ".") 
      {
          if ((cnumber.length == 1) || (decimalFlag == 1)) 
          {
              return "-1";
          }
          else 
          {
              decimalFlag = 1;
          }
      }
  }
  if (cnumber == "") 
  {
     return "0";
  }
  else
    return cnumber;

}

function getFieldValue (strFieldName) {
var strFieldValue;
var objRegExp = new RegExp(strFieldName + "=([^&]+)","gi");
if (objRegExp.test(location.search))
strFieldValue = unescape(RegExp.$1);
else strFieldValue="";
return strFieldValue;
}

function Currency(money1)
{
   var money = new String(money1);
   var tempdollaramt = "";
   if (money.indexOf("e") != -1)
   {
      tempdollaramt = "$0.00"
      return tempdollaramt;
   }
   if (money.indexOf(".",[0]) == -1)
   {
      money = money + ".00";
   }
   var decimal = money.indexOf(".",[0]);
   money = money.substring(0,decimal+3);
   var dollarsonly = "";
   var negamount = "";
   if (parseInt(money) > 0) 
   {
      dollarsonly = money.substring(0, money.indexOf("."));
   }
   else
   {
      negamount = "-";
      dollarsonly = money.substring(1, money.indexOf("."));
   }
   var decimalandcents = money.substring(money.indexOf("."), money.length);
   var lenDollarsOnly = dollarsonly.length;
   if (lenDollarsOnly < 4)
   {
      tempdollaramt = "$" + money;
      return tempdollaramt;
   }
   while (lenDollarsOnly > 3) 
   {
     tempdollaramt = "," + dollarsonly.substring(lenDollarsOnly-3,lenDollarsOnly) + tempdollaramt;
     lenDollarsOnly = lenDollarsOnly - 3;
   }
   if (lenDollarsOnly > 0)
   {
      tempdollaramt = dollarsonly.substring(0, lenDollarsOnly) + tempdollaramt;
   }
   tempdollaramt = "$" + negamount + tempdollaramt + decimalandcents;
   return tempdollaramt;
}
var startmonth;
startmonth = getFieldValue("startm");
var monthofpayment = new String;
month = getFieldValue("month");
monthPay = getFieldValue("monthPay");
Houseprice = getFieldValue("price");
Ir = getFieldValue("interest");
Ir = Ir/1200;
StaryearValue = getFieldValue("startyear");
function checkprice() 
{
   var price = document.calcform.price.value;
   if(price.indexOf(",") >= 0) 
   {
      alert("Please do not put commas in the Price number")
   }
}


function checkAnnualInterest()
{
    if (NumericCheck(document.calcform.interest.value)=="-1")
   {
      alert("Annual Interest Rate is not numeric");
      return;
   }
}   

function checkAmortization()
{
  if (NumericCheck(document.calcform.time.value)=="-1")
   {
      alert("Amortization Length is not numeric");
      return;
   }
}
