<!--
//  File Name: savingcalc.js
//
//  Copyright (c) 2010 Harvey Software, Inc.  All rights reserved.
//
//  Description: Saving Calculator Functions
//
//  Written by: Bert Hamilton
//
//  Date written: 05/04/2010
//
//  Last Revised by:  Bert Hamilton	
//
//  Date of Last Revision:
// 
//  Revision:
//
//
///////////////////////////

// Declare global variables:

var sErrMsg1 = "";		// Process Error Message 

var cselect = "";		// SCProcess command

var rbf = 3.09;			// RateBot Factor
var srbf = 11.38;		// Smart RateBot Factor
var avmf = .36;			// AVM Factor
var tmp = 0.00;			// Temporary Variable

var pkgcnt = 0;			// Number of Packages - Number
var spkgcnt = " ";		// Number of Packages - String

var dwmy = "Day";		// Day Week Month or Year Value for Calc from Pulldown
var rcm = " ";			// Residential Commercial MiX from Pulldown
var avmcb = 0;			// AVM Check Box
var ratebotcb = 0;		// RateBot Check Box
var sratebotcb = 0;		// Smart RateBot Check Box

var prb = 0.00;			// Per Package RateBot Savings - Number
var psrb = 0.00;		// Per Package Smart RateBot Savings - Number
var pavm = 0.00;		// Per Package AVM Savings - Number
var prbavm = 0.00;		// Per Package RateBot+AVM Savings - Number
var psrbavm = 0.00;		// Per Package Smart RateBot+AVM Savings - Number
var trb = 0.00;			// Total RateBot Savings - Number
var tsrb = 0.00;		// Total Smart RateBot Savings - Number
var tavm = 0.00;		// Total AVM Savings - Number
var trbavm = 0.00;		// Total RateBot+AVM Savings - Number
var tsrbavm = 0.00;		// Total Smart RateBot+AVM Savings - Number

var sprb = " ";			// Per Package RateBot Savings - String
var spsrb = " ";		// Per Package Smart RateBot Savings - String
var spavm = " ";		// Per Package AVM Savings - String
var sprbavm = " ";		// Per Package RateBot+AVM Savings - String
var spsrbavm = " ";		// Per Package Smart RateBot+AVM Savings - String
var strb = " ";			// Total RateBot Savings - String
var stsrb = " ";		// Total Smart RateBot Savings - String
var stavm = " ";		// Total AVM Savings - String
var strbavm = " ";		// Total RateBot+AVM Savings - String
var stsrbavm = " ";		// Total Smart RateBot+AVM Savings - String
var dts = " ";			// Displayed Total Savings - String

var CSResults = "http://www.harveysoft.com/savcalc/csresults.htm";
var CSLoad = "http://www.harveysoft.com/savcalc/calc.htm";
var CShelp01 = "http://www.harveysoft.com/savcalc/schelp01.htm";
var CShelp02 = "http://www.harveysoft.com/savcalc/schelp02.htm";
var CShelp03 = "http://www.harveysoft.com/savcalc/schelp03.htm";
var CShelp04 = "http://www.harveysoft.com/savcalc/schelp04.htm";
var CShelp05 = "http://www.harveysoft.com/savcalc/schelp05.htm";

