
function getQueryStringValue(key) {
    return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}
//######### Function to Search Trade Data  #########//

function GetViewTradeData(RefNo, flag) {
    var url = $('#urlGetOrigination').val() + "?SFMRefNo=" + RefNo; 
    var urlPNL = $('#urlGetPNL').val() + "?SFMRefNo=" + RefNo; 

    $.ajax({
        type: "POST",
        contentType: 'application/json',
        url: url,
        data: null,
        cache: 'false',
        success: function (data) {
            ViewSFM_Ref_Number.SetValue(data.SFM_Ref_No);
            $('#spSFM_Ref_No').text(data.SFM_Ref_No != null ? data.SFM_Ref_No : '');
            $('#spParent_SFM_Ref_No').text(data.Parent_SFM_Ref_No != null ? data.Parent_SFM_Ref_No : '');
            $("#spParent_SFM_Ref_No").attr("href", data.Parent_SFM_Ref_No != null ? 'OpenTrades?SFMRefNo=' + data.Parent_SFM_Ref_No : ''); // Make parent number as link
            if (data.CanAccessParent == false) {
                $('#spParent_SFM_Ref_No').css("cursor", "not-allowed");
                $("#spParent_SFM_Ref_No").attr("disabled", true);
                $("#spParent_SFM_Ref_No").removeAttr("href");
            }
            else {
                $('#spParent_SFM_Ref_No').css("cursor", "pointer");
                $("#spParent_SFM_Ref_No").attr("disabled", false);
            }
            $('#spTrade_Status_Name').text(data.Trade_Status_Name != null ? data.Trade_Status_Name : '');
            $('#spCreated_By').text(data.Created_By != null ? data.Created_By : '');
            $('#spCreated_On').text(data.Created_On != null ? data.Created_On : '');

            //********** If Click on Search and Data not found  ***********//
            if (flag == "search") {
                if (data.SFM_Ref_No == null) {
                    $('#spSFMViewPartialError').text("Sorry, No Record found with Ref No: " + RefNo);
                    $('#spSFM_Ref_No').text("");
                    $('#spParent_SFM_Ref_No').text("");
                    $('#spTrade_Status_Name').text("");
                    $('#spCreated_By').text("");
                    $('#spCreated_On').text("");
                }
                else {
                    $('#spSFMViewPartialError').text("");
                }
            }

            //********* IF Loading for New Entry *********//

        },
        error: function () {
            $('#PopDiv1').css('color', 'red');
            $("#PopDiv1").fadeIn();
            $("#PopDiv2").html('Error finding data!');
            $("#PopDiv1").fadeOut(8000);
        }
    });

    $.ajax({
        type: "POST",
        contentType: 'application/json',
        url: urlPNL,
        data: null,
        cache: 'false',
        success: function (data) {
            $('#divAccountPNL').html(data);
        },
        error: function () { }
    });
}


//######### ON PAGE LOAD IF QUERY STRING IS NOT NULL #########//
$(window).load(function () {
    var Ref = getQueryStringValue("SFMRefNo");
    ViewSFM_Ref_Number.SetValue(Ref);
    GetViewTradeData(ViewSFM_Ref_Number.GetText(), '');

    //######### ON CLICK OF SEARCH BUTTON #########//
    $("#btnViewSubmit").click(function () {
        var RefNo = ViewSFM_Ref_Number.GetText();
        if (RefNo == null || RefNo == "Enter SFM Ref No.") {
            $('#spSFMViewPartialError').text("Please Select Reference number");
            return false;
        }
        else {
            GetViewTradeData(RefNo, 'search');
            RearrangeSectionOnSearch();
        }
    });
});

