/*
	box actions
*/
var boxVisible = new Array();
boxVisible['spam-percent-details'] = "no";
boxVisible['spam-blocked-details'] = "no";

var objectID;
function roi_details(objectID) {
	if (boxVisible[objectID] == "yes") {
		$(objectID).style.display = "none";
	  boxVisible[objectID] = "no";
	}
	else {
		$(objectID).style.display = "inline";
	  boxVisible[objectID] = "yes";
	}
}


/*
	select box switching
*/
var objectID;
function connection_type(objectID) {
	var type = $F(objectID);

	if (type == "Employees") {
		$('conn-input-text').update('Connections/user per day:');

		$('conn-input-w').style.display = "inline";
		$('conn-input-r').style.display = "inline";
	}
	else {
		$('conn-input-text').update('Connections per day:');

		$('conn-input-w').style.display = "none";
		$('conn-input-r').style.display = "none";
	}
}


/*
	calculate
*/
function roi_calculate() {
	var company							= $F('company');
	var conn								= $F('conn');
	var conn_type						= $F('conn-type');
	var connections					= $F('connections');
	var messages						= $F('no-messages');
	var avg_size						= $F('avg-size');
	var avg_cost						= $F('avg-cost');

	var spam_percent 				= $F('spam-percent');
	if (spam_percent > 100) {
		spam_percent = 100;
		$('spam-percent').update(spam_percent);
	}

	var spam_blocked 				= $F('spam-blocked');
	if (spam_blocked > 100) {
		spam_blocked = 100;
		$('spam-blocked').update(spam_blocked);
	}

	var bandwidth_cost					= $F('bandwidth-cost');
	var bandwidth_use						= $F('bandwidth-use');

	var infrastructure_cost			= $F('infrastructure-cost');
	var infrastructure_capacity	= $F('infrastructure-capacity');

	var malwarecleanup_cost			= $F('malwarecleanup-cost');


	/*
		number check
	*/
	var anum					= /^\d+$/;
	var afloat				= /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
	var error_string	= '';
	var is_error			= 'no';

	if (anum.test(conn) != true)
		error_string = 'Must be a positive number without decimals.<br />';
	if (afloat.test(connections) != true)
		is_error = 'yes';
	if (afloat.test(messages) != true)
		is_error = 'yes';
	if (afloat.test(spam_percent) != true)
		is_error = 'yes';
	if (afloat.test(spam_blocked) != true)
		is_error = 'yes';
	if (afloat.test(avg_size) != true)
		is_error = 'yes';
	if (afloat.test(avg_cost) != true)
		is_error = 'yes';
	if (afloat.test(bandwidth_cost) != true)
		is_error = 'yes';
	if (afloat.test(bandwidth_use) != true)
		is_error = 'yes';
	if (afloat.test(infrastructure_cost) != true)
		is_error = 'yes';
	if (afloat.test(infrastructure_capacity) != true)
		is_error = 'yes';
	if (afloat.test(malwarecleanup_cost) != true)
		is_error = 'yes';

	if (is_error == 'yes')
		error_string += 'All parameter values must be a positive number.<br />';

	if (error_string != '') {
		$('notification').update('<br />' + error_string);
		$('notification').style.display = "inline";
		return;
	}
	else {
		$('notification').style.display = "none";
	}


	/*
		calculations
	*/
	var total_connections		= conn;

	if (conn_type == "Employees") {
		total_connections			= (conn * connections);
	}


	var total_messages					= Math.round(total_connections * messages);
	var total_spam_percent			= (total_messages * spam_percent / 100.00);
	var total_spam_blocked			= (total_spam_percent * spam_blocked / 100.00);
	var total_avg_size					= (total_spam_blocked * avg_size / 1000.00);
	var total_avg_cost					= (total_avg_size * avg_cost);
//	var total_mails_per_hour		= (total_messages / 24);
	var total_mails_per_hour		= Math.round(total_messages / 16);   // david wants to use 16 hours. this offsets the fact that mail does not come in evenly and we need to consider peak hours
	var total_mails_per_second	= Math.round(total_messages / 24 / 60 /60);

	$('total-connections').update( commify(total_connections) + ' connections' );
	$('total-no-messages').update( commify(total_messages) + ' messages' );
	$('total-spam-percent').update( commify(total_spam_percent.toFixed(0)) + ' are spam' );
	$('total-spam-blocked').update( commify(total_spam_blocked.toFixed(0)) + ' are blocked' );
	$('total-avg-size').update( commify(total_avg_size.toFixed(2)) + ' MB blocked' );
	$('total-avg-cost').update('$ ' + commify(total_avg_cost.toFixed(2)) + ' wasted storing spam' );
	$('total-mails-per-hour').update(commify(total_mails_per_hour.toFixed(2)) + ' messages' );

	var infrastructure_no_appliances			= Math.ceil(total_mails_per_hour / infrastructure_capacity);
	var infrastructure_cost_appliances		= Math.round(infrastructure_no_appliances * infrastructure_cost);
	var infrastructure_mails_filtered			= Math.round(total_mails_per_hour * spam_blocked / 100.00);
	var infrastructure_mails_sent					= Math.round(total_mails_per_hour - infrastructure_mails_filtered);
	var infrastructure_no_ts_appliances		= Math.ceil(infrastructure_mails_sent / infrastructure_capacity);
	var infrastructure_cost_ts_appliances	= Math.round(infrastructure_no_ts_appliances * infrastructure_cost);
	var infrastructure_savings						= Math.round(infrastructure_cost_appliances - infrastructure_cost_ts_appliances);

	$('infrastructure-no-appliances').update( commify(infrastructure_no_appliances) + ' appliance(s)');
	$('infrastructure-cost-appliances').update( '$ ' + commify(infrastructure_cost_appliances) );
	$('infrastructure-mails-filtered').update( commify(infrastructure_mails_filtered) + ' messages');
	$('infrastructure-mails-sent').update( commify(infrastructure_mails_sent) + ' messages');
	$('infrastructure-no-ts-appliances').update( commify(infrastructure_no_ts_appliances) + ' appliance(s)');
	$('infrastructure-cost-ts-appliances').update( '$ ' + commify(infrastructure_cost_ts_appliances) );
	$('infrastructure-savings').update( '<b>$ ' + commify(infrastructure_savings) + '</b>');

	var annual_spam_blocked							= (total_spam_blocked * 365);
	var annual_bandwidth_savings				= (bandwidth_cost * (bandwidth_use / 100.00) * (spam_percent / 100.00) * (spam_blocked / 100.00) * 12);
	var annual_infrastructure_savings		= infrastructure_savings;
	var annual_storage_savings					= ((total_connections * messages * (spam_percent / 100.00) * (spam_blocked / 100.00) * avg_size * avg_cost * 365) / 1000);
	var annual_malwarecleanup_savings		= (malwarecleanup_cost * conn);
	var annual_total_savings						= ( annual_bandwidth_savings + annual_infrastructure_savings + annual_storage_savings + annual_malwarecleanup_savings);

	$('annual-spam-blocked').update( commify(annual_spam_blocked.toFixed(0)) );
	$('annual-bandwidth-savings').update( '$ ' + commify(annual_bandwidth_savings.toFixed(0)) );
	$('annual-bandwidth-savings2').update( '$ ' + commify(annual_bandwidth_savings.toFixed(0)) );
	$('annual-infrastructure-savings').update( '$ ' + commify(annual_infrastructure_savings.toFixed(0)) );
	$('annual-storage-savings').update( '$ ' + commify(annual_storage_savings.toFixed(0)) );
	$('annual-storage-savings2').update( '$ ' + commify(annual_storage_savings.toFixed(0)) );
	$('annual-malwarecleanup-savings').update( '$ ' + commify(annual_malwarecleanup_savings.toFixed(0)) );
	$('annual-malwarecleanup-savings2').update( '$ ' + commify(annual_malwarecleanup_savings.toFixed(0)) );
	$('annual-total-savings').update( '<b>$ ' + commify(annual_total_savings.toFixed(0)) + '</b>');


	update_image('img-messages', '/roi/roi_msgs.php?msgbd=' + annual_spam_blocked);
	update_image('img-savings', '/roi/roi_money.php?asv=' + annual_total_savings);

	$('company-w').style.display = "none";
	$('company-r').update(company);
	$('company-r').style.display = "inline";

	$('conn-w').style.display = "none";
	$('conn-r').update(conn + ' ' + conn_type);
	$('conn-r').style.display = "inline";

	$('connections-w').style.display = "none";
	$('connections-r').update('connections');
	$('connections-r').style.display = "inline";

	$('no-messages-w').style.display = "none";
	$('no-messages-r').update(messages + ' msg');
	$('no-messages-r').style.display = "inline";

	$('spam-percent-w').style.display = "none";
	$('spam-percent-r').update(spam_percent + ' %');
	$('spam-percent-r').style.display = "inline";

	$('spam-blocked-w').style.display = "none";
	$('spam-blocked-r').update(spam_blocked + ' %');
	$('spam-blocked-r').style.display = "inline";

	$('avg-size-w').style.display = "none";
	$('avg-size-r').update(avg_size + ' KB');
	$('avg-size-r').style.display = "inline";

	$('avg-cost-w').style.display = "none";
	$('avg-cost-r').update('$ ' + avg_cost);
	$('avg-cost-r').style.display = "inline";

	$('bandwidth-use-w').style.display = "none";
	$('bandwidth-use-r').update( commify(bandwidth_use) + ' %');
	$('bandwidth-use-r').style.display = "inline";

	$('bandwidth-cost-w').style.display = "none";
	$('bandwidth-cost-r').update('$ ' + commify(bandwidth_cost) );
	$('bandwidth-cost-r').style.display = "inline";

	$('infrastructure-capacity-w').style.display = "none";
	$('infrastructure-capacity-r').update( commify(infrastructure_capacity) + ' req/h');
	$('infrastructure-capacity-r').style.display = "inline";

	$('infrastructure-cost-w').style.display = "none";
	$('infrastructure-cost-r').update('$ ' + commify(infrastructure_cost) );
	$('infrastructure-cost-r').style.display = "inline";

	$('malwarecleanup-cost-w').style.display = "none";
	$('malwarecleanup-cost-r').update('$ ' + commify(malwarecleanup_cost) );
	$('malwarecleanup-cost-r').style.display = "inline";

	$('calc-button').style.display = "none";
	$('edit-button').style.display = "inline";

	$('value').style.display = "inline";
}


