<?php
// Start the PHP session to handle form submissions and store data if needed
session_start();

// Initialize variables
$roiResult = "";
$errorMessage = "";

// Function to calculate General ROI
function calculateGeneralROI($investmentCost, $totalRevenue) {
    if ($investmentCost <= 0) {
        return "Investment cost must be greater than zero.";
    }
    $netProfit = $totalRevenue - $investmentCost;
    $roi = ($netProfit / $investmentCost) * 100;
    return number_format($roi, 2) . "%";
}

// Function to calculate Marketing ROI
function calculateMarketingROI($investmentCost, $totalRevenue, $additionalCosts) {
    $totalInvestment = $investmentCost + $additionalCosts;
    if ($totalInvestment <= 0) {
        return "Total investment must be greater than zero.";
    }
    $netProfit = $totalRevenue - $totalInvestment;
    $roi = ($netProfit / $totalInvestment) * 100;
    return number_format($roi, 2) . "%";
}

// Function to calculate Real Estate ROI
function calculateRealEstateROI($purchasePrice, $rentalIncome, $operatingExpenses, $appreciationRate) {
    $netProfit = ($rentalIncome - $operatingExpenses) + ($purchasePrice * ($appreciationRate / 100));
    $roi = ($netProfit / $purchasePrice) * 100;
    return number_format($roi, 2) . "%";
}

// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve and sanitize user inputs
    $investmentType = isset($_POST['investmentType']) ? $_POST['investmentType'] : 'general';
    $investmentCost = isset($_POST['investmentCost']) ? floatval($_POST['investmentCost']) : 0;
    $totalRevenue = isset($_POST['totalRevenue']) ? floatval($_POST['totalRevenue']) : 0;

    switch ($investmentType) {
        case "general":
            $roiResult = calculateGeneralROI($investmentCost, $totalRevenue);
            break;
        case "marketing":
            $additionalCosts = isset($_POST['additionalCosts']) ? floatval($_POST['additionalCosts']) : 0;
            $roiResult = calculateMarketingROI($investmentCost, $totalRevenue, $additionalCosts);
            break;
        case "realEstate":
            $rentalIncome = isset($_POST['rentalIncome']) ? floatval($_POST['rentalIncome']) : 0;
            $operatingExpenses = isset($_POST['operatingExpenses']) ? floatval($_POST['operatingExpenses']) : 0;
            $appreciationRate = isset($_POST['appreciationRate']) ? floatval($_POST['appreciationRate']) : 0;
            $roiResult = calculateRealEstateROI($investmentCost, $rentalIncome, $operatingExpenses, $appreciationRate);
            break;
        default:
            $roiResult = "Invalid Investment Type.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Comprehensive ROI Calculator</title>
    <style>
        /* Basic CSS for styling */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        .calculator {
            background-color: #fff;
            padding: 25px;
            border-radius: 8px;
            max-width: 600px;
            margin: auto;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        .calculator h2 {
            text-align: center;
            margin-bottom: 20px;
        }
        .calculator .form-group {
            margin-bottom: 15px;
        }
        .calculator label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .calculator input, .calculator select {
            width: 100%;
            padding: 10px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .calculator button {
            width: 48%;
            padding: 12px;
            margin: 1%;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        .calculator button.calculate {
            background-color: #28a745;
            color: #fff;
        }
        .calculator button.reset {
            background-color: #dc3545;
            color: #fff;
        }
        .result, .error {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            font-size: 18px;
            text-align: center;
        }
        .result {
            background-color: #d4edda;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
        /* Responsive Design */
        @media (max-width: 600px) {
            .calculator button {
                width: 100%;
                margin: 5px 0;
            }
        }
        /* Footer Styling */
        .footer {
            text-align: center;
            margin-top: 40px;
            font-size: 14px;
            color: #555;
        }
        .affiliate-disclaimer {
            margin-top: 10px;
            font-size: 12px;
            color: #888;
        }
        .all-rights {
            margin-top: 5px;
            font-size: 12px;
            color: #888;
        }
        /* Ad Banner Container */
        .ad-banner {
            max-width: 600px;
            margin: 20px auto;
        }
    </style>
    <script>
        // JavaScript to show/hide specific fields based on investment type
        function showSpecificFields() {
            var investmentType = document.getElementById('investmentType').value;
            var marketingFields = document.getElementById('marketingFields');
            var realEstateFields = document.getElementById('realEstateFields');
            // Hide all specific fields initially
            marketingFields.style.display = 'none';
            realEstateFields.style.display = 'none';
            // Show specific fields based on investment type
            if (investmentType === 'marketing') {
                marketingFields.style.display = 'block';
            } else if (investmentType === 'realEstate') {
                realEstateFields.style.display = 'block';
            }
            // Add more conditions for other investment types as needed
        }

        // Function to clear the result when reset button is clicked
        function clearResult() {
            document.getElementById('result').innerText = '';
            document.getElementById('result').classList.remove('result', 'error');
        }

        // Add event listener to the reset button to clear the result
        window.onload = function() {
            showSpecificFields();
            document.getElementById('resetButton').addEventListener('click', clearResult);
        };
    </script>
</head>
<body>

<div class="calculator">
    <h2>Comprehensive ROI Calculator</h2>
    <?php
        // Display result or error message
        if (!empty($roiResult)) {
            if (strpos($roiResult, '%') !== false) {
                echo '<div class="result" id="result">Your ROI is: ' . htmlspecialchars($roiResult) . '</div>';
            } else {
                echo '<div class="error" id="result">' . htmlspecialchars($roiResult) . '</div>';
            }
        }
    ?>
    <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <div class="form-group">
            <label for="investmentType">Investment Type</label>
            <select id="investmentType" name="investmentType" onchange="showSpecificFields()" required>
                <option value="general" <?php if(isset($_POST['investmentType']) && $_POST['investmentType'] == 'general') echo 'selected'; ?>>General Investment</option>
                <option value="marketing" <?php if(isset($_POST['investmentType']) && $_POST['investmentType'] == 'marketing') echo 'selected'; ?>>Marketing Campaign</option>
                <option value="realEstate" <?php if(isset($_POST['investmentType']) && $_POST['investmentType'] == 'realEstate') echo 'selected'; ?>>Real Estate</option>
                <!-- Removed SEO, Affiliate Marketing, and Content Marketing options -->
            </select>
        </div>
        <div class="form-group">
            <label for="investmentCost">Investment Cost ($)</label>
            <input type="number" id="investmentCost" name="investmentCost" step="0.01" min="0" placeholder="Enter investment cost" value="<?php if(isset($_POST['investmentCost'])) echo htmlspecialchars($_POST['investmentCost']); ?>" required>
        </div>
        <div class="form-group">
            <label for="totalRevenue">Total Revenue ($)</label>
            <input type="number" id="totalRevenue" name="totalRevenue" step="0.01" min="0" placeholder="Enter total revenue" value="<?php if(isset($_POST['totalRevenue'])) echo htmlspecialchars($_POST['totalRevenue']); ?>" required>
        </div>

        <!-- Marketing Specific Inputs -->
        <div class="form-group" id="marketingFields" style="display: none;">
            <label for="additionalCosts">Additional Marketing Costs ($)</label>
            <input type="number" id="additionalCosts" name="additionalCosts" step="0.01" min="0" placeholder="Enter additional marketing costs" value="<?php if(isset($_POST['additionalCosts'])) echo htmlspecialchars($_POST['additionalCosts']); ?>">
        </div>

        <!-- Real Estate Specific Inputs -->
        <div class="form-group" id="realEstateFields" style="display: none;">
            <label for="rentalIncome">Annual Rental Income ($)</label>
            <input type="number" id="rentalIncome" name="rentalIncome" step="0.01" min="0" placeholder="Enter annual rental income" value="<?php if(isset($_POST['rentalIncome'])) echo htmlspecialchars($_POST['rentalIncome']); ?>">

            <label for="operatingExpenses">Annual Operating Expenses ($)</label>
            <input type="number" id="operatingExpenses" name="operatingExpenses" step="0.01" min="0" placeholder="Enter annual operating expenses" value="<?php if(isset($_POST['operatingExpenses'])) echo htmlspecialchars($_POST['operatingExpenses']); ?>">

            <label for="appreciationRate">Property Appreciation Rate (%)</label>
            <input type="number" id="appreciationRate" name="appreciationRate" step="0.01" min="0" placeholder="Enter appreciation rate" value="<?php if(isset($_POST['appreciationRate'])) echo htmlspecialchars($_POST['appreciationRate']); ?>">
        </div>

        <!-- Add similar sections for other investment types as needed -->

        <button type="submit" class="calculate">Calculate ROI</button>
        <button type="reset" class="reset" id="resetButton">Reset</button>
    </form>
</div>

<!-- Ad Banner -->
<div class="ad-banner">
    <script type="text/javascript" src="https://mybigcommissions.com/amember/b/7a314331754d/ismelg"></script>
</div>

<!-- Affiliate Disclaimer and All Rights Reserved -->
<div class="footer">
    <div class="affiliate-disclaimer">
        I am an independent Affiliate. The opinions expressed here are my own and are not official statements. If you follow a link and make a purchase, I may earn a commission.
    </div>
    <div class="all-rights">
        &copy; <?php echo date("Y"); ?> IsmelGuerrero.Com. All rights reserved.
    </div>
</div>

</body>
</html>
