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
;
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 +
'"');
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
;
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 +
'"');
109 // If the item occupies a whole row then undisplay its row, otherwise hide its cells.
111 if (trgelem1
) colspan +
= trgelem1
.colSpan
;
112 if (trgelem2
) colspan +
= trgelem2
.colSpan
;
114 if (trgelem1
) trgelem1
.style
.visibility
= condition ?
'hidden' : 'visible';
115 if (trgelem2
) trgelem2
.style
.visibility
= condition ?
'hidden' : 'visible';
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
;