
// EDITABLE variables
var num_adverse = 0.00634; // Adverse outcome rate per 1000 births
var pct_expense = 80; // % of cases that will incur expenses
var pct_indemnity = 60; // % of cases that will incur indemnity
var cost_expense = 90000; // $ Estimated cost of incurred expenses
var cost_indemnity = 2500000; // $ Estimated cost of incurred indemnity
var pct_air_target = 64; // % of targeted risks for AirStrip OB (AOB)
var pct_fetaldistress = 24; // % of meritorious cases
var pct_fetalheart = 20; // % of meritorious cases
var pct_commbreakdown = 20; // % of meritorious cases
var pct_air_lossprev = 10; // % loss prevention impact with AOB
var cost_varperuser = 225; // $ Cost variable? per user per month
var cost_listperuser = 475; // $ Cost list per user per month



// variables to solve for in calculate()
var num_users; // Number of AOB users
var num_cases; // Total number of problematic cases you can anticipate annually
var total_perinatalclaims; // Annual estimated perinatal claims
var totalcost_perinatalclaims; // $ Annual estimated perinatal costs
var num_fetaldistress; // Fetal distress delays
var num_fetalheart; // Fetal heart misinterpretations
var num_commbreakdown; // Communication breakdowns
var total_air_perinatalclaims; // Total perinatal claims targeted by AOB
var num_atrisk // Estimated lives at-risk
var num_license; // Number of licensed AOB users
var cost_fetaldistress; // $ Fetal distress costs
var cost_fetalheart; // $ Fetal heart misinterpretations costs
var cost_commbreakdown; // $ Communication breakdown costs
var totalcost_air_perinatalclaims; // $ Total cost of perinatal claims targeted by AOB
var cost_atrisk; // $ Estimated cost savings for lives at-risk
var cost_license; // $ Total cost varibale? price per annum
var cost_listlicense; // $ Total cost list price per annum
var total_roi; // $ Total ROI for AOB

// convert variables
var pct_expensedec = pct_expense*.01;
var pct_indemnitydec = pct_indemnity*.01;
var pct_fetaldistressdec = pct_fetaldistress*.01;
var pct_fetalheartdec = pct_fetalheart*.01;
var pct_commbreakdowndec = pct_commbreakdown*.01;
var pct_air_lossprevdec = pct_air_lossprev*.01;


// initialize!
function init() {
try
{
	document.forms['Form'].num_adverse.value = num_adverse; 
	document.forms['Form'].pct_expense.value = pct_expense; 
	document.forms['Form'].pct_indemnity.value = pct_indemnity; 
	document.forms['Form'].cost_expense.innerHTML = addCommas(cost_expense); 
	document.forms['Form'].cost_indemnity.innerHTML = addCommas(cost_indemnity); 
	document.forms['Form'].pct_air_target.innerHTML = pct_air_target; 
	document.forms['Form'].pct_air_lossprev.innerHTML = pct_air_lossprev;  
	}
	catch(e)
	{
	
	}
}
init();

// math!
function calculate() { 
	
		var user_total_births = document.forms['Form'].num_births.value;
		var user_total_users = document.forms['Form'].num_users.value;
		
		checkValues(user_total_births,user_total_users);
		
		num_cases = num_adverse*user_total_births;
		document.getElementById("num_cases").innerHTML = num_cases.toFixed(2);
		
		total_perinatalclaims = (pct_expensedec)*num_cases;
		document.getElementById("total_perinatalclaims").innerHTML = Math.round(total_perinatalclaims);
		
		totalcost_perinatalclaims = Math.round( ( ((pct_expensedec*total_perinatalclaims)*cost_expense)+(((pct_indemnitydec*(pct_expensedec)*total_perinatalclaims))*cost_indemnity)) );
		document.getElementById("totalcost_perinatalclaims").innerHTML = addCommas(totalcost_perinatalclaims);
		
		num_fetaldistress = Math.round(total_perinatalclaims*pct_fetaldistressdec);
		document.getElementById("num_fetaldistress").innerHTML = num_fetaldistress;
	
		num_fetalheart = Math.round(total_perinatalclaims*pct_fetalheartdec);
		document.getElementById("num_fetalheart").innerHTML = num_fetalheart;
	
		num_commbreakdown = Math.round(total_perinatalclaims*pct_commbreakdowndec);
		document.getElementById("num_commbreakdown").innerHTML = num_commbreakdown;
		
		total_air_perinatalclaims = num_fetaldistress+num_fetalheart+num_commbreakdown;
		document.getElementById("total_air_perinatalclaims").innerHTML = total_air_perinatalclaims;
		
		num_atrisk = Math.round((pct_air_lossprevdec)*total_air_perinatalclaims);
		document.getElementById("num_atrisk").innerHTML = num_atrisk;
		
		cost_fetaldistress = Math.round(pct_fetaldistressdec*totalcost_perinatalclaims);
		document.getElementById("cost_fetaldistress").innerHTML = addCommas(cost_fetaldistress);

		cost_fetalheart = Math.round(pct_fetalheartdec*totalcost_perinatalclaims);
		document.getElementById("cost_fetalheart").innerHTML = addCommas(cost_fetalheart);

		cost_commbreakdown = Math.round(pct_commbreakdowndec*totalcost_perinatalclaims);
		document.getElementById("cost_commbreakdown").innerHTML = addCommas(cost_commbreakdown);

		totalcost_air_perinatalclaims = (cost_fetaldistress+cost_fetalheart)+cost_commbreakdown;
		document.getElementById("totalcost_air_perinatalclaims").innerHTML = addCommas(totalcost_air_perinatalclaims);
		
		cost_atrisk = Math.round(totalcost_air_perinatalclaims*pct_air_lossprevdec);
		document.getElementById("cost_atrisk").innerHTML = addCommas(cost_atrisk);
		
		num_license = user_total_users;
		document.getElementById("num_license").innerHTML = num_license;
		
		cost_license = cost_varperuser*12;
		document.getElementById("cost_license").innerHTML = addCommas(cost_license);
		
		cost_listlicense = (cost_listperuser*12)*user_total_users;
		document.getElementById("cost_listlicense").innerHTML = addCommas(cost_listlicense);
		
		total_roi = cost_atrisk-cost_listlicense;
		document.getElementById("total_roi").innerHTML = addCommas(total_roi);
		
}
function checkValues(user_total_births,user_total_users) {
	error = 0;
	if ( isNaN(user_total_births) || user_total_births <= 0) { showAlert("births"); error++; }
	if ( isNaN(user_total_users) || user_total_users <= 0) { showAlert("users"); error++; }
	return error;
}

function hideAlert(whichalert) {
var popalert = document.getElementById("alert_"+whichalert);
popalert.className="";
}
function showAlert(whichalert) {
var popalert = document.getElementById("alert_"+whichalert);
popalert.className = "show";
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

