//Copyright Sam & Andy Inc.
//Description: Mortgage Calculator
//Created By: Andy Prochazka
//Created On: 4/11/2003
//Last Changed On: 4/11/2003
//Version: v1.0


// Call the args_init () function to set up the args [] array:

function floor(number)
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function MortgageCalc_RefreshForm(price, propertyTax)
{
document.MortgageCalc.ListPrice.value = price;
document.MortgageCalc.PropertyTax.value = propertyTax;
document.MortgageCalc.DownPayment.value = 0.1*price;
MortgageCalc_Calculate();
}

function MortgageCalc_UpdateDownPayment()
{
// figure out the DownPayment value
  var temp = 0;
  if (document.MortgageCalc.DownPaymentSelect.options[0].selected) temp = 5;
  if (document.MortgageCalc.DownPaymentSelect.options[1].selected) temp = 10;
  if (document.MortgageCalc.DownPaymentSelect.options[2].selected) temp = 15;
  if (document.MortgageCalc.DownPaymentSelect.options[3].selected) temp = 20;
  if (document.MortgageCalc.DownPaymentSelect.options[4].selected) temp = 25;
  if (document.MortgageCalc.DownPaymentSelect.options[5].selected) temp = 30;
  if (document.MortgageCalc.DownPaymentSelect.options[6].selected) temp = 35;
  if (document.MortgageCalc.DownPaymentSelect.options[7].selected) temp = 45;

// change downpayment value
  document.MortgageCalc.DownPayment.value = Math.round((temp * document.MortgageCalc.ListPrice.value))/100;

// update form
  MortgageCalc_Calculate();

}

function MortgageCalc_Calculate()
{

// determine insurance stuff
  var temp = document.MortgageCalc.DownPayment.value/document.MortgageCalc.ListPrice.value;
  var insuranceRate = 0;

  //Standard Insurance up to and including 25 years amortization.
  if ( temp < 0.05 )  { insuranceRate = 0.0310 } else
  if ( temp < 0.10 )  { insuranceRate = 0.0275 } else
  if ( temp < 0.15 ) { insuranceRate = 0.0200 } else
  if ( temp < 0.20 ) { insuranceRate = 0.0175 } else
  if ( temp < 0.25 ) { insuranceRate = 0 }

  // figure out the amortization period
  var tempAmortization = 0;
  if (document.MortgageCalc.Amortization.options[0].selected) tempAmortization = 5;
  if (document.MortgageCalc.Amortization.options[1].selected) tempAmortization = 10;
  if (document.MortgageCalc.Amortization.options[2].selected) tempAmortization = 15;
  if (document.MortgageCalc.Amortization.options[3].selected) tempAmortization = 20;
  if (document.MortgageCalc.Amortization.options[4].selected) tempAmortization = 25;
  if (document.MortgageCalc.Amortization.options[5].selected) tempAmortization = 30;
  if (document.MortgageCalc.Amortization.options[6].selected) tempAmortization = 35;
  if (document.MortgageCalc.Amortization.options[7].selected) tempAmortization = 40;
  if (document.MortgageCalc.Amortization.options[8].selected) tempAmortization = 45;
  if (document.MortgageCalc.Amortization.options[9].selected) tempAmortization = 50;

  //Insurance surcharge based on mortgage over 25 years.
  if ( tempAmortization > 35 && insuranceRate != 0)  { insuranceRate += 0.0060 } else
  if ( tempAmortization > 30 && insuranceRate != 0)  { insuranceRate += 0.0040 } else
  if ( tempAmortization > 25 && insuranceRate != 0)  { insuranceRate += 0.0020 }


  // get temporary values
  var temp_mortgageAmount = document.MortgageCalc.ListPrice.value - document.MortgageCalc.DownPayment.value;
  var temp_insurance = Math.round((temp_mortgageAmount * insuranceRate)*100)/100;
  var temp_TotalMortgage = temp_insurance + temp_mortgageAmount;
  var temp_PropertyTax = Math.round((document.MortgageCalc.PropertyTax.value / 12)*100)/100;
  var temp_CondoFees = Math.round((document.MortgageCalc.CondoFees.value)*100)/100;
  var temp_UtilityCosts = Math.round((document.MortgageCalc.UtilityCosts.value)*100)/100;


  var mi = document.MortgageCalc.InterestRate.value / 1200;
  var base = 1;
  var mbase = 1 + mi;
  for (i=0; i<tempAmortization * 12; i++)
  {
    base = base * mbase;
  }
  var temp_TotalMortgagePayment = floor(temp_TotalMortgage * mi / ( 1 - (1/base)))
  var temp_TotalMonthly = temp_UtilityCosts + temp_CondoFees + temp_PropertyTax + temp_TotalMortgagePayment;

  // prepare report
  document.getElementById('report_Price').innerHTML = "$ " + document.MortgageCalc.ListPrice.value;
  document.getElementById('report_MortgageAmount').innerHTML = "$ " + temp_mortgageAmount;
  document.getElementById('report_Insurance').innerHTML = "$ " + temp_insurance;
  document.getElementById('report_TotalMortgage').innerHTML = "$ " + temp_TotalMortgage;
  document.getElementById('report_InsuranceRate').innerHTML =  "Insurance Fee (" + insuranceRate*100 + "% of Mortgage)";

  document.getElementById('report_TotalMortgagePayment').innerHTML = "$ " + temp_TotalMortgagePayment;
  document.getElementById('report_PropertyTax').innerHTML = "$ " + temp_PropertyTax;
  document.getElementById('report_CondoFees').innerHTML = "$ " + temp_CondoFees;
  document.getElementById('report_UtilityCosts').innerHTML = "$ " + temp_UtilityCosts;
  document.getElementById('report_TotalMonthly').innerHTML = "$ " + temp_TotalMonthly;
}

// This function is included to overcome a bug in Netscape's implementation
// of the escape () function:

function MortgageCalc_myunescape (str)
{
	str = '' + str;
	while (true)
	{
		var i = str . indexOf ('+');
		if (i < 0)
			break;
		str = str . substring (0, i) + ' ' + str . substring (i + 1, str . length);
	}
	return unescape (str);
}



// This function creates the args [] array and populates it with data
// found in the URL's search string:

function MortgageCalc_args_init ()
{
	args = new Array ();
	var argstring = window . location . search;
	if (argstring . charAt (0) != '?')
		return;
	argstring = argstring . substring (1, argstring . length);
	var argarray = argstring . split ('&');
	var i;
	var singlearg;
	for (i = 0; i < argarray . length; ++ i)
	{
		singlearg = argarray [i] . split ('=');
		if (singlearg . length != 2)
			continue;
		var key = MortgageCalc_myunescape (singlearg [0]);
		var value = MortgageCalc_myunescape (singlearg [1]);
		args [key] = value;
	}
}

