
//<!-- Begin Script

//GLOBAL ARRAYS
dealers_hand = new Array();  //card,suit,value
players_hand = new Array();
players_hand2 = new Array(); //only 2 long, but use all 6 just in case

//GLOBAL VARIABLES
//NOTE: TOTAL CHIP AMOUNT IS ACTUALLY CHIPS_LEFT + BET
var bet = 5;
var chips_left = 250;

var player_total = 0;
var dealer_total = 0;

//VARIABLES FOR SPLITS AND DOUBLES
var doubled_down = false;	//BOOLEAN
var split_hand = false; 	//BOOLEAN
var hand_1_amount = 0;  	//PLACE HOLDER FOR HAND 1 TOTAL SCORE IF SPLIT
var hand_1_double = false;  //BOOLEAN: DID PLAYER DOUBLE DOWN ON FIRST HAND?

//VARIABLES FOR THE WAITRESS
var tip_amount = 0;
var waitress_timer = 0;
var waitress_return = true;
var drink = "";
var drink_time = 3;


//===========================================================
//===========================================================
// ALL FUNCTIONS:

function before_start() {

  reset_all();
  get_chips();
}

function get_chips() {

  chips_left = prompt("How Much Money to Cash in for Chips?", 55);
  if (chips_left >= "a")
    chips_left = 250;
  else if (chips_left < 5)
    chips_left = 5;
  else if (chips_left > 10495)
    chips_left = 10495;
  else 
    chips_left = Math.floor(chips_left / 5) * 5;
  
  decrease_chips_by(bet);
    
  document.getElementById("bet_amount").innerHTML = bet;
  document.getElementById("chips_amount").innerHTML = chips_left;  
  show_bet();
  show_chips();

}

//===========================================================
// RESET FUNCTION:

function reset_all() {
  var i;
  
  //RESET ARRAYS
  for (i = 0; i < 10; i++) { //i should only need to be 5, but... to take care of extraneous cases (i.e. nine 2s)
    if (dealers_hand[i]) {
      dealers_hand[i][0] = 0;
      dealers_hand[i][1] = 0;
      dealers_hand[i][2] = 0;
    }
    else
      dealers_hand[i] = new Array(0,0,0);
    if (players_hand[i]) {
      players_hand[i][0] = 0;
      players_hand[i][1] = 0;
      players_hand[i][2] = 0;
    }
    else {
      players_hand[i] = new Array(0,0,0);
    if (players_hand2[i]) {
      players_hand2[i][0] = 0;
      players_hand2[i][1] = 0;
      players_hand2[i][2] = 0;
    }
    else {
      players_hand2[i] = new Array(0,0,0);
    }
    }
  }
  
  //RESET SOME VARIABLES
  doubled_down = false;
  split_hand = false;
  hand_1_double = false;
  hand_1_amount = 0; 
 
  //DELETE WAITRESS IF SHE IS THERE
  //speech.background = "images/null.gif";
  document.getElementById("speech").background = "images/null.gif"; 
  document.main_form.waitress.src = "images/null.gif";
  document.getElementById("waitress_response").innerHTML = "<br>&nbsp;<br>"; 
  document.getElementById("waitress_question").innerHTML = "";
  if (document.main_form.hint_box.checked == false)
    document.getElementById("hint").innerHTML = "";


 
  //CLEAR OUT THE SRC IMAGES
  document.main_form.player_card0.src = "images/null.gif";
  document.main_form.player_card1.src = "images/null.gif";
  document.main_form.player_card2.src = "images/null.gif";
  document.main_form.player_card3.src = "images/null.gif";
  document.main_form.player_card4.src = "images/null.gif"; 
  document.main_form.player_card5.src = "images/null.gif";
  
  document.main_form.dealer_card0.src = "images/null.gif";
  document.main_form.dealer_card1.src = "images/null.gif";
  document.main_form.dealer_card2.src = "images/null.gif";
  document.main_form.dealer_card3.src = "images/null.gif";
  document.main_form.dealer_card4.src = "images/null.gif"; 
  document.main_form.dealer_card5.src = "images/null.gif";
 
  //CLEAR OUT TEXT ON PAGE
  document.getElementById("notes").innerHTML = ""; 
  document.getElementById("notes_2").innerHTML = "";
  document.getElementById("notes_2b").innerHTML = "";
  document.getElementById("notes_3").innerHTML = "";
  document.getElementById("hand_2").innerHTML = "";
  document.getElementById("result").innerHTML = "";
  document.getElementById("hint").innerHTML = "";
 
  show_bet();
  show_chips();
 
}


//=====================================================
// DEALER'S TURN (AFTER PLAYER HAS PLAYER IT OUT)

