
<!--
/*********************************************************************/
// initializing strings for all of the FORM ELEMENTS
// the description will appear below where their default value is set
var doc_mortgage_term = document.BMForm.mortgage_term;
var doc_mortgage_payment = document.BMForm.mortgage_payment;
var doc_mortgage_interest = document.BMForm.mortgage_interest;

//  var doc_mortgage_amortization = document.BMForm.mortgage_amortization;
var doc_I10_amount = document.BMForm.I10_amount;
var doc_I10_year_month = document.BMForm.I10_year_month;

var doc_I12 = document.BMForm.I12;
var doc_months_penalty = document.BMForm.months_penalty;
var doc_dollar_penalty = document.BMForm.dollar_penalty;
var doc_lump_sum_payment = document.BMForm.lump_sum_payment;
var doc_new_mortgage_interest = document.BMForm.new_mortgage_interest;

//  var doc_new_mortgage_amortization = document.BMForm.new_mortgage_amortization;
var doc_I20_amount = document.BMForm.I20_amount;
var doc_I20_year_month = document.BMForm.I20_year_month;

var doc_new_mortgage_payment = document.BMForm.new_mortgage_payment;


var doc_mortgage_balance = document.BMForm.mortgage_balance;
var doc_I34 = document.BMForm.I34;
var doc_interest_payment = document.BMForm.interest_payment;
var doc_total_penalty = document.BMForm.total_penalty;

var doc_new_mortgage_balance = document.BMForm.new_mortgage_balance;
var doc_I46 = document.BMForm.I46;
var doc_I49 = document.BMForm.I49;
var doc_I51 = document.BMForm.I51;

var doc_I53 = document.BMForm.I53;
var doc_I54 = document.BMForm.I54;
var doc_I56 = document.BMForm.I56;
var doc_I57 = document.BMForm.I57;




// initializing the variables for the computations
var var_mortgage_term;
var var_mortgage_payment;
var var_mortgage_interest;
var var_mortgage_amortization;
var var_I10_amount;
var var_I10_year_month;
var var_I12;
var var_months_penalty;
var var_dollar_penalty;
var var_lump_sum_payment;
var var_new_mortgage_interest;
var var_new_mortgage_amortization;
var var_new_mortgage_payment;

var var_mortgage_balance;
var var_I34;
var var_interest_payment;
var var_total_penalty;

var var_new_mortgage_balance;
var var_I46;
var var_I49;
var var_I51;

var var_I53;
var var_I54;
var var_I56;
var var_I57;





//_____________________________________FUNCTION_______________________________________
// NA "CURRENT REMAINING AMORTIZATION PERIOD" from the "CURRENT OUTSTANDING BALANCE"
// this function is called from the changing of the doc_I12 field 
function updateCurrRemainAmort()
{
	
	// check  the field value to see if it is numeric
	var_I10_amount = eval(isNumeric(doc_I10_amount.value));	

	//---------outputing--------------
	// rewrite the value into the field with a decimal format
	doc_I10_amount.value = decimal(var_I10_amount);	

	//set the value to the opposite field and variables to 0
	var_I12 = "-";
	doc_I12.value = "-";
	
	alert("If, indeed, you decide to provide the Remaining Amortization Period of your mortgage, the Outstanding Balance will be set to \"-\". This is being done to facilitate the rest of the calculations. The calculator require only one OR the other.");

	// call main function
	updateBM();	
}

//_____________________________________FUNCTION_______________________________________
// NA "CURRENT OUTSTANDING BALANCE" from the "CURRENT REMAINING AMORTIZATION PERIOD"
// this function is called from the changing of the doc_I10 field 
function updateCurrOutstdBal()
{
	
	// check  the field value to see if it is numeric
	var_I12 = eval(isNumeric(doc_I12.value));	

	//---------outputing--------------
	// rewrite the value into the field with a dollar format
	doc_I12.value = cash(var_I12);	
	
	//set the value to the opposite field and variables to 0
	var_I10_amount = "-";
	doc_I10_amount.value = "-";
	alert("If, indeed, you decide to provide the Outstanding Balance of your mortgage, the Remaining Amortization Period will be set to \"-\". This is being done to facilitate the rest of the calculations. The calculator require only one OR the other.");
	// call main function
	updateBM();
}



