var pageIsLoad = false;
$(document).ready(function()
{

	if($(".PromoIndexText").length)
    {
		$(".PromoIndexText").find("div.text").each(function(){$(this).css("display", "none")});
		$(".PromoIndexText").find("div.text").eq(0).css("display", "block");
	}

	if($(".tabBlock").length)
    {
		var url = window.location;
		var id=url.hash.substring(1);
		var t = $(".tabBlock .tabs #tab"+id);
		showTab(t, id);
	}
    if ($("#nextcheck").size() > 0)
        $("#nextcheck").focus();
    if ($("#betplaceSlip").size() > 0)
    {
        $("#betplaceSlip").click(function()
        {
            formObj        = document.forms.stakeform;
            // check min stake
            ms = parseFloat( $("#minstake").val() );
            st = parseFloat( $("#stake").val() );
            if ( st < ms )
            {
                alert( placebetErr + " " + ms + currency );
                $("#stake").val(ms);
                calcmw( formObj );
                return;
            }

            // ok
            document.getElementById("betdata").style.display='none';
            document.getElementById("accepting").style.display='block';
            formObj.submit();
        });
    }
    if ($("#combiplaceSlip").size() > 0)
    {
        $("#combiplaceSlip").click(function()
        {
            formObj        = document.forms.stakeform;
            // check min stake
            ms = parseFloat( $("#minStakeSlip").text() );
            st = parseFloat( $("#stake").val() );
            if ( st < ms )
            {
              alert( placebetErr + " " + ms + currency );
              $("#stake").val(ms);
              calcmw( formObj );
              return;
            }

            // ok
            $("#betdata").hide();
            $("#accepting").show();
            formObj.submit();
        });
    }

    if ($("#liveplaceSlip").size() > 0)
    {
        $("#liveplaceSlip").click(function()
        {
            formObj        = document.forms.stakeform;
            // check min stake
            ms = parseFloat( $("#minstake").val() );
            st = $("#stake").val();
            if (st == "")
                st = 0.0;
            else
                st = parseFloat( st );
            if ( st < ms )
            {
                alert( placebetErr + " " + ms + currency );
                $("#stake").val(ms);
                calcmw( formObj );
                return;
            }

            // ok
            document.getElementById("betdata").style.display='none';
            document.getElementById("accepting").style.display='block';
            formObj.submit();
        });
    }
    pageIsLoad = true;
});

function showLiveMatchInfo(id){

    if($(".livecontainer").length){
        $('.livecontainer').html("<div class='whait'></div>");
        $('.livecontainer').load("?do=livemonitor&id="+id);
    }
}

function show( objName )
{
  var o = document.getElementById( objName );
  o.style.display = ( o.style.display == 'none' ) ? '' : 'none';
}
function bet( sportP, leagueP, oidP )
{
  formObj = document.forms.betform;
  formObj.sport.value = sportP;
  formObj.league.value = leagueP;
  formObj.oid.value = oidP;
  formObj.submit();
}

function bet2( sportP, leagueP, oidP )
{
  formObj                                 = document.forms.betform;
  formObj.action                          = actionUrl;
  document.getElementById("formDo").value = "login";
  formObj.sport.value                     = sportP;
  formObj.league.value                    = leagueP;
  formObj.oid.value                       = oidP;
  formObj.submit();
}


