function GetPostType() {
    var value = chkManualPost.GetValue();
    $('#hdnPostType').val(value);
    if (value == 'S')
        txtPostingJVNo.SetVisible(false);
    else
        txtPostingJVNo.SetVisible(true);
}
function GetAccountTypeToPost(type) {
    $('#hdnDataType').val(type);
}
//################### Get name/id of selected Company in Posting list
var selectedIDs;
var AccMergedId;
var BSMergedId;
var singEntityId;
function onSelectionChanged(s, e) {
    s.GetSelectedFieldValues("Booking_Entity_Id", GetSelectedFieldValuesCallback);
    s.GetSelectedFieldValues("Acc_Merged_Id", GetSelectedAcc_Merged_IdValuesCallback);
    s.GetSelectedFieldValues("BS_Merged_Id", GetSelectedBS_Merged_IdValuesCallback);
}

function GetSelectedFieldValuesCallback(values) {
    $('#hdnCompanyName').val(values[0]);
}

function GetSelectedAcc_Merged_IdValuesCallback(values) {
    AccMergedId = "";
    for (var i = 0; i < values.length; i++) {
        if (AccMergedId == "")
            AccMergedId = AccMergedId + values[i];
        else
            AccMergedId = AccMergedId + "," + values[i];
    }
    if (AccMergedId)
        $('#hdnAcc_Merged_Id').val(AccMergedId);
}
function GetSelectedBS_Merged_IdValuesCallback(values) {
    BSMergedId = "";
    for (var i = 0; i < values.length; i++) {
        if (BSMergedId == "")
            BSMergedId = BSMergedId + values[i];
        else
            BSMergedId = BSMergedId + "," + values[i];
    }
    if (BSMergedId)
        $('#hdnBS_Merged_Id').val(BSMergedId);
}
// Check if JV No entered or not in manual posting
function PostTrade() {
    var JvNo = txtPostingJVNo.GetText();
    var _url = $('#urlPostData').val();
    var data = {
        JV_No: txtPostingJVNo.GetText(),
        Posting_Type: chkManualPost.GetValue(),
        BalanceSheetIds: $('#hdnBS_Merged_Id').val(),
        AccountingIds: $('#hdnAcc_Merged_Id').val()
    };
    if (txtPostingJVNo.GetVisible()) {
        if (JvNo == null || JvNo == '') {
            alert('Please enter JV No.');
        }
        else {
            // Post Trade Data
            $.ajax({
                url: _url,
                type: "POST",
                dataType: 'json',
                data: JSON.stringify(data),
                contentType: "application/json; charset=utf-8",
                cache: false,
                success: function (result) {
                   // alert(result);
                    if (result == 'JV Number already Exist!') {
                        return false;
                    }
                    else {
                        $('#hdnBS_Merged_Id').val('');
                        $('#hdnAcc_Merged_Id').val('');
                       // PostAlert.Hide();
                        // Reload all grids 
                        gvPostedAccount.PerformCallback(); // Perform Callback for Posted Data
                    }
                },
                error: function (result) {
                    //alert(result);
                }
            });
        }
    }
    else {
        //SAP Posting
        alert('SAP Posting not allowed as of now!');
    }
}


// Clear Hidden field if user cancel posting operation
function ClearDataIfPostClose() {
    $('#hdnBS_Merged_Id').val('');
    $('#hdnAcc_Merged_Id').val('');
}
// Print selected page.
function PrintThis(posting) {
    var divToPrint = document.getElementById(posting);
    newWin = window.open("");
    newWin.document.write(divToPrint.outerHTML);
    newWin.print();
    newWin.close();
}
// Set Vessel Name on posting page
function setVessel() {
    var sfmRef = TSFM_Ref_Number.GetText();
    var _url = $('#urlGetVessel').val() + "?_sfmRefNo=" + sfmRef;
    $.ajax({
        url: _url,
        type: "POST",
        dataType: 'json',
        data: null,
        contentType: "application/json; charset=utf-8",
        cache: false,
        success: function (result) {
            document.getElementById("spnPostingVessel").textContent = result;
        },
        Error: function () { }
    });
}

function ShowFinalPostingScreen() {
    var hdnAcc_Merged_Id = $('#hdnAcc_Merged_Id').val();
    var hdnBS_Merged_Id = $('#hdnBS_Merged_Id').val();
    var hdnCompanyName = $('#hdnCompanyName').val();
    if (!hdnCompanyName && !hdnAcc_Merged_Id && !hdnBS_Merged_Id)
        alert("Please select data for posting");
    else {
       // PostAlert.Show();
        gvPostingDetails.PerformCallback({ companyId: $('#hdnCompanyName').val() });
    }
}

var selectedJV;
function SelectionChanged(s, e) {
    s.GetSelectedFieldValues("JV_No", GetSelectedFieldsValuesCallback);
}
function GetSelectedFieldsValuesCallback(values) {
    try {
        selectedJV = "";
        for (var i = 0; i < values.length; i++) {
            selectedJV = values[i];
        }
    } finally {
        $('#hdnJVNo').val(selectedJV);
    }
}

//************* Reverse Post Account Date

function ReversePostData() {
    var _url = $('#urlReversePostData').val();
    var JvNo = $('#hdnJVNo').val();
    var data = {
        JV_No: $('#hdnJVNo').val()
    }
    if (JvNo == null || JvNo == '') {
        alert('Please select data to reverse');
    }
    else {
        $.ajax({
            method: 'POST',
            url: _url,
            dataType: 'json',
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            cache: false,
            success: function (result) {
                //alert(result);
                $('#hdnJVNo').val('');
                ReversePost.Hide();

                // Reload all grids 
                gvPostedAccount.PerformCallback(); // Perform Callback for Posted Data
            },
            error: function () {
                alert('error!');
            }
        });
    }
}
// Set Vessel Name on posting print page
function setVasselForPrint() {
    var sfmRef = TSFM_Ref_Number.GetText();
    var _url = $('#urlGetVessel').val() + "?_sfmRefNo=" + sfmRef;
    $.ajax({
        url: _url,
        type: "POST",
        dataType: 'json',
        data: null,
        contentType: "application/json; charset=utf-8",
        cache: false,
        success: function (result) {
            document.getElementById("spnPrintVessel").textContent = result;
        },
        Error: function () { }
    });
}


function PrintDetails(jvNo) {
    $('#hdnAcc_JVNo').val(jvNo);
    PrintPopup.Show();
    gvPostingPrintDetails.PerformCallback({ jvNo: jvNo });
}

//Reaload all grid on Filter button click
function ReloadPostingGrids() {
    var entity = LegalEntity.GetValue();
    if (entity == null) {
        alert('Please select Legal Entity');
        return false;
    }
    //alert(FromDate.GetText());
    gvBulkPostAccount.PerformCallback();
    gvPostedAccount.PerformCallback();
}