//_____________________________________FUNCTION_______________________________________
// New Mortgage, desired Amortization Period
function setNewDesiredAmort()
{
	
	// check  the field value to see if it is numeric
	var_I20_amount = eval(isNumeric(doc_I20_amount.value));	

	//---------outputing--------------
	// rewrite the value into the field with a decimal format
	doc_I20_amount.value = decimal(var_I20_amount);		
	
	//set the value to the opposite field and variables to 0
	var_new_mortgage_payment = "-";
	doc_new_mortgage_payment.value = "-";
	alert("If, indeed, you decide to provide the desired Amortization Period of your new mortgage, the desired Monthly Payment will be set to \"-\". This is being done to facilitate the rest of the calculations. The calculator require only one OR the other. ");
	
	// call main function
	updateBM();	
}

//_____________________________________FUNCTION_______________________________________
// New Mortgage, desired Monthly Payment
function setNewDesiredPayment()
{
	
	// check  the field value to see if it is numeric
	var_new_mortgage_payment = eval(isNumeric(doc_new_mortgage_payment.value));	

	//---------outputing--------------
	// rewrite the value into the field with a dollar format
	doc_new_mortgage_payment.value = cash(var_new_mortgage_payment);	
	
	//set the value to the opposite field and variables to 0
	var_I20 = "-";
	doc_I20_amount.value = "-";
	alert("If, indeed, you decide to provide the desired Monthly Payment of your new mortgage, the desired Amortization Period will be set to \"-\". This is being done to facilitate the rest of the calculations. The calculator require only one OR the other. ");
	// call main function
	updateBM();
}



//_____________________________________FUNCTION_______________________________________
function initializeCurrent_Term_Payment_Interest()
{
	
	//Input: Information About Current Mortgage
	var_mortgage_term = eval(isNumeric(doc_mortgage_term.value));	
	var_mortgage_payment = eval(isNumeric(doc_mortgage_payment.value));					
	var_mortgage_interest = eval(isNumeric(doc_mortgage_interest.value))/100;						// percentage
	
}

//_____________________________________FUNCTION_______________________________________
function initializeNEW_Interest_Amortization()
{
	
	//Input: Information About NEW Mortgage
	var_new_mortgage_interest = eval(isNumeric(doc_new_mortgage_interest.value))/100;		// percentage	
	var_new_mortgage_payment = eval(isNumeric(doc_new_mortgage_payment.value));	
	
	
}

//_____________________________________FUNCTION_______________________________________
function initializeCurrent_mortgage_amortization()
{
	
	// setting the value of 1(months) or 12 (year = 12 months) to the variable for the CURRENT mortgage
	var_I10_amount = eval(isNumeric(doc_I10_amount.value));	
	var_I10_year_month = eval(doc_I10_year_month.options[doc_I10_year_month.selectedIndex].value);		
	var_mortgage_amortization = var_I10_amount * var_I10_year_month;		
	
	// var_I12 = eval(isNumeric(doc_I12.value));			 // Original
	
}
//_____________________________________FUNCTION_______________________________________
function initializeNEW_mortgage_amortization()
{
	
	// setting the value of 1(months) or 12 (year = 12 months) to the variable for the NEW mortgage
	var_I20_amount = eval(isNumeric(doc_I20_amount.value));	
	var_I20_year_month = eval(doc_I20_year_month.options[doc_I20_year_month.selectedIndex].value);		
	var_new_mortgage_amortization = var_I20_amount * var_I20_year_month;		
	// var_new_mortgage_amortization = eval(isNumeric(doc_new_mortgage_amortization.value));	
	
}


