/**
 * Created by Huzaifa on 9/18/15.
 */

$(document).on('click','.btnAddGrid', function() {
    $html = '';
    $html += '<tr id="grid_row_'+$grid_row+'" data-row_id="'+$grid_row+'">';
    $html += '<td>';
    $html += '<a onclick="removeRow(this);" title="Remove" class="btn btn-xs btn-danger" href="javascript:void(0);"><i class="fa fa-times"></i></a>';
    //$html += '&nbsp;<a title="Add" class="btn btn-xs btn-primary btnAddGrid" href="javascript:void(0);"><i class="fa fa-plus"></i></a>';
    $html += '</td>';
    $html += '<td>';
    $html += '<input type="text" style="min-width: 100px;" class="form-control" name="opening_stock_details['+$grid_row+'][manual_ref_no]" id="opening_stock_detail_manual_ref_no_'+$grid_row+'" value="" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<input onchange="getProductByCode(this);" type="text" style="min-width: 100px;" class="form-control" name="opening_stock_details['+$grid_row+'][product_code]" id="opening_stock_detail_product_code_'+$grid_row+'" value="" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<div class="input-group">';
    $html += '<select style="min-width: 300px;" onchange="getProductById(this);" class="form-control select2" id="opening_stock_detail_product_id_'+$grid_row+'" name="opening_stock_details['+$grid_row+'][product_id]" >';
    $html += '<option value="">&nbsp;</option>';
//    $products.forEach(function($product) {
//        $html += '<option value="'+$product.product_id+'">'+$product.name+'</option>';
//    });
    $html += '</select>';
    $html += '<span class="input-group-btn ">';
    $html += '<button class="btn btn-default btn-flat QSearchProduct" id="QSearchProduct" type="button" data-element="opening_stock_detail_product_id_'+$grid_row+'" data-field="product_id">';
    $html += '<i class="fa fa-search"></i>';
    $html += '</button>';
    $html += '</span>';
    $html += '</div>';
    $html += '</td>';
    //$html += '<td>';
    //$html += '<input style="min-width: 100px;" type="text" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][cubic_meter]" id="opening_stock_detail_cubic_meter_'+$grid_row+'" value="0" readonly/>';
    //$html += '</td>';
    //$html += '<td>';
    //$html += '<input style="min-width: 100px;" type="text" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][cubic_feet]" id="opening_stock_detail_cubic_feet_'+$grid_row+'" value="0" readonly/>';
    //$html += '</td>';
    $html += '<td>';
    $html += '<input style="min-width: 100px;" type="text" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][container_no]" id="opening_stock_detail_container_no_'+$grid_row+'" value="0" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<input style="min-width: 100px;" type="text" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][batch_no]" id="opening_stock_detail_batch_no_'+$grid_row+'" value="0" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<input type="hidden" class="form-control" name="opening_stock_details['+$grid_row+'][unit_id]" id="opening_stock_detail_unit_id_'+$grid_row+'" value="" />';
    $html += '<input style="min-width: 100px;" type="text" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][qty]" id="opening_stock_detail_qty_'+$grid_row+'" value="1" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<input onchange="calculateTotalCubicFeet(this);" type="text" class="form-control" name="opening_stock_details['+$grid_row+'][total_cubic_meter]" id="opening_stock_detail_total_cubic_meter_'+$grid_row+'" value=""/>';
    $html += '</td>';
    $html += '<td>';
    $html += '<input onchange="calculateTotalCubicMeter(this);" type="text" class="form-control" name="opening_stock_details['+$grid_row+'][total_cubic_feet]" id="opening_stock_detail_total_cubic_feet_'+$grid_row+'" value=""/>';
    $html += '</td>';
    $html += '<td>';
    $html += '<input onchange="calculateAmount(this);" style="min-width: 100px;" type="text" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][rate]" id="opening_stock_detail_rate_'+$grid_row+'" value="0.00" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<input type="text" style="min-width: 100px;" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][amount]" id="opening_stock_detail_amount_'+$grid_row+'" value="0.00" readonly="true" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<a title="Duplicate" class="btn btn-xs btn-primary btnAddDuplicate" href="javascript:void(0);"><i class="fa fa-clone"></i></a>';
    $html += '&nbsp;<a title="Add" class="btn btn-xs btn-primary btnAddGrid" href="javascript:void(0);"><i class="fa fa-plus"></i></a>';
    //$html += '&nbsp;<a onclick="removeRow(this);" title="Remove" class="btn btn-xs btn-danger" href="javascript:void(0);"><i class="fa fa-times"></i></a>';
    $html += '</td>';
    $html += '</tr>';


    if($(this).parent().parent().data('row_id')=='H') {
        $('#tblOpeningStockDetail tbody').prepend($html);
    } else {
        $(this).parent().parent().after($html);
    }
    //$('#opening_stock_detail_product_id_'+$grid_row).select2({width: '100%'});
    $('#opening_stock_detail_product_id_'+$grid_row).select2({
        width: '100%',
        ajax: {
            url: $UrlGetProductJSON,
            dataType: 'json',
            type: 'post',
            mimeType:"multipart/form-data",
            delay: 250,
            data: function (params) {
                return {
                    q: params.term, // search term
                    page: params.page
                };
            },
            processResults: function (data, params) {
                // parse the results into the format expected by Select2
                // since we are using custom formatting functions we do not need to
                // alter the remote JSON data, except to indicate that infinite
                // scrolling can be used
                params.page = params.page || 1;

                return {
                    results: data.items,
                    pagination: {
                        more: (params.page * 30) < data.total_count
                    }
                };
            },
            cache: true
        },
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 5,
        templateResult: formatRepo, // omitted for brevity, see the source of this page
        templateSelection: formatRepoSelection // omitted for brevity, see the source of this page                }
    });
    calculateTotal();

    $grid_row++;
});

