// pokerFunctions.js
// This file is a compilation of all applicable Poker Functions for the site
//

/***************************************************************************
** Function:  	randomCards()
** Purpose:   	To generate random Cards and display as a graphic 
** Parameters:	none
***************************************************************************/

function randomCards(cardsID, quizID) {
	
var rank=new Array(13);
rank[1]="2";
rank[2]="3";
rank[3]="4";
rank[4]="5";
rank[5]="6";
rank[6]="7";
rank[7]="8";
rank[8]="9";
rank[9]="10";
rank[10]="J";
rank[11]="Q";
rank[12]="K";
rank[13]="A";

var suit=new Array(4);
suit[1]="c";
suit[2]="d";
suit[3]="h";
suit[4]="s";

var card1,card2;
var cardSuit1,cardSuit2;
var handValue;
var rankVal1, rankVal2;
var newHTML;

rankVal1 = getRank();
cardSuit1 = suit[getSuit()];
card1 = rank[rankVal1];
card1 += cardSuit1;

rankVal2 = getRank();
cardSuit2 = suit[getSuit()];
card2 = rank[rankVal2];
card2 += cardSuit2;

if ( (rankVal1 == rankVal2) && (cardSuit1 == cardSuit2) ) {
  while ( (rankVal1 == rankVal2) && (cardSuit1 == cardSuit2) ) {
    rankVal2 = getRank();
    card2 = rank[rankVal2];
    card2 += suit[getSuit()];
  }
}


newHTML = "<br><img src=\"images/cards/" + card1 + ".gif\">";
newHTML += "&nbsp";
newHTML += "<img src=\"images/cards/" + card2 + ".gif\">";

handValue = getHandValue(rankVal1, cardSuit1, rankVal2, cardSuit2);

newHTML += "<br>Hand Value is: " + handValue;
newHTML += "<p><form name=\"poker\">";
newHTML += "<input type=\"button\" value=\"Try another Hand\" onclick=\"window.location.reload()\"><br>";
newHTML += "</form>";

print(cardsID, newHTML);

quizUser(handValue, quizID);

}

/***************************************************************************
** Function:  	quizUser()
** Purpose:   	To generate/grade User's Assessment of hands
** Parameters:	handValue, quizID
** Returns:	none
** Assumes:	Result is the ID of the answer
***************************************************************************/

function quizUser(Val, quizID) {

var newHTML;

newHTML = "What should you do in:<br>\n";
newHTML += "<table>\n<tr><td width = 200><B>Early Position</B>";
newHTML += "<form name=EP><input Name=early type=radio value=fold onClick=\"answerEPQuiz(" + Val + ", document.EP);\">Fold<br>";
newHTML += "<input Name=early type=radio value=call onClick=\"answerEPQuiz(" + Val + ", document.EP);\">Call<br>\n";
newHTML += "<input Name=early type=radio value=raise onClick=\"answerEPQuiz(" + Val + ", document.EP);\">Raise\n</form>";

newHTML += "<td width=200><B>Middle Position</B>";
newHTML += "<form name=MP><input Name=middle type=radio value=fold onClick=\"answerMPQuiz(" + Val + ", document.MP);\">Fold<br>";
newHTML += "<input Name=middle type=radio value=call onClick=\"answerMPQuiz(" + Val + ", document.MP);\">Call<br>\n";
newHTML += "<input Name=middle type=radio value=raise onClick=\"answerMPQuiz(" + Val + ", document.MP);\">Raise\n</form>";

newHTML += "<td width=200><B>Late Position</B>";
newHTML += "<form name=LP><input Name=late type=radio value=fold onClick=\"answerLPQuiz(" + Val + ", document.LP);\">Fold<br>";
newHTML += "<input Name=late type=radio value=call onClick=\"answerLPQuiz(" + Val + ", document.LP);\">Call<br>\n";
newHTML += "<input Name=late type=radio value=raise onClick=\"answerLPQuiz(" + Val + ", document.LP);\">Raise\n</form>";

newHTML += "<tr><td><div ID=eresult></div>";
newHTML += "<td><div ID=mresult></div>";
newHTML += "<td><div ID=lresult></div>";

newHTML += "</table>";

print(quizID, newHTML);
}

