﻿function NumberRound(number, precision) {
    var coef = Math.pow(10, precision) * 1.0;
    return Math.round(number * coef) / coef;
}

function StringToNumber(str) {
    var result = 0.0;
    if (str != null) {
        str += '';
        var tmp = str.replace(',', '.') * 1.0;
        if (!isNaN(tmp)) {
            result = tmp;
        }
    }
    return result;
}

function NumberToString(number, delimiter) {
    var result = NumberRound(number, 2);
    if (delimiter != null) {
        result = result.toString().replace('.', delimiter);
    }
    return result;
}

function CountPPrice(currencyID) {
    var betRateValue = document.getElementById('betSumRate');
    if (betRateValue == null)
        return;

    var amountValue = document.getElementById("amount").value;
    var betRateValue = betRateValue.innerHTML;
    var secondAmountElem = document.getElementById("secondAmount");
    var potentialWinElem = document.getElementById('potentialWin');
    var secondPotentialWinElem = document.getElementById('secondPotentialWin');
    var withComma = betRateValue.indexOf(',') >= 0;

    amountValue = StringToNumber(amountValue);
    betRateValue = StringToNumber(betRateValue);
 
    if (betRateValue != null && !isNaN(amountValue))
    {
        //	zobrazeni hodnoty dualni meny
        if (!isNaN(currencyID)) 
        {
            var currencyRate = 30.126;
            var secondAmountValue = 0.0;
            var potentialWinValue = 0.0;
            var secondPotentialWinValue = 0.0;

            potentialWinValue = NumberRound(amountValue * betRateValue, 2);

            if (currencyID == 1)	//sk
            {
                secondAmountValue = amountValue / currencyRate;
                secondPotentialWinValue = potentialWinValue / currencyRate;
            }
            else {
                secondAmountValue = amountValue * currencyRate;
                secondPotentialWinValue = potentialWinValue * currencyRate;
            }
            secondAmountValue = NumberRound(secondAmountValue, 2);
            secondPotentialWinValue = NumberRound(secondPotentialWinValue, 2);

            if (withComma) {
                secondAmountValue = NumberToString(secondAmountValue, ',');
                potentialWinValue = NumberToString(potentialWinValue, ',');
                secondPotentialWinValue = NumberToString(secondPotentialWinValue, ',');
            }

            secondAmountElem.value = secondAmountValue;
            
            var potencialWinElemParts = potentialWinElem.innerHTML.split(' ');
            potentialWinElem.innerHTML = potentialWinValue + ' ' + potencialWinElemParts[potencialWinElemParts.length - 1];
            var secondPotencialWinElemParts = secondPotentialWinElem.innerHTML.split(' ');
            secondPotentialWinElem.innerHTML = '/' + secondPotentialWinValue + ' ' + secondPotencialWinElemParts[secondPotencialWinElemParts.length - 1];
        }
    }
}


