Bug 23905: Button "Search to add" doesn't work on Quick add new patron
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / members.js
1 // this function checks id date is like DD/MM/YYYY
2 function CheckDate(field) {
3 var d = field.value;
4 if (d!=="") {
5       var amin = 1900;
6       var amax = 2100;
7       var date = d.split("/");
8       var ok=1;
9       var msg;
10       if ( (date.length < 2) && (ok==1) ) {
11         msg = MSG_SEPARATOR.format(field.name);
12         alert(msg); ok=0; field.focus();
13         return;
14       }
15       var dd   = date[0];
16       var mm   = date[1];
17       var yyyy = date[2];
18       // checking days
19       if ( ((isNaN(dd))||(dd<1)||(dd>31)) && (ok==1) ) {
20         msg = MSG_INCORRECT_DAY.format(field.name);
21         alert(msg); ok=0; field.focus();
22         return false;
23       }
24       // checking months
25       if ( ((isNaN(mm))||(mm<1)||(mm>12)) && (ok==1) ) {
26         msg = MSG_INCORRECT_MONTH.format(field.name);
27         alert(msg); ok=0; field.focus();
28         return false;
29       }
30       // checking years
31       if ( ((isNaN(yyyy))||(yyyy<amin)||(yyyy>amax)) && (ok==1) ) {
32         msg = MSG_INCORRECT_YEAR.format(field.name);
33         alert(msg); ok=0; field.focus();
34         return false;
35       }
36    }
37 }
38
39 //function test if member is unique and if it's right the member is registred
40 function unique() {
41 var msg1;
42 var msg2;
43 if (  document.form.check_member.value==1){
44     if (document.form.categorycode.value != "I"){
45
46         msg1 += MSG_DUPLICATE_PATRON;
47         alert(msg1);
48     check_form_borrowers(0);
49     document.form.submit();
50
51     }else{
52         msg2 += MSG_DUPLICATE_ORGANIZATION;
53         alert(msg2);
54     check_form_borrowers(0);
55     }
56 }
57 else
58 {
59     document.form.submit();
60 }
61
62 }
63 //end function
64 //function test if date enrooled < date expiry
65 // WARNING: format-specific test.
66 function check_manip_date(status) {
67 if (status=='verify'){
68 // this part of function('verify') is used to check if dateenrolled<date expiry
69 if (document.form.dateenrolled !== '' && document.form.dateexpiry.value !=='') {
70 var myDate1=document.form.dateenrolled.value.split ('/');
71 var myDate2=document.form.dateexpiry.value.split ('/');
72     if ((myDate1[2]>myDate2[2])||(myDate1[2]==myDate2[2] && myDate1[1]>myDate2[1])||(myDate1[2]==myDate2[2] && myDate1[1]>=myDate2[1] && myDate1[0]>=myDate2[0]))
73
74         {
75         document.form.dateenrolled.focus();
76         var msg = MSG_LATE_EXPIRY;
77         alert(msg);
78         }
79     }
80     }
81 }
82 //end function
83
84 function check_password( password ) {
85     if ( password.match(/^\s/) || password.match(/\s$/)) {
86         return false;
87     }
88     return true;
89 }
90
91 // function to test all fields in forms and nav in different forms(1 ,2 or 3)
92 function check_form_borrowers(nav){
93     var statut=0;
94     var message = "";
95     var message_champ="";
96     if (document.form.check_member.value == 1 )
97     {
98         if (document.form.answernodouble) {
99             if( (!(document.form.answernodouble.checked))){
100                 document.form.nodouble.value=0;
101             } else {
102                 document.form.nodouble.value=1;
103             }
104         }
105     }
106
107     //patrons form to test if you checked no to the question of double
108     if (statut!=1 && document.form.check_member.value > 0 ) {
109         if (!(document.form.answernodouble.checked)){
110             message_champ+= MSG_DUPLICATE_SUSPICION;
111             statut=1;
112             document.form.nodouble.value=0;
113         } else {
114             document.form.nodouble.value=1;
115         }
116     }
117
118     if (statut==1){
119         //alert if at least 1 error
120         alert(message+"\n"+message_champ);
121         return false;
122     } else {
123         return true;
124     }
125 }
126
127 function Dopop(link) {
128 // //   var searchstring=document.form.value[i].value;
129     var newin=window.open(link,'popup','width=600,height=400,resizable=no,toolbar=false,scrollbars=no,top');
130 }
131
132 function clear_entry(node) {
133     var original = $(node).parent();
134     $("textarea", original).attr('value', '');
135     $("select", original).attr('value', '');
136 }
137
138 function clone_entry(node) {
139     var original = $(node).parent();
140     var clone = original.clone();
141
142     var newId = 50 + parseInt(Math.random() * 100000);
143     $("input,select,textarea", clone).attr('id', function() {
144         return this.id.replace(/patron_attr_\d+/, 'patron_attr_' + newId);
145     });
146     $("input,select,textarea", clone).attr('name', function() {
147         return this.name.replace(/patron_attr_\d+/, 'patron_attr_' + newId);
148     });
149     $("label", clone).attr('for', function() {
150         return $(this).attr("for").replace(/patron_attr_\d+/, 'patron_attr_' + newId);
151     });
152     $("input#patron_attr_" + newId, clone).attr('value','');
153     $("select#patron_attr_" + newId, clone).attr('value','');
154     $(original).after(clone);
155     return false;
156 }
157
158 function update_category_code(category_code) {
159     if ( $(category_code).is("select") ) {
160         category_code = $("#categorycode_entry").find("option:selected").val();
161     }
162     var mytables = $(".attributes_table");
163     $(mytables).find("li").hide();
164     $(mytables).find(" li[data-category_code='"+category_code+"']").show();
165     $(mytables).find(" li[data-category_code='']").show();
166 }
167
168 function select_user(borrowernumber, borrower) {
169     let is_guarantor = $(`.guarantor-details[data-borrowernumber=${borrower.borrowernumber}]`).length;
170     console.log("LEN: "  + is_guarantor );
171
172     if ( is_guarantor ) {
173         alert("Patron is already a guarantor for this patron");
174     } else {
175         $('#guarantor_id').val(borrower.borrowernumber);
176         $('#guarantor_surname').val(borrower.surname);
177         $('#guarantor_firstname').val(borrower.firstname);
178
179         var fieldset = $('#guarantor_template').clone();
180         fieldset.removeAttr('id');
181
182         var guarantor_id = $('#guarantor_id').val();
183         if ( guarantor_id ) {
184             fieldset.find('.new_guarantor_id').first().val( guarantor_id );
185             fieldset.find('.new_guarantor_id_text').first().text( guarantor_id );
186         } else {
187             fieldset.find('.guarantor_id').first().hide();
188         }
189         $('#guarantor_id').val("");
190
191         var guarantor_surname = $('#guarantor_surname').val();
192         fieldset.find('.new_guarantor_surname').first().val( guarantor_surname );
193         fieldset.find('.new_guarantor_surname_text').first().text( guarantor_surname );
194         $('#guarantor_surname').val("");
195
196         var guarantor_firstname = $('#guarantor_firstname').val();
197         fieldset.find('.new_guarantor_firstname').first().val( guarantor_firstname );
198         fieldset.find('.new_guarantor_firstname_text').first().text( guarantor_firstname );
199         $('#guarantor_firstname').val("");
200
201         var guarantor_relationship = $('#relationship').val();
202         fieldset.find('.new_guarantor_relationship').first().val( guarantor_relationship );
203         $('#relationship').find('option:eq(0)').prop('selected', true);
204
205         fieldset.find('.guarantor-details').first().attr( 'data-borrowernumber', borrower.borrowernumber );
206         console.log( fieldset.find('.guarantor-details').first().data() );
207
208         $('#guarantor_relationships').append( fieldset );
209         fieldset.show();
210     }
211
212     return 0;
213 }
214
215 function CalculateAge(dateofbirth) {
216     var today = new Date();
217     var dob = Date_from_syspref(dateofbirth);
218     var age = {};
219
220     age.year = today.getFullYear() - dob.getFullYear();
221     age.month = today.getMonth() - dob.getMonth();
222     var day = today.getDate() - dob.getDate();
223
224     if(day < 0) {
225         age.month = parseInt(age.month) -1;
226     }
227
228     if(age.month < 0) {
229         age.year = parseInt(age.year) -1;
230         age.month = 12 + age.month;
231     }
232
233     return age;
234 }
235
236 function write_age() {
237     var hint = $("#dateofbirth").siblings(".hint").first();
238     hint.html(dateformat);
239
240     var age = CalculateAge(document.form.dateofbirth.value);
241
242     if (!age.year && !age.month) {
243         return;
244     }
245
246     var age_string;
247     if (age.year || age.month) {
248         age_string = LABEL_AGE + ": ";
249     }
250
251     if (age.year) {
252         age_string += age.year > 1 ? MSG_YEARS.format(age.year) : MSG_YEAR.format(age.year);
253         age_string += " ";
254     }
255
256     if (age.month) {
257         age_string += age.month > 1 ? MSG_MONTHS.format(age.month) : MSG_MONTH.format(age.month);
258     }
259
260     hint.html(age_string);
261 }
262
263 $(document).ready(function(){
264     if($("#yesdebarred").is(":checked")){
265         $("#debarreduntil").show();
266     } else {
267         $("#debarreduntil").hide();
268     }
269     $("#yesdebarred,#nodebarred").change(function(){
270         if($("#yesdebarred").is(":checked")){
271             $("#debarreduntil").show();
272             $("#datedebarred").focus();
273         } else {
274             $("#debarreduntil").hide();
275         }
276     });
277     var mandatory_fields = $("input[name='BorrowerMandatoryField']").val().split ('|');
278     $(mandatory_fields).each(function(){
279         $("[name='"+this+"']").attr('required', 'required');
280     });
281
282     $("fieldset.rows input, fieldset.rows select").addClass("noEnterSubmit");
283
284     $('body').on('click', '#guarantor_search', function(e) {
285         e.preventDefault();
286         var newin = window.open('guarantor_search.pl','popup','width=600,height=400,resizable=no,toolbar=false,scrollbars=yes,top');
287     });
288
289     $('#guarantor_relationships').on('click', '.guarantor_cancel', function(e) {
290         e.preventDefault();
291         $(this).parents('fieldset').first().remove();
292     });
293
294     $(document.body).on('change','.select_city',function(){
295         var selected_city = $(this).val();
296         var addressfield = $(this).data("addressfield");
297         var myRegEx=new RegExp(/(.*)\|(.*)\|(.*)\|(.*)/);
298         var matches = selected_city.match( myRegEx );
299         $("#" + addressfield + "zipcode").val( matches[1] );
300         $("#" + addressfield + "city").val( matches[2] );
301         $("#" + addressfield + "state").val( matches[3] );
302         $("#" + addressfield + "country").val( matches[4] );
303     });
304
305     dateformat = $("#dateofbirth").siblings(".hint").first().html();
306
307     if( $('#dateofbirth').length ) {
308         write_age();
309     }
310
311     $.validator.addMethod(
312         "phone",
313         function(value, element, phone) {
314             var e164 = "^\\+?[1-9]\\d{1,14}$";
315             var re = new RegExp(e164);
316
317             let has_plus = value.charAt(0) === '+';
318             value = value.replace(/\D/g,'');
319             if ( has_plus ) value = '+' + value;
320             element.value = value;
321
322             return this.optional(element) || re.test(value);
323         },
324         jQuery.validator.messages.phone);
325
326     $("#entryform").validate({
327         rules: {
328             email: {
329                 email: true
330             },
331             emailpro: {
332                 email: true
333             },
334             B_email: {
335                 email: true
336             },
337             password: {
338                password_strong: true,
339                password_no_spaces: true
340             },
341             password2: {
342                password_match: true
343             },
344             SMSnumber: {
345                phone: true,
346             }
347         },
348         submitHandler: function(form) {
349             $("body, form input[type='submit'], form button[type='submit'], form a").addClass('waiting');
350             if (form.beenSubmitted)
351                 return false;
352             else
353                 form.beenSubmitted = true;
354                 form.submit();
355             }
356     });
357
358     var mrform = $("#manual_restriction_form");
359     var mrlink = $("#add_manual_restriction");
360     mrform.hide();
361     mrlink.on("click",function(e){
362         $(this).hide();
363         mrform.show();
364         e.preventDefault();
365     });
366
367     $("#cancel_manual_restriction").on("click",function(e){
368         $('#debarred_expiration').val('');
369         $('#add_debarment').val(0);
370         $('#debarred_comment').val('');
371         mrlink.show();
372         mrform.hide();
373         e.preventDefault();
374     });
375     $('#floating-save').css( { bottom: parseInt( $('#floating-save').css('bottom') ) + $('#changelanguage').height() + 'px' } );
376     $('#qa-save').css( {
377         bottom: parseInt( $('#qa-save').css('bottom') ) + $('#changelanguage').height() + 'px' ,
378         "background-color": "rgba(185, 216, 217, 0.6)",
379         "bottom": "3%",
380         "position": "fixed",
381         "right": "1%",
382         "width": "150px",
383     } );
384 });