Bug 22862: Normalize SMS messaging numbers before validating them
[koha-equinox.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 Dopopguarantor(link) {
133     var newin=window.open(link,'popup','width=800,height=500,resizable=no,toolbar=false,scrollbars=yes,top');
134 }
135
136 function clear_entry(node) {
137     var original = $(node).parent();
138     $("textarea", original).attr('value', '');
139     $("select", original).attr('value', '');
140 }
141
142 function clone_entry(node) {
143     var original = $(node).parent();
144     var clone = original.clone();
145
146     var newId = 50 + parseInt(Math.random() * 100000);
147     $("input,select,textarea", clone).attr('id', function() {
148         return this.id.replace(/patron_attr_\d+/, 'patron_attr_' + newId);
149     });
150     $("input,select,textarea", clone).attr('name', function() {
151         return this.name.replace(/patron_attr_\d+/, 'patron_attr_' + newId);
152     });
153     $("label", clone).attr('for', function() {
154         return $(this).attr("for").replace(/patron_attr_\d+/, 'patron_attr_' + newId);
155     });
156     $("input#patron_attr_" + newId, clone).attr('value','');
157     $("select#patron_attr_" + newId, clone).attr('value','');
158     $(original).after(clone);
159     return false;
160 }
161
162 function update_category_code(category_code) {
163     if ( $(category_code).is("select") ) {
164         category_code = $("#categorycode_entry").find("option:selected").val();
165     }
166     var mytables = $(".attributes_table");
167     $(mytables).find("li").hide();
168     $(mytables).find(" li[data-category_code='"+category_code+"']").show();
169     $(mytables).find(" li[data-category_code='']").show();
170 }
171
172 function select_user(borrowernumber, borrower) {
173     var form = $('#entryform').get(0);
174     if (form.guarantorid.value) {
175         $("#contact-details, #quick_add_form #contact-details").find('a').remove();
176         $("#contactname, #contactfirstname, #quick_add_form #contactname, #quick_add_form #contactfirstname").parent().find('span').remove();
177     }
178
179     var id = borrower.borrowernumber;
180     form.guarantorid.value = id;
181     $('#contact-details, #quick_add_form #contact-details')
182         .show()
183         .find('span')
184         .after('<a target="blank" href="/cgi-bin/koha/members/moremember.pl?borrowernumber=' + id + '">' + id + '</a>');
185
186     $(form.contactname)
187         .val(borrower.surname)
188         .before('<span>' + borrower.surname + '</span>').get(0).type = 'hidden';
189     $("#quick_add_form #contactname").val(borrower.surname).before('<span>'+borrower.surname+'</span.').attr({type:"hidden"});
190     $(form.contactfirstname,"#quick_add_form #contactfirstname")
191         .val(borrower.firstname)
192         .before('<span>' + borrower.firstname + '</span>').get(0).type = 'hidden';
193     $("#quick_add_form #contactfirstname").val(borrower.firstname).before('<span>'+borrower.firstname+'</span.').attr({type:"hidden"});
194
195     form.streetnumber.value = borrower.streetnumber;
196     form.address.value = borrower.address;
197     form.address2.value = borrower.address2;
198     form.city.value = borrower.city;
199     form.state.value = borrower.state;
200     form.zipcode.value = borrower.zipcode;
201     form.country.value = borrower.country;
202     form.branchcode.value = borrower.branchcode;
203
204     $("#quick_add_form #streetnumber").val(borrower.streetnumber);
205     $("#quick_add_form #address").val(borrower.address);
206     $("#quick_add_form #address2").val(borrower.address2);
207     $("#quick_add_form #city").val(borrower.city);
208     $("#quick_add_form #state").val(borrower.state);
209     $("#quick_add_form #zipcode").val(borrower.zipcode);
210     $("#quick_add_form #country").val(borrower.country);
211     $("#quick_add_form select[name='branchcode']").val(borrower.branchcode);
212
213     form.guarantorsearch.value = LABEL_CHANGE;
214     $("#quick_add_form #guarantorsearch").val(LABEL_CHANGE);
215
216     return 0;
217 }
218
219 function CalculateAge(dateofbirth) {
220     var today = new Date();
221     var dob = Date_from_syspref(dateofbirth);
222     var age = {};
223
224     age.year = today.getFullYear() - dob.getFullYear();
225     age.month = today.getMonth() - dob.getMonth();
226     var day = today.getDate() - dob.getDate();
227
228     if(day < 0) {
229         age.month = parseInt(age.month) -1;
230     }
231
232     if(age.month < 0) {
233         age.year = parseInt(age.year) -1;
234         age.month = 12 + age.month;
235     }
236
237     return age;
238 }
239
240 function write_age() {
241     var hint = $("#dateofbirth").siblings(".hint").first();
242     hint.html(dateformat);
243
244     var age = CalculateAge(document.form.dateofbirth.value);
245
246     if (!age.year && !age.month) {
247         return;
248     }
249
250     var age_string;
251     if (age.year || age.month) {
252         age_string = LABEL_AGE + ": ";
253     }
254
255     if (age.year) {
256         age_string += age.year > 1 ? MSG_YEARS.format(age.year) : MSG_YEAR.format(age.year);
257         age_string += " ";
258     }
259
260     if (age.month) {
261         age_string += age.month > 1 ? MSG_MONTHS.format(age.month) : MSG_MONTH.format(age.month);
262     }
263
264     hint.html(age_string);
265 }
266
267 $(document).ready(function(){
268     if($("#yesdebarred").is(":checked")){
269         $("#debarreduntil").show();
270     } else {
271         $("#debarreduntil").hide();
272     }
273     $("#yesdebarred,#nodebarred").change(function(){
274         if($("#yesdebarred").is(":checked")){
275             $("#debarreduntil").show();
276             $("#datedebarred").focus();
277         } else {
278             $("#debarreduntil").hide();
279         }
280     });
281     var mandatory_fields = $("input[name='BorrowerMandatoryField']").val().split ('|');
282     $(mandatory_fields).each(function(){
283         $("[name='"+this+"']").attr('required', 'required');
284     });
285
286     $("fieldset.rows input, fieldset.rows select").addClass("noEnterSubmit");
287     $("#guarantordelete").click(function() {
288         $("#quick_add_form #contact-details, #contact-details").hide().find('a').remove();
289         $("#quick_add_form #guarantorid, #quick_add_form  #contactname, #quick_add_form #contactfirstname, #guarantorid, #contactname, #contactfirstname").each(function () { this.value = ""; });
290         $("#quick_add_form #contactname, #quick_add_form #contactfirstname, #contactname, #contactfirstname")
291             .each(function () { this.type = 'text'; })
292             .parent().find('span').remove();
293         $("#quick_add_form #guarantorsearch, #guarantorsearch").val(LABEL_SET_TO_PATRON);
294     });
295
296     $(document.body).on('change','.select_city',function(){
297         var selected_city = $(this).val();
298         var addressfield = $(this).data("addressfield");
299         var myRegEx=new RegExp(/(.*)\|(.*)\|(.*)\|(.*)/);
300         var matches = selected_city.match( myRegEx );
301         $("#" + addressfield + "zipcode").val( matches[1] );
302         $("#" + addressfield + "city").val( matches[2] );
303         $("#" + addressfield + "state").val( matches[3] );
304         $("#" + addressfield + "country").val( matches[4] );
305     });
306
307     dateformat = $("#dateofbirth").siblings(".hint").first().html();
308
309     if( $('#dateofbirth').length ) {
310         write_age();
311     }
312
313     $.validator.addMethod(
314         "phone",
315         function(value, element, phone) {
316             var e164 = "^\\+?[1-9]\\d{1,14}$";
317             var re = new RegExp(e164);
318
319             let has_plus = value.charAt(0) === '+';
320             value = value.replace(/\D/g,'');
321             if ( has_plus ) value = '+' + value;
322             element.value = value;
323
324             return this.optional(element) || re.test(value);
325         },
326         jQuery.validator.messages.phone);
327
328     $("#entryform").validate({
329         rules: {
330             email: {
331                 email: true
332             },
333             emailpro: {
334                 email: true
335             },
336             B_email: {
337                 email: true
338             },
339             password: {
340                password_strong: true,
341                password_no_spaces: true
342             },
343             password2: {
344                password_match: true
345             },
346             SMSnumber: {
347                phone: true,
348             }
349         },
350         submitHandler: function(form) {
351             $("body, form input[type='submit'], form button[type='submit'], form a").addClass('waiting');
352             if (form.beenSubmitted)
353                 return false;
354             else
355                 form.beenSubmitted = true;
356                 form.submit();
357             }
358     });
359
360     var mrform = $("#manual_restriction_form");
361     var mrlink = $("#add_manual_restriction");
362     mrform.hide();
363     mrlink.on("click",function(e){
364         $(this).hide();
365         mrform.show();
366         e.preventDefault();
367     });
368
369     $("#cancel_manual_restriction").on("click",function(e){
370         $('#debarred_expiration').val('');
371         $('#add_debarment').val(0);
372         $('#debarred_comment').val('');
373         mrlink.show();
374         mrform.hide();
375         e.preventDefault();
376     });
377     $('#floating-save').css( { bottom: parseInt( $('#floating-save').css('bottom') ) + $('#changelanguage').height() + 'px' } );
378     $('#qa-save').css( {
379         bottom: parseInt( $('#qa-save').css('bottom') ) + $('#changelanguage').height() + 'px' ,
380         "background-color": "rgba(185, 216, 217, 0.6)",
381         "bottom": "3%",
382         "position": "fixed",
383         "right": "1%",
384         "width": "150px",
385     } );
386 });