Bug 26597: Transfer informations from guarantor when adding a guarantor to an existin...
[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         if(typeof guarantor_attributes !== 'undefined') {
126             $.ajax({
127                 url: '/api/v1/patrons/'+borrowernumber,
128                 method: "GET",
129                 dataType: 'json',
130                 success: function(data){
131                     for (var i = 0; i < parseInt(guarantor_attributes.length, 10); i++) {
132                         var attribute = guarantor_attributes[i];
133                         if ( data[attribute] != null && document.forms.entryform[attribute].value == "" ) {
134                             document.forms.entryform[attribute].value = data[attribute];
135                         }
136                     }
137                 }
138             });
139         }
140     }
141
142     return 0;
143 }
144
145 function CalculateAge(dateofbirth) {
146     var today = new Date();
147     var dob = new Date(dateofbirth);
148     var age = {};
149
150     age.year = today.getFullYear() - dob.getFullYear();
151     age.month = today.getMonth() - dob.getMonth();
152     var day = today.getDate() - dob.getDate();
153
154     if(day < 0) {
155         age.month = parseInt(age.month) -1;
156     }
157
158     if(age.month < 0) {
159         age.year = parseInt(age.year) -1;
160         age.month = 12 + age.month;
161     }
162
163     return age;
164 }
165
166 function write_age() {
167     var hint = $("#dateofbirth_hint");
168     hint.html(dateformat);
169
170     var age = CalculateAge(document.form.dateofbirth.value);
171
172     if (!age.year && !age.month) {
173         return;
174     }
175
176     var age_string;
177     if (age.year || age.month) {
178         age_string = __("Age") + ": ";
179     }
180
181     if (age.year) {
182         age_string += age.year > 1 ? __("%s years").format(age.year) : __("%s year").format(age.year);
183         age_string += " ";
184     }
185
186     if (age.month) {
187         age_string += age.month > 1 ? __("%s months").format(age.month) : __("%s month").format(age.month);
188     }
189
190     hint.html(age_string);
191 }
192
193 $(document).ready(function(){
194     if($("#yesdebarred").is(":checked")){
195         $("#debarreduntil").show();
196     } else {
197         $("#debarreduntil").hide();
198     }
199     $("#yesdebarred,#nodebarred").change(function(){
200         if($("#yesdebarred").is(":checked")){
201             $("#debarreduntil").show();
202             $("#datedebarred").focus();
203         } else {
204             $("#debarreduntil").hide();
205         }
206     });
207     var mandatory_fields = $("input[name='BorrowerMandatoryField']").val().split ('|');
208     $(mandatory_fields).each(function(){
209         let input = $("[name='"+this+"']")
210         if ( input.hasClass('flatpickr') ) {
211             $(input).siblings('.flatpickr_wrapper').find('input.flatpickr').prop('required', true)
212         }
213         input.prop('required', true);
214     });
215
216     $("fieldset.rows input, fieldset.rows select").addClass("noEnterSubmit");
217
218     $('#guarantor_relationships').on('click', '.guarantor_cancel', function(e) {
219         e.preventDefault();
220         $(this).parents('fieldset').first().remove();
221     });
222
223     $(document.body).on('change','.select_city',function(){
224         var selected_city = $(this).val();
225         var addressfield = $(this).data("addressfield");
226         var myRegEx=new RegExp(/(.*)\|(.*)\|(.*)\|(.*)/);
227         var matches = selected_city.match( myRegEx );
228         $("#" + addressfield + "zipcode").val( matches[1] );
229         $("#" + addressfield + "city").val( matches[2] );
230         $("#" + addressfield + "state").val( matches[3] );
231         $("#" + addressfield + "country").val( matches[4] );
232     });
233
234     dateformat = $("#dateofbirth").siblings(".hint").first().html();
235
236     if( $('#dateofbirth').length ) {
237         write_age();
238     }
239
240     $.validator.addMethod(
241         "phone",
242         function(value, element, phone) {
243             let e164_re = /^((\+?|(0{2})?)?[1-9]{0,2})?\d{1,12}$/;
244             let has_plus = value.charAt(0) === '+';
245             value = value.replace(/\D/g,'');
246             if ( has_plus ) value = '+' + value;
247             element.value = value;
248
249             return this.optional(element) || e164_re.test(value);
250         },
251         jQuery.validator.messages.phone);
252
253     $("#entryform").validate({
254         rules: {
255             email: {
256                 email: true
257             },
258             emailpro: {
259                 email: true
260             },
261             B_email: {
262                 email: true
263             },
264             password: {
265                password_strong: true,
266                password_no_spaces: true
267             },
268             password2: {
269                password_match: true
270             },
271             SMSnumber: {
272                phone: true,
273             }
274         },
275         submitHandler: function(form) {
276             $("body, form input[type='submit'], form button[type='submit'], form a").addClass('waiting');
277             if (form.beenSubmitted)
278                 return false;
279             else
280                 form.beenSubmitted = true;
281                 form.submit();
282             }
283     });
284
285     var mrform = $("#manual_restriction_form");
286     var mrlink = $("#add_manual_restriction");
287     mrform.hide();
288     mrlink.on("click",function(e){
289         $(this).hide();
290         mrform.show();
291         e.preventDefault();
292     });
293
294     $("#cancel_manual_restriction").on("click",function(e){
295         $('#debarred_expiration').val('');
296         $('#add_debarment').val(0);
297         $('#debarred_comment').val('');
298         mrlink.show();
299         mrform.hide();
300         e.preventDefault();
301     });
302     $('#floating-save').css( { bottom: parseInt( $('#floating-save').css('bottom') ) + $('#changelanguage').height() + 'px' } );
303     $('#qa-save').css( {
304         bottom: parseInt( $('#qa-save').css('bottom') ) + $('#changelanguage').height() + 'px' ,
305         "background-color": "rgba(185, 216, 217, 0.6)",
306         "bottom": "3%",
307         "position": "fixed",
308         "right": "1%",
309         "width": "150px",
310     } );
311 });