$(document).on('click','.btnAddDuplicate', function() {
    var $row_id = $(this).parent().parent().data('row_id');
    var $manual_ref_no = $('#opening_stock_detail_manual_ref_no_'+$row_id).val();
    var $warehouse_id = $('#opening_stock_detail_warehouse_id_'+$row_id+' option:selected').val();
    var $product_code = $('#opening_stock_detail_product_code_'+$row_id).val();
    var $product_id = $('#opening_stock_detail_product_id_'+$row_id+' option:selected').val();
    var $product_name = $('#opening_stock_detail_product_id_'+$row_id+' option:selected').text();
    var $cubic_meter = $('#opening_stock_detail_cubic_meter_'+$row_id).val();
    var $cubic_feet = $('#opening_stock_detail_cubic_feet_'+$row_id).val();
    var $container_no = $('#opening_stock_detail_container_no_'+$row_id).val();
    var $batch_no = $('#opening_stock_detail_batch_no_'+$row_id).val();
    var $unit_id = $('#opening_stock_detail_unit_id_'+$row_id).val();
    var $qty = $('#opening_stock_detail_qty_'+$row_id).val();
    var $total_cubic_meter = $('#opening_stock_detail_total_cubic_meter_'+$row_id).val();
    var $total_cubic_feet = $('#opening_stock_detail_total_cubic_feet_'+$row_id).val();
    var $rate = $('#opening_stock_detail_rate_'+$row_id).val();
    var $amount = $('#opening_stock_detail_amount_'+$row_id).val();

    $html = '';
    $html += '<tr id="grid_row_'+$grid_row+'" data-row_id="'+$grid_row+'">';
    $html += '<td>';
    $html += '<a onclick="removeRow(this);" title="Remove" class="btn btn-xs btn-danger" href="javascript:void(0);"><i class="fa fa-times"></i></a>';
    //$html += '&nbsp;<a title="Add" class="btn btn-xs btn-primary btnAddGrid" href="javascript:void(0);"><i class="fa fa-plus"></i></a>';
    $html += '</td>';
    $html += '<td>';
    $html += '<input type="text" style="min-width: 100px;" class="form-control" name="opening_stock_details['+$grid_row+'][manual_ref_no]" id="opening_stock_detail_manual_ref_no_'+$grid_row+'" value="'+$manual_ref_no+'" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<input onchange="getProductByCode(this);" type="text" style="min-width: 100px;" class="form-control" name="opening_stock_details['+$grid_row+'][product_code]" id="opening_stock_detail_product_code_'+$grid_row+'" value="'+$product_code+'" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<div class="input-group">';
    $html += '<select style="min-width: 100px;" onchange="getProductById(this);" class="form-control select2" id="opening_stock_detail_product_id_'+$grid_row+'" name="opening_stock_details['+$grid_row+'][product_id]" >';
    $html += '<option selected="true" value="'+$product_id+'">'+$product_name+'</option>';
    $html += '</select>';
    $html += '<span class="input-group-btn ">';
    $html += '<button class="btn btn-default btn-flat QSearchProduct" id="QSearchProduct" type="button" data-element="opening_stock_detail_product_id_'+$grid_row+'" data-field="product_id">';
    $html += '<i class="fa fa-search"></i>';
    $html += '</button>';
    $html += '</span>';
    $html += '</div>';
    $html += '</td>';
    //$html += '<td>';
    //$html += '<input style="min-width: 100px;" type="text" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][cubic_meter]" id="opening_stock_detail_cubic_meter_'+$grid_row+'" value="'+$cubic_meter+'" readonly/>';
    //$html += '</td>';
    //$html += '<td>';
    //$html += '<input style="min-width: 100px;" type="text" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][cubic_feet]" id="opening_stock_detail_cubic_feet_'+$grid_row+'" value="'+$cubic_feet+'" readonly/>';
    //$html += '</td>';
    $html += '<td>';
    $html += '<input style="min-width: 100px;" type="text" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][container_no]" id="opening_stock_detail_container_no_'+$grid_row+'" value="'+$container_no+'" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<input style="min-width: 100px;" type="text" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][batch_no]" id="opening_stock_detail_batch_no_'+$grid_row+'" value="'+$batch_no+'" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<input type="hidden" class="form-control" name="opening_stock_details['+$grid_row+'][unit_id]" id="opening_stock_detail_unit_id_'+$grid_row+'" value="'+$unit_id+'" />';
    $html += '<input style="min-width: 100px;" type="text" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][qty]" id="opening_stock_detail_qty_'+$grid_row+'" value="'+$qty+'" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<input onchange="calculateTotalCubicFeet(this);" type="text" class="form-control" name="opening_stock_details['+$grid_row+'][total_cubic_meter]" id="opening_stock_detail_total_cubic_meter_'+$grid_row+'" value="'+$total_cubic_meter+'" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<input onchange="calculateTotalCubicMeter(this);" type="text" class="form-control" name="opening_stock_details['+$grid_row+'][total_cubic_feet]" id="opening_stock_detail_total_cubic_feet_'+$grid_row+'" value="'+$total_cubic_feet+'" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<input onchange="calculateAmount(this);" style="min-width: 100px;" type="text" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][rate]" id="opening_stock_detail_rate_'+$grid_row+'" value="'+$rate+'" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<input type="text" style="min-width: 100px;" class="form-control fPDecimal" name="opening_stock_details['+$grid_row+'][amount]" id="opening_stock_detail_amount_'+$grid_row+'" value="'+$amount+'" readonly="true" />';
    $html += '</td>';
    $html += '<td>';
    $html += '<a title="Duplicate" class="btn btn-xs btn-primary btnAddDuplicate" href="javascript:void(0);"><i class="fa fa-clone"></i></a>';
    $html += '&nbsp;<a title="Add" class="btn btn-xs btn-primary btnAddGrid" href="javascript:void(0);"><i class="fa fa-plus"></i></a>';
    //$html += '&nbsp;<a onclick="removeRow(this);" title="Remove" class="btn btn-xs btn-danger" href="javascript:void(0);"><i class="fa fa-times"></i></a>';
    $html += '</td>';
    $html += '</tr>';


    $('#tblOpeningStockDetail tbody #grid_row_' + $row_id).after($html);
    $('#opening_stock_detail_product_id_'+$grid_row).select2({
        width: '100%',
        ajax: {
            url: $UrlGetProductJSON,
            dataType: 'json',
            type: 'post',
            mimeType:"multipart/form-data",
            delay: 250,
            data: function (params) {
                return {
                    q: params.term, // search term
                    page: params.page
                };
            },
            processResults: function (data, params) {
                // parse the results into the format expected by Select2
                // since we are using custom formatting functions we do not need to
                // alter the remote JSON data, except to indicate that infinite
                // scrolling can be used
                params.page = params.page || 1;

                return {
                    results: data.items,
                    pagination: {
                        more: (params.page * 30) < data.total_count
                    }
                };
            },
            cache: true
        },
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 5,
        templateResult: formatRepo, // omitted for brevity, see the source of this page
        templateSelection: formatRepoSelection // omitted for brevity, see the source of this page                }
    });
    $('#opening_stock_detail_warehouse_id_'+$grid_row).select2({width: '100%'});
    $grid_row++;
    calculateTotal();
});

