Tag & search js cleanup
[civicrm-core.git] / templates / CRM / Contribute / Form / SoftCredit.js
index 001b210f466459a58b179bb871cf6eb2fa52c000..b8159a47c2032a152713cfd151c7f7d56ef30e05 100644 (file)
@@ -11,9 +11,9 @@ cj(function($) {
     return false;
   }
 
-  $('#addMoreSoftCredit').click(function(){
-    $('.crm-soft-credit-block tr.hiddenElement :first').show().removeClass('hiddenElement');
-    if ( $('.crm-soft-credit-block tr.hiddenElement').length < 1 ) {
+  $('#addMoreSoftCredit').live('click', function () {
+    $('.crm-contribution-form-block-soft_credit_to tr.hiddenElement').filter(':first').show().removeClass('hiddenElement');
+    if ($('.crm-soft-credit-block tr.hiddenElement').length < 1) {
       $('#addMoreSoftCredit').hide();
     }
     return false;
@@ -34,6 +34,7 @@ cj(function($) {
         'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=contact&id=' + $(this).val());
       $.ajax({
         url     : dataUrl,
+        async   : false,
         success : function(html){
           htmlText = html.split( '|' , 2);
           $('#soft_credit_contact_' + rowCnt).val(htmlText[0]);
@@ -49,4 +50,29 @@ cj(function($) {
     }
   });
 
+  $('.delete-link').click(function(){
+    var row = $(this).attr('row-no');
+    $('#soft-credit-row-' + row).hide().find('input').val('');
+    $('input[name="soft_credit_contact_select_id['+row+']"]').val('');
+    return false;
+  });
+
+  $('input[name^="soft_credit_contact["]').change(function(){
+    var rowNum = $(this).prop('id').replace('soft_credit_contact_','');
+    var totalAmount = $('#total_amount').val();
+    //assign total amount as default soft credit amount
+    $('#soft_credit_amount_'+ rowNum).val(totalAmount);
+    var thousandMarker = CRM.monetaryThousandSeparator;
+    $('#soft_credit_type_'+ rowNum).val($('#sct_default_id').val());
+    totalAmount = Number(totalAmount.replace(thousandMarker,''));
+    if (rowNum > 1) {
+      var scAmount = Number($('#soft_credit_amount_'+ (rowNum - 1)).val().replace(thousandMarker,''));
+      if (scAmount < totalAmount) {
+     //if user enters less than the total amount and adds another soft credit row, 
+     //the soft credit amount default will be left empty 
+        $('#soft_credit_amount_'+ rowNum).val(''); 
+      }
+    }
+  });
+
 });