-- CRM-12653 soft credit amount field in batch entry grid
[civicrm-core.git] / templates / CRM / Contribute / Form / SoftCredit.js
1 // http://civicrm.org/licensing
2 cj(function($) {
3 $('#showPCP, #showSoftCredit').click(function(){
4 return showHideSoftCreditAndPCP();
5 });
6
7 function showHideSoftCreditAndPCP() {
8 $('.crm-contribution-pcp-block').toggle();
9 $('.crm-contribution-pcp-block-link').toggle();
10 $('.crm-contribution-form-block-soft_credit_to').toggle();
11 return false;
12 }
13
14 $('#addMoreSoftCredit').click(function(){
15 $('.crm-soft-credit-block tr.hiddenElement :first').show().removeClass('hiddenElement');
16 if ( $('.crm-soft-credit-block tr.hiddenElement').length < 1 ) {
17 $('#addMoreSoftCredit').hide();
18 }
19 return false;
20 });
21
22 var pcpURL = CRM.url('civicrm/ajax/rest',
23 'className=CRM_Contact_Page_AJAX&fnName=getPCPList&json=1&context=contact&reset=1');
24 $('#pcp_made_through').autocomplete(pcpURL,
25 { width : 360, selectFirst : false, matchContains: true
26 }).result( function(event, data, formatted) {
27 $("#pcp_made_through_id" ).val( data[1]);
28 });
29
30 var rowCnt = 1;
31 $('input[name^="soft_credit_contact_select_id["]').each(function(){
32 if ($(this).val()){
33 var dataUrl = CRM.url('civicrm/ajax/rest',
34 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=contact&id=' + $(this).val());
35 $.ajax({
36 url : dataUrl,
37 async : false,
38 success : function(html){
39 htmlText = html.split( '|' , 2);
40 $('#soft_credit_contact_' + rowCnt).val(htmlText[0]);
41 rowCnt++;
42 }
43 });
44 }
45 });
46
47 $('.crm-soft-credit-block tr span').each(function () {
48 if ($(this).hasClass('crm-error')) {
49 $(this).parents('tr').show();
50 }
51 });
52
53 $('.delete-link').click(function(){
54 var row = $(this).attr('row-no');
55 $('#soft-credit-row-' + row).hide().find('input').val('');
56 $('input[name="soft_credit_contact_select_id['+row+']"]').val('');
57 return false;
58 });
59
60 $('input[name^="soft_credit_contact["]').change(function(){
61 var rowNum = $(this).attr('id').replace('soft_credit_contact_','');
62 var totalAmount = Number($('#total_amount').val().replace(',',''));
63 //assign total amount as default soft credit amount
64 $('#soft_credit_amount_'+ rowNum).val(totalAmount);
65 if (rowNum > 1) {
66 var scAmount = Number($('#soft_credit_amount_'+ (rowNum - 1)).val());
67 if (scAmount < totalAmount) {
68 //if user enters less than the total amount and adds another soft credit row,
69 //the soft credit amount default will be left empty
70 $('#soft_credit_amount_'+ rowNum).val('');
71 }
72 }
73 });
74
75 });