/**
 * Project:     
 * File:        
 * @author:     Michael D. Wailes   mwailes@holonyx.com
 * Version:     
 * Description: 
 *
 */
// JavaScript Document
/*--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--*/
/*
* This function formats a number to Currency with two(2) decimal places */
function formatCurrency(num){
	num = num.toString().replace(/\$\\, /g, '');
	if(isNaN(num))
	num = "0";
	sign = (num ==(num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i =0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$'+ num + '.' + cents);
}
/*--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--*/




/*--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--*/
/*
* This function calcualtes the montly mortgage payment based on purchase price and interest rate. 
* In addition, it calculates the total of all payments */
function calcMortPayment(){
	//Collect the values
	var prin = document.mortgagePayment.principle.value;
	var rate = document.mortgagePayment.rate.value;
	var term = document.mortgagePayment.term.value;
	//Calculate the total number of payments
	var numPayments = term * 12;
	//If there is no interest, simple division will provide the answer
	if(rate == 0){
		payment = prin / numPayments;
		total = payment * 12 * term;
	} else {
		//If there is any interest, calculate it into the payment
		if(rate>0){
			rate = rate / 100.0;
		}
		rate /= 12;
		
		var pow = 1;
		for (var j = 0; j < numPayments; j++)
			pow = pow * (1 + rate);
		payment = (prin * pow * rate) / (pow - 1);
		total = payment * 12 * term;
	}
	//Output the payment
	document.mortgagePayment.payment.value = formatCurrency(payment);
	document.mortgagePayment.total.value = formatCurrency(total);
}
/*--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--*/	




/*--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--*/
/*
*This function clears and resets the fields in the Mortgage Payment Calculator*/	
function clearMortCalc(){
	document.mortgagePayment.principle.value = "0";
	document.mortgagePayment.rate.value = "0.00";
	document.mortgagePayment.term.value = "0";
	document.mortgagePayment.payment.value = "$0.00";
	document.mortgagePayment.total.value = "$0.00";
}
/*--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--*/




/*--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--*/
/*
* This function calculates the % down based on purchase price and amount down, or it will calculate
* the amount down based on purchase price and % down*/
function calcDownpayment(){
	//Collect the values
	var principle = parseFloat(document.mortgagePayment.principle2.value);
	var downpayment = parseFloat(document.mortgagePayment.moneyDown.value);
	var percentDown = parseFloat(document.mortgagePayment.percentDown.value);
	//This logic decides on the calculation based on the information given
	if (downpayment == 0.00 && percentDown > 0){// This calculates actual dollars down
		percentDown = percentDown /100;
		downpayment = principle * percentDown;
		percentDown = percentDown * 100;
	} else if (downpayment > 0.00 && percentDown == 0){ //This calculates the % being put down
		percentDown = (downpayment / principle) * 100;
	} else if (percentDown > 0 && downpayment > 0){
		percentDown = (downpayment / principle) * 100;
	}
	document.mortgagePayment.moneyDown.value = downpayment;
	document.mortgagePayment.percentDown.value = percentDown.toPrecision(3);
}
/*--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--*/




/*--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--*/
/*
* This function clears and resets the Down Payment Calculator */
function clearMoneyDown(){
	document.mortgagePayment.moneyDown.value = "0.00";
}
/*--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--*/




/*--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--*/
/*
* This function clears and resets the Down Payment Calculator */
function clearDownpaymentCalc(){
	document.mortgagePayment.principle2.value = "0.00";
	document.mortgagePayment.moneyDown.value = "0.00";
	document.mortgagePayment.percentDown.value = "0.0";
}
/*--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--#--*/	
	