function CountSystemPPrice(currencyID) {
    var amountElem = document.getElementById('amount');
    var secondAmountElem = document.getElementById("secondAmount");
    var hasOneStake = amountElem != null;
    var cardinalities = document.getElementById('systemselection_cardinalities').value.split('|');
    var amount = hasOneStake ? amountElem.value : "0.0";
    var withComma = amount.indexOf(',') >= 0;

    // zjisteni hodnot, ktere je potreba interaktivne zobrazit v kuponu - celkova vsazena castka, celkova vyhra
    var sumStake = 0.0;
    var sumWinPrize = 0.0;
    for (var i = 0; i < cardinalities.length; i++) {
        var cardinality = cardinalities[i];
        if (!hasOneStake) {
            amount = document.getElementById('systemselection_amount_' + cardinality).value;
            withComma |= amount.indexOf(',') >= 0;
        }
        amount = StringToNumber(amount);

        var rate = StringToNumber(document.getElementById('systemselection_rates_' + cardinality).value);
        var count = StringToNumber(document.getElementById('systemselection_counts_' + cardinality).value);

        if (amount != null && !isNaN(amount) && rate != null && !isNaN(rate) && count != null && !isNaN(count)) {
            sumStake += count * amount;
            sumWinPrize += rate * amount;
        }
    }
    sumStake = NumberRound(sumStake, 2);
    sumWinPrize = NumberRound(sumWinPrize, 2);

    //	zobrazeni hodnoty dualni meny
    if (!isNaN(currencyID)) {
        var currencyRate = 30.126;
        var secondSumStake = 0.0;
        var secondSumWinPrize = 0.0;
        var secondAmountValue = 0.0;

        if (currencyID == 1)	//sk
        {
            secondSumStake = sumStake / currencyRate;
            secondSumWinPrize = sumWinPrize / currencyRate;
            secondAmountValue = amount / currencyRate;
        }
        else {
            secondSumStake = sumStake * currencyRate;
            secondSumWinPrize = sumWinPrize * currencyRate;
            secondAmountValue = amount * currencyRate;
        }
        secondSumStake = NumberRound(secondSumStake, 2);
        secondSumWinPrize = NumberRound(secondSumWinPrize, 2);
        secondAmountValue = NumberRound(secondAmountValue, 2);

        if (withComma) {
            sumStake = NumberToString(sumStake, ',');
            secondSumStake = NumberToString(secondSumStake, ',');
            sumWinPrize = NumberToString(sumWinPrize, ',');
            secondSumWinPrize = NumberToString(secondSumWinPrize, ',');
            secondAmountValue = NumberToString(secondAmountValue, ',');
        }

        var sumStakeElem = document.getElementById('sumCombStake');
        var secondSumStakeElem = document.getElementById('secondSumCombStake');
        var sumWinPrizeElem = document.getElementById('potentialWin');
        var secondSumWinPrizeElem = document.getElementById('secondPotentialWin');

        var sumStakeElemParts = sumStakeElem.innerHTML.split(' ');
        var currencyAcr = (sumStakeElemParts.length > 0) ? sumStakeElemParts[sumStakeElemParts.length - 1] : "";
        var secondSumStakeElemParts = secondSumStakeElem.innerHTML.split(' ');
        var secondCurrencyAcr = (secondSumStakeElemParts.length > 0) ? secondSumStakeElemParts[secondSumStakeElemParts.length - 1] : "";

        sumStakeElem.innerHTML = sumStake + ' ' + currencyAcr;
        secondSumStakeElem.innerHTML = '/' + secondSumStake + ' ' + secondCurrencyAcr;
        sumWinPrizeElem.innerHTML = sumWinPrize + ' ' + currencyAcr;
        secondSumWinPrizeElem.innerHTML = '/' + secondSumWinPrize + ' ' + secondCurrencyAcr;
        if (secondAmountElem != null) {
            secondAmountElem.value = secondAmountValue;
        }
    }
}


////
// staticke funkce tiketu
coupon.lineID = function(id) {
    var parts = id.split('-');
    if (parts.length >= 2) {
        return parts[0] + '-' + parts[1];
    }
    return 0;
}

coupon.tip = function(id) {
    if (id.indexOf('_') > -1) {
        s = id.split('_');
        return s[1];
    }
    return 0;
}


//contains tip identifier (format EventId-GameId-DetailId_TipId)
function coupon() {
    this.items = new Array();

    this.add = function(id) {
        var position = -1;
        var oldItem = null;

        switch (this.contain(id)) {
            // tento radek (EventID-GameID) zatim neni v seznamu -> pridame jej
            case 0:
                this.items[this.items.length] = id;
                break;
            // tento radek (EventID-GameID) jiz je v seznamu ale s jinym tipem -> nahradime     
            case 1:
                position = this.position(id);
                if (position >= 0) {
                    oldItem = this.items[position];
                    this.items[position] = id;
                }
                break;
            // presne tento tip je jiz v seznamu - neprovedeme nic     
            case 2:
                break;
        }

        return oldItem;
    }

    this.remove = function(intIndex) {
        this.items.splice(intIndex, 1);
    }

    this.removeAll = function() {
        this.items.splice(0, this.items.length);
    }

    this.contain = function(id) {
        var inLineID = coupon.lineID(id);

        for (var i = 0; i < this.items.length; i++) {
            if (id == this.items[i]) {
                return 2;
            }
            else if ((inLineID == coupon.lineID(this.items[i])) && (inLineID != 0)) {
                return 1;
            }
        }
        return 0;
    }

    this.position = function(id) {
        var inLineID = coupon.lineID(id);

        for (var i = 0; i < this.items.length; i++) {
            if ((inLineID == coupon.lineID(this.items[i])) && (inLineID != 0)) {
                return i;
            }
        }
        return -1;
    }

    this.getLinesInfoString = function() {
        var s = '';
        for (var x = 0; x < this.items.length; x++) {
            if (document.getElementById('h_i' + this.items[x]) != null) {
                s += document.getElementById('h_i' + this.items[x]).value + '#';
            }
        }
        s = s.replace(/#$/, '');

        return s;
    }
}
