﻿
    var m_nShownBoardCards=0;
    var i = 0;
    var nMiddleBar = 10;
    var nTimerId = 0,nClockTimer = 0;
    var m_nScore = 0;
    var m_nNumOfPlayers = 0;
    var m_nPlayMode = 0; //0 = Practice,  1 = Test , 2 = Click Next to start Test
    var FIRE_FOX = "Netscape";
    var IEXPLORER = "Microsoft Internet Explorer";
    
    function Init_Webipoker()
    {
        //--------------------  On form load   -------------------------------------------------------    
        if(document.getElementById('hidMinutes').value != "")
            m_nMinutes = document.getElementById('hidMinutes').value;
            
        if(document.getElementById('hidSeconds').value != "")
            m_nSeconds = document.getElementById('hidSeconds').value;
            
        if(document.getElementById('hidPlayMode').value != "")
            m_nPlayMode = document.getElementById('hidPlayMode').value;
            
         if(document.getElementById('hidNumOfPlayers').value != "")
            m_nNumOfPlayers = document.getElementById('hidNumOfPlayers').value;

        //Test Mode
        if(m_nPlayMode == 1 || m_nPlayMode == 2)
        {
            if(m_nPlayMode == 2)
            {
                document.getElementById('TopBoard').innerHTML = "Fai clic su Successivo per iniziare a giocare !";
                document.getElementById('PokerMessageBoard').innerHTML = "Successivo = $10 &nbsp&nbsp|&nbsp&nbsp Fold = $0 ";
                StartTest(true);
            }
            
            StartTest(false);
            
            if(document.getElementById('hidScore').value == '')
                m_nScore = 0;
            else
                m_nScore = document.getElementById('hidScore').value;
                
            //Update the score display according to m_nScore.
            //UpdateUserScore(0);
        }
        else if(m_nPlayMode == 0)
        {
            document.getElementById('PokerMessageBoard').innerHTML = rngOddsMSG[0];// + "<br/> Click Next to see the Flop cards"; //Write player Odds    
            StartPractice(false);
        }
        
        //Initiate  all cards
        for(i=1 ; i <= 4 ; ++i)
        {    
            //Show the number of players with background
            if(i == m_nNumOfPlayers)
                document.getElementById(m_nNumOfPlayers + "Players").style.backgroundColor = "#E3E3E3";
            else 
                document.getElementById(i + "Players").style.backgroundColor = "White";
            
        }
          
        if(document.getElementById('hidShowOdds').value == '1' || document.getElementById('hidShowTOdds').value == '1')
        {
            ShowOdds((document.getElementById('hidShowOdds').value == '1'),(document.getElementById('hidShowTOdds').value == '1'));
        }
    }
    function btnFold_onclick()
    {
        if(m_nShownBoardCards == 0)
        {
            ShowFlopCard();
            ShowFlopCard();
            ShowFlopCard();
            DealTheTurn();
            DealTheRiver();
        }
       
        else if(m_nShownBoardCards == 3) 
        {
            DealTheTurn();
            DealTheRiver();
            
            if(m_nPlayMode == 1)
                UpdateUserScore(-20);
        }
        else if(m_nShownBoardCards == 4) 
        {
            DealTheRiver();
            
            if(m_nPlayMode == 1)
                UpdateUserScore(-30);
        }
        
        m_nShownBoardCards = 5;
        document.getElementById('SoundPlayer').src="../App_Res/Sounds/check.wav";
        document.getElementById('PokerMessageBoard').innerHTML = "";
        
        for(i = 0; i < rngMessage.length ; ++i)
        {
            document.getElementById('PokerMessageBoard').innerHTML += rngMessage[i] + "<br>";
        }
        
        //If this is a test mode show all the cards at the end of each round.
        if(m_nPlayMode == 1)
            ShowCards(true)
            
        if(document.getElementById('hidShowOdds').value == 1)
            ShowOdds(true,false);
           
    }
    
    function UpdateUserScore(nScore)
    {
        m_nScore = parseInt(m_nScore) + parseInt(nScore);
        document.getElementById('Score').innerHTML = "$" + m_nScore  ;
        document.getElementById('hidScore').value = m_nScore;
    }

    function ShowFlopCard()
    {
        if(m_nShownBoardCards == 3)
        {
            clearInterval(nTimerId);
            
            if(document.getElementById('hidShowOdds').value == '1' || document.getElementById('hidShowTOdds').value == '1')
            {
                ShowOdds((document.getElementById('hidShowOdds').value == '1'),(document.getElementById('hidShowTOdds').value == '1'));
            }
        }
        else
        {      
            //document.getElementById('HouseCard' + (m_nShownBoardCards + 1)).src = '../App_Res/Pics/Cards/' + rngHouseCards[m_nShownBoardCards];;
            document.getElementById('HouseCard' + (m_nShownBoardCards + 1)).src =  document.getElementById('HidFlop' + (m_nShownBoardCards + 1)).src;
            document.getElementById('SoundPlayer').src="../App_Res/Sounds/Card_sound.wav";
            ++m_nShownBoardCards;
        } 
    }
    function InitClock()
    {
        m_nSeconds = 0;
        m_nMinutes = 2;
        
        document.getElementById('hidMinutes').value = "2";
        document.getElementById('hidSeconds').value = "0";
        document.getElementById('Clock').innerHTML = "2:00";

    }    
    function InitScore()
    {
        m_nScore = 0;
        document.getElementById('hidScore').value = '0';
        document.getElementById('Score').innerHTML = "$0";
    }
    
    function ClockDisplay_Callback()
    {
         if(m_nSeconds == 0)
        {
            if(m_nMinutes != 0)
            {
                m_nMinutes = m_nMinutes - 1;
                m_nSeconds = 59;
            }
            else
            {
                clearInterval(nClockTimer);
                document.PokerGameForm.submit();
                //document.getElementById('PlayerDetails').style.display = 'inline';

                return;
            }
        }
        else
            m_nSeconds = m_nSeconds - 1;            
        
        ClockDisplay();
    }
    function ClockDisplay()
    {
        document.getElementById('hidMinutes').value = m_nMinutes;
        document.getElementById('hidSeconds').value = m_nSeconds;

        if(m_nSeconds < 10)
        {
            document.getElementById('Clock').innerHTML = m_nMinutes + ":0" + m_nSeconds;
        }
            
        else
        {
            document.getElementById('Clock').innerHTML = m_nMinutes + ":" + m_nSeconds;
        }
            
        
        //Time's out.
        if(m_nSeconds == 0 && m_nMinutes == 0)
        {
            document.getElementById('Clock').innerHTML = "0:00";
            document.getElementById('hidMinutes').value = "0";
            document.getElementById('hidSeconds').value = "0";

            document.getElementById('btnDeal').disabled= 'disabled';
            document.getElementById('btnFold').disabled= 'disabled';

            return;
            
        }        
    }
    
    
    function DealTheFlop()
    {
        //Show the flop cards with an interval of 50 milliseconds + play sound
        nTimerId = setInterval(ShowFlopCard,50);
    }
    
    function DealTheTurn()
    {
        document.getElementById('HouseCard4').src = document.getElementById('HidTurn').src;
        document.getElementById('SoundPlayer').src="../App_Res/Sounds/Card_sound.wav";
    }
    function DealTheRiver()
    {      
        document.getElementById('HouseCard5').src = document.getElementById('HidRiver').src;
        document.getElementById('SoundPlayer').src="../App_Res/Sounds/Card_sound.wav";
    }

    
    //----------    Deal the board cards Flop - Turn - River.   ----------
    function btnDeal_onclick() 
    {   
        //Start the test mode...
        if(m_nPlayMode == 2)
        {
           document.getElementById('TopBoard').innerHTML = "";
            m_nPlayMode = 1;
            StartTest(false);
        }
         
        //Deal the flop
        if(m_nShownBoardCards == 0)
        {
            DealTheFlop();
            
            if(m_nPlayMode == 1)
            {
                UpdateUserScore(10);
                document.getElementById('PokerMessageBoard').innerHTML = "Successivo = $10 &nbsp&nbsp|&nbsp&nbsp Fold = $-20 ";
                
                //document.getElementById('PokerMessageBoard').innerHTML = "Click deal [+$10] to see the Turn card, Or Fold [-$20]";
            }
            else if(m_nPlayMode == 0)   //Practice mode
                document.getElementById('PokerMessageBoard').innerHTML = rngOddsMSG[1];// + "<br/> Click Next to see the Turn card";
            
            return;
        }
        
         //Deal the Turn
        else if(m_nShownBoardCards == 3)
        {
            DealTheTurn();
            
            if(m_nPlayMode == 1)
            {
                document.getElementById('PokerMessageBoard').innerHTML = "Successivo=$10 | Fold=$-30 <br/> Vincere=$50 | Perdere =$-50";          
//                document.getElementById('PokerMessageBoard').innerHTML = "Click deal [+$10] to see the River card, Or Fold [-$30]";
                UpdateUserScore(10);
            }
            else if(m_nPlayMode == 0)
                document.getElementById('PokerMessageBoard').innerHTML = rngOddsMSG[2];// + "<br/> Click Next to see the River card";;
                
            m_nShownBoardCards = 4;
        }
        
        //Deal the River    
        else if(m_nShownBoardCards == 4)
        {
            DealTheRiver();
             
            if(navigator.appName == IEXPLORER)
            {
                document.getElementById('btnDeal').value = "Deal";
                //document.getElementById('btnDeal').childNodes[0].Value = "Deal";
            }
            
            //If the user won
            if(rngHandswOdds[(m_nNumOfPlayers * 3)] == 100)
            {
                if(m_nPlayMode == 1)
                    UpdateUserScore(50);
                    
                document.getElementById('SoundPlayer').src="../App_Res/Sounds/chip_collect.wav";
            }
            else if(rngHandswOdds[(m_nNumOfPlayers * 3)] == 0)
            {
            
                if(m_nPlayMode == 1)
                    UpdateUserScore(-50);
            }
            
            m_nShownBoardCards = 5;
           
            document.getElementById('PokerMessageBoard').innerHTML = "";
           
            for(i = 0; i < rngMessage.length ; ++i)
            {
                document.getElementById('PokerMessageBoard').innerHTML += rngMessage[i] + "<br>";
            }
           
            //If this is a test mode show all the cards at the end of each round.
            if(m_nPlayMode == 1)
                ShowCards(true)
                
            //Highlight board cards.
            for(i = 0; i < rngWinHouseCards.length ; ++i)
            {
                document.getElementById('HouseCard' + (parseInt(rngWinHouseCards[i]) + 1)).className = "ChosenCard";
            }
            //Highlight winning player cards.
            for(i = 0; i < rngWinUserCards.length ; i = i + 2)
            {
                document.getElementById('User' + rngWinUserCards[i] + 'Card' + (parseInt(rngWinUserCards[i+1]) + 1)).className = "ChosenCard";
            }
        
        }
        else if(m_nShownBoardCards == 5)
        {
            //history.go(-1); 
            document.PokerGameForm.submit();
            
        }
        
        if(document.getElementById('hidShowOdds').value == '1' || document.getElementById('hidShowTOdds').value == '1')
        {
            ShowOdds((document.getElementById('hidShowOdds').value == '1'),(document.getElementById('hidShowTOdds').value == '1'));
        }
        
    }

    //----------    Show winning Odds for every player.    ---------------
    function ShowOdds(bShowWOdds,bShowTOdds)
    {
        var nFirstIndex;
        var sPlayer,sTPlayer;
        
        //Before the Flop
        if(m_nShownBoardCards == 0)
        {
            nFirstIndex = 0;
        } //Before the River
        else if(m_nShownBoardCards == 3)
        {
            nFirstIndex = m_nNumOfPlayers;
        }//Before the Turn
        else if(m_nShownBoardCards == 4)
        {
            nFirstIndex = m_nNumOfPlayers * 2;
        }
        else if(m_nShownBoardCards == 5)
        {
            nFirstIndex = m_nNumOfPlayers * 3;
        }
        
        for(i=1 ; i <= m_nNumOfPlayers; ++i)
        {   
            sPlayer = 'User' + i + 'Odds';
            if(bShowWOdds == true)
                document.getElementById(sPlayer).style.visibility = "visible";
                
            document.getElementById(sPlayer).style.fontWeight = "Normal";
            
            sTPlayer = 'User' + i + 'TOdds';
            if(bShowTOdds == true)
                document.getElementById(sTPlayer).style.visibility = "visible";
                
            document.getElementById(sTPlayer).style.fontWeight = "Normal";
            
            //Disaply Win / Lose / Tie at the showdown:
            if(m_nShownBoardCards == 5)
            {
                if(rngHandswOdds[nFirstIndex] == 100)
                {
                    document.getElementById(sPlayer).innerHTML = "Win";
                    document.getElementById(sPlayer).style.fontWeight = "bold";
                }
                else if(rngHandswOdds[nFirstIndex] == 0)
                {
                    document.getElementById(sPlayer).innerHTML = "Lose";
                }
                else if(rngHandsTOdds[nFirstIndex] == 100)
                {
                    document.getElementById(sPlayer).innerHTML = "Tie";
                    document.getElementById(sPlayer).style.fontWeight = "bold";
                }
                
                document.getElementById(sTPlayer).innerHTML = "(Tie = " + rngHandstOdds[nFirstIndex] + "%)";
            }
            else
            {
               document.getElementById(sPlayer).innerHTML = rngHandswOdds[nFirstIndex] + "%";//<br/><span style=\"font-size:10px;color:#aaaaaa\">(Tie " + rngHandstOdds[nFirstIndex] + "%)</span>" ;
               document.getElementById(sTPlayer).innerHTML = "(Tie = " + rngHandstOdds[nFirstIndex] + "%)";
            }
                        
            nFirstIndex = parseInt(nFirstIndex) + 1;
        }
    }

    function ChangePlayerNumber(NumOfPlayers)
    {

        if(m_nPlayMode == 0)
        {  
            document.getElementById('hidNumOfPlayers').value = NumOfPlayers;
            //history.go(-1); 
            document.PokerGameForm.submit();
            
        }
    }
    
    function ShowOdds_onclick() 
    {
        if(m_nPlayMode == 1 || m_nPlayMode == 2) return;

        if(document.getElementById('hidShowOdds').value == 0)
        {
            document.getElementById('hidShowOdds').value = "1";
            document.getElementById('OddsDislpay').src = "../App_Res/Pics/Source/Check.jpg";  
                      
            ShowOdds(true,(document.getElementById('hidShowTOdds').value == '1'));

        }
        else
        {
        
            document.getElementById('hidShowOdds').value = "0";
            document.getElementById('OddsDislpay').src = "../App_Res/Pics/Source/UnCheck.jpg";            
                                    
            //Hide Odds.
            for(i=1 ; i <= m_nNumOfPlayers; ++i)
            {  
                document.getElementById('User' + i + 'Odds').style.visibility = "hidden";
            }
        }
    }
    
    function ShowTOdds_onclick() 
    {
        if(m_nPlayMode == 1 || m_nPlayMode == 2) return;

        if(document.getElementById('hidShowTOdds').value == 0)
        {
            document.getElementById('hidShowTOdds').value = "1";
            document.getElementById('OddsTDislpay').src = "../App_Res/Pics/Source/Check.jpg";  
                      
            ShowOdds((document.getElementById('hidShowOdds').value == '1'),true);
        }
        else
        {
            document.getElementById('hidShowTOdds').value = "0";
            document.getElementById('OddsTDislpay').src = "../App_Res/Pics/Source/UnCheck.jpg";            
                                    
            //Hide Odds.
            for(i=1 ; i <= m_nNumOfPlayers; ++i)
            {  
                document.getElementById('User' + i + 'TOdds').style.visibility = "hidden";
            }
        }
    }
    
    function ShowAllCards_onclick() 
    {
        if(m_nPlayMode == 1 || m_nPlayMode == 2) return;
        
        if(document.getElementById('hidShowAllCards').value == 0)
        {
            document.getElementById('hidShowAllCards').value = 1;
            document.getElementById('ShowAllCards').src = "../App_Res/Pics/Source/Check.jpg";  
            ShowCards(true);          
        }
        else
        {
            document.getElementById('hidShowAllCards').value = 0;
            document.getElementById('ShowAllCards').src = "../App_Res/Pics/Source/UnCheck.jpg";
            ShowCards(false);
        }
    }    
    
    function ShowCards(bShow)
    {
        var nPlayer = 2;    //Show players 1,2,3 cards.

        //Show or hide opponents cards.
        for(i=2 ; i <= m_nNumOfPlayers; ++i)
        {
            if(bShow == true)
            {   
                document.getElementById('User' + i + 'Card1').src = '../App_Res/Pics/Cards/' + rngUsersCards[nPlayer];
                 ++nPlayer;
                document.getElementById('User' + i + 'Card2').src = '../App_Res/Pics/Cards/' + rngUsersCards[nPlayer];
                 ++nPlayer;
            }
            else
            {
                 document.getElementById('User' + i + 'Card1').src = '../App_Res/Pics/Cards/Empty.gif';
                 document.getElementById('User' + i + 'Card2').src = '../App_Res/Pics/Cards/Empty.gif'
            }
        }
    }
    
    function StartPractice(bUserClick)
    {          
        document.getElementById('TopBoard').innerHTML = "Fai clic su &#34;Next&#34;, vedi come cambiano le probabilità ...";
        //--------------    Title Variation
        //"It's all about odds"
        //"Click &#34;Next&#34; to start practice, Revel poker odds secrets";
        //Start Practice, explore your winning odds
        //document.getElementById('TopBoard').innerHTML = "Start Practice, see your winning odds changing ...";
        //--------------------------
        
        //Prevent multiple clicks
        if(m_nPlayMode == 0 ) return;
       
        InitClock();
        InitScore();
        clearInterval(nClockTimer);
        
        m_nPlayMode = 0;
        document.getElementById('hidPlayMode').value = 0;

        if(bUserClick)
        {                            
            
            ShowAllCards_onclick();
            ShowOdds_onclick() ;
            //history.go(-1); 
            document.PokerGameForm.submit();
        }
    }
    
    function StartTest(bInit)   
    {        
        document.getElementById('Timer').style.visibility = "visible";                    

        if(bInit == true)
        {
            InitClock(); 
            m_nScore = 0;
            UpdateUserScore(0);
            
           // document.getElementById('hidShowTOdds').value = 0;
            //document.getElementById('hidShowOdds').value = 0;
           // document.getElementById('hidShowAllCards').value = 0;
           // document.getElementById('hidPlayMode').value = 2;
           // document.getElementById('hidNumOfPlayers').value = 4;   
            
            //history.back
            //history.go(-1);   //This causes the all parameters to be initialized like was when the user first enter the site!
            //document.form1.submit();    //This will submit the form every time the user click on Take A Test.
            return;
        }
        
        ClockDisplay();
        
        if(m_nPlayMode == 2)
            return;
            
        //If the game is over
        if(m_nMinutes == 0 && m_nSeconds == 0)
            return;
        
        m_nPlayMode = 1;
        clearInterval(nClockTimer);

        nClockTimer = setInterval(ClockDisplay_Callback,1000);
        document.getElementById('hidPlayMode').value = 1;
        ShowCards(false);
    }
    
    function bookmarksite()
    {
        window.history.back
        // If it's Internet Explorer, use automatic link
        if (document.all)
            window.external.AddFavorite("http://www.webipoker.com", "Play Texas Holdam Poker on the web in WebiPoker.com");
        else if (window.sidebar)
            window.sidebar.addPanel("http://www.webipoker.com", "Play Texas Holdam Poker on the web in WebiPoker.com");
    }
  

    function  ShowBest()
    {
        if(document.getElementById('PLAYERS_LIST').style.visibility == "visible")
            document.getElementById('PLAYERS_LIST').style.visibility = "hidden";
        else
            document.getElementById('PLAYERS_LIST').style.visibility = "visible";
        
    }
    
    function ToggleList(Listid,Arrowid)
    {
        if(document.getElementById(Listid).style.display == "")
        {
            if(Listid == 'HOF_AT_List')
                document.getElementById('hidDisplayAll').value  = "";
            else if(Listid == 'HOFDailyList');
                document.getElementById('hidDisplayDaily').value = "";
                
            document.getElementById(Listid).style.display = "none"; 
            document.getElementById(Arrowid).src= "../App_Res/Pics/Source/arrow_close.gif";
        }
        else
        {
            if(Listid == 'HOF_AT_List')
                document.getElementById('hidDisplayAll').value = "1";
            else if(Listid == 'HOFDailyList');
                document.getElementById('hidDisplayDaily').value = "1";
                
            document.getElementById(Listid).style.display = ""; 
            document.getElementById(Arrowid).src="../App_Res/Pics/Source/arrow_open.gif";
        }
    }
    
    //-----------------------------------------------------------------                  
  /*  function preloadImages() 
    {
    
      var d=document; 
      alert('');
      //Old browser does not have the images collection
      if(d.images)
      { 
        if(!d.p) 
            d.p=new Array();
            
        var i,j=d.p.length;//,a=preloadImages.arguments; 

        for(i=0; i < rngUsersCards.length; i++)
            { 
                
                d.p[j]=new Image; 
                d.p[j++].src='http://webipoker.com/../App_Res/Pics/Cards/' + rngUsersCards[i];
                document.getElementById('Test1').innerHTML += '<br>' + d.p[j-1].src;

            }
        }
    }
    */

