<!-- Begin

/*****************************************************************************
  This routine is the basic program loop for the criteria screen. It's
  invoked by the operator hitting the "Smack Me" key.
  	  PostValues   = Add the values entered on the screen to the Hand arrays
	  EditValues   = Check the entered values for consistency
	  Adjust       = Changes max values based on minimums.
	  ExoticErrors = Checks for errors like 7-6-0-0 dist and 21 HCP
	  new_deal     = Generate a hand based on the hand arrays.
	  hand_display = Display the hand
*****************************************************************************/
function StartCriteria()
	{
	PostValues();
	if ( EditValues() )
		return;
	Adjust();
	if ( ExoticErrors() )
		return;
	NewDeal();
	frames["page"].location = "HandDisp.htm";
	}

/*****************************************************************************
  This routine is the basic program loop for the Hand display screen. It's
  invoked by the Operator hitting the "Deal" key.
	  new_deal     = Generate a hand based on the hand arrays.
	  hand_display = Display the hand
*****************************************************************************/
function StartHand()
	{
	NewDeal();
	HandDisplay(4);
	}

/*****************************************************************************
  This routine is invoked when the Criteria display is loaded. It moves values
  from the Hand Arrays into the screen fields.
*****************************************************************************/
function NewCriteria()
	{
	frames["page"].document.numbers.NhcpMin.value = North.hcpmin;
	frames["page"].document.numbers.NhcpMax.value = North.hcpmax;

	frames["page"].document.numbers.NspMin.value = North.mins[S];
	frames["page"].document.numbers.NspMax.value = North.maxs[S];
	frames["page"].document.numbers.NhtMin.value = North.mins[H];
	frames["page"].document.numbers.NhtMax.value = North.maxs[H];
	frames["page"].document.numbers.NdiMin.value = North.mins[D];
	frames["page"].document.numbers.NdiMax.value = North.maxs[D];
	frames["page"].document.numbers.NclMin.value = North.mins[C];
	frames["page"].document.numbers.NclMax.value = North.maxs[C];

	frames["page"].document.numbers.EhcpMin.value = East.hcpmin;
	frames["page"].document.numbers.EhcpMax.value = East.hcpmax;

	frames["page"].document.numbers.EspMin.value = East.mins[S];
	frames["page"].document.numbers.EspMax.value = East.maxs[S];
	frames["page"].document.numbers.EhtMin.value = East.mins[H];
	frames["page"].document.numbers.EhtMax.value = East.maxs[H];
	frames["page"].document.numbers.EdiMin.value = East.mins[D];
	frames["page"].document.numbers.EdiMax.value = East.maxs[D];
	frames["page"].document.numbers.EclMin.value = East.mins[C];
	frames["page"].document.numbers.EclMax.value = East.maxs[C];

	frames["page"].document.numbers.ShcpMin.value = South.hcpmin;
	frames["page"].document.numbers.ShcpMax.value = South.hcpmax;

	frames["page"].document.numbers.SspMin.value = South.mins[S];
	frames["page"].document.numbers.SspMax.value = South.maxs[S];
	frames["page"].document.numbers.ShtMin.value = South.mins[H];
	frames["page"].document.numbers.ShtMax.value = South.maxs[H];
	frames["page"].document.numbers.SdiMin.value = South.mins[D];
	frames["page"].document.numbers.SdiMax.value = South.maxs[D];
	frames["page"].document.numbers.SclMin.value = South.mins[C];
	frames["page"].document.numbers.SclMax.value = South.maxs[C];

	frames["page"].document.numbers.WhcpMin.value = West.hcpmin;
	frames["page"].document.numbers.WhcpMax.value = West.hcpmax;

	frames["page"].document.numbers.WspMin.value = West.mins[S];
	frames["page"].document.numbers.WspMax.value = West.maxs[S];
	frames["page"].document.numbers.WhtMin.value = West.mins[H];
	frames["page"].document.numbers.WhtMax.value = West.maxs[H];
	frames["page"].document.numbers.WdiMin.value = West.mins[D];
	frames["page"].document.numbers.WdiMax.value = West.maxs[D];
	frames["page"].document.numbers.WclMin.value = West.mins[C];
	frames["page"].document.numbers.WclMax.value = West.maxs[C];
	}