/* REARRANGE ALL SECTION AND SHOW ONLY ORIGINATION SECTION ON SEARCH BUTTON CLICK*/
function RearrangeSectionOnSearch() {
    //### Reset all flag
    var flag1 = 1;
    var flag2 = 0;
    var flag3 = 0;
    var flag4 = 0;
    var flag5 = 0;
    //### Collapse all Section and expand only Origination section
    $('.distribution-child').slideUp();
    $('.shipment-child').slideUp();
    $('.accounting-child').slideUp();
    $('.documentation-child').slideUp();
    $('.origination-child').slideDown();

    //### Change Icon of each section to '+' and Origination to '-'
    $("#distribution").parent().css("border-color", "#fff").find("legend").css("background-color", "#0a67a8").css("color", "#fff");
    $("#imgdistribution").attr("src", "../Content/Images/plus.png");

    $("#shipment").parent().css("border-color", "#fff").find("legend").css("background-color", "#0a67a8").css("color", "#fff");
    $("#imgshipment").attr("src", "../Content/Images/plus.png");

    $("#accounting").parent().css("border-color", "#fff").find("legend").css("background-color", "#0a67a8").css("color", "#fff");
    $("#imgaccounting").attr("src", "../Content/Images/plus.png");

    $("#documentation").parent().css("border-color", "#fff").find("legend").css("background-color", "#0a67a8").css("color", "#fff");
    $("#imgdocumentation").attr("src", "../Content/Images/plus.png");

    $("#origination").parent().css("border-color", "#0072C6").find("legend").css("background-color", "#DEEAFF").css("color", "#111");
    $("#imgorigination").attr("src", "../Content/Images/minus.png");

    //### Set Toggle icon of each section
    $("#origination").click(function () {
        flag1 = flag1 == 0 ? 1 : 0;

        if (flag1 == 0) {
            $("#origination").parent().css("border-color", "#fff").find("legend").css("background-color", "#0a67a8").css("color", "#fff");
            $("#imgorigination").attr("src", "../Content/Images/plus.png");

        }
        else {
            $("#origination").parent().css("border-color", "#0072C6").find("legend").css("background-color", "#DEEAFF").css("color", "#111");
            $("#imgorigination").attr("src", "../Content/Images/minus.png");
        }

    });
    $("#distribution").click(function () {
        flag2 = flag2 == 0 ? 1 : 0;

        if (flag2 == 0) {
            $("#distribution").parent().css("border-color", "#fff").find("legend").css("background-color", "#0a67a8").css("color", "#fff");
            $("#imgdistribution").attr("src", "../Content/Images/plus.png");

        }
        else {
            $("#distribution").parent().css("border-color", "#0072C6").find("legend").css("background-color", "#DEEAFF").css("color", "#111");
            $("#imgdistribution").attr("src", "../Content/Images/minus.png");
        }
    });
    $("#shipment").click(function () {
        flag3 = flag3 == 0 ? 1 : 0;

        if (flag3 == 0) {
            $("#shipment").parent().css("border-color", "#fff").find("legend").css("background-color", "#0a67a8").css("color", "#fff");
            $("#imgshipment").attr("src", "../Content/Images/plus.png");
        }
        else {
            $("#shipment").parent().css("border-color", "#0072C6").find("legend").css("background-color", "#DEEAFF").css("color", "#111");
            $("#imgshipment").attr("src", "Content/Images/minus.png");
        }
    });

    $("#accounting").click(function () {
        flag4 = flag4 == 0 ? 1 : 0;
        if (flag4 == 0) {
            $("#accounting").parent().css("border-color", "#fff").find("legend").css("background-color", "#0a67a8").css("color", "#fff");
            $("#imgaccounting").attr("src", "Content/Images/plus.png");
        }
        else {
            $("#accounting").parent().css("border-color", "#0072C6").find("legend").css("background-color", "#DEEAFF").css("color", "#111");
            $("#imgaccounting").attr("src", "Content/Images/minus.png");
        }
    });
    $("#documentation").click(function () {
        flag5 = flag5 == 0 ? 1 : 0;

        if (flag5 == 0) {
            $("#documentation").parent().css("border-color", "#fff").find("legend").css("background-color", "#0a67a8").css("color", "#fff");
            $("#imgdocumentation").attr("src", "Content/Images/plus.png");
        }
        else {
            $("#documentation").parent().css("border-color", "#0072C6").find("legend").css("background-color", "#DEEAFF").css("color", "#111");
            $("#imgdocumentation").attr("src", "Content/Images/minus.png");
        }
    });
}

function PrintTrade() {
    //************ Hide unnecessary item before print ************//

    $('#inner-header').hide();
    $('#cssmenu').hide();
    $('#mnu').hide();
    $('#divButton').hide();
    window.print();
    //********** Show all item after print **********//
    $('#inner-header').show();
    $('#cssmenu').show();
    $('#mnu').show();
    $('#divButton').show();
    e.preventDefault();
}