//make default variables
if (!objBody) { var objBody = document.body; objBody = $(objBody); }
if (blnMakeGlobal) var stores = [];
if (!strCalcTitle) var strCalcTitle = "Income Forecast";
if (!intCompPlan) var intCompPlan = 174;
if (!intScenarioCount) var intScenarioCount = 7;
if (!intLocals) var intLocals = 0;
if (!intLicenses) var intLicenses = 0;
if (!intRx) var intRx = 0;
if (typeof(blnShowMultiple) == "undefined") var blnShowMultiple = false;
if (typeof(blnShowSelf) == "undefined") var blnShowSelf = false;
if (typeof(blnFR) == "undefined") var blnFR = false;
if (typeof(blnDDCN) == "undefined") var blnDDCN = false;
if (typeof(blnShowShopping) == "undefined") var blnShowShopping = false;
if (typeof(blnShowLocal) == "undefined") var blnShowLocal = false;
if (typeof(blnShowLicense) == "undefined") var blnShowLicense = false;
if (typeof(blnShowSweetBees) == "undefined") var blnShowSweetBees = false;
if (typeof(blnShowCoBrand) == "undefined") var blnShowCoBrand = false;
if (typeof(blnShowPrivateLabel) == "undefined") var blnShowPrivateLabel = false;
if (typeof(icURL) == "undefined") var icURL = "/_files/js/ic_storeData.asp";

//added to override any page settings. remove line below to reactivate
var blnShowSweetBees = false;

//write the html to create the objects for use later
document.write('<h1>' + strCalcTitle + '</h1>');
//document.write('<form id="scenarioBuilder" name="scenarioBuilder">');
if (blnShowMultiple) {
	document.write('<p>Click each number below for a different referral scenario.</p>');
	document.write('<p>');
	for (sc = 1; sc < intScenarioCount; sc++) document.write('<a id="sb_' + sc + '" class="sb" onclick="switchDisplay(' + sc + ');return false;">' + sc + '</a>');
	document.write('</p>');
	for (m = 1; m < intScenarioCount; m++) writeTable(m);
	}
else writeTable(2);
document.write('<p id="icLoading" class="status">Loading... please wait.</p>')
//document.write('</form>');
document.write('<h4 id="icTotals"><div class="title">Calculator Totals</div>Month Total Commission: <span id="spnCalcCashBack" class="cashback">$0.00</span><br>Annual Total Commission: <span id="spnCalcCashBackYR" class="cashback">$0.00</span><span style="display:block;font-size:x-small;font-weight:normal">Annual assumes same production for all 12 months.</span></h4>');
document.write('<p id="icFootnote"></p>');

$$(".statusSelection").each(function(el){
	new Element("option", {"text" : "Member", "value" : 0}).inject(el);
	if (intCompPlan == 174) {
		new Element("option", {"text" : "Marketing Director (MD)", "value" : 1}).inject(el);
		new Element("option", {"text" : "Fundraiser (FR)", "value" : 2}).inject(el);
		new Element("option", {"text" : "Regional Marketing Director (RMD)/Private Label (PL)", "value" : 3}).inject(el);
		if (objBody.hasClass("NMD")) new Element("option", {"text" : "National Marketing Director (NMD)", "value" : 4}).inject(el);
		}
	if (intCompPlan == 175) {
		new Element("option", {"text" : "Gateway Licensee (GL)", "value" : 1}).inject(el);
		new Element("option", {"text" : "Qualified Gateway Licensee (QGL)", "value" : 2}).inject(el);
		new Element("option", {"text" : "Fundraiser (FR)", "value" : 3}).inject(el);
		new Element("option", {"text" : "Gateway Developer (GD)", "value" : 4}).inject(el);
		new Element("option", {"text" : "Area Developer (AD)", "value" : 5}).inject(el);
		new Element("option", {"text" : "Regional Developer (RD)", "value" : 6}).inject(el);
		if (objBody.hasClass("ND")) new Element("option", {"text" : "National Developer (ND)", "value" : 7}).inject(el);
		}
	});

//get data to populate store drop down
if (blnShowShopping) {
	var strTop20Stores = "";
	new Request({
		url : icURL
		,method : 'get'
		,data : 'return=Top20String&r=' + Math.random()
		,onSuccess : function (rText, rXml) {
			new Element('p', {id:'icFootnote'}).setStyles({'font-size':'smaller','color':'gray'}).set('html', 'NOTE: 20 Most Popular stores include ' + rText.trim().split('|').erase('').join(', ') + '.').inject($('icTotals'), 'after');
			}
		}).send();
	new Request({
		 url:icURL
		,method:'get'
		,data:'cp=' + intCompPlan + '&sort=desc&' + Math.random()
		,onSuccess : function (rText, rXML) {
			var stores = eval("[" + rText + "]");
			var storeOptions = "";
			stores.each(function(store, s) {
				if (store.cash == true) var comms = ["C", store.commissions.level1, store.commissions.level2, store.commissions.level3, store.commissions.level4, store.commissions.level5, store.commissions.level6, store.commissions.level7, store.commissions.level8, store.commissions.level9, store.commissions.level10];
				else var comms = ["P", store.commissions.level1, store.commissions.level2, store.commissions.level3, store.commissions.level4, store.commissions.level5, store.commissions.level6, store.commissions.level7, store.commissions.level8, store.commissions.level9, store.commissions.level10];
				storeOptions += '<option value="' + comms.join('_') + '">' + store.name + '</option>';
				});
			if (blnShowMultiple) { for (x = 1; x < intScenarioCount; x++) { $("shop" + x).set('html', storeOptions); } }
			else $("shop2").set('html', storeOptions);
			if (objBody.hasClass("ND") || blnShowSelf == true) {
				if (blnShowMultiple) { for (x = 1; x < intScenarioCount; x++) { $("self_shop" + x).set('html', storeOptions); } }
				else $("self_shop2").set('html', storeOptions);
				}
			switchDisplay(2);
			}
		}).send();
	}