function round(variable)
{
		t_variable = variable + '';
    if (t_variable.match(/^[1-9]([0-9])*$/))
		{
			t_variable += '.00';
			return t_variable;
		}
    if (t_variable.match(/^[1-9]([0-9])*\.$/))
		{
			t_variable += '00';
			return t_variable;
		}
    if (t_variable.match(/^[1-9]([0-9])*\.[0-9]{1}$/))
		{
			t_variable += '0';
			return t_variable;
		}

    var test_adding = '';
    var test_preadding = '';

    var value_string = '';
    value_string +=  variable;

    start_position = value_string.indexOf('.');
    if (start_position == -1)
    {
        return variable;
    }
    else
    {
        new_value = value_string.substr(0, start_position+3);
        test_adding = parseInt(value_string.substring(start_position+3, start_position+4));
        last_digit = parseInt(value_string.substring(start_position+2, start_position+3));
        first_digit = parseInt(value_string.substring(start_position+1, start_position+2));
        integer_part = parseInt(value_string.substr(0, start_position));

        if (test_adding >= 5)
        {
            if (last_digit != 9){ last_digit += 1;}
            else
            {
                last_digit = 0;
                if (first_digit != 9){ first_digit += 1;}
                else
                {
                    first_digit = 0;
                    integer_part += 1;
                }
            }
        }
        new_value = '' + integer_part + '.' + first_digit + last_digit;

        if (new_value.match(/^[1-9]([0-9])*$/))           new_value += '.00';
        if (new_value.match(/^[1-9]([0-9])*\.$/))         new_value += '00';
        if (new_value.match(/^[1-9]([0-9])*\.[0-9]{1}$/)) new_value += '0';

				return new_value;
    }
}


function getMaxWin(maxKoef, stakeValue)
{
	return round(round(maxKoef) * round(stakeValue));

}

function calcmw(formObj)
{
	var stakeObj = formObj.stake;
	maxCoef = parseFloat(document.getElementById("oddval").value);

	if (stakeObj.value == undefined || maxCoef == undefined)
	{
        maxWinObj.value = '-';
		return;
	}

	var maxWinObj = document.getElementById("maxwin");

    stakeObj.value = stakeObj.value.replace(/^\s+/, '');
    stakeObj.value = stakeObj.value.replace(/\s+$/, '');
    stakeObj.value = stakeObj.value.replace(/,/, '.');

    var sv = stakeObj.value;
    
	if ( !sv.match(/^[1-9][0-9]*(\.[0-9]{1,2})?$/) )
	{
        maxWinObj.value = '-';
        return;
	}

	newVal = getMaxWin(maxCoef, sv);
	if ( newVal != "NaN" ) {
	  maxWinObj.value = newVal;
    }
}

function showAccDetails(){
    if($("#moreDetails").length){
        if($("#moreDetails").css("display")=="table"){
            $("#moreDetails").hide();
        }else{
            $("#moreDetails").show();
        }
    }
}


function showNextPromo(){
  var pos = 0;
  $(".PromoIndexText").find("div.text").each(function(){if($(this).css("display")=="block"){pos = $(this).index()}});
    $(".PromoIndexText").find("div.text").each(function(){$(this).css("display", "none")});
  if(pos<$(".PromoIndexText div.text").length){
    $(".PromoIndexText").find("div.text").eq(pos).show();
  }else{
    $(".PromoIndexText").find("div.text").eq(0).show();
  }
}


function TabLink(id){
    if($(".GamesList").length){
        var t = $(".tabBlock .tabs #tab"+id);
        showTab(t, id);
        location.href="#AllTabBlockAnchor";
    }
}
function showTab(t, id){
	var block=$(t).parent(".tabs").parent("div.tabBlock");
	$(block).find(".tab").each(function(){
        $(this).removeClass("on");
        $(this).removeClass("livetabon");
	});
	$(block).find(".block").each(function(){
		$(this).css("display", "none");
	});

	$(t).addClass("on");
    if($(t).hasClass("livetab")){
        $(t).addClass("livetabon");
    }
	$(block).find("#block"+id).show();
}

function playReal(nm, url){
    if(pageIsLoad){
        var r = new RegExp(' ', 'g');
        var p = nm.replace(r, '_');
        pp(url+"&nm="+p+"&t=1", 790, 650, 100, 100, "casino_game", 0, 0);
    }
}

function playDemo(nm, url){
    if(pageIsLoad){
        var r = new RegExp(' ', 'g');
        var p = nm.replace(r, '_');
        pp(url+"&nm="+p+"&t=0", 790, 650, 100, 100, "casino_game", 0, 0);
    }
}


