1function calculateISS() {
2    const aisScores = [
3        parseInt(document.getElementById('traumacalc_head').value) || 0,
4        parseInt(document.getElementById('traumacalc_face').value) || 0,
5        parseInt(document.getElementById('traumacalc_chest').value) || 0,
6        parseInt(document.getElementById('traumacalc_abdomen').value) || 0,
7        parseInt(document.getElementById('traumacalc_extremity').value) || 0,
8        parseInt(document.getElementById('traumacalc_external').value) || 0
9    ];
10
11    for (let score of aisScores) {
12        if (score < 0 || score > 6) {
13            alert("AIS scores must be between 0 and 6.");
14            return 0;
15        }
16    }
17
18    if (aisScores.includes(6)) return 75;
19
20    const sortedScores = aisScores.sort((a, b) => b - a).slice(0, 3);
21    return sortedScores[0] ** 2 + sortedScores[1] ** 2 + sortedScores[2] ** 2;
22}
23
24function calculateRTS() {
25    const gcs = parseInt(document.getElementById('traumacalc_gcs').value) || 0;
26    const sbp = parseInt(document.getElementById('traumacalc_sbp').value) || 0;
27    const rr = parseInt(document.getElementById('traumacalc_rr').value) || 0;
28
29    if (gcs < 3 || gcs > 15) {
30        alert("GCS must be between 3 and 15.");
31        return 0;
32    }
33    if (sbp < 0) {
34        alert("Systolic BP must be non-negative.");
35        return 0;
36    }
37    if (rr < 0) {
38        alert("Respiratory Rate must be non-negative.");
39        return 0;
40    }
41
42    const gcsValue = gcs >= 13 ? 4 : gcs >= 9 ? 3 : gcs >= 6 ? 2 : gcs >= 4 ? 1 : 0;
43    const sbpValue = sbp > 89 ? 4 : sbp >= 76 ? 3 : sbp >= 50 ? 2 : sbp >= 1 ? 1 : 0;
44    const rrValue = (rr >= 10 && rr <= 29) ? 4 : rr > 29 ? 3 : rr >= 6 ? 2 : rr >= 1 ? 1 : 0;
45
46    const rts = (0.9368 * gcsValue) + (0.7326 * sbpValue) + (0.2908 * rrValue);
47    return rts.toFixed(3);
48}
49
50function calculateTRISS(rts, iss) {
51    const age = parseInt(document.getElementById('traumacalc_age').value) || 0;
52    const coeffSet = document.getElementById('traumacalc_coeff').value;
53
54    if (age < 0 || age > 120) {
55        alert("Age must be between 0 and 120.");
56        return { blunt: 0, penetrating: 0 };
57    }
58
59    // Age index (A): 0 for ≤54, 1 for >54
60    const A = age > 54 ? 1 : 0;
61
62    // Define coefficient sets
63    const coefficients = {
64        standard: {
65            blunt: { b0: -1.2470, b1: 0.9544, b2: -0.0768, b3: -1.9052 },
66            penetrating: { b0: -0.6029, b1: 1.1430, b2: -0.1516, b3: -2.6676 }
67        },
68        qmh: {
69            blunt: { b0: -0.4499, b1: 0.8085, b2: -0.0835, b3: -1.7430 },
70            penetrating: { b0: -2.5355, b1: 0.9934, b2: -0.0651, b3: -1.1360 }
71        }
72    };
73
74    // Select coefficients based on user choice
75    const selectedCoeffs = coefficients[coeffSet];
76
77    // Calculate b for blunt and penetrating trauma
78    const bluntB = selectedCoeffs.blunt.b0 +
79                   (selectedCoeffs.blunt.b1 * rts) +
80                   (selectedCoeffs.blunt.b2 * iss) +
81                   (selectedCoeffs.blunt.b3 * A);
82    const penB = selectedCoeffs.penetrating.b0 +
83                 (selectedCoeffs.penetrating.b1 * rts) +
84                 (selectedCoeffs.penetrating.b2 * iss) +
85                 (selectedCoeffs.penetrating.b3 * A);
86
87    // Probability of survival = 1 / (1 + e^(-b))
88    const bluntProb = (1 / (1 + Math.exp(-bluntB)) * 100).toFixed(1);
89    const penProb = (1 / (1 + Math.exp(-penB)) * 100).toFixed(1);
90
91    return { blunt: bluntProb, penetrating: penProb };
92}
93
94function calculateAll() {
95    const iss = calculateISS();
96    if (iss === 0) return;
97    document.getElementById('traumacalc_iss').value = iss;
98
99    const rts = calculateRTS();
100    if (rts === 0) return;
101    document.getElementById('traumacalc_rts').value = rts;
102
103    const triss = calculateTRISS(rts, iss);
104    document.getElementById('traumacalc_trissBlunt').value = triss.blunt + "%";
105    document.getElementById('traumacalc_trissPenetrating').value = triss.penetrating + "%";
106}
107
108function clearAll() {
109    document.querySelectorAll('.traumacalc select').forEach(select => select.value = '0');
110    document.querySelectorAll('.traumacalc input[type="text"]').forEach(input => input.value = '');
111}