var reset_total = false;
function calculate_difference() {
    //if(!reset_total) {
        $('.amtShow').attr('type','hidden');
        $('.amtHide').attr('type','text');
        var cs_amount = $('[name="cs_amount"]').val();
        var hidden_amount = parseFloat($('[name="hidden_amount"]').val());
        var total_diff = hidden_amount - parseFloat(cs_amount)  ;
        $('[name="cs_amount"]').val((hidden_amount).toFixed(2));
        reset_total = true;
        $('[name="cs_amount"]').attr('data-diff-amt',total_diff);
    //}
}

function setTotal() {
    // Calculate Prasadam Total
    var edit_id = $('input[name="hidden_id"]').val();
    var book_status = $('input[name="book_status"]').val();
    if(edit_id == '') {
        var total = parseFloat( $('[name="fn_type"] option:selected').attr('data-amt') );
    } else {
        var total = parseFloat( $('[name="fn_type"]').attr('data-amt') );
    } 
    $('.stotal').each(function(i, iKey){
        var value = parseFloat( $(this).val() ) || 0;
        //console.log( 'Sub :' + value );
        
        total += parseFloat( value );
    });
    

    var csAmt = parseFloat($('[name="cs_amount"]').val()) || "";
    if( total > 0 || csAmt != '' ) {
        $('#payment-btn').removeAttr('disabled');
    }else {
        // Disable Payment Btn
        $('#payment-btn').attr('disabled', 'disabled');
    }
    
    
    var already_paid_amt = $('input[name="hidden_amount"]').val();
    var paying_amt = parseFloat(total) - parseFloat(already_paid_amt);
    var cs_amount = $('[name="cs_amount"]').val();

    if($('[name="cs_amount"]').attr('data-diff-amt') !== undefined)  {
        var diff = parseFloat($('[name="cs_amount"]').attr('data-diff-amt')) || 0;
        total += diff;
    }

    if(edit_id == '' || (book_status == '' && book_status == 1)) {
        $('[name="cs_amount"]').val( total.toFixed(2) );
    } else if(edit_id != '' && book_status == 2) {
           $('[name="cs_amount"]').val( total.toFixed(2) ); 
    } else {
        $('[name="cs_amount"]').val( total.toFixed(2) );
    }
    
    if(edit_id != '' && (parseFloat(already_paid_amt) < parseFloat($('[name="cs_amount"]').val()))) {
        $('#payment-btn').html('<i class="fa fa-credit-card"></i> Payment');
    }
    
}

