
_month_limit = 24;

all_ps_cas_existing_dist_values = new Array(_month_limit);
all_ps_cas_uni_dist_values = new Array(_month_limit);
all_ps_cas_fsb_existing_dist_values = new Array(_month_limit);
all_ps_cas_fsb_new_dist_values = new Array(_month_limit);
all_total_dist_values = new Array(_month_limit);

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
prev_ps_cas_fsb_new_dist = new Array(8); //differs from prev_ps_cas_existing_dist in that it only contains the fast start bonus dists

curr_ps_cas_existing_dist = new Array(8);
curr_ps_cas_uni_dist = new Array(8);
curr_ps_cas_fsb_existing_dist = new Array(8);//at the end of the month, add this to curr_ps_cas_uni_existing_dist to get prev_total_dist
curr_ps_cas_fsb_new_dist = new Array(8);

curr_total_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;
_cats_per_month_limit = 25;
_max_distributors_per_month = 999;
_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;

temp = 0;
temp2 = 0;
//alert("before init() call");

function init(){
	iterate(1);
}	

initAll();


/**
 * 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) {
//		document.getElementById('monthly_signups').value = eval(_signup_rate * _cats_per_month);
//		_distributors_per_month = document.getElementById('monthly_signups').value;
//	}
//	else {
		document.getElementById("monthly_signups").value = m_round(_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');
	//writeEverything('t1');
}

/**
 * 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)
{
	//alert("iterate");
	if (month < 1) {
		_month = 0;
		month = 1;
		document.getElementById('month').value = 1;
	}

	if (_cats_per_month < 1) {
		document.getElementById('cats_per_month').value = 1;
		_cats_per_month = 1;
	}
	else {
		_cats_per_month = Math.round(_cats_per_month);
		document.getElementById('cats_per_month').value = _cats_per_month;
	}
	
	if (month > _month_limit)
	{
		iterate(_month_limit);
		return;
	}
	if (_distributors_per_month > _max_distributors_per_month) {
	    _distributors_per_month = _max_distributors_per_month;
	    document.getElementById('monthly_signups').value = _max_distributors_per_month;
	}
	if (_cats_per_month > _cats_per_month_limit) {
	    _cats_per_month = _cats_per_month_limit;
	    document.getElementById('cats_per_month').value = _cats_per_month_limit;
	}
	if (month == 0)
	{
		return;
	}
	calculateCatProfit(month);////////////////////

	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(document);
				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('&nbsp;', 'dist_ret_' + m);
		writeit('&nbsp;', 'ps_cas_fsb_' + m);
		writeit('&nbsp;', 'ps_cas_uni_' + m);
		writeit('&nbsp;', 'fs_bonus_' + m);
		writeit('&nbsp;', 'uni_bonus_' + m);
		writeit('&nbsp;', 'ps_dist_' + m);
		writeit('&nbsp;', 'ps_cat_sent_' + m);
	}
}

//blank or null number
function bnn(number)
{
	return ((number != null)? number : 0);
}

function iterate_back_and_forward(back)
{
	if (_distributors_per_month == 1) {
		x = document.getElementById('retention_rate1');
		x.disabled = true;
		x.length = 1;
		x.options[0].value = "0";
		x.options[0].text = "-------";
	}
	else {
		x = document.getElementById('retention_rate1');
		x.disabled = false;
		x.length = 7;
		x.options[0].value = _company_average_activity_rate/100.0; x.options[0].text = _average_text+"(" + _company_average_activity_rate + "%)";
		x.options[1].value = "1"; x.options[1].text = "100%";
		x.options[2].value = ".75"; x.options[2].text = "75%";
		x.options[3].value = ".5"; x.options[3].text = "50%";
		x.options[4].value = ".25"; x.options[4].text = "25%";
		x.options[5].value = ".1"; x.options[5].text = "10%";
		x.options[6].value = ".05"; x.options[6].text = "5%";
	}
	if (back) {
		//		Array t_array = new Array(8);
		if (debug) alert("We're going back!");
		for (var mi = 0; mi < 8; mi++) {
			prev_ps_cas_existing_dist[mi] = all_ps_cas_existing_dist_values[eval(_month - 2)][mi];
			prev_ps_cas_uni_dist[mi] = all_ps_cas_uni_dist_values[_month - 2][mi];
			prev_ps_cas_fsb_existing_dist[mi] = all_ps_cas_fsb_existing_dist_values[_month - 2][mi];
			prev_ps_cas_fsb_new_dist[mi] = all_ps_cas_fsb_new_dist_values[_month - 2][mi];
			prev_total_dist[mi] = all_total_dist_values[_month - 2][mi];
		}
	}
	if (_distributors_per_month < 1) {
		_distributors_per_month = 1;
		document.getElementById('monthly_signups').value = 1;
	}
	else {
		_distributors_per_month = Math.round(_distributors_per_month);
		//document.getElementById('monthly_signups').value = _distributors_per_month;
		document.getElementById('monthly_signups').value = _distributors_per_month;
		//alert(document.getElementById('monthly_signups').value);
	}

	t_total = _month * _distributors_per_month;
	var t_ret_rate = '';
	var t_fsb_bonus = '';
	var t_uni_bonus = '';

	curr_total_dist = new Array(8);
	curr_ps_cas_existing_dist = new Array(8);
	curr_ps_cas_fsb_existing_dist = new Array(8);
	curr_ps_cas_uni_dist = new Array(8);
	curr_ps_cas_fsb_new_dist = new Array(8);
	for (var i = 0; i < 8; i++) {
	    curr_total_dist[i] = 0;
	    curr_ps_cas_existing_dist[i] = 0;
	    curr_ps_cas_fsb_existing_dist[i] = 0;
	    curr_ps_cas_uni_dist[i] = 0;
	    curr_ps_cas_fsb_new_dist[i] = 0;
	}
	
	if (_month == 1) {
		t_fsb_bonus = m_round(_distributors_per_month) * _price_per_case * fsb_bonus[0];
		t_ret_rate = m_round(_distributors_per_month) + _new_dist_symbol;
		t_ps_cas_fsb = m_round(_distributors_per_month);
		//		new_ps_cas_fsb_dist[0] = t_ps_cas_fsb;//this is not used anywhere
		curr_ps_cas_fsb_existing_dist[0] = 0;
		curr_ps_cas_existing_dist[0] = 0;
		curr_ps_cas_fsb_new_dist[0] = m_round(_distributors_per_month);
	}
	else {
		t_ret_rate = m_round(_distributors_per_month) + _existing_dist_symbol + m_round(_distributors_per_month) + _new_dist_symbol;
		//existing_dist_ret = _distributors_per_month;//this is not needed anymore, for the first generation, for others, yes
		curr_ps_cas_fsb_existing_dist[0] = m_round(_distributors_per_month);
		curr_ps_cas_existing_dist[0] = m_round(_distributors_per_month);
		t_ps_cas_fsb = m_round(_distributors_per_month) * 2;//because there's 100% retention for the first generation
		curr_ps_cas_fsb_new_dist[0] = m_round(_distributors_per_month);
		//		new_ps_cas_fsb_dist[0] = t_ps_cas_fsb;//this is not used anywhere
		t_fsb_bonus = m_round(_distributors_per_month) * 2 * _price_per_case * fsb_bonus[0];
		//prev_ps_cas_fsb_existing_dist[0] = t_ps_cas_fsb;//save the total number of existing distributors for the next month
	}
	
	curr_total_dist[0] = t_total;
	_total_dist = t_total;
	//alert("curr_total_dist[0]=" + curr_total_dist[0]);
	//alert("prev_total_dist[0]=" + prev_total_dist[0]);
	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 - (m_round(_distributors_per_month) * 2);
	if (t_ps_cas_uni <= 0) {
		writeit('--', 'ps_cas_uni_1');
		t_ps_cas_uni = 0;
	}
	else {
		curr_ps_cas_uni_dist[0] = t_ps_cas_uni;//save the number of unilevel distributors for generation 1
		writeit(t_ps_cas_uni, 'ps_cas_uni_1');
		t_uni_bonus = eval(t_ps_cas_uni * _price_per_case * uni_bonus[0]);//calculate the unilevel bonus for this month, generation 1
		dist_uni_bonus[0] = t_uni_bonus;//save the unilevel bonus for this month, generation 1
		writeit(formatMoney(t_uni_bonus), 'uni_bonus_1');
	}
	//for the Distributor Multiplication Tab

	
	writeit(formatNumber(_total_dist), 'ps_dist_1');
	writeit(formatNumber(_cats_per_month), 'ps_cat_sent_0');
	prev_cats_sent[0] = eval(unFormatNumber(_total_dist) * _cats_per_month);
	writeit(formatNumber(prev_cats_sent[0]), 'ps_cat_sent_1');
//TODO: change to distributers and then contacts * distributers;
	writeit(m_round(_distributors_per_month), 'ps_dist_1');
	writeit(eval(_cats_per_month * m_round(_distributors_per_month)), 'ps_cat_sent_1');
	

	for (m = 2; m <= 8 && _month != 1; m++)
	{
		//calculate the new totals
		//alert(prev_total_dist[eval(m-2)]);
		if (debug) alert("prev_total_dist[" + m + "-2]=" + prev_total_dist[eval(m-2)]);

		//if (m == 2 || m == 3)
		//t_retention_rate = .5;
		//else
		t_retention_rate = _retention_rate;

		if (debug) alert("t_retention_rate=" + t_retention_rate);
		//t_retention_rate = _retention_rate;
		if (debug) alert("new_dist_ret=" + new_dist_ret);
		if (_distributors_per_month == 1) {
			//special case for if distributors_per_month is 1, uni_level drops off at 50%
			//existing_dist_ret = m_round(eval(prev_ps_cas_fsb_new_dist[eval(m-1)]));//m-1 because we want the previous month's new distributors, and it's a 0-based array
			//new_dist_ret = m_round(eval(prev_total_dist[eval(m-2)] * m_round(_distributors_per_month) * t_retention_rate));//m-2 because we want the generation previous to m, and it's a 0-based array
			existing_dist_ret = m_round(eval(prev_ps_cas_fsb_new_dist[eval(m-1)]));//m-1 because we want the previous month's new distributors, and it's a 0-based array
			new_dist_ret = m_round(eval(prev_total_dist[eval(m-2)] * _distributors_per_month));//m-2 because we want the generation previous to m, and it's a 0-based array
		}
		else {
			new_dist_ret = m_round(eval(prev_total_dist[eval(m-2)] * m_round(_distributors_per_month) * t_retention_rate));//m-2 because we want the generation previous to m, and it's a 0-based array
//			existing_dist_ret = curr_ps_cas_uni_dist[eval(m-2)];
			existing_dist_ret =	prev_ps_cas_fsb_new_dist[m-1];
		}
		
		if (debug) alert("existing_dist_ret=" + existing_dist_ret);
		_total_dist = eval(new_dist_ret + ((existing_dist_ret != null)? existing_dist_ret : 0));
		if (debug) alert("_total_dist=" + _total_dist);
		t_ps_cas_fsb = _total_dist;
		//add the number of previous unilevel distributors to the number of previous fsb existing distributors
		if (debug) alert("prev_ps_cas_uni_dist[" + m + "-1]=" + prev_ps_cas_uni_dist[eval(m-1)]);
		if (debug) alert("prev_ps_cas_existing_dist[" + m + "-1]=" + prev_ps_cas_existing_dist[eval(m-1)]);
		//special case for if distributors_per_month is 1, uni_level drops off at 50%
		if (_distributors_per_month == 1) {
			//prev_ps_cas_uni_dist[eval(m-1)] = m_round(eval(prev_ps_cas_uni_dist[eval(m-1)] * .5));
			t_retention_rate = .5;
			if (_month > 12 && (_month - 12 > m)) {
				t_retention_rate = .35;
			}
			//prev_ps_cas_uni_dist[eval(m-1)] = m_round(eval(prev_ps_cas_uni_dist[eval(m-1)] * t_deg_rate));
			//t_ps_cas_uni = eval(bnn(prev_ps_cas_uni_dist[eval(m-2)]) + bnn(prev_ps_cas_existing_dist[eval(m-2)]) * t_retention_rate);
			t_ps_cas_uni = eval(bnn(prev_ps_cas_uni_dist[eval(m-1)]) + bnn(prev_ps_cas_existing_dist[eval(m-1)]) * t_retention_rate);
		} else {
			t_ps_cas_uni = eval(bnn(prev_ps_cas_uni_dist[eval(m-1)]) + bnn(prev_ps_cas_existing_dist[eval(m-1)]));
		}
		if (debug) alert("t_ps_cas_uni=" + t_ps_cas_uni);
		curr_ps_cas_uni_dist[m-1] = t_ps_cas_uni;

		//now save everything for next month
		curr_ps_cas_existing_dist[m-1] = ((existing_dist_ret != null)? existing_dist_ret : 0);
//		curr_ps_cas_existing_dist[m-1] = new_dist_ret;
		curr_ps_cas_fsb_new_dist[m-1] = new_dist_ret;//m-1 because it's a 0-based array
		if (debug) alert("Setting curr_total_dist[" + m + "-1]=" + eval(_total_dist + t_ps_cas_uni));
		curr_total_dist[eval(m-1)] = eval(_total_dist + t_ps_cas_uni);//save the total number of distributors for next month

		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;
			curr_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);
			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);
			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[eval(m-1)] = unFormatNumber(_total_dist) * _cats_per_month;
		writeit(formatNumber(prev_cats_sent[eval(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 == m_round(_distributors_per_month))) {
			break;
		}
	}
	t_month = eval(_month - 1);
	all_ps_cas_fsb_new_dist_values[t_month] = new Array();
	all_ps_cas_fsb_new_dist_values[t_month] = new Array();
	all_ps_cas_existing_dist_values[t_month] = new Array();
	all_ps_cas_fsb_existing_dist_values[t_month] = new Array();
	all_total_dist_values[t_month] = new Array();
	all_ps_cas_uni_dist_values[t_month] = new Array();
	
	//total distributors is equal to the number of fsb distributors + the number of uni distributors
	for (var mx = 0; mx < 8; mx++) {
		prev_total_dist[mx] = curr_total_dist[mx];
		prev_ps_cas_existing_dist[mx] = curr_ps_cas_existing_dist[mx];
		prev_ps_cas_uni_dist[mx] = curr_ps_cas_uni_dist[mx];
		prev_ps_cas_fsb_existing_dist[mx] = curr_ps_cas_fsb_existing_dist[mx];
		prev_ps_cas_fsb_new_dist[mx] = curr_ps_cas_fsb_new_dist[mx];

		all_ps_cas_fsb_new_dist_values[t_month][mx] = curr_ps_cas_fsb_new_dist[mx];
		all_ps_cas_existing_dist_values[t_month][mx] = curr_ps_cas_existing_dist[mx];
		all_ps_cas_fsb_existing_dist_values[t_month][mx] = curr_ps_cas_fsb_existing_dist[mx];
		all_total_dist_values[t_month][mx] = curr_total_dist[mx];
		all_ps_cas_uni_dist_values[t_month][mx] = curr_ps_cas_uni_dist[mx];
	}

	//calculate all of the totals
	calculateTotals();
}

function showMonths()
{
    var t_total_dist = '';
    for (var i = 0; i < _month; i++) {
	t_total_dist += "M" + i + "  **************\n";
	for (var j = 0; j < 8; j++) {
	    t_total_dist += "G" + j + "=" + bnn(all_total_dist_values[i][j]) + "  ";
	}
	t_total_dist += "\n";
    }
    t_total_dist += "$$$$$$$$$$$$$$$$$$$$\n";
    debug_frame2(t_total_dist);
}

/**
 * Makes sure everything is written when a particular tab is displayed
 */