function answerEPQuiz(hVal, form) {

var answer = getRadioValue(form.early);
var newTEXT;

if (hVal >= 9) {
  if (answer == "raise") {
    newTEXT = "Correct";
  } else {
    newTEXT = "Incorrect.  You should Raise";
  }
}

if (hVal == 8) {
  if (answer == "call") {
    newTEXT = "Correct";
  } else {
    newTEXT = "Incorrect.  You should Call";
  }
}

if (hVal < 8) {
  if (answer == "fold") {
    newTEXT = "Correct";
  } else {
    newTEXT = "Incorrect.  You should Fold";
  }
}

print(eresult, newTEXT);

}


function answerMPQuiz(hVal, form) {

var answer = getRadioValue(form.middle);
var newTEXT;

if (hVal >= 9) {
  if (answer == "raise") {
    newTEXT = "Correct";
  } else {
    newTEXT = "Incorrect.  You should Raise";
  }
}

if ( (hVal >= 7) && (hVal <9) ) {
  if (answer == "call") {
    newTEXT = "Correct";
  } else {
    newTEXT = "Incorrect.  You should Call";
  }
}

if (hVal < 7) {
  if (answer == "fold") {
    newTEXT = "Correct";
  } else {
    newTEXT = "Incorrect.  You should Fold";
  }
}

print(mresult, newTEXT);

}

function answerLPQuiz(hVal, form) {

var answer = getRadioValue(form.late);
var newTEXT;

if (hVal >= 9) {
  if (answer == "raise") {
    newTEXT = "Correct";
  } else {
    newTEXT = "Incorrect.  You should Raise";
  }
}

if ((hVal >= 6)  && (hVal <9) ) {
  if (answer == "call") {
    newTEXT = "Correct";
  } else {
    newTEXT = "Incorrect.  You should Call";
  }
}

if (hVal < 6) {
  if (answer == "fold") {
    newTEXT = "Correct";
  } else {
    newTEXT = "Incorrect.  You should Fold";
  }
}

print(lresult, newTEXT);

}

/***************************************************************************
** Function:  	getRank()
** Purpose:   	Random Generator
** Parameters:	none
** Returns:	Card Rank
***************************************************************************/

function getRank(){
var Rank = (Math.round((Math.random() * 13)));
while (Rank == 0) {
  Rank = (Math.round((Math.random() * 13)));
}
return Rank;
}

/***************************************************************************
** Function:  	getSuit()
** Purpose:   	Random Generator
** Parameters:	none
** Returns:	Card Suit
***************************************************************************/

function getSuit() {
var Suit = (Math.round((Math.random() * 4)));
while (Suit ==0) {
  var Suit = (Math.round((Math.random() * 4)));
}
return Suit;
}

/***************************************************************************
** Function:  	getHandValue()
** Purpose:   	Determine's hand's value based upon Chen's formula
** Parameters:	rank1, suit1, rank2, suit2
**		Rank and Suit of each card (1 and 2)
** Returns:	Hand Value
***************************************************************************/

function getHandValue(rank1, suit1, rank2, suit2) {
var Value
var TempVal
var Diff

rank1 = (rank1-0);
rank2 = (rank2-0);

if (rank2 > rank1) {
  //Swap so rank1 is always the highest card
  TempVal = rank1;
  rank1 = rank2;
  rank2 = TempVal;
}

rank1++;
rank2++;

Diff = rank1 - rank2;

if (rank1 <= 10) {
  Value = rank1 / 2;
} else {
  if (rank1 == 11) {
    Value = 6;
  }
  if (rank1 == 12) {
    Value = 7;
  }
  if (rank1 == 13) {
    Value = 8;
  }
  if (rank1 == 14) {
    Value = 10;
  }
}

if (rank1 == rank2) {
  Value = Value * 2;
  if (Value < 5) {
    return 5;
  } else {
    return Value;
  }
}

if (suit1 == suit2) {
  Value += 2;
}

if (Diff == 1) {
  Value++;
}

if ( (Diff == 2) && (rank1 >= 11) ) {
  Value--;
}

if (Diff == 3) {
  Value -= 2;
}

if (Diff == 4) {
  Value -= 4;
}

if (Diff >= 5) {
  Value -= 5;
}
return Value;
}



/***************************************************************************
** Function:  	print()
** Purpose:   	Random Generator
** Parameters:	ID: ID of the Div where HTML is to be printed
**    		txt: String of HTML to be printed
** Returns:	nothing
***************************************************************************/