/*****************************************************************************
  Initializes the constant values and posts the values retrieved from the
  initial screen into the hand arrays. The minus zero on the end of these is a
  sneaky way to convert strings to numbers.
*****************************************************************************/
function PostValues()
	{
	North.hcpmin = frames["page"].document.numbers.NhcpMin.value - 0;
	North.hcpmax = frames["page"].document.numbers.NhcpMax.value - 0;

	North.mins[S] = frames["page"].document.numbers.NspMin.value - 0;
	North.maxs[S] = frames["page"].document.numbers.NspMax.value - 0;
	North.mins[H] = frames["page"].document.numbers.NhtMin.value - 0;
	North.maxs[H] = frames["page"].document.numbers.NhtMax.value - 0;
	North.mins[D] = frames["page"].document.numbers.NdiMin.value - 0;
	North.maxs[D] = frames["page"].document.numbers.NdiMax.value - 0;
	North.mins[C] = frames["page"].document.numbers.NclMin.value - 0;
	North.maxs[C] = frames["page"].document.numbers.NclMax.value - 0;

	East.hcpmin = frames["page"].document.numbers.EhcpMin.value - 0;
	East.hcpmax = frames["page"].document.numbers.EhcpMax.value - 0;

	East.mins[S] = frames["page"].document.numbers.EspMin.value - 0;
	East.maxs[S] = frames["page"].document.numbers.EspMax.value - 0;
	East.mins[H] = frames["page"].document.numbers.EhtMin.value - 0;
	East.maxs[H] = frames["page"].document.numbers.EhtMax.value - 0;
	East.mins[D] = frames["page"].document.numbers.EdiMin.value - 0;
	East.maxs[D] = frames["page"].document.numbers.EdiMax.value - 0;
	East.mins[C] = frames["page"].document.numbers.EclMin.value - 0;
	East.maxs[C] = frames["page"].document.numbers.EclMax.value - 0;

	South.hcpmin = frames["page"].document.numbers.ShcpMin.value - 0;
	South.hcpmax = frames["page"].document.numbers.ShcpMax.value - 0;

	South.mins[S] = frames["page"].document.numbers.SspMin.value - 0;
	South.maxs[S] = frames["page"].document.numbers.SspMax.value - 0;
	South.mins[H] = frames["page"].document.numbers.ShtMin.value - 0;
	South.maxs[H] = frames["page"].document.numbers.ShtMax.value - 0;
	South.mins[D] = frames["page"].document.numbers.SdiMin.value - 0;
	South.maxs[D] = frames["page"].document.numbers.SdiMax.value - 0;
	South.mins[C] = frames["page"].document.numbers.SclMin.value - 0;
	South.maxs[C] = frames["page"].document.numbers.SclMax.value - 0;

	West.hcpmin = frames["page"].document.numbers.WhcpMin.value - 0;
	West.hcpmax = frames["page"].document.numbers.WhcpMax.value - 0;

	West.mins[S] = frames["page"].document.numbers.WspMin.value - 0;
	West.maxs[S] = frames["page"].document.numbers.WspMax.value - 0;
	West.mins[H] = frames["page"].document.numbers.WhtMin.value - 0;
	West.maxs[H] = frames["page"].document.numbers.WhtMax.value - 0;
	West.mins[D] = frames["page"].document.numbers.WdiMin.value - 0;
	West.maxs[D] = frames["page"].document.numbers.WdiMax.value - 0;
	West.mins[C] = frames["page"].document.numbers.WclMin.value - 0;
	West.maxs[C] = frames["page"].document.numbers.WclMax.value - 0;
	}

/*****************************************************************************
  Edits the retrieved values and outputs error messages.
*****************************************************************************/
function EditValues ()
	{
	/**************************************************************************
	  Start by checking each individual field.
	**************************************************************************/
	if ( North.Edit() )
		{
		alert( "Error. North hand.\n" + North.errmsg + "\nPlease re-enter.");
		return (-1);
		}
	
	if ( East.Edit() )
		{
		alert( "Error. East hand.\n" + East.errmsg + "\nPlease re-enter.");
		return (-1);
		}

	if ( South.Edit() )
		{
		alert( "Error. South hand.\n" + South.errmsg + "\nPlease re-enter.");
		return (-1);
		}
	
	if ( West.Edit() )
		{
		alert( "Error. West hand.\n" + West.errmsg + "\nPlease re-enter.");
		return (-1);
		}
		
	/**************************************************************************
	  Finally, check all four hands combined.
	**************************************************************************/
	if (North.hcpmin + East.hcpmin + South.hcpmin + West.hcpmin > 40 )
		{
		alert("Error. Four hands combined.\n"    +
				 "More than a 40 HCP minimum.\n" +
				 "Please Re-enter.");
		return(-1);
		}

	if (North.mins[S] + East.mins[S] + South.mins[S] + West.mins[S] > 13 )
		{
		alert("Error. Four hands combined.\n"     +
				"More than a 13 Spade minimum.\n" +
				"Please Re-enter.");
		return(-1);
		}

	if (North.mins[H] + East.mins[H] + South.mins[H] + West.mins[H] > 13 )
		{
		alert("Error. Four hands combined.\n"     +
				"More than a 13 Heart minimum.\n" +
				"Please Re-enter.");
		return(-1);
		}

	if (North.mins[D] + East.mins[D] + South.mins[D] + West.mins[D] > 13 )
		{
		alert("Error. Four hands combined.\n"     +
				"More than 13 Diamond minimum.\n" +
				"Please Re-enter.");
		return(-1);
		}

	if (North.mins[C] + East.mins[C] + South.mins[C] + West.mins[C] > 13 )
		{
		alert("Error. Four hands combined.\n"    +
				"More than a 13 Club minimum.\n" +
				"Please Re-enter.");
		return(-1);
		}

	if (North.maxs[S] + East.maxs[S] + South.maxs[S] + West.maxs[S] < 13 )
		{
		alert("Error. Four hands combined.\n"     +
				"Less than a 13 Spade maximum.\n" +
				"Please Re-enter.");
		return(-1);
		}

	if (North.maxs[H] + East.maxs[H] + South.maxs[H] + West.maxs[H] < 13 )
		{
		alert("Error. Four hands combined.\n"     +
				"Less than a 13 Heart maximum.\n" +
				"Please Re-enter.");
		return(-1);
		}

	if (North.maxs[D] + East.maxs[D] + South.maxs[D] + West.maxs[D] < 13 )
		{
		alert("Error. Four hands combined.\n"     +
				"Less than 13 Diamond maximum.\n" +
				"Please Re-enter.");
		return(-1);
		}

	if (North.maxs[C] + East.maxs[C] + South.maxs[C] + West.maxs[C] < 13 )
		{
		alert("Error. Four hands combined.\n"    +
				"Less than a 13 Club maximum.\n" +
				"Please Re-enter.");
		return(-1);
		}

	return (0);
	}

