

let bounceNumber_bounceRow = 2;
let nBounce_bounceRow = [];
let d1d2_bounceRow = [];
let d1d3_bounceRow = [];
let d2d1_bounceRow = [];
let d2d3_bounceRow = [];
let d3d1_bounceRow = [];
let d3d2_bounceRow = [];
let helpBouncy_bounceRow = false;
let bouncyDisplay_bounceRow = false;
let useBouncyDozenBet_bounceRow = false;
let useBouncyRowBet_bounceRow = false;

let useBouncyDozenBet_progress_loss_bounceRow = true;
let useBouncyDozenBet_progress_win_bounceRow = false;
let useBouncyDozenBet_previous_loss_bounceRow = false;
let useBouncyDozenBet_previous_win_bounceRow = false;
let useBouncyDozenBet_reset_any_win_bounceRow = false;
let bouncyDozenBetPlaced_bounceRow = false;

let bouncyProfit_bounceRow = 0;

function bounce_bounceRow() {

    let container = document.createElement('div');
    container.style.marginLeft = '25px';
    container.style.marginBottom = '15px';
    container.style.fontSize = '20px';
    container.id = 'bouncy-tool_bounceRow';

    let savedColor = localStorage.getItem(container.id + '-containerIdbackgroundColor_bounceRow');
    if (savedColor) {
        container.style.backgroundColor = savedColor;
    }

    let colorChangerQuickSpin = quickSpin_colorChanger(container.id);
    container.append(colorChangerQuickSpin);



    let titleDiv = document.createElement('div');
    titleDiv.style.display = 'flex';
    titleDiv.style.alignItems = 'center';
    titleDiv.style.marginLeft = '25px';


    // Create the title span
    let titleSpan = document.createElement('span');
    titleSpan.innerHTML = '<h2>Bouncy Rows</h2>';
    titleSpan.style.marginRight = '10px';

    let radioDozenRow = createRadioButtons();
    let betForm = createDynamicForm_bounceRow();


    // Create the help button
    const button = document.createElement('button');
    button.id = 'how-to-bouncy_bounceRow';
    button.textContent = "How to";
    button.style.padding = '5px';
    button.style.marginRight = '5px';
    button.onclick = toggleButtonHelpBouncy_bounceRow;



    var bouncyCheckbox = document.createElement('input');
    bouncyCheckbox.type = 'checkbox';
    bouncyCheckbox.id = "bouncy-display_bounceRow";
    bouncyCheckbox.name = "bouncyDisplay";
    bouncyCheckbox.placeholder = "";
    bouncyCheckbox.checked = bouncyDisplay_bounceRow;
    bouncyCheckbox.style.transform = 'scale(2)';
    bouncyCheckbox.style.transformOrigin = 'top left';
    bouncyCheckbox.style.marginBottom = '12px';
    bouncyCheckbox.style.marginLeft = '10px';
    bouncyCheckbox.addEventListener('change', function () {
        bouncyDisplay_bounceRow = this.checked;
        appendOrRemoveAlert_bounceRow();
        addAllItemsToAlertWindow();
    });


    // Group Label
    var bouncyCheckboxLabel = document.createElement('label');
    bouncyCheckboxLabel.htmlFor = 'bouncy-display_bounceRow';
    bouncyCheckboxLabel.style.marginLeft = '15px';
    bouncyCheckboxLabel.style.fontSize = '20px';
    bouncyCheckboxLabel.appendChild(document.createTextNode('Show alert above board.'));


    // Append the title span and button to the title div
    titleDiv.appendChild(titleSpan);


    titleDiv.appendChild(button);


    titleDiv.appendChild(bouncyCheckbox);
    titleDiv.appendChild(bouncyCheckboxLabel);




    const numberInput = document.createElement('input');
    style_textbox_container(numberInput);
    numberInput.type = 'number';
    numberInput.id = 'number-input-bouncy_bounceRow';
    numberInput.value = bounceNumber_bounceRow

    // Add event listener for the input event
    //numberInput.addEventListener('input', bouncyUpdate);
    // Button 1
    let LookBackButton = document.createElement('button');
    LookBackButton.id = 'update-bouncy-button_bounceRow';
    LookBackButton.textContent = 'Update';
    LookBackButton.style = 'width: auto; height: 50px; margin: 5px 10px 5px 10px; font-size: 30px;';
    if (LookBackButton) { LookBackButton.onclick = bouncyUpdate_bounceRow; }

    // Create the alert div
    let alert = document.createElement('div');
    alert.id = 'bounce-alert_bounceRow';
    alert.style.border = '5px solid yellow';
    alert.style.padding = '5px';
    alert.style.display = 'none';

    // Create a container to hold both elements
    const containerInput = document.createElement('div');
    containerInput.id = 'input-alert-container_bounceRow';
    containerInput.style.marginLeft = '25px';
    containerInput.style.marginBottom = '10px';
    containerInput.appendChild(numberInput);
    containerInput.appendChild(LookBackButton);
    containerInput.appendChild(alert);

    // Apply styles using JavaScript
    containerInput.style.display = 'flex';
    containerInput.style.alignItems = 'center';
    containerInput.style.gap = '10px';


    let helpDiv = document.createElement('div');
    helpDiv.id = 'help-bouncy_bounceRow';
    helpDiv.style.marginLeft = '25px';
    helpDiv.style.marginBottom = '15px';



    let contentDiv = document.createElement('div');
    contentDiv.id = 'bounceDiv_bounceRow';
    contentDiv.style.marginLeft = '25px';
    contentDiv.style.marginBottom = '15px';

    let newLine = document.createElement('br');


    container.appendChild(titleDiv);
    container.appendChild(helpDiv);

    container.appendChild(radioDozenRow);
    container.appendChild(betForm);

    container.appendChild(containerInput);
    container.appendChild(newLine);
    container.appendChild(contentDiv);
    container.appendChild(newLine);
    container.appendChild(newLine);


    return container;

}
function bouncyUpdate_bounceRow(event) {

    handleInputChangeBounce_bounceRow(event);
    handleWinCondition_bounceRow(event);


    if (useBouncyDozenBet_bounceRow) {

        bouncy_dozen_round_profit_bounceRow();
        on_spin_dozen_bouncy_bet_bounceRow();
        diplay_dozen_bet_bounceRow();
        bet_on_bouncy_dozen_bounceRow();


    }
    appendOrRemoveAlert_bounceRow();
}

