// Define our constants and formulas


// Pricing (monthly)
var PRICE_SCORA_SETUP	= 75.00;
var PRICE_SCORA_DOMAIN	= 10.00;
var PRICE_SCORA_MAILBOX	=  1.50;

// Scora effectiveness (in percent)
var SCORA_EFF_FILTER	= .90;		// Percent less spam
var SCORA_EFF_VIRUS	= .9999;	// Percent less email viruses
var SCORA_EFF_LOSTORDER	= .75;		// Percent less email lost orders

// Coefficients
var COEFF_SORT_INBOX	= 4;	// Time (in seconds per email) that it 
				// takes to figure out if mail in the 
				// INBOX is valid.
var COEFF_SORT_REPORT	= 1.5;	// Time (in seconds per email) that it
				// takes to figure out if mail in the
				// SCORA report is valid.

var DEFAULT_LOST_ORDER_PERCENTAGE = .75; // Typical amount recovered
					 // when a sale is "lost" due
					 // to Spam.

/*
Master function for calculating the Scora savings based on input.
*/
function Calculate()
  {
	var no_scora_cost = 0;
	var scora_cost = 0;
	var scora_service_cost = 0;
	var monthly_savings = 0;
	var recoup_time = 0;

	// Figure out the payroll costs with and without SCORA.
	no_scora_cost	 = Calc_payroll_no_scora();
	scora_cost	 = Calc_payroll_scora();

	// Figure out the virus cost without SCORA.  Use the SCORA virus
	// effectiveness to figure out the virus costs with SCORA.
	no_scora_cost	+= Calc_virus_no_scora();
	scora_cost	+= (1 - SCORA_EFF_VIRUS) * Calc_virus_no_scora();

	// Figure out the "lost email orders" cost without SCORA.  Use 
	// the SCORA "lost email orders" effectiveness to figure out 
	// the costs with SCORA.
	no_scora_cost	+= Calc_lost_order_no_scora();
	scora_cost	+= (1 - SCORA_EFF_LOSTORDER) * 
			   Calc_lost_order_no_scora();

	// Figure out the SCORA service costs.
	scora_service_cost	+= Calc_scora_service();

	scora_cost += scora_service_cost;

	// Find the required recoup time
	recoup_time = Calc_recoup(scora_service_cost, 
				  no_scora_cost, scora_cost);

	// Format the cost 
	no_scora_cost 	= Dollar_format(no_scora_cost);
	scora_cost 	= Dollar_format(scora_cost);

	// Display the output
	document.form1.costnoscoray.value = no_scora_cost;
	document.form1.costwithscoray.value = scora_cost;

	// Find monthly cost
	no_scora_cost /= 12;
	scora_cost /= 12;
	scora_service_cost /= 12;
	
	// Format the cost 
	no_scora_cost 		= Dollar_format(no_scora_cost);
	scora_cost 		= Dollar_format(scora_cost);
	scora_service_cost 	= Dollar_format(scora_service_cost);

	document.form1.costnoscora.value = no_scora_cost;
	document.form1.costwithscora.value = scora_cost;
	document.form1.monthlyscora.value = scora_service_cost;
	document.form1.recouptime.value = recoup_time;
  }