function dealers_turn() {

  var i = 0;
  var flag = false;

  //SHOW HIDDEN CARD AND ADD VALUE TO TOTAL
  document.main_form.dealer_card0.src = "images/card_" + dealers_hand[0][0] + "_" + dealers_hand[0][1] + ".gif"; 
  dealer_total = dealer_total + dealers_hand[0][2];
  document.getElementById("dealer_amount").innerHTML = dealer_total;  

  //CHECKING TO SEE IF DEALER CAN USE ACE AS 1 INSTEAD OF 11 (IN CASE OF 2 ACES)
  if (dealer_total > 21) 
    aceCheck("dealer", dealers_hand);

  if (dealer_total <= 17)
    flag = true;

  //HIT UNTIL 17
  while (flag) {
 
    //HIT ON SOFT 17 CHECK
    if (dealer_total == 17)
      aceCheck("dealer", dealers_hand);
    
    //IF SOFT CHECK FAILED:
    if (dealer_total == 17)
      flag = false;
  
    if (flag) {
      //GO TO NEXT FREE SLOT FOR A CARD
      i = getaHit(dealers_hand);

      show_dealer_cards();

      dealer_total = dealer_total + dealers_hand[i][2];
      document.getElementById("dealer_amount").innerHTML = dealer_total;
      
      //THIS SECTION FOR THE RARE OCCURRENCE OF MORE THAN 5 CARDS IN A HAND (MOST POSSIBLE IS ABOUT 10)
      //ALL IT DOES IT PUT INFO INTO TOTAL.
      i = 0;
      while (dealers_hand[i][0] != 0)
        i++;     
      if (i > 5)
        document.getElementById("dealer_amount").innerHTML = dealer_total + " (Most recent card: " + dealers_hand[i][0] + ")";
      
  
      //IF BUST, CHECK TO SEE IF DEALER CAN USE ACE AS 1 INSTEAD OF 11
      if (dealer_total > 21) {
        aceCheck("dealer", dealers_hand);
        
        if (dealer_total > 17) {
          document.getElementById("dealer_amount").innerHTML = dealer_total;
          flag = false;
        }     
      } 
    }
    //CONTINUE HITTING LOOP
    if (dealer_total > 17)
      flag = false;
    document.getElementById("dealer_amount").innerHTML = dealer_total;
  }
  
  show_dealer_cards(); 
  done_hand();
}

//============================================================
// DONE HAND (DECIDE WHO WON AND FIGURE OUT WINNINGS / LOSSES)

function done_hand() {

  var d = dealer_total;
  var p = player_total;

  if (doubled_down == true) {
    increase_bet_by(bet);
  }

  //IF PLAYER GETS BLACKJACK.  IF DEALER ALSO DOES, ITS A PUSH; ELSE THEN PLAYER WINS DOUBLE
  if (isBlackjack(players_hand[0],players_hand[1],players_hand[2])) {
    if (isBlackjack(dealers_hand[0],dealers_hand[1],dealers_hand[2]))
      document.getElementById("notes").innerHTML = "It's a Push";
    else {
      document.getElementById("result").innerHTML = "BLACKJACK!";
      document.getElementById("notes").innerHTML = "You Win Double: $" + (bet * 2) + "!";
      increase_chips_by(bet*2); 
    }
  }
  //IF DEALER GETS BLACKJACK, BUT PLAYER DOESN'T (PLAYER BJ NOTED ABOVE), DEALER WINS
  else if (isBlackjack(dealers_hand[0],dealers_hand[1],dealers_hand[2])) {
    //DEALER WINS
    document.getElementById("result").innerHTML = "Dealer Blackjack";
    document.getElementById("notes").innerHTML = "You Lose $" + (bet);
    decrease_chips_by(bet); 
  }
  
  //IF PLAYER BUSTS, DEALER WINS NO MATTER WHAT THEY GET
  else if (p > 21) {
    //DEALER WINS
    document.getElementById("result").innerHTML = "Bust. House Wins";
    document.getElementById("notes").innerHTML = "You Lose $" + (bet);
    decrease_chips_by(bet);
  }
  
  //IF DEALER BUSTS BUT PLAYER DOESN'T, PLAYER WINS
  else if (p <= 21 && d > 21) {
    //PLAYER WINS
    document.getElementById("notes").innerHTML = "YOU WIN $" + bet + "!";
    document.getElementById("result").innerHTML = "Dealer Busts";
    increase_chips_by(bet);
  }

  //IF BOTH PLAYER AND DEALER ARE EQUAL, IT'S A PUSH
  else if (p == d)
    document.getElementById("notes").innerHTML = "It's a Push";
  

  //IF DEALER IS LESS THAN 21 AND PLAYER IS LESS THAN DEALER, DEALER WINS
  else if (d > p && d <= 21) {
    //DEALER WINS
    document.getElementById("result").innerHTML = "House Wins";
    document.getElementById("notes").innerHTML = "You Lose $" + (bet);
    decrease_chips_by(bet);
  } 

  //IF PLAYER IS LESS THAN 21 AND PLAYER IS MORE THAN DEALER, PLAYER WINS
  else if (p > d && p <= 21) {
    //PLAYER WINS
    document.getElementById("notes").innerHTML = "YOU WIN $" + bet + "!";
    increase_chips_by(bet);
  }  
    
  if (doubled_down == true) { //ALREADY AWARDED DOUBLED POINTS, NOW RETURN TO NORMAL
    if (bet % 2 == 0) decrease_bet_by(bet/2);
    doubled_down = false;
  }


  //=======IF THIS IS A SPLIT HAND, CALCULATE SPLIT SCORE SEPARATELY:=========
    
    
  if (hand_1_amount != 0) {  //IF NOT 0 THEN VALUSE IS VALUE OF 'FIRST HAND'
    var d = dealer_total;
    var p = hand_1_amount;

    document.getElementById("notes_2").innerHTML = "PREVIOUS HAND:";

    if (hand_1_double == true) {
      increase_bet_by(bet);
    }
  
    //IF PLAYER GETS BLACKJACK.  IF DEALER ALSO DOES, ITS A PUSH; ELSE THEN PLAYER WINS DOUBLE
    if (isBlackjack(players_hand[0],players_hand[1],players_hand[2])) {
      if (isBlackjack(dealers_hand[0],dealers_hand[1],dealers_hand[2]))
        document.getElementById("notes_2b").innerHTML = "It's a Push";
      else {
        document.getElementById("notes_2b").innerHTML = "Blackjack wins Double: $" + (bet * 2) + "!";
        increase_chips_by(bet*2); 
      }
    }
    //IF DEALER GETS BLACKJACK, BUT PLAYER DOESN'T (PLAYER BJ NOTED ABOVE), DEALER WINS
    else if (isBlackjack(dealers_hand[0],dealers_hand[1],dealers_hand[2])) {
      //DEALER WINS
      document.getElementById("notes_2b").innerHTML = "You Lose $" + (bet);
      decrease_chips_by(bet); 
    }
  
    //IF PLAYER BUSTS, DEALER WINS NO MATTER WHAT THEY GET
    else if (p > 21) {
      //DEALER WINS
      document.getElementById("notes_2b").innerHTML = "Bust. You Lose $" + (bet);
      decrease_chips_by(bet);
    }
  
    //IF DEALER BUSTS BUT PLAYER DOESN'T, PLAYER WINS
    else if (p <= 21 && d > 21) {
      //PLAYER WINS
      document.getElementById("notes_2b").innerHTML = "YOU WIN $" + bet + "!";
      increase_chips_by(bet);
    }

    //IF BOTH PLAYER AND DEALER ARE EQUAL, IT'S A PUSH
    else if (p == d)
      document.getElementById("notes_2b").innerHTML = "It's a Push";
  
    //IF DEALER IS LESS THAN 21 AND PLAYER IS LESS THAN DEALER, DEALER WINS
    else if (d > p && d <= 21) {
      //DEALER WINS
      document.getElementById("notes_2b").innerHTML = "You Lose $" + (bet);
      decrease_chips_by(bet);
    } 

    //IF PLAYER IS LESS THAN 21 AND PLAYER IS MORE THAN DEALER, PLAYER WINS
    else if (p > d && p <= 21) {
      //PLAYER WINS
      document.getElementById("notes_2b").innerHTML = "YOU WIN $" + bet + "!";
      increase_chips_by(bet);
    }     
  
    if (hand_1_double == true) { //ALREADY AWARDED POINTS (DOUBLED BET ABOVE), NOW RETURN TO NORMAL
      if (bet % 2 == 0) decrease_bet_by(bet/2);
      hand_1_double = false;
    }
  }  
  
  // ========= BOTH REGULAR HAND AND SPLIT HAND: SHOW FUNCTIONS ======
  
  show_chips();
  show_bet(); 
  
  //ENABLE MAIN BUTTONS AGAIN
  document.main_form.bet_plus.disabled = false;
  document.main_form.bet_minus.disabled = false;
  document.main_form.bet_set.disabled = false;
  document.main_form.deal_button.disabled = false;
  
  waitress();
  
}

