function get_pledge_amount_details(){
    var service_type = $('select[name="fn_type"]').val();
    var name = $('.name').val();
    var contact_no = $('.contact_no').val();
    
    if(service_type != '' && name != '' && contact_no != '') {
       $.ajax({
        async: false,
        url: base_url + 'billing_donation/get_pledge_amount_details',
        type: 'POST',
        data: { 
            service_type: service_type,
            name: name,
            contact_no: contact_no
        },
        dataType: "json"
        })
        .done(function(data) {
            if(data.pledge_data) {
                if(data.pledge_data.pledge_type == 1) {
                    $('.contact_no').addClass('req');
                    if(data != '') {
                        $('.show_transaction_details').fadeIn('slow');
                        $('input[name="hidden_pledge_type"]').val(data.pledge_data.pledge_type);
                        if(data.pledge_data.pledge_type == 1) {
                            $('input:radio[name=pledge_type][value=1]').iCheck('check');
                            var balance_amount = parseFloat(data.pledge_data.pledge_amount) - parseFloat(data.pledge_data.amount);
                            $('input[name="hidden_pledge_type"]').val('1');
                            $('input:radio[name=pledge_type][value=1]').prop('disabled','disabled');
                            $('input:radio[name=pledge_type][value=0]').prop('disabled','disabled');
                        } else {
                            $('input[name="hidden_pledge_type"]').val('0');
                        }
                            
                        var amount_type = '';
                        if(data.pledge_data.amount_type == 1){
                            amount_type = 'Amount';
                            $('.pledge_amount_div').fadeIn('slow');
                            $('.balance_amount_div').fadeIn('slow');
                            var last_donation = parseFloat(data.pledge_data.pledge_amount) - parseFloat(balance_amount);
                            
                            $('.balance_amount_div').html('<label>Balance Amount</label><div class="input-group"><div class="input-group-addon readonly"><b>$</b></div><input type="text" class="form-control readonly" name="balance_amount" data-balanceAmt="' + (parseFloat(balance_amount).toFixed(2)) + '" placeholder="0.00" value="' + (parseFloat(balance_amount).toFixed(2)) + '" readonly /></div><span style="padding-left: 165px; color:#0B45B3;" class="last_donation">Last Donated Amount / Item :<b>' + (parseFloat(last_donation).toFixed(2)) + '</b></span>');
                            
                            $('.change_amt_type_div').html('<input type="hidden" name="amount_type" value="' + data.pledge_data.amount_type + '"/> <input type="text" class="form-control readonly" name="pledgeAmount" value="' + amount_type + '" readonly />');
                            
                            $('input[name="pledging_amount"]').val(data.pledge_data.pledge_amount);
                            $('input[name="hidden_pledge_amount"]').val(data.pledge_data.pledge_amount);
                            
                            $('.change_icon').html('<label>Amount (Current Donation)<span class="required">*</span></label><div class="input-group"><div class="input-group-addon"><b>$</b></div><input type="text" class="form-control" name="cs_amount" data-err-target=".sub-title" /></div><input type="hidden" name="oldPledgeamount" class="oldPledgeamount" value="' + data.pledge_data.pledge_amount + '" />');
                        }
                        else if(data.pledge_data.amount_type == 2){
                            amount_type = 'Item';
                            $('.pledge_item_div').fadeIn('slow');
                            $('.balance_amount_div').fadeIn('slow');
                            var last_donation = parseFloat(data.pledge_data.pledge_amount) - parseFloat(balance_amount);
                            
                            $('.balance_amount_div').html('<label>Balance Amount</label><div class="input-group"><input type="text" class="form-control readonly" name="balance_amount" data-balanceAmt="' + (parseFloat(balance_amount).toFixed(2)) + '" value="' + (parseFloat(balance_amount).toFixed(2)) + '" readonly /><div class="input-group-addon readonly"><b>grams</b></div></div><span style="padding-left: 165px; color:#0B45B3;" class="last_donation">Last Donated Amount / Item :<b>' + (parseFloat(last_donation).toFixed(2)) + '</b></span>');
                            
                            $('.change_amt_type_div').html('<input type="hidden" name="amount_type" value="' + data.pledge_data.amount_type + '"/> <input type="text" class="form-control readonly" name="pledgeAmount" value="' + amount_type + '" readonly />');
                            
                            $('input[name="pledging_item"]').val(data.pledge_data.pledge_amount);
                            $('input[name="hidden_pledge_amount"]').val(data.pledge_data.pledge_amount);
                            
                            $('.change_icon').html('<label>Item (Current Donation)<span class="required">*</span></label><div class="input-group"><input type="text" class="form-control" name="cs_amount" data-err-target=".sub-title" /><div class="input-group-addon"><b>grams</b></div></div><input type="hidden" name="oldPledgeamount" class="oldPledgeamount" value="' + data.pledge_data.pledge_amount + '" />');
                        }
                        
                        $('.email').val(data.pledge_data.email);
                        $('.tamil-name').val(data.pledge_data.tamil_name);
                        $('.address').val(data.pledge_data.address);
                        $('input[name="cs_desc"]').val(data.pledge_data.desc);
                        $('input[name="cs_amount"]').addClass('amount num-pad');
                        
                    } else {
                        if($('input[name="pledge_type"]:checked').val() == 1) {
                            $('.pledge_type_div').show();
                        } else {
                            $('.pledge_type_div').hide();
                        }
                        
                    }
                    
                    var html = '';
                    if(data != '') {
                        html += '<h4 class="heading_lbl">Transaction History</h4>';
                        html += '<div id="retrieve-details" class="resp-table" style="display: block; margin:0px !important;">';
                        
                        for(var i in data.transaction_data) {
                            var transaction = data.transaction_data[i];
                            
                            var _stle = i % 2 != 0 ? 'background: rgba(17, 187, 170, 0.06);' : '';
                            var style = 'padding: 0px 2px;';
                            
                            html += '<div class="col-xs-12">';
                            html += '<div class="col-md-2" style="'+ style +'">';
                            html += '<div class="form-group">';
                            html += '<div class="lbl"><i class="fa fa-fire"></i>S.No</div><div class="lbl-value" style="'+ _stle +'; text-align: center;">' + (++i) + '</div>';
                            html += '</div>';
                            html += '</div>';
                            
                            var d = new Date(transaction.pledge_date);
                            
                            html += '<div class="col-md-2" style="'+ style +'">';
                            html += '<div class="form-group">';
                            html += '<div class="lbl"><i class="fa fa-fire"></i>Date</div><div class="lbl-value" style="'+ _stle +'; text-align: center;">'+ ( getTwoDigits( d.getDate() ) +' '+ monthNames[d.getMonth()] +' '+d.getFullYear() ) +'</div>';
                            html += '</div>';
                            html += '</div>';
                                
                            html += '<div class="col-md-2" style="'+ style +'">';
                            html += '<div class="form-group">';
                            html += '<div class="lbl"><i class="fa fa-fire"></i>Ref No</div><div class="lbl-value" style="'+ _stle +'; text-align: center;">' + transaction.receipt_no + '</div>';
                            html += '</div>';
                            html += '</div>';

                            html += '<div class="col-md-2" style="'+ style +'">';
                            html += '<div class="form-group">';
                            html += '<div class="lbl"><i class="fa fa-fire"></i>Amount'+ ((data.pledge_data.amount_type == 2) ? ' (grams)' : '') +'</div><div class="lbl-value" style="'+ _stle +'; text-align: right;">' + transaction.amount + '</div>';
                            html += '</div>';
                            html += '</div>';
                            
                            html += '<div class="col-md-1" style="'+ style +'">';
                            html += '<div class="form-group">&nbsp;</div>';
                            html += '</div>';
                            
                            html += '<div class="col-md-2" style="'+ style +'">';
                            html += '<div class="form-group">';
                            if(i == 1) {
                                html += '<a href="' + base_url + 'billing_donation/print_transaction_details/' + service_type + '/' + name + '/' + contact_no +'" target="_blank"><button type="button" id="print-btn" class="btn btn-danger"><i class="fa fa-print"></i> Print</button></a>';
                            } else {
                                html += '&nbsp;';
                            }
                            html += '</div>';
                            html += '</div>';
                            
                            html += '<div class="col-md-1" style="'+ style +'">';
                            html += '<div class="form-group">&nbsp;</div>';
                            html += '</div>';
                            
                            html += '</div>';
                            
                        }
                        html += '</div>';
                    }
                
                    $('.transaction_history_container').html(html);
                }
            }
        })
        .fail(function(jqXHR, status_code, error_thrown) {
            console.log(jqXHR.responseText);
        }); 
    }
}

