$(document).ready(
        function()
        {
            if ($("a.oddj").size() > 0)
                $("a.oddj").click(oddclick);
            if ($("#cleanSlip").size() > 0)
                $("#cleanSlip").click(deleteallbets);
            if ($("a.delstringitem").size() > 0)
                $("a.delstringitem").click(deletebetstr);
            if ($("a.bankeritem").size() > 0)
                $("a.bankeritem").click(doBanker);
            if ($("#buttonSlip").size() > 0)
                $("#buttonSlip").click(submitslip);
            if ($("#checkbetid").size() > 0)
                $("#checkbetid").focus();
        }
);
function addbet( pars )
{
    $.post('slipmanager',
       {
         action: "add",
         sport:  pars[0],
         league: pars[1],
         oid:    pars[2]
       },
       onSuccessAdd
    );
}

function gotoLogin( pars )
{
    formObj        = document.forms.betform;
    formObj.action = urls["login"];
    $("#formDo").val("login");
    formObj.sport.value  = pars[0];
    formObj.league.value = pars[1];
    formObj.oid.value    = pars[2];
    formObj.submit();
}

// delete item from slip right
function deletebet()
{
    $.post('slipmanager',
       {
         action: "del",
         betId:  $(this).attr("betId")
       },
       onSuccessDel
    );
}

// delete item from slip center
function deletebetstr()
{
    $.post('slipmanager',
       {
         action: "del",
         betId:  $(this).attr("betId")
       },
       onSuccessDelItem
    );
}

// delete noajax item from slip right
function delbet(obj)
{
    $.post('slipmanager',
       {
         action: "del",
         betId:  $(obj).attr("betId")
       },
       onSuccessDel
    );
}

function deleteallbets()
{
    $.post('slipmanager',
       {
         action: "delall"
       },
       onSuccessDelAll
    );
}

// set type to server after change select
function setTypeSlip()
{
    nType = $("#typeExpress").val();
    $.post('slipmanager',
            {
                action: "changeType",
                newType: nType
            },
            onSuccessChangeType
    );
}

// set type to slip on site after action (add, del, doBanker)
function setTypeFromXML(code, type)
{
    var obj  = eval("(" + code + ")");
    if (typeof(obj[0]) != "undefined")
        $("#typeSlip").html(obj[0]);
    else
        generateListTypes(obj, type);
}

// on/off banker on slip
function doBanker(obj)
{
    $.post('slipmanager',
            {
                action: "doBanker",
                betId:  obj.value,
                status: obj.checked.toString()
            },
            onSuccessDoBanker
    );
}

function showBanker()
{
    $.post('slipmanager',
            {
                action: "showBanker"
            },
            onSuccessShowBanker
    );
}

function oddclick()
{
  auth = $(this).attr("isauthorized");
  betData = $(this).attr("betdata");
  params = betData.split(", ");
  if (auth == "true")
    addbet(params);
  else
    gotoLogin(params);
  return false;
}

function submitslip()
{
    countItems     = parseInt($("#buttonSlip").attr("countItems"));
    if (isNaN(countItems) || !isFinite(countItems))
        countItems = 0;
    if (minCombiSize != 0 && typeof(minCombiSize) != "undefined" && minCombiSize > countItems && countItems != 1)
    {
        alert(minCombiErr);
        return false;
    }
    formObj        = document.forms.slipform;
    if (countItems > 1)
    formObj.action = urls["combibet"];
    else if (countItems == 1)
    formObj.action = urls["bet"];
    else if (countItems == 0)
    formObj.action = '#';
    formObj.submit();
}

