2 // Copyright (C) 2014 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 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
);
23 var date1
= new Date(datefld
.value
);
24 var date2
= asof ?
new Date(asof
) : new Date();
27 var msecs
= date2
.getTime() - date1
.getTime();
28 var days
= Math
.round(msecs
/ (24 * 60 * 60 * 1000));
29 var weeks
= Math
.floor(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
' ) ?>');
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)) {
44 if (monthDiff
< 0 ||
(monthDiff
== 0 && dayDiff
< 0)) --age
;
48 age
= '' + ageInMonths
;
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
;
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
;
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
);
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 +
'"');
93 var condition
= false;
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
;
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
;
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 +
'"');
140 // If the item occupies a whole row then undisplay its row, otherwise hide its cells.
142 if (trgelem1
) colspan +
= trgelem1
.colSpan
;
143 if (trgelem2
) colspan +
= trgelem2
.colSpan
;
145 if (trgelem1
) trgelem1
.style
.visibility
= condition ?
'hidden' : 'visible';
146 if (trgelem2
) trgelem2
.style
.visibility
= condition ?
'hidden' : 'visible';
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
;