
function nextBet() {
    let container = document.createElement('div');
    container.style.marginLeft = '25px';
    container.style.marginBottom = '15px';
    container.style.fontSize = '20px';
    container.id ='nextBet-tool';

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

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

    let titleDiv = document.createElement('div');
    let titleSpan = document.createElement('span');
    titleSpan.innerHTML = '<h2>Next Bet Calculator</h2>';
    titleDiv.appendChild(titleSpan);
    titleDiv.style.marginBottom = '-20px';

    container.appendChild(titleDiv);

    let item = increaseDecrease('nextBet');
    container.appendChild(item);

    const bStraights = document.createElement('button');
    bStraights.textContent = 'Straights';
    bStraights.style.fontSize = '20px';
    bStraights.onclick = () => {
        let content = calculate("Straights", 35);
        contentFromButtonClick.innerHTML = ''; 
        contentFromButtonClick.appendChild(content); 
        currentTaIbtem = "Straights";
        currentTabOdd = 35;
        
    };

    const bSplits = document.createElement('button');
    bSplits.textContent = 'Splits';
    bSplits.style.fontSize = '20px';
    bSplits.onclick = () => {
        let content = calculate("Splits", 17);
        contentFromButtonClick.innerHTML = ''; 
        contentFromButtonClick.appendChild(content); 
        currentTaIbtem = "Splits";
        currentTabOdd = 17;
    };

    const bStreets = document.createElement('button');
    bStreets.textContent = 'Streets';
    bStreets.style.fontSize = '20px';
    bStreets.onclick = () => {
        let content = calculate("Streets", 11);
        contentFromButtonClick.innerHTML = ''; 
        contentFromButtonClick.appendChild(content); 
        currentTaIbtem = "Streets";
        currentTabOdd = 11;
    };

    const bCorners = document.createElement('button');
    bCorners.textContent = 'Corners';
    bCorners.style.fontSize = '20px';
    bCorners.onclick = () => {
        let content = calculate("Corners", 8);
        contentFromButtonClick.innerHTML = ''; 
        contentFromButtonClick.appendChild(content); 
        currentTaIbtem = "Corners";
        currentTabOdd = 8;
    };

    const bLine = document.createElement('button');
    bLine.textContent = 'Line';
    bLine.style.fontSize = '20px';
    bLine.onclick = () => {
        let content = calculate("Liness", 5);
        contentFromButtonClick.innerHTML = ''; 
        contentFromButtonClick.appendChild(content); 
        currentTaIbtem = "Doubles";
        currentTabOdd = 5;
    };

    const bColumns = document.createElement('button');
    bColumns.textContent = 'Dozens/Rows';
    bColumns.style.fontSize = '20px';
    bColumns.onclick = () => {
        let content = calculate("Columns", 2);
        contentFromButtonClick.innerHTML = ''; 
        contentFromButtonClick.appendChild(content); 
        currentTaIbtem = "Columns";
        currentTabOdd = 2;
    };

    const bEvens = document.createElement('button');
    bEvens.textContent = 'Evens';
    bEvens.style.fontSize = '20px';
    bEvens.onclick = () => {
        let content = calculate("Evens", 1);
        contentFromButtonClick.innerHTML = ''; 
        contentFromButtonClick.appendChild(content); 
        currentTaIbtem = "Evens";
        currentTabOdd = 1;
    };

    let contentFromButtonClick = document.createElement('div');
    contentFromButtonClick.id = 'button-item-contents';


    container.appendChild(bStraights);
    container.appendChild(bSplits);
    container.appendChild(bStreets);
    container.appendChild(bCorners);
    container.appendChild(bLine);
    container.appendChild(bColumns);
    container.appendChild(bEvens);
    container.appendChild(document.createElement('br'));
    container.appendChild(document.createElement('br'));
    container.appendChild(contentFromButtonClick);

    return container;
}



