Bug 34180: Template variable in JavaScript prevents authority MARC preview from displ...
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / members.js
1 // function to test all fields in forms and nav in different forms(1 ,2 or 3)
2 function check_form_borrowers(nav){
3     var statut=0;
4     var message = "";
5     var message_champ="";
6     if (document.form.check_member.value == 1 )
7     {
8         if (document.form.answernodouble) {
9             if( (!(document.form.answernodouble.checked))){
10                 document.form.nodouble.value=0;
11             } else {
12                 document.form.nodouble.value=1;
13             }
14         }
15     }
16
17     //patrons form to test if you checked no to the question of double
18     if (statut!=1 && document.form.check_member.value > 0 ) {
19         if (!(document.form.answernodouble.checked)){
20             message_champ += __("Please confirm whether this is a duplicate patron");
21             statut=1;
22             document.form.nodouble.value=0;
23         } else {
24             document.form.nodouble.value=1;
25         }
26     }
27
28     if (statut==1){
29         //alert if at least 1 error
30         alert(message+"\n"+message_champ);
31         return false;
32     } else {
33         return true;
34     }
35 }
36
37 function clear_entry(node) {
38     var original = $(node).parent();
39     $("textarea", original).val('');
40     $("select", original).val('');
41 }
42
43 function clone_entry(node) {
44     var original = $(node).parent();
45     var clone = original.clone();
46
47     var newId = 50 + parseInt(Math.random() * 100000);
48     $("input,select,textarea", clone).attr('id', function() {
49         return this.id.replace(/patron_attr_\d+/, 'patron_attr_' + newId);
50     });
51     $("input,select,textarea", clone).attr('name', function() {
52         return this.name.replace(/patron_attr_\d+/, 'patron_attr_' + newId);
53     });
54     $("label", clone).attr('for', function() {
55         return $(this).attr("for").replace(/patron_attr_\d+/, 'patron_attr_' + newId);
56     });
57     $("input#patron_attr_" + newId, clone).attr('value','');
58     $("select#patron_attr_" + newId, clone).attr('value','');
59     $(original).after(clone);
60     return false;
61 }
62
63 function update_category_code(category_code) {
64     if ( $(category_code).is("select") ) {
65         category_code = $("#categorycode_entry").find("option:selected").val();
66     }
67     var mytables = $(".attributes_table");
68     $(mytables).find("li").hide();
69     $(mytables).find(" li[data-category_code='"+category_code+"']").show();
70     $(mytables).find(" li[data-category_code='']").show();
71
72     //Change password length hint
73     var hint = $("#password").siblings(".hint").first();
74     var min_length = $('select'+category_selector+' option:selected').data('pwdLength');
75     var hint_string = __("Minimum password length: %s").format(min_length);
76     hint.html(hint_string);
77 }
78
79 function select_user(borrowernumber, borrower, relationship) {
80     let is_guarantor = $(`.guarantor-details[data-borrowernumber=${borrowernumber}]`).length;
81
82     if ( is_guarantor ) {
83         alert("Patron is already a guarantor for this patron");
84     } else {
85         $('#guarantor_id').val(borrowernumber);
86         $('#guarantor_surname').val(borrower.surname);
87         $('#guarantor_firstname').val(borrower.firstname);
88
89         var fieldset = $('#guarantor_template').clone();
90         fieldset.removeAttr('id');
91
92         var guarantor_id = $('#guarantor_id').val();
93         if ( guarantor_id ) {
94             fieldset.find('.new_guarantor_id').first().val( guarantor_id );
95             fieldset.find('.new_guarantor_id_text').first().text( borrower.cardnumber );
96             fieldset.find('.new_guarantor_link').first().attr("href", "/cgi-bin/koha/members/moremember.pl?borrowernumber=" + guarantor_id );
97         } else {
98             fieldset.find('.guarantor_id').first().hide();
99         }
100         $('#guarantor_id').val("");
101
102         var guarantor_surname = $('#guarantor_surname').val();
103         fieldset.find('.new_guarantor_surname').first().val( guarantor_surname );
104         fieldset.find('.new_guarantor_surname_text').first().text( guarantor_surname );
105         $('#guarantor_surname').val("");
106
107         var guarantor_firstname = $('#guarantor_firstname').val();
108         fieldset.find('.new_guarantor_firstname').first().val( guarantor_firstname );
109         fieldset.find('.new_guarantor_firstname_text').first().text( guarantor_firstname );
110         $('#guarantor_firstname').val("");
111
112         var guarantor_relationship = $('#relationship').val();
113         fieldset.find('.new_guarantor_relationship').first().val( guarantor_relationship );
114         $('#relationship').find('option:eq(0)').prop('selected', true);
115
116         fieldset.find('.guarantor-details').first().attr( 'data-borrowernumber', borrowernumber );
117
118         $('#guarantor_relationships').append( fieldset );
119         fieldset.show();
120
121         if ( relationship ) {
122             fieldset.find('.new_guarantor_relationship').val(relationship);
123         }
124     }
125
126     return 0;
127 }
128
129 function CalculateAge(dateofbirth) {
130     var today = new Date();
131     var dob = new Date(dateofbirth);
132     var age = {};
133
134     age.year = today.getFullYear() - dob.getFullYear();
135     age.month = today.getMonth() - dob.getMonth();
136     var day = today.getDate() - dob.getDate();
137
138     if(day < 0) {
139         age.month = parseInt(age.month) -1;
140     }
141
142     if(age.month < 0) {
143         age.year = parseInt(age.year) -1;
144         age.month = 12 + age.month;
145     }
146
147     return age;
148 }
149
150 function write_age() {
151     var hint = $("#dateofbirth_hint");
152     hint.html(dateformat);
153
154     var age = CalculateAge(document.form.dateofbirth.value);
155
156     if (!age.year && !age.month) {
157         return;
158     }
159
160     var age_string;
161     if (age.year || age.month) {
162         age_string = __("Age") + ": ";
163     }
164
165     if (age.year) {
166         age_string += age.year > 1 ? __("%s years").format(age.year) : __("%s year").format(age.year);
167         age_string += " ";
168     }
169
170     if (age.month) {
171         age_string += age.month > 1 ? __("%s months").format(age.month) : __("%s month").format(age.month);
172     }
173
174     hint.html(age_string);
175 }
176
177 $(document).ready(function(){
178     if($("#yesdebarred").is(":checked")){
179         $("#debarreduntil").show();
180     } else {
181         $("#debarreduntil").hide();
182     }
183     $("#yesdebarred,#nodebarred").change(function(){
184         if($("#yesdebarred").is(":checked")){
185             $("#debarreduntil").show();
186             $("#datedebarred").focus();
187         } else {
188             $("#debarreduntil").hide();
189         }
190     });
191     var mandatory_fields = $("input[name='BorrowerMandatoryField']").val().split ('|');
192     $(mandatory_fields).each(function(){
193         let input = $("[name='"+this+"']")
194         if ( input.hasClass('flatpickr') ) {
195             $(input).siblings('.flatpickr_wrapper').find('input.flatpickr').prop('required', true)
196         }
197         input.prop('required', true);
198     });
199
200     $("fieldset.rows input, fieldset.rows select").addClass("noEnterSubmit");
201
202     $('body').on('click', '#guarantor_search', function(e) {
203         e.preventDefault();
204         var newin = window.open('/cgi-bin/koha/members/search.pl?columns=cardnumber,name,category,branch,dateofbirth,address-library,action','popup','width=1024,height=768,resizable=no,toolbar=false,scrollbars=yes,top');
205     });
206
207     $('#guarantor_relationships').on('click', '.guarantor_cancel', function(e) {
208         e.preventDefault();
209         $(this).parents('fieldset').first().remove();
210     });
211
212     $(document.body).on('change','.select_city',function(){
213         var selected_city = $(this).val();
214         var addressfield = $(this).data("addressfield");
215         var myRegEx=new RegExp(/(.*)\|(.*)\|(.*)\|(.*)/);
216         var matches = selected_city.match( myRegEx );
217         $("#" + addressfield + "zipcode").val( matches[1] );
218         $("#" + addressfield + "city").val( matches[2] );
219         $("#" + addressfield + "state").val( matches[3] );
220         $("#" + addressfield + "country").val( matches[4] );
221     });
222
223     dateformat = $("#dateofbirth").siblings(".hint").first().html();
224
225     if( $('#dateofbirth').length ) {
226         write_age();
227     }
228
229     $.validator.addMethod(
230         "phone",
231         function(value, element, phone) {
232             let e164_re = /^((\+?|(0{2})?)?[1-9]{0,2})?\d{1,12}$/;
233             let has_plus = value.charAt(0) === '+';
234             value = value.replace(/\D/g,'');
235             if ( has_plus ) value = '+' + value;
236             element.value = value;
237
238             return this.optional(element) || e164_re.test(value);
239         },
240         jQuery.validator.messages.phone);
241
242     $("#entryform").validate({
243         rules: {
244             email: {
245                 email: true
246             },
247             emailpro: {
248                 email: true
249             },
250             B_email: {
251                 email: true
252             },
253             password: {
254                password_strong: true,
255                password_no_spaces: true
256             },
257             password2: {
258                password_match: true
259             },
260             SMSnumber: {
261                phone: true,
262             }
263         },
264         submitHandler: function(form) {
265             $("body, form input[type='submit'], form button[type='submit'], form a").addClass('waiting');
266             if (form.beenSubmitted)
267                 return false;
268             else
269                 form.beenSubmitted = true;
270                 form.submit();
271             }
272     });
273
274     var mrform = $("#manual_restriction_form");
275     var mrlink = $("#add_manual_restriction");
276     mrform.hide();
277     mrlink.on("click",function(e){
278         $(this).hide();
279         mrform.show();
280         e.preventDefault();
281     });
282
283     $("#cancel_manual_restriction").on("click",function(e){
284         $('#debarred_expiration').val('');
285         $('#add_debarment').val(0);
286         $('#debarred_comment').val('');
287         mrlink.show();
288         mrform.hide();
289         e.preventDefault();
290     });
291     $('#floating-save').css( { bottom: parseInt( $('#floating-save').css('bottom') ) + $('#changelanguage').height() + 'px' } );
292     $('#qa-save').css( {
293         bottom: parseInt( $('#qa-save').css('bottom') ) + $('#changelanguage').height() + 'px' ,
294         "background-color": "rgba(185, 216, 217, 0.6)",
295         "bottom": "3%",
296         "position": "fixed",
297         "right": "1%",
298         "width": "150px",
299     } );
300 });