function change_pledge_amount_div(_this,cs_amount_default){
    if($(_this).val() == 1) {
        $('.pledge_type_div').fadeIn('slow');
        $('input[name="hidden_pledge_type"]').val('1');
        
        $('select[name="amount_type"] option:selected').prop('selected',false);
        
        if($('select[name="amount_type"] option:selected').val() == 1) {
            $('.pledge_amount_div').fadeIn('slow');
            $('.pledge_item_div').fadeOut('slow');
            $('input[name="pledging_item"]').removeClass('req');
            $('input[name="pledging_amount"]').addClass('req');
        } else if($('select[name="amount_type"] option:selected').val() == 2) {
            $('.pledge_item_div').fadeIn('slow');
            $('.pledge_amount_div').fadeOut('slow');
            $('input[name="pledging_item"]').addClass('req');
            $('input[name="pledging_amount"]').removeClass('req');
        } else {
            $('.pledge_item_div').fadeOut('slow');
            $('.pledge_amount_div').fadeOut('slow');
            $('input[name="pledging_item"]').removeClass('req');
            $('input[name="pledging_amount"]').removeClass('req');
        }
        $('.contact_no').addClass('req');
        
    } 
    else if($(_this).val() == 0) {
        $('.pledge_type_div').fadeOut('slow');
        $('input[name="hidden_pledge_type"]').val('0');
        
        if($('select[name="amount_type"] option:selected').val() == 1) {
            $('.pledge_amount_div').fadeOut('slow');
        } else if($('select[name="amount_type"] option:selected').val() == 2) {
            $('.pledge_item_div').fadeOut('slow');
        }
        
        $('.change_icon').html('<label>Amount<span class="required">*</span></label><div class="input-group"><div class="input-group-addon"><b>$</b></div><input type="text" class="form-control req amount num-pad" name="cs_amount" placeholder="0.00" data-err-target=".sub-title" value="' + cs_amount_default + '"/></div>');
        
        $('.contact_no').removeClass('req');
    }
    
}

