var sunSelectedAccounts;
$(document).ready(function () { });
//$(window).load(function () { });
function PostFxOpportunityData() {
    
    var selectedKeys = gvBloomberg.GetSelectedKeysOnPage();

    if (selectedKeys == '')
        alert('Please Select To Post')
    else {
        sunSelectedAccounts = selectedKeys.toString();
        var data = {
            bloombergtradeNos: sunSelectedAccounts,
            Entity: $('#Entity').val()
        }
        $.ajax({
            method: 'POST',
            url: 'CheckBaseRate',
            dataType: 'json',
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            cache: false,
            success: function (result) {
                if (result) {
                    SunBoombergPostPopUp.Show();
                }
                else {
                    alert("FX-Rate Not Found");
                }
            },
            error: function () {
                alert('error!');
            }
        });
    }
}
function PostFxOptionOppData() {
    
   // var selectedKeys = gvBloombergOption.GetSelectedKeysOnPage();
    var sunvalues = gvBloombergOption.GetSelectedKeysOnPage();
    var sunaccounts = "";
    for (var i = 0; i < sunvalues.length; i++) {
        sunaccounts += (sunaccounts == "" ? "" : "|") + sunvalues[i];
    }
    if (sunvalues == '')
        alert('Please Select To Post')
    else {
        var data = {
            bloombergtradeNos: sunaccounts,
            Entity: $('#Entity').val()
        }
        $.ajax({
            method: 'POST',
            url: 'CheckBaseRateOptionOpp',
            dataType: 'json',
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            cache: false,
            success: function (result) {
                if (result) {
                    SunBoombergOptionPostPopUp.Show();
                }
                else {
                    alert("FX-Rate Not Found");
                }
            },
            error: function () {
                alert('error!');
            }
        });
    }
}
function PostFxOptionData() {
    
    var selectedKeys = gvBloombergOption.GetSelectedKeysOnPage();
    if (selectedKeys == '')
        alert('Please Select To Post')
    else {
        sunSelectedAccounts = selectedKeys.toString();
        var data = {
            bloombergtradeNos: sunSelectedAccounts,
            Entity: $('#Entity').val()
        }
        $.ajax({
            method: 'POST',
            url: 'CheckBaseRateOption',
            dataType: 'json',
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            cache: false,
            success: function (result) {
                if (result) {
                    SunBoombergOptionPostPopUp.Show();
                }
                else {
                    alert("FX-Rate Not Found");
                }
            },
            error: function () {
                alert('error!');
            }
        });
    }
}

function PostFxDirectionalData() {
    
    var selectedKeys = gvBloombergDirectional.GetSelectedKeysOnPage();
    if (selectedKeys == '')
        alert('Please Select To Post')
    else {
        sunSelectedAccounts = selectedKeys.toString();

        var data = {
            bloombergtradeNos: sunSelectedAccounts,
            Entity: $('#Entity').val()
        }
        $.ajax({
            method: 'POST',
            url: 'CheckBaseRateDir',
            dataType: 'json',
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            cache: false,
            success: function (result) {
                if (result) {
                    SunBoombergDirectianalPostPopUp.Show();
                }
                else {
                    alert("FX-Rate Not Found");
                }
            },
            error: function () {
                alert('error!');
            }
        });
    }
}

function PostFxJVSGVegoilData() {

    var selectedKeys = gvBloombergJVSGVegoil.GetSelectedKeysOnPage();
    if (selectedKeys == '')
        alert('Please Select To Post')
    else {
        sunSelectedAccounts = selectedKeys.toString();

        var data = {
            bloombergtradeNos: sunSelectedAccounts,
            Entity: $('#Entity').val()
        }
        $.ajax({
            method: 'POST',
            url: 'CheckBaseRateJVSGVeg',
            dataType: 'json',
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            cache: false,
            success: function (result) {
                if (result) {
                    SunBoombergJVSGVegoilPostPopUp.Show();
                }
                else {
                    alert("FX-Rate Not Found");
                }
            },
            error: function () {
                alert('error!');
            }
        });
    }
}

