// Javascript class: Final Project - Blackjack
// File:  Blackjackv3.js  -- This is Version 3 of the Blackjack.  
// Author: William Howard
// 
// History:  
//   Version 1:  Basic functionality with no graphics (just text).
//   Version 2:  Now there are graphics and a hint section.
//   Version 3:  Now I'm cleaning up the code.

var deck = new Array();
var masterCards = new Array();
var currentMasterCard = 0;
var currentDealerCard = 0;
var currentPlayerCard = 0;
var dealerhand = new Array();
var playerhand = new Array();

var showDealer = new String;
var showPlayer = new String;

var playerCash = 0;
var currentBet = 0;

var bufferSpace = "&nbsp;<br />&nbsp;<br />";
var standardHint = "<strong>Payouts:</strong> Tie = Your $ back, Win = Double your $ back, Blackjack = Tripple your $ back.";
var currentHint = "";
var showHint = true;



// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: buildDeck();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function buildDeck() {
  //alert("building the deck");
  for(i=0;i<=51;i++){
    // Now, we need to find a random card to put in our new deck.
    var intRandomCard = pickAcard();
    deck[i] = intRandomCard;
  }
}
 
// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: buildMaster();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function buildMaster() {
  var suit = 0;
  var whichCard = 0;
  var cardValue = 0;
  var whichSuit = new Array;
  for(i=0;i<52;i++){
    whichCard = i % 13;
    if(whichCard == 0) {
      // New Suit:
      suit++;
    }
    switch(suit){
      case 1:  // hearts
	    whichSuit[1] = "hearts";
        break;
      case 2:  // clubs
	    whichSuit[2] = "clubs";
        break;
      case 3:  // diamond
	    whichSuit[3] = "diamonds";
        break;
      case 4:  //spades
	    whichSuit[4] = "spades";
        break;
    }
    switch(whichCard) {
      case 0:  // Ace
        cardValue = 1;
		fileName = "a_"+whichSuit[suit];
        break;
      case 1:  // 2
        cardValue = 2;
		fileName = "2_"+whichSuit[suit];
        break;
      case 2:  // 3
        cardValue = 3;
		fileName = "3_"+whichSuit[suit];
        break;
      case 3:  // 4
        cardValue = 4;
		fileName = "4_"+whichSuit[suit];
        break;
      case 4:  // 5
        cardValue = 5;
		fileName = "5_"+whichSuit[suit];
        break;
      case 5:  // 6
        cardValue = 6;
		fileName = "6_"+whichSuit[suit];
        break;
      case 6:  // 7
        cardValue = 7;
		fileName = "7_"+whichSuit[suit];
        break;
      case 7:  // 8
        cardValue = 8;
		fileName = "8_"+whichSuit[suit];
        break;
      case 8:  // 9
        cardValue = 9;
		fileName = "9_"+whichSuit[suit];
        break;
      case 9:  // 10
        cardValue = 10;
		fileName = "10_"+whichSuit[suit];
        break;
      case 10:  // Jack
        cardValue = 10;
		fileName = "j_"+whichSuit[suit];
        break;
      case 11:  // Queen
        cardValue = 10;
		fileName = "q_"+whichSuit[suit];
        break;
      case 12:  // King
        cardValue = 10;
		fileName = "k_"+whichSuit[suit];
        break;
    }
    masterCards[i] = new Array();
    masterCards[i][0] = suit;
    masterCards[i][1] = whichCard;
    masterCards[i][2] = cardValue;   
    masterCards[i][3] = fileName;	
  }
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: check4Bust();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function check4Bust(whichHand) {
 var personBusted = false;
  // check to see if the dealer/player busted... if so then react, otherwise return a false.
  switch(whichHand){
    case "dealer":
      if (returnCardsValue("dealer") >21) {
        personBusted = true;
      }
      break;
      
    case "player":
      if(returnCardsValue("player") >21) {
        personBusted = true;
      }
      break;
  }
  
  return personBusted;
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: checkFor5Card();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function checkFor5Card(fromWho){
  var got5Card = false;
  var cardsValue = 0;
  switch(fromWho){
    case "dealer":
      howManyCards = dealerhand.length;
      cardsValue = returnCardsValue("dealer");
      break;
    case "player":
      howManyCards = playerhand.length;
      cardsValue = returnCardsValue("player")
      break;
    default:
      alert("Broken: checkForBlackjack didn't know who to check for");
      break;
  }
  //
  if(howManyCards >= 5){
    if(cardsValue <= 21) {
      // Ok.  If the person has 5 cards and hasn't broken 21 then they win.
      got5Card = true;
    }
  }
  
  return got5Card;
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: checkForBlackjack();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function checkForBlackjack(fromWho){
  var gotBlackjack = false;
  var gotAce = false;
  var gotTen = false;
  var howManyCards = 0;
  var cardValues = 0;
  switch(fromWho){
    case "dealer":
      howManyCards = dealerhand.length;
      break;
    case "player":
      howManyCards = playerhand.length;
      break;
    default:
      alert("Broken: checkForBlackjack didn't know who to check for");
      break;
  }
  //
  for(i=0;i<howManyCards;i++) {
    switch(fromWho){
      case "dealer":
        whichCard = dealerhand[i];
        break;
      case "player":
        whichCard = playerhand[i];
        break;
    }
    // determine the value of the card.
    if(masterCards[whichCard][2] == 1){
      // Ace.
      gotAce = true;
    }
    if(masterCards[whichCard][2] == 10){
      // One of the Ten value cards.
      gotTen = true;
    }
    cardValues += masterCards[whichCard][2];
  }
  // Now check.
  if (cardValues == 11) {
    // Check to see if we have an Ace and a Ten.
    if(gotAce && gotTen){
      gotBlackjack = true;
    }
  }
  return gotBlackjack;
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: giveAcardTo();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function giveAcardTo(toWho){
   // Based on who is given the card - that will be toWho.
   switch(toWho) {
     case "dealer":
       dealerhand[currentDealerCard] = deck[currentMasterCard];
       currentDealerCard++;
       break;
     case "player":
       playerhand[currentPlayerCard] = deck[currentMasterCard];
       currentPlayerCard++;
       break;
     default:
       alert("Error with giveAcardTo function: I'm not sure who to give the card to.");
       break;
   }
   // Give the card to someone, then add on to the card counter.
   currentMasterCard++;
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: handleDealer();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function handleDealer (){
  // Ok.  Check to see if the player is below 16.  If so then give a card and loop.  
  // Check to see if the player busted, if not then continue to give the dealer cards.
  if(check4Bust("player")) {
    // Nothing.
  } else {
    var keepInLoop = true;
    while (keepInLoop) {
      if(returnCardsValue("dealer") <= 16) {
        // Give the dealer another card.
        giveAcardTo("dealer");
        updateCardArea("dealer");
      } else {
        keepInLoop = false;
      }
      // check to see if we have 5 cards.
      if(checkFor5Card("dealer")) {
        keepInLoop = false;
      }
    }
  }
  updateCardArea("dealer");
  
  if(checkFor5Card("dealer") && returnCardsValue("dealer") <= 21) {
    // Ok.  The dealer got 5 cards and is 21 or lower.  So the dealer wins.
    payDealer("<strong>Dealer got 5 cards without busting.</strong>  The dealer wins.");
  } else {
    if(check4Bust("dealer")) {
      // Dealer busted.
	    payPlayer(2,"Dealer Busted");
    } else {
      // Otherwise then check to see if the dealer went above the player (i.e, won).
      if(returnCardsValue("dealer") > returnCardsValue("player")) {
        payDealer();
      } else {
        // Check to see if the player won.
        if(returnCardsValue("player") > 21) {
          // Player busted.  Dealer won.
          payDealer();
        } else {
          if(returnCardsValue("player") > returnCardsValue("dealer")){
            //alert("player won");
		        payPlayer(2,"You Won!!!");
          } else {
            // If not then tie.
		        payPlayer(1,"You Tied with Dealer");
          }
        }
      }
    }
  }
  // Alert the player on the overall status of the hand.
  // Unhide/show the "new hand/deal option, or quit option".
  
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: hitAction();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function hitAction() {
  //alert("so you want to hit?");
  giveAcardTo("player");
  updateCardArea("player");
  // check to see if the player busted.
  if(check4Bust("player")){
    // Player busted.  Go ahead and handle dealer.
    //alert("you busted");
	  // turn off the hit, stand, and deal buttons until dealer finished.
    document.playeractionform.hitbutton.disabled = true;
    document.playeractionform.standbutton.disabled = true;
    document.playeractionform.dealbutton.disabled = false;
    payDealer("<strong>You busted!</strong>");
    //handleDealer();
  }
  if(returnCardsValue("player")==21) {
    // Player won.
	payPlayer(2,"You Won!!!");
    document.playeractionform.hitbutton.disabled = true;
    document.playeractionform.standbutton.disabled = true;
    document.playeractionform.dealbutton.disabled = false;
    //startingScreen();
  } else {
  if(checkFor5Card("player") && !check4Bust("player")){
    // Ok.  Player has 5 cards and hasn't busted.
    payPlayer(2,"You got a 5 cards without going to 21.  You won.");
    document.playeractionform.hitbutton.disabled = true;
    document.playeractionform.standbutton.disabled = true;
    document.playeractionform.dealbutton.disabled = false;
    //startingScreen();
  }
  }
  
  // Let's give them a hint.
  // Determine what the dealer is showing...  Don't use returncardsValue("dealer") since that will show the hidden card.
  var whatDoesDealerHave = returnFirstValue();
  
  // Now determine what value's the player has.
  var whatDoesPlayerHave = returnCardsValue("player");
  
  // Now give them the right hint.
  if(whatDoesPlayerHave <= 8) {
    // Normally the player will hit on anything from 8 and below.
	updateHintArea("You should take a card.");
  } else {
    if(whatDoesPlayerHave < 12) {
	  // Normally you should hit here also.
	  updateHintArea("Recommend that you take a card.");
	} else {
	  // Player has a 12 or higher.  Now things get tricky.
	  if(whatDoesPlayerHave == 12) {
	    if(whatDoesDealerHave == 2 || whatDoesDealerHave == 3) {
		  updateHintArea("You should normally hit if you have a 12 and the dealer shows a 2 or 3.");
		} else {
		  if(whatDoesDealerHave == 4 || whatDoesDealerHave == 5 || whatDoesDealerHave == 6){
		    updateHintArea("You should normally stand if you have a 12 and the dealer shows 4-6.");
		  } else {
		    updateHintArea("You should normally hit if you have a 12 and the dealer shows a 7 or higher.");
		  }
		}
	  } else {
	    if(whatDoesPlayerHave == 13 || whatDoesPlayerHave == 14 || whatDoesPlayerHave == 15 || whatDoesPlayerHave == 16) {
		  if(whatDoesDealerHave >= 7) {
		    updateHintArea("You should normally hit if you have "+whatDoesPlayerHave+" and the dealer shows a 7 or higher.");
		  } else {
		    updateHintArea("You should normally stand if you have "+whatDoesPlayerHave+" and the dealer shows something between 2 and 6.");
		  }
		} else {
          updateHintArea("You should normally stand if you have a 17 or higher.");		
		}
	  }
	}
  }
  
  return true;
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: newDealAction();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function newDealAction(){
  // rebuild the deck 
  // turn on the hit, stand, and deal buttons until dealer finished.
  document.playeractionform.hitbutton.disabled = false;
  document.playeractionform.standbutton.disabled = false;
  document.playeractionform.dealbutton.disabled = true;
  // turn off the bet button.
  document.betform.setbet.disabled = true;

  // clear out debug area:
  document.getElementById("debug").innerHTML = "";
  
  rebuildAll();
  currentMasterCard = 0;
  currentDealerCard = 0;
  currentPlayerCard = 0;

  // Take the $ away from the person's bank account.
  if (placeAbet()) {

  // get each player 2 starting off cards.
  giveAcardTo("dealer");
  giveAcardTo("dealer");
  giveAcardTo("player");
  giveAcardTo("player");
  //
  // update the display
  updateCardArea("dealer",false);
  updateCardArea("player");
  //
  
  // Check to see if either the player or the dealer got a blackjack.
  if(checkForBlackjack("dealer")) {
	  // Dealer has Blackjack right off the bat.
  	updateActionsArea(true,"Sorry, the dealer got Blackjack...");
    // Now do whatever I need to do when the dealer wins.
	  updateCardArea("dealer");
	  payDealer("Dealer got Blackjack");
  } else {
    if(checkForBlackjack("player")) {
	    // Player has Blackjack.
	    updateActionsArea(true,"You got a Blackjack.  Congrats...");
	    payPlayer(2,"You got a Blackjack!!!!");
	    //
	    // Now do whatever we need to do when the player wins....
    } else {
	    updateActionsArea(true,"");
    }
  }

  // Let's give them a hint.
  // Determine what the dealer is showing...  Don't use returncardsValue("dealer") since that will show the hidden card.
  var whatDoesDealerHave = returnFirstValue();
  
  // Now determine what value's the player has.
  var whatDoesPlayerHave = returnCardsValue("player");
  
  // Now give them the right hint.
  /*
  5 - 8  	Anything  	HIT. You should always hit on 8 or less.
9 	2 	HIT. Hit on 9 when the dealer shows a 2.
9 	3 - 6 	DOUBLE. Double on 9 when the dealer shows 3 - 6.
9 	7 - Ace 	HIT. Hit on 9 when the dealer shows 7 or higher.
10 	2 - 9 	DOUBLE. Double on 10 when the dealer shows 2 - 9.
10 	10, A 	HIT. Hit on 10 when the dealer has 10 or higher.
11 	2 - 10 	DOUBLE. Double on 11 when the dealer has 2 - 10.
11 	Ace 	HIT. Hit on 11 when the dealer has an Ace.
12 	2, 3 	HIT. Hit on 12 when the dealer has a 2 or 3.
12 	4 - 6 	STAND. Stand on 12 when the dealer has 4, 5, or 6.
12 	7 - Ace 	HIT. Hit on 12 when the dealer has 7 or higher.
13 	2 - 6 	STAND. Stand on 13 when the dealer has 2 - 6.
13 	7 - Ace 	HIT. Hit on 13 when the dealer has 7 or higher.
14 	2 - 6 	STAND. Stand on 14 when the dealer has 2 - 6.
14 	7 - Ace 	HIT. Hit on 14 when the dealer has 7 or higher.
15 	2 - 6 	STAND. Stand on 15 when the dealer has 2 - 6.
15 	7 - Ace 	HIT. Hit on 15 when the dealer has 7 or higher.
16 	2 - 6 	STAND. Stand on 16 when the dealer has 2 - 6.
16 	7 - Ace 	HIT. Hit on 16 when the dealer has 7 or higher.
17 - 21 	Anything 	STAND. You should always stand on hard 17 or higher.
Ace, 2 	2 - 4 	HIT. Hit on soft 13 when the dealer has 2 - 4.
Ace, 2 	5, 6 	DOUBLE. Double on soft 13 when the dealer has 5 or 6.
Ace, 2 	7 - Ace 	HIT. Hit on soft 13 when the dealer has 7 or higher.
Ace, 3 	2 - 4 	HIT. Hit on soft 14 when the dealer has 2 - 4.
Ace, 3 	5, 6 	DOUBLE. Double on soft 14 when the dealer has 5 or 6.
Ace, 3 	7 - Ace 	HIT. Hit on soft 14 when the dealer has 7 or higher.
Ace, 4 	2 - 3 	HIT. Hit on soft 15 when the dealer has 2 or 3.
Ace, 4 	4 - 6 	DOUBLE. Double on soft 15 when the dealer has 4, 5, or 6.
Ace, 4 	7 - Ace 	HIT. Hit on soft 15 when the dealer has 7 or higher.
Ace, 5 	2 - 3 	HIT. Hit on soft 16 when the dealer has 2 or 3.
Ace, 5 	4 - 6 	DOUBLE. Double on soft 16 when the dealer has 4, 5, or 6.
Ace, 5 	7 - Ace 	HIT. Hit on soft 16 when the dealer has 7 or higher.
Ace, 6 	2 	HIT. Hit on soft 17 when the dealer has a 2.
Ace, 6 	3 - 6 	DOUBLE. Double on soft 17 when the dealer has 3 - 6.
Ace, 6 	7 - Ace 	HIT. Hit on soft 17 when the dealer has 7 or higher.
Ace, 7 	2 	STAND. Stand on soft 18 when the dealer has a 2.
Ace, 7 	3 - 6 	DOUBLE. Double on soft 18 when the dealer has 3 - 6.
Ace, 7 	7, 8 	STAND. Stand on soft 18 when the dealer has 7 or 8.
Ace, 7 	9 - Ace 	HIT. Hit on soft 18 when the dealer has 9 or higher.
Ace, 8 	Anything 	STAND. You should always stand on soft 19.
Ace, 9 	Anything 	STAND. You should always stand on soft 20.
Ace, 10 	Anything 	STAND. You've got Blackjack!
2, 2 or 3, 3 	2 or 3 	HIT. Only split 2's or 3's if the dealer has a 4 - 7.
2, 2 or 3, 3 	4 - 7 	SPLIT. Split 2's or 3's if the dealer has 4 - 7.
2, 2 or 3, 3 	8 - Ace 	HIT. Only split 2's or 3's if the dealer has a 4 - 7.
4, 4 	2 - 4, 7 or higher 	HIT. Only split 4's if the dealer has 5 or 6.
4, 4 	5 - 6 	SPLIT. Split 4's if the dealer has 5 or 6.
5, 5 	2 - 9 	DOUBLE. Double on 5's if the dealer has 2 - 9.
5, 5 	10 - Ace 	HIT. You should never split 5's.
6, 6 	2 - 6 	SPLIT. Split 6's if the dealer has 2 - 6.
6, 6 	7 - Ace 	HIT. Don't split 6's if the dealer has 7 or higher.
7, 7 	2 - 7 	SPLIT. Split 7's if the dealer has 2 - 7.
7, 7 	8 - Ace 	HIT. Don't split 7's if the dealer has 8 or higher.
A, A or 8, 8 	Anything 	SPLIT. You should always split Aces and 8's.
9,9 	2 - 6 	SPLIT. Split 9's if the dealer has 2 - 6.
9,9 	7 	STAND. Don't split 9's if the dealer has 7.
9,9 	8, 9 	SPLIT. Split 9's if the dealer has 8 or 9.
9, 9 	10 - Ace 	STAND. Don't split 9's if the dealer has 10 or higher.
10,10 	Anything 	STAND. You should never split 10's.
Any 4 cards with
11 or less or a
soft hand 	Anything 	HIT. With 4 cards worth 11 or less, try for 5 Card Charlie.
     */
  if(whatDoesPlayerHave <= 8) {
    // Normally the player will hit on anything from 8 and below.
	updateHintArea("You should take a card.");
  } else {
    if(whatDoesPlayerHave < 12) {
	  // Normally you should hit here also.
	  updateHintArea("Recommend that you take a card.");
	} else {
	  // Player has a 12 or higher.  Now things get tricky.
	  if(whatDoesPlayerHave == 12) {
	    if(whatDoesDealerHave == 2 || whatDoesDealerHave == 3) {
		  updateHintArea("You should normally hit if you have a 12 and the dealer shows a 2 or 3.");
		} else {
		  if(whatDoesDealerHave == 4 || whatDoesDealerHave == 5 || whatDoesDealerHave == 6){
		    updateHintArea("You should normally stand if you have a 12 and the dealer shows 4-6.");
		  } else {
		    updateHintArea("You should normally hit if you have a 12 and the dealer shows a 7 or higher.");
		  }
		}
	  } else {
	    if(whatDoesPlayerHave == 13 || whatDoesPlayerHave == 14 || whatDoesPlayerHave == 15 || whatDoesPlayerHave == 16) {
		  if(whatDoesDealerHave >= 7) {
		    updateHintArea("You should normally hit if you have "+whatDoesPlayerHave+" and the dealer shows a 7 or higher.");
		  } else {
		    updateHintArea("You should normally stand if you have "+whatDoesPlayerHave+" and the dealer shows something between 2 and 6.");
		  }
		} else {
          updateHintArea("You should normally stand if you have a 17 or higher.");		
		}
	  }
	}
  }
  
  // Wait for the player.
  } 
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: newHand();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function newHand() {
  // Rebuild the deck.
  // Give each player two cards.
  // Update the card areas.
  // check for blackjacks.
  // If still going then just wait for a button press.
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: payDealer();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function payDealer(WhatToSay) {
  if(WhatToSay == null) {  // nothing was passed, then just give it the default.
    WhatToSay = "<strong>Dealer Won.</strong>  You areally aren't good at this game, huh.";
  }
  document.playeractionform.dealbutton.disabled = false;
  document.playeractionform.hitbutton.disabled = true;
  document.playeractionform.standbutton.disabled = true;
  updateActionsArea(true,WhatToSay);
  updateHintArea(standardHint);
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: payPlayer();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function payPlayer(byWhat,WhatToSay) {
  document.playeractionform.dealbutton.disabled = false;
  document.playeractionform.hitbutton.disabled = true;
  document.playeractionform.standbutton.disabled = true;
  // change the deal button text to "keep same bet and deal?"
  // ....  come back here ...
	
  // Give the person their $$$
  playerCash += currentBet * byWhat;
  walletUpdate();
  showBetArea();
  updateActionsArea(true,"<strong>"+WhatToSay+"</strong>  Congrats...");
  updateHintArea(standardHint);
	
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: pickAcard();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function pickAcard() {
  var intCardPicked = 0;
  var GoodCardFound = false;
  
  while(!GoodCardFound){
    var intRandomCard = Math.round(Math.random()*51);
    GoodCardFound = true;
    // Loop through the current deck.
    for(var intWhichCard in deck){
      if(deck[intWhichCard] == intRandomCard){
        // Opps.  Already have picked that card.
        GoodCardFound = false;
      }
    }
    // Now, make sure we didn't already pick that card.
    // If we are ok, then continue, otherwise pick again.
    if(intRandomCard < 0 || intRandomCard > 51) {
      GoodCardFound = false;
    }
  }
  
  return intRandomCard;
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: placeAbet();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function placeAbet(){
  var retVal = true;
  // Take the current bet amount out of the bank.
  playerCash -= currentBet;

  // Check to see if the player has no funds.  If the current bank is below 0 then alert the user.
  if(playerCash < 0){
    alert("You have no money left to bet.");
    playerCash += currentBet;  // put the $ back.  No bet.
    retVal = false;
  } else {
    // update the cookie.
    walletUpdate();
  }
  updateCardArea("none");
  
  return retVal;
}


// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: rebuildDeck();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function rebuildAll() {

  while(deck.length > 0 ) {
    deck.pop();
  }
  
  // Rebuild the deck.
  buildDeck();
  
  // Empty the player hand.
  while(playerhand.length > 0 ) {
    playerhand.pop();
  }
  
  // Empty the dealer hand.
  while(dealerhand.length > 0 ) {
    dealerhand.pop();
  }
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: returnCardsValue();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function returnCardsValue(fromWho) {
  var retVal = 0;
  var cardValues = 0;
  var whichCard;
  var howManyCards = 0;
  var aceFound = 0;
  switch(fromWho){
    case "dealer":
      howManyCards = dealerhand.length;
      break;
    case "player":
      howManyCards = playerhand.length;
      break;
  }
  //
  for(i=0;i<howManyCards;i++) {
    switch(fromWho){
      case "dealer":
        whichCard = dealerhand[i];
        break;
      case "player":
        whichCard = playerhand[i];
        break;
    }
    // determine the value of the card.
    // If the card is an Ace then check to see if we are going to go over and if not then we can have it as 11.
    if (masterCards[whichCard][2] == 1) {
    //alert("ace found");
      aceFound++;
    }
    cardValues += masterCards[whichCard][2];
  }
  // Ace found?
  if(aceFound > 0 ) {
    if(cardValues + 10 > 21) {
	  // Do nothing.
	} else {
	  cardValues = cardValues + 10;
	}
  }
  return cardValues;
}
 

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: returnFirstValue();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function returnFirstValue() {
  var cardValues = 0;
  var whichCard;

  // and we are only showing the first card.
  whichCard = dealerhand[0];

  cardValues = masterCards[whichCard][2];
  
  return cardValues;
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: setBet();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function setBet(){
  if(document.betform.betamount.value > 0){
    currentBet = document.betform.betamount.value;
    updateCardArea("none");
  }
  // Turn the "deal" button on.
  document.playeractionform.dealbutton.disabled = false;
  var dummyText = document.getElementById("notices").innerHTML.replace("Don't forget to bet.  Then you can select the 'DEAL' button.","Now press the 'DEAL' button.");
  document.getElementById("notices").innerHTML = dummyText;

  return true;
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: showAcard();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function showAcard(toWho,whichCard) {

  var strCardDisplay = "";
  
  // determine the set number based on theme.
  var whichSet = "set01_";
  var cardMidId = "";
  var cardSuit = "";
  var retVal = "";
  var imgSrc = new String; 
  switch(toWho) {
    case "dealer":
      whichCard = dealerhand[whichCard];
      break;
    case "player":
      whichCard = playerhand[whichCard];
      break;
  }
  //var whichCard = deck[currentCard];
  switch(masterCards[whichCard][1]){
      case 0:  // Ace
        strCardDisplay += "Ace";
		cardMidId = "a_";
        break;
      case 1:  // 2
        strCardDisplay += "Two";
		cardMidId = "2_";
        break;
      case 2:  // 3
        strCardDisplay += "Three";
		cardMidId = "3_";
        break;
      case 3:  // 4
        strCardDisplay += "Four";
		cardMidId = "4_";
        break;
      case 4:  // 5
        strCardDisplay += "Five";
		cardMidId = "5_";
        break;
      case 5:  // 6
        strCardDisplay += "Six";
		cardMidId = "6_";
        break;
      case 6:  // 7
        strCardDisplay += "Seven";
		cardMidId = "7_";
        break;
      case 7:  // 8
        strCardDisplay += "Eight";
		cardMidId = "8_";
        break;
      case 8:  // 9
        strCardDisplay += "Nine";
		cardMidId = "9_";
        break;
      case 9:  // 10
        strCardDisplay += "Ten";
		cardMidId = "10_";
        break;
      case 10:  // Jack
        strCardDisplay += "Jack";
		cardMidId = "j_";
        break;
      case 11:  // Queen
        strCardDisplay += "Queen";
		cardMidId = "q_";
        break;
      case 12:  // King
        strCardDisplay += "King";
		cardMidId = "k_";
        break;
    }
  switch(masterCards[whichCard][0]){
      case 1:  // hearts
        strCardDisplay += " of Hearts";
		cardSuit = "hearts";
        break;
      case 2:  // clubs
        strCardDisplay += " of Clubs";
		cardSuit = "clubs";
        break;
      case 3:  // diamond
        strCardDisplay += " of Diamonds";
		cardSuit = "diamonds";
        break;
      case 4:  //spades
        strCardDisplay += " of Spades";
		cardSuit = "spades";
        break;
    }

  imgSrc = whichSet + cardMidId + cardSuit + ".png";
  retVal = "<img src=\""+imgSrc+"\" alt=\""+strCardDisplay+"\">";  
  //return strCardDisplay;
  return retVal;
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: showBlankCard();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function showBlankCard() {
  var whichSet = "set01_";
  var retVal = "";
  var imgSrc = new String; 
 
  strCardDisplay = "back of card";
  imgSrc = whichSet + "cardback.png";
  retVal = "<img src=\""+imgSrc+"\" alt=\""+strCardDisplay+"\">";  

  return retVal;
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: showBetArea();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function showBetArea(){
  // shouldn't the form deafult to whatever the current bet is set at?  And maybe a "choose one" option?
  //
  
  var bankForm = "<form name=\"betform\" action=\"#\">";
  bankForm += "<select name=\"betamount\">";
  for(var i=5;i<=25;i=i+5){
    if(i == currentBet){
      bankForm += "<option selected=\"selected\" value=\"" + i + "\">"+i+"</option>";   
    } else {
      bankForm += "<option value=\"" + i + "\">"+i+"</option>";   
    }
  }
  bankForm += "</select><input type=\"button\" value=\"bet\" name=\"setbet\" onclick=\"setBet();\" /> </form>";
  var bufferSpace = "&nbsp;<br />&nbsp;<br />";
  var betPlacedText = "";
  // No matter who you are updating go ahead and update the wallet section.
  if(currentBet < 1) {
    betPlacedText = "You have not placed a bet yet.<br />";
  }  else {
    betPlacedText = "Your bet: $"+currentBet+".00 dollars.<br />";
  }
  
  document.getElementById("money").innerHTML = "Your bank account is: " + walletRead()+"<br />"+bufferSpace+betPlacedText+bankForm;

}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: showHideHints();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function showHideHints() {
  // Toggle showHint.
  if (showHint) {
    showHint = false;
  } else {
    showHint = true;
  }
  // show or hid the hint.
  updateHintArea(currentHint);
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: standAction();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function standAction() {
  // check to see if I got blackjack.  If I did then I won, otherwise let the dealer play.
  if(checkForBlackjack("player")) {
    // Player won with blackjack.
	// Don't forget to pay the guy!!!!
    payPlayer(2,"You got a Blackjack!");	
	startingScreen();
  } else {
    //alert("so you want to stand?");
    document.playeractionform.hitbutton.disabled = true;
    document.playeractionform.standbutton.disabled = true;
    document.playeractionform.dealbutton.disabled = true;
    // handle dealer.
    handleDealer();
  }
  return true;
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: startingScreen();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function startingScreen() {
  // Setup the dealer area.
  document.getElementById("dealer").innerHTML = bufferSpace + bufferSpace + bufferSpace;
  
  // Setup the notice area.
  document.getElementById("notices").innerHTML = "Don't forget to bet.  Then you can select the 'DEAL' button." + bufferSpace;
  
  // Setup the player area.
  document.getElementById("player").innerHTML = bufferSpace + bufferSpace + bufferSpace;
  
  currentHint = standardHint;
  updateHintArea(currentHint);
  
  // Setup the bet area.
  showBetArea();

  // Also turn off any non-active buttons.
  document.playeractionform.hitbutton.disabled = true;
  document.playeractionform.standbutton.disabled = true;
  // For now I'm going to turn off the deal button until the person sets their bet.
  document.playeractionform.dealbutton.disabled = true;

}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: startup();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function startup(){
  // check to see if the person has a cookie.  If not then give them an initial bankroll of 500 dollars.
  if(walletRead() < 1){
    playerCash = 500;
    walletUpdate();
  }
  
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: updateActionsArea();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function updateActionsArea(rebuildyn,newText) {
  if(rebuildyn) {
    document.getElementById("notices").innerHTML = newText + bufferSpace+ bufferSpace +  bufferSpace;
  } else {
    var tempString = document.getElementById("notices").innerHTML.replace(bufferSpace,"");
    document.getElementById("notices").innerHTML = tempString + "<br />" + newText + bufferSpace+ bufferSpace +  bufferSpace;

  }
	
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: updateCardArea();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function updateCardArea(whichPerson,showAll) {
  var bankInfo = "";
  var playerInfo = "";
  if(showAll == null) {
    showAll = true;
  }

  switch(whichPerson){
    case "dealer":
      showDealer = "";
	  showDealer = "<ul>";
      if(showAll) {
      for(whichCard in dealerhand){
      //showDealer += showAcard("dealer",whichCard)+"<br />";
	  showDealer += "<li>"+showAcard("dealer",whichCard)+"</li>";
      }
	  showDealer += "</ul>";
      showDealer += "<br />value:"+returnCardsValue("dealer"); 
	  } else {
	    showDealer = "<ul>";
		showDealer += "<li>"+showAcard("dealer",0)+"</li>";
		showDealer += "<li>"+showBlankCard()+"</li></ul>";
	  }
      document.getElementById("dealer").innerHTML = showDealer;
      break;
    case "player":
      showPlayer = "<ul>";
      for(whichCard in playerhand){
      showPlayer += "<li>"+showAcard("player",whichCard)+"</li>";
      }
      showPlayer += "</ul><br />value:"+returnCardsValue("player");
      document.getElementById("player").innerHTML = showPlayer;
      break;      
    default:
      showBetArea();
  }
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: updateHintArea();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function updateHintArea(nowSayWhat){
  currentHint = nowSayWhat;
  document.getElementById("hints").innerHTML = nowSayWhat;
  if(showHint) {
    document.getElementById("hints").style.display = 'inline';
  } else {
    document.getElementById("hints").style.display = 'none';
  }
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: walletRead();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function walletRead(){
  // Determine the date for when the cookie should expire.
  var cookieThe = document.cookie;
  var cookieInfo = document.cookie.split(";");
  var retVal = new String;
  for(var i=0;i < cookieInfo.length; i++){
    var cookiePart = cookieInfo[i];
    var arrCrumb = cookiePart.split("=");
    for(var j=0;j < arrCrumb.length; j++){
      var tempString = new String;
      tempString = arrCrumb[j];
      if (tempString.indexOf("wallet2") > -1) {
        retVal = parseInt(arrCrumb[j+1]);
      }
    }
  }
  //set the local var to the cookie amount.
  playerCash = retVal;
  return retVal;
}

// -------------------------------------------------------------------------------------------------------------------------
// 
// Function: walletUpdate();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function walletUpdate(){
    // Determine the date for when the cookie should expire.
    var dtCookieExpire = new Date();
    dtCookieExpire.setTime(dtCookieExpire.getTime()+(365*24*60*60*1000));
    var strCookieExpire ="expires="+dtCookieExpire.toGMTString();
    document.cookie = "wallet2="+playerCash +";"+strCookieExpire+";path=/";
  
}

// =====================================================================================================
// 
// End of Script
// 
// =====================================================================================================

 
 
 
 /*
 // -------------------------------------------------------------------------------------------------------------------------
// 
// Function: dealAction();
// What it does:
// Pass to ->:
// Return from: <- :
// 
function dealAction(){
  // rebuild the deck 
  buildDeck();
  // get each player 2 starting off cards.
  giveAcardTo("dealer");
  giveAcardTo("dealer");
  giveAcardTo("player");
  giveAcardTo("player");
  //
  // update the display
  updateCardArea("dealer");
  updateCardArea("player");
  //
  // Check to see if either the player or the dealer got a blackjack.
  // turn off the deal button until dealer finished.
  document.playeractionform.dealbutton.disabled = true;
  // Wait for the player.
}


 
 
 */
 