// callback function for add action
function onSuccessAdd(xml)
{
    error = $("error", xml);
    if (error.size() > 0)
    {
        alert(error.text());
        return false;
    }
    countItems  = $("countItems",  xml).text();
    type        = $("type",        xml).text();
    betId       = $("betId",       xml).text();
    sportId     = $("sportId",     xml).text();
    leagueId    = $("leagueId",    xml).text();
    evnt        = $("event",       xml).text();
    oddstype    = $("oddstype",    xml).text();
    runner      = $("runner",      xml).text();
    odd         = $("odd",         xml).text();
    totalOdd    = $("totalOdd",    xml).text();
    minStake    = $("minStake",    xml).text();
    maxStake    = $("maxStake",    xml).text();
    oid         = $("oid",         xml).text();
    titleBanker = $("titleBanker", xml).text();
    sBankers    = $("showBankers", xml).text();
    showBankers = (sBankers === 'true');
    // set type slip
    //setTypeSlip(type);
    var code = $("listTypesJS", xml).text();
    setTypeFromXML(code, type);
    $("#totalOddSlip").text(totalOdd);
    $("#minStakeSlip").text(minStake);
    $("#maxStakeSlip").text(maxStake);
    $("#buttonSlip").attr("countitems", countItems);

    // template of slip's item
    tplSlipItem = '<tr id="id_@betId@_0" class="itemslip">' +
                    '<input type="hidden" value="' + sportId + '" name="sport"/>' +
                    '<input type="hidden" value="' + leagueId + '" name="league"/>' +
                    '<input type="hidden" value="' + oid + '" name="oid"/>' +
                    '<td valign="top" class="sliptborder">' +
                      '<a href="#" class="delimg" betId="@betId@"><img src="' + imgRoot + '/delslip.gif" border="0" width="9" height="12" alt="' + titleSlip + '"/></a>' +
                    '</td>' +
                    '<td colspan="2" valign="top" class="sliptborder slipevent"> ' + evnt + ' </td>' +
                    '<td valign="top" class="sliptborder bnktd">' +
                            '<input type="checkbox" onclick="doBanker(this);" id="banker_@betId@" value="@betId@" title="' + titleBanker +'"/>' +
                       '</td>' +
                '</tr>' +
                '<tr id="id_@betId@_1" class="itemslip">' +
                    '<td><img src="' + imgRoot + '/tr.gif" border="0" width="1" height="1" alt="" /><br /></td>' +
                    '<td valign="top" colspan="2" class="slipmarket"> ' + oddstype + ' </td>' +
                    '<td class="bnktd"><img src="' + imgRoot + '/tr.gif" border="0" width="1" height="1" alt="" /><br /></td>' +
                '</tr>' +
                '<tr id="id_@betId@_2" class="itemslip">' +
                    '<td class="slipbborder"><img src="' + imgRoot + '/tr.gif" border="0" width="1" height="1" alt="" /><br /></td>' +
                    '<td valign="top" class="slipbborder slipoutcome"> ' + runner + ' </td>' +
                    '<td align="right" valign="top" class="slipbborder slipodd">' + odd + '</td>' +
                    '<td class="slipbborder bnktd"><img src="' + imgRoot + '/tr.gif" border="0" width="1" height="1" alt="" /><br /></td>' +
                '</tr>';

    matchTemplate = tplSlipItem.replace(new RegExp("@betId@", "g"), betId);

    $("#bankerSlip0").before(matchTemplate);
    if (!showBankers)
        $("td.bnktd").hide();
    $('#id_' + betId + "_0 a.delimg").bind('click', deletebet);
    $("#emptySlip").hide();
    $("#headerSlip1").show();
    $("#headerSlip2").show();
    $("#bankerSlip0").show();
    $("#bankerSlip1").show();
    $("#footerSlip0").show();
    $("#footerSlip1").show();
    $("#footerSlip2").show();
}

