(function ($) {

    //######### Get Data for Edit/Delete #########//
    $('#DistributionTab tr > td > a').click(function () {
        alert('JAS');
        var DocId = this.id.substr(3);                  //Get Only ID 
        var DoWhat = this.id.substr(0, 2);              //What to Do, Delete or Edit
        var urlEdit = $('#urlEdit').val() + '?DistId=';
        //************ Edit ************//

        if (DoWhat == 'ED') {
            $.ajax({
                type: "GET",
                conentType: "application/json",
                url: urlEdit + DocId,
                data: null,
                cache: 'false',
                success: function (data) {
                    $('#Distribution_Id').val(data.Distribution_Id);
                    DisInvestorBankId.Focus();
                    DisInvestorBankId.SetValue(data.Investor1_Bank_Id);
                    DisCP_Short_Name.Focus();
                    DisCP_Short_Name.SetValue(data.CP1_Id);
                    InvestorBankId2.Focus();
                    InvestorBankId2.SetValue(data.Investor2_Bank_Id);
                    DisCP_Short_Name2.Focus();
                    DisCP_Short_Name2.SetValue(data.CP2_Id);
                    DisAmount.Focus();
                    DisAmount.SetText(data.Amount);
                    DisMarginOverLibor.SetText(data.Margin_Over_Libor);
                    DisOtherCost.SetText(data.Other_Costs);
                    DisMaturityDate.Focus();
                    DisMaturityDate.SetText(data.Maturity_Date);
                    DisDiscountDate.Focus();
                    DisDiscountDate.SetText(data.Discount_Date);
                    LCNumber.SetText(data.LC_No);
                    disRemarks.SetText(data.Remarks)
                },
                error: function () { alert('Error!'); }
            });
            DistPopUP.Show();
        }
        //######### Delete #########//
        if (DoWhat == 'DE') {
            var url = $('#urlDelete').val() + '?isDelete=D';
            var cnf = confirm("Do you want to delete?");
            if (cnf == true) {
                var data = {
                    Distribution_Id: DocId,
                    SFM_Ref_No: $('#hdnGlobalRef').val(),
                    Investor_Bank_Id: 0,
                    CP_Id: 0,
                    Amount: 0,
                    Margin_Over_Libor: 0,
                    Other_Costs: 0,
                    Discount_Date: null,
                    Maturity_Date: null
                };
                $.ajax({
                    type: 'POST',
                    data: JSON.stringify(data),
                    url: url,
                    contentType: "application/json; charset=utf-8",
                    cache: false,
                    success: function (result) {
                        // Show Success Message
                        $("#PopDiv1").fadeIn();
                        $("#PopDiv2").html('Delete Operation Completed');
                        $("#PopDiv1").fadeOut(5000);
                        // Rebind Distribution List
                        $('#divDistributionData').html('');
                        $('#divDistributionData').html(result);
                    },

                    error: function (xhr) {
                        $("#PopDiv1").fadeIn();
                        $("#PopDiv2").html('Delete not Completed');
                        $("#PopDiv1").fadeOut(5000);
                    }
                })
            }
            else {
                $("#PopDiv1").fadeIn();
                $("#PopDiv2").html('Delete not Completed');
                $("#PopDiv1").fadeOut(5000);
            }
        }
        //######### Redirect to LC #########//
        if (DoWhat == 'LC') {
            var refNo = $('#hdnGlobalRef').val();
            var urlLocate = $('#urlLocate').val() + '?RefNo=' + refNo;
            window.location.href = urlLocate;
        }
        return false;
    });
    //************ End Get Data for Edit/Delete ************//

    $(function () {
        //#########  Fill Distribution List #########//
        $('#distribution').click(function () {
            var RefNo = $('#hdnGlobalRef').val();
            var url = $('#urlGetDistribution').val() + "?RefNo=";
            $.ajax({
                type: 'GET',
                contentType: 'application.json',
                url: url + RefNo,
                data: null,
                cache: 'false',
                success: function (data) {
                    $('#divDistributionData').html(data);
                },
                error: function (xhr) {
                    $("#PopDiv1").fadeIn();
                    $("#PopDiv2").html('Error occurd findig data!');
                    $("#PopDiv1").fadeOut(5000);
                }
            })
        });
        //************ End Fill Distribution List ************//
    })

    // Check for Access on Click of Distribution Div
//    var canAddDistribution = $('#canAddDistribution').val();
//    if (canAddDistribution == 'false') {
//        $('#btnAddNewDistribution').css("cursor", "not-allowed");
//        btnAddNewDistribution.SetEnabled(false);
//    }

})(jQuery);