var inter = '';
$(document).on('ready', function() {
    /*$('#bkd-btn').on('click', function(){
        $('#booked-dialog .filter-btn').trigger('click'); 
    });*/
    
    /* $('#sbmt-cancel-btn').on('click', function(){
        //$('[name="paid_amount"], .ptype-box input').val('');
        $('#payment-btn').removeClass('dialog-btn').removeAttr('data-target');
        $('.payment-btn').removeAttr('disabled');
        
    }) */
    
    $('#booked-dialog .reset-btn').click(function() {
        // No results
        html = '<div class="col-xs-12" style="text-align: center; color: #1ba;">Sorry, No results found</div>';
        $('#booked-details').html(html).fadeIn('slow');
    });
    
    $('input[name="cs_amount"]').keyup(function() {
        var value = $(this).val() || "";
        if( value != "" ) {
            $('#payment-btn').removeAttr('disabled');
        }else {
            $('#payment-btn').attr('disabled', 'disabled');
        }
    });
    
    /* if($('[name="hidden_id"]') != '' && $('[name="book_status"]').val() == 2) {
        $('#prasadam-dialog .can-btn').show();
    } else {
        $('#prasadam-dialog .can-btn').hide();       
    } */
    
    $('input[name="fn_from_time"]').change(function(){
        var start_time = $(this).val();
        var hours = start_time.split(':')[0];
        var min = start_time.split(':')[1];
        var time = new Date(0,0,0,hours,min,0,0);
        var end_time = new Date(time.getTime() + (1*60*60*1000));
        var endTime = ((end_time.getHours() < 10) ? '0' + end_time.getHours() : end_time.getHours())  + ':' + ((end_time.getMinutes() < 10) ? '0' + end_time.getMinutes() : end_time.getMinutes());
        $('input[name="fn_to_time"]').val(endTime);
    });
    
    $('.payment-btn').on('click', function(){
        var paidAmt = parseFloat( $('[name="paid_amount"]').val()) || 0
        ,   payAmt  = parseFloat($('[name="pay_amount"]').val()) || 0
        ,   toDisable = paidAmt <= 0 || payAmt != paidAmt ? true : false;
        
        if( autoValidate()) {
            // Go to payment
            /* if( toDisable ) {
                $('[type="submit"]').attr('disabled', 'disabled');
            }
            
            $('.ptype-box input:first').parent().closest('.modal').find('.err-blck').html('');
            $(this).addClass('dialog-btn').attr('data-target','#ptypes-dialog').trigger('click'); */
            return true;
        }/* else if( toDisable ) {
            $('[type="submit"]').attr('disabled', 'disabled');
        } */
        
        return false;
    })
    
    $('input[name="cs_amount"]').keyup(function() {
        $('[name="hidden_amount"]').val($(this).val());
    });
    
    $('#payment-btn').on('click', function(){
        var edit_id = $('input[name="hidden_id"]').val();
        if(reset_total || (edit_id == '' || edit_id == undefined)) {
            var $Amount = parseFloat($('input[name="cs_amount"]').val());
        } else {
            var $Amount = parseFloat($('input[name="hidden_amount"]').val());
        }
        $('[name="pay_amount"]').val($Amount.toFixed(2));
        
        var paidAmt = parseFloat( $('[name="paid_amount"]').val()) || 0
        ,   payAmt  = parseFloat($('[name="pay_amount"]').val()) || 0
        ,   toDisable = paidAmt <= 0 || payAmt != paidAmt ? true : false;
        
        var already_paid_amt = ($('input[name="hidden_amount"]').val() !== undefined) ? $('input[name="hidden_amount"]').val() : 0;
        var cs_amount = $('input[name="cs_amount"]').val();
        
        var book_status = $('input[name="book_status"]').val();
        
        //if((parseFloat(already_paid_amt) < parseFloat(cs_amount)) || (edit_id == '' && (book_status == '' || book_status == 1))) {
        if((edit_id == '' && (book_status == '' || book_status == 1))) {    
            if( autoValidate() && !$(this).hasClass('dialog-btn') ) {
                // Go to payment
                if( toDisable ) {
                    $('[type="submit"]').attr('disabled', 'disabled');
                }
                
                $('.ptype-box input:first').parent().closest('.modal').find('.err-blck').html('');
                $(this).addClass('dialog-btn').attr('data-target','#ptypes-dialog').trigger('click');
                return true;
            }else if( toDisable ) {
                $('[type="submit"]').attr('disabled', 'disabled');
            }
            $('#ptypes-dialog .pay-type:first, [name="paid_amount"]').val((parseFloat(payAmt)).toFixed(2));
            $('#sbmt-btn').removeAttr('disabled');
            return false;
        
        } /* else if((parseFloat(already_paid_amt) > parseFloat(cs_amount)) && (edit_id != '' && book_status == 1)) {
            var paying_amt = parseFloat(already_paid_amt) - parseFloat(cs_amount);
            msgBox('Amount ' + ((parseFloat(paying_amt)).toFixed(2)) + ' is Refunded!','','info');
            window.setTimeout(function(){
                $('[type="submit"]').removeAttr('disabled').trigger('click');
            }, 3500);
        
        }  */else if(edit_id != '' && book_status == 2) {
           if( autoValidate() && !$(this).hasClass('dialog-btn') ) {
                // Go to payment
                if( toDisable ) {
                    $('[type="submit"]').attr('disabled', 'disabled');
                }
                
                $('.ptype-box input:first').parent().closest('.modal').find('.err-blck').html('');
                $(this).addClass('dialog-btn').attr('data-target','#ptypes-dialog').trigger('click');
                return true;
            }else if( toDisable ) {
                $('[type="submit"]').attr('disabled', 'disabled');
            }
            $('#ptypes-dialog .pay-type:first, [name="paid_amount"]').val((parseFloat(payAmt)).toFixed(2));
            $('#sbmt-btn').removeAttr('disabled');
            return false;
        
        
        } else {
           if( autoValidate() && !$(this).hasClass('dialog-btn') ) {
                // Go to payment
                if( toDisable ) {
                    $('[type="submit"]').attr('disabled', 'disabled');
                }
                
                $('.ptype-box input:first').parent().closest('.modal').find('.err-blck').html('');
                $(this).addClass('dialog-btn').attr('data-target','#ptypes-dialog').trigger('click');
                return true;
            }else if( toDisable ) {
                $('[type="submit"]').attr('disabled', 'disabled');
            }
            
            if(edit_id == '') {
                $('#ptypes-dialog .pay-type:first, [name="paid_amount"]').val((parseFloat(payAmt)).toFixed(2));
            }
            
            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);
            }
            //$('#sbmt-btn').removeAttr('disabled');
            return false;
        
        
        } /* else {
            //msgBox('Amount is already paid!','','info');
            //window.setTimeout(function(){
                $('[type="submit"]').removeAttr('disabled').trigger('click');
            //}, 3500);
        } */
    })
    
    $('[name="fn_type"]').on('change', function(){
        var value = $(this).val();
        if( value == '' ) {
            $('#prasadam-btn').fadeOut('slow');
        }else {
            $('#prasadam-btn').fadeIn('slow');
        }
        
        setTotal();
    });
    
    
    // Prasadam
    $(document).on('click', '.details-row .ctrl.edit-btn, .details-row .ctrl.rm-btn, .cancel-rw-edit', function(){
        
        var $wrapper = $(this).parent().closest('.modal')
        ,   $footer = $wrapper.find('.modal-footer');
        
        if( $(this).hasClass('cancel-rw-edit') ) {
            // Cancel Editing Row
            var $row = $wrapper.find('.editing');
            $row.removeClass('editing');
            $wrapper.find('.details td.ctrls *').fadeIn('slow');
            $footer.find('.new-btn').fadeIn('slow');
            
            if( $(this).hasClass('save') ) {
                // Update Row
                $(this).removeClass('cancel-rw-edit save').addClass('save-btn');
                
                var price = $('#pr_price').val()
                ,   qty   = $('#pr_qty').val()
                ,   sTotal = (qty*price).toFixed(2);
                
                $row.find('.data-a input').val( $('#pr_deity').val() );
                $row.find('.data-a span').html( $('#pr_deity option:selected').text() );
                
                $row.find('.data-b input').val( $('#pr_prasadam').val() );
                $row.find('.data-b span').html( $('#pr_prasadam option:selected').text() );
                
                $row.find('.data-c input').val( qty );
                $row.find('.data-c span').html( qty );
                
                $row.find('.data-d input').val( price );
                $row.find('.data-d span').html( price );
                
                
                $row.find('.data-e input').val( sTotal );
                $row.find('.data-e span').html( sTotal );
                
            }
            
            // Clear fields
            $('#pr_deity, #pr_prasadam, #pr_qty, #pr_price').val('');
                       
            $footer.find('.cancel-rw-edit').addClass('cancel_popup').attr('data-dismiss', 'modal').removeClass('cancel-rw-edit');
            
        }else {
            var $row = $(this).parent().closest('.details-row');
            
            if( $(this).hasClass('rm-btn') ) {
                // Delete Row
                
                if( window.confirm("Are you sure want to remove..?") ) {
                    $row.fadeOut('slow', function(){
                        $(this).addClass('remove-me');//remove();
                        
                        var len = $('.details-row').not('.details-row.remove-me').length
                        ,   $saveBtn = $wrapper.find('.modal-footer .save-btn');
                        $saveBtn.find('span').html( ' ('+ len +')' );

                        /*if( len > 0 ) {
                            $saveBtn.removeAttr('disabled');
                        }else {
                            $saveBtn.attr('disabled', 'disabled');
                        }*/
                        
                        if( $wrapper.find('.details .details-row').length == 0 ) {
                            $wrapper.find('.details').fadeOut('slow');
                        }
                        
                    });
                }
                
                return false;
                
            }else {
            
                // Edit Row
                $row.addClass('editing');
                $wrapper.find('.details td.ctrls *').fadeOut('slow');
                $footer.find('.new-btn').fadeOut('slow');
                $footer.find('.save-btn').addClass('cancel-rw-edit save').removeClass('save-btn');
                $footer.find('.cancel_popup').removeClass('cancel_popup').removeAttr('data-dismiss').addClass('cancel-rw-edit');

                $('#pr_deity').val( $row.find('.data-a input').val());
                $('#pr_prasadam').val( $row.find('.data-b input').val());
                $('#pr_qty').val( $row.find('.data-c input').val());
                $('#pr_price').val( $row.find('.data-d input').val());
            }    
        }
        add_desc(); 
    });
    
    $(document).on('click', '.new-btn, .save-btn', function() {
        
        var $this    = $(this)  
        ,   $wrapper = $this.parent().closest('.modal')
        ,   $body    = $wrapper.find('.modal-footer')   
        ,   html     = ''
        ,   isOk    = autoValidate( $wrapper ) ? true : false
        ,   isSaveOk    = $this.hasClass('save-btn') ? true : false;
        
        var edit_id = $('[name="hidden_id"]').val();
        var book_status = $('[name="book_status"]').val();

        if( isOk || ( isOk && $this.hasClass('save-btn') && $('#prasadam-dialog .details-row').length == 0 ) ) {
            
        
            if( $body.find('.details').length == 0 ) {
                var table = '<table class="details">';
                    table += '<tr>';
                    table += '<th>Deity</th><th>Prasadam</th><th>Quantity</th><th>Price</th><th>Sub Total</th>';
                    //if(edit_id == '' || book_status == 2) {
                        table += '<th>Action</th>';
                    //}
                    table += '</tr>';
                    table += '</table>';
                $body.append(table);
            }

            $body.find('.details').slideDown('slow');

            var price   = $('#pr_price').val()
            ,   qty     = $('#pr_qty').val()
            ,   sTotal  = (qty*price).toFixed(2)
            ,   dbId    = $('#pr_deity').attr('data-db-id') || ""
                key    = $('.data-a').length;
            

            html += '<tr class="details-row" style="display: none; border-bottom: 2px solid #fff;" data-strings="' + $('#pr_deity option:selected').val() + '-' + $('#pr_prasadam option:selected').val() + '">';
                if( dbId != '' ) {
                    
                    html += '<input type="hidden" name="pr_id['+ key +']" class="db-id" value="'+ dbId +'">';
                }
                html += '<td><div class="data-a" style="text-align: left;"><input type="hidden" name="pr_deity['+ key +']" value="'+ $('#pr_deity').val() +'"><span>'+ $('#pr_deity option:selected').text() +'</span></div></td>';
                //html += '<td><select name="pr_prasadam">'+ $('[name="pr_prasadam"]').html() +'</select></td>';
                html += '<td><div class="data-b" style="text-align: left;"><input type="hidden" name="pr_prasadam['+ key +']" value="'+ $('#pr_prasadam').val() +'"><span>'+ $('#pr_prasadam option:selected').text() +'</span></div></td>';
                html += '<td><div class="data-c"><input type="hidden" name="pr_qty['+ key +']" value="'+ qty +'"><span>'+ qty +'</span></div></td>';
                html += '<td><div class="data-d"><input type="hidden" name="pr_price['+ key +']" value="'+ price +'"><span>'+ price +'</span></div></td>';
                html += '<td><div class="data-e"><input type="hidden" name="pr_stotal['+ key +']" value="'+ sTotal +'" class="stotal"><span>'+ sTotal +'</span></div></td>';
                
                //if(edit_id == '' || book_status == 2) {
                    html += '<td class="ctrls" style="text-align: center;"><i class="fa fa-pencil ctrl edit-btn" title="Edit"></i> <i class="fa fa-times ctrl rm-btn" title="Remove"></i></td>';
                //}
            html += '</tr>';

            if( $body.find('.details tr.details-row').length == 0 ) {

                $body.find('.details').append( html );
                
            }else {
                $body.find('.details tr.details-row:first').before( html );
            }
            $body.find('.details tr.details-row:first').delay(500).fadeIn('slow');

            // Clear fields
            $('#pr_deity, #pr_prasadam, #pr_qty, #pr_price').val('');
           
        }
        
        
        if( isSaveOk && $('#prasadam-dialog .details-row').length > 0 ){
            $wrapper.find('.err-ele').trigger('click');
            
            if( $this.hasClass('save-btn') ) {
                /*
                 * Take the db id of removing items
                 */
                if( $('[name="removed-items"]').length == 0 ) {
                    $('form').append('<input type="hidden" name="removed-items" value="">');
                }
                
                var valStr = $('[name="removed-items"]').val();
                $('#prasadam-dialog .remove-me').each(function(){
                    var dbId = $(this).find('.db-id').val() || "";
                    if( dbId != '' ) {
                        valStr += '-'+dbId;
                    }
                });
                $('[name="removed-items"]').val(valStr);
                
                $('#prasadam-dialog .remove-me').remove();
                $('#prasadam-btn span').html( ' ('+ $('#prasadam-dialog .details-row').length +')' );
                setTotal();

                $('#prasadam-dialog .cancel_popup').trigger( 'click' );
                
            }
        }
        
        var len = $('.details-row').not('.details-row.remove-me').length
        ,   $saveBtn = $wrapper.find('.modal-footer .save-btn');
            $saveBtn.find('span').html( ' ('+ len +')' );
            add_desc();
            setTotal();
            
            /*if( len > 0 ) {
                $saveBtn.removeAttr('disabled');
            }else {
                $saveBtn.attr('disabled', 'disabled');
            }*/
        
        return false;
    })
    
    $('#prasadam-dialog .cancel_popup').on('click', function(){
        $('#prasadam-dialog .remove-me').removeClass('remove-me').show();
        $('#prasadam-dialog .save-btn span').html( ' ('+ $('#prasadam-dialog .details-row').length +')' );
    })
    
    $(document).on('click', '.modal .filter-btn', function(){
        
        var $appnedTo = $('#booked-dialog .modal-footer')
        ,   style = 'padding: 0px 2px;'
        ,   html = '';
        
        if( $appnedTo.find('#booked-details').length == 0 ) {
            $appnedTo.append('<div id="booked-details" class="resp-table"></div>');
        }
        
        var $bookedWrapper = $appnedTo.find('#booked-details');
        
        $bookedWrapper.html( '<div class="col-xs-12" style="text-align: center; color: #1ba;">Please wait ...</div>' );
        
        var data = {
            act     : 'booked',
            fromDate: $('[name="bk_from_date"]').val(),
            toDate: $('[name="bk_to_date"]').val(),
            period: $('[name="bk_period"]').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);' : '';

                    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>Devotee Name</div><div class="lbl-value" style="'+ _stle +'">'+ iV['devotee_name1'] +'</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>Tamil Name</div><div class="lbl-value" style="'+ _stle +'">'+ ( iV['tamil_name1'] != '' ? iV['tamil_name1'] : 'N/A' )  +'</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 +'; text-align: right;">'+ 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>Type</div><div class="lbl-value" style="'+ _stle +'">'+ iV['service_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>Amount</div><div class="lbl-value" style="'+ _stle +'; text-align: center;">'+ iV['amount'] +'</div>';
                    html +=         '</div>';
                    html +=     '</div>';

                    var d = new Date(iV['date']);

                    html +=     '<div class="col-md-2" style="'+ style +'">';
                    html +=         '<div class="form-group">';
                    html +=             '<div class="lbl"><i class="fa fa-fire"></i>Function Date</div><div class="lbl-value" style="'+ _stle +';text-align: right;">'+ ( 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>Action</div><div class="lbl-value" style="'+ _stle +';text-align: center;"><a class="label btn btn-primary edit" href="' + base_url + 'billing_ubayam/edit/' + iV['id'] + '/'+ iV['type_id'] + '"> <span>Edit</span></a>';
                    html +=                '<a class="label btn btn-danger delete" href="' + base_url + 'billing_ubayam/delete/' + iV['id'] + '/'+ iV['type_id'] + '"> <span>Delete</span></a>';
                    
                    html +=             '</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');
            });

        });
        
        
    });
    
    //window.setTimeout(function(){
     if( $('#prasadam-hidden-details').length > 0 ) {
        
        var details = $('#prasadam-hidden-details').val();
        details = JSON.parse( details );
        Object.keys( details ).map(function(i, iV){
            window.setTimeout(function(){
                $('#pr_deity').val(details[i]['deity_id']).attr('data-db-id', details[i]['id']);
                $('#pr_prasadam').val(details[i]['prasadam']);
                $('#pr_qty').val(details[i]['quantity']);
                $('#pr_price').val(details[i]['price']);
                $('.new-btn').trigger('click');
                $('#pr_deity').removeAttr('data-db-id');
            }, 1000);
        })

        if($('[name="hidden_id"]').val()) {
            $('.amtHide').attr('type','hidden');
            $('.amtShow').attr('type','text');
        }
        setTimeout(function(){
            
            calculate_difference(); 
            
        }, (details.length+1) * 1000);
     }    
    //}, 2000);

    $('#prasadam-dialog #pr_deity, #pr_prasadam').change(function(){
        deity_prasadam_exists();
    });  
    var detail = '';
    $('#prasadam-dialog .details .details-row').each(function() {
        detail  += $(this).parents().find('<tr> <td> <input> <span>').text();
    });
    
    $('.pr_prasadam').change(function() {
        var prasadam = $(this).val();
        $.ajax({
            async: false,
            url: base_url + 'billing_ubayam/get_prasadam_price',
            type: 'POST',
            data: { id: prasadam},
            dataType: "json"
        }).done(function(data) {
            $('.pr_price').val(data.price);
        }).fail(function(XHR, status_code, error_thrown) {
            console.log(error_thrown);
        });
    });
})