function print(idDiv, txt) {
idDiv.innerHTML=txt;
}


/***************************************************************************
** Function:  	cardsNotValid()
** Purpose:   	Error Checker.  Determines if cards are Identical
** Parameters:	r1, s1, r2, s2
**    		The parameters are the rank and suit of 2 cards
** Returns:	1 if true (cards are identical)
**		0 if false (Cards are OK)
***************************************************************************/

function cardsNotValid(r1, s1, r2, s2) {

if ( (r1 == r2) && (s1 == s2) ) {
 return(1);
}
return(0);
}

/***************************************************************************
** Function:  	getRadioValue()
** Purpose:   	Determines choice of Radio Button Selection
** Parameters:	Radio Form Object (document.form.Radio)
**    		The parameters are the rank and suit of 2 cards
** Returns:	1 if true (cards are identical)
**		0 if false (Cards are OK)
***************************************************************************/

function getRadioValue(radio) {
var index;
var newHTML;

for (index=0; index<radio.length; index++)
{ if (radio[index].checked) {
    return radio[index].value; }
  }

return null;
}

/***************************************************************************
** Function:  	determineValue()
** Purpose:   	Determines of a hand.
** Parameters:	Radio Form Object (document.form.Radio)
**    		The parameters are the rank and suit of 2 cards
** Returns:	1 if true (cards are identical)
**		0 if false (Cards are OK)
***************************************************************************/

function determineValue(idDiv, form) {
var Value;
var TempVal;
var Diff;
var rank1 = getRadioValue(form.Rank1);
var suit1 = getRadioValue(form.Suit1);
var rank2 = getRadioValue(form.Rank2);
var suit2 = getRadioValue(form.Suit2);

if ( (rank1 == null) || (suit1 == null) || (rank2 == null) || (suit2 == null) ) {
  print(idDiv, "Please choose a Rank and Suit for each Card");
  return;
}

rank1 = (rank1-0);
rank2 = (rank2-0);

if (rank2 > rank1) {
  //Swap so rank1 is always the highest card
  TempVal = rank1;
  rank1 = rank2;
  rank2 = TempVal;
}

if (cardsNotValid(rank1, suit1, rank2, suit2)) {
  print(idDiv, "Cards Cannot be Identical.  Please choose distinct Cards");
  return;
}

Diff = rank1 - rank2;

if (rank1 <= 10) {
  Value = rank1 / 2;
} else {
  if (rank1 == 11) {
    Value = 6;
  }
  if (rank1 == 12) {
    Value = 7;
  }
  if (rank1 == 13) {
    Value = 8;
  }
  if (rank1 == 14) {
    Value = 10;
  }
}

if (rank1 == rank2) {
  Value = Value * 2;
  if (Value < 5) {
    chgText(idDiv, "5");
    return;
  } else {
    chgText(idDiv, Value);
    return;
  }
}

if (suit1 == suit2) {
  Value += 2;
}

if (Diff == 1) {
  Value++;
}

if ( (Diff == 2) && (rank1 >= 11) ) {
  Value--;
}

if (Diff == 3) {
  Value -= 2;
}

if (Diff == 4) {
  Value -= 4;
}

if (Diff >= 5) {
  Value -= 5;
}
chgText(idDiv, Value);
}


/***************************************************************************
** Function:  	chgText()
** Purpose:   	Changes the Text of defined DIV
** Parameters:	Radio Form Object (document.form.Radio)
**    		The parameters are the rank and suit of 2 cards
***************************************************************************/

function chgText(idDiv, Val) {
newHTML = "Hand Value is: ";
newHTML += Val + "<br>Early Position: ";

if (Val >= 9) {
  newHTML += "Raise<br>";
}


if (Val == 8) {
  newHTML += "Call<br>";
}

if (Val < 8) {
  newHTML += "Fold<br>";
}

newHTML += "Middle Position: ";

if (Val >= 9) {
  newHTML += "Raise<br>";
}

if ( (Val >= 7) && (Val < 9) ){
  newHTML += "Call<br>";
}

if (Val < 7) {
  newHTML += "Fold<br>";
}

newHTML += "Late Position: ";

if (Val >= 9) {
  newHTML += "Raise<br>";
}

if ( (Val >= 6) && (Val < 9) ){
  newHTML += "Call<br>";
}

if (Val < 6) {
  newHTML += "Fold<br>";
}
print(idDiv, newHTML);
}