function setTotal() {
    var pay_amount = parseFloat($('[name="cs_amount"]').val());
    if( parseFloat($('[name="cs_amount"]').val()) <= parseFloat(0) || $('[name="cs_amount"]').val() == '' ) {
        // Disable Payment Btn
        $('#payment-btn').attr('disabled', 'disabled');
    } else 
        $('#payment-btn').removeAttr('disabled');
    
    if($('select[name="amount_type"]').val() == 2) {
        $('input[name="pay_amount"]').val('0.00');
    } else if($('input[name="amount_type"]').val() == 2) {
        $('input[name="pay_amount"]').val('0.00');
    }
    else {
        $('input[name="pay_amount"]').val(pay_amount.toFixed(2));
    }
    //$('#payment-btn').removeAttr('disabled');
    var edit = $('input[name="hidden_id"]').val();
    var old_amount = parseFloat($('input[name="old_amount"]').val()) || 0;
    if(edit != '' && parseFloat(old_amount) < parseFloat(pay_amount) ) {
        $('#payment-btn').html('<i class="fa fa-credit-card"></i> Payment');
    }
}

var inter = '';
$(document).ready(function(){
    
   /*  $('.num-pad .ok-btn').on('click', function(){
        alert('test');
        get_pledge_amount_details();
    }); */
    
    $('#retrieve-dialog .reset_btn, .cancel_popup').click(function() {        
        resetAll($('#retrieve-dialog'));
        $('.p_search_by_ref').val('1');
        $('.p_search_by_con').val('0');
        $('.p_search_by_ref').iCheck('check');
        $('.resp-table').css('display','none');
        
        var reference ='';
        reference += '<option value="">-- Select Ref No --</option>';
        $('select[name="p_ref_no"]').html(reference);
        
        var contact ='';
        contact += '<option value="">-- Select Contact No --</option>';
        $('select[name="p_contact_no"]').html(contact);
    });
    
    $('input[name="pledge_type"]').on('ifChecked', function(event){
        var cs_amount_default = $('input[name="cs_amount"]').val();
        $('input[name="cs_amount_default"]').val(cs_amount_default);
        change_pledge_amount_div(this,$('input[name="cs_amount_default"]').val());
        setTotal();
    });
    
    $('select[name="amount_type"]').on('change', function(event){
        var cs_amount_default = $('input[name="cs_amount_default"]').val();
        if($(this).val() == 1) {
            $('.pledge_amount_div').fadeIn('slow');
            $('.pledge_item_div').fadeOut('slow');
            $('input[name="pledging_amount"]').addClass('req');
            $('input[name="pledging_item"]').removeClass('req');
            
            $('.change_icon').html('<label>Amount (Current Donation)<span class="required">*</span></label><div class="input-group"><div class="input-group-addon"><b>$</b></div><input type="text" class="form-control req float_value amount num-pad" name="cs_amount"  data-err-target=".sub-title" /></div>');
        
        } else if($(this).val() == 2) {
            $('.pledge_amount_div').fadeOut('slow');
            $('.pledge_item_div').fadeIn('slow');
            $('input[name="pledging_item"]').addClass('req');
            $('input[name="pledging_amount"]').removeClass('req');
            
            $('.change_icon').html('<label>Item (Current Donation)<span class="required">*</span></label><div class="input-group"><input type="text" class="form-control req float_value amount num-pad" name="cs_amount" data-err-target=".sub-title" /><div class="input-group-addon"><b>grams</b></div></div>');
            
            
        } else {
            $('.pledge_amount_div').fadeOut('slow');
            $('.pledge_item_div').fadeOut('slow');
            $('input[name="pledging_item"]').removeClass('req');
            $('input[name="pledging_amount"]').removeClass('req');
            
            $('.change_icon').html('<label>Amount<div class="input-group"><div class="input-group-addon"><b>$</b></div><input type="text" class="form-control req float_value" name="cs_amount" data-err-target=".sub-title" value="' + cs_amount_default + '" readonly /></div>');
        }
        setTotal();
    });
    
    
    $('select[name="fn_type"]').change(function(){
        get_service_amount($(this).val());
        setTotal();       
    });
    
    $('[name="cs_amount"]').keyup(function(){
        setTotal();
    });
    
    $('#payment-btn').on('click', function(){
        
        /* if( !autoValidate() ) {
           return false; 
        } */
       var payAmt = parseFloat( $('[name="pay_amount"]').val()) || 0;
       var paidAmt = parseFloat( $('[name="paid_amount"]').val()) || 0;
       
       var amount_type = $('input[name="hidden_pledge_amount_type"]').val();
       var edit_id = $('input[name="hidden_id"]').val();
       var add_amount_type = ($('select[name="amount_type"]').val()) || ($('input[name="amount_type"]').val()) || '';
      
       var old_amount = parseFloat($('input[name="old_amount"]').val()) || 0;
       var cs_amount = parseFloat($('input[name="cs_amount"]').val()) || 0;
       
       var diff_amount = parseFloat(cs_amount) - parseFloat(old_amount);
       
       if((edit_id == '' && (add_amount_type == 1 || add_amount_type == '')) || edit_id != '' && (amount_type == 1)) {
            var payamount = (parseFloat(diff_amount)).toFixed(2);
            $('#ptypes-dialog [name="pay_amount"]').val(cs_amount);
            if( autoValidate() && !$(this).hasClass('dialog-btn') ) {
               // Go to payment
                $('.ptype-box input:first').parent().closest('.modal').find('.err-blck').html('');
                if( paidAmt <= 0 ) {
                    $('[type="submit"]').attr('disabled', 'disabled');
                }
                $(this).addClass('dialog-btn').attr('data-target','#ptypes-dialog').trigger('click');
                return true;
            }
           
            if( paidAmt <= 0 ) {
               $('[type="submit"]').attr('disabled', 'disabled');
            }
            if(edit_id == '') {
                $('#ptypes-dialog .pay-type:first, [name="paid_amount"]').val(payamount);
            }
            
            if(parseFloat(payAmt) != parseFloat(paidAmt)) {
                $('#sbmt-btn').attr('disabled','disabled'); 
            } else {
                $('#sbmt-btn').removeAttr('disabled'); 
            }
            //$('#sbmt-btn').removeAttr('disabled');            
            return false;
        } else if(amount_type == 3) {
            if( autoValidate() && !$(this).hasClass('dialog-btn') ) {
               // Go to payment
                $('.ptype-box input:first').parent().closest('.modal').find('.err-blck').html('');
                if( paidAmt <= 0 ) {
                    $('[type="submit"]').attr('disabled', 'disabled');
                }
                $(this).addClass('dialog-btn').attr('data-target','#ptypes-dialog').trigger('click');
                return true;
            }
           
            if( paidAmt <= 0 ) {
               $('[type="submit"]').attr('disabled', 'disabled');
            }
            
            if(edit_id == '') {
                $('#ptypes-dialog .pay-type:first, [name="paid_amount"]').val(cs_amount);
            }
            if(edit_id != '') {
                var $bank_name = $('#cheque-box .hidden_bank_name').val();
                var $cheque_no = $('#cheque-box .hidden_chq_no').val();
                var $cheque_date = $('#cheque-box .hidden_chq_date').val();
                
                $('#cheque-box [name="chq_bnk_name"]').val($bank_name);
                $('#cheque-box [name="chq_no"]').val($cheque_no);
                $('#cheque-box [name="chq_date"]').val($cheque_date);
            }
            if(parseFloat(payAmt) != parseFloat(paidAmt)) {
                $('#sbmt-btn').attr('disabled','disabled'); 
            } else {
                $('#sbmt-btn').removeAttr('disabled'); 
            }
            //$('#sbmt-btn').removeAttr('disabled');            
            return false;
        }
       /* else if((edit_id != '' && (amount_type == 1) && parseFloat(old_amount) == parseFloat(cs_amount))) {
           // msgBox('Amount is already paid!','','info');
            //window.setTimeout(function(){
                $('[type="submit"]').removeAttr('disabled').trigger('click');
            //}, 3500);
        }  else if((parseFloat(old_amount) > parseFloat(cs_amount)) && (edit_id != '')) {
            var paying_amt = parseFloat(old_amount) - parseFloat(cs_amount);
            msgBox('Amount ' + ((parseFloat(paying_amt)).toFixed(2)) + ' is Refunded!','','info');
            window.setTimeout(function(){
                $('[type="submit"]').removeAttr('disabled').trigger('click');
            }, 3500);
        
        }  */else {
            $('[type="submit"]').removeAttr('disabled').trigger('click');
        }
   });
   
   
    
    
    /* $('#sbmt-cancel-btn').on('click', function(){
        $('[name="paid_amount"], .ptype-box input').val('');
        $('#payment-btn').removeClass('dialog-btn').removeAttr('data-target');
        
    }) */
    
    $(document).on('change', '#retrieve-dialog .p_fn_type ', function(){
        get_ref_or_contact_no(this);
    });
  
    $('input[name="p_search_by"]').on('ifChecked', function(event){
        if($(this).val() == 1) {
            $('.search_ref_no').show();
            $('.search_contact_no').hide();            
        } else if($(this).val() == 0) {
            $('.search_ref_no').hide();
            $('.search_contact_no').show();
        }
        get_ref_or_contact_no($('#retrieve-dialog .p_fn_type'));
    });
        
    $(document).on('click', '.modal .filter-btn', function(){
        if($('[name="p_fn_type"]').val() != '') {
            if($('[name="p_ref_no"]').val() != '' || $('[name="p_contact_no"]').val() != '') {
                var $appnedTo = $('#retrieve-dialog .modal-footer')
                ,   style = 'padding: 0px 2px;'
                ,   html = '';
                
                if( $appnedTo.find('#retrieve-details').length == 0 ) {
                    $appnedTo.append('<div id="retrieve-details" class="resp-table"></div>');
                }
                
                var $bookedWrapper = $appnedTo.find('#retrieve-details');
                
                $bookedWrapper.html( '<div class="col-xs-12" style="text-align: center; color: #1ba;">Please wait ...</div>' );
                
                var data = {
                    service_type: $('[name="p_fn_type"]').val(),
                    search_by: $('[name="p_search_by"]:checked').val(),
                    ref_no: $('[name="p_ref_no"]').val(),
                    contact_no: $('[name="p_contact_no"]').val()
                };
                
                $.post( base_url + controller + '/ajax/', data, function( results ){
                    results = JSON.parse( results );
                    if( results != '' ) {
                        
                        Object.keys( results ).map(function(i, iV){
                            iV = results[i];
                            var _stle = i % 2 != 0 ? 'background: rgba(17, 187, 170, 0.06);' : '';
                            $('[name="p_name"]').val(iV['name']);
                            $('[name="p_amount"]').val(iV['amount']);
                             
                            html += '<div class="col-xs-12" style="margin-top: 30px;">';

                            html +=     '<div class="col-md-2" style="'+ style +'">';
                            html +=         '<div class="form-group">';
                            html +=             '<div class="lbl"><i class="fa fa-fire"></i>Ref Id</div><div class="lbl-value" style="'+ _stle +'">'+ iV['receipt_no'] +'</div>';
                            html +=         '</div>';
                            html +=     '</div>';

                            html +=     '<div class="col-md-3" style="'+ style +'">';
                            html +=         '<div class="form-group">';
                            html +=             '<div class="lbl"><i class="fa fa-fire"></i>Type</div><div class="lbl-value" style="'+ _stle +'">'+ ( iV['service_name'] != '' ? iV['service_name'] : 'N/A' )  +'</div>';
                            html +=         '</div>';
                            html +=     '</div>';

                            html +=     '<div class="col-md-3" style="'+ style +'">';
                            html +=         '<div class="form-group">';
                            html +=             '<div class="lbl"><i class="fa fa-fire"></i>Name</div><div class="lbl-value" style="'+ _stle +';">'+ iV['name'] +'</div>';
                            html +=         '</div>';
                            html +=     '</div>';

                            html +=     '<div class="col-md-2" style="'+ style +'">';
                            html +=         '<div class="form-group">';
                            html +=             '<div class="lbl"><i class="fa fa-fire"></i>Contact  No</div><div class="lbl-value" style="'+ _stle +'">'+ iV['contact_no'] +'</div>';
                            html +=         '</div>';
                            html +=     '</div>';

                            html +=     '<div class="col-md-2" style="'+ style +'">';
                            html +=         '<div class="form-group">';
                            html +=             '<div class="lbl"><i class="fa fa-fire"></i>Amount</div><div class="lbl-value" style="'+ _stle +'; text-align: right;">'+ iV['amount'] +'</div>';
                            html +=         '</div>';
                            html +=     '</div>';

                            html += '</div>';
                        });
                    }else {
                        // No results
                        html += '<div class="col-xs-12" style="text-align: center; color: #1ba;">Sorry, No results found</div>';

                    }
                    
                    $bookedWrapper.fadeOut('slow', function(){
                        $(this).html(html).fadeIn('slow');
                    });
                });
            } else {
                msgBox('Please select Ref.No or Contact No!');
                $('[name="p_ref_no"]').focus();
                $('[name="p_contact_no"]').focus();
            }
        } else {
            msgBox('Please select Type!');
            $('[name="p_fn_type"]').focus();
        }
        
    });
    
    $(document).on('keyup', 'input[name="cs_amount"]', function(){
        var pay_amount = parseFloat($(this).val());
        if($('select[name="amount_type"] option:selected').val() == 2) {
            $('input[name="pay_amount"]').val('0.00');
            //$('[type="submit"]').removeAttr('disabled');
        } else if($('input[name="amount_type"]').val() == 2) {
            $('input[name="pay_amount"]').val('0.00');
            //$('[type="submit"]').removeAttr('disabled');
        } else {
            $('input[name="pay_amount"]').val(pay_amount.toFixed(2));
        }
        window.clearTimeout(inter);
        var $this   = $(this);
        
        inter = window.setInterval(function(){
            window.clearTimeout(inter);
            var value   = $this.val();
            var $errBlck = $this.parents().find('.box-body .err-blck');
            
            if($('input[name="pledge_type"]:checked').val() == 1) {
                var balance_amount = $('input[name="balance_amount"]').val();
                var edit_id = $('input[name="hidden_id"]').val();
                var old_amount = $('input[name="old_amount"]').val();
                
                var pledgeAmount = ($('select[name="amount_type"]').val() == 1 || $('input[name="amount_type"]').val() == 1) ? (parseFloat($('input[name="pledging_amount"]').val()) || 0) : (parseFloat($('input[name="pledging_item"]').val()) || 0);
                
                var amount = $this.val();
                if(parseFloat(balance_amount) < parseFloat(amount)) {
                    $('#payment-btn').attr('disabled','disabled'); 
                    $errBlck.html( 'Amount/Item can not be exceeded than Balance Amount/Item!' ).slideDown('slow');
                    window.setTimeout(function(){
                        $errBlck.slideUp('slow', function(){ $(this).html('') });
                        if(edit_id != '') {
                            $this.val((parseFloat(old_amount)).toFixed(2));
                            $('input[name="pay_amount"]').val((parseFloat(old_amount)).toFixed(2));
                        } else {
                            $this.val((parseFloat(balance_amount)).toFixed(2));
                            $('input[name="pay_amount"]').val((parseFloat(balance_amount)).toFixed(2));
                        }
                        $('#payment-btn').removeAttr('disabled');
                    }, 3500);
                } /* else if(parseFloat(amount) < parseFloat(old_amount)) {
                    $('#payment-btn').attr('disabled','disabled');
                    $errBlck.html( 'Amount/Item can not be reduce than previous Amount/Item!' ).slideDown('slow');
                    window.setTimeout(function(){
                        $errBlck.slideUp('slow', function(){ $(this).html('') });
                        if(edit_id != '') {
                            $this.val((parseFloat(old_amount)).toFixed(2));
                            $('input[name="pay_amount"]').val((parseFloat(old_amount)).toFixed(2));
                        } else {
                            $this.val((parseFloat(balance_amount)).toFixed(2));
                            $('input[name="pay_amount"]').val((parseFloat(balance_amount)).toFixed(2));
                        }
                        $('#payment-btn').removeAttr('disabled');
                    }, 3500);
                } */ else if(parseFloat(pledgeAmount) < parseFloat(amount)) {
                    $('#payment-btn').attr('disabled','disabled');
                    $errBlck.html( 'Amount/Item should be less than Pledge Amount/Item!' ).slideDown('slow');
                    window.setTimeout(function(){
                        $errBlck.slideUp('slow', function(){ $(this).html('') });
                        if(edit_id != '') {
                            $this.val((parseFloat(old_amount)).toFixed(2));
                            $('input[name="pay_amount"]').val((parseFloat(old_amount)).toFixed(2));
                        } else {
                            $this.val((parseFloat(pledgeAmount)).toFixed(2));
                            $('input[name="pay_amount"]').val((parseFloat(pledgeAmount)).toFixed(2));
                        }
                        if(parseFloat(pledgeAmount) > parseFloat(0) || parseFloat(old_amount) > parseFloat(0)) {
                            $('#payment-btn').removeAttr('disabled');
                        }
                        
                    }, 3500);
                } setTotal();
            }
        }, 400);
    });
    
    /* $(document).on('keyup', 'input[name="cs_amount"]', function(){
        window.clearTimeout(inter);
        var $this   = $(this);
        
        inter = window.setInterval(function(){
            window.clearTimeout(inter);
            var value   = $this.val();
            var $errBlck = $this.parents().find('.box-body .err-blck');
            
            if($('input[name="pledge_type"]:checked').val() == 1) {
                var balance_amount = $('input[name="balance_amount"]').val();
                var amount = $this.val();
                if(parseFloat(balance_amount) < parseFloat(amount)) {
                     $errBlck.html( 'Amount/Item can not be exceeded than Balance amount!' ).slideDown('slow');
                    window.setTimeout(function(){
                        $errBlck.slideUp('slow', function(){ $(this).html('') });
                        $this.val(parseFloat(balance_amount));
                        $('#payment-btn').removeAttr('disabled');
                    }, 3500);
                }
            }
        }, 400);
    }); */
    
    $('input[name="pledging_amount"]').keyup(function(){
        window.clearTimeout(inter);
        var $this   = $(this);
        
        inter = window.setInterval(function(){
            window.clearTimeout(inter);
            var value   = $this.val();
            var $errBlck = $this.parents().find('.box-body .err-blck');
            var pledge_old_amt = $('input[name="hidden_pledge_amount"]').val();
            var balance = parseFloat($('input[name="balance_amount"]').attr('data-balanceAmt')) || 0;
            
            if(parseFloat(value) < parseFloat(pledge_old_amt)){
                $errBlck.html( 'We couldn\'t reduce the Pledge Amount!' ).slideDown('slow');
                $('#payment-btn').attr('disabled','disabled'); 
                window.setTimeout(function(){
                    $errBlck.slideUp('slow', function(){ $(this).html('') });
                    $this.val(parseFloat(pledge_old_amt));
                    //$('input[name="balance_amount"]').val(balance.toFixed(2));
                    $('#payment-btn').removeAttr('disabled');
                }, 3500);
            }
        }, 400);
        
        balanceAmount(this);
    });
   
    $('input[name="pledging_item"]').keyup(function(){
        balanceAmount(this);
    });
    
    /* $('input[name="pledging_amount"], input[name="pledging_item"]').keydown(function(){
        balanceAmount(this);
    }); */
});

