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