<!-- Begin

/*************************************************************************
   These Arrays contain all the info regarding a given hand.
*************************************************************************/
var  North = new Hand( "North");
var  South = new Hand( "South");
var  East = new Hand( "East");
var  West = new Hand( "West");

/*************************************************************************
 Following is the "constructor" for the hand arrays. It creates a default
 Hand array containing criteria for a "hand record" type deal, working
 storage values for the new_deal routine to use, and a couple of useful
 methods.

 Criteria:
 	hcpmin = Minimum HCP
	hcpmax = Maximum HCP
    mins   = Minimum suit lengths
	maxs   = Maximum suit lengths

 Working storage:
    suits  = The cards dealt are put here.
	counts = Number of each suit currently dealt.
	hcp    = Number of HCP this hand currently has.
	slots  = Number of open spaces in the suit arrays.
	
 Methods:
    debug    	  = Displays the Hand array using alerts.
	ClearIt  	  = Initializes working storage values.
	HandFull 	  = Returns true if the hand has 13 cards, false if less.
	Edit     	  = Checks criteria values for validity
	arrayTOstring = Squeezes blanks out of the suit arrays and turns them
	                into strings.
*************************************************************************/
function Hand( direction)
	{
	this.dir = direction;
	this.criteria = DispCriteria;
	this.working = DispWorking;
	this.ClearIt = ZeroHand;
	this.HandFull = FullHand;
	this.Edit = EditCriteria;
	this.arrayTOstring = AtoS;

	this.hcp = 0;
	this.hcpmin = 0;
	this.hcpmax = 37;
	this.slots  = 13;

	this.suits = new Array( new Array(13),
							new Array(13),
							new Array(13),
							new Array(13) );

    this.mins   = new Array(0, 0, 0, 0);
	this.maxs   = new Array(13, 13, 13, 13);
	this.counts = new Array(0, 0, 0, 0);
	}

/***********************************************************************
  This initializes all the working fields of the Hand arrays, but leaves
  the criteria fields intact.
************************************************************************/
function ZeroHand()
	{
	this.hcp = 0;
	this.slots = 13;

	for (x = 0; x < 4; x++)
		{
		this.counts[x] = 0;
		this.suits[x] = new Array(13);

		for (y = 0; y < 13; y++)
			this.suits[x][y] = " ";
		}
	}
	
/**************************************************************************
  A debugging tool. Displays working values from the Hand arrays.
**************************************************************************/
function DispWorking ()
	{
	alert( this.dir + "  CurrentHCP = " + this.hcp);
			
	alert( this.dir +	"  S-" + this.suits[0] +
						"  H-" + this.suits[1] +
						"  D-" + this.suits[2] +
						"  C-" + this.suits[3] );
	}

/**************************************************************************
  A debugging tool. Displays criteria values from the Hand arrays.
**************************************************************************/
function DispCriteria ()
	{
	alert( this.dir + "  minHCP = " + this.hcpmin);
	alert( this.dir + "  maxHCP = " + this.hcpmax);
			
	alert( this.dir + 	"  Suit Minimums  = "
	 	+ this.mins[0] + "-" + this.mins[1] + "-" + this.mins[2] + "-" + this.mins[3] );
				
	alert( this.dir + 	"  Suit Maximums  = "
	 	+ this.maxs[0] + "-" + this.maxs[1] + "-" + this.maxs[2] + "-" + this.maxs[3] );
	}

/*******************************************************************************
  Adds the cards currently in each suit of a hand and compares the total to 13.
********************************************************************************/
function FullHand()
	{
	var x =   this.counts[0]
	     	+ this.counts[1]
	     	+ this.counts[2]
			+ this.counts[3];

	if ( x < 13 )
		return false
	else
		return true;
	}