function calculate(betType, odds) {
    let container = document.createElement('div');
    container.style.fontSize = '20px';

var displayTable = "<table border=1>";
displayTable += "<tr>";
displayTable += "<th colspan=4 style='padding: 8px;'> " + betType + "</th>";
displayTable += "</tr>";
displayTable += "<tr>";
displayTable += "<th style='padding: 8px;'> Spots </th>";
displayTable += "<th style='padding: 8px;'> Bet </th>";
displayTable += "<th style='padding: 8px;'> Profit </th>";
displayTable += "<th style='padding: 8px;'> Cost </th>";
displayTable += "</tr>";

for (var x = 1; x < odds + 1; x++) {
    displayTable += "<tr>";
    displayTable += "<td style='padding: 8px;'>" + x + "</td>";
    let bet = calcBet(x, odds);
    displayTable += "<td style='padding: 8px;'>" + bet + "</td>";
    let profit = calcProfit(x, bet, odds);
    displayTable += "<td style='padding: 8px;'>" + profit + "</td>";
    let total = bet * x;
    displayTable += "<td style='padding: 8px;'>" + total + "</td>";
    displayTable += "</tr>";
}
displayTable += "</table>";

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

function calcBet(x, odds) {
    let totalLoss = document.getElementById('number-input-nextBet');

    if (totalLoss) {
        totalLoss = totalLoss.value;
    }
    else {
        totalLoss = 0
    }

    totalLoss = ensureInputValue(totalLoss, 0);

    let bet = 0;
    let minProfit = document.getElementById('profit-number-input-nextBet');
    if (minProfit) {
        minProfit = minProfit.value;
    } else {
        minProfit = 1;
    }
    minProfit = ensureInputValue(minProfit, 1);

    while ((bet * odds + bet - bet * x - minProfit) < totalLoss) {
        bet++;
    }
    return bet;
}
function calcProfit(x, bet, odds) {
    let totalLoss = document.getElementById('number-input-nextBet')
    if (totalLoss) {
        totalLoss = totalLoss.value;
      
    }
    else {
        totalLoss = 0
    }
    totalLoss = ensureInputValue(totalLoss, 0);
    let profit = odds * bet + bet - totalLoss - x * bet;
    return profit;
}

function updateNextBet()
{
   
    setBank();

    if(currentTaIbtem !== null)
        {
           
            
            let content = calculate(currentTaIbtem, currentTabOdd);
     
            let contentFromButtonClick = document.getElementById('button-item-contents');
            if(contentFromButtonClick)
            {
                contentFromButtonClick.innerHTML = ''; 
                contentFromButtonClick.appendChild(content); 
            }
    
        }
}
function handleInputChange() {
  
    if(currentTaIbtem !== null)
    {
       

        let content = calculate(currentTaIbtem, currentTabOdd);
 
        let contentFromButtonClick = document.getElementById('button-item-contents');
        if(contentFromButtonClick)
        {
            contentFromButtonClick.innerHTML = ''; 
            contentFromButtonClick.appendChild(content); 
        }

    }


}
function changeStartingBankrollUndo() {
  

    startingBank = bankValue;

	currentBank = bankValue;
    let loss = document.getElementById('number-input-nextBet')
    if(loss)
    {
        loss.value = startingBank - startingBank;
    }

    let currentBankSpan = document.getElementById('currentBankSpan');
    if (currentBankSpan) {
        currentBankSpan.innerHTML = 'Current Bank: '+currentBank;
    }

    let startingBankSpan = document.getElementById('startingBankSpan');
    if (startingBankSpan) {
        startingBankSpan.innerHTML = 'Current Bank: '+startingBank;
    }

    if(currentTaIbtem !== null)
    {
        
        let content = calculate(currentTaIbtem, currentTabOdd);

        let contentFromButtonClick = document.getElementById('button-item-contents');
        if(contentFromButtonClick)
        {
            contentFromButtonClick.innerHTML = ''; 

            contentFromButtonClick.appendChild(content); 
        }

    }


}
function changeStartingBankroll() {
  
    startingBank = bankValue;
	currentBank = bankValue;
    let loss = document.getElementById('number-input-nextBet')
    if(loss)
    {
        loss.value = startingBank - startingBank;
    }
    let currentBankSpan = document.getElementById('currentBankSpan');
    if (currentBankSpan) {
        currentBankSpan.innerHTML = 'Current Bank: '+currentBank;
    }

    let startingBankSpan = document.getElementById('startingBankSpan');
    if (startingBankSpan) {
        startingBankSpan.innerHTML = 'Starting Bank: '+startingBank;
    }

    if(currentTaIbtem !== null)
    {

        let content = calculate(currentTaIbtem, currentTabOdd);

    
        let contentFromButtonClick = document.getElementById('button-item-contents');
        if(contentFromButtonClick)
        {
  
            contentFromButtonClick.innerHTML = ''; 
            contentFromButtonClick.appendChild(content); 
        }

    }


}




function setBank() {
    currentBank = bankValue;
    var loss = startingBank - currentBank;
    let lossTextBox = document.getElementById('number-input-nextBet');
    if (lossTextBox) {
        lossTextBox.value = loss;
    }
    let currentBankSpan = document.getElementById('currentBankSpan');
    if (currentBankSpan) {
        currentBankSpan.innerHTML = 'Current Bank: '+currentBank;
    }

}



function initializeDocumentReadyFunctions() {


    const tabLinks = document.querySelectorAll(".tablinks");
    const tabContents = document.querySelectorAll(".tabcontent");

    const activateTab = target => {
        const contentId = target.getAttribute('data-target');

        tabContents.forEach(tabContent => {
            tabContent.style.display = tabContent.id === contentId ? 'block' : 'none';
        });

        tabLinks.forEach(tablink => {
            tablink.classList.remove('active');
        });

        target.classList.add('active');
    };

    const tabbedWindows = evt => {
        const target = evt.currentTarget;
        activateTab(target);
    };

    if (tabLinks.length > 0) {
        tabLinks.forEach(tab => {
            tab.addEventListener('click', tabbedWindows, false);
        });

        // Activate the first tab by default if there are any tabs
        activateTab(tabLinks[0]);
    }

}
function style_textbox_container(item)
{
	item.style.width = '100px';
    item.style.fontSize = '24px';
    item.style.fontWeight = '400';
    item.style.lineHeight = '1.7';
    item.style.color = '#212529';
    item.style.backgroundColor = '#fff';
    item.style.backgroundClip = 'padding-box';
    item.style.border = '1px solid #ced4da';
    item.style.appearance = 'none';
    item.style.borderRadius = '4px';
    item.style.transition = 'border-color .15s ease-in-out,box-shadow .15s ease-in-out';
    item.setAttribute('type', 'tel');
    item.setAttribute('pattern', '[0-9]*');
    item.setAttribute('inputmode', 'numeric');

    item.addEventListener('focus', function() {
        item.style.color = '#212529';
        item.style.backgroundColor = '#fff';
        item.style.borderColor = '#86b7fe';
        item.style.outline = '0';
        item.style.boxShadow = '0 0 0 0.25rem rgb(13 110 253 / 25%)';
    });
}

function increaseDecrease(id) {
   


    // Create the wrapper div
    const wrapperDiv = document.createElement('div');
    wrapperDiv.id = 'number-input-wrapper-' + id;


        let startingBankSpan = document.createElement('span');
        startingBankSpan.id = 'startingBankSpan';
        startingBankSpan.textContent = 'Starting Bank: '+startingBank;
        startingBankSpan.style.marginLeft = '10px';

        let currentBankSpan = document.createElement('span');
        currentBankSpan.id = 'currentBankSpan';
        currentBankSpan.textContent = 'Current Bank: '+currentBank;
        currentBankSpan.style.marginLeft = '10px';

    let loss = startingBank - currentBank;
    // Create the number input
    const numberInput = document.createElement('input');
    style_textbox_container(numberInput);
    numberInput.type = 'number';
    numberInput.id = 'number-input-' + id;
    numberInput.value = loss;

    // Add event listener for the input event
    numberInput.addEventListener('input', handleInputChange);

    // Create the number input
    const numberInputProfit = document.createElement('input');
    style_textbox_container(numberInputProfit);
    numberInputProfit.type = 'number';
    numberInputProfit.id = 'profit-number-input-' + id;
    numberInputProfit.value = 1;

    // Add event listener for the input event
    numberInputProfit.addEventListener('input', handleInputChange);




    let span = document.createElement('span');
    span.textContent = 'Total loss';
    span.style.marginLeft = '10px';




    let spanProfit = document.createElement('span');
    spanProfit.textContent = 'Profit next spin';
    spanProfit.style.marginLeft = '10px';



    // Append elements to the wrapper div
    
    wrapperDiv.appendChild(startingBankSpan);
    wrapperDiv.appendChild(document.createElement('br'));
    wrapperDiv.appendChild(document.createElement('br'));
    wrapperDiv.appendChild(currentBankSpan);
    wrapperDiv.appendChild(document.createElement('br'));
    wrapperDiv.appendChild(document.createElement('br'));
    wrapperDiv.appendChild(numberInput);
    wrapperDiv.appendChild(span);
    wrapperDiv.appendChild(document.createElement('br'));
    wrapperDiv.appendChild(document.createElement('br'));
    wrapperDiv.appendChild(numberInputProfit);
    wrapperDiv.appendChild(spanProfit);
    wrapperDiv.appendChild(document.createElement('br'));
    wrapperDiv.appendChild(document.createElement('br'));
    wrapperDiv.appendChild(document.createElement('br'));

    return wrapperDiv;
}