/*
Calculate the yearly payroll costs of sorting spam without SCORA.
	(260 / 3600) * (Employees) * (SPAM [per employee, per day]) * 
		(Pay [per employee, per hour]) * COEFF_SORT_INBOX
*/
function Calc_payroll_no_scora()
  {
	var costa = 0, costb = 0, costc = 0, costd = 0;
	var cost = 0;
	var emp = 0;
	var pay = 0;
	var email = 0;
	var spam = 0;

	// Set the first column values.
	if(isPositiveInteger(document.form1.numempa.value))
	  {
		emp = document.form1.numempa.value;
	  }
	if(isPositiveInteger(document.form1.avgpaya.value))
	  {
		pay = document.form1.avgpaya.value;
	  }
	if(isPositiveInteger(document.form1.avgemaila.value))
	  {
		email = document.form1.avgemaila.value;
	  }
	if(isPositiveInteger(document.form1.avgspama.value))
	  {
		spam = document.form1.avgspama.value;
	  }
	
	// Convert the string values to numbers.
	spam = spam/1;
	email = email/1;

	// Figure out the first column total.
	costa = 260/3600 * emp * spam * COEFF_SORT_INBOX * pay;

	// Reset the variables for the next column.
	emp = 0; pay = 0; email = 0; spam = 0;
	
	// Set the second column values.
	if(isPositiveInteger(document.form1.numempb.value))
	  {
		emp = document.form1.numempb.value;
	  }
	if(isPositiveInteger(document.form1.avgpayb.value))
	  {
		pay = document.form1.avgpayb.value;
	  }
	if(isPositiveInteger(document.form1.avgemailb.value))
	  {
		email = document.form1.avgemailb.value;
	  }
	if(isPositiveInteger(document.form1.avgspamb.value))
	  {
		spam = document.form1.avgspamb.value;
	  }

	// Convert the string values to numbers.
	spam = spam/1;
	email = email/1;

	// Figure out the second column totals.
	costb = 260/3600 * emp * spam * COEFF_SORT_INBOX * pay;

	// Reset the variables to 0.
	emp = 0; pay = 0; email = 0; spam = 0;
	
	// Set the 3rd column values.
	if(isPositiveInteger(document.form1.numempc.value))
	  {
		emp = document.form1.numempc.value;
	  }
	if(isPositiveInteger(document.form1.avgpayc.value))
	  {
		pay = document.form1.avgpayc.value;
	  }
	if(isPositiveInteger(document.form1.avgemailc.value))
	  {
		email = document.form1.avgemailc.value;
	  }
	if(isPositiveInteger(document.form1.avgspamc.value))
	  {
		spam = document.form1.avgspamc.value;
	  }
	
	// Convert the string values to numbers.
	spam = spam/1;
	email = email/1;

	// Figure out the 3rd column cost.
	costc = 260/3600 * emp * spam * COEFF_SORT_INBOX * pay;

	// Reset the variables.
	emp = 0; pay = 0; email = 0; spam = 0;

	// Get the 4th column variables.
	if(isPositiveInteger(document.form1.numempd.value))
	  {
		emp = document.form1.numempd.value;
	  }
	if(isPositiveInteger(document.form1.avgpayd.value))
	  {
		pay = document.form1.avgpayd.value;
	  }
	if(isPositiveInteger(document.form1.avgemaild.value))
	  {
		email = document.form1.avgemaild.value;
	  }
	if(isPositiveInteger(document.form1.avgspamd.value))
	  {
		spam = document.form1.avgspamd.value;
	  }
	
	// Convert the string values to numbers.
	spam = spam/1;
	email = email/1;

	// Figure out the 4th column total.
	costd = 260/3600 * emp * spam * COEFF_SORT_INBOX * pay;

	// Sum the totals from all columns.
	cost = costa + costb + costc + costd;

	return(cost);
  }