/*******************************************************************************
  This routine adjust maximum values on the basis of minimums entered. Example,
  if a hand requests a minimum of 7 spades and 6 hearts, its maximum minor suit
  length is zero and its maximum major suit lengths are 7 and 6.
*******************************************************************************/
function Adjust ()
	{
	var maxValue = 0;
	var Xsuit = 0;
	var Xhand = 0;

	/************************************************************************
	 Set maxs based on total suit mins for all 4 hands.
	************************************************************************/
	for (Xsuit = 0; Xsuit < 4; Xsuit++)
		{
		maxValue = 13 	- North.mins[Xsuit] - East.mins[Xsuit]
						- South.mins[Xsuit]	- West.mins[Xsuit];

		for (Xhand = 0; Xhand < 4; Xhand++)
			{
			Sethand("A", Xhand);
			
			if (Ahand.maxs[Xsuit] > Ahand.mins[Xsuit] + maxValue)
				Ahand.maxs[Xsuit] = Ahand.mins[Xsuit] + maxValue;
			}/* End of Xhand for loop */
		}/* End of Xsuit for loop */

	/************************************************************************
	 Set maxs within an individual hand.
	************************************************************************/
	for (Xhand = 0; Xhand < 4; Xhand++)
		{
		Sethand("A", Xhand);

		maxValue = 13 - Ahand.mins[S] - Ahand.mins[H] - Ahand.mins[D] - Ahand.mins[C];
		
		for (Xsuit = 0; Xsuit < 4; Xsuit++)
			{
			if (Ahand.maxs[Xsuit] > Ahand.mins[Xsuit] + maxValue)
				Ahand.maxs[Xsuit] = Ahand.mins[Xsuit] + maxValue;
			}  /* End of Xhand for loop */
		}  /* End of Xsuit for loop */
		
	/************************************************************************
	 Set maxs HCP.
	************************************************************************/
	maxValue = 40 - North.hcpmin - East.hcpmin - South.hcpmin - West.hcpmin;
	for (Xhand = 0; Xhand < 4; Xhand++)
		{
		Sethand("A", Xhand);
			
		if (Ahand.hcpmax > Ahand.hcpmin + maxValue)
			Ahand.hcpmax = Ahand.hcpmin + maxValue;
		}  /* End of Xhand for loop */

	}  /* End of Adjust */


/*****************************************************************************
  Edits for impossible combinations of HCP and distribution.
*****************************************************************************/
function ExoticErrors ()
	{
	for (Xhand = 0; Xhand < 4; Xhand++)
		{
		Sethand("A", Xhand );
		var tot = 0;

		for (Xsuit = 0; Xsuit < 4; Xsuit++)
			{
			if (Ahand.maxs[Xsuit] >= 4)
				tot += 10
			else if (Ahand.maxs[Xsuit] == 3)
				tot += 9
			else if (Ahand.maxs[Xsuit] == 2)
				tot += 7
			else if (Ahand.maxs[Xsuit] == 1)
				tot += 4;
			}  /* End of Xsuit for loop */

		if (Ahand.hcpmin > tot)
			{
			alert(Ahand.dir + " hand.\n" +
					"Can't put " + Ahand.hcpmin + " HCP into this distribution.\n" +
					"Please re-enter.");
			return(-1);
			}
		} /* End of Xhand for loop */
	} /* End of ExoticErrors */
-->
