LBF: Report fields from other sources, save form even if no native fields, some other...
[openemr.git] / library / options.js.php
blob23311cd2f7e993559441097d4a6afdf8586c25ca
1 <?php
2 // Copyright (C) 2014 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.
8 //
9 // This is the place to put JavaScript functions that are needed to support
10 // options.inc.php. Include this in the <head> section of relevant modules.
11 // It's a .php module so that translation can be supported.
13 <script type="text/javascript">
15 // JavaScript support for date types when the A or B edit option is used.
16 // Called to recompute displayed age dynamically when the corresponding date is
17 // changed. Must generate the same age formats as the oeFormatAge() function.
19 function updateAgeString(fieldid, asof, format) {
20 var datefld = document.getElementById('form_' + fieldid);
21 var f = datefld.form;
22 var age = '';
23 var date1 = new Date(datefld.value);
24 var date2 = asof ? new Date(asof) : new Date();
25 if (format == 3) {
26 // Gestational age.
27 var msecs = date2.getTime() - date1.getTime();
28 var days = Math.round(msecs / (24 * 60 * 60 * 1000));
29 var weeks = Math.floor(days / 7);
30 days = days % 7;
31 age = '<?php echo xls('Gest age') ?> ' +
32 weeks + (weeks == 1 ? ' <?php echo xls('week') ?>' : ' <?php echo xls('weeks') ?>') + ' ' +
33 days + (days == 1 ? ' <?php echo xls('day' ) ?>' : ' <?php echo xls('days' ) ?>');
35 else {
36 // Years or months.
37 var dayDiff = date2.getDate() - date1.getDate();
38 var monthDiff = date2.getMonth() - date1.getMonth();
39 var yearDiff = date2.getFullYear() - date1.getFullYear();
40 var ageInMonths = yearDiff * 12 + monthDiff;
41 if (dayDiff < 0) --ageInMonths;
42 if (format == 1 || (format == 0 && ageInMonths >= 24)) {
43 age = yearDiff;
44 if (monthDiff < 0 || (monthDiff == 0 && dayDiff < 0)) --age;
45 age = '' + age;
47 else {
48 age = '' + ageInMonths;
49 if (format == 0) {
50 age = age + ' ' + (ageInMonths == 1 ? '<?php echo xls('month') ?>' : '<?php echo xls('months') ?>');
53 if (age != '') age = '<?php echo xls('Age') ?> ' + age;
55 document.getElementById('span_' + fieldid).innerHTML = age;
58 // Function to show or hide form fields (and their labels) depending on "skip conditions"
59 // defined in the layout.
61 var cskerror = false; // to avoid repeating error messages
62 function checkSkipConditions() {
63 var myerror = cskerror;
64 var prevandor = '';
65 var prevcond = false;
66 for (var i = 0; i < skipArray.length; ++i) {
67 var target = skipArray[i].target;
68 var id = skipArray[i].id;
69 var itemid = skipArray[i].itemid;
70 var operator = skipArray[i].operator;
71 var value = skipArray[i].value;
73 var tofind = id;
74 if (itemid) tofind += '[' + itemid + ']';
75 // Some different source IDs are possible depending on the data type.
76 var srcelem = document.getElementById('check_' + tofind);
77 if (srcelem == null) srcelem = document.getElementById('radio_' + tofind);
78 if (srcelem == null) srcelem = document.getElementById('form_' + tofind);
79 if (srcelem == null) {
80 if (!cskerror) alert('<?php echo xls('Cannot find a skip source field for'); ?> "' + tofind + '"');
81 myerror = true;
82 continue;
85 var condition = false;
86 if (operator == 'eq') condition = srcelem.value == value; else
87 if (operator == 'ne') condition = srcelem.value != value; else
88 if (operator == 'se') condition = srcelem.checked ; else
89 if (operator == 'ns') condition = !srcelem.checked;
91 // Logic to accumulate multiple conditions for the same target.
92 // alert('target = ' + target + ' prevandor = ' + prevandor + ' prevcond = ' + prevcond); // debugging
93 if (prevandor == 'and') condition = condition && prevcond; else
94 if (prevandor == 'or' ) condition = condition || prevcond;
95 prevandor = skipArray[i].andor;
96 prevcond = condition;
97 var j = i + 1;
98 if (j < skipArray.length && skipArray[j].target == target) continue;
100 // At this point condition indicates if the target should be hidden.
102 var trgelem1 = document.getElementById('label_id_' + target);
103 var trgelem2 = document.getElementById('value_id_' + target);
104 if (trgelem1 == null && trgelem2 == null) {
105 if (!cskerror) alert('<?php echo xls('Cannot find a skip target field for'); ?> "' + target + '"');
106 myerror = true;
107 continue;
109 // If the item occupies a whole row then undisplay its row, otherwise hide its cells.
110 var colspan = 0;
111 if (trgelem1) colspan += trgelem1.colSpan;
112 if (trgelem2) colspan += trgelem2.colSpan;
113 if (colspan < 4) {
114 if (trgelem1) trgelem1.style.visibility = condition ? 'hidden' : 'visible';
115 if (trgelem2) trgelem2.style.visibility = condition ? 'hidden' : 'visible';
117 else {
118 if (trgelem1) trgelem1.parentNode.style.display = condition ? 'none' : '';
119 else trgelem2.parentNode.style.display = condition ? 'none' : '';
122 // If any errors, all show in the first pass and none in subsequent passes.
123 cskerror = cskerror || myerror;
126 </script>