//=================================================================================
//HELPER and SHOW FUNCTIONS

function isBlackjack(card1,card2,card3) {

  var ret = false;
   
  if (card1[2] + card2[2] == 21)
    if (card1[0] == "ace" || card2[0] == "ace") 
      if (card3[2] == 0) { //IF ONLY TWO CARDS LONG
        ret = true;
      }
      
  return ret;
}

//====================================================================================
//THIS FUNCTION DEDUCES HOW MANY $5 CHIPS, $50 CHIPS, AND $500 CHIPS TO DISPLAY
//AND USING THE DIV AND MOD FUNCTIONS, FIGURES OUT WHICH IMAGE TO DISPLAY 

function show_bet() {

  var x, y;

  if (bet > 10000+450+45)  //WHICH IT SHOULD NEVER BE 
    x = 10495;
  else
    x = bet;

  x = ( Math.floor(x/500.0) ) * 500;
  if (x > 0) document.main_form.bet_chips3.src = "images/chips_" + x + ".gif";
  else document.main_form.bet_chips3.src = "images/null.gif";
  
  y = bet - x;
  y = ( Math.floor(y/50.0) ) * 50;
  if (y > 0) document.main_form.bet_chips2.src = "images/chips_" + y + ".gif";
  else document.main_form.bet_chips2.src = "images/null.gif";

  x = bet - x - y;
  if (x > 0) document.main_form.bet_chips1.src = "images/chips_" + x + ".gif";
  else document.main_form.bet_chips1.src = "images/null.gif";

  document.getElementById("bet_amount").innerHTML = bet; 
}

//====================================================================================
//THIS FUNCTION DEDUCES HOW MANY $5 CHIPS, $50 CHIPS, AND $500 CHIPS TO DISPLAY
//AND USING THE DIV AND MOD FUNCTIONS, FIGURES OUT WHICH IMAGE TO DISPLAY 

function show_chips() {
  
  var x, y, temp_chips;


  if (chips_left > 10495) 
    temp_chips = 10495;			//CAN'T DRAW MORE THAN $10495 IN CHIPS, BUT WILL SHOW CORRECT VALUE IN TEXT ANYWAY
  else
    temp_chips = chips_left;

  x = ( Math.floor(temp_chips/500.0) ) * 500;
  if (x > 0) document.main_form.chips_left3.src = "images/chips_" + x + ".gif";
  else document.main_form.chips_left3.src = "images/null.gif";
 
  y = temp_chips - x;
  y = ( Math.floor(y/50.0) ) * 50;
  if (y > 0) document.main_form.chips_left2.src = "images/chips_" + y + ".gif";
  else document.main_form.chips_left2.src = "images/null.gif";

  x = temp_chips - x - y;
  if (x > 0) document.main_form.chips_left1.src = "images/chips_" + x + ".gif";
  else document.main_form.chips_left1.src = "images/null.gif";

  document.getElementById("chips_amount").innerHTML = chips_left;
}

//====================================================================================