function detectDozenBounce_bounceRow() {
    const bounceCount = parseInt(document.getElementById('bounceCount_bounceRow').value);
    const bounceType = document.getElementById('bounceType_bounceRow').value.split(' - ');

    let streak = [];
    let dozenStreak = [];

    for (number in numbers) {
        const dozen = getDozen_bounceRow(number);
        if (dozen !== '0') {
            if (streak.length < 2) {
                streak.push(dozen);
            } else {
                if (dozen !== streak[streak.length - 1]) {
                    if (streak.length > 1 && dozen === streak[streak.length - 2]) {
                        streak.push(dozen);
                        dozenStreak.push(number);
                        if (streak.length === bounceCount + 1 && streak[0] === bounceType[0] && streak[1] === bounceType[1]) {
                            alertDiv.textContent = `Row bounce detected: ${streak.slice(-bounceCount).join(' - ')} (${dozenStreak.slice(-bounceCount).join(', ')})`;
                            alertDiv.style.display = 'block';
                            streak = [];
                            dozenStreak = [];
                        }
                    } else {
                        streak = [streak[streak.length - 1], dozen];
                        dozenStreak = [dozenStreak[dozenStreak.length - 1], number];
                    }
                } else {
                    streak = [streak[streak.length - 1]];
                    dozenStreak = [dozenStreak[dozenStreak.length - 1]];
                }
            }
        }
    }
}
function getColor_bounceRow(number) {
    if (number === 0) return 'green';
    return red.includes(number) ? 'red' : 'black';
}

function getDozen_bounceRow(number) {
    if (rouletteAttributes.columns[0].includes(number)) return 1;
    if (rouletteAttributes.columns[1].includes(number)) return 2;
    if (rouletteAttributes.columns[2].includes(number)) return 3;
    return 0;
}

