var currentEditableVisibleIndexIncome;
var preventEndEditOnLostFocusIncome = false;
var lastBE_IDIncome;
var setValueFlagIncome;
var setpnlValueFlagIncome;
var setpnlCodeValueFlagIncome;
var setICValueFlagIncome;

// ####### Booking Entity Combo Selected index change
function BECombo_SelectedIndexChanged(s, e) {
    //######## Function to get SAP PNL by Booking Entity
    GetIncomeSAP_PNLByBE();
    var currentValue = s.GetValue();
    if (lastBE_IDIncome == currentValue) {
        if (IncomePC_Id.GetSelectedIndex() < 0)
            IncomePC_Id.SetSelectedIndex(-1);

        if (IncomePNLCombo.GetSelectedIndex() < 0)
            IncomePNLCombo.SetSelectedIndex(-1);

        if (IncomePNLCodeCombo.GetSelectedIndex() < 0)
            IncomePNLCodeCombo.SetSelectedIndex(-1);

        if (IncomeInterComp_Entity_Id.GetSelectedIndex() < 0)
            IncomeInterComp_Entity_Id.SetSelectedIndex(-1);

        return;
    }
    lastBE_IDIncome = currentValue;

    if (IncomeBooking_Entity_Id.GetValue() != null) {
        if (CheckpermissibleEntry(currentValue)) {
            IncomeInterComp_Entity_Id.PerformCallback(); // Bind Income Intercompany
            IncomePC_Id.PerformCallback();         // Bind Income PC
            IncomePNLCombo.PerformCallback();  // Bind Income PNL Description
            IncomePNLCodeCombo.PerformCallback();  // Bind Income PNLCode
        }
        else {
            alert('You can\'t create entry for this Entity');
            IncomeBooking_Entity_Id.SetValue(null);
            IncomeSAP_Account_Id.SetText(null);
            IncomePNLCombo.SetValue(null);
            IncomePNLCodeCombo.SetValue(null);
            IncomeInterComp_Entity_Id.SetValue(null);
            IncomePC_Id.SetValue(null);
        }
    }
}

// ####### Intercompany Combo Begin Callback
function IncomeInterCo_BeginCallback(s, e) {
    e.customArgs['CompanyID'] = lastBE_IDIncome;
    e.customArgs['sfmRefNo'] = SFM_Ref_Number.GetValue();
}
// ####### Profit Center Combo Begin Callback
function IncomePcCombo_BeginCallback(s, e) {
    e.customArgs['CompanyID'] = lastBE_IDIncome;
}

// ####### PNL Account Combo Begin Callback
function IncomePNL_BeginCallback(s, e) {
    e.customArgs['CompanyID'] = lastBE_IDIncome;
}

// ####### PNL Account code Combo Begin Callback
function IncomePNLCode_BeginCallback(s, e) {
    e.customArgs['CompanyID'] = lastBE_IDIncome;
}

// ####### Initialize all variables for Gridview
function IntializeGlobalVariables(gvAccountIncome) {
    lastBE_IDIncome = -1;
    currentEditableVisibleIndexIncome = -1;
    setValueFlagIncome = -1;
    setpnlValueFlagIncome = -1;
    setpnlCodeValueFlagIncome = -1;
    setICValueFlagIncome = -1;
}

// ####### Gridview Init
function OnIncomeInit(s, e) {
    IntializeGlobalVariables(s);
}

// ####### Gridview End Callback
function OnIncomeEndCallback(s, e) {
    IntializeGlobalVariables(s);
    // Get summary and check whether Account Entry Exist or not
   // GetIncomeSummary();
    //CheckIncomeAccountForBSEntry(SFM_Ref_Number.GetValue(), 'I');
    gvAccountIncomeBalanceSheet.PerformCallback();
    //gvBookingEntityBS.PerformCallback();  // Reload Balance Sheet BS
}

// ####### InterCompany Combo End Callback
function IncomeInterCo_EndCallback(s, e) {
    if (setICValueFlagIncome == -1) {
        if (s.GetSelectedIndex() == -1) {
            s.SetSelectedIndex(-1);
        }
    }
    else if (setICValueFlagIncome > -1) {
        IncomeInterComp_Entity_Id.SetSelectedItem(IncomeInterComp_Entity_Id.FindItemByValue(setICValueFlagIncome));
        setICValueFlagIncome = -1;
    }
}

// ####### Profit Center Combo End Callback
function IncomePcCombo_EndCallback(s, e) {
    if (setValueFlagIncome == -1) {
        if (s.GetSelectedIndex() == -1) {
            s.SetSelectedIndex(-1);
        }
    }
    else if (setValueFlagIncome > -1) {
        IncomePC_Id.SetSelectedItem(IncomePC_Id.FindItemByValue(setValueFlagIncome));
        setValueFlagIncome = -1;
    }
}
// ####### PNL Account Description Combo End Callback
function IncomePNL_EndCallback(s, e) {
    if (setpnlValueFlagIncome == -1) {
        if (s.GetSelectedIndex() == -1) {
            s.SetSelectedIndex(-1); // To Show Select as default
        }
    }
    else if (setpnlValueFlagIncome > -1) {
        IncomePNLCombo.SetSelectedItem(IncomePNLCombo.FindItemByValue(setpnlValueFlagIncome));
        setpnlValueFlagIncome = -1;
    }
}