function show_player_cards() {

    if (players_hand[1][0] != 0) 
      document.main_form.player_card1.src = "images/card_" + players_hand[1][0] + "_" + players_hand[1][1] + ".gif";
    if (players_hand[2][0] != 0) 
      document.main_form.player_card2.src = "images/card_" + players_hand[2][0] + "_" + players_hand[2][1] + ".gif";
    if (players_hand[3][0] != 0) 
      document.main_form.player_card3.src = "images/card_" + players_hand[3][0] + "_" + players_hand[3][1] + ".gif";
    if (players_hand[4][0] != 0) 
      document.main_form.player_card4.src = "images/card_" + players_hand[4][0] + "_" + players_hand[4][1] + ".gif";
    if (players_hand[5][0] != 0) 
      document.main_form.player_card5.src = "images/card_" + players_hand[5][0] + "_" + players_hand[5][1] + ".gif"; 
}

//====================================================================================

function show_dealer_cards() {
    if (dealers_hand[2][0] != 0) 
      document.main_form.dealer_card2.src = "images/card_" + dealers_hand[2][0] + "_" + dealers_hand[2][1] + ".gif";
    if (dealers_hand[3][0] != 0) 
      document.main_form.dealer_card3.src = "images/card_" + dealers_hand[3][0] + "_" + dealers_hand[3][1] + ".gif";
    if (dealers_hand[4][0] != 0) 
      document.main_form.dealer_card4.src = "images/card_" + dealers_hand[4][0] + "_" + dealers_hand[4][1] + ".gif";
    if (dealers_hand[5][0] != 0) 
      document.main_form.dealer_card5.src = "images/card_" + dealers_hand[5][0] + "_" + dealers_hand[5][1] + ".gif";
}

//=====================================================
// CHOOSING A RANDOM CARD

function getRandomCard() {

  var ret;
  ret = Math.floor((Math.random() * 250) % 13) + 1;
  if (ret == 1)
    ret = "ace";
  else if (ret == 11)
    ret = "jack";
  else if (ret == 12)
    ret = "queen";
  else if (ret == 13)
    ret = "king";
      
  return ret; //which will be a number by default
}

//======================================================

function getRandomSuit() {
  var ret;
  ret = Math.floor((Math.random() * 250) % 4) + 1;
  
  if (ret == 1)
    ret = "clubs";
  else if (ret == 2)
    ret = "diamonds";
  else if (ret == 3)
    ret = "hearts";
  else
    ret = "spades";
  
  return ret;

}

//======================================================

function getValue(card) {

  var ret;
  if (card == "ace")
    ret = 11;
  else if (card == "jack" || card == "queen" || card == "king")
    ret = 10;
  else
    ret = card;
    
  return ret;

}

//=====================================================
//BET FUNCTIONS

function set_bet() {
 
  x = prompt('Bet Amount?',20);
  prev_bet = bet;
  
  //IF X IS NOT A NUMBER:
  if (x >= "a") {
    if (chips_left > 1000)
      bet = 50;
    else
      bet = 5;
  }
  else {
    if (x > chips_left+prev_bet) {
      if (chips_left+prev_bet <= 10495)
        bet = chips_left+prev_bet;
      else
        bet = 10495;
    }
    else if (x > 10495)
      bet = 10495;
    else if (x < 0 )
      bet = 5;
    else
      bet = Math.floor(x/5)*5;
  }

  if (prev_bet > bet) {
    increase_chips_by(prev_bet - bet);
  }
  else if (prev_bet < bet ) {
    decrease_chips_by(bet - prev_bet);
  }
  
  show_bet(); 
}

//======================================================

function increase_bet_by(val) {

  if (chips_left >= val) {
    if (bet + val > 10495) {
      if (split_hand == false && doubled_down == false && hand_1_double == false)
        document.getElementById("notes_3").innerHTML = "Bet Max is $10495"; 
      else {
        bet = bet + val;
        decrease_chips_by(val);      
      }  
    }
    else {
      bet = bet + val;
      decrease_chips_by(val);
    }
  }
  else
    document.getElementById("notes_3").innerHTML = "Out of Chips";
    
  show_bet();
 
}

//======================================================

function decrease_bet_by(val) {
 
  if (bet - val < 0) {  //DOUBLE CHECK
    bet = 0; 
    increase_chips_by(bet); 
  }
  else {
    bet = bet - val;
    increase_chips_by(val); 
  } 
  if (chips_left + bet > 0)
    document.getElementById("notes_3").innerHTML = "";
       
  show_bet();
 
}

//======================================================

function increase_chips_by(val) {

  chips_left = chips_left + val;

  show_chips();
}

//======================================================

function decrease_chips_by(val) {

  //(NOTE:  TOTAL CHIPS IS chips_left + bet (LIKE IN REAL LIFE)
  //IF WE ARE DECREASING CHIPS BY MORE CHIPS THEN WE HAVE (SHOULDN'T HAPPEN)
  if (val > chips_left + bet) {
    chips_left = 0;
    bet = 0;
    document.getElementById("notes_3").innerHTML = "Out of Chips";
  }
  //ELSE IF WE ARE TAKING AWAY MORE CHIPS THEN chips_left, WE DECREASE THE BET BY REMAINDER
  else if (val > chips_left) {  // 20 chips left, take away val 30 (bet is 50)
    bet = bet - val + chips_left;
    chips_left = 0;

    //AT THIS POINT, THE BET SHOULD BE ALL THE CHIPS WE HAVE LEFT
  }
  else
    chips_left = chips_left - val;
    
  show_bet();  
  show_chips();
}

//=====================================================
//PLAY FUNCTIONS (HIT, STAND, DOUBLE, SPLIT)