function calculateBounce_bounceRow() {

    let container = document.createElement('div');
    container.style.fontSize = '20px';

    var displayTable = "<table border=1>";
    let bounceLength = (bounceNumber_bounceRow * 2) - 1;
    let numbersLength = (bounceNumber_bounceRow * 2) - 1;
    if (numbers.length < numbersLength) {
        numbersLength = numbers.length;
    }



    d1d2_bounceRow = [];
    d1d3_bounceRow = [];
    d2d1_bounceRow = [];
    d2d3_bounceRow = [];
    d3d1_bounceRow = [];
    d3d2_bounceRow = [];
    nBounce_bounceRow = [];

    for (var x = numbersLength; x > 0; x--) {

        nBounce_bounceRow.push(getDozen_bounceRow(numbers[numbers.length - x]));


    }

    for (var x = 0; x < bounceLength; x++) {
        d1d2_bounceRow.push(x % 2 === 0 ? 1 : 2);
        d1d3_bounceRow.push(x % 2 === 0 ? 1 : 3);
        d2d1_bounceRow.push(x % 2 === 0 ? 2 : 1);
        d2d3_bounceRow.push(x % 2 === 0 ? 2 : 3);
        d3d1_bounceRow.push(x % 2 === 0 ? 3 : 1);
        d3d2_bounceRow.push(x % 2 === 0 ? 3 : 2);
    }

    displayTable += "<tr>";
    displayTable += "<td style='padding: 8px;'>Numbers</td>";

    for (var x = numbersLength; x > 0; x--) {
        displayTable += "<td style='padding: 8px;'>" + numbers[numbers.length - x] + "</td>";
    }
    displayTable += "</tr>";








    displayTable += "<tr>";
    displayTable += "<td style='padding: 8px;'>R1 R2</td>";
    for (var x = 0; x < bounceLength; x++) {
        if (x % 2 == 1) {
            displayTable += getBackground_bounceRow(2, "R2", x);
        }
        else {
            displayTable += getBackground_bounceRow(1, "R1", x);
        }

    }
    displayTable += "</tr>";

    displayTable += "<tr>";
    displayTable += "<td style='padding: 8px;'>R1 R3</td>";
    for (var x = 0; x < bounceLength; x++) {
        if (x % 2 == 1) {
            displayTable += getBackground_bounceRow(3, "R3", x);
        }
        else {
            displayTable += getBackground_bounceRow(1, "R1", x);
        }

    }
    displayTable += "</tr>";

    displayTable += "<tr>";
    displayTable += "<td style='padding: 8px;'>R2 R1</td>";
    for (var x = 0; x < bounceLength; x++) {
        if (x % 2 == 1) {
            displayTable += getBackground_bounceRow(1, "R1", x);
        }
        else {
            displayTable += getBackground_bounceRow(2, "R2", x);
        }

    }
    displayTable += "</tr>";

    displayTable += "<tr>";
    displayTable += "<td style='padding: 8px;'>R2 R3</td>";
    for (var x = 0; x < bounceLength; x++) {
        if (x % 2 == 1) {
            displayTable += getBackground_bounceRow(3, "R3", x);
        }
        else {
            displayTable += getBackground_bounceRow(2, "R2", x);
        }

    }
    displayTable += "</tr>";

    displayTable += "<tr>";
    displayTable += "<td style='padding: 8px;'>R3 R1</td>";
    for (var x = 0; x < bounceLength; x++) {
        if (x % 2 == 1) {
            displayTable += getBackground_bounceRow(1, "R1", x);
        }
        else {
            displayTable += getBackground_bounceRow(3, "R3", x);
        }

    }
    displayTable += "</tr>";

    displayTable += "<tr>";
    displayTable += "<td style='padding: 8px;'>R3 R2</td>";
    for (var x = 0; x < bounceLength; x++) {
        if (x % 2 == 1) {
            displayTable += getBackground_bounceRow(2, "R2", x);
        }
        else {
            displayTable += getBackground_bounceRow(3, "R3", x);
        }

    }
    displayTable += "</tr>";

    displayTable += "</table>";

    let tableDiv = document.createElement('div');
    tableDiv.innerHTML = displayTable;
    container.appendChild(tableDiv);
    return container;
}
function handleInputChangeBounce_bounceRow() {

    let bouncyNumber = document.getElementById('number-input-bouncy_bounceRow');
    if (bouncyNumber) {
        if (bouncyNumber.value === '' || isNaN(bouncyNumber.value) || bouncyNumber.value < 2) {
            bouncyNumber.value = 2;
        }
        if (bouncyNumber.value) {
            bounceNumber_bounceRow = bouncyNumber.value;

        } else {
            bounceNumber_bounceRow = 2;
        }

        let content = calculateBounce_bounceRow();


        let contentFromButtonClick = document.getElementById('bounceDiv_bounceRow');
        if (contentFromButtonClick) {
            contentFromButtonClick.innerHTML = '';
            contentFromButtonClick.appendChild(content);
        }
    }


}
function handleWinCondition_bounceRow() {
    let alertBox = document.getElementById('bounce-alert_bounceRow');

    if (alertBox) {
        alertBox.style.display = 'none';
        alertBox.innerHTML = '';
        if (arraysEqual_bounceRow(d1d2_bounceRow, nBounce_bounceRow)) alertBox.innerHTML = "Bet Row 1, 3";
        if (arraysEqual_bounceRow(d1d3_bounceRow, nBounce_bounceRow)) alertBox.innerHTML = "Bet Row 1, 2";
        if (arraysEqual_bounceRow(d2d1_bounceRow, nBounce_bounceRow)) alertBox.innerHTML = "Bet Row 2, 3";
        if (arraysEqual_bounceRow(d2d3_bounceRow, nBounce_bounceRow)) alertBox.innerHTML = "Bet Row 1, 2";
        if (arraysEqual_bounceRow(d3d1_bounceRow, nBounce_bounceRow)) alertBox.innerHTML = "Bet Row 2, 3";
        if (arraysEqual_bounceRow(d3d2_bounceRow, nBounce_bounceRow)) alertBox.innerHTML = "Bet Row 1, 3";

        if (!(alertBox.innerHTML === '')) {
            alertBox.style.display = 'block';
        }
    }

    //Show alert above Board

    return;
}
function getBackground_bounceRow(dozenNumber, dozenText, x) {
    let displayTable = '';
    if (nBounce_bounceRow.length > 0) {
        if (nBounce_bounceRow[x] == dozenNumber && dozenNumber > 0) {
            displayTable = "<td style='padding: 8px; background-color: yellow'>" + dozenText + "</td>";
        }
        else {
            displayTable = "<td style='padding: 8px;'>" + dozenText + "</td>";
        }
    }
    return displayTable;
}

function arraysEqual_bounceRow(A, B) {
    if (A.length !== B.length) return false;
    return A.every((value, index) => value === B[index]);
}

function toggleButtonHelpBouncy_bounceRow() {
    const button = document.getElementById(`how-to-bouncy_bounceRow`);
    if (button) {
        helpBouncy_bounceRow = !helpBouncy_bounceRow;
        button.style.backgroundColor = helpBouncy_bounceRow ? 'lightgreen' : '';

        let helpDiv = document.getElementById('help-bouncy_bounceRow');
        helpDiv.innerHTML = '';

        if (helpBouncy_bounceRow) {
            const img = document.createElement('img');
            img.src = 'items/bouncyHelp.png';
            img.alt = 'Help Image';

            img.onerror = function () {
                console.error('Failed to load image: ' + img.src);
                // Additional diagnostic information
                console.log('Current directory: ' + window.location.href);
            };
            helpDiv.appendChild(img);
        }
    }

}

