function getElementPosition(elemID)
{
	var offsetTrail = document.getElementById(elemID);
	var offsetLeft = 0;
	var offsetTop = 0;
	while (offsetTrail)
	{
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined')
	{
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	return {left:offsetLeft,top:offsetTop};
}

function addElement(bgtop,txttop) 
{
	var x=getElementPosition('help').left;
	var y=getElementPosition('help').top;
	var ni = document.getElementById('global');
	var newdiv = document.createElement('span');
	newdiv.setAttribute('id','popup');
	newdiv.setAttribute('align','left');
    newdiv.style.width="170px";
	newdiv.style.backgroundColor=bgtop;
    newdiv.style.color=txttop;
	newdiv.style.top=y;
	newdiv.style.left=x;
	newdiv.style.border="#000";
	newdiv.style.position="absolute";
	newdiv.innerHTML="<strong>Information about the email</strong><br>This email must be the one you will use when you request cashout. All payments will be sent to this email.";
	ni.appendChild(newdiv);
}

function removeElement() 
{
	var d = document.getElementById('global');
	var olddiv = document.getElementById('popup');
	d.removeChild(olddiv);
}
function resizeIframe() 
{
    var height = document.documentElement.clientHeight;
    height -= document.getElementById('frame').offsetTop;
    
    // not sure how to get this dynamically
    height -= 42; /* whatever you set your body bottom margin/padding to be */
    
    document.getElementById('frame').style.height = height +"px";
    
}
function clicked(name)
{
	if (name=='logout.php')
    {
    	Delete_Cookie( 'carrinho', '', '' )
    }
	document.getElementById('frame').src=name;
}

function mouseover(name)
{
	document.getElementById(name).style.textDecoration="underline"
}

function mouseout(name)
{
	document.getElementById(name).style.textDecoration="none";
}
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 'center_index.php';
	}
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function conta()
{
	try
    {
    	var value=0;
        var ganhos=0;
        var total=0;
        var conteudo="";
        var cook=Get_Cookie('carrinho');
        var temp = cook.split('#');
        var temp2 = new Array();
        for (var i=0;i<temp.length-1;i++)
        {
            temp2=temp[i].split('|');
            if (Math.round(document.getElementById('b'+temp2[0]).value*100)>0)
            {
                value=Math.round(100*document.getElementById('b'+temp2[0]).value)/100
                ganhos=ganhos+Math.round(value*temp2[2]*100)/100
                total=total+Math.round(100*document.getElementById('b'+temp2[0]).value)/100
            }
            else
            	value=0;
            conteudo=conteudo+temp2[0]+'|'+temp2[1]+'|'+temp2[2]+'|'+temp2[3]+'|'+temp2[4]+'|'+value+'#';
        }
        if (i)
        {
            document.getElementById('b'+temp2[0]).value=Math.round(100*document.getElementById('b'+temp2[0]).value)/100
            document.getElementById('maxwin').innerHTML=Math.round(ganhos*100)/100;
            document.getElementById('total').innerHTML=Math.round(total*100)/100;
            Set_Cookie( 'carrinho', conteudo, '', '', '', '' );
        }
        else
        {
        	document.getElementById('maxwin').innerHTML=0
       	 	document.getElementById('total').innerHTML=0
    	}	
    }
    catch(e) 
	{
        document.getElementById('maxwin').innerHTML=0
        document.getElementById('total').innerHTML=0
    }
}

function decimal2fractional(odd)
{
	numerador=Math.round(odd*100);
	denominador=100;
	while (numerador%2==0)
	{
		numerador=$numerador/2;
		denominador=denominador/2;
	}
	numerador=numerador-(denominador);
	return$numerador + "/" + $denominador;
}

function decimal2american(odd)
{
	if (odd>=2)
		odd="+" + Math.floor(100*(odd-1));
	else
		odd=Math.floor(-100/(odd-1));
	return odd;
}

function adiciona(id,bet,odd,hometeam,visitorteam,num,value)
{
var odd1=odd;
var type='';
switch (type)
{
	case 'fractional': 	odd1=decimal2fractional(odd);
    					break;
    case 'american':	odd1=decimal2american(odd);
    					break;
}

ok1=1;
var conteudo="";
var cook=Get_Cookie('carrinho');
temp = cook.split('#');
var temp2 = new Array();
if(!num)
{
    for (i=0;i<temp.length-1;i++)
    {
        temp2=temp[i].split('|');
        if (temp2[0]==id)
            ok1=0;
        conteudo=conteudo+temp2[0]+'|'+temp2[1]+'|'+temp2[2]+'|'+temp2[3]+'|'+temp2[4]+'|'+temp2[5]+'#';
    }
    if (!ok1)
        alert('Game already picked');
}    
     if(ok1)
     {
         if (!parent.document.getElementsByTagName) return;
         if (!num)
         	tabBody=parent.document.getElementsByTagName('tbody').item(1);
         else
         	tabBody=document.getElementsByTagName('tbody').item(1);
         row=document.createElement('TR');
         cell1 = document.createElement('TD');
         cell2 = document.createElement('TD');
         cell3 = document.createElement('TD');
         cell4 = document.createElement('TD');
         cell5 = document.createElement('TD');
         var element1 = document.createElement('input');
		 element1.type = 'text';
         element1.size='2';
         element1.setAttribute('style','font-size:10px');
         element1.setAttribute('onchange','conta();');
         element1.id='b'+id;
         if(value)
         	element1.value=value
         cell5.appendChild(element1);
         if (bet==hometeam)
         {
         	cellodd = document.createElement('TD');
            textnodeodd = document.createTextNode(odd1);
            cellodd.appendChild(textnodeodd);
            cellodd.setAttribute('style','float:right;');
            cell2.appendChild(cellodd);
         	 textnode1=document.createTextNode('x');
         	 textnode2=document.createTextNode(hometeam);
             textnode3=document.createTextNode('Draw');
         	 textnode4=document.createTextNode(visitorteam);
         }
         else
         {
             if (bet==visitorteam)
             {
             	cellodd = document.createElement('TD');
                textnodeodd = document.createTextNode(odd1);
                cellodd.appendChild(textnodeodd);
                cellodd.setAttribute('style','float:right;');
                cell4.appendChild(cellodd);
             	 textnode1=document.createTextNode('x')
                 textnode2=document.createTextNode(hometeam);
                 textnode3=document.createTextNode('Draw');
             	 textnode4=document.createTextNode(visitorteam);
             }
             else
             {
             	cellodd = document.createElement('TD');
                textnodeodd = document.createTextNode(odd1);
                cellodd.appendChild(textnodeodd);
                cellodd.setAttribute('style','float:right;');
                cell3.appendChild(cellodd);
             	 textnode1=document.createTextNode('x')
         		 textnode2=document.createTextNode(hometeam);
             	 textnode3=document.createTextNode('Draw');
                 textnode4=document.createTextNode(visitorteam);
             }
         }
    
         var cssString = 'text-decoration:underline;font-weight:bold;';
         
         cell1.appendChild(textnode1);
         cell2.appendChild(textnode2);
         cell3.appendChild(textnode3);
         cell4.appendChild(textnode4);
         cell1.setAttribute('style','width:5%');
         cell1.setAttribute('onclick','elimina("'+id+'")');
         cell2.setAttribute('style','width:32%');
         cell3.setAttribute('style','width:20%');
         cell4.setAttribute('style','width:32%');
         cell5.setAttribute('style','width:11%');
         if (bet==hometeam)
         {
             cell2.setAttribute('style',cssString);
         }
         else
         {
             if (bet==visitorteam)
             {
                 cell4.setAttribute('style',cssString);
             }
             else
             {
                 cell3.setAttribute('style',cssString);
             }
         }
         
         row.appendChild(cell1);
         row.appendChild(cell2);
         row.appendChild(cell3);
         row.appendChild(cell4);
         row.appendChild(cell5);
         row.setAttribute('ID',id);
         tabBody.appendChild(row);
         
         conteudo = conteudo + id+'|'+bet+'|'+odd+'|'+hometeam+'|'+visitorteam+'|'+value+'#';
         if(!num)
		 {
             Set_Cookie( 'carrinho', conteudo, '', '', '', '' );
         }
     }
}

function elimina(id) 
{
var conteudo="";
	try 
	{
		var table = document.getElementsByTagName("tbody").item(1);
		
		k=0;
		for(var i=0; i<table.rows.length; i++) 
		{
            if (table.rows[i].id==id)
			{
				table.deleteRow(i);
                i=table.rows.length+1;
			}
		}
        var cook=Get_Cookie('carrinho');
        temp = cook.split('#');
        for (i=0;i<temp.length-1;i++)
        {
            temp2=temp[i].split('|');
            if (temp2[0]!=id)
            	conteudo=conteudo+temp2[0]+'|'+temp2[1]+'|'+temp2[2]+'|'+temp2[3]+'|'+temp2[4]+'|'+temp2[5]+'#';
        }
        Set_Cookie( 'carrinho', conteudo, '', '', '', '' );
        conta();
 	}
	catch(e) 
	{
		alert(e);
	}
}

function insert()
{
	var i=0;
	var cook=Get_Cookie('carrinho');
    //alert(cook);
    if (cook!='center_index.php')
    {
        temp = cook.split('#');
        var temp2 = new Array();
        for (i=0;i<temp.length-1;i++)
        {
            temp2=temp[i].split('|');
            adiciona(temp2[0],temp2[1],temp2[2],temp2[3],temp2[4],1,temp2[5]);
        } 
        conta();
    }
}
