very good,
however, I need to call this code from within a function, because I write values to the text fields (in javascript)
to a table cell associated with an id.
works great in jaascript, howver I'd like to do the same thing in actionscript.
I'm not sure if a "for" or "while" would do this.
here is the complete javascript code:
let me know what you think
var month = 1;
var monthly_signups = 2;
this.retensionComboBoxMC.retention_rate1 = 50;
// ////////////////////////////
// ///////////////////tool base
// ///////////////////////////
_price_per_case = 120;
// number of dollars per case of juice
_currency_symbol = "$";
// symbol for dollars
_currency_places = 2;
// so the 52 shows up in, 40.52
_existing_dist_symbol = "E*";
_new_dist_symbol = "N ";
prev_ps_cas_existing_dist = new Array(8);
// on a per month basis
prev_ps_cas_uni_dist = new Array(8);
// on a per month basis
prev_ps_cas_fsb_existing_dist = new Array(8);
// differs from prev_ps_cas_existing_dist in that it only contains the fast start bonus dists
new_ps_cas_fsb_dist = new Array(8);
prev_total_dist = new Array(8);
prev_cats_sent = new Array(8);
dist_uni_bonus = new Array(8);
dist_fsb_bonus = new Array(5);
dist_ret_rate = new Array(8);
existing_dist_ret = 0;
t_ps_cas_fsb = "";
_distributors_per_month = 2;
_retention_rate = .5;
_buyer_retention_rate = .5;
_conversion_rate = .5;
_fractioned_distributors = 0;
_month_limit = 24;
_cats_per_month_limit = 25;
_max_distributors_per_month = 10;
_total_dist = 0;
_total_cats = 0;
_monthly_signups_changed = false;
_signup_rate = .1;
_cat_response_rate = .1;
_cats_per_month = 25;
_cat_new_buyers = 0;
_cat_existing_buyers = 0;
_cat_total_buyers = 0;
_cat_retail_profit = 0;
_cat_personal_rebate_profit = 0;
_cat_fsb_profit = 0;
_cat_total_profit = 0;
_average_sale_per_cat = 120;
uni_bonus = new Array(8);
uni_bonus[0] = .01;
uni_bonus[1] = .05;
uni_bonus[2] = .05;
uni_bonus[3] = .06;
uni_bonus[4] = .06;
uni_bonus[5] = .07;
uni_bonus[6] = .07;
uni_bonus[7] = .08;
fsb_bonus = new Array(5);
fsb_bonus[0] = .2;
fsb_bonus[1] = .05;
fsb_bonus[2] = .05;
fsb_bonus[3] = .05;
fsb_bonus[4] = .1;
_month = 0;
var totalCalcs = 2;
changed = false;
debug = false;
// /////////////////////////////
// ////////////////////////////
function rollOver (whichCalc, whichImg, onOff) {
var theImg = eval("parent.c"+whichCalc+"arrow"+whichImg);
theImg.src = "images/calc_btn"+whichImg+onOff+".gif";
}
function swapCalc (whichOne) {
for (i=1; i<=totalCalcs; i++) {
var theLayer = eval("parent.all.calc"+i);
if (i == whichOne) {
theLayer.style.visibility = "visible";
} else {
theLayer.style.visibility = "hidden";
}
}
}
function init () {
// tabContents.innerHTML = t1Contents.innerHTML;
// parameter_cell.innerHTML = parent.all("t3ParamContents"

.innerHTML + parent.all("t1ParamContents"

.innerHTML;
// retRateParam.innerHTML = parent.all("t1RetRate"

.innerHTML;
// initTab('t1');
iterate(1);
}
// this will have to change as more tabs work
// parent.all['back_one_month'].disabled = true;
initAll();
// markOptionSelected(parent.all['retention_rate1'], _retention_rate);
// markOptionSelected(parent.all['cat_response_rate'], _cat_response_rate);
// markOptionSelected(parent.all['conversion_rate'], _conversion_rate);
// parent.all['monthly_signups'].value=_distributors_per_month;
// *
// * Convenience method for marking a particular option as selected in a form
//
function markOptionSelected (form, valueToBeSelected) {
formOptions = form.options;
for (var t = 0; t<formOptions.length; t++) {
formOptions[t].selected = (formOptions[t].value == valueToBeSelected) ? true : false;
}
}
// * Called from within initTab to ensure that monthly signups are carried across
//
function setMonthlySignups () {
if (!_monthly_signups_changed) {
this.monthly_signups = eval(_signup_rate*_cats_per_month);
_distributors_per_month = parent.getElementById('monthly_signups').value;
} else {
this.monthly_signups = _distributors_per_month;
_monthly_signups_changed = false;
}
}
// *
// * The entire calculation for the Catalog Multiplication Tool
//
function calculateCatProfit (month) {
// calculate total buyers
_cat_new_buyers = _cats_per_month*_cat_response_rate;
_cat_existing_buyers = 0;
var t_buyers = _cat_new_buyers;
for (var t = 1; t<month; t++) {
t_buyers = eval(t_buyers*_buyer_retention_rate);
_cat_existing_buyers = eval(_cat_existing_buyers+t_buyers);
}
_cat_total_buyers = eval(_cat_new_buyers+_cat_existing_buyers);
// calculate profits
_cat_retail_profit = eval(.3*_average_sale_per_cat*_cat_total_buyers);
_cat_personal_rebate_profit = eval(.2*.5*_cat_retail_profit);
_cat_fsb_profit = eval(_conversion_rate*_cat_total_buyers*_price_per_case*fsb_bonus[0]);
_cat_total_profit = eval(_cat_personal_rebate_profit+_cat_retail_profit+_cat_fsb_profit);
writeEverything('t2');
}
// *
// * Called by every change in month, retention rate, monthly signups
// * Manages whether we should go back, or forward in calculating (which way is faster)
//
function iterate (month) {
calculateCatProfit(month);
if (this.month>_month_limit) {
iterate(_month_limit);
return;
}
if (_distributors_per_month>_max_distributors_per_month) {
_distributors_per_month = _max_distributors_per_month;
this.monthly_signups = _max_distributors_per_month;
}
if (_cats_per_month>_cats_per_month_limit) {
_cats_per_month = _cats_per_month_limit;
this.cats_per_month = _cats_per_month_limit;
}
if (month == 0) {
return;
}
if (changed) {
_month = 0;
initAll();
clearCells(1);
for (z=_month; z<month; z++) {
increaseMonth();
iterate_back_and_forward(0);
}
// we only want to clear everything if there is something to clear
if (month<9) {
clearFromNumberDown(eval((month*1)+(1*1)));
}
// iterate_back_and_forward(0);
changed = false;
return;
}
if (month<_month) {
// we're going back
// find out if it's fastest to go back or to start over
if ((_month-month)<month) {
// iterate back
if (month<9) {
clearFromNumberDown(eval((month*1)+(1*1)));
}
for (h=_month; h>month; h--) {
decreaseMonth(parent);
iterate_back_and_forward(1);
}
if (month<9) {
clearFromNumberDown(eval((month*1)+(1*1)));
}
} else {
// start from the beginning
_month = 0;
clearFromNumberDown(eval((month*1)+(1*1)));
// clear everything
initAll();
for (z=1; z<=month; z++) {
increaseMonth();
iterate_back_and_forward(0);
}
clearFromNumberDown(eval((month*1)+(1*1)));
}
} else if (month>_month) {
for (z=_month; z<month; z++) {
increaseMonth();
iterate_back_and_forward(0);
}
// we only want to clear everything if there is something to clear
if (month<9) {
clearFromNumberDown(eval((month*1)+(1*1)));
}
} else if (month == _month) {
return;
}
}
function clearCells()
{
for (m = 1; m < 9; m++)
{
writeit('', 'dist_ret_' + m);
writeit('', 'ps_cas_fsb_' + m);
writeit('', 'ps_cas_uni_' + m);
writeit('', 'fs_bonus_' + m);
writeit('', 'uni_bonus_' + m);
writeit('', 'ps_dist_' + m);
writeit('', 'ps_cat_sent_' + m);
}
}
function iterate_back_and_forward (back) {
t_total = _month*_distributors_per_month;
var t_ret_rate = '';
var t_fsb_bonus = '';
var t_uni_bonus = '';
if (_month == 1) {
t_fsb_bonus = _distributors_per_month*_price_per_case*fsb_bonus[0];
t_ret_rate = _distributors_per_month+_new_dist_symbol;
t_ps_cas_fsb = _distributors_per_month;
new_ps_cas_fsb_dist[0] = t_ps_cas_fsb;
prev_ps_cas_fsb_existing_dist[0] = 0;
} else {
t_ret_rate = _distributors_per_month+_existing_dist_symbol+_distributors_per_month+_new_dist_symbol;
existing_dist_ret = _distributors_per_month;
prev_ps_cas_existing_dist[0] = _distributors_per_month;
t_ps_cas_fsb = _distributors_per_month*2;
new_ps_cas_fsb_dist[0] = t_ps_cas_fsb;
t_fsb_bonus = _distributors_per_month*2*_price_per_case*fsb_bonus[0];
prev_ps_cas_fsb_existing_dist[0] = t_ps_cas_fsb;
}
dist_fsb_bonus[0] = t_fsb_bonus;
writeit(formatMoney(t_fsb_bonus), 'fs_bonus_1');
writeit(t_ret_rate, 'dist_ret_1');
dist_ret_rate[0] = t_ret_rate;
writeit(formatNumber(t_ps_cas_fsb), 'ps_cas_fsb_1');
var t_ps_cas_uni = t_total-(_distributors_per_month*2);
if (t_ps_cas_uni<=0) {
writeit('--', 'ps_cas_uni_1');
t_ps_cas_uni = 0;
} else {
prev_ps_cas_uni_dist[0] = t_ps_cas_uni;
writeit(t_ps_cas_uni, 'ps_cas_uni_1');
t_uni_bonus = eval(t_ps_cas_uni*_price_per_case*uni_bonus[0]);
dist_uni_bonus[0] = t_uni_bonus;
writeit(formatMoney(t_uni_bonus), 'uni_bonus_1');
}
// for the Distributor Multiplication Tab
_total_dist = eval(t_ps_cas_fsb*1+t_ps_cas_uni*1);
prev_total_dist[0] = _total_dist;
writeit(formatNumber(_total_dist), 'ps_dist_1');
prev_cats_sent[0] = eval(unFormatNumber(_total_dist)*_cats_per_month);
writeit(formatNumber(prev_cats_sent[0]), 'ps_cat_sent_1');
for (m=2; m<=8 && _month != 1; m++) {
// calculate how many new distributors there will be
var f = (t_ps_cas_uni>0) ? parseInt(t_ps_cas_uni) : 0;
var g = (existing_dist_ret>0) ? parseInt(existing_dist_ret) : 0;
new_dist_ret = eval(f+g)*_distributors_per_month*_retention_rate;
_fractioned_distributors += eval(new_dist_ret%1);
new_dist_ret += _fractioned_distributors;
_fractioned_distributors = eval(new_dist_ret%1);
new_dist_ret = new_dist_ret-_fractioned_distributors;
if ((m == 2 && _month == 2) || new_dist_ret == 0) {
new_dist_ret = _distributors_per_month;
}
new_ps_cas_fsb_dist[m-1] = new_dist_ret;
// this is where goBack() and goForward() go
if (back) {
t_ps_cas_uni = goBack(t_ps_cas_uni, m);
} else {
t_ps_cas_uni = goForward(t_ps_cas_uni, m);
}
t_dist_ret = "";
// now write everything to the dhtml
if (debug) {
alert("existing_dist_ret="+existing_dist_ret);
}
if (existing_dist_ret>0) {
t_dist_ret = formatNumber(existing_dist_ret)+_existing_dist_symbol;
// prev_ps_cas_fsb_existing_dist[m-1] = existing_dist_ret;
}
if (new_dist_ret>0) {
t_dist_ret = t_dist_ret+' '+formatNumber(new_dist_ret)+_new_dist_symbol;
}
if (t_dist_ret != '') {
dist_ret_rate[m-1] = t_dist_ret;
writeit(t_dist_ret, 'dist_ret_'+m);
}
if (t_ps_cas_fsb>0) {
writeit(formatNumber(t_ps_cas_fsb), 'ps_cas_fsb_'+m);
_total_dist = t_ps_cas_fsb;
prev_total_dist[m-1] = _total_dist;
prev_ps_cas_fsb_existing_dist[m-1] = _total_dist;
if (m<6) {
t_fsb_bonus = eval(t_ps_cas_fsb*_price_per_case*fsb_bonus[m-1]);
dist_fsb_bonus[m-1] = t_fsb_bonus;
writeit(formatMoney(t_fsb_bonus), 'fs_bonus_'+m);
}
}
if (t_ps_cas_uni>0) {
writeit(formatNumber(t_ps_cas_uni), 'ps_cas_uni_'+m);
_total_dist = eval(_total_dist+t_ps_cas_uni);
prev_total_dist[m-1] = _total_dist;
t_uni_bonus = eval(t_ps_cas_uni*_price_per_case*uni_bonus[m-1]);
dist_uni_bonus[m-1] = t_uni_bonus;
writeit(formatMoney(t_uni_bonus), 'uni_bonus_'+m);
}
// for the Distributor Multiplication Tool
writeit(formatNumber(_total_dist), 'ps_dist_'+m);
prev_cats_sent[m-1] = unFormatNumber(_total_dist)*_cats_per_month;
writeit(formatNumber(prev_cats_sent[m-1]), 'ps_cat_sent_'+m);
// if we have an existing distributor retention of 0 and the number of
// new distributors retained is equal to the number of distributors per month
// we're done calculating
if (existing_dist_ret<=0 && (new_dist_ret == _distributors_per_month)) {
break;
}
}
// calculate all of the totals
calculateTotals();
}
// *
// * Makes sure everything is written when a particular tab is displayed
//
function writeEverything (tabId) {
if (tabId == 't1') {
// if this is the first tab, only write the number of distributors
for (var rw = 1; rw<9; rw++) {
if (prev_total_dist[rw-1]>0) {
writeit(formatNumber(prev_total_dist[rw-1]), 'ps_dist_'+rw);
writeit(formatNumber(prev_cats_sent[rw-1]), 'ps_cat_sent_'+rw);
} else {
writeit(' ', 'ps_dist_'+rw);
}
}
writeit(formatNumber(_total_dist), 'total_dist');
writeit(formatNumber(_total_cats), 'dist_total_cats');
}
if (tabId == 't2') {
writeit(formatNumber(_cat_new_buyers), 'cat_new_buyers');
writeit(formatNumber(_cat_existing_buyers), 'cat_existing_buyers');
writeit(formatNumber(_cat_total_buyers), 'cat_total_buyers');
writeit(formatMoney(_cat_retail_profit), 'cat_retail_profit');
writeit(formatMoney(_cat_personal_rebate_profit), 'cat_personal_rebate_profit');
writeit(formatMoney(_cat_fsb_profit), 'cat_fsb_profit');
writeit(formatMoney(_cat_total_profit), 'cat_total_profit');
}
if (tabId == 't3') {
// alert("We're writing everything in tab3"

;
for (var rw = 1; rw<9; rw++) {
if (dist_fsb_bonus[rw-1]>0) {
writeit(formatMoney(dist_fsb_bonus[rw-1]), 'fsb_bonus_'+rw);
}
if (dist_ret_rate[rw-1] != '') {
writeit(dist_ret_rate[rw-1], 'dist_ret_'+rw);
}
var t_total = eval(prev_ps_cas_fsb_existing_dist[rw-1]);
if (t_total>0) {
writeit(t_total, 'ps_cas_fsb_'+rw);
}
if (prev_ps_cas_uni_dist[rw-1]>0) {
writeit(prev_ps_cas_uni_dist[rw-1], 'ps_cas_uni_'+rw);
}
if (dist_uni_bonus[rw-1]>0) {
writeit(formatMoney(dist_uni_bonus[rw-1]), 'uni_bonus_'+rw);
}
}
}
}
// *
// * Formats money, according to the specified places, using commas, etc.
//
function formatMoney (money) {
// money = MoneyFormatWithCommas(money);
money = new NumberFormat(money);
money.setCommas(true);
money.setCurrency(true);
money.setCurrencyPrefix(_currency_symbol);
money.setPlaces(_currency_places);
return money.toFormatted();
}
// *
// * Formats a number using commas
//
function formatNumber (number) {
number = new NumberFormat(number);
number.setCommas(true);
number.setCurrency(true);
number.setCurrencyPrefix('');
number.setPlaces(0);
return number.toFormatted();
}
// *
// * Formats a number without commas, also rounds
//
function unFormatNumber (number) {
number = new NumberFormat(number);
number.setCommas(false);
number.setCurrency(true);
number.setCurrencyPrefix('');
number.setPlaces(0);
return number.toFormatted();
}
// *
// * Resets all necessary global variables
//
function initAll () {
// initialize everything
for (p=0; p<8; p++) {
prev_ps_cas_existing_dist[p] = 0;
prev_ps_cas_fsb_existing_dist[p] = 0;
prev_ps_cas_uni_dist[p] = 0;
dist_uni_bonus[p] = 0;
new_ps_cas_fsb_dist[p] = 0;
prev_total_dist[p] = 0;
dist_ret_rate[p] = 0;
prev_cats_sent[p] = 0;
}
for (p=0; p<5; p++) {
dist_fsb_bonus[p] = 0;
}
}
// *
// * Clears a form from a number down. Used when going back in the month, and less cells in the table are needed
//
function clearFromNumberDown (number) {
number = parseInt(number);
if (debug) {
alert("number="+number);
}
for (m = number; m < 9; m++)
{
if (debug) alert("m=" + m);
writeit('', 'dist_ret_' + m);
writeit(';', 'ps_cas_fsb_' + m);
writeit('', 'fs_bonus_' + m);
writeit('', 'ps_cas_uni_' + m);
writeit('', 'uni_bonus_' + m);
writeit('', 'ps_dist_' + m);
writeit('', 'ps_cat_sent_' + m);
}
// On the 12th month are the 8th rows of unilevel stuff filled in
// if we're less than or equal to the 11th month, we don't want to clear anything, or get a javascript error
if (debug) {
alert("number="+number);
}
if (number<=10) {
// if the number is less than or equal to 10, we can clear both the 1st and the 2nd table fields back
if (number<=9 && number>1) {
writeit('', 'ps_cas_uni_'+eval(number-1));
writeit('', 'uni_bonus_'+eval(number-1));
prev_ps_cas_uni_dist[eval(number-2)] = 0;
dist_uni_bonus[eval(number-2)] = 0;
}
if (number>2) {
writeit(' ', 'ps_cas_uni_'+eval(number-2));
writeit(' ', 'uni_bonus_'+eval(number-2));
}
if (number>2) {
prev_ps_cas_uni_dist[eval(number-3)] = 0;
dist_uni_bonus[eval(number-3)] = 0;
}
}
// from number-1 because these fields are on a 0-based index
for (p=number-1; p<8; p++) {
prev_ps_cas_existing_dist[p] = 0;
prev_ps_cas_fsb_existing_dist[p] = 0;
prev_ps_cas_uni_dist[p] = 0;
dist_uni_bonus[p] = 0;
new_ps_cas_fsb_dist[p] = 0;
prev_total_dist[p] = 0;
prev_cats_sent[p] = 0;
dist_ret_rate[p] = 0;
}
for (p=number-1; p<5; p++) {
dist_fsb_bonus[p] = 0;
}
}
// *
// * Calculates totals for the Income Scenario Modeling Tool
//
function calculateTotals () {
var fsb = 0;
var usb = 0;
var retail_profit = 48;
for (y=0; y<5; y++) {
fsb += dist_fsb_bonus[y];
}
for (y=0; y<8; y++) {
usb += dist_uni_bonus[y];
}
var grand_total = fsb+usb+retail_profit;
var paid_coral = eval(dist_uni_bonus[0]+dist_uni_bonus[1]+dist_uni_bonus[2]+dist_uni_bonus[3]);
paid_coral += eval(dist_fsb_bonus[0]+dist_fsb_bonus[1]+dist_fsb_bonus[2]+dist_fsb_bonus[3]+dist_fsb_bonus[4])+retail_profit;
var paid_jade = eval(dist_uni_bonus[4]+dist_uni_bonus[5]);
var paid_pearl = dist_uni_bonus[6];
var paid_diamond = dist_uni_bonus[7];
// calculate the total number of distributors through all generations
// and the number of catalogs sent by those dists
_total_dist = 0;
_total_cats = 0;
for (var t = 0; t<8 && t<_month; t++) {
// alert("prev_total_dist[" + t + "]=" + prev_total_dist[t]);
_total_dist = eval(_total_dist+round(prev_total_dist[t]));
// _total_cats = eval(_total_cats + prev_total_dist[t] * _cats_per_month);
}
_total_cats = _total_dist*_cats_per_month;
writeit(formatMoney(retail_profit), 'retail_profit');
writeit(formatMoney(usb), 'unilevel_commission');
writeit(formatMoney(fsb), 'fsb_total');
writeit(formatMoney(grand_total), 'total');
writeit(formatMoney(paid_coral), 'paid_coral');
writeit(formatMoney(paid_jade), 'paid_jade');
writeit(formatMoney(paid_pearl), 'paid_pearl');
writeit(formatMoney(paid_diamond), 'paid_diamond');
writeit(formatNumber(_total_dist), 'total_dist');
writeit(formatNumber(_total_cats), 'dist_total_cats');
}
function round (val) {
var factor;
var i;
// round to a certain precision
factor = 1;
for (i=0; i<this.places; i++) {
factor *= 10;
}
val *= factor;
val = Math.round(val);
val /= factor;
return (val);
}
// *
// * For when the month is increased
//
function goForward (t_ps_cas_uni, m) {
// calculate number of existing distributors
existing_dist_ret = t_ps_cas_uni;
// calculate how many distributors are in unilevel for this iteration
// get the number of existing distributors from the previous month
// added to the current number of uni_level distributors
t_ps_cas_uni = prev_ps_cas_uni_dist[m-1]+prev_ps_cas_existing_dist[m-1];
prev_ps_cas_uni_dist[m-1] = t_ps_cas_uni;
if (debug) {
alert("prev_ps_cas_uni_dist["+eval(m-1)+"]="+prev_ps_cas_uni_dist[m-1]);
}
// copy the existing_dist for the next generation
prev_ps_cas_existing_dist[m-1] = existing_dist_ret;
// calculate how many personally sponsored FSB CAS distributors there will be
var t = (existing_dist_ret>0) ? existing_dist_ret : 0;
var s = (new_dist_ret>0) ? new_dist_ret : 0;
t_ps_cas_fsb = eval((t*1)+(s*1));
// new_ps_cas_fsb_dist[m-1] = t_ps_cas_fsb;
t_dist_ret = "";
return t_ps_cas_uni;
}
// *
// * For when the month is decreased
//
function goBack (t_ps_cas_uni, m) {
// calculate number of existing distributors
if (debug) {
alert("t_ps_cas_uni="+t_ps_cas_uni);
}
if (t_ps_cas_uni>0) {
existing_dist_ret = t_ps_cas_uni;
} else {
existing_dist_ret = 0;
}
// keep prev_ps_ stuff updated
prev_ps_cas_existing_dist[m-1] = existing_dist_ret;
t_ps_cas_uni = prev_ps_cas_uni_dist[m-1]-existing_dist_ret;
// keep prev_ps_ stuff updated
prev_ps_cas_uni_dist[m-1] = t_ps_cas_uni;
// calculate how many personally sponsored FSB CAS distributors there will be
var t = (existing_dist_ret>0) ? existing_dist_ret : 0;
var s = (new_dist_ret>0) ? new_dist_ret : 0;
t_ps_cas_fsb = t_ps_cas_fsb=eval((t*1)+(s*1));
// eval(t + s);
return t_ps_cas_uni;
}
function writeit (text, id) {
if (parent.getElementById) {
x = parent.getElementById(id);
if (x != null) {
x.innerHTML = text;
}
} else if (parent.all) {
x = parent.all[id];
x.innerHTML = text;
} else if (parent.layers) {
x = parent.layers[id];
x.parent.open();
x.parent.write(text);
x.parent.close();
}
}
function increaseMonth () {
_month++;
this.month = _month;
}
function decreaseMonth () {
_month--;
this.month = _month;
} jef