$(document).ready(function () {
    var flag1 = 0;
    var flag2 = 0;
    var flag3 = 0;
    var flag4 = 0;
    var flag5 = 0;
    var flag6 = 0;

    //######### Check if User redirects from Account Or Home Page #########//
    var acct = getQueryStringValue("TradeAC");
    if (acct != "") {
        flag4 = 1;
    }
    else {
        flag1 = 1;
    }

    //######### CUSTOM ACCORDION #########//
    $("#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");
        }
        $('.origination-child').slideToggle();

    });
    $("#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");
        }
        $('.distribution-child').slideToggle();
    });
    $("#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");
        }
        $('.shipment-child').slideToggle();
    });

    $("#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");
        }

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

        $('.bloomerg-child').slideToggle();
    });


    $("#documentation").click(function () {
        flag6 = flag6 == 0 ? 1 : 0;

        if (flag6 == 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");
        }
        $('.documentation-child').slideToggle();
    });
});

$(window).load(function () {
    //######### If User Redirects from Trade Account
    var acc = getQueryStringValue("TradeAC");

    var RefNo = getQueryStringValue("SFMRefNo");
    if (acc != "") {
        $("#accounting").parent().css("border-color", "#0072C6").find("legend").css("background-color", "#DEEAFF").css("color", "#111");
        $("#imgaccounting").attr("src", "Content/Images/minus.png");
        $('.accounting-child').css("display", "block");

        //   URL to Get SFM String in Account Section
        var urlAccountSFMString = $('#urlAccountString').val() + "?SFMRefNo=" + RefNo;
        var urlAccountPNLSummary = $('#urlAccountPNLSummary').val() + "?sfmRefNo=" + RefNo;
        var urlAccountVessel = $('#urlAccountVessel').val() + "?_sfmRefNo=" + RefNo;

        //Hide Add button if SFM Reference number is empty!!!
        if (RefNo == null || RefNo == '') {
            $('#btnAddNewCost').hide();
            $('#btnAddNewIncome').hide();
        }

        // ******* Set Reference Number if Coming from Trade Account Menu *****
        if (SFM_Ref_Number.GetValue() == null) {
            SFM_Ref_Number.SetValue(RefNo);
        }

        //######### Fill VESSEL Name in Account Partial #########//
        $.ajax({
            type: "GET",
            contentType: "application.json",
            url: urlAccountVessel,
            data: null,
            cache: false,
            success: function (data) {
                txtAccountVessel.SetText(data);
            },
            error: function (xhr) {
                $('#PopDiv1').css('color', 'red');
                $("#PopDiv1").fadeIn
                $("#PopDiv2").html('Error occurred while getting vessel');
                $("#PopDiv1").fadeOut(8000);
            }
        });

        //######### Get String for both Income & Cost #########//
        //$.ajax({
        //    type: "GET",
        //    contentType: "application.json",
        //    data: null,
        //    url: urlAccountSFMString,
        //    cache: false,
        //    success: function (data) {
        //        // Fill Income Section
        //        $('#AccShipmentstringIncomeData').html(data);
        //        // Fill Cost Section
        //        $('#AccShipmentstringCostData').html(data);
        //    },
        //    error: function (xhr) {
        //        $('#PopDiv1').css('color', 'red');
        //        $("#PopDiv1").fadeIn
        //        $("#PopDiv2").html('Error occurred while getting data');
        //        $("#PopDiv1").fadeOut(8000);
        //    }
        //});
        // Get PNL Summary for Account
        $.ajax({
            type: "GET",
            contentType: "application.json",
            data: null,
            url: urlAccountPNLSummary,
            cache: false,
            success: function (data) {
                $('#AccountPNLData').html(data);
            },
            error: function (xhr) {
                $('#PopDiv1').css('color', 'red');
                $("#PopDiv1").fadeIn
                $("#PopDiv2").html('Error getting PNL data');
                $("#PopDiv1").fadeOut(8000);
            }
        })
        gvAccountIncome.PerformCallback();
        gvAccountCost.PerformCallback();

        //########### Set Validation on Entity if navigating from Accounting menu
        GetValidationForEntry();

      ///  GetValidationForIEntityEntry();  //For Now We are not checking validation, getting only thoes entity in Intercompany for which entry need to be made
    }
    else {
        // ######### Set Origination section to default opened section #########//
        $("#origination").parent().css("border-color", "#0072C6").find("legend").css("background-color", "#DEEAFF").css("color", "#111");
        $("#imgorigination").attr("src", "Content/Images/minus.png");
        $('.origination-child').css("display", "block");
    }
});

//######### Upload Trade Document #########//
var isFirstLoad = true;

function UploadDoc_Complete() {
    //Check to see if this is the first load of the iFrame
    if (isFirstLoad == true) {
        isFirstLoad = false;
        return;
    }

    //Reset the file form so the file won't get uploaded again
    var doc = document.getElementById("DocForm").reset();

    //Grab the content of the textarea we named jsonResult .  This should be loaded into 
    //the hidden iFrame.

    var newDoc = $.parseJSON($("#UploadTarget").contents().find("#jsonResult")[0].innerHTML);
    if (newDoc.IsValid == true) {
        //Reset all the fields
        $('#docFile').val('');
        popDocument_Name.SetText('');
        pcDocument.Hide();
        // Reload Gridview  Data
        gvDocument.PerformCallback();
    }
    //If there was an error, display it to the user
    if (newDoc.IsValid == false) {
        alert(newDoc.Message);
        pcDocument.Hide();
        return;
    }
}

// Get value from Query string
function getQueryStringValue(key) {
    return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}

function ValidateFileUploadandSubmit() {
    var fileUpload = $("#docFile").val();

    if (fileUpload=='') {
        alert('Please Select file to upload');
    }
    else {
        $("#DocForm").submit();
    }
};