﻿//
function cbcheckall(obj)
{
 for(i = 0; i<document.forms[0].length; i++)
    {
        var el = document.forms[0].elements[i];
        if(el.type == "checkbox" )
        {
            el.checked = obj.checked;
        }
    }

return false;
}

//Sport
function customFloat(val)
{
var trans = String(val)
trans = trans.replace(',','.');
return parseFloat(trans);
//return trans;
}

function sa()
{
var s="";
var els = $(":checkbox:checked");
$(":checkbox:checked").each(function() {
                s=s+$(this).attr("sid")+";"
                window.location.href = window.location.href +"?s="+s;
            });
}
//Group 
function sg()
{
var s="";
var els = $(":checkbox:checked");
$(":checkbox:checked").each(function() {
                s=s+$(this).attr("id")+";"
	                    //
    window.location.href = "?group="+s;
            });
}
//Remove stake
function rs(k, kval)
{
    if(confirm("Бажаєте видалити ставку?"))
    {
      $.ajax({
       url: 'rs.aspx',
       cache: true,
       type: 'GET',
       timeout: 0,
       error: function() {alert('')},
       success: function(){
            $('#tr'+k).remove();
       },
       dataType: "html",
       data: {r : k, 'rnd' : (Math.round(Math.random() * 10000))}
      });
       
      //hdnExprTotal
      var hdnExprTotal = $("#ctl00_cartctrl_hdnExprTotal");//.val());
      var divExprTotal = $("#ctl00_cartctrl_divExprTotal");//.val());
      var prevk = customFloat(hdnExprTotal.val());

        
        //var nevk = 0;
        var nevk = (prevk / customFloat(kval)) ;
        
        hdnExprTotal.val(nevk);// = ;
        divExprTotal.text("Експрес: "+roundNumber(nevk,2));

        if(nevk<=1000)
        {
        divExprTotal.css("color","black");
        }

        if ($("#tbStakes tr").size() == 3|| $("#tbStakes tr").size() ==6)
            {
                $("#tbStakes tr").hide();
                $("#tbStakes tr:last").hide();
            }
            calcFrmWidth();
    }   
}

function clearcart()
{

if(confirm("Бажаєте очистити корзину?"))
{
;
 $.ajax({
       url: 'rs.aspx',
       cache: true,
       type: 'GET',
       timeout: 0,
       error: function() {},
       success: function(){
       
        $("#tbStakes tr.cartline").remove();
        
        $("#hdnExprTotal").val("0");// = ;
        $("#divExprTotal").text("Експрес:  "+$("#hdnExprTotal").val());
        $("#tbStakes tr").hide();
       },
       dataType: "html",
       data: {'c':'y', 'rnd' : (Math.round(Math.random() * 10000))}
      });

}
return false;
}



function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function fchange(ddl, dest) {
    $("#" + dest).text($(ddl).val());
}

function fchange2(ddl, dest, dest2, indicator, basekoef) {

    var nevkoef = $("#" + ddl.id + " option:selected").text();

    if (customFloat(nevkoef) < customFloat(indicator)) {
        $("#" + dest).text(basekoef);
        $("#" + dest2).text($(ddl).val());
    }
    else {
        $("#" + dest).text($(ddl).val());
        $("#" + dest2).text(basekoef);
        
        
    }
    
}

function dsb(k, n, f) {

//    alert(k);
//    alert(f);
    $.getJSON("ds.aspx?n=" + n + "&k=" + k + "&bk=" + $("#" + f.id).text(),
    function(data) {
        $("#tbStakes tr").show();
        $("#tbStakes tr:last").prev().before("<tr class='cartline' id='tr" + k + "'><td><table border='0' cellpadding ='2' cellspacing='0' width='100%'><tr><td style='white-space:nowrap;'>" + data.k1 + "-" + data.k2 + "</td><td style='white-space:nowrap;'>к. " + data.k + "</td></tr><tr><td >" + data.wk + "</td><td  align='right'><input type='image' src='img/Close-1-icon.png' onclick='rs(\"" + k + "\",\"" + data.k + "\");return false;' style='cursor: pointer;'  alt='Видалити' ></td></tr></table></td></tr>");
        $("#tbStakes tr:last").show();

        var hdnExprTotal = $("#ctl00_cartctrl_hdnExprTotal"); 
        var divExprTotal = $("#ctl00_cartctrl_divExprTotal"); 
        var prevk = customFloat(hdnExprTotal.val());
        var nevk = 0;
        if (prevk == 0) {
            nevk = customFloat(data.k);
        }
        else {
            nevk = (prevk * customFloat(data.k)); //),2);
        }
        hdnExprTotal.val(nevk);
        divExprTotal.text("Експрес:  " + roundNumber(nevk, 2));
        calcFrmWidth();
    });
    return false;
}


function ds(k, n)
{
    $.getJSON("ds.aspx?n="+n+"&k="+k,
    function(data){
        $("#tbStakes tr").show();
        $("#tbStakes tr:last").prev().before("<tr class='cartline' id='tr"+k+"'><td><table border='0' cellpadding ='2' cellspacing='0' width='100%'><tr><td style='white-space:nowrap;'>"+data.k1+"-"+data.k2+"</td><td style='white-space:nowrap;'>к. "+data.k+"</td></tr><tr><td >"+data.wk+"</td><td  align='right'><input type='image' src='img/Close-1-icon.png' onclick='rs(\""+k+"\",\""+data.k+"\");return false;' style='cursor: pointer;'  alt='Видалити' ></td></tr></table></td></tr>");
        $("#tbStakes tr:last").show();
        
        var hdnExprTotal = $("#ctl00_cartctrl_hdnExprTotal");//.val());
        var divExprTotal = $("#ctl00_cartctrl_divExprTotal");//.val(
        var prevk = customFloat(hdnExprTotal.val());
        var nevk = 0;
        if(prevk==0)
        {
            nevk =   customFloat(data.k);
        }
        else
        {
            nevk =  (prevk * customFloat(data.k));//),2);
        }
        hdnExprTotal.val(nevk);
        divExprTotal.text("Експрес:  "+roundNumber(nevk,2));
	if(nevk>1000)
        {
        	divExprTotal.css("color","red");
        }
        calcFrmWidth();
    });
return false;
}

var rgggg = /^\d+(\.\d{1,2})?$/;
function dsl(l, n, g)
{
 	$.getJSON("ds.aspx?n="+n+"&k="+l+"&c=n",
    function(data){
    showFrm(l,n,data.k1,data.k2,data.k,data.wk);
    });
 	
    return false;
}

function ShowRozpys(id)
{
    var obj = window.document.getElementById(id);
    if(obj.style.display == 'none')
        obj.style.display =  'inline';
    else
        obj.style.display= 'none';
}

function CheckLine(txt, linemax, linedate)
{
if(parseInt(txt.value) > parseInt(linemax)){
    alert("Максимальна ставка" + linemax +" грн.");
    txt.Focus();
    }
}


function CheckForm()
{
    var vantedtotal;
    vantedtotal= 0;

    for(i = 0; i<document.forms[0].length; i++)
    {
        var el = document.forms[0].elements[i];
        if(el.type == "text")
        {
            if(el.value!= "")
            {
                vantedtotal = customFloat(el.value) +vantedtotal;
            }
        }
    }
     var max = customFloat(document.getElementById("ctl00_ContentPlaceHolder1_ccartctrl_usrMaxAllow").value);
    
    if(vantedtotal>max)
    {
        alert("У вас недостатньо коштів.");
        return false;
    }
        
    return true;
}


////Cookies
function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
