/*
 * $Header: /HP/processor.js   19   2005-10-20 08:56:14+02:00   mkorthout $
 * Created on:      24-November-2003
 * Original author: Eric Ruis
 *
 * Description:
 *	The calculation processor. Uses the tables defined in the **ratesheet.txt file which is generated from the
 *	Excel spreadsheet with the actual rates.
 */

function cc_getProfile(amount, country) {
	var tableName = "profileTable";
	// get rid of white spaces surrounding the country symbol
	tableName += country;
	debugWindow.writeln("cc_getProfile: country table name " + tableName);

	// allocate the function to return the profile
	var f = new Function("amount",
		"try {" +
		"if (amount < " + tableName + "[0][0]) return 0;" +
		"for (var i = 0; i < " + tableName + ".length; i++) {" +
		"if (amount >= " + tableName + "[i][0] && amount <= " + tableName + "[i][1]) return i;" +
		"}" +
		"return " + tableName + ".length - 1;" +
		"} catch(E) { return 0; }"
		);

	debugWindow.writeln("cc_getProfile: getting profile from " + tableName + " using " + f);
	return f(amount);
}

/*
 * The cc_getAssets method returns the list of assets for the specified country from the ratesheet.
 * The method returns null if the table cannot be found
 */
function cc_getAssets(country) {
	var tableName = "assetTable" + country;
	debugWindow.writeln("cc_getAssets: country=" + country + ",var=" + tableName);
	var f = new Function(" try { return " + tableName + "; } catch(E) { debugWindow.writeln(E.message);return null; }");
	return f();
}

/*
 * The cc_getProducts method returns the list of lease products for the specified country from the ratesheet
 * The method returns null if the table cannot be found.
 */
function cc_getProducts(country) {
	var tableName = "productTable" + country;
	debugWindow.writeln("cc_getProducts: country=" + country + ",var=" + tableName);
	var f = new Function(" try { return " + tableName + "; } catch(E) { debugWindow.writeln(E.message);return null; }");
	return f();
}

/*
 * The cc_getCommissions method returns the list of additional commissions that may be added per country.
 * The method returns null if the table cannot be found.
 */
function cc_getCommissions(country) {
	var tableName = "commissionTable" + country;
	debugWindow.writeln("cc_getCommissions: country=" + country + ",var=" + tableName);
	var f = new Function(" try { return " + tableName + "; } catch(E) { debugWindow.writeln(E.message);return null; }");
	return f();
}

/*
 * The cc_getRate method selects the rate from the appropriate table. The name of the rate table used
 * is determined by the following:
 * - The transaction amount (determines the profile)
 * - The frequence (once per month, etc.) and the period (over 3 years)
 * - The country (different rates per country)
 * - The table extension. The standard rate table has no extension. All other rate tables, such as the promo
 *   rates used by IBM are required to have an extension such as 'Hardware' for the IBM hardware. For agco finance
 *   valid extensions are the lease type, e.g. 'Sabatini', etc.
 * - The pause period in months. This is the period at the beginning of a lease in which the customer does not to have
 *   pay anything yet.
 */
function cc_getRate(amount, frequency, period, country, extension) {
	amount = parseFloat(amount);
	var tableName = "rate";

	debugWindow.writeln("cc_getRate: amount=" + amount +
			  ", frequency=" + frequency +
			  ", period=" + period +
			  ", country=" + country +
			  ", extension=" + extension);

	// Sanity checks
	if (frequency != "Month" && frequency != "BiMonthly" && frequency != "Semiannual" && frequency != "Quarter" && frequency != "Year" && frequency != "3PerYear") {
		alert("Unknown frequency specified in call to cc_getRate");
		return -1;
	}
	if (country == null) {
		alert("Unknown country specified in call to cc_getRate");
		return -1;
	}

	// get the profile
	var profile = cc_getProfile(amount, country);
	debugWindow.writeln("cc_getRate: using profile " + profile + " to determine the rate");

	// Add the frequency to the table name
	tableName += frequency;
	tableName += "ProfileTable";

	// Add the country abbreviation
	tableName += country;

	// Add the extension
	if (extension == null) extension = "";
	tableName += extension;

	// Sanity check
	if (period < 0 || period > tableName.length) {
		alert("Rate not defined for specified period");
		return -1;
	}

	// Allocate a new function to return the rate
	period -= 1; // The period index is zero based while the period passed to this function is 1 based.
	var f = new Function("period", "profile", "return " + tableName + "[period][profile];");
	debugWindow.writeln("cc_getRate: using function " + f + " to get the rate");
	debugWindow.writeln("cc_getRate: arguments: period=" + period + ",profile=" + profile);
	return f(period, profile);
}

/*
 * The cc_calculate method calculates the lease payment as a function of the
 * amount due, the commission and the lease rate.
 *
 * Note that normally you should enter 0 for the commission variable since the
 * the commission added here is calculated over the investment causing all sorts
 * of VAT issues. To circumvent that problem the additional commission is now
 * stored in the form of extra rate tables: one per selectable additional commission.
 */
function cc_calculate(amount, commission, rate) {
	// show the values if the debug switch is set to true
		// set this property to true if you want to see debug messages
	debugWindow.writeln("cc_calculate: amount=" + amount +
			  ", commission=" + commission +
			  ", rate=" + rate);

	// make sure that we are using numbers for our calculation
	amount = parseFloat(amount);
	rate = parseFloat(rate);
	commission = parseFloat(commission);

	return amount * (1 + commission) * rate / 1000;
}

function calc_setup() {
	this.getRate = cc_getRate;
	this.calculate = cc_calculate;
	this.getAssets = cc_getAssets;
	return this;
}

var rateCalculator = new Object();
rateCalculator = calc_setup();