function getValue(theString)
{
    var noJunk = "";
    var withDollar = "";
    var foundDecimal = 0;
    var foundAlphaChar = 0;

    theString += "";
    for (i=0; i <= theString.length; i++) {
        var thisChar = theString.substring(i, i+1);

        if (thisChar == ".") {
          foundDecimal = 1;
          noJunk = noJunk + thisChar;
        }

        if ((thisChar < "0") || (thisChar > "9")) {
          if ((thisChar != "$") && (thisChar !=".") && (thisChar != ",") && (thisChar != " ") && (thisChar !="")) foundAlphaChar = 1;
        } else {
	   withDollar = withDollar + thisChar
	   noJunk = noJunk + thisChar
	}

	if ((thisChar == "$") || (thisChar == ".") || (thisChar == ",")) {
	  withDollar = withDollar + thisChar
	}
     }

     if (foundDecimal) { return parseFloat(noJunk); }
     else if (noJunk.length > 0) { return parseInt(noJunk); }
     else return 0;
}

function formatUSCurrency(theNumber) {
  var isNegative = 0
  if (theNumber != "") {

    var workingNumber = theNumber + "" // Evaluate to a string

    if (workingNumber.charAt(0) == "-") {
      isNegative = 1;
      workingNumber = workingNumber.substring(1, workingNumber.length)
    }

    var withoutChars = ""
    for (x=0; x<=((workingNumber.length)-1); x++) {
      thisChar = workingNumber.charAt(x)
      charAsNum = parseInt(thisChar)
      if ( ((thisChar >= "0") & (thisChar <= "9")) || (thisChar == ".")  ) {
	withoutChars += workingNumber.charAt(x)
      }
    }
    workingNumber = withoutChars
    decimalPoint = workingNumber.indexOf(".")

    if (decimalPoint == -1) {
      dollarValue = workingNumber
      centsValue = "00"
      } else if (decimalPoint == 0) {
      dollarValue = "0"
      centsValue = workingNumber.substring(decimalPoint + 1, workingNumber.length)
    } else {
        dollarValue = workingNumber.substring(0, decimalPoint)
        if (decimalPoint == (workingNumber.length - 1)) {
	  centsValue = "00";
        } else {
          centsValue = getValue(workingNumber.substring(decimalPoint + 1, workingNumber.length));
          centsValue += "0";
          centsValue = centsValue.charAt(0) + centsValue.charAt(1)
        }
    }

    var theString = dollarValue;
    var totalCommas = Math.floor((theString.length - 1) / 3)

    var dollarAmt = ""
    x=dollarValue.length
    position = 0
    while (x > 0) {
	x = x - 1
        thisChar = dollarValue.charAt(x)
	rounded = Math.round(position/3)
	if ( (position/3 == rounded ) & (position != 0) ) {
           dollarAmt = "," + dollarAmt
	}
        dollarAmt = thisChar +  dollarAmt
	position = position + 1
    }

    if (isNegative) {
      theString = "$" + dollarAmt + "." + centsValue
      //theString = "($" + dollarAmt + "." + centsValue + ")"
    } else {
      theString = "$" + dollarAmt + "." + centsValue
    }
    return (theString);
  } else {
    return("$0.00");
  }
}

function formatPercent(theNumber,decimalPlaces) {
  theNumber = getValue(theNumber);
  if (theNumber < 1) { theNumber = theNumber * 100; }
  with (Math) theNumber = (round(theNumber * pow(10,decimalPlaces))) / pow(10,decimalPlaces);
  return(theNumber + "%");
}

function formatPercentpts(theNumber,decimalPlaces) {
  theNumber = getValue(theNumber);
  with (Math) theNumber = (round(theNumber * pow(10,decimalPlaces))) / pow(10,decimalPlaces);
  return(theNumber + "%");
}