function SunPostingDetail_BeginCallback(s, e) {
    e.customArgs['bloombergtradeNos'] = sunSelectedAccounts;
    e.customArgs['sourceType'] = 'FXOPP';
}
function SunPostingDetailOptionOpportunity_BeginCallback(s, e) {
    var sunvalues = gvBloombergOption.GetSelectedKeysOnPage();
    var sunaccounts = "";
    for (var i = 0; i < sunvalues.length; i++)
    {
        sunaccounts += (sunaccounts == "" ? "" : "|") + sunvalues[i];
    }
    $('#tradeids').val(sunaccounts);
    e.customArgs['bloombergtradeNos'] = sunaccounts;
        e.customArgs['sourceType'] = 'OptOpp';
}
function SunPostingDetailOptionOpportunityMaturity_BeginCallback(s, e) {
    var sunvalues = gvBloombergOption.GetSelectedKeysOnPage();
    var sunaccounts = "";
    for (var i = 0; i < sunvalues.length; i++) {
        sunaccounts += (sunaccounts == "" ? "" : "|") + sunvalues[i];
    }
    $('#tradeids').val(sunaccounts);
    e.customArgs['bloombergtradeNos'] = sunaccounts;
    e.customArgs['sourceType'] = 'OptOppM';
}
function SunPostingDetailOption_BeginCallback(s, e) {
    e.customArgs['bloombergtradeNos'] = sunSelectedAccounts;
    e.customArgs['sourceType'] = 'FXO';
}

function SunPostingDetailDirectional_BeginCallback(s, e) {
    e.customArgs['bloombergtradeNos'] = sunSelectedAccounts;
    e.customArgs['sourceType'] = 'FXD';
}

function SunPostingDetailJVSGVegoil_BeginCallback(s, e) {
    e.customArgs['bloombergtradeNos'] = sunSelectedAccounts;
    e.customArgs['sourceType'] = 'FXJVSGV';
}

function GetPostType() {
    
    var value = chkCostManualPost.GetValue();
    $('#hdnPostType').val(value);
    if (value == 'S') {
        txtPostingJVNo1.SetVisible(false);
        txtPostingJVNo2.SetVisible(false);
    }
    else
    {
        txtPostingJVNo1.SetVisible(true);
        txtPostingJVNo2.SetVisible(true);
    }
        
}

function GetAccountTypeToPost(type) {
    $('#hdnDataType').val(type);
}
function PostOptOppBloombergData() {
    
    if (chkCostManualPost.GetValue() == "M") {
        if (txtPostingJVNo1.GetValue() == null) {
            alert('Please enter JV No.');
            return;
        }
        else if ($('#sourcetype').val() == "OptOpp")
        {
            var jvNo = txtPostingJVNo1.GetValue();
            var str = jvNo.toLowerCase();
            if ((str.indexOf("cat-") < 0) || (str.indexOf("ared-") < 0) || (str.indexOf(",") < 0)) {
                alert('Please enter JV No in correct format (for example, CAT-1234,ARED-5678)');
                return;
            }
        }
        
    }
    var data = {
        bloombergtradeNos: $('#tradeids').val(),
        sourcetype: $('#sourcetype').val(),
        postingType: chkCostManualPost.GetValue(),
        journalNo: txtPostingJVNo1.GetText(),
        Strategy: $('#Strategy').val(),
        Currency: $('#Currency').val(),
        MaturityDate: $('#MaturityDate').val(),
        Entity: $('#Entity').val()
    }
    $.ajax({
        method: 'POST',
        url: 'SAPPostAccount',

        dataType: 'json',
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        cache: false,
        success: function (result) {
            alert(result.message);
            location.reload();
        },
        error: function () {
            alert('error!');
            location.reload();
        }
    });
}
// Post Sun accounting data
function PostBloombergData() {
    if (txtPostingJVNo2.GetValue() == null && chkCostManualPost.GetValue() == "M") {
        alert('Please enter JV No 2.');
        return;
    }
    data = {
        bloombergtradeNos: sunSelectedAccounts,
        sourcetype: 'FXOPP',
        postingType: chkCostManualPost.GetValue(),
        journalNo1: txtPostingJVNo1.GetText(),
        journalNo2: txtPostingJVNo2.GetText(),
        Strategy: $('#Strategy').val(),
        Currency: $('#Currency').val(),
        MaturityDate: $('#MaturityDate').val(),
        Entity: $('#Entity').val()
    }
    $.ajax({
        method: 'POST',
        url: 'SAPPostAccount',

        dataType: 'json',
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        cache: false,
        success: function (result) {
            alert(result.message);
            window.location = '\FxOpportunityPosting?Strategy=' + result.Strategy + '&Currency=' + result.Currency + '&maturityDate=' + result.maturityDate + '&Entity=' + result.Entity;
        },
        error: function () {
            alert('error!');
        }

    });
}

