//=============================================================================
// Card and Stack Objects
//=============================================================================
//-----------------------------------------------------------------------------
// Card constructor function.
//-----------------------------------------------------------------------------
function Card(rank, suit) {
  this.rank = rank;
  this.suit = suit;
  this.createNode = cardCreateNode;
}
//-----------------------------------------------------------------------------
// cardCreateNode(): Returns a DIV node which can be used to display the card 
// on a page.
//-----------------------------------------------------------------------------
// Preload graphics.
var cardImg0 = new Image(); cardImg0.src= "cardback.gif";
var cardImg1 = new Image(); cardImg1.src= "jack.gif";
var cardImg2 = new Image(); cardImg2.src= "queen.gif";
var cardImg3 = new Image(); cardImg3.src= "king.gif";
function cardCreateNode() {
  var cardNode, frontNode, indexNode, spotNode, tempNode, textNode;
  var indexStr, spotChar;
  // This is the main node, a DIV tag.
  cardNode = document.createElement("DIV");
  cardNode.className = "card";
  // Build the front of card.
  frontNode = document.createElement("DIV");
  frontNode.className = "front";
  // Get proper character for card suit and change font color if necessary.
  spotChar = "\u00a0";
  switch (this.suit) {
    case "C" :
      spotChar = "\u2663";
      break;
    case "D" :
      frontNode.className += " red";
      spotChar = "\u2666";
      break;
    case "H" :
      frontNode.className += " red";
      spotChar = "\u2665";
      break;
    case "S" :
      spotChar = "\u2660";
      break;
  }
  // Create and add the index (rank) to the upper-left corner of the card.
  indexStr = this.rank;
  if (this.toString() == "")
    indexStr = "\u00a0";
  spotNode = document.createElement("DIV");
  spotNode.className = "index";
  textNode = document.createTextNode(indexStr);
  spotNode.appendChild(textNode);
  spotNode.appendChild(document.createElement("BR"));
  textNode = document.createTextNode(spotChar);
  spotNode.appendChild(textNode);
  frontNode.appendChild(spotNode);
  // Create and add spots based on card rank (Ace thru 10).
  spotNode = document.createElement("DIV");
  textNode = document.createTextNode(spotChar);
  spotNode.appendChild(textNode);
  if (this.rank == "A") {
    spotNode.className = "ace";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
  }
  if (this.rank == "3" || this.rank == "5" || this.rank == "9") {
    spotNode.className = "spotB3";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
  }
  if (this.rank == "2" || this.rank == "3") {
    spotNode.className = "spotB1";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
  }
  if (this.rank == "2" || this.rank == "3") {
    spotNode.className = "spotB5";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
  }
  if (this.rank == "4" || this.rank == "5" || this.rank == "6" ||
      this.rank == "7" || this.rank == "8" || this.rank == "9" ||
      this.rank == "10") {
    spotNode.className = "spotA1";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
    spotNode.className = "spotA5";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
    spotNode.className = "spotC1";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
    spotNode.className = "spotC5";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
  }
  if (this.rank == "6" || this.rank == "7" || this.rank == "8") {
    spotNode.className = "spotA3";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
    spotNode.className = "spotC3";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
  }
  if (this.rank == "7" || this.rank == "8" || this.rank == "10") {
    spotNode.className = "spotB2";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
  }
  if (this.rank == "8" || this.rank == "10") {
    spotNode.className = "spotB4";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
  }
  if (this.rank == "9" || this.rank == "10") {
    spotNode.className = "spotA2";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
    spotNode.className = "spotA4";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
    spotNode.className = "spotC2";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
    spotNode.className = "spotC4";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
  }
  // For face cards (Jack, Queen or King), create and add the proper image.
  tempNode = document.createElement("IMG");
  tempNode.className = "face";
  if (this.rank == "J")
    tempNode.src = "jack.gif";
  if (this.rank == "Q")
    tempNode.src = "queen.gif";
  if (this.rank == "K")
    tempNode.src = "king.gif";
  // For face cards, add suit characters to the upper-left and lower-right
  // corners.
  if (this.rank == "J" || this.rank == "Q" || this.rank == "K") {
    frontNode.appendChild(tempNode);
    spotNode.className = "spotA1";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
    spotNode.className = "spotC5";
    tempNode = spotNode.cloneNode(true);
    frontNode.appendChild(tempNode);
  }
  // Add front node to the card node.
  cardNode.appendChild(frontNode);
  // Return the card node.
  return cardNode;
}
//-----------------------------------------------------------------------------
// Stack constructor function.
//-----------------------------------------------------------------------------
function Stack() {
  // Create an empty array of cards.
  this.cards = new Array();
  this.makeDeck  = stackMakeDeck;
  this.shuffle   = stackShuffle;
  this.deal      = stackDeal;
  this.cardCount = stackCardCount;
}
//-----------------------------------------------------------------------------
// stackMakeDeck(n): Initializes a stack using 'n' packs of cards.
//-----------------------------------------------------------------------------
function stackMakeDeck(n) {
  var ranks = new Array("A", "2", "3", "4", "5", "6", "7", "8", "9",
                        "10", "J", "Q", "K");
  var suits = new Array("C", "D", "H", "S");
  var i, j, k;
  var m;
  m = ranks.length * suits.length;
  // Set array of cards.
  this.cards = new Array(n * m);
  // Fill the array with 'n' packs of cards.
  for (i = 0; i < n; i++)
    for (j = 0; j < suits.length; j++)
      for (k = 0; k < ranks.length; k++)
        this.cards[i * m + j * ranks.length + k] = new Card(ranks[k], suits[j]);
}
//-----------------------------------------------------------------------------
// stackShuffle(n): Shuffles a stack of cards 'n' times. 
//-----------------------------------------------------------------------------
function stackShuffle(n) {
  var i, j, k;
  var temp;
  // Shuffle the stack 'n' times.
  for (i = 0; i < n; i++)
    for (j = 0; j < this.cards.length; j++) {
      k = Math.floor(Math.random() * this.cards.length);
      temp = this.cards[j];
      this.cards[j] = this.cards[k];
      this.cards[k] = temp;
    }
}
//-----------------------------------------------------------------------------
// stackDeal(): Removes the first card in the stack and returns it.
//-----------------------------------------------------------------------------
function stackDeal() {
  if (this.cards.length > 0)
    return this.cards.shift();
  else
    return null;
}
//-----------------------------------------------------------------------------
// stackCardCount(): Returns the number of cards currently in the stack.
//-----------------------------------------------------------------------------
function stackCardCount() {
  return this.cards.length;
}
// ============================================================================
// Acey-Deucey game.
// ============================================================================
// Constants.
var numPacks      =    4;
var numShuffles   =   10;
var maxSplits     =    0;
var minBet        =   50;
var maxBet        =  500;
var initCredit    = 1000;
var initBet       =   50;
var dealTimeDelay =  750;
var firstCard     =  0;
var secondCard    =  0;
var thirdCard     =  0;
// Globals.
var deck;
var burnCard;
var dealer;
var player = new Array(maxSplits + 1);
var curPlayerHand, numPlayerHands;
var credits, defaultBet;
var creditsTextNode, defaultTextNode;
var dealRoundCounter;
// Initialize game on page load.
window.onload = initGame;
function initGame() {
  var i;
  // Locate credits and default bet text nodes on the page.
  creditsTextNode = document.getElementById("credits").firstChild;
  defaultTextNode = document.getElementById("default").firstChild;
  // Initialize player's credits and bet amount.
  credits    = initCredit;
  defaultBet = initBet;
  changeBet(0);
  updateBetDisplay(0);
  // Initialize card deck.
  deck = new Stack();
  newDeck();
  // Create dealer and player hands.
  dealer = new Hand("dealer");
  for (i = 0; i < player.length; i++)
    player[i] = new Hand("player" + i);
}
// ----------------------------------------------------------------------------
// Acey-Deucey hand object.     Hand.prototype.leftIncr  =  2.5               
// ----------------------------------------------------------------------------
Hand.prototype.leftIncr  =  3.75;  // For positioning cards.
Hand.prototype.topIncr   =  0.0;
Hand.prototype.rollEvery =  2;
function Hand(id) {
  this.cards = new Array();
  // Get page elements based on id.
  this.fieldNode     = document.getElementById(id);
  this.cardsNode     = document.getElementById(id + "Cards");
  this.scoreTextNode = document.getElementById(id + "Score").firstChild;
  if (id != "dealer") {
    this.betTextNode    = document.getElementById(id + "Bet").firstChild;
    this.resultTextNode = document.getElementById(id + "Result").firstChild;
  }
  this.reset      = handReset;
  this.addCard    = handAddCard;
  this.removeCard = handRemoveCard;
//  this.getScore   = handGetScore;
  this.getSpread  = handGetSpread;
  this.clearCards = handClearCards;
  // Initialize as an empty hand.
  this.reset();
}
function handReset() {
  // Remove any cards and initialize properties.
  this.clearCards();
  this.cards     = new Array();
//  this.blackjack = false;
  this.split     = false;
  this.doubled   = false;
  this.play = false;
  this.left      = 0;
  this.top       = 0;
//  this.scoreTextNode.nodeValue  = "\u00a0";
  if (this.betTextNode) {
    this.betTextNode.parentNode.className = "textBox dollars";
    this.betTextNode.nodeValue = "\u00a0";
  }
  if (this.resultTextNode)
    this.resultTextNode.nodeValue = "\u00a0";
}
function handAddCard(card, down) {
  var n;
  var node;
  // Add the given card to the hand.
  n = this.cards.length;
  this.cards[n] = card;
  // Create a card node for display, set as face down if requested.
  node = this.cards[n].createNode();
  if (down)
    node.firstChild.style.visibility = "hidden";
  // Add the card display to the associated card area on the page.
if (n == 0) {  node.style.left = this.left + "em"; }
if (n == 1) {  node.style.left = 2 * this.left + "em"; }
if (n == 2) {  node.style.left = this.left - (.5 * this.left) + "em"; }
  node.style.top  = this.top  + "em";
  this.cardsNode.appendChild(node);
  this.left += this.leftIncr;
  if (this.cards.length % this.rollEvery == 0)
    this.top = 0;
  else
    this.top += this.topIncr;
}
function handRemoveCard() {
  var card;
  // Remove the last card in the array and save it.
  card = null;
  if (this.cards.length > 0) {
    card = this.cards.pop();
    // Remove the card node from the display and reset position.
    this.cardsNode.removeChild(this.cardsNode.lastChild);
    this.left -= this.leftIncr;
    this.top  -= this.topIncr;
  }
  // Return the card.
  return card;
}
function handGetScore() {
  var i, total;
  total = 0;
  // Total card values counting Aces as one.
  for (i = 0; i < this.cards.length; i++)
    if (this.cards[i].rank == "A")
      total++;
    else {
      if (this.cards[i].rank == "J" || this.cards[i].rank == "Q" ||
          this.cards[i].rank == "K")
        total += 10;
      else
        total += parseInt(this.cards[i].rank, 10);
    }
  // Change as many ace values to 11 as possible.
  for (i = 0; i < this.cards.length; i++)
    if (this.cards[i].rank == "A" && total <= 11)
      total += 10;
  return total;
}
function handGetSpread() {
  var i, spread, spreadTempA, spreadTempB;
  spread = 0;
  // card values counting Aces as 14.
if (this.cards[0].rank == "2") spreadTempA = 2;
if (this.cards[1].rank == "2") spreadTempB = 2;
if (this.cards[0].rank == "3") spreadTempA = 3;
if (this.cards[1].rank == "3") spreadTempB = 3;
if (this.cards[0].rank == "4") spreadTempA = 4;
if (this.cards[1].rank == "4") spreadTempB = 4;
if (this.cards[0].rank == "5") spreadTempA = 5;
if (this.cards[1].rank == "5") spreadTempB = 5;
if (this.cards[0].rank == "6") spreadTempA = 6;
if (this.cards[1].rank == "6") spreadTempB = 6;
if (this.cards[0].rank == "7") spreadTempA = 7;
if (this.cards[1].rank == "7") spreadTempB = 7;
if (this.cards[0].rank == "8") spreadTempA = 8;
if (this.cards[1].rank == "8") spreadTempB = 8;
if (this.cards[0].rank == "9") spreadTempA = 9;
if (this.cards[1].rank == "9") spreadTempB = 9;
if (this.cards[0].rank == "10") spreadTempA = 10;
if (this.cards[1].rank == "10") spreadTempB = 10;
if (this.cards[0].rank == "J") spreadTempA = 11;
if (this.cards[1].rank == "J") spreadTempB = 11;
if (this.cards[0].rank == "Q") spreadTempA = 12;
if (this.cards[1].rank == "Q") spreadTempB = 12;
if (this.cards[0].rank == "K") spreadTempA = 13;
if (this.cards[1].rank == "K") spreadTempB = 13;
if (this.cards[0].rank == "A") spreadTempA = 14;
if (this.cards[1].rank == "A") spreadTempB = 14;
if (spreadTempA < spreadTempB) { 
spread = (spreadTempB - spreadTempA) - 1;
}
else {
spread = (spreadTempA - spreadTempB) - 1;
}
  firstCard = spreadTempA;
  secondCard = spreadTempB;
  return spread;
}
function handClearCards() {
  // Remove the card nodes in the associated card area.
  while (this.cardsNode.lastChild)
    this.cardsNode.removeChild(this.cardsNode.lastChild);
}
// ----------------------------------------------------------------------------
// Game functions.
// ----------------------------------------------------------------------------
function newDeck() {
  // Create a deck.
  deck.makeDeck(numPacks);
  deck.shuffle(numShuffles);
  // Set the burn card.
  burnCard = Math.round(Math.random() * 26) + 26;
}
function getNextCard() {
  // If there are no cards left, start a new deck.
  if (deck.cardCount() == 0) {
    alert("New Deck");
    newDeck();
  }
  return deck.deal();
}
function startRound() {
  var i;
  // Reset all hands.
  dealer.reset();
  for (i = 0; i < player.length; i++) {
    player[i].reset();
    if (i > 0)
      player[i].fieldNode.style.display = "none";
  }
  // Start with a single player hand.
  curPlayerHand  = 0;
  numPlayerHands = 1;
  // Enable/disable buttons.
  document.forms["controls"].elements["deal"].disabled      = true;
   document.forms["controls"].elements["deal"].style.visibility      = "hidden";
  document.forms["controls"].elements["increase"].disabled  = true;
   document.forms["controls"].elements["increase"].style.visibility      = "hidden";
  document.forms["controls"].elements["decrease"].disabled  = true;
   document.forms["controls"].elements["decrease"].style.visibility      = "hidden";
  DisablePlayButtons();
  // If the burn card was reached, start a new deck.
  if (deck.cardCount() < burnCard) {
    alert("New deck.");
    newDeck();
  }
  // Take the player's bet.
document.getElementById("dealerTalk").innerHTML = 'New hand has started. No more betting allowed.';
  player[0].bet = defaultBet;
  credits -= player[0].bet;
  updateBetDisplay(0);
  // Start dealing the cards.
  dealRoundCounter = 1;
  setTimeout(dealRound, dealTimeDelay);
//  dealRound();
}
function dealRound()
{
  // Deal a card to the player or the dealer based on the counter.
  switch(dealRoundCounter)
  {
    case 1:
      player[0].addCard(getNextCard(), false);
      break;
    case 2:
//      dealer.addCard(getNextCard(), true);
      player[0].addCard(getNextCard(), false);
//      dealer.scoreTextNode.nodeValue = player[0].getSpread();
      break;
    case 3:
      player[0].addCard(getNextCard(), true);
      break;
//    case 4:
//      dealer.addCard(getNextCard(), true);
//      break;
    default:
      // No more cards to deal, play the round.
if (player[0].cards[2].rank == "2") spreadTempA = 2;
if (player[0].cards[2].rank == "3") spreadTempA = 3;
if (player[0].cards[2].rank == "4") spreadTempA = 4;
if (player[0].cards[2].rank == "5") spreadTempA = 5;
if (player[0].cards[2].rank == "6") spreadTempA = 6;
if (player[0].cards[2].rank == "7") spreadTempA = 7;
if (player[0].cards[2].rank == "8") spreadTempA = 8;
if (player[0].cards[2].rank == "9") spreadTempA = 9;
if (player[0].cards[2].rank == "10") spreadTempA = 10;
if (player[0].cards[2].rank == "J") spreadTempA = 11;
if (player[0].cards[2].rank == "Q") spreadTempA = 12;
if (player[0].cards[2].rank == "K") spreadTempA = 13;
if (player[0].cards[2].rank == "A") spreadTempA = 14;
thirdCard = spreadTempA;
dealer.scoreTextNode.nodeValue = player[0].getSpread();
if (dealer.scoreTextNode.nodeValue == "-1") {
    dealer.scoreTextNode.nodeValue = "Pair";
    player[0].resultTextNode.nodeValue = "Odds: 11:1";
document.getElementById("dealerTalk").innerHTML = 'If the down card makes a 3 of a kind, payout will be 11:1.';
      playRound();
    }
    else if (dealer.scoreTextNode.nodeValue == "0") {
    dealer.scoreTextNode.nodeValue = "Push";
    player[0].resultTextNode.nodeValue = "Push";
    DisablePlayButtons();
document.getElementById("dealerTalk").innerHTML = 'No spread, so this hand is a push.';
    endRound();    
    }
    else if (dealer.scoreTextNode.nodeValue == "1") {
    dealer.scoreTextNode.nodeValue = "1";
    player[0].resultTextNode.nodeValue = "Odds: 5:1";
document.getElementById("dealerTalk").innerHTML = 'Only a one card spread, payout will be 5:1 if you win.';
      playRound();
    }
    else if (dealer.scoreTextNode.nodeValue == "2") {
    dealer.scoreTextNode.nodeValue = "2";
    player[0].resultTextNode.nodeValue = "Odds: 4:1";
document.getElementById("dealerTalk").innerHTML = 'Only a two card spread, payout will be 4:1 if you win.';
       playRound();
   }
    else if (dealer.scoreTextNode.nodeValue == "3") {
    dealer.scoreTextNode.nodeValue = "3";
    player[0].resultTextNode.nodeValue = "Odds: 2:1";
document.getElementById("dealerTalk").innerHTML = 'Only a three card spread, payout will be 2:1 if you win.';
       playRound();
   }
    else {
    player[0].resultTextNode.nodeValue = "Odds: 1:1";
document.getElementById("dealerTalk").innerHTML =  dealer.scoreTextNode.nodeValue + ' card spread, payout will be 1:1 if you win.';
      playRound();
    }
// dealer.scoreTextNode.nodeValue = thirdCard;
      return;
      break;
  }
  // Set a timer for the next call.
  dealRoundCounter++;
  setTimeout(dealRound, dealTimeDelay);
}
function playRound() {
  // Enable/disable buttons.
if (firstCard != secondCard) {
  document.forms["controls"].elements["double"].disabled    = false;
  document.forms["controls"].elements["double"].style.visibility      = "visible";
  }
  document.forms["controls"].elements["play"].disabled = false;
   document.forms["controls"].elements["play"].style.visibility      = "visible";
  // Highlight the player's hand.
  addClassName(player[0].fieldNode, "activeField");
}
function playerDouble() {
  // Double the player's bet and deal a single card.
  player[curPlayerHand].bet *= 2;
  credits -= defaultBet;
  updateBetDisplay(curPlayerHand);
  player[curPlayerHand].doubled = true;
  player[curPlayerHand].top = 0;
  player[0].cardsNode.lastChild.lastChild.style.visibility = "visible";
  endRound();
}
function playerplay() {
  // Unhighlight the player's hand.
  removeClassName(player[0].fieldNode, "activeField");
  // Note the play and end the round.
  player[0].cardsNode.lastChild.lastChild.style.visibility = "visible";
  player[0].play = true;
  endRound();
}
function endRound() {
  var i, d, p, tmp;
  // Enable/disable buttons.
  document.forms["controls"].elements["deal"].disabled = false;
   document.forms["controls"].elements["deal"].style.visibility      = "visible";
  EnableBetButtons();
  DisablePlayButtons();
if (((thirdCard < firstCard) && (thirdCard > secondCard)) || ((thirdCard > firstCard) && (thirdCard < secondCard))) {
document.getElementById("dealerTalk").innerHTML = 'Hey! You WON! ... Press Deal to play again.';
    player[0].resultTextNode.nodeValue = "Won";
  if (dealer.scoreTextNode.nodeValue == "1") {
      credits += (6 * player[0].bet); }
  else if (dealer.scoreTextNode.nodeValue == "2") {
      credits += (5 * player[0].bet); }
  else if (dealer.scoreTextNode.nodeValue == "3") {
      credits += (3 * player[0].bet); }
  else  {
      credits += (2 * player[0].bet); }
} else if ((thirdCard == firstCard) && (thirdCard == secondCard)) {
document.getElementById("dealerTalk").innerHTML = 'WOW 3 of a kind! You just got paid 11x your bet.';
credits += 12 * player[0].bet;
    player[0].resultTextNode.nodeValue = "Won";
} else if ((secondCard == firstCard) && (thirdCard != secondCard)) {
document.getElementById("dealerTalk").innerHTML = 'Too bad. Did not make 3 of a kind. This is a push.';
credits += player[0].bet;
    player[0].resultTextNode.nodeValue = "Push";
} else if (((firstCard-secondCard) == 1) || ((secondCard-firstCard) == 1)) {
document.getElementById("dealerTalk").innerHTML = 'That hand was a push. No way to win with a 0 spread.';
credits += player[0].bet;
    player[0].resultTextNode.nodeValue = "Push";
} else {
document.getElementById("dealerTalk").innerHTML = 'Oh well, you can not win them all. Try again.';
    player[0].resultTextNode.nodeValue = "Lost";
}
updateBetDisplay(i);

      if (credits <= 0) {
        var msg="Game Over!" + "\n" + "You lost all your money." + "\n" + "\n" + "Click OK to play again." + "\n" + "\n" + "Click Cancel to return to the" + "\n" + "main myHIP WebApps page." ;
        if(confirm(msg)){
          window.location.href="http://www.myhip.com/reddog/reddog.html";
        }
        else{
          window.location.href="http://www.myhip.com/iphone.html";
        }
      }

}
function updateBetDisplay(n) {
  var s;
  // Display the current bet on the given hand.
  if (player[n]) {
    if (player[n].bet != null)
      s = "Bet: " + formatDollar(player[n].bet);
    else
      s = "\u00a0";
    player[n].betTextNode.nodeValue = s;
  }
  // Display current credits.
  creditsTextNode.nodeValue = "Credits: " + formatDollar(credits);
}
function formatDollar(n) {
  var a, b;
  // Format the given number as a dollar amount for display.
  a = Math.abs(n);
  b = 100 * (a - Math.floor(a));
  if (b < 10)
    b = "0" + b;
  return (n < 0 ? "-" : "" ) + "$" + Math.floor(a);
// + "." + b;
}
function changeBet(n) {
  // Increase or decrease the default bet.
  defaultBet += n;
  defaultBet = Math.max(Math.min(defaultBet, maxBet), minBet);
  defaultTextNode.nodeValue = "Default Bet: " + formatDollar(defaultBet);
  // Reset the increase/decrease buttons.
  EnableBetButtons();
}
function EnableBetButtons() {
  // Enable the increase and decrease bet buttons provided the current bet
  // amount is within the allowed min/max value.
if (defaultBet >= maxBet)   
    document.forms["controls"].elements["increase"].style.visibility      = "hidden";
    else 
    document.forms["controls"].elements["increase"].style.visibility      = "visible";
if (defaultBet <= minBet)
   document.forms["controls"].elements["decrease"].style.visibility      = "hidden";
   else
   document.forms["controls"].elements["decrease"].style.visibility      = "visible";
  document.forms["controls"].elements["increase"].disabled = (defaultBet >= maxBet);
  document.forms["controls"].elements["decrease"].disabled = (defaultBet <= minBet);
}
function DisablePlayButtons() {
  // Disable all the buttons used for playing a hand.
   document.forms["controls"].elements["double"].style.visibility      = "hidden";
   document.forms["controls"].elements["play"].style.visibility      = "hidden";
  document.forms["controls"].elements["double"].disabled    = true;
  document.forms["controls"].elements["play"].disabled = true;
}
function toggleRules() {
  var el;
  // Display or hide the game rules text.
  el = document.getElementById("rulesBox");
  if (el.style.display == "") {
    el.style.display = "block";
    document.forms["controls"].elements["rules"].value = "Hide Rules";
  }
  else {
    el.style.display = "";
    document.forms["controls"].elements["rules"].value = "Show Rules";
  }
}
function addClassName(el, name)
{
	// Remove the class name if it already exists in the element's class name
	// list.
	removeClassName(el, name);
	// Add the class name to the element's current list of class names.
	if (el.className.length > 0)
		name = " " + name;
	el.className += name;
}
function removeClassName(el, name)
{
	// If the element has no class names, exit.
	if (el.className == null)
		return;
	// Rebuild the list of class names on the element but exclude the specified
	// class name.
	var newList = new Array();
	var curList = el.className.split(" ");
	for (var i = 0; i < curList.length; i++)
		if (curList[i] != name)
			newList.push(curList[i]);
	el.className = newList.join(" ");
}