/*
	edit
*/
function roi_edit() {
	$('company-w').style.display = "inline";
	$('company-r').style.display = "none";

	$('conn-w').style.display = "inline";
	$('conn-r').style.display = "none";

	$('connections-w').style.display = "inline";
	$('connections-r').style.display = "none";

	$('no-messages-w').style.display = "inline";
	$('no-messages-r').style.display = "none";

	$('spam-percent-w').style.display = "inline";
	$('spam-percent-r').style.display = "none";

	$('spam-blocked-w').style.display = "inline";
	$('spam-blocked-r').style.display = "none";

	$('avg-size-w').style.display = "inline";
	$('avg-size-r').style.display = "none";

	$('avg-cost-w').style.display = "inline";
	$('avg-cost-r').style.display = "none";

	$('bandwidth-cost-w').style.display = "inline";
	$('bandwidth-cost-r').style.display = "none";

	$('bandwidth-use-w').style.display = "inline";
	$('bandwidth-use-r').style.display = "none";

	$('infrastructure-cost-w').style.display = "inline";
	$('infrastructure-cost-r').style.display = "none";

	$('infrastructure-capacity-w').style.display = "inline";
	$('infrastructure-capacity-r').style.display = "none";

	$('malwarecleanup-cost-w').style.display = "inline";
	$('malwarecleanup-cost-r').style.display = "none";

	$('total-connections').update('');
	$('total-no-messages').update('');
	$('total-spam-percent').update('');
	$('total-spam-blocked').update('');
	$('total-avg-size').update('');
	$('total-avg-cost').update('');
	$('total-mails-per-hour').update('');
	$('infrastructure-no-appliances').update('');
	$('infrastructure-cost-appliances').update('');
	$('infrastructure-mails-filtered').update('');
	$('infrastructure-mails-sent').update('');
	$('infrastructure-no-ts-appliances').update('');
	$('infrastructure-cost-ts-appliances').update('');
	$('infrastructure-savings').update('');

	$('annual-storage-savings2').update('');
	$('annual-spam-blocked').update('');
	$('annual-bandwidth-savings2').update('');
	$('annual-malwarecleanup-savings2').update('');

	$('calc-button').style.display = "inline";
	$('edit-button').style.display = "none";

	$('value').style.display = "none";
}