// ####### PNL Account Code Combo End Callback
function IncomePNLCode_EndCallback(s, e) {
    if (setpnlCodeValueFlagIncome == -1) {
        if (s.GetSelectedIndex() == -1) {
            s.SetSelectedIndex(-1); // To Show Select as default
        }
    }
    else if (setpnlCodeValueFlagIncome > -1) {
        IncomePNLCodeCombo.SetSelectedItem(IncomePNLCodeCombo.FindItemByValue(setpnlCodeValueFlagIncome));
        setpnlCodeValueFlagIncome = -1;
    }
}

// ####### Gridview Edit Start
function OnIncomeBatchEditStartEditing(s, e) {
    currentEditableVisibleIndexIncome = e.visibleIndex;
    $('#hdnValidateInterCompanyCode').val('');
    var currentBEID = gvAccountIncome.batchEditApi.GetCellValue(currentEditableVisibleIndexIncome, "Booking_Entity_Id");
    var pcIDColumn = s.GetColumnByField("PC_Id");
    var pnlIDColumn = s.GetColumnByField("PNL_Account_No");
    var pnlCodeIDColumn = s.GetColumnByField("PNL_Account_code");
    var IC_IDColumn = s.GetColumnByField("InterComp_Entity_Id");

    if (!e.rowValues.hasOwnProperty(pcIDColumn.index) || !e.rowValues.hasOwnProperty(pnlIDColumn.index) || !e.rowValues.hasOwnProperty(IC_IDColumn.index) || !e.rowValues.hasOwnProperty(pnlCodeIDColumn.index)) {
        return;
    }
    var cellInfo = e.rowValues[pcIDColumn.index];
    var pnlcellInfo = e.rowValues[pnlIDColumn.index];
    var pnlCodecellInfo = e.rowValues[pnlCodeIDColumn.index];
    var ICcellInfo = e.rowValues[IC_IDColumn.index];

    if (lastBE_IDIncome == currentBEID) {
        if (IncomePC_Id.FindItemByValue(cellInfo.value) != null)
            IncomePC_Id.SetValue(cellInfo.value);
        else
            RefreshPCData(cellInfo);

        if (IncomePNLCombo.FindItemByValue(pnlcellInfo.value) != null)
            IncomePNLCombo.SetValue(pnlcellInfo.value);
        else
            RefreshPNLData(pnlcellInfo);

        if (IncomePNLCodeCombo.FindItemByValue(pnlCodecellInfo.value) != null)
            IncomePNLCodeCombo.SetValue(pnlCodecellInfo.value);
        else
            RefreshPNLCodeData(pnlCodecellInfo);

        if (IncomeInterComp_Entity_Id.FindItemByValue(ICcellInfo.value) != null)
            IncomeInterComp_Entity_Id.SetValue(ICcellInfo.value);
        else
            RefreshIntercompanyData(ICcellInfo);
    }
    else {
        if (currentBEID == null) {
            IncomePC_Id.SetSelectedIndex(-1);
            IncomePNLCombo.SetSelectedIndex(-1);
            IncomePNLCodeCombo.SetSelectedIndex(-1);
            IncomeInterComp_Entity_Id.SetSelectedIndex(-1);
            return;
        }
        lastBE_IDIncome = currentBEID;
        RefreshPCData(cellInfo);
        RefreshPNLData(pnlcellInfo);
        RefreshPNLCodeData(pnlCodecellInfo);
        RefreshIntercompanyData(ICcellInfo);
    }
}
// #######  Gridview End Edit
function OnIncomeBatchEditEndEditing(s, e) {
    currentEditableVisibleIndexIncome = -1;
    var pcIDColumn = s.GetColumnByField("PC_Id");
    var pnlIDColumn = s.GetColumnByField("PNL_Account_No");
    var pnlCodeIDColumn = s.GetColumnByField("PNL_Account_code");
    var IC_IDColumn = s.GetColumnByField("InterComp_Entity_Id");

    if (!e.rowValues.hasOwnProperty(pcIDColumn.index) || !e.rowValues.hasOwnProperty(pnlIDColumn.index) || !e.rowValues.hasOwnProperty(IC_IDColumn.index) || !e.rowValues.hasOwnProperty(pnlCodeIDColumn.index))
        return;

    var cellInfo = e.rowValues[pcIDColumn.index];
    var pnlcellInfo = e.rowValues[pnlIDColumn.index];
    var pnlCodecellInfo = e.rowValues[pnlCodeIDColumn.index];
    var ICcellInfo = e.rowValues[IC_IDColumn.index];

    cellInfo.value = IncomePC_Id.GetValue();
    cellInfo.text = IncomePC_Id.GetText();
    IncomePC_Id.SetValue(null);

    pnlcellInfo.value = IncomePNLCombo.GetValue();
    pnlcellInfo.text = IncomePNLCombo.GetText();
    IncomePNLCombo.SetValue(null);

    pnlCodecellInfo.value = IncomePNLCodeCombo.GetValue();
    pnlCodecellInfo.text = IncomePNLCodeCombo.GetText();
    IncomePNLCodeCombo.SetValue(null);

    ICcellInfo.value = IncomeInterComp_Entity_Id.GetValue();
    ICcellInfo.text = IncomeInterComp_Entity_Id.GetText();
    IncomeInterComp_Entity_Id.SetValue(null);

}