function balanceAmount(_this) {
    var balance = parseFloat($('input[name="balance_amount"]').attr('data-balanceAmt')) || 0;
    var pledge_amount = parseFloat($(_this).val()) || 0;
    var old_pledge_amount = $('.oldPledgeamount').val();
    var diff_amt =  parseFloat(old_pledge_amount) - parseFloat(balance);
    var current_balance = parseFloat(pledge_amount) - parseFloat(diff_amt);
    $('input[name="balance_amount"]').val(current_balance.toFixed(2));
    
}

function get_ref_or_contact_no(_this) {
    var service_type = $(_this).val();
    var search_by = $('input[name="p_search_by"]:checked').val();
    if(service_type != '') {
        $.ajax({
            async: false,
            url: base_url + 'billing_donation/get_data_by_service_type',
            type: 'POST',
            data: { service_type: service_type},
            dataType: "json"
        })
        .done(function(data) {
            if(search_by == 1) {
                var reference ='';
                reference += '<option value="">-- Select Ref No --</option>';
                for(var i in data.get_data) {
                    var ref = data.get_data[i];
                    reference += '<option value="'+ ref.receipt_no +'">'+ ref.receipt_no +'</option>';
                }
                $('select[name="p_ref_no"]').html(reference);
            } else if(search_by == 0) {
                var contact ='';
                contact += '<option value="">-- Select Contact No --</option>';
                for(var i in data.get_contact) {
                    var con = data.get_contact[i];
                    contact += '<option value="'+ con.contact_no +'">'+ con.contact_no +'</option>';
                }
                $('select[name="p_contact_no"]').html(contact);
            }
            
        })
        .fail(function(jqXHR, status_code, error_thrown) {
            console.log(jqXHR.responseText);
        });
    } else {
        var reference ='';
        reference += '<option value="">-- Select Ref No --</option>';
        $('select[name="p_ref_no"]').html(reference);
        
        var contact ='';
        contact += '<option value="">-- Select Contact No --</option>';
        $('select[name="p_contact_no"]').html(contact);
    }
}