function writeEverything(tabId)
{
	if (debug) alert("tabId=" + tabId);
	if (tabId == 't2')
	{
		//if this is the first tab, only write the number of distributors
		for (var rw = 1; rw < 9; rw++) {
			if (prev_total_dist[eval(rw-1)] > 0) {
				writeit(formatNumber(curr_total_dist[eval(rw-1)]), 'ps_dist_' + rw);
				writeit(formatNumber(curr_total_dist[eval(rw-1)] * _cats_per_month), 'ps_cat_sent_' + rw);
			}
			else { writeit('&nbsp;', 'ps_dist_' + rw); }
		}
		writeit(formatNumber(_total_dist), 'total_dist');
		writeit(formatNumber(_total_cats), 'dist_total_cats');
	}
	else if (tabId == 't3') {
		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');
	}
	else if (tabId == 't1')
	{
		//		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()
{
	//alert("We're initing All.");
	//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;
		prev_ps_cas_fsb_new_dist[p] = 0;

		curr_ps_cas_existing_dist[p] = 0;
		curr_ps_cas_fsb_existing_dist[p] = 0;
		curr_ps_cas_uni_dist[p] = 0;
		curr_ps_cas_fsb_new_dist[p] = 0;
		curr_total_dist[p] = 0;

		dist_uni_bonus[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++)
	{
	    writeit('&nbsp;', 'dist_ret_' + m);
		writeit('&nbsp;', 'ps_cas_fsb_' + m);
		writeit('&nbsp;', 'fs_bonus_' + m);
		writeit('&nbsp;', 'ps_cas_uni_' + m);
		writeit('&nbsp;', 'uni_bonus_' + m);
		writeit('&nbsp;', 'ps_dist_' + m);
		writeit('&nbsp;', '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('&nbsp;', 'ps_cas_uni_' + eval(number - 1));
			writeit('&nbsp;', '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('&nbsp;', 'ps_cas_uni_' + eval(number - 2));
			writeit('&nbsp;', '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 = _global_retail_profit;
	
	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
	t_total_dist = 0;
	_total_cats = 0;
	//for (var t = 0; t < 8 && t < _month; t++) {
	//		t_total_dist = eval(t_total_dist + m_round(prev_total_dist[t]));
	//	}
	if (debug) alert("_month=" + _month);
	for (var i = 0; i < _month; i++) {
		for (var j = 0; j < 8; j++) {
			t_total_dist += bnn(all_total_dist_values[i][j]);
		}
	}
	_total_cats = t_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');
	if (debug) alert("t_total_dist=" + t_total_dist);
	if (debug) alert("_total_cats=" + _total_cats);
	writeit(formatNumber(t_total_dist), 'total_dist');
	writeit(formatNumber(_total_cats), 'dist_total_cats');
}
/*
function m_round(val)
{
	var factor;
	var i;

	// m_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);
}
*/
function m_round(val) {

	//	alert("val=" + val);
	if (val < 1 && val != 0) {
		return 1;
	}
	//else if (val % 1 > 0) {
	//		val = val / 1;
	//		val++;
	//		return val;
	//	}

	return Math.round(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)
{
	 //alert("layer " + id + " = " + text);
   if (document.getElementById)
   {
           x = document.getElementById(id);
		   if (x != null)
           x.innerHTML = text;
   }
   else if (document.all)
   {
           x = document.all[id];
           x.innerHTML = text;
   }
   else if (document.layers)
   {
           x = document.layers[id];
           x.document.open();
           x.document.write(text);
           x.document.close();
   }
}

function increaseMonth() {
   _month++;
   document.getElementById('month').value = _month;
}

function decreaseMonth() {
   _month--;
   document.getElementById('month').value = _month;
}
/*
function alert(message)
{
    x = document.getElementById('debug_frame');
    if (x != null)
		x.value = x.value + "\n" + message;
}
*/
function debug_frame2(message)
{
    x = document.getElementById('debug_frame2');
    if (x != null)
		x.value = x.value + "\n" + message;
}