else switchDisplay(2);

function writeTable(x) {
	var blnDoND = objBody.hasClass("ND");
	var blnNMD = objBody.hasClass("NMD");
	document.write('<table border="0" id="table' + x + '" style="display:none">');
	document.write('<tr><td>You are a:</td><td><select size="1" name="pos_1_' + x + '" id="pos_1_' + x + '" class="statusSelection" onchange="calcSB(' + x + ')"></select></td></tr>');
	y = 2;
	while (y <= x) {
		document.write('<tr><td>Referral is:</td><td><select size="1" name="pos_' + y + '_' + x + '" id="pos_' + y + '_' + x + '" class="statusSelection" onchange="calcSB(' + x + ')"></select></td></tr>');
		y++;
		}
	if (blnShowShopping) {
		if (blnDoND || blnShowSelf) {
			//add "Your Own Shopping section"
			document.write('<tr><td colspan="2"><h5>Your own shopping</h5></td></tr>');
			document.write('<input type="hidden" name="self_purch' + x + '" id="self_purch' + x + '" value="1">');
			document.write('<tr><td>Spend:</td><td>$<input type="text" name="self_spend' + x + '" id="self_spend' + x + '" size="10" value="132.00" onkeyup="calcSB(' + x + ')"> each month</td></tr>');
			document.write('<tr><td>At:</td><td><select size="1" name="self_shop' + x + '" id="self_shop' + x + '" onchange="calcSB(' + x + ')"></select></td></tr>');
			document.write('<tr><td>SubTotal:</td><td id="self_shopcomm' + x + '" class="cashback">$0.00</td></tr>');
			}
		document.write('<tr><td colspan="2"><h5>Shopping activity</h5></td></tr>');
		document.write('<tr><td>Make:</td><td><input type="text" name="purch' + x + '" id="purch' + x + '" size="10" value="' + intPurchases + '" onkeyup="calcSB(' + x + ')"> purchases.<div class="callout" style="font-weight:normal;font-size:smaller">Simulate an entire organization of referral members by using a larger number of purchases above.</div></td></tr>');
		document.write('<tr><td>Spending:</td><td>$<input type="text" name="spend' + x + '" id="spend' + x + '" size="10" value="132.00" onkeyup="calcSB(' + x + ')"> each time</td></tr>');
		document.write('<tr><td>At:</td><td><select size="1" name="shop' + x + '" id="shop' + x + '" onchange="calcSB(' + x + ')"></select></td></tr>');
		document.write('<tr><td>SubTotal:</td><td id="shopcomm' + x + '" class="cashback">$0.00</td></tr>');
		if (blnDoND) {
			//add "Gateway Overrides section"
			document.write('<tr><td colspan="2"><h5>Gateway Overrides</h5></td></tr>');
			document.write('<tr><td>Paid to 1st Gateway</td><td>$<input type="text" name="over1_' + x + '" id="over1' + x + '" size="10" value="0.00" onkeyup="calcSB(' + x + ')"></td></tr>');
			document.write('<tr><td>Paid to 2nd Gateway</td><td>$<input type="text" name="over2_' + x + '" id="over2' + x + '" size="10" value="0.00" onkeyup="calcSB(' + x + ')"></td></tr>');
			document.write('<tr><td>Paid to 3rd Gateway</td><td>$<input type="text" name="over3_' + x + '" id="over3' + x + '" size="10" value="0.00" onkeyup="calcSB(' + x + ')"></td></tr>');
			document.write('<tr><td>Paid to 4th Gateway</td><td>$<input type="text" name="over4_' + x + '" id="over4' + x + '" size="10" value="0.00" onkeyup="calcSB(' + x + ')"></td></tr>');
			document.write('<tr><td>SubTotal:</td><td id="overcomm' + x + '" class="cashback">$0.00</td></tr>');
			}
		//if ($('icFootnote')) $('icFootnote').set('html', "NOTE: Top 20 Stores include " + strTop20Stores.split('|').join(', ') + ".");
		}
	if (blnShowLocal) {
		document.write('<tr><td colspan="2"><h5>Local Listings subscriptions</h5></td></tr>');
		document.write('<tr><td>Sell:</td><td><input type="text" name="localnum' + x + '" id="localnum' + x + '" onkeyup="calcSB(' + x + ')" value="' + intLocals + '" size="5">listings.</td></tr>');
		document.write('<tr><td>License type:</td><td><select size="1" name="local' + x + '" id="local' + x + '" onchange="calcSB(' + x + ')"><option></option><option>Local Listings Subscription</option></select></td></tr>');
		document.write('<tr><td>SubTotal:</td><td id="localcomm' + x + '" class="cashback">$0.00</td></tr>');
		if (blnDoND) {
			//add "Your Local Overrides section"
			document.write('<tr><td colspan="2"><h5>Gateway Overrides</h5></td></tr>');
			document.write('<tr><td>Locals in 1st Gateway</td><td><input type="text" name="local1_' + x + '" id="local1' + x + '" size="10" value="0" onkeyup="calcSB(' + x + ')"></td></tr>');
			document.write('<tr><td>Locals in 2nd Gateway</td><td><input type="text" name="local2_' + x + '" id="local2' + x + '" size="10" value="0" onkeyup="calcSB(' + x + ')"></td></tr>');
			document.write('<tr><td>Locals in 3rd Gateway</td><td><input type="text" name="local3_' + x + '" id="local3' + x + '" size="10" value="0" onkeyup="calcSB(' + x + ')"></td></tr>');
			document.write('<tr><td>Locals in 4th Gateway</td><td><input type="text" name="local4_' + x + '" id="local4' + x + '" size="10" value="0" onkeyup="calcSB(' + x + ')"></td></tr>');
			document.write('<tr><td>SubTotal:</td><td id="localover' + x + '" class="cashback">$0.00</td></tr>');
			}
		if (blnNMD) document.write('<tr><td>Overrides:</td><td id="localover' + x + '" class="cashback">$0.00</td></tr>');
		if (blnShowSweetBees && intCompPlan == 174) {
			document.write('<tr><td colspan="2"><h5>Sweet Bees Voucher purchases</h5></td></tr>');
			document.write('<tr><td>Purchases:</td><td><input type="text" name="sweetbeesnum' + x + '" id="sweetbeesnum' + x + '" onkeyup="calcSB(' + x + ')" value="' + intLocals + '" size="5">listings.</td></tr>');
			document.write('<tr><td>Voucher Cost:</td><td><select size="1" name="sweetbeesvc' + x + '" id="sweetbeesvc' + x + '" onchange="calcSB(' + x + ')"><option value="0"></option><option value="5">$5</option><option value="10">$10</option><option value="25">$25</option><option value="50">$50</option></select></td></tr>');
			document.write('<tr><td>SubTotal:</td><td id="sweetbeescomm' + x + '" class="cashback">$0.00</td></tr>');
			}
		}
	if (blnShowLicense) {
		document.write('<tr><td colspan="2"><h5>License Sales</h5></td></tr>');
		document.write('<tr><td>Sell:</td><td><input type="text" name="licnum' + x + '" id="licnum' + x + '" onkeyup="calcSB(' + x + ')" value="' + intLicenses + '" size="5">licenses.</td></tr>');
		if (intCompPlan == 174) document.write('<tr><td>License type:</td><td><select size="1" name="lictype' + x + '" id="lictype' + x + '" onchange="calcSB(' + x + ')"><option></option><option>Marketing Director (MD)</option><option>Regional Marketing Director (RMD)</option></select></td></tr>');
		if (intCompPlan == 175) document.write('<tr><td>License type:</td><td><select size="1" name="lictype' + x + '" id="lictype' + x + '" onchange="calcSB(' + x + ')"><option></option><option>Gateway Licensee</option></select></td></tr>');
		document.write('<tr><td>SubTotal:</td><td id="liccomm' + x + '" class="cashback">$0.00</td></tr>');
		if (blnDoND) {
			//add "Developer Bonuses section"
			document.write('<tr><td colspan="2"><h5>Developer Bonuses</h5></td></tr>');
			document.write('<tr><td>Your GLs Sponsor</td><td><input type="text" name="numOfDBs2_' + x + '" id="numOfDBs1' + x + '" size="10" value="0" onkeyup="calcSB(' + x + ')"> licenses</td></tr>');
			document.write('<tr><td>Your QGLs Sponsor</td><td><input type="text" name="numOfDBs3_' + x + '" id="numOfDBs2' + x + '" size="10" value="0" onkeyup="calcSB(' + x + ')"> licenses</td></tr>');
			document.write('<tr><td>Your GDs Sponsor</td><td><input type="text" name="numOfDBs4_' + x + '" id="numOfDBs3' + x + '" size="10" value="0" onkeyup="calcSB(' + x + ')"> licenses</td></tr>');
			document.write('<tr><td>Your ADs Sponsor</td><td><input type="text" name="numOfDBs5_' + x + '" id="numOfDBs4' + x + '" size="10" value="0" onkeyup="calcSB(' + x + ')"> licenses</td></tr>');
			document.write('<tr><td>Your RDs Sponsor</td><td><input type="text" name="numOfDBs6_' + x + '" id="numOfDBs5' + x + '" size="10" value="0" onkeyup="calcSB(' + x + ')"> licenses</td></tr>');
			document.write('<tr><td>SubTotal:</td><td id="dbcomm' + x + '" class="cashback">$0.00</td></tr>');
			}
		if (blnNMD) document.write('<tr><td>Month End Bonuses:</td><td id="licbonus' + x + '" class="cashback">$0.00</td></tr>');
		if (intCompPlan == 175) {
			//add "Krew Bonuses section"
			document.write('<tr><td colspan="2"><h5>Krew Bonuses</h5></td></tr>');
			document.write('<tr><td>Your GLs who sell two licenses</td><td><input type="text" name="numOfKrews_' + x + '" id="numOfKrews_' + x + '" size="10" value="0" onkeyup="calcSB(' + x + ')"></td></tr>');
			document.write('<tr><td>SubTotal:</td><td id="krewbonus' + x + '" class="cashback">$0.00</td></tr>');
			}
		}
	if (blnDDCN) {
		document.write('<tr><td colspan="2"><h5>Discount Drug Card Now! Usage*</h5></td></tr>');
		document.write('<tr><td>Fill:</td><td><input type="text" name="rx' + x + '" id="rx' + x + '" onkeyup="calcSB(' + x + ')" value="' + intRx + '" size="5">prescriptions.</td></tr>');
		document.write('<tr><td>SubTotal:</td><td id="ddcncomm' + x + '" class="cashback">$0.00</td></tr>');
		document.write('<caption align="bottom" style="text-align:left;font-size:smaller;color:#666">*Additional licensing and monthly fees aply for the DDCN Add-on license.</caption>');
		}
	if (blnShowCoBrand) {
		document.write('<tr><td colspan="2"><h5>Co-Brand Site Sales</h5></td></tr>');
		document.write('<tr><td>Sell:</td><td><input type="text" name="cb' + x + '" id="cb' + x + '" onkeyup="calcSB(' + x + ')" value="' + intRx + '" size="5">sites.</td></tr>');
		document.write('<tr><td>SubTotal:</td><td id="cbcomm' + x + '" class="cashback">$0.00</td></tr>');
		}
	if (blnShowPrivateLabel) {
		document.write('<tr><td colspan="2"><h5>Private Label Site Sales</h5></td></tr>');
		document.write('<tr><td>Sell:</td><td><input type="text" name="pl' + x + '" id="pl' + x + '" onkeyup="calcSB(' + x + ')" value="' + intRx + '" size="5">sites.</td></tr>');
		document.write('<tr><td>Type:</td><td><select name="plt' + x + '" id="plt' + x + '" onchange="calcSB(' + x + ')" value="' + intRx + '"><option value="0"></option><option value="1">www.freecashbackmall.com</option><option value="2">www.greenbackstreet.com</option><option value="3">www.saveandearn.com</option></select></td></tr>');
		document.write('<tr><td>SubTotal:</td><td id="plcomm' + x + '" class="cashback">$0.00</td></tr>');
		}
	document.write('</table>');
	}