//_____________________________________FUNCTION_______________________________________
function initializeCurrent_Rest()
{
	// set a new value to that field
	var_I12 = eval(isNumeric(doc_I12.value));
	
	//Input: Information About New Mortgage
	var_months_penalty = eval(isNumeric(doc_months_penalty.value));
	var_dollar_penalty = eval(isNumeric(doc_dollar_penalty.value));	
	if((var_dollar_penalty != 0 && var_months_penalty != 0))
	{
		alert("If, indeed, there is a penalty in DOLLAR VALUE, then the MONTH VALUE field will be set to zero. \n\n if you want the penalty to be in MONTH VALUE, then reset the DOLLAR VALUE field to zero since this one has precedence.");
		doc_months_penalty.value = 0;
	}	
	
	var_lump_sum_payment = eval(isNumeric(doc_lump_sum_payment.value));			
	/*                               Input                               */
	/* ------------------------------------------------------------------*/
}









//_____________________________________FUNCTION_______________________________________
function updateBM()
{
	initializeCurrent_Term_Payment_Interest();
	initializeNEW_Interest_Amortization();
	initializeCurrent_mortgage_amortization();
	initializeNEW_mortgage_amortization();
	initializeCurrent_Rest();
	
	/* ------------------------------------------------------------------*/
	/*                               Output                              */
	
	// Output: Information About Current Mortgage 
	
	// _____VAR_MORTGAGE_BALANCE_____
	// Current Outstanding Mortgage Balance = IF(I10="N/A",I12,PV((1+mortgage.interest/2)^(1/6)-1,mortgage.amortization,-mortgage.payment,0,0))
	if(var_I10_amount == 0)
	{
		var_mortgage_balance = var_I12
	}else{
		var_mortgage_balance =  Math.abs(presentValue(Math.pow((1+var_mortgage_interest/2),(1/6))-1,var_mortgage_amortization,-var_mortgage_payment,0,0));
	}
			

	
	// _____VAR_I34_____
	// Current Amortization Period = IF(I12="N/A",I10,NPER((1+mortgage.interest/2)^(1/6)-1,mortgage.payment,-I12,0,0))
	if(var_I12 == 0)
	{
		var_I34 = var_mortgage_amortization;
	}else{
		var_I34 = numberPeriod(Math.pow((1+var_mortgage_interest/2),(1/6))-1,var_mortgage_payment,-var_I12,0,0);
	}
	
	// How much interest will you pay this month? = +mortgage.balance*((1+mortgage.interest/2)^(1/6)-1) 
	var_interest_payment = Math.abs(var_mortgage_balance*(Math.pow((1+var_mortgage_interest/2),(1/6))-1));
	
	// Total Dollar Amount of Penalty: =+months.penalty*interest.payment+dollar.penalty 
	var_total_penalty = Math.abs(var_months_penalty * var_interest_payment + var_dollar_penalty);
	
	// New Mortgage Balance to be Financed =+mortgage.balance+total.penalty-lump.sum.payment
	var_new_mortgage_balance = Math.abs(var_mortgage_balance+var_total_penalty-var_lump_sum_payment);
	
	
	// --------------------------------------------------------------------------------------------------
  /* alert("var_mortgage_balance = " +var_mortgage_balance + " \n var_total_penalty = " + var_total_penalty + " \n var_lump_sum_payment = " +var_lump_sum_payment);*/


	
	// Output: Information about New Mortgage
	
	// _____VAR_I46_____
	// New Monthly Payment: = =IF(I24="N/A",I26,PMT((1+new.mortgage.interest/2)^(1/6)-1,new.mortgage.amortization,-new.mortgage.balance,0,0))
	if(var_I20_amount == 0)
	{
		var_I46 = var_new_mortgage_payment;
	}else{
		var_I46 = PayMenT(Math.pow((1+var_new_mortgage_interest/2),(1/6))-1,var_new_mortgage_amortization,-var_new_mortgage_balance,0,0);
	}
	
	// Compared to the old payment, how much will you save/lose per month?  =+mortgage.payment-I46
	var_I49 = var_mortgage_payment-var_I46;
	
	// How Much Will You Save/Lose over Remaining Term? = PV((1+new.mortgage.interest/2)^(1/6)-1,mortgage.term,-(mortgage.payment-I46),0)
	var_I51 = presentValue(Math.pow((1+var_new_mortgage_interest/2),(1/6))-1,var_mortgage_term,-(var_mortgage_payment-var_I46),0,0);
	
	
	// _____VAR_I53_____
	// New Amortization Period: = =IF(I26="N/A",I24,NPER((1+new.mortgage.interest/2)^(1/6)-1,new.mortgage.payment,-new.mortgage.balance,0,0))
	if(var_new_mortgage_payment == 0)
	{
		var_I53 = var_new_mortgage_amortization;
	}else{
		var_I53 = numberPeriod(Math.pow((1+var_new_mortgage_interest/2),(1/6))-1,var_new_mortgage_payment,-var_new_mortgage_balance,0,0);
	}
	
	// (Assuming the desired monthly payments) = +I53/12
	var_I54 = var_I53/12;
	
	
	// _____VAR_I56_____
	// How Many Months of Payments will You Save/Lose? = +mortgage.amortization-I53
	// =I34-I53
	var_I56 = var_I34 - var_I53;
	
	
	var_I57 = var_I56/12; // in months ...
	
	
	/* It will take =NPER((1+E64/365)^(365/12)-1,E66,-E62)
	var_E67 = numberPeriod(Math.pow((1+var_E64/365),(365/12))-1,var_E66,-var_E62,0,0); */
	
	/*                               Output                              */
	/* ------------------------------------------------------------------*/
	
	
	
	
	updateBMfield();
	
}


