//Tiremaster ROI calculations

function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) ||    (key==46) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
//else if (dec && (keychar == "."))
//   {
//   myfield.form.elements[dec].focus();
//   return false;
//   }
else
   return false;
}


function GrandTotals(){
	

	var required  = [ document.tm_roi.ppi1,
			 document.tm_roi.cpy1,
			document.tm_roi.ppi2,
			 document.tm_roi.cpy2,
			 document.tm_roi.rampup,			
			 document.tm_roi.avginv,
			 document.tm_roi.perInv,
			 document.tm_roi.acctfees,
			 document.tm_roi.redperexp];
	
	for (i=0; i<9; i++){
		if (required[i].value == "")  {
			required[i].focus();
			return false;
		}
	}
						  



	// Make sure each total has been set
 	SetSectionSavings('remindTotal');
 	SetSectionSavings('addonTotal');
 	SetSectionSavings('reportTotal');
 	SetSectionSavings('expenseTotal');
	/*
	// now display the grand totals
	var marketTotal = parseFloat(document.tm_roi.G_marketTotal.value);
	var addonTotal = parseFloat(document.tm_roi.G_addonTotal.value);
	var reportTotal = parseFloat(document.tm_roi.G_reportTotal.value);
	var expenseTotal = parseFloat(document.tm_roi.G_expenseTotal.value);
	var roiTotal = marketTotal + addonTotal + reportTotal + expenseTotal;
	
	
	var totalHtml = "<p align=center><strong>How did you do?</strong></p>";
	totalHtml += '<table width="80%" align="center" cellpadding="3" cellspacing="0" >';
	totalHtml += '<tr><td width="60%" align="right"><strong>Total Marketing Savings</strong></td><td align="left">' + formatCurrency(marketTotal) + '</td></tr>';
	totalHtml += '<tr><td width="60%" align="right"><strong>Total Add-on Sales Savings</strong></td><td>' + formatCurrency(addonTotal) + '</td></tr>';
	
	
	totalHtml += '<tr><td width="60%" align="left">&nbsp;</td><td>-----------------</td></tr>';
	totalHtml += '<tr><td width="60%" align="right"><strong>Grand Total Savings</strong></td><td>' + formatCurrency(roiTotal) + '</td></tr>';
	totalHtml += '</table>';
	
	document.getElementById('GrandTotal').innerHTML = totalHtml;
	return false;
	*/
	
	return true;
}


function SetSectionSavings(section) 
{

	var totalHtml;

	switch (section) {
		case 'declineTotal':
		case 'remindTotal':
			if (section == 'declineTotal') {
				document.tm_roi.ppi2.value = document.tm_roi.ppi1.value;
			}

		
			var ppi1 = parseFloat(document.tm_roi.ppi1.value);
			var cpy1 = parseFloat(document.tm_roi.cpy1.value);
			var ppi2 = parseFloat(document.tm_roi.ppi2.value);
			var cpy2 = parseFloat(document.tm_roi.cpy2.value);
			var rampup = parseFloat(document.tm_roi.rampup.value);
			var total1, total2, marketTotal;
			
			if ((isNaN(ppi1))|| (isNaN(cpy1))) {
				total1 = 0;
				}
			else {
				total1 = ppi1 * cpy1;
			}
			if (section == 'declineTotal') {
				document.tm_roi.ppi2.value = ppi1;
			}
			
			
			if (total1 >0) {
				totalHtml = "<strong>" + formatCurrency(total1) + "</strong>"; 
				document.getElementById('declineTotal').innerHTML = totalHtml;
			}

			if ((isNaN(ppi2)) || (isNaN(cpy2))) {
				total2 = 0;
				}
			else {
				total2 = ppi2 * cpy2;
			}
			
			
			totalHtml = "<strong>" + formatCurrency(total2) + "</strong>"; 
			document.getElementById('remindGPTotal').innerHTML = totalHtml;
			
			total2 = total2 * rampup;
			totalHtml = "<strong>" + formatCurrency(total2) + "</strong>"; 
			document.getElementById('remindTotal').innerHTML = totalHtml;
			
			marketTotal = total1 + total2;
			document.tm_roi.G_marketTotal.value = marketTotal;
			totalHtml = "<p><strong>Total Return on Investment for Marketing: " + formatCurrency(marketTotal) + "</strong></p>"; 
			document.getElementById('marketingGrandTotal').innerHTML = totalHtml;
			
			break;
	
		case 'addonTotal':
			var avginv = parseFloat(document.tm_roi.avginv.value);
			var perInv = parseFloat(document.tm_roi.perInv.value);
			var total, weeklyTotal, addonTotal;
			
			if ((isNaN(avginv))|| (isNaN(perInv))) {
				total = 0;
				}
			else {
				total = avginv * perInv;
			}
			if (total >0) {
				totalHtml = "<strong>" + formatCurrency(total) + "</strong>"; 
				document.getElementById('weeklyTotal').innerHTML = totalHtml;
			}
			
			addonTotal = total * 52;
			totalHtml = "<p><strong>Total Yearly Return on Investment for Add-ons: " + formatCurrency(addonTotal) + "</strong> (" + formatCurrency(total) + " * 52 weeks) </p>"; 
			document.getElementById('addonGrandTotal').innerHTML = totalHtml;
			document.tm_roi.G_addonTotal.value = addonTotal;
		break;
	
		case 'reportTotal':
			var gps = parseFloat(document.tm_roi.gps.value);
			var incpergp = parseFloat(document.tm_roi.incpergp.value) / 100;
			var total,  reportTotal;
			
			if ((isNaN(gps))|| (isNaN(incpergp))) {
				total = 0;
				}
			else {
			
				total = gps * incpergp;
			}
			if (total >0) {
				totalHtml = "<strong>" + formatCurrency(total) + "</strong>"; 
				document.getElementById('reportTotal').innerHTML = totalHtml;
			}
			
			
			totalHtml = "<p><strong>Total Return on Investment for Analyisis Reports: " + formatCurrency(total) + "</strong></p>"; 
			document.getElementById('reportGrandTotal').innerHTML = totalHtml;
			document.tm_roi.G_reportTotal.value = total;
			break;

		case 'expenseTotal':
			var acctfees = parseFloat(document.tm_roi.acctfees.value);
			var redperexp = parseFloat(document.tm_roi.redperexp.value) / 100;
			var total,  expenseTotal;
			
			if ((isNaN(acctfees))|| (isNaN(redperexp))) {
				total = 0;
				}
			else {
			
				total = acctfees * redperexp;
			}
			if (total >0) {
				totalHtml = "<strong>" + formatCurrency(total) + "</strong>"; 
				document.getElementById('expenseTotal').innerHTML = totalHtml;
			}
			
			
			totalHtml = "<p><strong>Total Reduction in Expenses: " + formatCurrency(total) + "</strong></p>"; 
			document.getElementById('expenseGrandTotal').innerHTML = totalHtml;
			document.tm_roi.G_expenseTotal.value = total;
			break;
	
	}
	
	
	
	
	
	
}

function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}