function switchDisplay(num) {
	if ($("icLoading")) $("icLoading").setStyle("display", "none");
	window.status = window.defaultStatus;
	if (blnShowMultiple) {
		for (sbo = 1; sbo < intScenarioCount; sbo++) {
			if (blnShowLocal) $("local" + sbo).selectedIndex = 1;
			$("table" + sbo).setStyles({"display" : "none"});
			$("sb_" + sbo).setStyle("background-color", "#000");
			for (i = 1; i < intScenarioCount; i++) {
				if ($("pos_" + sbo + "_" + i)) $("pos_" + sbo + "_" + i).selectedIndex = 0;
				if ($("sell" + i)) $("sell" + i).selectedIndex = 0;
				}
			if (typeof(intDefStatus) != "undefined") {
				if (!isNaN(intDefStatus)) $("pos_1_" + sbo).selectedIndex = intDefStatus;
				}
			}
		}
	if (blnShowLocal) $("local" + num).selectedIndex = 1;
	if (blnShowLicense) $("lictype" + num).selectedIndex = 1;
	if (typeof(intDefStatus) != "undefined") {
		if (!isNaN(intDefStatus)) $("pos_1_" + num).selectedIndex = intDefStatus;
		if (num > 1) $("pos_2_" + num).selectedIndex = 0;
		}
	$("table" + num).setStyle('display', 'block');
	if ($("sb_" + num)) $("sb_" + num).setStyle('background-color', '#999');
	$("spnCalcCashBack").set('html', "$0.00");
	$("spnCalcCashBackYR").set('html', "$0.00");
	calcSB(num);
	}