function formatCurrency(num)  							// Returns a string in the $0,000.00 format from an unformatted number
{

	num = num.toString().replace(/\$|\,/g,'');

	if(isNaN(num))
	{
		num = "0";
	}

	var sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	var cents = num%100;
	num = Math.floor(num/100).toString();

	if(cents<10)
	{
		cents = "0" + cents;
	}

	for(var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	{
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	}

	return(((sign)?'':'-') + '$' + num + '.' + cents);
}

function formatIntCommas(num)  							// Returns a string in the 0,000 format from an unformatted integer
{

	num = num.toString().replace(/\$|\,/g,'');

	if(isNaN(num))
	{
		num = "0";
	}

	var sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	var cents = num%100;
	num = Math.floor(num/100).toString();

	if(cents<10)
	{
		cents = "0" + cents;
	}

	for(var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	{
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	}

	return(((sign)?'':'-') + num);
}

function SCProcess(cselect)										// Calculator commands
{
	// CSLOAD command

	if (cselect.toUpperCase() == "CSLOAD")								// Load Calculator
    	{   
		window.open(CSLoad,"SCwin",'height=650,width=340,resizable=no,scrollbars=no,toolbar=no');
		return true;

	}

	// CSHELP01 command

	if (cselect.toUpperCase() == "CSHELP01")								// Load Help
    	{   
		var SCHwindow = window.open(CShelp01,"SCHwin",'height=700,width=500,resizable=yes,scrollbars=yes,toolbar=no');
		SCHwindow.document.close();
		return true;

	}

	// CSHELP02 command

	if (cselect.toUpperCase() == "CSHELP02")								// Load Help
    	{   
		var SCHwindow = window.open(CShelp02,"SCHwin",'height=450,width=500,resizable=yes,scrollbars=yes,toolbar=no');
		SCHwindow.document.close();
		return true;

	}

	// CSHELP03 command

	if (cselect.toUpperCase() == "CSHELP03")								// Load Help
    	{   
		var SCHwindow = window.open(CShelp03,"SCHwin",'height=450,width=500,resizable=yes,scrollbars=yes,toolbar=no');
		SCHwindow.document.close();
		return true;

	}

	// CSHELP04 command

	if (cselect.toUpperCase() == "CSHELP04")								// Load Help
    	{   
		var SCHwindow = window.open(CShelp04,"SCHwin",'height=400,width=500,resizable=yes,scrollbars=yes,toolbar=no');
		SCHwindow.document.close();
		return true;

	}

	// CSHELP05 command

	if (cselect.toUpperCase() == "CSHELP05")								// Load Help
    	{   
		var SCHwindow = window.open(CShelp05,"SCHwin",'height=400,width=500,resizable=yes,scrollbars=yes,toolbar=no');
		SCHwindow.document.close();
		return true;

	}

	// FIGURESAVINGS command

	if (cselect.toUpperCase() == "FIGURESAVINGS")							// Calculate
    	{
		// Verify input and fill pkgcnt variable
   
		form = document.forms[0];


		if (form.NOPKGS.value == "" || form.NOPKGS.value == "0" || form.NOPKGS.value == "1")	// Check for bad data
		{ 
			sErrMsg1 = "Enter a value 2 or greater...";
			alert(sErrMsg1);
			form.NOPKGS.focus();
			return false;
		}


		pkgcnt = form.NOPKGS.value;
		spkgcnt = formatIntCommas(pkgcnt);							// Format package count

		if (isNaN(pkgcnt))									// Check for not a number
		{ 
			sErrMsg1 = "Enter a number 2 or greater";
			alert(sErrMsg1);
			form.NOPKGS.focus();
			return false;
		}

		if (pkgcnt < 0)										// Check for a negative number
		{ 
			sErrMsg1 = "Enter a postive number 2 or greater...";
			alert(sErrMsg1);
			form.NOPKGS.focus();
			return false;
		}


		// Fill Day Week Month Year variable from form

		if (form.TIME.selectedIndex == 0)
		{
			dwmy = "Day";
		}

		if (form.TIME.selectedIndex == 1)
		{
			dwmy = "Week";
		}

		if (form.TIME.selectedIndex == 2)
		{
			dwmy = "Month";
		}

		if (form.TIME.selectedIndex == 3)
		{
			dwmy = "Year";
		}

		// Fill Residential Commercial MiX from form
		
		if (form.ResCom[0].checked)				// 90% Homes and 10% Businesses.
		{
			rcm = "90% homes and 10% businesses.";
			rbf = 3.04;					// RateBot Factor
			srbf = 7.90;					// Smart RateBot Factor
			avmf = .36;					// AVM Factor

		}
		if (form.ResCom[1].checked)				// 50% Homes and 50% Businesses.
		{
			rcm = "50% homes and 50% businesses.";
			rbf = 2.99;					// RateBot Factor
			srbf = 6.28;					// Smart RateBot Factor
			avmf = .36;					// AVM Factor
		}
		if (form.ResCom[2].checked)				// 10% Homes and 90% Businesses.
		{
			rcm = "10% homes and 90% businesses.";
			rbf = 2.95;					// RateBot Factor
			srbf = 4.67;					// Smart RateBot Factor
			avmf = .36;					// AVM Factor
		}

		// Fill CPS Tools Used from form

		avmcb = 0;
		ratebotcb = 0;
		sratebotcb = 0;

		if (form.AVM.checked)				// AVM
		{
			avmcb = 1;
		}
		if (form.RATEBOT.checked)			// RateBot
		{
			ratebotcb = 1;
		}
		if (form.SRATEBOT.checked)			// SRateBot
		{
			sratebotcb = 1;
		}


		// Calculate values

		prb = rbf;
		psrb = srbf;
		pavm = avmf;

		tmp = 0.00;
		tmp = prb+pavm;
		prbavm = tmp;

		tmp = 0.00;
		tmp = psrb+pavm;
		psrbavm = tmp;

		tmp = 0.00;
		tmp = pkgcnt*prb;
		trb = tmp;

		tmp = 0.00;
		tmp = pkgcnt*psrb;
		tsrb = tmp;

		tmp = 0.00;
		tmp = pkgcnt*pavm;
		tavm = tmp;

		tmp = 0.00;
		tmp = pkgcnt*prbavm;
		trbavm = tmp;

		tmp = 0.00;
		tmp = pkgcnt*psrbavm;
		tsrbavm = tmp;

		// Format numbers to currency


		sprb = formatCurrency(prb);
		spsrb = formatCurrency(psrb);
		spavm = formatCurrency(pavm);
		sprbavm = formatCurrency(prbavm);
		spsrbavm = formatCurrency(psrbavm);
		strb = formatCurrency(trb);
		stsrb = formatCurrency(tsrb);
		stavm = formatCurrency(tavm);
		strbavm = formatCurrency(trbavm);
		stsrbavm = formatCurrency(tsrbavm);

		// Fill Displayed Total Savings


		if (sratebotcb)					// Smart RateBots Used
		{
			if (avmcb)				// AVM Used
			{
				dts = stsrbavm;			// Total Smart RateBot+AVM Savings - String
			}	
			else
			{
				dts = stsrb;			// Total Smart RateBot Savings - String
			}
		}
		else
		{
			if (ratebotcb)				// RateBot Used
			{
				if (avmcb)			// AVM Used
				{
					dts = strbavm;		// Total RateBot+AVM Savings - String
				}
				else
				{
					dts = strb;		// Total RateBot Savings - String
				}
			}
			else
			{
				if (avmcb)			// AVM Used
				{
					dts = stavm;			// Total AVM Savings - String
				}
				else
				{
					dts = "$0.00";		// No Tools Used
				}
			}
		}


		// Distpay Results in new window

		var SCRwindow = window.open("","SCRwin",'height=700,width=550,resizable=yes,scrollbars=yes,toolbar=no');


		SCRwindow.document.write("<html>");
		SCRwindow.document.write("<head>");
		SCRwindow.document.write("<title>CPS Savings Calulator Results...</title>");
		SCRwindow.document.write("</head>");
		SCRwindow.document.write("<body style='font-family: Arial; font-size: 10pt; color: #000000'>");
		SCRwindow.document.write("<p><b><font size='4'>Estimated Savings for ");

		SCRwindow.document.write(spkgcnt);  	// Number of Packages

		SCRwindow.document.write(" Packages per ");

		SCRwindow.document.write(dwmy);		// Display Day Week Month or Year


		SCRwindow.document.write(" Using </font></b><img border='0' src='../images/logos/cps02CoxxSm.gif' alt='Harvey Software's Computerized Parcel System (CPS)' width='38' height='20'></p>");
		SCRwindow.document.write("<p><font size='2'>This estimate contains packages sent to ");

		SCRwindow.document.write(rcm);		// Display percent of homes and businesses

		SCRwindow.document.write("</p>");
		SCRwindow.document.write("<p>");
		SCRwindow.document.write("<b>");

		// Decide if CPS Tools are Used
		if ((avmcb+sratebotcb+ratebotcb) == 0)
                {
			SCRwindow.document.write("No optimization tools used for this estimate!");	// No Tools Used
		}
		else
		{
			SCRwindow.document.write("This estimate was optimized using:");			// Tools Used
		}

		SCRwindow.document.write("</b></p>");
		SCRwindow.document.write("</font><p><b><font size='2'>");

		// List Tools Used
		if (avmcb)				// AVM
		{
			// AVM Used
			SCRwindow.document.write("<img border='0' src='../images/bullets/boxiconsmchecked.gif' width='22' height='22'> Address Verification Module (AVM)<br>");
		}

		if (sratebotcb)				// SRateBot
		{
			// Smart RateBots Used
			SCRwindow.document.write("<img border='0' src='../images/bullets/boxiconsmchecked.gif' width='22' height='22'> Smart RateBots");
		}
		else
		{
			if (ratebotcb)				// RateBot
			{
				// RateBots Used
				SCRwindow.document.write("<img border='0' src='../images/bullets/boxiconsmchecked.gif' width='22' height='22'> RateBot");
			}
		}


		SCRwindow.document.write("</font></b></p>");

		SCRwindow.document.write("<table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='100%'>");
		SCRwindow.document.write("<tr>");
		SCRwindow.document.write("<td align='center' bgcolor='#FFE76B' height='40'>");
		SCRwindow.document.write("<p style='margin-left: 4; margin-right: 4'><b><font size='5'>Your Estimated </font></b>");
		SCRwindow.document.write("<img border='0' src='../images/logos/cps02CoYxSm.gif' alt='Harvey Software's Computerized Parcel System (CPS)' width='69' height='38'><font size='5'><b>Savings</b></font></p>");
		SCRwindow.document.write("</td>");
		SCRwindow.document.write("</tr>");
		SCRwindow.document.write("<tr>");
		SCRwindow.document.write("<td align='center' height='40'><b><font size='5'>");

		// Display Total Savings
		SCRwindow.document.write(dts);	// Total Savings
		SCRwindow.document.write(" / ");
		SCRwindow.document.write(dwmy);		// Display Day Week Month or Year

		SCRwindow.document.write("</font></b></td>");
		SCRwindow.document.write("</tr>");
		SCRwindow.document.write("</table>");


		SCRwindow.document.write("<p><b><font size='3'>");

		if (avmcb)				// Message when AVM Used
		{
			if (sratebotcb)			// Message when AVM + Smart RateBot Used
			{
				SCRwindow.document.write("Looks Like Optimized Shipping is in Your Future!<br>All You Need Now is </font></b><font size='3'><img border='0' src='../images/logos/cps02CoxxSm.gif' alt='Harvey Software's Computerized Parcel System (CPS)' width='38' height='20'></font><b><font size='3'>");
			}
			else 				// Message when AVM used but no Smart RateBot
			{
				SCRwindow.document.write("Hint: Try Smart RateBots for more savings...");
			}
		}
		else					// Message no AVM Used
		{
			if (sratebotcb)			// Message when Smart RateBot Used but No AVM
			{
				SCRwindow.document.write("Hint: Try adding AVM for more savings...");
			}
			if (ratebotcb)			// Message when RateBot Used but no AVM
			{
				SCRwindow.document.write("Hint: Try Smart RateBots and AVM for more savings...");
			}
		}
		
		if ((avmcb+sratebotcb+ratebotcb) == 0)	// Message when no tools used
                {

			SCRwindow.document.write("Hint: Try adding AVM, RateBots and SmartBots to start the savings!");
		}

		SCRwindow.document.write("</font></b></p>");
		SCRwindow.document.write("<p><a href='#' onclick='window.close()'>");
		SCRwindow.document.write("<img border='0' src='../images/buttons/close.gif' alt='Close Results...' width='36' height='36'></a>");
		SCRwindow.document.write("<a href='javascript:if (window.print) window.print()'>");
		SCRwindow.document.write("<img border='0' src='../images/buttons/print.gif' alt='Print Current Page' width='36' height='36'></a></p>");
		SCRwindow.document.write("<p><b><font size='1'>Note:&nbsp; This report takes into account three tools in CPS.&nbsp; ");
		SCRwindow.document.write("In addition, this report is a savings estimate based on average savings as reported ");
		SCRwindow.document.write("to us by our customers using these tools.&nbsp; Use of other tools made to work ");
		SCRwindow.document.write("with CPS may increase your savings.&nbsp; Your location and the way you set up CPS ");
		SCRwindow.document.write("RateBots can change your savings.&nbsp; For these reasons your results may vary.&nbsp;");
		SCRwindow.document.write("</font></b></p>");
		SCRwindow.document.write("<p>");
		SCRwindow.document.write("<img border='0' src='../images/logos/HSILogoBoxSM.gif' width='250' height='147'><br>");
		SCRwindow.document.write("<i><b>&nbsp;&nbsp;&nbsp;&nbsp; We Solve Your Shipping Problems</b></i></p>");
		SCRwindow.document.write("<p><font size='1'>Copyright </font><span style='font-family:'''><font size='1'>© ");
		SCRwindow.document.write("2010 Harvey Software, Inc.&nbsp; All Rights Reserved.</font></span></p>");

		SCRwindow.document.write("</body>");
		SCRwindow.document.write("</html>");

		SCRwindow.document.close();
		return true;

	}


	// Handle bad command requests if none were found

	sErrMsg1 = "Not a valid process!";
	alert(sErrMsg1);
	return false;
}

//-->


