incremented patch version 13
[openemr.git] / sites / default / LBF / LBFvbf.plugin.php
blob093a818fb171f217397beb9634d6e0dfcb69695f
1 <?php
2 // Copyright (C) 2010 Rod Roark <rod@sunsetsystems.com>
3 //
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
14 // functions.
16 function LBFvbf_javascript() {
17 global $formid;
19 echo "// Compute Body Mass Index.
20 function vbfComputeBMI() {
21 var f = document.forms[0];
22 var bmi = 0;
23 var stat = '';
24 var height = parseFloat(f.form_height_in.value);
25 var weight = parseFloat(f.form_weight_lbs.value);
26 if(isNaN(height) || isNaN(weight) || height <= 0 || weight <= 0) {
27 bmi = '';
29 else {
30 bmi = weight / height / height * 703;
31 bmi = bmi.toFixed(1);
32 if (bmi > 42 ) stat = '" . xl('Obesity III') . "';
33 else if (bmi > 34 ) stat = '" . xl('Obesity II' ) . "';
34 else if (bmi > 30 ) stat = '" . xl('Obesity I' ) . "';
35 else if (bmi > 27 ) stat = '" . xl('Overweight' ) . "';
36 else if (bmi > 18.5) stat = '" . xl('Normal' ) . "';
37 else stat = '" . xl('Underweight') . "';
39 if (f.form_bmi) f.form_bmi.value = bmi;
40 if (f.form_bmi_status) f.form_bmi_status.value = stat;
44 echo "// Height in cm has changed.
45 function vbf_height_cm_changed() {
46 var f = document.forms[0];
47 var cm = f.form_height_cm.value;
48 if (cm == parseFloat(cm)) {
49 inch = cm / 2.54;
50 f.form_height_in.value = inch.toFixed(2);
52 else {
53 f.form_height_in.value = '';
55 vbfComputeBMI();
59 echo "// Height in inches has changed.
60 function vbf_height_in_changed() {
61 var f = document.forms[0];
62 var inch = f.form_height_in.value;
63 if (inch == parseFloat(inch)) {
64 cm = inch * 2.54;
65 f.form_height_cm.value = cm.toFixed(2);
67 else {
68 f.form_height_cm.value = '';
70 vbfComputeBMI();
74 echo "// Weight in kg has changed.
75 function vbf_weight_kg_changed() {
76 var f = document.forms[0];
77 var kg = f.form_weight_kg.value;
78 if (kg == parseFloat(kg)) {
79 lbs = kg / 0.45359237;
80 f.form_weight_lbs.value = lbs.toFixed(2);
82 else {
83 f.form_weight_lbs.value = '';
85 vbfComputeBMI();
89 echo "// Weight in lbs has changed.
90 function vbf_weight_lbs_changed() {
91 var f = document.forms[0];
92 var lbs = f.form_weight_lbs.value;
93 if (lbs == parseFloat(lbs)) {
94 kg = lbs * 0.45359237;
95 f.form_weight_kg.value = kg.toFixed(2);
97 else {
98 f.form_weight_kg.value = '';
100 vbfComputeBMI();
104 echo "// Temperature in centigrade has changed.
105 function vbf_temperature_c_changed() {
106 var f = document.forms[0];
107 var tc = f.form_temperature_c.value;
108 if (tc == parseFloat(tc)) {
109 tf = tc * 9 / 5 + 32;
110 f.form_temperature_f.value = tf.toFixed(2);
112 else {
113 f.form_temperature_f.value = '';
118 echo "// Temperature in farenheit has changed.
119 function vbf_temperature_f_changed() {
120 var f = document.forms[0];
121 var tf = f.form_temperature_f.value;
122 if (tf == parseFloat(tf)) {
123 tc = (tf - 32) * 5 / 9;
124 f.form_temperature_c.value = tc.toFixed(2);
126 else {
127 f.form_temperature_c.value = '';
132 // Compute patient age and sex.
133 $ptrow = sqlQuery("SELECT DOB, sex FROM patient_data WHERE " .
134 "pid = '$pid' LIMIT 1");
135 $pt_age = 0 + getpatientAge($ptrow['DOB']);
136 $pt_sex = strtoupper(substr($ptrow['sex'], 0, 1)) == 'F' ? 1 : 0;
138 echo "// Compute Body Fat Percentage.
139 function vbfComputeBF() {
140 var f = document.forms[0];
141 var age = $pt_age; // Patient age in years
142 var sex = $pt_sex; // 0 = Male, 1 = Female
143 if (!f.form_sf_sum || !f.form_body_fat) return;
144 var sfsum = f.form_sf_sum.value;
145 if (sfsum != parseFloat(sfsum) || sfsum <= 0) {
146 f.form_body_fat.value = '';
147 return;
149 var d = 0;
150 var sflog = Math.LOG10E * Math.log(sfsum);
151 if (sex == 0) {
152 if (age < 17) d = 1.1533 - 0.0643 * sflog;
153 else if (age < 20) d = 1.1620 - 0.0630 * sflog;
154 else if (age < 30) d = 1.1631 - 0.0632 * sflog;
155 else if (age < 40) d = 1.1422 - 0.0544 * sflog;
156 else if (age < 50) d = 1.1620 - 0.0700 * sflog;
157 else d = 1.1715 - 0.0779 * sflog;
159 else {
160 if (age < 17) d = 1.1369 - 0.0598 * sflog;
161 else if (age < 20) d = 1.1549 - 0.0678 * sflog;
162 else if (age < 30) d = 1.1599 - 0.0717 * sflog;
163 else if (age < 40) d = 1.1423 - 0.0632 * sflog;
164 else if (age < 50) d = 1.1333 - 0.0612 * sflog;
165 else d = 1.1339 - 0.0645 * sflog;
167 var bf = 495 / d - 450;
168 f.form_body_fat.value = bf.toFixed(2);
172 echo "// Tally skin fold measurements.
173 function vbfSFChanged() {
174 var f = document.forms[0];
175 var sum = 0;
176 for (var i = 0; i < f.elements.length; ++i) {
177 var e = f.elements[i];
178 if (e.name.substring(0,8) == 'form_sf_' && e.name != 'form_sf_sum') {
179 if (e.value == parseFloat(e.value)) sum += parseFloat(e.value);
182 f.form_sf_sum.value = sum.toFixed(2);
183 vbfComputeBF();
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() {
194 echo "
195 var f = document.forms[0];
196 if (f.form_weight_lbs && f.form_weight_kg) {
197 // Set onchange handlers to convert kg to lbs and vice versa.
198 f.form_weight_lbs.onchange = function () { vbf_weight_lbs_changed(); };
199 f.form_weight_kg.onchange = function () { vbf_weight_kg_changed() ; };
201 if (f.form_height_in && f.form_height_cm) {
202 // Set onchange handlers to convert centimeters to inches and vice versa.
203 f.form_height_in.onchange = function () { vbf_height_in_changed(); };
204 f.form_height_cm.onchange = function () { vbf_height_cm_changed(); };
206 if (f.form_temperature_f && f.form_temperature_c) {
207 // Set onchange handlers to convert centigrade to farenheit and vice versa.
208 f.form_temperature_f.onchange = function () { vbf_temperature_f_changed(); };
209 f.form_temperature_c.onchange = function () { vbf_temperature_c_changed(); };
211 // Set computed fields to be readonly.
212 if (f.form_bmi) {
213 f.form_bmi.readOnly = true;
215 if (f.form_bmi_status) {
216 f.form_bmi_status.readOnly = true;
218 if (f.form_body_fat) {
219 f.form_body_fat.readOnly = true;
221 // More of the same, for skin folds.
222 if (f.form_sf_sum) {
223 f.form_sf_sum.readOnly = true;
224 for (var i = 0; i < f.elements.length; ++i) {
225 var e = f.elements[i];
226 if (e.name.substring(0,8) == 'form_sf_' && e.name != 'form_sf_sum') {
227 e.onchange = function () { vbfSFChanged(); };