
/*------------------------------------------------------------------------------*\
' Function:     Calc_Repayment
' Description:  Function used to only calculate monthly repayment
' Arguments:    Loan Term, Loan Amount (Preset value) and interest rate
' Return Value: Boolean
\*------------------------------------------------------------------------------*/
	function Calc_Repayment(p_Term, p_Balance, p_Rate) {
		var Repayment = p_Balance * p_Rate / (1 - (Math.pow(1/(1 + p_Rate), p_Term)));
		return round(Repayment,2);
	}
	

/*------------------------------------------------------------------------------*\
' Function:     Calc_PV
' Description:  Function used to only calculate monthly repayment
' Arguments:    Repayment, Loan Term, Loan Amount and interest rate
' Return Value: PV (Present Value)
\*------------------------------------------------------------------------------*/
	function Calc_PV(p_Payment, p_Rate, p_Term, p_Balance) {
		var PV = (p_Payment - (Math.pow((1 + p_Rate), p_Term) * (p_Payment - (p_Balance * p_Rate)))) / p_Rate;
		return round(PV,5);
	}
	
	
/*------------------------------------------------------------------------------*\
' Function:     round
' Description:  Function used to only calculate monthly repayment
' Arguments:    numeric value and accuracy
' Return Value: rounded value
\*------------------------------------------------------------------------------*/
	function round(number,X) {
		// rounds number to X decimal places, defaults to 2
		X = (!X ? 2 : X);
		return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
	}
	
	
	