/*
	reset
*/
function roi_reset() {
	$('company-w').style.display = "inline";
	$('company-r').style.display = "none";

	$('conn-w').style.display = "inline";
	$('conn-r').style.display = "none";

	$('connections-w').style.display = "inline";
	$('connections-r').style.display = "none";

	$('no-messages-w').style.display = "inline";
	$('no-messages-r').style.display = "none";

	$('spam-percent-w').style.display = "inline";
	$('spam-percent-r').style.display = "none";

	$('spam-blocked-w').style.display = "inline";
	$('spam-blocked-r').style.display = "none";

	$('avg-size-w').style.display = "inline";
	$('avg-size-r').style.display = "none";

	$('avg-cost-w').style.display = "inline";
	$('avg-cost-r').style.display = "none";

	$('bandwidth-cost-w').style.display = "inline";
	$('bandwidth-cost-r').style.display = "none";

	$('bandwidth-use-w').style.display = "inline";
	$('bandwidth-use-r').style.display = "none";

	$('infrastructure-cost-w').style.display = "inline";
	$('infrastructure-cost-r').style.display = "none";

	$('infrastructure-capacity-w').style.display = "inline";
	$('infrastructure-capacity-r').style.display = "none";

	$('calc-button').style.display = "inline";
	$('edit-button').style.display = "none";

	$('malwarecleanup-cost-w').style.display = "inline";
	$('malwarecleanup-cost-r').style.display = "none";

	$('total-connections').update('');
	$('total-no-messages').update('');
	$('total-spam-percent').update('');
	$('total-spam-blocked').update('');
	$('total-avg-size').update('');
	$('total-avg-cost').update('');
	$('total-mails-per-hour').update('');
	$('infrastructure-no-appliances').update('');
	$('infrastructure-cost-appliances').update('');
	$('infrastructure-mails-filtered').update('');
	$('infrastructure-mails-sent').update('');
	$('infrastructure-no-ts-appliances').update('');
	$('infrastructure-cost-ts-appliances').update('');
	$('infrastructure-savings').update('');

	$('annual-storage-savings2').update('');
	$('annual-spam-blocked').update('');
	$('annual-bandwidth-savings2').update('');
	$('annual-malwarecleanup-savings2').update('');

	$('value').style.display = "none";

	// set default form values
	$('conn').value = '1000';
	$('conn-type').value = 'Employees';
	$('company').value = '';
	$('connections').value = '117';
	$('no-messages').value = '1.3';
	$('spam-percent').value = '90';
	$('spam-blocked').value = '60';
	$('avg-size').value = '20';
	$('avg-cost').value = '0.10';
	$('bandwidth-cost').value = '5000';
	$('bandwidth-use').value = '50';
	$('infrastructure-cost').value = '5000';
	$('infrastructure-capacity').value = '50000';
	$('malwarecleanup-cost').value = '25';
}


var nStr;
function commify (nStr) {
	nStr += '';
	var x 	= nStr.split('.');
	var x1 	= x[0];
	var x2 	= x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


var num;
function format_currency(num) {
	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);
}


/*
	replace image source url
*/
var imageID;
var url;
function update_image(imageID, url) {
	var image = $(imageID);
	image.src = url;
}

