function hideDiv(id)
    {
        if (!document.getElementById) {    return;    }
        var el = document.getElementById(id);
        if (el.style.display == '')
        {
            el.style.display = 'none';
        }
        else
        {
            el.style.display = '';
        }
    }

document.onclick = function (e) {
    e = e || event;
    var target = e.target || e.srcElement;
    var box1 = document.getElementById("eroare");
     do {
        if (box1 == target) {
            // Click occured inside the box, do nothing.
            return
        }
        target = target.parentNode;
    } while (target);
    // Click was outside the box, hide it.
    box1.style.display = "none";
}



function create_pariu_div(mid,className,ch,p1,px,p2){
    if((pdv=document.getElementById("pariu")))
        document.getElementById("sub_middle").removeChild(pdv);
    pdv = document.createElement('div');
    pdv.setAttribute('id',"pariu");       //give id to it
    pdv.className="pariu";
    pdv.style.position="absolute";
    pdv.style.top=41;
    pdv.style.marginLeft=100;
    pdv.style.width=200;

    document.getElementById("sub_middle").appendChild(pdv);
    fdv = document.createElement('div');
    fdv.setAttribute('id',"left_flow_div");       //give id to it
    document.getElementById("pariu").appendChild(fdv);
    fdv = document.createElement('div');
    fdv.setAttribute('id',"right_flow_div");       //give id to it
    document.getElementById("pariu").appendChild(fdv);

    ddv = document.createElement('div'); // create dynamically div tag
    ddv.setAttribute('id',"div_panel");       //give id to it
    ddv.style.width=160;
    ddv.style.top=0;
    ddv.style.left=0;
    
    dv = document.createElement('div'); // create dynamically div tag
    dv.setAttribute('id',"panel");       //give id to it
  
    dv.style.position="relative";
    dv.style.top=0;
    //dv.style.left=20;
    dv.style.width=160;
    if(ch==1){ 
        marginLeft=310;
        msg="Pariu victorie acasa (1)";
    }
    if(ch==0){ 
        marginLeft=355;
       msg="Pariu egal (X)";
    }
    if(ch==2){ 
        marginLeft=380;
        msg="Pariu victorie deplasare (2)";
    }

   if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion <= "7.0")
       marginLeft=marginLeft-30;
   
    pdv.style.marginLeft=marginLeft
    dv.style.fontSize="10px";
    dv.style.backgroundColor="black";

    dv.innerHTML='<div align="right" onClick="hideDiv(\'pariu\')"><a href="#">x</a></div>'
            +msg+'<div align="center"><form action="elements/place_bet.php"> <tr cellpadding="3" > <td >miza: </td>'+
          '<td ><input id="miza" name="miza" type="text" value="" maxlength=5 size=5 onkeyup="restrict(this,\'0123456789.\');calc_castig('+ch+','+p1+','+px+','+p2+',this.value);"/></td>'+
          '</tr><input name="pariu" type="hidden" value="'+ch+'" />'+
          '<input name="meci" type="hidden" value="'+mid+'" />'+ 
          '<br>castig posibil <div id="castig1"></div><div id="castig2"></div>'+
          '<br><input name="submit" type="submit" value="bet" onClick="return askquestion()"/></form></div>';

    ddv.appendChild(dv);
    

    fdv = document.createElement('div');
    fdv.setAttribute('id',"bottom_corner_left");       //give id to it
    ddv.appendChild(fdv);


    fdv = document.createElement('div');
    fdv.setAttribute('id',"bottom_corner");       //give id to it
    fdv.style.width=120;
    ddv.appendChild(fdv);
    
    fdv = document.createElement('div');
    fdv.setAttribute('id',"bottom_corner_right");       //give id to it
    ddv.appendChild(fdv);


    document.getElementById("pariu").appendChild(ddv);
  

}
var IE = document.all?true:false;