function createArray(strValues) {
	newArray = strValues.split("_");
	newArray.each(function(item,indx,arr){
		if (item == "") arr[indx] = 0;
		else if (!isNaN(item)) arr[indx] = item.toFloat();
		});
	return newArray;
	}

function calcSB(i) {
	//Initialize end variables
	var curSelfShoppingCommission = 0;
	var curShoppingCommission = 0;
	var gatewayOverAmount = 0;
	var curLocalCommission = 0;
	var curLicenseCommission = 0;
	var curLocOverAmount = 0;
	var curBonusAmount = 0;
	var curOverAmount = 0;
	var curDDCNCommission = 0;
	var curKrewAmount = 0;
	var curSBCommission = 0;
	var curCBCommission = 0;
	var curPLCommission = 0;
	//SET THE LEVEL RANKS
	var aPos = new Array()
	var nFR = 0;
	var nNMD = 0;
	for (x = 1; x <= i; x++) aPos[x] = $("pos_" + x + "_" + i).selectedIndex;

	//THIS HANDLES THE SHOPPING
	var curShoppingCommission = 0;
	var curSelfShoppingCommission = 0;
	var gatewayOverAmount = 0;
	if (blnShowShopping) {
		if (objBody.hasClass("ND") || blnShowSelf == true) {
			//do self shopping for nd and fr calculators
			purchNum = ($("self_purch" + i).value).toInt();
			spendAmt = ($("self_spend" + i).value).toFloat();
			if (isNaN(purchNum)) purchNum = 0;
			if (isNaN(spendAmt)) spendAmt = 0;
			strStore = $("self_shop" + i).options[$("self_shop" + i).selectedIndex].value;
			aStore = createArray(strStore);
			if (aPos[1] == 0) lvlComm = (aStore[1]).toFloat();
			else lvlComm = (aStore[1]).toFloat() + (aStore[2]).toFloat() + (aStore[3]).toFloat();
			if (aPos[1] == 7) lvlComm += (aStore[8]).toFloat();
			if (aStore[0] == "C") curSelfShoppingCommission = lvlComm * purchNum;
			else curSelfShoppingCommission = lvlComm * spendAmt * purchNum;
			$("self_shopcomm" + i).set('html', curSelfShoppingCommission.formatCurrency());
			}
		purchNum = ($("purch" + i).value).toInt();
		spendAmt = ($("spend" + i).value).toFloat();
		if (isNaN(purchNum)) purchNum = 0;
		if (isNaN(spendAmt)) spendAmt = 0;
		strStore = $("shop" + i).options[$("shop" + i).selectedIndex].value;
		aStore = createArray(strStore);
		if (intCompPlan == 174) {
			aStore[9] = 0;
			aStore[10] = 0;
			if (i > 1) {
				//this is setting paid levels to 0 so they are not included in the sum for paying to top person
				//Purchaser
				if (aPos[i] == 0) { aStore[1] = 0; }
				if (aPos[i] == 1) { aStore[1] = 0; aStore[2] = 0; aStore[3] = 0; }
				if (aPos[i] == 2) { aStore[1] = 0; aStore[2] = 0; aStore[3] = 0; aStore[4] = 0; }
				if (aPos[i] == 3) { aStore[1] = 0; aStore[2] = 0; aStore[3] = 0; aStore[4] = 0; aStore[5] = 0; }
				if (aPos[i] == 4) { aStore[1] = 0; aStore[2] = 0; aStore[3] = 0; aStore[4] = 0; aStore[5] = 0; aStore[6] = 0; nNMD++; }
				//Referrers
				if (i > 2) {
					for (p = i - 1; p > 1; p--) {
						if (aPos[p] == 0) { aStore[2] = 0; }
						if (aPos[p] == 1) { aStore[1] = 0; aStore[2] = 0; aStore[3] = 0; }
						if (aPos[p] == 2) { aStore[1] = 0; aStore[2] = 0; aStore[3] = 0; aStore[4] = 0; }
						if (aPos[p] == 3) { aStore[1] = 0; aStore[2] = 0; aStore[3] = 0; aStore[4] = 0; aStore[5] = 0; }
						if (aPos[p] == 4) { aStore[1] = 0; aStore[2] = 0; aStore[3] = 0; aStore[4] = 0; aStore[5] = 0; aStore[6] = 0; nNMD++; }
						}
					}
				}
			//Payee
			var asl = aStore.length;
			if (aPos[1] == 0) {
				if (i == 1) { for (v = 2; v < asl; v++) { aStore[v] = 0; } }
				else { for (v = 3; v < asl; v++) { aStore[v] = 0; } }
				}
			if (aPos[1] == 1) { for (v = 4; v < asl; v++) { aStore[v] = 0; } }
			if (aPos[1] == 2) { for (v = 5; v < asl; v++) { aStore[v] = 0; } }
			if (aPos[1] == 3) { for (v = 6; v < asl; v++) { aStore[v] = 0; } }
			if (aPos[1] == 4) {
				nNMD++;
				if (nNMD == 2) aStore[8] = 0;
				else { aStore[7] = 0; aStore[8] = 0; }
				}
			//alert(aStore.join(", "));
			if (aStore[0] == "C") curShoppingCommission = (aStore[1] + aStore[2] + aStore[3] + aStore[4] + aStore[5] + aStore[6] + aStore[7] + aStore[8]) * purchNum;
			else {
				//alert("L1 = " + aStore[1] + "\nL2 = " + aStore[2] + "\nL3 = " + aStore[3] + "\nL4 = " + aStore[4] + "\nL5 = " + aStore[5] + "\nL6 = " + aStore[6] + "\nL7 = " + aStore[7] + "\nL8 = " + aStore[8] + "\n\nSum = " + (aStore[1] + aStore[2] + aStore[3] + aStore[4] + aStore[5] + aStore[6] + aStore[7] + aStore[8]));
				curShoppingCommission = (aStore[1] + aStore[2] + aStore[3] + aStore[4] + aStore[5] + aStore[6] + aStore[7] + aStore[8]) * spendAmt * purchNum;
				}
			}
		if (intCompPlan == 175) {
			var curShoppingCommission = 0;
			var mmCount = 0;
			var iboCount = 0;
			var ndCount = 0;
			var mmComm = new Array(0, (aStore[1]).toFloat(), (aStore[2]).toFloat());
			var llComm = new Array(0, (aStore[1]).toFloat() + (aStore[2]).toFloat() + (aStore[3]).toFloat(), (aStore[4]).toFloat(), (aStore[5]).toFloat(), (aStore[6]).toFloat(), (aStore[7]).toFloat())
			var ndComm = new Array(0, (aStore[8]).toFloat(), (aStore[9]).toFloat(), (aStore[10]).toFloat());
			var lvlComm = 0;
			for (d = i; d > 0; d--) {
				var si = $("pos_" + d + "_" + i).selectedIndex;
				if (si == 0) mmCount++;
				if (si > 0) iboCount++;
				if (si == 7) ndCount++;
				}
			if (aPos[1] == 0) {
				if (iboCount > 0) lvlComm = 0;
				else if (mmCount > 2) lvlComm = 0;
				else lvlComm = mmComm[mmCount];
				}
			else {
				if (mmCount > 1) lvlComm = mmComm[1] + mmComm[2];
				else if (mmCount == 1) lvlComm = mmComm[1];
				if (iboCount == 1) lvlComm = llComm[1] - lvlComm;
				else if (iboCount < llComm.length) lvlComm = llComm[iboCount];
				if (ndCount < ndComm.length && aPos[1] == 7) lvlComm += ndComm[ndCount];
				}
			if (aStore[0] == "C") curShoppingCommission = lvlComm * purchNum;
			else curShoppingCommission = lvlComm * spendAmt * purchNum;
			if (objBody.hasClass("ND")) {
				//do gateway overrides for ND calculator
				var gv1 = ($("over1" + i).value).toFloat() * aStore[4] * 2;
				var gv2 = ($("over2" + i).value).toFloat() * aStore[5] * 2;
				var gv3 = ($("over3" + i).value).toFloat() * aStore[6] * 2;
				var gv4 = ($("over4" + i).value).toFloat() * aStore[7] * 2;
				gatewayOverAmount = gv1 + gv2 + gv3 + gv4;
				$("overcomm" + i).set('html', gatewayOverAmount.formatCurrency());
				}
			}
		$("shopcomm" + i).set('html', curShoppingCommission.formatCurrency());
		}
	
	//THIS HANDLES THE LOCAL LICENSES
	var curLocOverAmount = 0;
	var curLocalCommission = 0;
	var myComm = new Array(0,0,0,0,0,0,0)
	if (blnShowLocal) {
		localNum = ($("localnum" + i).value).toInt();
		if (isNaN(localNum)) localNum = 0;
		localSold = $("local" + i).selectedIndex;
		minStatus = 0;
		if (aPos[1] > 0 && localSold > 0 && localNum > 0) {
			totComm = 0;
			if (intCompPlan == 174) {
				for (d = i; d > 0; d--) {
					var si = $("pos_" + d + "_" + i).selectedIndex;
					if (si == 1 && minStatus < 1)  { myComm[d] = 100 - totComm; totComm += myComm[d]; minStatus = 1; }
					if (si == 2 && minStatus == 1) { myComm[d] = 150 - totComm; if (totComm > 0) { totComm += myComm[d] } else { totComm = 100 }; minStatus = 2; }
					if (si == 2 && minStatus == 0) { myComm[d] = 100 - totComm; if (totComm > 0) { totComm += myComm[d] } else { totComm = 100 }; minStatus = 2; }
					if (si == 3 && minStatus < 3)  { myComm[d] = 200 - totComm; if (totComm > 0) { totComm += myComm[d] } else { totComm = 200 }; minStatus = 3; }
					if (si == 4 && minStatus < 4)  { myComm[d] = 300 - totComm; if (totComm > 0) { totComm += myComm[d] } else { totComm = 300 }; minStatus = 4; }
					}
				//OVERRIDES
				var curLocOverAmount = 0;
				if (aPos[1] == 4) {
					nmdCount = 1;
					if (i > 1) for (d = 2; d <= i; d++) if ($("pos_" + d + "_" + i).selectedIndex == 4) nmdCount++;
					if (nmdCount < 4 && nmdCount > 1) {
						//overrideNumber = 4 - nmdCount;
						//if (overrideNumber == 1) curLocOverAmount = 10 * localNum;
						//else curLocOverAmount = 20 * localNum;
						curLocOverAmount = 25 * localNum;
						$("localover" + i).set('html', curLocOverAmount.formatCurrency());
						}
					else $("localover" + i).set('html', (0).formatCurrency());
					}
				else if ($("localover" + i)) $("localover" + i).set('html', (0).formatCurrency());
				curLocalCommission = myComm[1] * localNum;
				}
			if (intCompPlan == 175) {
				var mmCount = 0;
				var iboCount = 0;
				var ndCount = 0;
				var mmComm = new Array(0, 0, 0);
				var llComm = new Array(0, 0 + 0 + 200, 25, 25, 25, 25)
				var ndComm = new Array(0, 20, 15, 10, 5);
				var lvlComm = 0;
				for (d = i; d > 0; d--) {
					if ($("pos_" + d + "_" + i).selectedIndex < 2) mmCount++;
					if ($("pos_" + d + "_" + i).selectedIndex > 1) iboCount++;
					if ($("pos_" + d + "_" + i).selectedIndex == 7) ndCount++;
					}
				if (aPos[1] < 2) {
					if (iboCount > 0) lvlComm = 0;
					else if (mmCount > 2) lvlComm = 0;
					else lvlComm = mmComm[mmCount];
					}
				else {
					if (mmCount > 1) lvlComm = mmComm[1] + mmComm[2];
					else if (mmCount == 1) lvlComm = mmComm[1];
					if (iboCount == 1) lvlComm = llComm[1] - lvlComm;
					else if (iboCount < llComm.length) lvlComm = llComm[iboCount];
					if (ndCount < (ndComm.length) && aPos[1] == 7) lvlComm += ndComm[ndCount];
					}
				curLocalCommission = lvlComm * localNum;
				if (objBody.hasClass("ND")) {
					var lcl1 = ($("local1" + i).value).toFloat() * 25;
					var lcl2 = ($("local2" + i).value).toFloat() * 25;
					var lcl3 = ($("local3" + i).value).toFloat() * 25;
					var lcl4 = ($("local4" + i).value).toFloat() * 25;
					curLocOverAmount = lcl1 + lcl2 + lcl3 + lcl4;
					$("localover" + i).set('html', curLocOverAmount.formatCurrency());
					}
				}
			}
		$("localcomm" + i).set('html', curLocalCommission.formatCurrency());
		var curSBCommission = 0;
		var vcCost = 0
		var vcNum = 0;
		var minStatus = 0;
		var totComm = 0;
		if (blnShowSweetBees && intCompPlan == 174) {
			vcNum = ($('sweetbeesnum' + i).value).toInt();
			vcCost = ($('sweetbeesvc' + i).options[$('sweetbeesvc' + i).selectedIndex].value).toInt();
			if (intCompPlan == 174 && aPos[1] > 0) {
				for (d = i; d > 0; d--) {
					if ($("pos_" + d + "_" + i).selectedIndex == 4 && minStatus < 4)  { myComm[d] = .05 - totComm; totComm += myComm[d]; minStatus = 4; }
					if ($("pos_" + d + "_" + i).selectedIndex == 3 && minStatus < 3)  { myComm[d] = .04 - totComm; totComm += myComm[d]; minStatus = 3; }
					if ($("pos_" + d + "_" + i).selectedIndex > 0 && minStatus < 1)  { myComm[d] = .02 - totComm; totComm += myComm[d]; minStatus = 1; }
					}
				}
			curSBCommission += (myComm[1] * vcNum * vcCost);
			$("sweetbeescomm" + i).set('html', curSBCommission.formatCurrency());
			}
		}
	
	//THIS HANDLES THE LICENSES
	var curOverAmount = 0;
	var minStatus = 0;
	var curBonusAmount = 0;
	var curLicenseCommission = 0;
	var curKrewAmount = 0;
	myComm = new Array(0,0,0,0,0,0,0)
	if (blnShowLicense) {
		licNum = ($("licnum" + i).value).toInt();
		if (isNaN(licNum)) licNum = 0;
		licSold = $("lictype" + i).selectedIndex;
		if (aPos[1] > 0 && licSold > 0 && licNum > 0) {
			if (intCompPlan == 174) {
				switch (licSold) {
					case 1:
						//MD Sold
						totComm = 0;
						minStatus = 1;
						for (d = i; d > 0; d--) {
							if ($("pos_" + d + "_" + i).selectedIndex == 1 && minStatus < 1) { myComm[d] = 100 - totComm; totComm += myComm[d]; minStatus = 1; }
							if ($("pos_" + d + "_" + i).selectedIndex == 2 && minStatus < 2) { myComm[d] = 100 - totComm; if (totComm > 0) { totComm += myComm[d] } else { totComm = 100 }; minStatus = 2; }
							if ($("pos_" + d + "_" + i).selectedIndex == 3 && minStatus < 3) { myComm[d] = 200 - totComm; if (totComm > 0) { totComm += myComm[d] } else { totComm = 200 }; minStatus = 3; }
							if ($("pos_" + d + "_" + i).selectedIndex == 4 && minStatus < 4) { myComm[d] = 250 - totComm; if (totComm > 0) { totComm += myComm[d] } else { totComm = 250 }; minStatus = 4; }
							}
						break;
					case 2:
						//RMD Sold
						totComm = 0;
						minStatus = 2;
						for (d = i; d > 0; d--) {
							//if ($("pos_" + d + "_" + i).selectedIndex == 3 & minStatus < 3) { myComm[d] = 500 - totComm; totComm += myComm[d]; minStatus = 3; }
							if ($("pos_" + d + "_" + i).selectedIndex == 4 & minStatus < 4) { myComm[d] = 500 - totComm; totComm += myComm[d]; minStatus = 4; }
							}
						//BONUSES
						/*
						var curBonusAmount = 0;
						if (aPos[1] == 4 && licNum > 1) {
							blnOnlyNMD = true;
							for (r = 2; r < aPos.length; r++) {
								if (aPos[r] == 4) {
									blnOnlyNMD = false;
									break;
									}
								}
							if (blnOnlyNMD == true) {
								var curBonusPer = (licNum - 1) * 100;
								if (curBonusPer > 1000) curBonusPer = 1000;
								curBonusAmount = licNum * curBonusPer;
								$("licbonus" + i).innerHTML = curBonusAmount.formatCurrency();
								}
							else $("licbonus" + i).innerHTML = (0).formatCurrency();
							}
						else if ($("licbonus" + i)) $("licbonus" + i).innerHTML = (0).formatCurrency();
						*/
						break;
					}
				}
			if (intCompPlan == 175 && aPos[1] != 3) {
				//Ranks: 0 = MM, 1 = GL, 2 = QGL, 3 = FR, 4 = GD, 5 = AD, 6 = RD, 7 = ND
				var iboCount = 0;
				var ndCount = 0;
				var tmpRank = 1; //set to 1 so that GLs do not get paid, must be QGL to get bonus on license sale
				var lsComm = new Array(0, 100, 25, 25, 25, 25)
				var ndComm = new Array(0, 20, 15, 10, 5);
				var krewCount = 0;
				for (d = i; d > 0; d--) {
					if ($("pos_" + d + "_" + i).selectedIndex > tmpRank || $("pos_" + d + "_" + i).selectedIndex == 7) { iboCount++; tmpRank = $("pos_" + d + "_" + i).selectedIndex; }
					if ($("pos_" + d + "_" + i).selectedIndex == 7) ndCount++;
					}
				if (iboCount < (lsComm.length - 1) && aPos[1] > 1) myComm[1] = lsComm[iboCount];
				if (ndCount < (ndComm.length - 1) &&  aPos[1] == 7) myComm[1] += ndComm[ndCount];
				krewCount = ($("numOfKrews_" + i).value).toInt();
				if (krewCount > (licNum / 2)) {
					if (!$('krewAlert')) {
						new Element('span', {'id':'krewAlert', 'html':'Not enough licensees to fill krews.', 'styles':{'color':'#c60'}}).inject($("numOfKrews_" + i), 'after');
						}
					}
				else {
					if ($('krewAlert')) { $('krewAlert').dispose(); }
					curKrewAmount = krewCount * 60;
					$('krewbonus').set('html', curKrewAmount.formatCurrency());
					}
				if (objBody.hasClass("ND")) {
					numOfDBs1 = ($("numOfDBs1" + i).value).toInt() * 100; //sponsored by GLs
					numOfDBs2 = ($("numOfDBs2" + i).value).toInt() * 100; //sponsored by QGLs
					numOfDBs3 = ($("numOfDBs3" + i).value).toInt() * 75;  //sponsored by GDs
					numOfDBs4 = ($("numOfDBs4" + i).value).toInt() * 50;  //sponsored by ADs
					numOfDBs5 = ($("numOfDBs5" + i).value).toInt() * 25;  //sponsored by RDs
					curOverAmount = numOfDBs1 + numOfDBs2 + numOfDBs3 + numOfDBs4 + numOfDBs5;
					$("dbcomm" + i).set('html', curOverAmount.formatCurrency());
					}
				}
			}
		curLicenseCommission += (myComm[1] * licNum);
		$("liccomm" + i).set('html', curLicenseCommission.formatCurrency());
		}
	
	//THIS HANDLES THE DRUG CARD COMMISSIONING IF PRESENT
	var curDDCNCommission = 0;
	var rxNum = 0;
	var minStatus = 0;
	var totComm = 0;
	if (blnDDCN) {
		rxNum = ($('rx' + i).value).toInt();
		if (intCompPlan == 174 && aPos[1] > 0) {
			for (d = i; d > 0; d--) {
				if ($("pos_" + d + "_" + i).selectedIndex == 4 && minStatus < 4)  { myComm[d] = 1 - totComm; totComm += myComm[d]; minStatus = 4; }
				if ($("pos_" + d + "_" + i).selectedIndex == 3 && minStatus < 3)  { myComm[d] = .75 - totComm; totComm += myComm[d]; minStatus = 3; }
				if ($("pos_" + d + "_" + i).selectedIndex > 0 && minStatus < 1)  { myComm[d] = .50 - totComm; totComm += myComm[d]; minStatus = 1; }
				}
			}
		if (intCompPlan == 175 && aPos[1] > 0) {
			//Ranks: 0 = MM, 1 = GL, 2 = QGL, 3 = FR, 4 = GD, 5 = AD, 6 = RD, 7 = ND
			var iboCount = 0;
			var ndCount = 0;
			var tmpRank = 1; //set to 1 so that GLs do not get paid, must be QGL to get paid
			var lsComm = new Array(0, .5)
			var ndComm = new Array(0, .25);
			for (d = i; d > 0; d--) {
				if ($("pos_" + d + "_" + i).selectedIndex > tmpRank || $("pos_" + d + "_" + i).selectedIndex == 7) { iboCount++; tmpRank = $("pos_" + d + "_" + i).selectedIndex; }
				if ($("pos_" + d + "_" + i).selectedIndex == 7) ndCount++;
				}
			if (iboCount < (lsComm.length) && aPos[1] > 1) myComm[1] = lsComm[iboCount];
			if (ndCount < (ndComm.length) &&  aPos[1] == 7) myComm[1] += ndComm[ndCount];
			}
		curDDCNCommission += (myComm[1] * rxNum);
		$("ddcncomm" + i).set('html', curDDCNCommission.formatCurrency());
		}

	//THIS HANDLES THE CO-BRAND COMMISSIONING IF PRESENT
	var curCBCommission = 0;
	var cbNum = 0;
	var minStatus = 0;
	var totComm = 0;
	if (blnShowCoBrand) {
		cbNum = ($('cb' + i).value).toInt();
		if (intCompPlan == 174 && aPos[1] > 0) {
			for (d = i; d > 0; d--) {
				if ($("pos_" + d + "_" + i).selectedIndex == 4 && minStatus < 4)  { myComm[d] = 250 - totComm; totComm += myComm[d]; minStatus = 4; }
				if ($("pos_" + d + "_" + i).selectedIndex == 3 && minStatus < 3)  { myComm[d] = 200 - totComm; totComm += myComm[d]; minStatus = 3; }
				if ($("pos_" + d + "_" + i).selectedIndex > 0 && minStatus < 1)  { myComm[d] = 100 - totComm; totComm += myComm[d]; minStatus = 1; }
				}
			}
		curCBCommission += (myComm[1] * cbNum);
		$("cbcomm" + i).set('html', curCBCommission.formatCurrency());
		}

	//THIS HANDLES THE PRIVATE LABEL COMMISSIONING IF PRESENT
	var curPLCommission = 0;
	var plNum = 0;
	var plTyp = 0;
	var minStatus = 0;
	var totComm = 0;
	if (blnShowPrivateLabel) {
		plNum = ($('pl' + i).value).toInt();
		plTyp = ($('plt' + i).options[$('plt' + i).selectedIndex].value).toInt();
		if (intCompPlan == 174 && aPos[1] > 0) {
			switch (plTyp) {
				case 1:
					nmdComm = 2500;
					rmdComm = 2000;
					mdComm = 1000;
					break;
				case 2:
					nmdComm = 5000;
					rmdComm = 4000;
					mdComm = 2000;
					break;
				case 3:
					nmdComm = 7500;
					rmdComm = 6000;
					mdComm = 3000;
					break;
				default:
					nmdComm = 0;
					rmdComm = 0;
					mdComm = 0;
					break;
				}
			for (d = i; d > 0; d--) {
				if ($("pos_" + d + "_" + i).selectedIndex == 4 && minStatus < 4)  { myComm[d] = nmdComm - totComm; totComm += myComm[d]; minStatus = 4; }
				if ($("pos_" + d + "_" + i).selectedIndex == 3 && minStatus < 3)  { myComm[d] = rmdComm - totComm; totComm += myComm[d]; minStatus = 3; }
				if ($("pos_" + d + "_" + i).selectedIndex > 0 && minStatus < 1)  { myComm[d] = mdComm - totComm; totComm += myComm[d]; minStatus = 1; }
				}
			}
		curPLCommission += (myComm[1] * plNum);
		$("plcomm" + i).set('html', curPLCommission.formatCurrency());
		}
	
	//THIS SUMS THE TOTAL AND WRITE IT TO THE PAGE
	$("spnCalcCashBack").set('html', (curSelfShoppingCommission + curShoppingCommission + gatewayOverAmount + curLocalCommission + curLicenseCommission + curLocOverAmount + curBonusAmount + curOverAmount + curDDCNCommission + curKrewAmount + curSBCommission + curCBCommission + curPLCommission).formatCurrency());
	$("spnCalcCashBackYR").set('html', ((curSelfShoppingCommission + curShoppingCommission + gatewayOverAmount + curLocalCommission + curLicenseCommission + curLocOverAmount + curBonusAmount + curOverAmount + curDDCNCommission + curKrewAmount + curSBCommission + curCBCommission + curPLCommission) * 12).formatCurrency());

	if (Browser.chrome) {
		window.scrollBy(0, 1);
		window.scrollBy(0, -1)
		}
	}

//This may fix for mac safari
if (Browser.safari) {
	$$('select').each(function(el){
		el.setStyle('font-family', 'sans-serif');
		});
	}