//_____________________________________FUNCTION_______________________________________
function updateBMfield()
{
	
	
	/* ------------------------------------------------------------------*/
	/*                   setting values to the form fields               */
	
	//Input: Information About Current Mortgage
	
	doc_mortgage_term.value = cash(var_mortgage_term);	// Current Mortgage, Remaining Term
	doc_mortgage_payment.value = cash(var_mortgage_payment);	// Current Mortgage, Monthly Payment
	doc_mortgage_interest.value = decimal(var_mortgage_interest*100);	// Current Mortgage, (annual) Interest Rate
	//doc_I10_amount.value = decimal(var_I10_amount);	// Current Mortgage, Remaining Amortization Period
	
	//Input: Information About New Mortgage
	
	if(var_dollar_penalty != 0)
	{
		doc_months_penalty.value = 0;
	}else{
		doc_months_penalty.value = decimal(var_months_penalty);	// Months of Interest Penalty for Breaking Mortgage
	}	
	doc_dollar_penalty.value = cash(var_dollar_penalty);	// Dollar Value of Penalty to Break Mortgage
	doc_lump_sum_payment.value = cash(var_lump_sum_payment);	// Desired Lump Sum Payment to Reduce Mortgage
	doc_new_mortgage_interest.value = decimal(var_new_mortgage_interest*100);	
	
	//doc_I20_amount.value = decimal(var_I20_amount);	
	
	
	
	
	
	
	/****************************************************/
	// --- Output: Information About Current Mortgage ---
	
	
	doc_I34.value = decimal(var_I34); 				// Original	
	doc_interest_payment.value = cash(var_interest_payment);
	doc_total_penalty.value = cash(var_total_penalty);
	doc_mortgage_balance.value = cash(var_mortgage_balance); 
	doc_new_mortgage_balance.value = cash(var_new_mortgage_balance); 
	
	
	/************************************************/
	// --- Output: Information about New Mortgage ---
	
	doc_I46.value = cash(var_I46); 
	//doc_new_mortgage_payment.value = cash(var_new_mortgage_payment);  
	
	
	doc_I49.value = cash(var_I49);
	doc_I51.value = cash(var_I51);
	
	doc_I53.value = decimal(var_I53); 
	
	doc_I54.value = decimal(var_I54);
	doc_I56.value = decimal(var_I56);
	doc_I57.value = decimal(var_I57);
	
	
	/*if(var_I12 == 0)
	{
		doc_I10_amount.value = "N/A"
	}else{
		doc_I12.value = "N/A"
	}*/
	
}

//-->