function getProductById($obj) {
    $product_id = $($obj).val();
    var $row_id = $($obj).parent().parent().parent().data('row_id');
    $.ajax({
        url: $UrlGetProductById,
        dataType: 'json',
        type: 'post',
        data: 'product_id=' + $product_id,
        mimeType:"multipart/form-data",
        beforeSend: function() {
            $('#grid_row_'+$row_id+' .QSearchProduct i').removeClass('fa-search').addClass('fa-refresh fa-spin');
        },
        complete: function() {
            $('#grid_row_'+$row_id+' .QSearchProduct i').removeClass('fa-refresh').removeClass('fa-spin').addClass('fa-search');
        },
        success: function(json) {
            if(json.success)
            {
                $('#opening_stock_detail_product_code_'+$row_id).val(json.product['product_code']);
                $('#opening_stock_detail_unit_id_'+$row_id).val(json.product['unit_id']);
                $('#opening_stock_detail_cubic_meter_'+$row_id).val(json.product['cubic_meter']);
                $('#opening_stock_detail_cubic_feet_'+$row_id).val(json.product['cubic_feet']);
                $('#opening_stock_detail_rate_'+$row_id).val(json.product['cost_price']);

                $('#opening_stock_detail_rate_'+$row_id).trigger('change');
                $('#opening_stock_detail_discount_percent_'+$row_id).trigger('change');
                $('#opening_stock_detail_tax_percent_'+$row_id).trigger('change');

                calculateTotal();
            }
            else {
                alert(json.error);
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(xhr.responseText);
        }
    })
}

function getProductByCode($obj) {
    $product_code = $($obj).val();
    var $row_id = $($obj).parent().parent().data('row_id');
    $.ajax({
        url: $UrlGetProductByCode,
        dataType: 'json',
        type: 'post',
        data: 'product_code=' + $product_code,
        mimeType:"multipart/form-data",
        beforeSend: function() {
            $('#grid_row_'+$row_id+' .QSearchProduct i').removeClass('fa-search').addClass('fa-refresh fa-spin');
        },
        complete: function() {
            $('#grid_row_'+$row_id+' .QSearchProduct i').removeClass('fa-refresh').removeClass('fa-spin').addClass('fa-search');
        },
        success: function(json) {
            $('#opening_stock_detail_product_id_'+$row_id).select2('destroy');
            if(json.success)
            {
                $('#opening_stock_detail_unit_id_'+$row_id).val(json.product['unit_id']);
                $('#opening_stock_detail_unit_'+$row_id).val(json.product['unit']);
                $('#opening_stock_detail_product_id_'+$row_id).html('<option selected="selected" value="'+json.product['product_id']+'">'+json.product['name']+'</option>');
                $('#opening_stock_detail_rate_'+$row_id).val(json.product['cost_price']);
                $('#opening_stock_detail_cubic_meter_'+$row_id).val(json.product['cubic_meter']);
                $('#opening_stock_detail_cubic_feet_'+$row_id).val(json.product['cubic_feet']);

                $('#opening_stock_detail_rate_'+$row_id).trigger('change');
                $('#opening_stock_detail_discount_percent_'+$row_id).trigger('change');
                $('#opening_stock_detail_tax_percent_'+$row_id).trigger('change');
            } else {
                alert(json.error);
                $('#opening_stock_detail_unit_id_'+$row_id).val('');
                $('#opening_stock_detail_unit_'+$row_id).val('');
                $('#opening_stock_detail_product_id_'+$row_id).html('');
                $('#opening_stock_detail_rate_'+$row_id).val('0.00');
            }
            $('#opening_stock_detail_product_id_'+$row_id).select2({
                width: '100%',
                ajax: {
                    url: $UrlGetProductJSON,
                    dataType: 'json',
                    type: 'post',
                    mimeType:"multipart/form-data",
                    delay: 250,
                    data: function (params) {
                        return {
                            q: params.term, // search term
                            page: params.page
                        };
                    },
                    processResults: function (data, params) {
                        // parse the results into the format expected by Select2
                        // since we are using custom formatting functions we do not need to
                        // alter the remote JSON data, except to indicate that infinite
                        // scrolling can be used
                        params.page = params.page || 1;

                        return {
                            results: data.items,
                            pagination: {
                                more: (params.page * 30) < data.total_count
                            }
                        };
                    },
                    cache: true
                },
                escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                minimumInputLength: 5,
                templateResult: formatRepo, // omitted for brevity, see the source of this page
                templateSelection: formatRepoSelection // omitted for brevity, see the source of this page                }
            });

            calculateTotal();
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(xhr.responseText);
        }
    })
}

function setProductInformation($obj) {
    var $data = $($obj).data();
    console.log($data);
    var $row_id = $('#'+$data['element']).parent().parent().parent().data('row_id');
    $('#_modal').modal('hide');
    $('#opening_stock_detail_product_code_'+$row_id).val($data['product_code']);
    $('#opening_stock_detail_cubic_meter_'+$row_id).val($data['cubic_meter']);
    $('#opening_stock_detail_cubic_feet_'+$row_id).val($data['cubic_feet']);
    $('#opening_stock_detail_unit_id_'+$row_id).val($data['unit_id']);
    $('#opening_stock_detail_unit_'+$row_id).val($data['unit']);
    $('#opening_stock_detail_rate_'+$row_id).val($data['cost_price']);
    $('#opening_stock_detail_total_cubic_meter_'+$row_id).val(0);
    $('#opening_stock_detail_total_cubic_feet_'+$row_id).val(0);
    $('#opening_stock_detail_product_id_'+$row_id).select2('destroy');
    $('#opening_stock_detail_product_id_'+$row_id).html('<option selected="selected" value="'+$data['product_id']+'">'+$data['name']+'</option>');
    $('#opening_stock_detail_product_id_'+$row_id).select2({
        width: '100%',
        ajax: {
            url: $UrlGetProductJSON,
            dataType: 'json',
            type: 'post',
            mimeType:"multipart/form-data",
            delay: 250,
            data: function (params) {
                return {
                    q: params.term, // search term
                    page: params.page
                };
            },
            processResults: function (data, params) {
                // parse the results into the format expected by Select2
                // since we are using custom formatting functions we do not need to
                // alter the remote JSON data, except to indicate that infinite
                // scrolling can be used
                params.page = params.page || 1;

                return {
                    results: data.items,
                    pagination: {
                        more: (params.page * 30) < data.total_count
                    }
                };
            },
            cache: true
        },
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 5,
        templateResult: formatRepo, // omitted for brevity, see the source of this page
        templateSelection: formatRepoSelection // omitted for brevity, see the source of this page                }
    });

    calculateTotal();
}

function removeRow($obj) {
    var $row_id = $($obj).parent().parent().data('row_id');
    $('#grid_row_'+$row_id).remove();
    calculateTotal();
}

function calculateTotalCubicFeet(obj) {
    var $row_id = $(obj).parent().parent().data('row_id');
    var $total_cubic_meter = parseFloat($(obj).val());
    var $total_cubic_feet = ($total_cubic_meter * 35.31467).toFixed(4);

    $('#opening_stock_detail_total_cubic_feet_'+$row_id).val($total_cubic_feet);
    calculateAmount(obj);
}

function calculateTotalCubicMeter(obj) {
    var $row_id = $(obj).parent().parent().data('row_id');
    var $total_cubic_feet = parseFloat($(obj).val());
    var $total_cubic_meter = ($total_cubic_feet / 35.31467).toFixed(4);

    $('#opening_stock_detail_total_cubic_meter_'+$row_id).val($total_cubic_meter);
    calculateAmount(obj);
}

function calculateAmount($obj) {
    var $row_id = $($obj).parent().parent().data('row_id');
    var $total_cubic_feet = parseFloat($('#opening_stock_detail_total_cubic_feet_' + $row_id).val());
    var $rate = parseFloat($('#opening_stock_detail_rate_' + $row_id).val());
    var $amount = ($total_cubic_feet * $rate).toFixed(2);
    console.log($row_id, $total_cubic_feet, $rate, $amount);
    $('#opening_stock_detail_amount_' + $row_id).val($amount);

    calculateTotal();
}

function calculateTotal() {
    var $total_qty = 0;
    var $total_cubic_meter = 0;
    var $total_cubic_feet = 0;
    var $net_amount = 0;
    $('#tblOpeningStockDetail tbody tr').each(function() {
        var $row_id = $(this).data('row_id');
        var $qty = $('#opening_stock_detail_qty_' + $row_id).val();
        var $cubic_meter = $('#opening_stock_detail_total_cubic_meter_' + $row_id).val();
        var $cubic_feet = $('#opening_stock_detail_total_cubic_feet_' + $row_id).val();
        var $amount = $('#opening_stock_detail_amount_' + $row_id).val();

        $total_qty += parseInt($qty);
        $total_cubic_meter += parseFloat($cubic_meter);
        $total_cubic_feet += parseFloat($cubic_feet);
        $net_amount += parseFloat($amount);
    })

    //console.log($net_amount, $total_cubic_meter, $total_cubic_meter * 35.3147);
    $('#total_qty').val($total_qty.toFixed(2));
    $('#total_cubic_meter').val(($total_cubic_meter).toFixed(4));
    $('#total_cubic_feet').val(($total_cubic_feet).toFixed(4));
    //$('#total_cubic_feet').val(($total_cubic_meter * 35.31467).toFixed(4));
    $('#net_amount').val($net_amount.toFixed(2));
}

function submitForm() {
    if($('#form').valid()) {
        $('.pull-right').find('.btn-primary').prop('disable',true);
        var dialog = bootbox.dialog({
            message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> Updating records please wait ...</p>',
            closeButton: false
        });
        dialog.find('.modal-content').css({
            'background-color': '#CDC',
            'font-weight' : 'bold',
            'color': '#F80',
            'font-size': '2em',
            'margin-top': function (){
                var w = $( window ).height();
                var b = $(".modal-dialog").height();
                // should not be (w-h)/2
                var h = (w-b)/2;
                return h+"px";
            }
        });
        $('#form').submit();
    }
}

function printDetailReport() {
    console.log("here");
    $('#forms').attr('action', $UrlPrintDetailReport).submit();
}