// callback function for delete action
function onSuccessDel(xml)
{
    error = $("error", xml);
    if (error.size() > 0)
    {
        alert(error.text());
        return false;
    }
    betId      = $("betId", xml).text();
    countItems = $("countItems", xml).text();
    // delete html of removed items
    $("#id_" + betId + "_0").remove();
    $("#id_" + betId + "_1").remove();
    $("#id_" + betId + "_2").remove();
    $("#buttonSlip").attr("countitems", countItems);
    if (countItems == 0)
        showEmptySlip();
    else
    {
        // set type slip
        type     = $("type", xml).text();
        totalOdd = $("totalOdd", xml).text();
        minStake = $("minStake", xml).text();
        maxStake = $("maxStake", xml).text();
        var code = $("listTypesJS", xml).text();
        setTypeFromXML(code, type);
        if (type == "0" || type == "1")
            showTotalOdd(totalOdd);
        $("#totalOddSlip").text(totalOdd);
        $("#minStakeSlip").text(minStake);
        $("#maxStakeSlip").text(maxStake);
    }
}

// callback function for delete action
function onSuccessDelItem(xml)
{
    error = $("error", xml);
    if (error.size() > 0)
    {
        alert(error.text());
        return false;
    }
    var betId      = $("betId", xml).text();
    var countItems = $("countItems", xml).text();
    $("#buttonSlip").attr("countitems", countItems);
    // delete html of removed items
    $("#id_" + betId + "_0").remove();
    if (countItems == 0)
        showEmptyPlaceCombi();
    else
    {
        if (countItems == 1)
            document.forms.stakeform.action = urls["bet"];
        type         = $("type", xml).text();
        var totalOdd = $("totalOdd", xml).text();
        var maxStake = $("maxStake", xml).text();
        var code     = $("listTypesJS", xml).text();
        setTypeFromXML(code, type);
        $("#oddval").val(totalOdd);
        var stake = parseInt($("#stake").val());
        if (stake > 0)
        {
            var maxwin = parseFloat(totalOdd) * stake;
            $("#maxwin").val(maxwin);
        }
        if (type == "0" || type == "1")
            showTotalOdd(totalOdd);
        $("#maxStakeSlip").text(maxStake);
    }
}

// callback function for delete action
function onSuccessDelAll(xml)
{
    error = $("error", xml);
    $("#buttonSlip").attr("countitems", "0");
    if (error.size() > 0)
    {
        alert(error.text());
        return false;
    }
    $("tr.itemslip").remove();
    showEmptySlip();
}

// callback function for change type slip in select
function onSuccessChangeType(xml)
{
    var error = $("error", xml);
    if (error.size() > 0)
    {
        alert(error.text());
        return;
    }
    var newType  = $("newType", xml).text();
    var minStake = $("minStake", xml).text();
    var maxStake = $("maxStake", xml).text();
    totalOdd     = $("totalOdd", xml).text();
    $("#oddval").val(totalOdd);
    if (newType != "1" && newType != "0")
    {
        var countBets = $("countBets", xml).text();
        showCountBets(countBets);
    }
    else
        showTotalOdd(totalOdd);
    var stake = parseInt($("#stake").val());
    if (stake > 0)
    {
        var maxwin = round(parseFloat(totalOdd) * stake);
        $("#maxwin").val(maxwin);
    }
    $("#minStakeSlip").text(minStake);
    $("#maxStakeSlip").text(maxStake);
}

function onSuccessDoBanker(xml)
{
    var error = $("error", xml);
    if (error.size() > 0)
    {
        alert(error.text());
        return;
    }
    var code      = $("listTypesJS", xml).text();
    var newStatus = $("newStatus", xml).text();
    var betId     = $("betId", xml).text();
    var totalOdd  = $("totalOdd", xml).text();
    var newType   = $("newType", xml).text();
    var maxStake  = $("maxStake", xml).text();
    var actionTxt = $("actionText", xml).text();
    var aid       = "#banker_" + betId;
    $("#oddval").val(totalOdd);
    $(aid).get(0).checked = (newStatus === 'true');
    $(aid).attr("title", actionTxt);
    setTypeFromXML(code, newType);
    if (newType != "1" && newType != "0")
    {
        var countBets = $("countBets", xml).text();
        showCountBets(countBets);
    }
    else
        showTotalOdd(totalOdd);
    var stake = parseInt($("#stake").val());
    if (stake > 0)
    {
        var maxwin = parseFloat(totalOdd) * stake;
        $("#maxwin").val(maxwin);
    }
    $("#maxStakeSlip").text(maxStake);
}

