

// 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, Other1)
{
document.MortgageCalc.ListPrice.value = price;
document.MortgageCalc.PropertyTax.value = propertyTax;
document.MortgageCalc.Other1.value = Other1;
document.MortgageCalc.DownPayment.value = 0.05*price;

MortgageCalc_Calculate();
}

/* function MortgageCalc_RefreshForm(price, propertyTax, Other1, rent)
{
document.MortgageCalc.ListPrice.value = price;
document.MortgageCalc.PropertyTax.value = propertyTax;
document.MortgageCalc.Other1.value = Other1;
document.MortgageCalc.DownPayment.value = 0.05*price;
document.MortgageCalc.Rent.value = rent;
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;  

// 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;
  if ( temp <= 0.05 ) { insuranceRate = 0.0275 } else
  if ( temp <= 0.10 ) { insuranceRate = 0.0200 } else
  if ( temp <= 0.15 ) { insuranceRate = 0.0175 } else
  if ( temp <= 0.20 ) { insuranceRate = 0.0} else  
  if ( temp <= 0.25 ) { insuranceRate = 0 } else

// 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;

  // 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_Other1 = Math.round((document.MortgageCalc.Other1.value)*100)/100;
  var temp_UtilityCosts = Math.round((document.MortgageCalc.UtilityCosts.value)*100)/100;
  var temp_Other2 = Math.round((document.MortgageCalc.Other2.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_Other2 + temp_UtilityCosts + temp_Other1 + temp_PropertyTax + temp_TotalMortgagePayment;

  // prepare report
  document.getElementById('report_Price').innerHTML = "$ " + document.MortgageCalc.ListPrice.value;
  document.getElementById('report_MortgageAmount').innerHTML = "$ " + temp_mortgageAmount.toFixed(2);
  document.getElementById('report_Insurance').innerHTML = "$ " + temp_insurance.toFixed(2);
  document.getElementById('report_TotalMortgage').innerHTML = "$ " + temp_TotalMortgage.toFixed(2);
  var temp_insuranceRate = insuranceRate*100;
  document.getElementById('report_InsuranceRate').innerHTML =  "Insurance fees  (" + temp_insuranceRate.toFixed(2) + "% if you dont have 20% down):";

  document.getElementById('report_TotalMortgagePayment').innerHTML = "$ " + temp_TotalMortgagePayment.toFixed(2);
  document.getElementById('report_PropertyTax').innerHTML = "$ " + temp_PropertyTax.toFixed(2);
  document.getElementById('report_Other1').innerHTML = "$ " + temp_Other1.toFixed(2);
  document.getElementById('report_UtilityCosts').innerHTML = "$ " + temp_UtilityCosts.toFixed(2);
  document.getElementById('report_Other2').innerHTML = "$ " + temp_Other2.toFixed(2);
  document.getElementById('report_TotalMonthly').innerHTML = "$ " + temp_TotalMonthly.toFixed(2);
  
  
}

// 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;
	}
}