function hit() {

  //GO TO NEXT FREE SLOT FOR A CARD
  i = getaHit(players_hand);
  
  //THESE LINES ARE FOR THE RARE CASE WHEN THERE IS MORE THAN 5 CARDS ON THE TABLE
  if (i > 5)
    document.getElementById("player_amount").innerHTML = player_total + " (Most recent card: " + players_hand[i][0] + ")";
 
  player_total = player_total + players_hand[i][2];

  //IF A SPLIT, THE FIRST ACE COULD BE A ONE (IN THE CASE OF A DOUBLE ACE)
  if (split_hand == true) {
    if (players_hand[0][2] == 1) {
      players_hand[0][2] = 11;
      player_total = player_total + 10;
    }
  }
  
  show_player_cards();

  if (split_hand != true)
    document.main_form.double_button.disabled = true;
  else if (players_hand[3][0] != 0)
    document.main_form.double_button.disabled = true;
    
  if (player_total > 21)
    aceCheck("player", players_hand);

    if (player_total > 21) {
      document.getElementById("player_amount").innerHTML = player_total;
      stand();
    }
 
  document.getElementById("player_amount").innerHTML = player_total;
  
  if (document.main_form.hint_box.checked == true)
    show_hint();
}

//====================================================================================

function getaHit(card_array) {

  var i = 0;

  while (card_array[i][0] != 0)
    i++;
  
  card_array[i][0] = getRandomCard();
  card_array[i][1] = getRandomSuit();
  card_array[i][2] = getValue(card_array[i][0]);
  
  return i;
}

//====================================================================================

function aceCheck(person, card_array) {

  if (person == "dealer")
    total = dealer_total;
  else 
    total = player_total;
    
  i = 0;
  while (i < 6 && card_array[i][0] != 0) {
    if (card_array[i][2] == 11) { 		//IF IT IS AN ACE
      card_array[i][2] = 1;      		//CHANGE VALUE FROM 11 TO 1
      total = total - 10;				//REFLECT CHANGE IN PLAYER TOTAL
      i = 100;   						//FINISH LOOP SINCE WE ONLY WANT TO DO THIS FOR ONE ACE AT A TIME    
    }
    i++;
  }
  
  if (person == "dealer")
    dealer_total = total;
  else 
    player_total = total;

}

//====================================================================================

function stand() {

  var j = 0;
  
  if (split_hand == true && hand_1_amount == 0) { 
  //IF WE GET INTO THIS LOOP, WE HAVE SPLIT AND HAVE JUST FINISHED THE FIRST HAND

    //RECORD SECOND (AND THIRD) CARD OF FIRST HAND INTO SLOTS OF HOLDERS TO LATER CHECK FOR BLACKJACK
    players_hand2[1][0] = players_hand[1][0];    
    players_hand2[1][1] = players_hand[1][1];    
    players_hand2[1][2] = players_hand[1][2];
    players_hand2[2][0] = players_hand[2][0];    
    players_hand2[2][1] = players_hand[2][1];    
    players_hand2[2][2] = players_hand[2][2];
     
    //COMPLETELY ERASE FIRST HAND
    for (j = 0; j < 6; j++) {
        players_hand[j][0] = 0;
        players_hand[j][1] = 0;
        players_hand[j][2] = 0;
    }

    //PUT SPLIT CARD (SECOND CARD) FROM FIRST HAND INTO FIRST SLOT OF SECOND HAND
    players_hand[0][0] = players_hand2[0][0];
    players_hand[0][1] = players_hand2[0][1];
    players_hand[0][2] = players_hand2[0][2];
   
    document.main_form.player_card0.src = "images/card_" + players_hand[0][0] + "_" + players_hand[0][1] + ".gif";  
    
    //NORMALLY, I'D JUST CALL SHOW_CARDS, BUT THAT STARTED SCREWING UP OTHER STUFF, SO ...
    //I'LL DELETE THE EXTRANEOUS IMAGES MANUALLY:
    document.main_form.player_card1.src = "images/null.gif";
    document.main_form.player_card2.src = "images/null.gif";
    document.main_form.player_card3.src = "images/null.gif";
    document.main_form.player_card4.src = "images/null.gif";
    document.main_form.player_card5.src = "images/null.gif";

    //NOTE IF PLAYER DOULBED DOWN ON FIRST HAND
    if (doubled_down == true) {
      hand_1_double = true;
      doubled_down = false;
    }

    hand_1_amount = player_total;           //RECORD FIRST TOTAL FOR LATER SCORING
    player_total = players_hand2[0][2];
    
    document.getElementById("player_amount").innerHTML = player_total; 
    document.getElementById("hand_2").innerHTML = "* SECOND HAND *  (FIRST HAND TOTAL: " + hand_1_amount + ")"; 

    show_hint();

  }
  
  //THE NORMAL FUNCTION ONE GOES INTO...
  else { 			
  
    document.main_form.hit_button.disabled = true; 
    document.main_form.stand_button.disabled = true; 
    document.main_form.double_button.disabled = true; 
    document.main_form.split_button.disabled = true; 
    
    dealers_turn();
  }  
}

//====================================================================================

function double_down() {

  doubled_down = true;

  document.getElementById("notes_2b").innerHTML = "Doubled Down";
  
  i = getaHit(players_hand)

  player_total = player_total + players_hand[i][2];

  //CHECK FOR ACES:
  if (player_total > 21) {
    aceCheck("player", players_hand);
  }
 
  document.getElementById("player_amount").innerHTML = player_total;

  show_player_cards();
  
  //DISABLE ALL BUTTONS AND MOVE ON TO DEALER (THE STAND() FUNCTION BELOW DOES THIS)
  stand();

}