function PostBloombergDirectionalData() {
    if (txtPostingJVNo2.GetValue() == null && chkCostManualPost.GetValue() == "M") {
        alert('Please enter JV No.');
        return;
    }
    var data = {
        bloombergtradeNos: sunSelectedAccounts,
        sourcetype: 'FXD',
        postingType: chkCostManualPost.GetValue(),
        journalNo1: txtPostingJVNo1.GetText(),
        journalNo2: txtPostingJVNo2.GetText(),
        Strategy: $('#Strategy').val(),
        Currency: $('#Currency').val(),
        MaturityDate: $('#MaturityDate').val(),
        Entity: $('#Entity').val()
    }
    $.ajax({
        method: 'POST',
        url: 'SAPPostAccount',

        dataType: 'json',
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        cache: false,
        success: function (result) {
            alert(result.message);
            window.location = '\FxDirectionalPosting?Strategy=' + result.Strategy + '&Currency=' + result.Currency + '&maturityDate=' + result.maturityDate + '&Entity=' + result.Entity;
        },
        error: function () {
            alert('error!');
        }
    });
}

function PostBloombergJVSGVegoilData() {
    if (txtPostingJVNo2.GetValue() == null && chkCostManualPost.GetValue() == "M") {
        alert('Please enter JV No.');
        return;
    }
    var data = {
        bloombergtradeNos: sunSelectedAccounts,
        sourcetype: 'FXJVSGV',
        postingType: chkCostManualPost.GetValue(),
        journalNo1: txtPostingJVNo1.GetText(),
        journalNo2: txtPostingJVNo2.GetText(),
        Strategy: $('#Strategy').val(),
        Currency: $('#Currency').val(),
        MaturityDate: $('#MaturityDate').val(),
        Entity: $('#Entity').val()
    }
    $.ajax({
        method: 'POST',
        url: 'SAPPostAccount',

        dataType: 'json',
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        cache: false,
        success: function (result) {
            alert(result.message);
            window.location = '\FxJVSGVegoilPosting?Strategy=' + result.Strategy + '&Currency=' + result.Currency + '&maturityDate=' + result.maturityDate + '&Entity=' + result.Entity;
        },
        error: function () {
            alert('error!');
        }
    });
}

function PostBloombergOptionData() {
    if (txtPostingJVNo1.GetValue() == null && chkCostManualPost.GetValue() == "M") {
        alert('Please enter JV No.');
        return;
    }
    var data = {
        bloombergtradeNos: sunSelectedAccounts,
        sourcetype: $('#sourcetype').val(),
        postingType: chkCostManualPost.GetValue(),
        journalNo2: txtPostingJVNo1.GetText(),
        Strategy: $('#Strategy').val(),
        Currency: $('#Currency').val(),
        MaturityDate: $('#MaturityDate').val(),
        Entity: $('#Entity').val()
    }
    $.ajax({
        method: 'POST',
        url: 'SAPPostAccount',

        dataType: 'json',
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        cache: false,
        success: function (result) {
            alert(result.message);
            window.location = '\FxOptionPosting?Strategy=' + result.Strategy + '&Currency=' + result.Currency + '&maturityDate=' + result.maturityDate + '&Entity=' + result.Entity;
        },
        error: function () {
            alert('error!');
        }
    });
}

function ReversePostData(s, e,sourceType,entity) {
    var selectedKey = s.GetRowKey(e.visibleIndex);;
    var data = {
        recordId: String(selectedKey),
        sourceType: sourceType,
        entity: entity
    }
    ReversePostModal.Show();
    $.ajax({
        method: 'POST',
        url: 'BloombergReversePosting',
        dataType: 'json',
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        cache: false,
        success: function (result) {
            ReversePostModal.Hide();

            alert(result);
            window.location.href = document.URL;
        },
        error: function () {
            alert('error!');
        }
    });
}