function appendOrRemoveAlert_bounceRow() {



    oldUserBets_rows = new Map();



    var bottomStickyBox = document.getElementById('sticky-bottom-alert');
    var bouncyAlertSticky = document.getElementById('bouncy-alert-sticky_bounceRow');
    if (bouncyAlertSticky) {
        bottomStickyBox.removeChild(bouncyAlertSticky);
    }



    if (bouncyDisplay_bounceRow && bouncyDozenBetPlaced_bounceRow) {


        var alertBox = document.createElement('div');
        alertBox.id = 'bouncy-alert-sticky_bounceRow';
        alertBox.style.border = '5px solid yellow';
        alertBox.style.padding = '10px';
        alertBox.style.paddingRight = '15px';
        alertBox.style.display = 'inline-flex'; // Use inline-flex to shrink to fit content
        alertBox.style.flex = '0'; // Ensure it doesn't stretch
        alertBox.style.whiteSpace = 'nowrap';
        alertBox.style.marginRight = '5px';
        alertBox.style.marginLeft = '5px';


        alertBox.innerHTML = '';



        if (arraysEqual_bounceRow(d1d2_bounceRow, nBounce_bounceRow)) {


            alertBox.innerHTML = "Bet Rows 1, 3<br/>Amount: " + global_bouncy_current_bet_bounceRow;


            oldUserBets_rows.set("1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
            oldUserBets_rows.set("3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);

            rebet();
        }
        if (arraysEqual_bounceRow(d1d3_bounceRow, nBounce_bounceRow)) {



            alertBox.innerHTML = "Bet Rows 1, 2<br/>Amount: " + global_bouncy_current_bet_bounceRow;


            oldUserBets_rows.set("1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
            oldUserBets_rows.set("2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
            rebet();
        }
        if (arraysEqual_bounceRow(d2d1_bounceRow, nBounce_bounceRow)) {
            alertBox.innerHTML = "Bet Rows 2, 3<br/>Amount: " + global_bouncy_current_bet_bounceRow;



            oldUserBets_rows.set("2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
            oldUserBets_rows.set("3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
            rebet();
        }
        if (arraysEqual_bounceRow(d2d3_bounceRow, nBounce_bounceRow)) {


            alertBox.innerHTML = "Bet Rows 1, 2<br/>Amount: " + global_bouncy_current_bet_bounceRow;



            oldUserBets_rows.set("1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
            oldUserBets_rows.set("2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
            rebet();
        }
        if (arraysEqual_bounceRow(d3d1_bounceRow, nBounce_bounceRow)) {


            alertBox.innerHTML = "Bet Rows 2, 3<br/>Amount: " + global_bouncy_current_bet_bounceRow;


            oldUserBets_rows.set("2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
            oldUserBets_rows.set("3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
            rebet();
        }
        if (arraysEqual_bounceRow(d3d2_bounceRow, nBounce_bounceRow)) {


            alertBox.innerHTML = "Bet Rows 1, 3<br/>Amount: " + global_bouncy_current_bet_bounceRow;


            oldUserBets_rows.set("1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
            oldUserBets_rows.set("3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
            rebet();
        }

        if (alertBox.innerHTML === '') alertBox.style.display = 'none';







        bottomStickyBox.appendChild(alertBox);
    }
    if (bouncyDisplay_bounceRow) {
        let bounceAlertDiv = document.getElementById('bounce-alert_bounceRow');
        if (bounceAlertDiv) {
            bounceAlertDiv.remove();
        }
        let bounceAlerts = document.createElement('div');
        bounceAlerts.id = 'bounce-alert_bounceRow';
        bounceAlerts.style.backgroundColor = 'white';
        bounceAlerts.style.border = '3px solid yellow';
        bounceAlerts.style.padding = '5px';
        bounceAlerts.style.width = '110px';
        bounceAlerts.draggable = true;
        bounceAlerts.style.position = 'absolute';
        bounceAlerts.style.zIndex = '1000';



        // Setting the inner HTML based on your conditions
        if (arraysEqual_bounceRow(d1d2_bounceRow, nBounce_bounceRow)) bounceAlerts.innerHTML = "Bet Row 1, 3";
        if (arraysEqual_bounceRow(d1d3_bounceRow, nBounce_bounceRow)) bounceAlerts.innerHTML = "Bet Row 1, 2";
        if (arraysEqual_bounceRow(d2d1_bounceRow, nBounce_bounceRow)) bounceAlerts.innerHTML = "Bet Row 2, 3";
        if (arraysEqual_bounceRow(d2d3_bounceRow, nBounce_bounceRow)) bounceAlerts.innerHTML = "Bet Row 1, 2";
        if (arraysEqual_bounceRow(d3d1_bounceRow, nBounce_bounceRow)) bounceAlerts.innerHTML = "Bet Row 2, 3";
        if (arraysEqual_bounceRow(d3d2_bounceRow, nBounce_bounceRow)) bounceAlerts.innerHTML = "Bet Row 1, 3";
        bounceAlerts.style.display = 'block';
        bounceAlerts.style.whiteSpace = 'nowrap';
        bounceAlerts.style.paddingRight = '15px';
        if (bounceAlerts.innerHTML === '') bounceAlerts.style.display = 'none';

        alertsbox.appendChild(bounceAlerts);

        makeElementDraggableNoToggle(bounceAlerts);

    } else {
        let bounceAlertDiv = document.getElementById('bounce-alert_bounceRow');
        if (bounceAlertDiv) {
            bounceAlertDiv.remove();
        }
    }



}

// --------------------------------------------------------------
// START Bouncy Dozen Bets
// --------------------------------------------------------------

function bouncy_bet_every_spin_bounceRow() {




}
function bet_on_bouncy_dozen_bounceRow() {

    if (arraysEqual_bounceRow(d1d2_bounceRow, nBounce_bounceRow)) {

        oldUserBets_rows = new Map();
        oldUserBets_rows.set("1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
        oldUserBets_rows.set("3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);

        rebet();

    }
    if (arraysEqual_bounceRow(d1d3_bounceRow, nBounce_bounceRow)) {

        oldUserBets_rows = new Map();
        oldUserBets_rows.set("1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
        oldUserBets_rows.set("2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);


        rebet();

    }
    if (arraysEqual_bounceRow(d2d1_bounceRow, nBounce_bounceRow)) {

        oldUserBets_rows = new Map();
        oldUserBets_rows.set("2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
        oldUserBets_rows.set("3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);

        rebet();

    }
    if (arraysEqual_bounceRow(d2d3_bounceRow, nBounce_bounceRow)) {

        oldUserBets_rows = new Map();
        oldUserBets_rows.set("1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
        oldUserBets_rows.set("2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);

        rebet();

    }
    if (arraysEqual_bounceRow(d3d1_bounceRow, nBounce_bounceRow)) {

        oldUserBets_rows = new Map();
        oldUserBets_rows.set("2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
        oldUserBets_rows.set("3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);


        rebet();

    }
    if (arraysEqual_bounceRow(d3d2_bounceRow, nBounce_bounceRow)) {

        oldUserBets_rows = new Map();
        oldUserBets_rows.set("1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);
        oldUserBets_rows.set("3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36", [global_bouncy_current_bet_bounceRow, "outside_row", 2]);




        rebet();

    }
}
function createDynamicForm_bounceRow() {

    let newLine = document.createElement('br');

    const form = document.createElement('form');

    let dozenForm = bouncyDozenBets_bounceRow();
    form.appendChild(dozenForm);


    return form;
}

function bouncyDozenBets_bounceRow() {
    // Create main container
    const mainContainer = document.createElement('div');
    mainContainer.style.border = '2px solid black';
    mainContainer.style.padding = '10px'; // Add some padding for better visual appearance

    const titleContiner = document.createElement('div');
    titleContiner.style.display = 'flex'; // Use flexbox to align items on the same line
    titleContiner.style.alignItems = 'center'; // Align items vertically in the center
    titleContiner.style.justifyContent = 'start';

    // Title for Dozen Bets
    const title = document.createElement('h2');
    title.innerText = 'Row Bets';
    title.style.marginLeft = '25px'; // Add some margin for alignment
    titleContiner.appendChild(title);

    


    // Checkbox for "Use dozen bets"
    const bouncyCheckbox = document.createElement('input');
    bouncyCheckbox.type = 'checkbox';
    bouncyCheckbox.id = "use-bouncy-dozen_bounceRow";
    bouncyCheckbox.checked = useBouncyDozenBet_bounceRow;
    bouncyCheckbox.style.transform = 'scale(2)';
    bouncyCheckbox.style.marginLeft = '25px';
    bouncyCheckbox.style.marginRight = '10px'; // Margin before the label
    bouncyCheckbox.style.marginTop = '-5px'; // Adjust vertical alignment due to scaling
    bouncyCheckbox.addEventListener('change', function () {
        useBouncyDozenBet_bounceRow = this.checked;
        useBouncyDozenBet = false;
        oldUserBets_dozens = new Map();
    });
    titleContiner.appendChild(bouncyCheckbox);

    // Label for "Use dozen bets"
    const bouncyCheckboxLabel = document.createElement('label');
    bouncyCheckboxLabel.htmlFor = 'use-bouncy-dozen_bounceRow';
    bouncyCheckboxLabel.style.fontSize = '20px';
    // Corrected the margin top to align with the vertically centered checkbox
    bouncyCheckboxLabel.style.marginTop = '0px'; // Reset margin top to zero for better alignment
    bouncyCheckboxLabel.appendChild(document.createTextNode('Use row bets.'));
    titleContiner.appendChild(bouncyCheckboxLabel);

    mainContainer.appendChild(titleContiner);




    // Create the first row with "Use dozen bets" checkbox and input
    const inputDiv = document.createElement('div');
    inputDiv.style.display = 'flex';
    inputDiv.style.alignItems = 'center'; // Vertically center items
    inputDiv.style.marginLeft = '25px';
    inputDiv.style.marginBottom = '10px'; // Add space below this row

    const numberInput_bounceRow = document.createElement('input');
    numberInput_bounceRow.type = 'text';
    numberInput_bounceRow.id = 'bouncyBets_bounceRow';
    numberInput_bounceRow.value = '1, 2, 2, 6, 6, 18, 18, 54, 54, 162, 162';
    numberInput_bounceRow.style.width = '500px'; // Set width of the input
    numberInput_bounceRow.style.height = '50px'; // Set height of the input to make it taller
    numberInput_bounceRow.style.fontSize = '24px'; // Increase font size for better readability
    numberInput_bounceRow.style.marginRight = '10px'; // Add space between elements



    // Default value to be used if no value is found in local storage
    const defaultValue = '1, 2, 2, 6, 6, 18, 18, 54, 54, 162, 162';

    // Try to get the value from localStorage
    let storedValue = localStorage.getItem('bouncyBetsValue_bounceRow');

    // Check if there is something in localStorage and it's not just an empty string
    if (storedValue && storedValue.length > 0) {
        numberInput_bounceRow.value = storedValue; // Set input value from localStorage
        global_bouncy_bet_array_bounceRow = numberInput_bounceRow.value.split(/\D+/).filter(Boolean).map(Number);
    } else {
        numberInput_bounceRow.value = defaultValue; // Set input value to default
        global_bouncy_bet_array_bounceRow = numberInput_bounceRow.value.split(/\D+/).filter(Boolean).map(Number);
        localStorage.setItem('bouncyBetsValue_bounceRow', defaultValue); // Save default value to localStorage
    }
    inputDiv.appendChild(numberInput_bounceRow);

    // Listen for input changes and update localStorage
    numberInput_bounceRow.addEventListener('input', () => {
        global_bouncy_bet_array_bounceRow = numberInput_bounceRow.value.split(/\D+/).filter(Boolean).map(Number);
        localStorage.setItem('bouncyBetsValue_bounceRow', numberInput_bounceRow.value);
    });






    // Create the button
    const defaultButton = document.createElement('button');
    defaultButton.innerText = 'Use Default';
    defaultButton.style.height = '50px'; // Match the height of the input box
    defaultButton.style.fontSize = '20px'; // Font size to match the input's readability
    defaultButton.style.verticalAlign = 'top'; // Align the top of the button with the input
    defaultButton.addEventListener('click', function () {
        event.preventDefault();
        numberInput_bounceRow.value = '1, 2, 2, 6, 6, 18, 18, 54, 54, 162, 162'; // Set default value on click
        global_bouncy_bet_array_bounceRow = numberInput_bounceRow.value.split(/\D+/).filter(Boolean).map(Number);
        localStorage.setItem('bouncyBetsValue_bounceRow', defaultValue);


    });
    inputDiv.appendChild(defaultButton);

    mainContainer.appendChild(inputDiv);

    // Create the second row with "Progress on Win" and "Progress on Loss"
    const progressRow = document.createElement('div');
    progressRow.style.display = 'flex';
    progressRow.style.alignItems = 'center';
    progressRow.style.marginLeft = '25px';
    progressRow.style.marginBottom = '10px';

    // Checkbox for "Progress on Win"
    const progressWinCheckbox = document.createElement('input');
    progressWinCheckbox.type = 'checkbox';
    progressWinCheckbox.id = "progress-win_bounceRow";
    progressWinCheckbox.checked = useBouncyDozenBet_progress_win_bounceRow;
    progressWinCheckbox.style.transform = 'scale(2)';
    progressWinCheckbox.style.marginRight = '10px';
    progressWinCheckbox.addEventListener('change', function () {
        useBouncyDozenBet_progress_win_bounceRow = this.checked;
    });
    progressRow.appendChild(progressWinCheckbox);

    // Label for "Progress on Win"
    const progressWinLabel = document.createElement('label');
    progressWinLabel.htmlFor = 'progress-win_bounceRow';
    progressWinLabel.style.fontSize = '20px';
    progressWinLabel.appendChild(document.createTextNode('Progress on Win'));
    progressWinLabel.style.marginRight = '20px';
    progressRow.appendChild(progressWinLabel);

    // Checkbox for "Progress on Loss"
    const progressLossCheckbox = document.createElement('input');
    progressLossCheckbox.type = 'checkbox';
    progressLossCheckbox.id = "progress-loss_bounceRow";
    progressLossCheckbox.checked = useBouncyDozenBet_progress_loss_bounceRow;
    progressLossCheckbox.style.transform = 'scale(2)';
    progressLossCheckbox.style.marginRight = '10px';
    progressLossCheckbox.addEventListener('change', function () {
        useBouncyDozenBet_progress_loss_bounceRow = this.checked;
    });
    progressRow.appendChild(progressLossCheckbox);

    // Label for "Progress on Loss"
    const progressLossLabel = document.createElement('label');
    progressLossLabel.htmlFor = 'progress-loss_bounceRow';
    progressLossLabel.style.fontSize = '20px';
    progressLossLabel.appendChild(document.createTextNode('Progress on Loss'));
    progressRow.appendChild(progressLossLabel);

    mainContainer.appendChild(progressRow);

    // Create the third row with "Previous Bet on Win" and "Previous Bet on Loss"
    const previousRow = document.createElement('div');
    previousRow.style.display = 'flex';
    previousRow.style.alignItems = 'center';
    previousRow.style.marginLeft = '25px';
    previousRow.style.marginBottom = '10px';

    // Checkbox for "Previous Bet on Win"
    const previousBetWinCheckbox = document.createElement('input');
    previousBetWinCheckbox.type = 'checkbox';
    previousBetWinCheckbox.id = "previous-bet-win_bounceRow";
    previousBetWinCheckbox.checked = useBouncyDozenBet_previous_win_bounceRow;
    previousBetWinCheckbox.style.transform = 'scale(2)';
    previousBetWinCheckbox.style.marginRight = '10px';
    previousBetWinCheckbox.addEventListener('change', function () {
        useBouncyDozenBet_previous_win_bounceRow = this.checked;
    });
    previousRow.appendChild(previousBetWinCheckbox);

    // Label for "Previous Bet on Win"
    const previousBetWinLabel = document.createElement('label');
    previousBetWinLabel.htmlFor = 'previous-bet-win_bounceRow';
    previousBetWinLabel.style.fontSize = '20px';
    previousBetWinLabel.appendChild(document.createTextNode('Previous Bet on Win'));
    previousBetWinLabel.style.marginRight = '20px';
    previousRow.appendChild(previousBetWinLabel);

    // Checkbox for "Previous Bet on Loss"
    const previousBetLossCheckbox = document.createElement('input');
    previousBetLossCheckbox.type = 'checkbox';
    previousBetLossCheckbox.id = "previous-bet-loss_bounceRow";
    previousBetLossCheckbox.checked = useBouncyDozenBet_previous_loss_bounceRow;
    previousBetLossCheckbox.style.transform = 'scale(2)';
    previousBetLossCheckbox.style.marginRight = '10px';
    previousBetLossCheckbox.addEventListener('change', function () {
        useBouncyDozenBet_previous_loss_bounceRow = this.checked;
    });
    previousRow.appendChild(previousBetLossCheckbox);

    // Label for "Previous Bet on Loss"
    const previousBetLossLabel = document.createElement('label');
    previousBetLossLabel.htmlFor = 'previous-bet-loss_bounceRow';
    previousBetLossLabel.style.fontSize = '20px';
    previousBetLossLabel.appendChild(document.createTextNode('Previous Bet on Loss'));
    previousRow.appendChild(previousBetLossLabel);

    mainContainer.appendChild(previousRow);
        // Create the second row with "Progress on Win" and "Progress on Loss"
        const resetAnyWinRow = document.createElement('div');
        resetAnyWinRow.style.display = 'flex';
        resetAnyWinRow.style.alignItems = 'center';
        resetAnyWinRow.style.marginLeft = '25px';
        resetAnyWinRow.style.marginBottom = '10px';

        // Checkbox for "Previous Bet on Loss"
        const resetAnyWinCheckbox = document.createElement('input');
        resetAnyWinCheckbox.type = 'checkbox';
        resetAnyWinCheckbox.id = "reset-any-win_bounceRow";
        resetAnyWinCheckbox.checked = useBouncyDozenBet_reset_any_win_bounceRow;
        resetAnyWinCheckbox.style.transform = 'scale(2)';
        resetAnyWinCheckbox.style.marginRight = '10px';
        resetAnyWinCheckbox.addEventListener('change', function () {
            useBouncyDozenBet_reset_any_win_bounceRow = this.checked;
        });
        resetAnyWinRow.appendChild(resetAnyWinCheckbox);
    
        // Label for "Previous Bet on Loss"
        const resetAnyWinLabel = document.createElement('label');
        resetAnyWinLabel.htmlFor = 'reset-any-win_bounceRow';
        resetAnyWinLabel.style.fontSize = '20px';
        resetAnyWinLabel.appendChild(document.createTextNode('Reset on any win'));
        resetAnyWinRow.appendChild(resetAnyWinLabel);
    
        mainContainer.appendChild(resetAnyWinRow);

    

    // Bets display
    const displayBetDiv = document.createElement('div');
    displayBetDiv.style.marginTop = '10px';
    displayBetDiv.style.marginLeft = '25px';
    const displayBet = document.createElement('span');
    displayBet.id = 'display_bouncy_dozen_bets_bounceRow';
    displayBetDiv.appendChild(displayBet);
    mainContainer.appendChild(displayBetDiv);

    // Bet on Dozen display
    const displayBetDozenDiv = document.createElement('div');
    displayBetDozenDiv.style.marginTop = '10px';
    displayBetDozenDiv.style.marginLeft = '25px';
    const displayDozenProfit = document.createElement('span');
    displayDozenProfit.id = 'displayDozenBet_bounceRow';
    displayBetDozenDiv.appendChild(displayDozenProfit);
    mainContainer.appendChild(displayBetDozenDiv);

    // Profit display
    const displayProfitDiv = document.createElement('div');
    displayProfitDiv.style.marginTop = '10px';
    displayProfitDiv.style.marginLeft = '25px';
    const displayProfit = document.createElement('span');
    displayProfit.id = 'displayProfit_bounceRow';
    displayProfitDiv.appendChild(displayProfit);
    mainContainer.appendChild(displayProfitDiv);

    return mainContainer;
}


function bouncy_dozen_round_profit_bounceRow() {





    if (global_win_loss_bounceRow === 0 && bouncyDozenBetPlaced_bounceRow) {
        bouncyProfit_bounceRow -= global_bouncy_current_bet_bounceRow * 2;

        if (useBouncyDozenBet_progress_loss_bounceRow) {

            global_bouncy_bet_index_bounceRow += 1;
            if (global_bouncy_bet_index_bounceRow > global_bouncy_bet_array_bounceRow.length - 1) global_bouncy_bet_index_bounceRow = 0;
        }

        if (useBouncyDozenBet_previous_loss_bounceRow) {
            global_bouncy_bet_index_bounceRow -= 1;
            if (global_bouncy_bet_index_bounceRow < 0) global_bouncy_bet_index_bounceRow = 0;
        }



    }
    if (global_win_loss_bounceRow === 1 && bouncyDozenBetPlaced_bounceRow) {
        bouncyProfit_bounceRow += global_bouncy_current_bet_bounceRow;

        if (useBouncyDozenBet_progress_win_bounceRow) {
            global_bouncy_bet_index_bounceRow += 1;
            if (global_bouncy_bet_index_bounceRow > global_bouncy_bet_array_bounceRow.length - 1) global_bouncy_bet_index_bounceRow = 0;
        }
        if (useBouncyDozenBet_previous_win_bounceRow) {
            global_bouncy_bet_index_bounceRow -= 1;
            if (global_bouncy_bet_index_bounceRow < 0) global_bouncy_bet_index_bounceRow = 0;
        }
        if(useBouncyDozenBet_reset_any_win_bounceRow)
        {
            global_bouncy_bet_index_bounceRow = 0;
            bouncyProfit_bounceRow = 0;
        }

    }


   
    global_bouncy_current_bet_bounceRow = global_bouncy_bet_array_bounceRow[global_bouncy_bet_index_bounceRow];



    let bouncyBetDisplay = document.getElementById('displayProfit_bounceRow');
    if (bouncyBetDisplay) {
        if (bouncyProfit_bounceRow > 0) {
            bouncyBetDisplay.innerText = 'Round Profit: ' + bouncyProfit_bounceRow + '  WON';
            oldUserBets_rows = new Map();

        }
        else {
            bouncyBetDisplay.innerText = 'Round Profit: ' + bouncyProfit_bounceRow
        }



    }
    if (bouncyProfit_bounceRow >= 0) {
        bouncyProfit_bounceRow = 0;
        global_bouncy_bet_index_bounceRow = 0;
        oldUserBets_rows = new Map();
    }
}

function cache_bouncy_dozen_bets_bounceRow() {

    let numberInput = document.getElementById('bouncyBets_bounceRow');
    if (numberInput) {

        let nullCache = JSON.parse(localStorage.getItem('bouncy_dozen_bets_bounceRow'));


        // Set the default array
        let defaultArray = [1, 2, 2, 6, 6, 18, 18, 54, 54, 162, 162];

        if (nullCache === null || nullCache.length === 0) {

            // If the cache is not set or empty, initialize it with the default array
            localStorage.setItem('bouncy_dozen_bets_bounceRow', JSON.stringify(defaultArray));

        }

        // Always set the input value from cache or default if cache is still empty
        nullCache = JSON.parse(localStorage.getItem('bouncy_dozen_bets_bounceRow')) || defaultArray;
        numberInput.value = nullCache.join(", ");

    }
}

function on_spin_dozen_bouncy_bet_bounceRow() {
    let numberInput = document.getElementById('bouncyBets_bounceRow');
    if (numberInput) {


        global_bouncy_current_bet_bounceRow = global_bouncy_bet_array_bounceRow[global_bouncy_bet_index_bounceRow];

        // Display the array in the UI if the display element exists
        let bouncyBetDisplay = document.getElementById('display_bouncy_dozen_bets_bounceRow');

        if (bouncyBetDisplay) {
            // Create an HTML string with each element of the array
            let displayArray = global_bouncy_bet_array_bounceRow.map((bet, index) => {
                if (index === global_bouncy_bet_index_bounceRow) {
                    // Highlight the element at global_bouncy_bet_index_bounceRow in yellow
                    return `<span style="background-color: yellow;">${bet}</span>`;
                } else {
                    return bet;
                }
            }).join(', '); // Join the elements with a comma and a space

            // Set the display content with the highlighted index
            bouncyBetDisplay.innerHTML = 'Using: ' + displayArray;
        }

    }
}

function diplay_dozen_bet_bounceRow() {
    global_bouncy_current_bet_bounceRow = global_bouncy_bet_array_bounceRow[global_bouncy_bet_index_bounceRow];

    let display_dozen_bet = document.getElementById('displayDozenBet_bounceRow');
    if (display_dozen_bet) {

        display_dozen_bet.innerHTML = 'Bet: ---';
        bouncyDozenBetPlaced_bounceRow = false;
        if (arraysEqual_bounceRow(d1d2_bounceRow, nBounce_bounceRow) || arraysEqual_bounceRow(d1d3_bounceRow, nBounce_bounceRow) || arraysEqual_bounceRow(d2d1_bounceRow, nBounce_bounceRow) ||
            arraysEqual_bounceRow(d2d3_bounceRow, nBounce_bounceRow) || arraysEqual_bounceRow(d3d1_bounceRow, nBounce_bounceRow) || arraysEqual_bounceRow(d3d2_bounceRow, nBounce_bounceRow)) {
            bouncyDozenBetPlaced_bounceRow = true;


            display_dozen_bet.innerHTML = 'Bet: ' + global_bouncy_current_bet_bounceRow;
        }


    }
}

