﻿// JScript File
var g_nGraceSubtotal = 0.00;
var g_nAdditionalSubtotal = 0.00;
var g_nCupSubtotal = 0.00;
var g_nAddDonation = 0.00;
var g_nShipping = 0.00;

function setCurSubtotalLabel(nAmount)
{
	var myTotal = getTotals();
	var tmpLabel = document.getElementById("ctl00_cphBody_lblSuggestedDonation");
	tmpLabel.innerText = "$"+fdp(myTotal,2);
	tmpLabel = document.getElementById("ctl00_cphBody_tbx58");
	tmpLabel.value = fdp(myTotal,2);
}
	
// Get total amount for single item (take amount times cost)
function getItemAmount(nIndex, nCost)
{
	var tmpAmount = 0.00;
	var nTmpValue = 0;
	var tmpElem = document.getElementById("ctl00_cphBody_tbx" + nIndex);
	if (tmpElem != null)
	{
		var nTmpValue = parseFloat(tmpElem.value);
		tmpAmount = (isNaN(nTmpValue))?0:nTmpValue*nCost;
	}
	return tmpAmount;
}
	
// get a range of controls for calculation
function getRangeTotal(nStartIndex, nEndIndex)
{
	var tmpElem;
	var tmpAmount = 0;
	var nTmpValue = 0;
	for (var i=nStartIndex; i<=nEndIndex;i++)
	{
		tmpElem = document.getElementById("ctl00_cphBody_tbx" + i);
		if (null != tmpElem)
		{
			nTmpValue = parseFloat(tmpElem.value);
			tmpAmount += (isNaN(nTmpValue) || (nTmpValue <= 0))?0:nTmpValue;
		}
	}
	return tmpAmount;
}

function getTotals()
{
	var tmpTotal = 0.00;
	tmpTotal += getGrastorationSubtotal();
	tmpTotal += getAdditionalItemSubtotal();
	tmpTotal += getCupbearersSubtotal();
	if (tmpTotal > 0)
	{
		tmpTotal += getShipping();
	}
	tmpTotal += getAdditonalDonation();
	return tmpTotal;
	
}
// Grastoration Materials - controls 1 - 42
function getGrastorationSubtotal()
{
	var nTotal = 0.00;
	var nCardDonationAmt = 0.05;
	var nTapeDonationAmt = 3.00;
	// Loop over Text Boxes to get selected amounts
	var tmpElem = null;
	var amountVal = 0.00;
	for (var i=1; i < 43; i++)
	{
		// Loop over controls to get the cost
		tmpElem = document.getElementById("ctl00_cphBody_tbx" + i);
		if (null != tmpElem)
		{
			var nTmpValue = parseInt(tmpElem.value);
			if ((isNaN(nTmpValue)==false) && (nTmpValue > 0))
			{
				amountVal += nTmpValue*(i%2==1?nCardDonationAmt:nTapeDonationAmt)
			}
		}
	}
	g_nGraceSubtotal = amountVal;
	return amountVal;
}

// Grastoration Materials - controls 43-49
function getAdditionalItemSubtotal()
{
	// Variable item costs so have to add individually
	var tmpAmount = 0.00;
	tmpAmount = getItemAmount(43, 1.50);
	tmpAmount += getItemAmount(44, 4.00);
	tmpAmount += getItemAmount(45, 7.00);
	tmpAmount += getItemAmount(46, 35.00);
	tmpAmount += getItemAmount(47, 55.00);
	tmpAmount += getItemAmount(48, 25.00);
	tmpAmount += getItemAmount(49, 15.00);
	g_nAdditionalSubtotal = tmpAmount;
	return tmpAmount;
}

// Grastoration Materials - controls 48 - 50
function getCupbearersSubtotal()
{
	// Item costs are 1.00 each
	g_nCupSubtotal = getRangeTotal(50, 52)*1.00;
	return g_nCupSubtotal;
}

// Additional Donation - control 58
function getAdditonalDonation()
{
	// This is what ever amount they type in so multiply by one
	g_nAddDonation = getItemAmount(57, 1.00);
	return g_nAddDonation;
}

// Shipping total - control rbtnlPostage
function getShipping()
{
	var tmpElem = document.getElementById("ctl00_cphBody_rbtnlPostage_0")
	if (null != tmpElem)
	{
		if (tmpElem.checked)
		{
			g_nShipping = parseFloat(tmpElem.value);
		}
		else
		{
			g_nShipping = parseFloat("5.00");
		}
	}
	return g_nShipping;
}

function fdp(n, d)
{
	d = (d>10?10:d);
	d = (d<0?0:d);
	var r = "" + Math.round(n * Math.pow(10,d));
	var p = (d==0?r:r.substring(0,r.length-d)+"."+r.substring(r.length-d,r.length));
	return p;
}