function create_pariumatch_div(event,pid,pariu_init,cota_p_init,pariul_meu,cota_mea,miza,index){
    if(pdv=document.getElementById("pariu"))
        document.getElementById("sub_middle").removeChild(pdv);
    pdv = document.createElement('div');
    pdv.setAttribute('id',"pariu");       //give id to it
    pdv.className="pariu";
    pdv.style.position="absolute";
    //pdv.style.top=110+index*20;
    if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion <= "7.0")
        pos_y=event.clientY-document.getElementById("middle").offsetTop-document.getElementById("sub_middle").offsetTop+ document.body.scrollTop+50;
    else
        pos_y = event.pageY-document.getElementById("middle").offsetTop-document.getElementById("sub_middle").offsetTop;
    pdv.style.top=pos_y;
    pdv.style.marginLeft=100;
    pdv.style.width=160;
    
    document.getElementById("sub_middle").appendChild(pdv);

    fdv = document.createElement('div');
    fdv.setAttribute('id',"top_corner_left");       //give id to it
    pdv.appendChild(fdv);


    fdv = document.createElement('div');
    fdv.setAttribute('id',"bottom_corner");       //give id to it
    fdv.style.fontSize="10px";
    fdv.innerHTML='<b>cod pariu: '+index+'</b>';
    fdv.style.width=120;
    pdv.appendChild(fdv);

    fdv = document.createElement('div');
    fdv.setAttribute('id',"top_corner_right");       //give id to it
    fdv.innerHTML='<div align="left" onClick="hideDiv(\'pariu\')"><a href="#">x</a></div>';

    pdv.appendChild(fdv);

    ddv = document.createElement('div'); // create dynamically div tag
    ddv.setAttribute('id',"div_panel");       //give id to it
    ddv.style.width=160;
    ddv.style.top=0;
    ddv.style.left=0;
    
    dv = document.createElement('div'); // create dynamically div tag
    dv.setAttribute('id',"panel");       //give id to it
    //dv.className="pariu";                 // set the style classname
    //set the inner styling of the div tag 
    dv.style.position="relative";
    dv.style.width=160;
    
    if(pariul_meu==1){ 
       
        pdv.style.marginLeft=300;
        var msg="Pariu victorie acasa (1)";
    }
    if(pariul_meu==0){ 
        
        pdv.style.marginLeft=330;
        var msg="Pariu pe egal X";
    }
    if(pariul_meu==2){ 
        pdv.style.marginLeft=380;
        var msg="Pariu victorie deplasare (2)";
    }

    dv.style.fontSize="10px";
    dv.style.backgroundColor="black";    
//set the html content inside the div tag    
    dv.innerHTML=msg+'<form action="elements/match_bet.php"> <div align="center"> miza: '+miza+
          '<td ></td>'+
          '<div id="castig"></div>'+
          '<div id="pierdere"></div>'+
          '<input name="pariu" type="hidden" value="'+pariul_meu+'" />'+ 
          '<input name="pid" type="hidden" value="'+pid+'" />'+
          '<input name="miza" type="hidden" value="'+miza+'" />'+ 
          '<br><input name="submit" type="submit" value="bet" onClick="return askquestion()"/></div></form>';
    
    ddv.appendChild(dv);

    fdv = document.createElement('div');
    fdv.setAttribute('id',"bottom_corner_left");       //give id to it
    ddv.appendChild(fdv);


    fdv = document.createElement('div');
    fdv.setAttribute('id',"bottom_corner");       //give id to it
    fdv.style.width=120;
    ddv.appendChild(fdv);

    fdv = document.createElement('div');
    fdv.setAttribute('id',"bottom_corner_right");       //give id to it
    ddv.appendChild(fdv);

    document.getElementById("pariu").appendChild(ddv);
    calc_castig_match(pariu_init,cota_p_init,pariul_meu,cota_mea,miza);
}

function stergePariu(pid,miza){
    if(stergequestion())
        window.location="/elements/sterge_pariu.php?pid="+pid+"&miza="+miza;

}