function deity_prasadam_exists() {
    var return_val = true;
    var _this = $('#prasadam-dialog #pr_deity').val();
    var prasadam = $('#pr_prasadam').val();
    $('.details .details-row').not('.remove-me').each(function() {
        var exist_deity = ($(this).attr('data-strings')).split('-')[0];
        var exist_prasadam = ($(this).attr('data-strings')).split('-')[1];
        
        if(exist_deity != '' && exist_prasadam != '' && exist_deity == _this && prasadam == exist_prasadam) {
            $('#prasadam-dialog .modal-dialog .modal-footer .new-btn .save-btn').attr('disabled','disabled');
            $('#prasadam-dialog #pr_deity, #pr_prasadam, #pr_qty, #pr_price').val('');
            msgBox('Deity Name and Prasadam are already exists!');
            return_val = false;
            return false;
        }
    });
    return return_val;
}

function add_desc() {
    var desc = '';
    var text_desc = '';
    if($('#prasadam-dialog .details-row').length > 0){
        $('#prasadam-dialog .details-row').each(function() {
            var deity = $(this).find('.data-a span').text();
            var prasadam = $(this).find('.data-b span').text();
            var qty = $(this).find('.data-c span').text();
            var stotal = $(this).find('.data-e span').text();
            desc += deity + '-' + prasadam + '-' + qty + '-' + stotal + ',';
        });
    } 
    text_desc += desc;
    var lastChar = text_desc.slice(-1);
    if(lastChar == ',') {
      text_desc = text_desc.slice(0, -1);
    }
    $('textarea[name="cs_desc"]').val(text_desc);
}
