Example of Using Embedded eScript for Mortgage Calculation

For compound calculations, embedded eScript is a more appropriate mechanism. The following is an example of a mortgage calculation using eScript:

var Amount= 0;
var NumPeriods= 0;
var Rate= 0;
var InterestPaid = new Array;
var Payment = new Array;
var Period = new Array;
var PrincipleBalance = new Array;
var PrinciplePaid = new Array;
var PeriodPayment = 0;
var TotalInterest = 0;
var TotalPayment = 0;

function GetInput (Inputs)
{
with(Inputs)
{
		var temp;
		temp = GetProperty("Amount");
		if (temp.length!=0)
		Amount= ToNumber(temp);
		temp = GetProperty("NumPeriods");
		if (temp.length!=0)
		NumPeriods= ToNumber(temp);
		temp = GetProperty("Rate");
		if (temp.length!=0)
		Rate= ToNumber(temp);
	}
}

function Main()
{
		Period[0] = 0;
		InterestPaid[0] = 0;
		PrinciplePaid[0] = 0;
		Payment[0] =0;
		PrincipleBalance[0] = Amount;
		Rate = Rate /1200;
		PeriodPayment = Amount / (1.00/Rate - (1.00/Rate)/Math.pow((1.00+Rate), 
		NumPeriods));
     for(var i=1; i<=NumPeriods-1; i++)
     { 
		    Period[i] = i;
		    InterestPaid[i] = PrincipleBalance[i-1]*Rate;
		    Payment[i] = PeriodPayment;
		    PrinciplePaid[i] = Payment[i]-InterestPaid[i];
		    PrincipleBalance[i] = PrincipleBalance[i-1] - PrinciplePaid[i];
     }
		TotalPayment = NumPeriods * PeriodPayment;
		TotalInterest = TotalPayment - Amount;
}

function Service_PreInvokeMethod (MethodName, Inputs, Outputs)
{
		GetInput(Inputs);
		Main();
		SetOutput(Outputs);
		return( CancelOperation );
}

		function SetOutput (Outputs)
		{
		Outputs.SetType("Result");
		var psArray = TheApplication().NewPropertySet();
		psArray.SetType("Array");
		var len = InterestPaid.length;
		for(var j=0; j<=len-1; j++)
		{
			var psArrayElement = TheApplication().NewPropertySet();
			psArrayElement.SetProperty("InterestPaid",InterestPaid[j]);
			psArrayElement.SetProperty("Payment",Payment[j]);
			psArrayElement.SetProperty("Period",Period[j]);
			psArrayElement.SetProperty("PrincipleBalance",PrincipleBalance[j]);
			psArrayElement.SetProperty("PrinciplePaid",PrinciplePaid[j]);
			psArray.AddChild(psArrayElement);
}
Outputs.AddChild(psArray);
var psPeriodPayment= TheApplication().NewPropertySet();
psPeriodPayment.SetProperty("PeriodPayment",PeriodPayment);
Outputs.AddChild(psPeriodPayment);
var psTotalInterest= TheApplication().NewPropertySet();
psTotalInterest.SetProperty("TotalInterest",TotalInterest);
Outputs.AddChild(psTotalInterest);
var psTotalPayment= TheApplication().NewPropertySet();
psTotalPayment.SetProperty("TotalPayment",TotalPayment);
Outputs.AddChild(psTotalPayment);
}