/*------------------------------------------------------------------------------*\
' Function:     irr_Calc to repalce calculation of "Math.pow(1/(1 + p_Rate), p_Term)"
' Description:  This function used to only increase the accuracy
' Arguments:    i is Loan Term iteration and r is interest rate
' Return Value: irr
\*------------------------------------------------------------------------------*/
	function irr_Calc(i, r){
		var fltPow = 1;
		fltPow = parseFloat(fltPow);
		for (var j=0; j<i; j++){
			fltPow *= 1 / (1+r);
		}
		return fltPow;
	}
	

	/*-------------------------------------------------------------------------------
	' AUTHOR		: Davy Wang 10/05/2003
	' PROCEDURE 	: CRCalculate
	' PARAMETERS	: frmCRCalc form object - All values filled.
	' DESCRIPTION	: Allow the Client Javascript to do both Validation and Calculation 
	'				  of the Repayments and Present Value at time (t: month).
	*/
	function CRCalculate(form) {
		var blnRetVal = true;
		var dblBalance;			//Total loan amount
		var intLoanTerm;		//Loan Term: 1 - 30 years
		var fltIntroRate;		//Introductory interest rate
		var dblIntroPayment;	//Monthly repayment for Introductory period	
		var intIntroPeriod;		//Introductory interest period: <= 84 months
		var fltStdRate;			//Standard interest rate
		var dblPV;				//Present/Future value
		var dblStdPayment; 		//Monthly repayment for Standard Interest period
		
		// Make sure that Loan Amount has been supplied:
		if (form.oTxtLoanAmount.value.length == 0 || 
			eval("form.oTxtLoanAmount.value < 5000") && blnRetVal){		
			alert ("You must supply a loan amount greater than or equal to $5000.");		
			form.oTxtLoanAmount.focus();
//			blnRetVal = false;
			return false;
		}	

		// Make sure that Loan Term has been supplied:
		if (form.oTxtLoanTerm.value.length == 0 || 
			eval("form.oTxtLoanTerm.value <= 1") || 
			eval("form.oTxtLoanTerm.value > 30") && blnRetVal){		
			alert ("You must supply a loan term greater than 1 and less than or equal to 30.");		
			form.oTxtLoanTerm.focus();
			return false;
		}	
		
		// Make sure that Introductory Interest Rate is valid: 
		if (form.oTxtIntroRate.value.length > 0 && 
			eval("form.oTxtIntroRate.value <= 0") || 
			eval("form.oTxtIntroRate.value > 99.99") && blnRetVal){		
			alert ("You must supply an initial rate greater than 0 and less than 100% or leave blank.");		
			form.oTxtIntroRate.focus();
			return false;
		}
		
		//We need calculate Intro Monthly Repayment if it exists:
		dblBalance = form.oTxtLoanAmount.value;
		intLoanTerm = form.oTxtLoanTerm.value * 12;
		if (form.oTxtIntroRate.value.length > 0 && blnRetVal){ 
			fltIntroRate = form.oTxtIntroRate.value / 1200;
			form.oTxtIntroPayment.value = Calc_Repayment(intLoanTerm, dblBalance, fltIntroRate);
		}

		// Make sure that Intro Period (months) has been supplied:
		if (form.oTxtIntroRate.value.length > 0 && blnRetVal){
			if (form.oTxtIntroPeriod.value.length == 0 &&
				eval("form.oTxtIntroPeriod.value <= 0") || 
				eval("form.oTxtIntroPeriod.value > 84") && blnRetVal){				
				alert ("You must supply an initial period figure greater than 0 and less than or equal to 84.");		
				form.oTxtIntroPeriod.focus();
				return false;
			}
			else{
				// Make sure that Intitial Term is not greater than Loan Term has been supplied:
				if (eval("form.oTxtIntroPeriod.value > intLoanTerm") && blnRetVal){		
					alert ("You must supply an Initial Period less than the Loan Term.");		
					form.oTxtIntroPeriod.focus();
					return false;
				}
			}
		}

		// Make sure that Standard (Revert) Variable Interest Rate has been supplied:
		if (form.oTxtStdInterest.value.length == 0 || 
			eval("form.oTxtStdInterest.value <= 0") ||
			eval("form.oTxtStdInterest.value > 99.99") && blnRetVal){		
			alert ("You must supply a standard interest rate greater than 0 and less than 100%.");		
			form.oTxtStdInterest.focus();
			return false;
		}	

		fltStdRate = form.oTxtStdInterest.value / 1200;
		if (form.oTxtIntroRate.value.length > 0 && blnRetVal){ 
		// Now work out Payment for remianing period under Std Variable Interest Rate:
			dblIntroPayment = form.oTxtIntroPayment.value;			
			intIntroPeriod = form.oTxtIntroPeriod.value;
			//Now work out standard Monthly Repayment:
			if (intIntroPeriod == intLoanTerm) {
				form.oTxtStdPayment.value = 0;
			}
			else{
				var intRemainingTerm = (intLoanTerm - intIntroPeriod);
				//Calculate Present/Future Value(PV i.e. remaining Balance) used for std monthly Repayment
				dblPV = Calc_PV(dblIntroPayment, fltIntroRate, intIntroPeriod, dblBalance);
				form.oTxtStdPayment.value = Calc_Repayment(intRemainingTerm, dblPV, fltStdRate);
			}
		}
		else {
		// Work out Payment for the whole period under one Std Interest Rate:
			form.oTxtIntroPayment.value = "";
			form.oTxtIntroPeriod.value = "";
			form.oTxtStdPayment.value = Calc_Repayment(intLoanTerm, dblBalance, fltStdRate);
		}	

		// Make sure that Upfront fee has been valid:
		if (form.oTxtUpfrontFee.value.length > 0 && blnRetVal){
			dblRatio = dblBalance / form.oTxtUpfrontFee.value;
			if (eval("dblRatio < 10") && blnRetVal){ 
				alert ("Please check the total upfront fees. The fee is over 10% of the loan amount.");		
				form.oTxtUpfrontFee.focus();
				return false;
			}
		}

		// Make sure that Upfront fee has been valid:
		if (form.oTxtUpfrontFee.value.length > 0 && blnRetVal){	//1% of $10 Million
			if (eval("form.oTxtUpfrontFee.value > 2000") && blnRetVal){ 
				alert ("Please check value entered for total upfront fees: $" + form.oTxtUpfrontFee.value);		
				form.oTxtUpfrontFee.focus();
				//return false;	//Reason for commented out: warning only - DW 8/1/2004
			}
		}
		
		// Make sure that Ongoing fee has been valid:
		if (form.oTxtOngoingFee.value.length > 0 && blnRetVal){
			if (eval("form.oTxtOngoingFee.value > 1000") && blnRetVal){ 
				alert("Please check the value entered for administration fee.");		
				form.oTxtOngoingFee.focus();
				return false;
			}
		}	
		
		// Make sure that Closing Fee has been valid:
		if (form.oTxtClosingFee.value.length > 0 && blnRetVal){
			if (eval("form.oTxtClosingFee.value > 2000") && blnRetVal){ 
				alert("Please check the value entered for closing fee.");		
				form.oTxtClosingFee.focus();
				return false;
			}
		}	
		
		form.submit();		
		return false;
	}


	function ResetTextBox(form) {
		form.oTxtLoanAmount.value = "";
		form.oTxtLoanTerm.value = "";
		form.oTxtIntroPeriod.value = "";
		form.oTxtIntroRate.value = "";
		form.oTxtStdInterest.value = "";
		form.oTxtUpfrontFee.value = "";
		form.oTxtOngoingFee.value = "";
		form.oTxtClosingFee.value = "";
		form.oTxtIntroPayment.value = "";
		form.oTxtStdPayment.value = "";
		form.oTxtCRValue.value  = "";
		form.oTxtLoanAmount.focus();
	}