function toValidateAmountincrease(_this) {
    window.clearTimeout(inter);
    var $this   = $(_this);
    
    inter = window.setInterval(function(){
        window.clearTimeout(inter);
        var value   = $this.val();
        var $errBlck = $this.parents().find('.box-body .err-blck');
        
        var pledge_amount = $this.val();
        var pledge_unchageable = $('input[name="hidden_pledge_amount"]').val();
        
        if($('input[name="pledge_type"]:checked').val() == 1) {
            if(parseFloat(pledge_amount) < parseFloat(pledge_unchageable)) {
                 $errBlck.html( 'Pledge Amount/Item can not be reduce than previous pledge amount/item!' ).slideDown('slow');
                window.setTimeout(function(){
                    $errBlck.slideUp('slow', function(){ $(this).html('') });
                    $this.val(parseFloat(pledge_unchageable));
                    $('#payment-btn').removeAttr('disabled');
                }, 3500);
            }
        }
    }, 400);
}

function get_service_amount(this_value){
    $.ajax({
        async: false,
        url: base_url + 'billing_donation/get_amount_by_service_id',
        type: 'POST',
        data: { service_type: this_value},
        dataType: "json"
    })
    .done(function(data) {
        /* if( parseFloat(data.amount) <= parseFloat(0) ) {
            // Disable Payment Btn
            $('#payment-btn').attr('disabled', 'disabled');
         } else 
            $('#payment-btn').removeAttr('disabled'); */
        
        $('[name="cs_amount"], [name="pay_amount"]').val( data.amount );
    })
    .fail(function(jqXHR, status_code, error_thrown) {
        console.log(jqXHR.responseText);
    });
}