Bug 28802: Untranslatable strings in browser.js
[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+= MSG_DUPLICATE_SUSPICION;
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).attr('value', '');
40     $("select", original).attr('value', '');
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 = MSG_PASSWORD_LENGTH.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=${borrower.borrowernumber}]`).length;
81
82     if ( is_guarantor ) {
83         alert("Patron is already a guarantor for this patron");
84     } else {
85         $('#guarantor_id').val(borrower.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', borrower.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 = Date_from_syspref(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").siblings(".hint").first();
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 = LABEL_AGE + ": ";
163     }
164
165     if (age.year) {
166         age_string += age.year > 1 ? MSG_YEARS.format(age.year) : MSG_YEAR.format(age.year);
167         age_string += " ";
168     }
169
170     if (age.month) {
171         age_string += age.month > 1 ? MSG_MONTHS.format(age.month) : MSG_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         $("[name='"+this+"']").attr('required', 'required');
194     });
195
196     $("fieldset.rows input, fieldset.rows select").addClass("noEnterSubmit");
197
198     $('body').on('click', '#guarantor_search', function(e) {
199         e.preventDefault();
200         var newin = window.open('guarantor_search.pl','popup','width=800,height=600,resizable=no,toolbar=false,scrollbars=yes,top');
201     });
202
203     $('#guarantor_relationships').on('click', '.guarantor_cancel', function(e) {
204         e.preventDefault();
205         $(this).parents('fieldset').first().remove();
206     });
207
208     $(document.body).on('change','.select_city',function(){
209         var selected_city = $(this).val();
210         var addressfield = $(this).data("addressfield");
211         var myRegEx=new RegExp(/(.*)\|(.*)\|(.*)\|(.*)/);
212         var matches = selected_city.match( myRegEx );
213         $("#" + addressfield + "zipcode").val( matches[1] );
214         $("#" + addressfield + "city").val( matches[2] );
215         $("#" + addressfield + "state").val( matches[3] );
216         $("#" + addressfield + "country").val( matches[4] );
217     });
218
219     dateformat = $("#dateofbirth").siblings(".hint").first().html();
220
221     if( $('#dateofbirth').length ) {
222         write_age();
223     }
224
225     $.validator.addMethod(
226         "phone",
227         function(value, element, phone) {
228             let e164_re = /^(\+[1-9]\d{0,2})?\d{1,12}$/;
229             let has_plus = value.charAt(0) === '+';
230             value = value.replace(/\D/g,'');
231             if ( has_plus ) value = '+' + value;
232             element.value = value;
233
234             return this.optional(element) || e164_re.test(value);
235         },
236         jQuery.validator.messages.phone);
237
238     $("#entryform").validate({
239         rules: {
240             email: {
241                 email: true
242             },
243             emailpro: {
244                 email: true
245             },
246             B_email: {
247                 email: true
248             },
249             password: {
250                password_strong: true,
251                password_no_spaces: true
252             },
253             password2: {
254                password_match: true
255             },
256             SMSnumber: {
257                phone: true,
258             }
259         },
260         submitHandler: function(form) {
261             $("body, form input[type='submit'], form button[type='submit'], form a").addClass('waiting');
262             if (form.beenSubmitted)
263                 return false;
264             else
265                 form.beenSubmitted = true;
266                 form.submit();
267             }
268     });
269
270     var mrform = $("#manual_restriction_form");
271     var mrlink = $("#add_manual_restriction");
272     mrform.hide();
273     mrlink.on("click",function(e){
274         $(this).hide();
275         mrform.show();
276         e.preventDefault();
277     });
278
279     $("#cancel_manual_restriction").on("click",function(e){
280         $('#debarred_expiration').val('');
281         $('#add_debarment').val(0);
282         $('#debarred_comment').val('');
283         mrlink.show();
284         mrform.hide();
285         e.preventDefault();
286     });
287     $('#floating-save').css( { bottom: parseInt( $('#floating-save').css('bottom') ) + $('#changelanguage').height() + 'px' } );
288     $('#qa-save').css( {
289         bottom: parseInt( $('#qa-save').css('bottom') ) + $('#changelanguage').height() + 'px' ,
290         "background-color": "rgba(185, 216, 217, 0.6)",
291         "bottom": "3%",
292         "position": "fixed",
293         "right": "1%",
294         "width": "150px",
295     } );
296 });