function removeCents(theNumber) {
  var isNegative = 0

  if (theNumber != "") {

    var workingNumber = theNumber + "" // Evaluate to a string

    if (workingNumber.charAt(0) == "-") {
      isNegative = 1;
      workingNumber = workingNumber.substring(1, workingNumber.length)
    }

    theNumber = getValue(workingNumber)

    theNumber = Math.round(theNumber * 100) / 100;
    theNumber = theNumber + ""
    decimalPoint = theNumber.indexOf(".")

    if (decimalPoint == -1) {
      dollarValue = theNumber
    } else if (decimalPoint == 0) {
      dollarValue = 0
    } else {
      dollarValue = theNumber.substring(0, decimalPoint)
    }

    var dollarAmt = ""
    x=dollarValue.length
    position = 0
    while (x > 0) {
	x = x - 1
        thisChar = dollarValue.charAt(x)
	rounded = Math.round(position/3)
	if ( (position/3 == rounded ) & (position != 0) ) {
           dollarAmt = "," + dollarAmt
	}
        dollarAmt = thisChar +  dollarAmt
	position = position + 1
    }

    if (isNegative == 1) {
      theString = "-$" + dollarAmt
      //theString = "($" + dollarAmt + "." + centsValue + ")"
    } else {
      theString = "$" + dollarAmt
    }

    dollarValue = "$" + dollarAmt
    return(theString)
  } else {
    return("$0")
  }
}

function calculate() {

    userdata    = document.forms[0];
    price       = getValue(userdata.price.value);
    percentdown = getValue(userdata.percentdown.value);
    rate        = getValue(userdata.rate.value);
    points      = getValue(userdata.points.value);
    taxes       = getValue(userdata.taxes.value);
    insurance   = getValue(userdata.insurance.value);
    term        = getValue(userdata.term.options[userdata.term.selectedIndex].text);

    var mortgage

    results = document.forms[0].results

    if (percentdown > 100) {
            results.value  = ('Your down payment should never exceed 100%.  ')
            results.value += ('You have entered a down payment of ' + percentdown + '%.')
    } else if ( (price == 0) || (rate == 0) ) {
            results.value  = ('Sorry...Some important information is missing from your form.  ')
            results.value += ('Please fill-in all necessary fields, and click "Calculate" again.');
    } else {


	    if (percentdown < 100) {
	        downpayment = (percentdown * price)/100
	    } else {
		downpayment = percentdown
	    }

	 //// MORTGAGE AND percentdown AMOUNTS

	  ratioDown = downpayment/price * 100

	  if (term >= 25) {
	    if (ratioDown >= 20) {
		MIfactor = 0
	    } else if (ratioDown >= 15) {
		MIfactor = .39
	    } else if (ratioDown >= 10) {
		MIfactor = .52
	    } else if (ratioDown >= 5) {
		MIfactor = .78
	    } else {
		MIfactor = 1.04
	    }

	  } else if (term == 20) {

	    if (ratioDown >= 20) {
		MIfactor = 0
	    } else if (ratioDown >= 15) {
		MIfactor = .21
	    } else if (ratioDown >= 10) {
		MIfactor = .41
	    } else if (ratioDown >= 5) {
		MIfactor = .67
	    } else {
		MIfactor = .79
	    }

	  } else {

	    if (ratioDown >= 20) {
		MIfactor = 0
	    } else if (ratioDown >= 15) {
		MIfactor = .21
	    } else if (ratioDown >= 10) {
		MIfactor = .41
	    } else if (ratioDown >= 5) {
		MIfactor = .67
	    } else {
		MIfactor = .79
	    }
	  }

	//// INSURANCE RATES - NOT BEING USED

	    ////propertyInsur = 17.5
	    ////if (taxes == 0) taxes = propertyInsur

	    ////homeownersInsur = 3
	    ////if (insurance == 0) insurance = homeownersInsur

	//// TAX BRACKET

	    if (term == 15) taxBracket = 15
	    if (term == 20) taxBracket = 17
	    if (term == 25) taxBracket = 28
	    if (term == 30) taxBracket = 33

	    mortgage       = price - downpayment
	    payment        = monthlyPayment(term, rate, mortgage)

	    //maxMortgage    = payment
	    monthlyTAX     = (taxes + insurance) / 12

	    monthlyMI 	   =  (MIfactor * mortgage) / 12 / 100

	    PITI 	   = payment + monthlyTAX + monthlyMI
	    PITI           = Math.round(PITI * 100) / 100;

	    payment        = Math.round(payment * 100) / 100;
	    monthlyTAX     = Math.round(monthlyTAX * 100) / 100;
	    monthlyMI      = Math.round(monthlyMI * 100) / 100;

	 //// POINTS

	    pointsPaid = mortgage * points / 100

	 //// LOAN AMOUNT & APR

	    monthly = payment + monthlyMI
	    amountFinanced = mortgage - (mortgage * .01) - 375 - (mortgage * rate / 100 / 365 * 30)

	    loanAmount = price - downpayment
	    APR = figureLoanAPR(term, rate, loanAmount, monthly)

	 /// CLOSING COSTS

	    closingCosts = (mortgage * .01) + 850 + (mortgage / 1000 * 2.1)

	 //// PREPAIDS

	    prePaids = (price * rate / 100 / 365 * 30) + ((taxes + insurance) / 12) + (monthlyMI * 2)

	 //// RESERVES

	    if (ratioDown >= 20) {
		reserves = PITI
	    } else {
		reserves = PITI * 2
	    }

	 //// TOTAL CASH

	    totalCash = downpayment + pointsPaid + closingCosts + prePaids + reserves

	 //// FORMAT RESULTS

	    downpayment    	= formatUSCurrency(downpayment)
	    mortgage     	= formatUSCurrency(mortgage)
	    payment           	= formatUSCurrency(payment)
	    monthlyTAX          = formatUSCurrency(monthlyTAX)
	    monthlyMI		= formatUSCurrency(monthlyMI)
	    PITI            	= formatUSCurrency(PITI)
	    price            	= formatUSCurrency(price)
	    APR                 = formatPercent(APR,3)
	    closingCosts    	= formatUSCurrency(closingCosts)
	    pointsPaid    	= formatUSCurrency(pointsPaid)
	    prePaids    	= formatUSCurrency(prePaids)
	    reserves    	= formatUSCurrency(reserves)
	    totalCash    	= formatUSCurrency(totalCash)

	 //// REMOVE CENTS

	    price 	 = removeCents(price)
	    downpayment  = removeCents(downpayment)
	    mortgage 	 = removeCents(mortgage)
	    closingCosts = removeCents(closingCosts)
	    pointsPaid   = removeCents(pointsPaid)
	    prePaids     = removeCents(prePaids)
	    reserves     = removeCents(reserves)
	    totalCash    = removeCents(totalCash)

	 //// DISPLAY RESULTS

	    if (percentdown >= 5) {
               results.value += ('\r\n');
	    }

            results.value = ('Based on the information you have provided, ');
            results.value += ('you will need the following amounts to  ');
            results.value += ('purchase a home of  ');
            results.value += ( price );
            results.value += (' with a mortgage amount of  ');
            results.value += ( mortgage );
            results.value += ('. ');

            results.value += ('\r\n\r\n');

            results.value += ('Downpayment:                       ' + downpayment  + '\r\n');
            results.value += ('Discount Points:                   ' + pointsPaid  + '\r\n');
            results.value += ('Closing Costs:                     ' + closingCosts  + '\r\n');
            results.value += ('Prepaids:                          ' + prePaids  + '\r\n');
            results.value += ('Reserves:                          ' + reserves  + '\r\n');
            results.value += ('Total Cash:                        ' + totalCash  + '\r\n');

            results.value += ('\r\n\r\n');
            results.value += ('Your total monthly payment would be: ');
	    	results.value += ('\r\n');
            results.value += ('P&I Payment:                       ' + payment  + '\r\n');
            results.value += ('Monthly Tax/Ins. Escrows:          ' + monthlyTAX + '\r\n');
            results.value += ('Monthly MI:                        ' + monthlyMI  + '\r\n');
            results.value += ('Total PITI:                        ' + PITI       + '\r\n');
            results.value += ('Estimated Annual Percentage Rate:  ' + APR       + '\r\n');
    }
}