//====================================================================================

function split() {
  
  document.main_form.split_button.disabled = true; 
  
  //DOUBLE CHECK TO SEE IF SPLIT IS POSSIBLE
  if (players_hand[0][0] != players_hand[1][0]) {
    split_hand = false;
  }
  else {
    split_hand = true;
    
    //TAKE AWAY SPLIT CARD IN TOTAL
    player_total = player_total - players_hand[1][2];
    document.getElementById("player_amount").innerHTML = player_total; 
        
    //PUT SECOND CARD INTO PLACEHOLDER
    players_hand2[0][0] = players_hand[1][0];    
    players_hand2[0][1] = players_hand[1][1];    
    players_hand2[0][2] = players_hand[1][2];

    //RESET SECOND CARD OF FIRST HAND TO NULL
    players_hand[1][0] = 0;
    players_hand[1][1] = 0;
    players_hand[1][2] = 0;    
    document.main_form.player_card1.src = "images/null.gif";  
     
    show_hint(); 
     
    document.getElementById("hand_2").innerHTML = "* FIRST HAND *";    
  }
}

//===========================================================
//HINT

function show_hint() {

  p = player_total;
  d = dealer_total;

  //FIGURE OUT IF IT THIS IS CALLED WHEN ONLY TWO CARDS ARE SHOWING
  //BECAUSE MOST OF HINTS WILL BE IRREVELANT IF MORE THAN TWO CARDS ARE SHOWING
  if (players_hand[2][2] == 0)
    first = true;
  else
    first = false;

  //IF NO CARDS HAVE BEEN DEALT, STOP PROCESS
  if ( players_hand[0][0] == 0 ) {
    document.getElementById("hint").innerHTML = "";   
  }

  //IF DEALER BLACKJACK, STOP PROCESS
  else if ( isBlackjack(dealers_hand[0], dealers_hand[1], dealers_hand[2]) ) {
    document.getElementById("hint").innerHTML = "Dealer Blackjack";   
  }

  //IF ONE CARD IS AN ACE
  else if ( (players_hand[0][0] == "ace" || players_hand[1][0] == "ace") && first == true) {
 
    //IF BOTH CARDS ARE ACES
    if (players_hand[0][0] == "ace" && players_hand[1][0] == "ace")
      document.getElementById("hint").innerHTML = "Always SPLIT with double aces";
    
    //ELSE IF ONE CARD (EITHER ONE) IS SOMETHING ELSE  
    else { 
      //FIND VALUE OF NON-ACE CARD
      if (players_hand[0][0] == "ace")
        other = players_hand[1][2];
      else
        other = players_hand[0][2];
       
      if (other == 10)
        document.getElementById("hint").innerHTML = "BLACKJACK!";  
      else if (other >= 8) 
        document.getElementById("hint").innerHTML = "Always STAND when above 18";         
      else if (other <= 6 && d >= 7) 
        document.getElementById("hint").innerHTML = "You should HIT since dealer shows more than 6"; 
      else if ( other >= 4 && other <= 7 && d >= 4 && d <= 6 )     
        document.getElementById("hint").innerHTML = "You should DOUBLE DOWN";
      else if (other == 7) {
        if (d == 2 || d == 7 || d == 8)       
          document.getElementById("hint").innerHTML = "You should STAND";
        else if (d >= 9)
          document.getElementById("hint").innerHTML = "You should HIT";
        else
          document.getElementById("hint").innerHTML = "You should DOUBLE DOWN";
      }
      else if (other <= 3) {
        if (d <= 4)
          document.getElementById("hint").innerHTML = "You should HIT";
        else  
          document.getElementById("hint").innerHTML = "You should DOUBLE DOWN";
      }
      else if (other == 6 && d == 3)
        document.getElementById("hint").innerHTML = "You should DOUBLE DOWN";
      else
        document.getElementById("hint").innerHTML = "You should HIT";
    }
  }  
  
  //ELSE IF BOTH CARDS ARE THE SAME (AND NOT ACES - THAT CASE TAKEN CARE OF ABOVE)
  else if ( (players_hand[0][0] == players_hand[1][0]) && first == true) {
    
    card = players_hand[0][2]; //BOTH CARD VALUES ARE THE SAME, SO IT DOESN'T MATTER WHICH
    
    if (card == 8)
      document.getElementById("hint").innerHTML = "Always SPLIT with double 8s"; 
    else if (card == 10)
      document.getElementById("hint").innerHTML = "Always STAND when both cards equal 10"; 
    else if (card == 9) {
      if (d == 7 || d >= 10)
        document.getElementById("hint").innerHTML = "You should STAND"; 
      else  
        document.getElementById("hint").innerHTML = "Usually SPLIT if dealer has less than 10";
    }
    else if (card == 7) {
      if (d >= 8)
        document.getElementById("hint").innerHTML = "You should HIT since dealer shows more than 7";  
      else
        document.getElementById("hint").innerHTML = "You should SPLIT since dealer shows less than 8";
    }
    else if (card == 6) {
      if (d == 2 || d >= 7)
        document.getElementById("hint").innerHTML = "You should HIT";
      else
        document.getElementById("hint").innerHTML = "Usually SPLIT since dealer shows less than 7";  
    }
    else if (card == 5) {
      if (d >= 10)
        document.getElementById("hint").innerHTML = "You should HIT since dealer is showing a good hand";
      else
        document.getElementById("hint").innerHTML = "You should DOUBLE DOWN since dealer is showing less than 10";
    }
    else if (card == 4)
      document.getElementById("hint").innerHTML = "You should always HIT on double 4s";
    else {
      if (d <= 3 && d >= 8)
        document.getElementById("hint").innerHTML = "You should HIT";
      else
        document.getElementById("hint").innerHTML = "You should SPLIT";
    }  
  }
  
  //ELSE YOU HAVE TWO REGULAR CARDS
  else {
      
    if (p > 21)
      document.getElementById("hint").innerHTML = ""; 
    else if (p == 21)  
      document.getElementById("hint").innerHTML = "You have 21!  STAND, of course"; 
    else if (p >= 17)
      document.getElementById("hint").innerHTML = "You should STAND since you have above 16"; 
    else if (p <= 8)
      document.getElementById("hint").innerHTML = "You should HIT since you have less than 8"; 
    else if (d >= 7 && p >= 12)
      document.getElementById("hint").innerHTML = "You should HIT since dealer shows above 6";
    else if (d <= 6 && p >= 12) {
      if (d <= 3 && p == 12)
        document.getElementById("hint").innerHTML = "You should HIT";
      else
        document.getElementById("hint").innerHTML = "You should STAND since dealer shows less than 7";
    }
    else if (p == 11) {
      if (d <= 10 && first == true)
        document.getElementById("hint").innerHTML = "You should DOUBLE DOWN since dealer is not showing an ace"; 
      else
        document.getElementById("hint").innerHTML = "You should HIT";
    }
    else if (p == 10) {
      if (d <= 9 && first == true)
        document.getElementById("hint").innerHTML = "You should DOUBLE DOWN since dealer is showing less than 10"; 
      else
        document.getElementById("hint").innerHTML = "You should HIT since dealer is showing 10 or above";
    }
    else if (p == 9) {
      if (d == 2 && d >= 7)
        document.getElementById("hint").innerHTML = "You should HIT";
      else if (first == true)
        document.getElementById("hint").innerHTML = "You should DOUBLE DOWN"; 
      else
        document.getElementById("hint").innerHTML = "You should HIT";
    }   
  }
} //END HINT


