CRM-12515: improving the credit card type in contribution forms.
[civicrm-core.git] / templates / CRM / Core / BillingBlock.js
1 // http://civicrm.org/licensing
2 (function($) {
3 function civicrm_billingblock_add_cc_logos() {
4 $.each(CRM.config.creditCardTypes, function(key, val) {
5 var html = '<a href="#" title="' + val + '" class="crm-credit_card_type-logo-' + key + '"><span>' + val + '</span></a>';
6 $('.crm-credit_card_type-logos').append(html);
7
8 cj('.crm-credit_card_type-logo-' + key).click(function() {
9 cj('#crm-container .credit_card_type-section #credit_card_type').val(val);
10 cj('#crm-container .credit_card_type-section a').css('opacity', 0.4);
11 cj('#crm-container .credit_card_type-section .crm-credit_card_type-logo-' + key).css('opacity', 1);
12 return false;
13 });
14
15 // Hide the CC type field (redundant)
16 $('#crm-container .credit_card_type-section select#credit_card_type').hide();
17 $('#crm-container .credit_card_type-section .label').hide();
18
19 // Select according to the number entered
20 cj('#crm-container input#credit_card_number').change(function() {
21 var ccnumtype = cj(this).val().substr(0, 4);
22
23 // Semi-hide all images, we will un-hide the right one afterwards
24 cj('#crm-container .credit_card_type-section a').css('opacity', 0.4);
25 cj('#credit_card_type').val('');
26
27 // https://en.wikipedia.org/wiki/Credit_card_numbers
28 if (ccnumtype.substr(0, 1) == '3') {
29 cj('#credit_card_type').val('Amex');
30 cj('#crm-container .credit_card_type-section .crm-credit_card_type-logo-visa').css('opacity', 1);
31 }
32 else if (ccnumtype.substr(0, 2) >= '51' && ccnumtype.substr(0, 2) <= '55') {
33 cj('#credit_card_type').val('MasterCard');
34 cj('#crm-container .credit_card_type-section .crm-credit_card_type-logo-mastercard').css('opacity', 1);
35 }
36 else if (ccnumtype.substr(0, 1) == '4') {
37 cj('#credit_card_type').val('Visa');
38 cj('#crm-container .credit_card_type-section .crm-credit_card_type-logo-visa').css('opacity', 1);
39 }
40 });
41 });
42 }
43
44 civicrm_billingblock_add_cc_logos();
45
46 $('#crm-container').on('crmFormLoad', '*', function() {
47 civicrm_billingblock_add_cc_logos();
48 });
49 })(cj);