function onSuccessShowBanker(xml)
{
    var error = $("error", xml);
    if (error.size() > 0)
    {
        alert(error.text());
        return;
    }
    var sBankers    = $("showBankers", xml).text();
    var showBankers = (sBankers === 'true');
    var bankersText = $("bankersText", xml).text();
    var code        = $("listTypesJS", xml).text();
    var newType     = $("newType", xml).text();
    var maxStake    = $("maxStake", xml).text();
    var totalOdd    = $("totalOdd", xml).text();
    if (showBankers)
    {
        $("#btSlipHead1").show();
        $("td.bnktd").show();
        $("#plcmb1").attr("colspan", 7);
        $("#plcmb2").attr("colspan", 7);
        $("#btslip1").show();
        $("#btslip2").attr("colspan", 4);
        $("#emptySlip td").attr("colspan", 4);
        $("#btslip3").show();
        $("#headerSlip2 td").attr("colspan", 4);
        $("#btslip4").attr("colspan", 4);
        $("#bankerSlip0 td").attr("colspan", 4);
        $("#bankerSlip1 td").attr("colspan", 4);
        $("#footerSlip1 td").attr("colspan", 4);
        $("#footerSlip2 td").attr("colspan", 4);
    }
    else
    {
        $("#btSlipHead1").hide();
        $("td.bnktd").hide();
        $("#plcmb1").attr("colspan", 6);
        $("#plcmb2").attr("colspan", 6);
        $("#btslip1").hide();
        $("#btslip2").attr("colspan", 3);
        $("#emptySlip td").attr("colspan", 3);
        $("#btslip3").hide();
        $("#headerSlip2 td").attr("colspan", 3);
        $("#bankerSlip0 td").attr("colspan", 3);
        $("#bankerSlip1 td").attr("colspan", 3);
        $("#btslip4").attr("colspan", 3);
        $("#footerSlip1 td").attr("colspan", 3);
        $("#footerSlip2 td").attr("colspan", 3);
        if (newType != "1" && newType != "0")
        {
            var countBets = $("countBets", xml).text();
            showCountBets(countBets);
        }
        else
            showTotalOdd(totalOdd);
        $("td.bnktd input").each(
            function()
            {
                this.checked = false;
            }
        );
        var stake = parseInt($("#stake").val());
        if (stake > 0)
        {
            var maxwin = parseFloat(totalOdd) * stake;
            $("#maxwin").val(maxwin);
        }
        $("#maxStakeSlip").text(maxStake);
    }
    $("#withBankerText").text(bankersText);
    setTypeFromXML(code, newType);
}
function showEmptySlip()
{
    $("#headerSlip1").hide();
    $("#headerSlip2").hide();
    $("#bankerSlip0").hide();
    $("#bankerSlip1").hide();
    $("#footerSlip0").hide();
    $("#footerSlip1").hide();
    $("#footerSlip2").hide();
    $("#emptySlip").show();
}

function showEmptyPlaceCombi()
{
    $("#placecombibet").hide();
    $("#placeBetMsg").text(emptyBet);
}

function showCountBets(countBets)
{
    $("#countBetsSlip").text(countBets);
    $("#countBetsTr").show();
    $("#totalOddTr").hide();
}

function showTotalOdd(totalOdd)
{
    $("#totalOddSlip").text(totalOdd);
    $("#totalOddTr").show();
    $("#countBetsTr").hide();
}

function generateListTypes(obj, type)
{
    typeSlipContent = '<select class="selecttext" style="width:110px;" name="typeExpress" id="typeExpress" onchange="setTypeSlip();">';
    for (key in obj)
        typeSlipContent += '<option value="' + key + '">' + obj[key] + '</option>';
    typeSlipContent += '</select>';
    $("#typeSlip").html(typeSlipContent);
    $("#typeExpress").val(type);
}
