function calculateProRata() {
var annualPremium = parseFloat(document.getElementById("annualPremium").value);
var policyStartDateStr = document.getElementById("policyStartDate").value;
var policyEndDateStr = document.getElementById("policyEndDate").value;
var calculationDateStr = document.getElementById("calculationDate").value;
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
if (isNaN(annualPremium) || annualPremium endDate) {
resultDiv.innerHTML = "Policy start date cannot be after the policy end date.";
return;
}
if (calcDate endDate) {
resultDiv.innerHTML = "Calculation date must be within the policy period.";
return;
}
// Calculate the total duration of the policy in days
// Add 1 to include both the start and end days for duration calculation
var policyDurationMs = endDate.getTime() – startDate.getTime();
var policyDurationDays = Math.floor(policyDurationMs / (1000 * 60 * 60 * 24)) + 1;
// Calculate the number of days from the policy start date to the calculation date
// Add 1 to include both the start and calculation days for duration calculation
var daysCoveredMs = calcDate.getTime() – startDate.getTime();
var daysCovered = Math.floor(daysCoveredMs / (1000 * 60 * 60 * 24)) + 1;
// Calculate the pro rata cost
var proRataCost = (annualPremium / policyDurationDays) * daysCovered;
resultDiv.innerHTML = "The pro rata cost of the insurance up to " +
calcDate.toLocaleDateString() +
" is:
$" + proRataCost.toFixed(2) + "";
}
.insurance-calculator-wrapper {
font-family: sans-serif;
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
max-width: 500px;
margin: 20px auto;
background-color: #f9f9f9;
}
.calculator-inputs h2 {
text-align: center;
color: #333;
margin-bottom: 15px;
}
.calculator-inputs p {
text-align: center;
color: #555;
margin-bottom: 25px;
font-size: 0.9em;
}
.input-group {
margin-bottom: 15px;
display: flex;
flex-direction: column;
}
.input-group label {
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.input-group input[type="number"],
.input-group input[type="date"] {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1em;
box-sizing: border-box; /* Ensures padding doesn't affect width */
}
.input-group input[type="date"] {
color: #555; /* Makes placeholder text visible */
}
button {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1.1em;
width: 100%;
margin-top: 10px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #45a049;
}
.calculator-results {
margin-top: 25px;
border-top: 1px solid #eee;
padding-top: 20px;
}
.calculator-results h3 {
text-align: center;
color: #333;
margin-bottom: 10px;
}
#result {
text-align: center;
font-size: 1.2em;
color: #007bff;
font-weight: bold;
}