function create_info_div(event,meci,user,otheruser,p,op,scor,c1,cx,c2){
    if((dv=document.getElementById("info")))
        document.getElementById("sub_middle").removeChild(dv);
    dv = document.createElement('div'); // create dynamically div tag
    dv.setAttribute('id',"info");       //give id to it
    dv.className="info";                 // set the style classname
    //set the inner styling of the div tag
    dv.style.position="absolute";
    
    if (IE)
        pos_y=event.clientY-document.getElementById("middle").offsetTop-document.getElementById("sub_middle").offsetTop+ document.body.scrollTop;
    else
        pos_y=event.pageY-document.getElementById("middle").offsetTop-document.getElementById("sub_middle").offsetTop;

    dv.style.width=250;
    dv.style.top=pos_y;
    dv.style.marginLeft=300;
    
    dv.style.fontSize="10px";
    dv.style.backgroundColor="black";

    if(p==0) p='X';
    if(op==0) op='X';
    dv.innerHTML='<div align="right" onClick="hideDiv(\'info\')"><a href="#">x</a></div>'+
        '<table style="font-size:10px;text-align:center;width:100%;">'+
        '<tr style="background:#5b5b5b;">'+
        '<td>partida</td>'+
        '<td>1</td>'+
        '<td>X</td>'+
        '<td>2</td>'+
        '</tr>'+
        '<tr style="background:#2b2b2b;">'+
        '<td>'+meci+'</td>'+
        '<td>'+c1+'</td>'+
        '<td>'+cx+'</td>'+
        '<td>'+c2+'</td>'+
        '</tr>'+
        '</table>'+
        '<b class="yellow">'+user+'</b> a pariat '+p+
        '<br><b class="yellow">'+otheruser+'</b> a pariat '+op+
        '<br>Rezultat final: '+scor;
         
    document.getElementById("sub_middle").appendChild(dv);
}

function askquestion() {
    var val=document.getElementById("miza").value;
    if(val<1){
        alert("Valoarea mizei trebuie sa fie mai mare de 1 credit!");
        return false; 
    }
    if (confirm("Doriti sa plasati acest pariu?") ) {
        return true;
      } else {
        return false;
   }
}

function stergequestion() {

    if (confirm("Doriti sa stergeti acest pariu?\n Veti pierde 10% din miza") ) {
        return true;
      } else {
        return false;
   }
}

function calc_castig_match(pi,ci,pm,cm,m){
    var miza=parseFloat(m);
    if(ci<cm){
         var val=miza;
         var val2=(ci/cm)*miza;
         document.getElementById("castig").innerHTML="castig posibil: +"+val.toFixed(2); 
         document.getElementById("pierdere").innerHTML="pierdere posibila: -"+val2.toFixed(2);  
    }else{
         var val=(cm/ci)*miza;
         var val2=miza;
         document.getElementById("castig").innerHTML="castig posibil: +"+val.toFixed(2);
         document.getElementById("pierdere").innerHTML="pierdere posibila: -"+val2.toFixed(2);     
    }
}

function calc_castig(ch,p1,px,p2,m){
    var miza=parseFloat(m);
    if(ch==1){
        var val=(p1/px)*miza;
        if(val>miza) val=miza;
        document.getElementById("castig1").innerHTML="fata de X: +"+val.toFixed(2); 
        val=(p1/p2)*miza;
        if(val>miza) val=miza;
        document.getElementById("castig2").innerHTML="fata de 2: +"+val.toFixed(2);
    }
    if(ch==0){
        var val=(px/p1)*miza;
        if(val>miza) val=miza;
        document.getElementById("castig1").innerHTML="fata de 1: +"+val.toFixed(2); 
        val=(px/p2)*miza;
        if(val>miza) val=miza;
        document.getElementById("castig2").innerHTML="fata de 2: +"+val.toFixed(2);
    }
    if(ch==2){
        var val=(p2/p1)*miza;
        if(val>miza) val=miza;
        document.getElementById("castig1").innerHTML="fata de 1: +"+val.toFixed(2); 
        val=(p2/px)*miza;
        if(val>miza) val=miza;
        document.getElementById("castig2").innerHTML="fata de x: +"+val.toFixed(2);
    }

}

function open_link_meci(id){
    window.location="/elements/open_meci.php?meci="+id;
}

function open_link_meci_order(id,order){
    window.location="/elements/open_meci.php?meci="+id+"&order="+order;
}

function open_link_user(id){
    window.location="/elements/open_user.php?user="+id;
}

function open_link_liga(id){
    window.location="/elements/open_liga.php?liga="+id;
}

var numb = "0123456789";
var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

function restrict(t,v){
    var w = "";
    if(v.length<1)
        v=numb+alpha;
    for (i=0; i < t.value.length; i++) {
        x = t.value.charAt(i);
        if (v.indexOf(x,0) != -1)
            w += x;
    }
    t.value = w;
}