/*******************************************************************************
  Checks the criteria fields for validity. Returns and error otherwise.
********************************************************************************/
function EditCriteria ()
	{
	/********************************************************************
	  Begin by checking each individual field for validity.
	********************************************************************/
	if (isNaN(this.hcpmin) || (this.hcpmin < 0) || (this.hcpmin > 37) )
		{
		this.errmsg = "HCP minimum out of range.";
		return(-1);
		}
		
	if (isNaN(this.hcpmax) || (this.hcpmax < 0) || (this.hcpmax > 37) )
		{
		this.errmsg = "HCP maximum out of range.";
		return(-1);
		}
		
	if (isNaN(this.mins[S]) || (this.mins[S] < 0) || (this.mins[S] > 13) )
		{
		this.errmsg = "Spade minimum out of range.";
		return(-1);
		}

	if (isNaN(this.maxs[S]) || (this.maxs[S] < 0) || (this.maxs[S] > 13) )
		{
		this.errmsg = "Spade maximum out of range.";
		return(-1);
		}

	if (isNaN(this.mins[H]) || (this.mins[H] < 0) || (this.mins[H] > 13) )
		{
		this.errmsg = "Heart minimum out of range.";
		return(-1);
		}

	if (isNaN(this.maxs[H]) || (this.maxs[H] < 0) || (this.maxs[H] > 13) )
		{
		this.errmsg = "Heart maximum out of range.";
		return(-1);
		}

	if (isNaN(this.mins[D]) || (this.mins[D] < 0) || (this.mins[D] > 13) )
		{
		this.errmsg = "Diamond minimum out of range.";
		return(-1);
		}

	if (isNaN(this.maxs[D]) || (this.maxs[D] < 0) || (this.maxs[D] > 13) )
		{
		this.errmsg = "Diamond maximum out of range.";
		return(-1);
		}

	if (isNaN(this.mins[C]) || (this.mins[C] < 0) || (this.mins[C] > 13) )
		{
		this.errmsg = "Club minimum out of range.";
		return(-1);
		}

	if (isNaN(this.maxs[C]) || (this.maxs[C] < 0) || (this.maxs[C] > 13) )
		{
		this.errmsg = "club maximum out of range.";
		return(-1);
		}
	
	/**************************
	  Now check max/min.
	**************************/
	if (this.hcpmax < this.hcpmin)
		{
		this.errmsg = "max HCP is less than min HCP.";
		return(-1);
		}

	if (this.maxs[S] < this.mins[S])
		{
		this.errmsg = "max Spades is less than min Spades.";
		return(-1);
		}

	if (this.maxs[H] < this.mins[H])
		{
		this.errmsg = "max Hearts is less than min Hearts.";
		return(-1);
		}

	if (this.maxs[D] < this.mins[D])
		{
		this.errmsg = "max Diamonds is less than min diamonds.";
		return(-1);
		}

	if (this.maxs[C] < this.mins[C])
		{
		this.errmsg = "max clubs is less than min Clubs.";
		return(-1);
		}

	if ( this.mins[S] + this.mins[H] + this.mins[D] + this.mins[C] > 13)
		{
		this.errmsg = "more than a 13 card minimum.";
		return(-1);
		}

	if (this.maxs[S] + this.maxs[H] + this.maxs[D] + this.maxs[C] < 13)
		{
		this.errmsg = "less than a 13 card maximum.";
		return(-1);
		}
	}

/******************************************************************************
	Squeeze blanks out of the suit arrays and convert them to strings.
******************************************************************************/
function AtoS ()
	{
	for (suit = 0; suit < 4; suit++)
		for (card = 0; card < 13; card++)
			{
			if ( this.suits[suit][card] == " ")
				for (xx = card+1; xx < 13; xx++)
					if (this.suits[suit][xx] != " ")
						{
						this.suits[suit][card] = this.suits[suit][xx];
						this.suits[suit][xx] = " ";
						break;						 						 
						}
			}


	for (suit = 0; suit < 4; suit++)
		if (this.suits[suit][0] == " ")
				this.suits[suit][0] = "-";

	for (suit = 0; suit < 4; suit++)
		this.suits[suit] = this.suits[suit].join("");
	}

-->