/*
Calculate the yearly payroll costs of sorting spam with SCORA.
	260/3600 * ((1 - SCORA_EFF_FILTER) * COEFF_SORT_INBOX + 
	            (SCORA_EFF_FILTER) * COEFF_SORT_REPORT) *
	(Employees) * (SPAM per emp, per day) * (Pay $/hr)
*/
function Calc_payroll_scora()
  {
	var costa = 0, costb = 0, costc = 0, costd = 0;
	var cost = 0;
	var emp = 0;
	var pay = 0;
	var email = 0;
	var spam = 0;

	// Get the first column values.
	if(isPositiveInteger(document.form1.numempa.value))
	  {
		emp = document.form1.numempa.value;
	  }
	if(isPositiveInteger(document.form1.avgpaya.value))
	  {
		pay = document.form1.avgpaya.value;
	  }
	if(isPositiveInteger(document.form1.avgemaila.value))
	  {
		email = document.form1.avgemaila.value;
	  }
	if(isPositiveInteger(document.form1.avgspama.value))
	  {
		spam = document.form1.avgspama.value;
	  }
	
	// Convert the string values to numbers.
	spam = spam/1;
	email = email/1;

	// Figure out the 1st column cost.
	costa = 260/3600 * ((1 - SCORA_EFF_FILTER) * COEFF_SORT_INBOX + 
		           SCORA_EFF_FILTER * COEFF_SORT_REPORT)
	       * emp * spam * pay;

	// Reset the variables.
	emp = 0; pay = 0; email = 0; spam = 0;
	
	// Get the 2nd column values.
	if(isPositiveInteger(document.form1.numempb.value))
	  {
		emp = document.form1.numempb.value;
	  }
	if(isPositiveInteger(document.form1.avgpayb.value))
	  {
		pay = document.form1.avgpayb.value;
	  }
	if(isPositiveInteger(document.form1.avgemailb.value))
	  {
		email = document.form1.avgemailb.value;
	  }
	if(isPositiveInteger(document.form1.avgspamb.value))
	  {
		spam = document.form1.avgspamb.value;
	  }

	// Convert the string values to numbers.
	spam = spam/1;
	email = email/1;

	// Figure out the 2nd column total.
	costb = 260/3600 * ((1 - SCORA_EFF_FILTER) * COEFF_SORT_INBOX + 
		           SCORA_EFF_FILTER * COEFF_SORT_REPORT)
	       * emp * spam * pay;

	// Reset the variables.
	emp = 0; pay = 0; email = 0; spam = 0;
	
	// Get the 3rd column values.
	if(isPositiveInteger(document.form1.numempc.value))
	  {
		emp = document.form1.numempc.value;
	  }
	if(isPositiveInteger(document.form1.avgpayc.value))
	  {
		pay = document.form1.avgpayc.value;
	  }
	if(isPositiveInteger(document.form1.avgemailc.value))
	  {
		email = document.form1.avgemailc.value;
	  }
	if(isPositiveInteger(document.form1.avgspamc.value))
	  {
		spam = document.form1.avgspamc.value;
	  }
	
	// Convert the string values to numbers.
	spam = spam/1;
	email = email/1;

	// Figure out the 3rd column total.
	costc = 260/3600 * ((1 - SCORA_EFF_FILTER) * COEFF_SORT_INBOX + 
		           SCORA_EFF_FILTER * COEFF_SORT_REPORT)
	       * emp * spam * pay;

	// Reset the variables.
	emp = 0; pay = 0; email = 0; spam = 0;

	// Get the 4th column totals.
	if(isPositiveInteger(document.form1.numempd.value))
	  {
		emp = document.form1.numempd.value;
	  }
	if(isPositiveInteger(document.form1.avgpayd.value))
	  {
		pay = document.form1.avgpayd.value;
	  }
	if(isPositiveInteger(document.form1.avgemaild.value))
	  {
		email = document.form1.avgemaild.value;
	  }
	if(isPositiveInteger(document.form1.avgspamd.value))
	  {
		spam = document.form1.avgspamd.value;
	  }
	
	// Convert the string values to numbers.
	spam = spam/1;
	email = email/1;

	// Figure out the 4th column total.
	costd = 260/3600 * ((1 - SCORA_EFF_FILTER) * COEFF_SORT_INBOX + 
		           SCORA_EFF_FILTER * COEFF_SORT_REPORT)
	       * emp * spam * pay;

	// Sum up the totals.
	cost = costa + costb + costc + costd;

	return(cost);
  }


function Calc_virus_no_scora()
  {
	var cost = 0;
	var virus = 0;
	var hours = 0;
	var pay = 0;

	// Get the values.
	if(isPositiveInteger(document.form1.numvirus.value))
	  {
		virus = document.form1.numvirus.value;
	  }
	if(isPositiveInteger(document.form1.timespent.value))
	  {
		hours = document.form1.timespent.value;
	  }
	if(isPositiveInteger(document.form1.avgsyspay.value))
	  {
		pay = document.form1.avgsyspay.value;
	  }

	// Figure out the total
	cost = virus * hours * pay;

	return(cost);
	
  }


