Fixed conditional hide fields with radio button
[openemr.git] / library / options.js.php
blobda998037f0588f33f34c08cffabb637658710a84
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;
72 var is_radio = false;
73 var tofind = id;
75 if (itemid) tofind += '[' + itemid + ']';
76 // Some different source IDs are possible depending on the data type.
77 var srcelem = document.getElementById('check_' + tofind);
78 var radio_id='form_' + tofind + '[' + value + ']';
79 if(typeof document.getElementById(radio_id)!=="undefined"){
80 srcelem = document.getElementById(radio_id);
81 is_radio = true;
83 if (srcelem == null) srcelem = document.getElementById('radio_' + tofind);
84 if (srcelem == null) srcelem = document.getElementById('form_' + tofind);
85 if (srcelem == null) srcelem = document.getElementById('text_' + tofind);
87 if (srcelem == null) {
88 if (!cskerror) alert('<?php echo xls('Cannot find a skip source field for'); ?> "' + tofind + '"');
89 myerror = true;
90 continue;
93 var condition = false;
96 if ( is_radio){
97 for (var k = 0; k < document.getElementsByName('form_' + tofind).length; k++){
98 if (document.getElementsByName('form_' + tofind)[k].checked){
99 var elem_val= document.getElementsByName('form_' + tofind)[k].value;
102 }else if( typeof srcelem.options!=="undefined"){
103 var elem_val=srcelem.options[srcelem.selectedIndex].text;
104 }else{
105 var elem_val=srcelem.value;
106 if(elem_val == null) elem_val = srcelem.innerText;
109 if (operator == 'eq') condition = elem_val == value; else
110 if (operator == 'ne') condition = elem_val != value; else
111 if (operator == 'se') condition = srcelem.checked ; else
112 if (operator == 'ns') condition = !srcelem.checked;
114 // Logic to accumulate multiple conditions for the same target.
115 // alert('target = ' + target + ' prevandor = ' + prevandor + ' prevcond = ' + prevcond); // debugging
116 if (prevandor == 'and') condition = condition && prevcond; else
117 if (prevandor == 'or' ) condition = condition || prevcond;
118 prevandor = skipArray[i].andor;
119 prevcond = condition;
120 var j = i + 1;
121 if (j < skipArray.length && skipArray[j].target == target) continue;
123 // At this point condition indicates if the target should be hidden.
125 var trgelem1 = document.getElementById('label_id_' + target);
126 var trgelem2 = document.getElementById('value_id_' + target);
128 if (trgelem1 == null && trgelem2 == null) {
129 var trgelem1 = document.getElementById('label_' + target);
130 var trgelem2 = document.getElementById('text_' + target);
131 if(trgelem2 == null){
132 trgelem2 = document.getElementById('form_' + target);
134 if (trgelem1 == null && trgelem2 == null) {
135 if (!cskerror) alert('<?php echo xls('Cannot find a skip target field for'); ?> "' + target + '"');
136 myerror = true;
137 continue;
140 // If the item occupies a whole row then undisplay its row, otherwise hide its cells.
141 var colspan = 0;
142 if (trgelem1) colspan += trgelem1.colSpan;
143 if (trgelem2) colspan += trgelem2.colSpan;
144 if (colspan < 4) {
145 if (trgelem1) trgelem1.style.visibility = condition ? 'hidden' : 'visible';
146 if (trgelem2) trgelem2.style.visibility = condition ? 'hidden' : 'visible';
148 else {
149 if (trgelem1) trgelem1.parentNode.style.display = condition ? 'none' : '';
150 else trgelem2.parentNode.style.display = condition ? 'none' : '';
153 // If any errors, all show in the first pass and none in subsequent passes.
154 cskerror = cskerror || myerror;
157 </script>