//===========================================================
//DEAL

function deal() {

  var x;
  
 if (bet > 0) {  
  
  reset_all();

  //DISABLE BETTING BUTTONS
  document.main_form.bet_plus.disabled = true;
  document.main_form.bet_minus.disabled = true;
  document.main_form.bet_set.disabled = true;
  document.main_form.deal_button.disabled = true;
  
  //GET FIRST TWO PLAYERS CARDS AND FIRST TWO DEALERS CARDS
  getaHit(players_hand);
  getaHit(players_hand); 
  getaHit(dealers_hand);
  getaHit(dealers_hand);  
  
  //DISPLAY 2 PLAYERS CARDS AND 1 DEALER CARD (OTHER DEALER CARD IS A DESIGN SO CAN'T SEE VALUE)
  document.main_form.player_card0.src = "images/card_" + players_hand[0][0] + "_" + players_hand[0][1] + ".gif";
  document.main_form.player_card1.src = "images/card_" + players_hand[1][0] + "_" + players_hand[1][1] + ".gif";
  document.main_form.dealer_card0.src = "images/card_hidden.gif";
  document.main_form.dealer_card1.src = "images/card_" + dealers_hand[1][0] + "_" + dealers_hand[1][1] + ".gif";
    
  //GET TOTALS
  player_total = players_hand[0][2] + players_hand[1][2];
  dealer_total = dealers_hand[1][2];
  
  //CHECK FOR ACES IN PLAYERS HAND (IN CASE OF TWO ACES):
  if (player_total > 21) {
    aceCheck("player", players_hand);
  }
  
  //DISPLAY ABOVE PLAYER AND DEALER TOTALS
  document.getElementById("player_amount").innerHTML = player_total;
  document.getElementById("dealer_amount").innerHTML = dealer_total;

  //SHOW HINT IF HINT TURNED ON
  if (document.main_form.hint_box.checked == true)
    show_hint();

  //IF EITHER HAND IS A BLACKJACK, HAND IS OVER
  if (isBlackjack(players_hand[0],players_hand[1],players_hand[2]) 
  || isBlackjack(dealers_hand[0],dealers_hand[1],dealers_hand[2])) {
    stand();
  }
  
  //OTHERWISE ENABLE APPROPRIATE BUTTONS AND END FUNCTION
  else {
    document.main_form.hit_button.disabled = false;
    document.main_form.stand_button.disabled = false;
    
    //IF ARE NOT ABLE TO DOUBLE BET, THEN DON'T ENABLE DOUBLE OR SPLIT (chips_left + bet is amount held)
    if (chips_left >= bet)    
      document.main_form.double_button.disabled = false;
      
    if ( (players_hand[0][0] == players_hand[1][0]) && (chips_left >= bet) )
      document.main_form.split_button.disabled = false;
  }

 } //end IF BET > 0

}

//===========================================================================
//===========================================================================
// THE WAITRESS and DRINK FUNCTIONS ARE BELOW


//===========================================================================
//MAIN WAITRESS FUNCTION

