2 // Copyright (C) 2010 Rod Roark <rod@sunsetsystems.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This provides enhancement functions for the LBFvbf visit form,
10 // "Vitals and Body Fat". It is invoked by interface/forms/LBF/new.php.
12 // The purpose of this function is to create JavaScript for the <head>
13 // section of the page. This in turn defines desired javaScript
16 function LBFvbf_javascript()
20 echo "// Compute Body Mass Index.
21 function vbfComputeBMI() {
22 var f = document.forms[0];
25 var height = parseFloat(f.form_height_in.value);
26 var weight = parseFloat(f.form_weight_lbs.value);
27 if(isNaN(height) || isNaN(weight) || height <= 0 || weight <= 0) {
31 bmi = weight / height / height * 703;
33 if (bmi > 42 ) stat = " . xlj('Obesity III') . ";
34 else if (bmi > 34 ) stat = " . xlj('Obesity II') . ";
35 else if (bmi > 30 ) stat = " . xlj('Obesity I') . ";
36 else if (bmi > 27 ) stat = " . xlj('Overweight') . ";
37 else if (bmi > 18.5) stat = " . xlj('Normal') . ";
38 else stat = " . xlj('Underweight') . ";
40 if (f.form_bmi) f.form_bmi.value = bmi;
41 if (f.form_bmi_status) f.form_bmi_status.value = stat;
45 echo "// Height in cm has changed.
46 function vbf_height_cm_changed() {
47 var f = document.forms[0];
48 var cm = f.form_height_cm.value;
49 if (cm == parseFloat(cm)) {
51 f.form_height_in.value = inch.toFixed(2);
54 f.form_height_in.value = '';
60 echo "// Height in inches has changed.
61 function vbf_height_in_changed() {
62 var f = document.forms[0];
63 var inch = f.form_height_in.value;
64 if (inch == parseFloat(inch)) {
66 f.form_height_cm.value = cm.toFixed(2);
69 f.form_height_cm.value = '';
75 echo "// Weight in kg has changed.
76 function vbf_weight_kg_changed() {
77 var f = document.forms[0];
78 var kg = f.form_weight_kg.value;
79 if (kg == parseFloat(kg)) {
80 lbs = kg / 0.45359237;
81 f.form_weight_lbs.value = lbs.toFixed(2);
84 f.form_weight_lbs.value = '';
90 echo "// Weight in lbs has changed.
91 function vbf_weight_lbs_changed() {
92 var f = document.forms[0];
93 var lbs = f.form_weight_lbs.value;
94 if (lbs == parseFloat(lbs)) {
95 kg = lbs * 0.45359237;
96 f.form_weight_kg.value = kg.toFixed(2);
99 f.form_weight_kg.value = '';
105 echo "// Temperature in centigrade has changed.
106 function vbf_temperature_c_changed() {
107 var f = document.forms[0];
108 var tc = f.form_temperature_c.value;
109 if (tc == parseFloat(tc)) {
110 tf = tc * 9 / 5 + 32;
111 f.form_temperature_f.value = tf.toFixed(2);
114 f.form_temperature_f.value = '';
119 echo "// Temperature in farenheit has changed.
120 function vbf_temperature_f_changed() {
121 var f = document.forms[0];
122 var tf = f.form_temperature_f.value;
123 if (tf == parseFloat(tf)) {
124 tc = (tf - 32) * 5 / 9;
125 f.form_temperature_c.value = tc.toFixed(2);
128 f.form_temperature_c.value = '';
133 // Compute patient age and sex.
134 $ptrow = sqlQuery("SELECT DOB, sex FROM patient_data WHERE " .
135 "pid = ? LIMIT 1", [$pid]);
136 $pt_age = 0 +
getpatientAge($ptrow['DOB']);
137 $pt_sex = strtoupper(substr($ptrow['sex'], 0, 1)) == 'F' ?
1 : 0;
139 echo "// Compute Body Fat Percentage.
140 function vbfComputeBF() {
141 var f = document.forms[0];
142 var age = $pt_age; // Patient age in years
143 var sex = $pt_sex; // 0 = Male, 1 = Female
144 if (!f.form_sf_sum || !f.form_body_fat) return;
145 var sfsum = f.form_sf_sum.value;
146 if (sfsum != parseFloat(sfsum) || sfsum <= 0) {
147 f.form_body_fat.value = '';
151 var sflog = Math.LOG10E * Math.log(sfsum);
153 if (age < 17) d = 1.1533 - 0.0643 * sflog;
154 else if (age < 20) d = 1.1620 - 0.0630 * sflog;
155 else if (age < 30) d = 1.1631 - 0.0632 * sflog;
156 else if (age < 40) d = 1.1422 - 0.0544 * sflog;
157 else if (age < 50) d = 1.1620 - 0.0700 * sflog;
158 else d = 1.1715 - 0.0779 * sflog;
161 if (age < 17) d = 1.1369 - 0.0598 * sflog;
162 else if (age < 20) d = 1.1549 - 0.0678 * sflog;
163 else if (age < 30) d = 1.1599 - 0.0717 * sflog;
164 else if (age < 40) d = 1.1423 - 0.0632 * sflog;
165 else if (age < 50) d = 1.1333 - 0.0612 * sflog;
166 else d = 1.1339 - 0.0645 * sflog;
168 var bf = 495 / d - 450;
169 f.form_body_fat.value = bf.toFixed(2);
173 echo "// Tally skin fold measurements.
174 function vbfSFChanged() {
175 var f = document.forms[0];
177 for (var i = 0; i < f.elements.length; ++i) {
178 var e = f.elements[i];
179 if (e.name.substring(0,8) == 'form_sf_' && e.name != 'form_sf_sum') {
180 if (e.value == parseFloat(e.value)) sum += parseFloat(e.value);
183 f.form_sf_sum.value = sum.toFixed(2);
189 // The purpose of this function is to create JavaScript that is run
190 // once when the page is loaded.
192 function LBFvbf_javascript_onload()
196 var f = document.forms[0];
197 if (f.form_weight_lbs && f.form_weight_kg) {
198 // Set onchange handlers to convert kg to lbs and vice versa.
199 f.form_weight_lbs.onchange = function () { vbf_weight_lbs_changed(); };
200 f.form_weight_kg.onchange = function () { vbf_weight_kg_changed() ; };
202 if (f.form_height_in && f.form_height_cm) {
203 // Set onchange handlers to convert centimeters to inches and vice versa.
204 f.form_height_in.onchange = function () { vbf_height_in_changed(); };
205 f.form_height_cm.onchange = function () { vbf_height_cm_changed(); };
207 if (f.form_temperature_f && f.form_temperature_c) {
208 // Set onchange handlers to convert centigrade to farenheit and vice versa.
209 f.form_temperature_f.onchange = function () { vbf_temperature_f_changed(); };
210 f.form_temperature_c.onchange = function () { vbf_temperature_c_changed(); };
212 // Set computed fields to be readonly.
214 f.form_bmi.readOnly = true;
216 if (f.form_bmi_status) {
217 f.form_bmi_status.readOnly = true;
219 if (f.form_body_fat) {
220 f.form_body_fat.readOnly = true;
222 // More of the same, for skin folds.
224 f.form_sf_sum.readOnly = true;
225 for (var i = 0; i < f.elements.length; ++i) {
226 var e = f.elements[i];
227 if (e.name.substring(0,8) == 'form_sf_' && e.name != 'form_sf_sum') {
228 e.onchange = function () { vbfSFChanged(); };