function monthlyPayment(term, rate, loanAmount) {
  if (rate > 1) { (rate = rate / 100); }
  rateMonthly = (rate / 12);

  totalPayments = (term * 12);
  if (rate == 0) {
      payment = (loanAmount / totalPayments)
  } else {
      payment = (loanAmount * rateMonthly) / (1 - Math.pow((1+rateMonthly), (-1*totalPayments)) )
  }
  return(payment)
}

function figureLoanAPR (loanYears, iRateAnnual, loanAmount, monthly) {
  loanMonths = loanYears * 12;
  if (iRateAnnual > 1) { (iRateAnnual = iRateAnnual / 100); }
  iRateMonthly = iRateAnnual / 12;

  prepaidFinanceCharges = Math.round(((.01 * loanAmount) + 375 + pointsPaid + (((loanAmount * iRateAnnual) / 365) * 30)) * 100) / 100;

  aprPrinciple = Math.round((loanAmount - prepaidFinanceCharges) * 100) / 100;
  compoundX = Math.pow( (1 + iRateMonthly) , (-1 * loanMonths) );

  loanAPR = ((monthly * (1 - (compoundX) )) / aprPrinciple) * 12 * 100;
  loanAPR = Math.round(loanAPR * 1000) / 1000;
  return(loanAPR);
}