// ####### Intercompany Combo Reload
function RefreshIntercompanyData(ICcellInfo) {
    setICValueFlagIncome = ICcellInfo.value;
    IncomeInterComp_Entity_Id.PerformCallback();
}


// ####### Profit Center Combo Reload
function RefreshPCData(cellInfo) {
    setValueFlagIncome = cellInfo.value;
    IncomePC_Id.PerformCallback();
}

// ####### PNL Account Combo Reload
function RefreshPNLData(pnlcellInfo) {
    setpnlValueFlagIncome = pnlcellInfo.value;
    IncomePNLCombo.PerformCallback();
}
RefreshPNLCodeData(pnlCodecellInfo)
{
    setpnlCodeValueFlagIncome = pnlCodecellInfo.value;
    IncomePNLCodeCombo.PerformCallback();
}

function IncomePcCombo_KeyDown(s, e) {
    var keyCode = ASPxClientUtils.GetKeyCode(e.htmlEvent);
    if (keyCode !== ASPxKey.Tab && keyCode !== ASPxKey.Enter) {
        return;
    }
    var moveActionName = e.htmlEvent.shiftKey ? "MoveFocusBackward" : "MoveFocusForward";
    if (gvAccountIncome.batchEditApi[moveActionName]()) {
        ASPxClientUtils.PreventEventAndBubble(e.htmlEvent);
        preventEndEditOnLostFocusIncome = true;
    }
}
function IncomePNL_KeyDown(s, e) {
    var keyCode = ASPxClientUtils.GetKeyCode(e.htmlEvent);
    if (keyCode !== ASPxKey.Tab && keyCode !== ASPxKey.Enter) return;
    var moveActionName = e.htmlEvent.shiftKey ? "MoveFocusBackward" : "MoveFocusForward";
    if (gvAccountIncome.batchEditApi[moveActionName]()) {
        ASPxClientUtils.PreventEventAndBubble(e.htmlEvent);
        preventEndEditOnLostFocusIncome = true;
    }
}

function IncomePNLCode_KeyDown(s, e) {
    var keyCode = ASPxClientUtils.GetKeyCode(e.htmlEvent);
    if (keyCode !== ASPxKey.Tab && keyCode !== ASPxKey.Enter) return;
    var moveActionName = e.htmlEvent.shiftKey ? "MoveFocusBackward" : "MoveFocusForward";
    if (gvAccountIncome.batchEditApi[moveActionName]()) {
        ASPxClientUtils.PreventEventAndBubble(e.htmlEvent);
        preventEndEditOnLostFocusIncome = true;
    }
}

function IncomeInterCo_KeyDown(s, e) {
    var keyCode = ASPxClientUtils.GetKeyCode(e.htmlEvent);
    if (keyCode !== ASPxKey.Tab && keyCode !== ASPxKey.Enter) return;
    var moveActionName = e.htmlEvent.shiftKey ? "MoveFocusBackward" : "MoveFocusForward";
    if (gvAccountIncome.batchEditApi[moveActionName]()) {
        ASPxClientUtils.PreventEventAndBubble(e.htmlEvent);
        preventEndEditOnLostFocusIncome = true;
    }
}

// ####### Get PNL Code on PNL Account Combo Selected Index change or Lost Focus
function IncomePNL_SelectedIndexChanged(s, e) {
    var BSId = s.GetValue();
    //Check If the account description is “Purchase Intercomp. Or Sales Intercomp”
    if (s.GetText().includes('Intercomp')) {
        $('#hdnValidateInterCompanyCode').val('1');
    }
    else {
        $('#hdnValidateInterCompanyCode').val('');
    }
    IncomePNLCodeCombo.SetValue(BSId);
}

// ####### Get PNL Code on PNL Account Combo Selected Index change or Lost Focus
function IncomePNLCode_SelectedIndexChanged(s, e) {
    var PNLCodeId = s.GetValue();

    if (s.GetText().includes('Intercomp')) {
        $('#hdnValidateInterCompanyCode').val('1');
    }
    else {
        $('#hdnValidateInterCompanyCode').val('');
    }
    IncomePNLCombo.SetValue(PNLCodeId);
}

//####### Check Whether Account Entry is permissible or not
function CheckpermissibleEntry(currentValue) {
    var permissibleEntites = $('#hdnParamforAccountBEValidation').val();
    var arr = permissibleEntites.split(',');

    for (i = 0; i < arr.length; i++) {
        if (arr[i] == currentValue) {
            return true;
        }
    }
    return false;
}