/*
Estimate the cost of lost orders without SCORA
*/
function Calc_lost_order_no_scora()
  {
	var orders = 0;
	var amount = 0;
	var resolution = '';
	var percentage = 0;
	var cost = 0;

	switch(document.form1.resolution.value)
	  {
		case "Lost":
			percentage = 1;
		break;

		case "Resolved":
			percentage = 0;
		break;

		case "Discounted":
			if(document.form1.discount.value)
			  {
				percentage = 
				  (100 - document.form1.discount.value) / 100;
			  }
			else
			  {
				percentage = DEFAULT_LOST_ORDER_PERCENTAGE;
			  }
		break;
		default:
			percentage = 1;
	  }
	
	if(isFloat(document.form1.lostorders.value))
	  {
		orders = document.form1.lostorders.value;
	  }
	if(isFloat(document.form1.avgsale.value))
	  {
		amount = document.form1.avgsale.value;
	  }
	
	cost = orders * amount * percentage;

	return(cost);
  }


/*
Calculate the cost of the SCORA service based on the number of
email addresses used.  Assume 1 domain.
*/
function Calc_scora_service()
  {
	var cost = 0;
	var addresses = 0;
	var t_addresses = 0;

	if(isPositiveInteger(document.form1.numempa.value))
	  {
		t_addresses = document.form1.numempa.value/1;
		addresses += t_addresses;
	  }
	if(isPositiveInteger(document.form1.numempb.value))
	  {
		t_addresses = document.form1.numempb.value/1;
		addresses += t_addresses;
	  }
	if(isPositiveInteger(document.form1.numempc.value))
	  {
		t_addresses = document.form1.numempc.value/1;
		addresses += t_addresses;
	  }
	if(isPositiveInteger(document.form1.numempd.value))
	  {
		t_addresses = document.form1.numempd.value/1;
		addresses += t_addresses;
	  }

	// We need to return the yearly cost.
	cost = 12 * ( (addresses * PRICE_SCORA_MAILBOX) + PRICE_SCORA_DOMAIN);

	return(cost);
  }


function Calc_recoup(scora_service_year, no_scora_cost_year, scora_cost_year)
  {
	var months = 1;
	var monthly_savings = (no_scora_cost_year - scora_cost_year) / 12;

	if(monthly_savings < 0)
	  {
		return("Never");
	  }

	while(scora_service_year >= months * monthly_savings)
	  {
		months++;
	  }

	return(months);
  }


/*
  {
	var display = '';
	var months = 0;
	var savings_per_year = no_scora - scora;
	var savings_per_month = savings_per_year / 12;
	scora_service_per_month = scora_service / 12;

alert("savings_per_month: " + savings_per_month);
alert("scora_service_per_month: " + scora_service_per_month);

	if(savings_per_month <= scora_service_per_month)
	  {
		return 'Never';
	  }

	while(scora_service > (months * savings_per_month))
	  {
		months++;
var display = "Inside function Calc_recoup\n";
var display = display + "  scora_service: " + scora_service + "\n";
var display = display + "  months: " + months + "\n";
var display = display + "  savings_per_month: " + savings_per_month + "\n";
var display = display + "  months * savings_per_month: " + months * savings_per_month + "\n";

alert(display);

		if(months > 60)
		  {
			return '> 5 years.';
		  }
	  }

	return months;
  }
*/

/*
Format the given cost into normal monetary display.  E.g.,
  129.13572 	=> 129.14
   95.5 	=>  95.50
  935		=> 935.00
*/
function Dollar_format(money)
  {
	var m = new String(money);
	var dollars = new String();
	var cents = new String();

	var decimal_place = m.indexOf("."); 
	if(decimal_place == -1)
	  {
		decimal_place = m.length;
	  }

	var dollars 	= m.substring(0, decimal_place);
	var cents 	= m.substring(decimal_place + 1, m.length);

	switch(cents.length)
	  {
		case 0:
			cents = '00';
		break;
		case 1:
			cents += '0';
		break;
		default:
			cents = cents.substring(0, 2);
	  }

	return(dollars + "." + cents);
  }
