Commit | Line | Data |
---|---|---|
55a9a4f4 ML |
1 | // http://civicrm.org/licensing |
2 | (function($) { | |
54be855c ML |
3 | |
4 | /** | |
222fa502 ML |
5 | * Adds the icons of enabled credit cards |
6 | * Handles clicking on a icon. | |
7 | * Changes the icon depending on the credit card number. | |
54be855c ML |
8 | * Removes spaces and dashes from credit card numbers. |
9 | */ | |
7d3c3387 SL |
10 | function civicrm_billingblock_creditcard_helper() { |
11 | $(function() { | |
12 | $.each(CRM.config.creditCardTypes, function(key, val) { | |
ce895597 | 13 | var html = '<a href="#" data-card_type=" + key + " title="' + val + '" class="crm-credit_card_type-icon-' + val.css_key + '"><span>' + val.label + '</span></a>'; |
7d3c3387 SL |
14 | $('.crm-credit_card_type-icons').append(html); |
15 | ||
ce895597 | 16 | $('.crm-credit_card_type-icon-' + val.css_key).click(function() { |
17 | $('#credit_card_type').val(key); | |
7d3c3387 | 18 | $('.crm-container .credit_card_type-section a').css('opacity', 0.25); |
ce895597 | 19 | $('.crm-container .credit_card_type-section .crm-credit_card_type-icon-' + val.css_key).css('opacity', 1); |
7d3c3387 SL |
20 | return false; |
21 | }); | |
55a9a4f4 ML |
22 | }); |
23 | ||
7d3c3387 SL |
24 | // Hide the CC type field (redundant) |
25 | $('#credit_card_type, .label', '.crm-container .credit_card_type-section').hide(); | |
54be855c | 26 | |
9b2e3ee6 MD |
27 | // set the card type value as default if any found |
28 | var cardtype = $('#credit_card_type').val(); | |
29 | if (cardtype) { | |
ce895597 | 30 | $.each(CRM.config.creditCardTypes, function(key, val) { |
9b2e3ee6 | 31 | // highlight the selected card type icon |
ce895597 | 32 | if (key === cardtype) { |
33 | $('.crm-container .credit_card_type-section .crm-credit_card_type-icon-' + val.css_key).css('opacity', 1); | |
9b2e3ee6 MD |
34 | } |
35 | else { | |
ce895597 | 36 | $('.crm-container .credit_card_type-section .crm-credit_card_type-icon-' + val.css_key).css('opacity', 0.25); |
9b2e3ee6 MD |
37 | } |
38 | }); | |
39 | } | |
40 | ||
7d3c3387 SL |
41 | // Select according to the number entered |
42 | $('.crm-container input#credit_card_number').change(function() { | |
43 | var ccnumber = cj(this).val(); | |
54be855c | 44 | |
7d3c3387 SL |
45 | // Remove spaces and dashes |
46 | ccnumber = ccnumber.replace(/[- ]/g, ''); | |
47 | cj(this).val(ccnumber); | |
54be855c | 48 | |
7d3c3387 SL |
49 | // Semi-hide all images, we will un-hide the right one afterwards |
50 | $('.crm-container .credit_card_type-section a').css('opacity', 0.25); | |
51 | $('#credit_card_type').val(''); | |
54be855c | 52 | |
7d3c3387 SL |
53 | civicrm_billingblock_set_card_type(ccnumber); |
54 | }); | |
55a9a4f4 | 55 | }); |
7d3c3387 | 56 | } |
55a9a4f4 | 57 | |
54be855c | 58 | function civicrm_billingblock_set_card_type(ccnumber) { |
9b2e3ee6 | 59 | var card_values = CRM.config.creditCardTypes; |
ce895597 | 60 | $.each(card_values, function(key, spec) { |
61 | if (ccnumber.match('^' + spec.pattern + '$')) { | |
62 | $('.crm-container .credit_card_type-section .crm-credit_card_type-icon-' + spec.css_key).css('opacity', 1); | |
63 | $('select#credit_card_type').val(key); | |
64 | return false; | |
54be855c ML |
65 | } |
66 | }); | |
67 | } | |
bef9421f CW |
68 | |
69 | civicrm_billingblock_creditcard_helper(); | |
70 | ||
3cc60a06 | 71 | })(CRM.$); |