function waitress() {

  var want_drink;
  
  waitress_timer++;

  //EVERY 10 HANDS, WE HAVE A ONE IN THREE CHANCE OF WAITRESS/DRINK ACTION
  if (waitress_timer % 9 == 0 && Math.floor(Math.random() * 255) % 2 == 0) {

    //IF NEVER HAD DRINK, OFFER A DRINK
    if (drink == "") {
    
      show_waitress("Would you like a<br>beverage?");

      document.getElementById("waitress_response").innerHTML = 
        "<input TYPE='Button' VALUE='Yes' onclick='get_drink()'><br><input TYPE='Button' VALUE='No' onclick='show_waitress(null)'>"; 
    }
  }
//ON A SHORTER INTERVAL, HAVE THE DRINK GO DOWN (MAKING SURE WE HAVE A DRINK)
  else if (waitress_timer % 5 == 0 && drink != "") {
    //IF IN MIDDLE OF DRINK, LOWER DRINK AMOUNT
    if (drink_time > 0) {
      drink_time = drink_time - 1;
      document.main_form.waitress_drink.src="images/" + drink + "_" + drink_time + ".gif";
    }
    //ELSE IF DONE DRINK, HAVE WAITRESS STOP BY AGAIN *IF* THEY WERE TIPPED THE LAST TIME
    else if (drink_time == 0) {  
      if (waitress_return == true) {
        show_waitress("Would you like another beverage?");

        document.getElementById("waitress_response").innerHTML = 
          "<input TYPE='Button' VALUE='Yes' onclick='get_drink()'><br><input TYPE='Button' VALUE='No' onclick='show_waitress(null)'>";
      }
    }
  }
}

//====================================================
// GET A DRINK!

function get_drink() {  //has user choose drink, then asks for a tip

  drink_time = 3; //START OVER DRINK TIME

  show_waitress("Which beverage<br>would you like?");

  document.getElementById("waitress_response").innerHTML = "<input TYPE='Button' VALUE=' Water ' onclick='set_drink(1)'> <input TYPE='Button' VALUE='Coffee' onclick='set_drink(2)'><br><input TYPE='Button' VALUE=' Beer ' onclick='set_drink(3)'> <input TYPE='Button' VALUE='Martini' onclick='set_drink(4)'><br><input TYPE='Button' VALUE='Rum&Coke' onclick='set_drink(5)'> <input TYPE='Button' VALUE='Scotch' onclick='set_drink(6)'>";

}

//====================================================
// SET DRINK!

function set_drink(num) {

  if (num == 1)
    drink = "water";
  else if (num == 2)
    drink = "coffee";
  else if (num == 3)
    drink = "beer";
  else if (num == 4)
    drink = "martini";
  else if (num == 5)
    drink = "rum_coke";
  else if (num == 6)
    drink = "scotch";

  document.main_form.waitress_drink.src="images/" + drink + "_" + drink_time + ".gif";

  show_waitress("Enjoy!");
  
  document.getElementById("waitress_response").innerHTML = 
   "<b>Tip? $" + tip_amount + "</b> <input TYPE='Button' VALUE='<' onclick='set_tip(-5)'> <input TYPE='Button' VALUE='>' onclick='set_tip(5)'><br><input TYPE='Button' VALUE='Done' onclick='done_tip()'> &nbsp; ";
  
}

function set_tip(num) {

  tip_amount = tip_amount + num;
  
  if (tip_amount < 0)
    tip_amount = 0;
  else if (tip_amount > chips_left + bet)
    tip_amount = chips_left + bet;
    
  document.getElementById("waitress_response").innerHTML = 
   "<b>Tip? $" + tip_amount + "</b> <input TYPE='Button' VALUE='<' onclick='set_tip(-5)'> <input TYPE='Button' VALUE='>' onclick='set_tip(5)'><br><input TYPE='Button' VALUE='Done' onclick='done_tip()'> &nbsp; ";
     
    
}

function done_tip() {

  decrease_chips_by(tip_amount);

  if (tip_amount <= 0 ) {
    waitress_return = false;
    //DELETE IMAGES
	sdocument.peech.background = "images/null.gif";
	document.main_form.waitress.src = "images/null.gif";
	document.getElementById("waitress_question").innerHTML = "";
	document.getElementById("waitress_response").innerHTML = "<br><br>"; 
  }
  else if (tip_amount < 50) {
	document.getElementById("waitress_question").innerHTML = "Thank you!";
	document.getElementById("waitress_response").innerHTML = "<br><br>";  
  }
  else if (tip_amount < 100) {
	document.getElementById("waitress_question").innerHTML = "Thanks! You've<br>made my night!";
	document.getElementById("waitress_response").innerHTML = "<br><br>";  
  }
  else if (tip_amount < 500) {
	document.getElementById("waitress_question").innerHTML = "Want to come<br>home with me?";
	document.getElementById("waitress_response").innerHTML = "<br><br>";  
  }
  else if (tip_amount < 1000) {
	document.getElementById("waitress_question").innerHTML = "You are my<br>god and I worship you!";
	document.getElementById("waitress_response").innerHTML = "<br><br>";  
  }
  else {
	document.getElementById("waitress_question").innerHTML = "Oh my God! (fainting in shock)";
	document.getElementById("waitress_response").innerHTML = "<br><br>";  
  }
 
 
}

//====================================================
// DISPLAY (OR UN-DISPLAY) WAITRESS-RELATED IMAGES

function show_waitress(the_string) {

  if (the_string == null) {
    //speech.background = "images/null.gif";
    document.getElementById("speech").background = "images/null.gif";
    document.main_form.waitress.src = "images/null.gif";
    document.main_form.waitress_drink.src = "images/null.gif";
    document.getElementById("waitress_question").innerHTML = "";
    document.getElementById("waitress_response").innerHTML = "<br><br>"; 
    drink = "";   
  }
  else {
    document.main_form.waitress.src = "images/waitress.jpg";
    //speech.background = "images/speech_bubble.gif";
    document.getElementById("speech").background = "images/speech_bubble.gif";
    document.getElementById("waitress_question").innerHTML = the_string; 
  }

}

// End Script --> 


