  /************************************************************************/
  // Variables
  // Methods
  XactikaBoard.prototype.initCards     = XactikaBoard_loadCards;
  XactikaBoard.prototype.cardClicked   = XactikaBoard_cardClicked;
  XactikaBoard.prototype.win           = XactikaBoard_win;
  XactikaBoard.prototype.lose          = XactikaBoard_lose;
  XactikaBoard.prototype.paint         = XactikaBoard_paint;
  XactikaBoard.prototype.loadCards     = XactikaBoard_loadCards;
  XactikaBoard.prototype.playShape     = XactikaBoard_playShape;
  XactikaBoard.prototype.nextRound     = XactikaBoard_nextRound;
  XactikaBoard.prototype.clearCards    = XactikaBoard_clearCards;
  XactikaBoard.prototype.undo          = XactikaBoard_undo;
  XactikaBoard.prototype.randomCards   = XactikaBoard_randomCards;
  XactikaBoard.prototype.setCommentary = XactikaBoard_setCommentary;
  XactikaBoard.prototype.syncCardImageNumber = XactikaBoard_syncCardImageNumber;

  //***********************************************************************/
  function XactikaBoard(zeroPoint, winner)
  {
		this.winner = winner;
    this.syncCardImageNumber();
    this.emptyCard =  "../images/xactika_cards/medium/xactika_card_empty.jpg";
    this.computer = new XactikaHand(this.emptyCard, this);
    this.player = new XactikaHand(this.emptyCard, this);
    this.cardsPlayed = new Array();
    this.shapePlayed = null;
  }
	//***********************************************************************/
	function XactikaBoard_syncCardImageNumber()
	{
//		alert("document.images.length = " + document.images.length);
		for (var i = 0; i < document.images.length; i++)
		{
			if (document.images[i].name == "card1")
			{
				this.zeroPoint = i;
//				alert("zeroPoint = " + this.zeroPoint);
				break;
			}
			else
			{
//				alert("name = '" + document.images[i].name + "'");
			}
		}
	}
  
  //***********************************************************************/
  function XactikaBoard_randomCards()
  {
    var mid = 8;
    this.computer = new XactikaHand(this.emptyCard, this);
    this.player = new XactikaHand(this.emptyCard, this);
    this.computer.setZeroPoint(this.zeroPoint);
    this.computer.setPlaySpace(this.zeroPoint + mid);
    this.player.setPlaySpace(this.zeroPoint + mid + 1);
    this.player.setZeroPoint(this.zeroPoint + mid + 1 + 4 + 1);
    this.shapePlayed = null;
    this.cardsPlayed = new Array();
    var cards = new Array();
    while(cards.length < 16)
    {
    	var rnd = Math.random();
      rnd = (rnd * 81);
      rnd = Math.round(rnd);
      rnd = rnd % 81;
      rnd++;
      //Check if unique
      var unique = true;
      for(var x = 0; (x < cards.length) && (unique); x++)
      {
        unique = cards[x] != rnd;
      }
      if (unique)
      {
        if (cards.length < mid)
        {
          this.computer.addCard(new XactikaCard(rnd));
        }
        else
        {
          this.player.addCard(new XactikaCard(rnd));
        }
        cards[cards.length] = rnd;
      }
    } 
//    alert(cards.length);
    this.paint();
  }
  //***********************************************************************/
  function XactikaBoard_loadCards()
  {
    var mid = arguments.length/2;
    this.computer.setZeroPoint(this.zeroPoint);
    this.computer.setPlaySpace(this.zeroPoint + mid);
    this.player.setPlaySpace(this.zeroPoint + mid + 1);
    this.shapeManager = new XactikaShapes(this.zeroPoint + mid + 1 + 1, "../images/xactika_cards/medium/shapes/empty.gif");
    this.player.setZeroPoint(this.zeroPoint + mid + 1 + 4 + 1);
    
  	for(var i = 0; i < arguments.length; i++)
  	{
      if (i < mid)
      {
  	    this.computer.addCard(new XactikaCard(arguments[i]));
      }
      else
      {
        this.player.addCard(new XactikaCard(arguments[i]));
      }
    }      
  }

  //***********************************************************************/
  function XactikaBoard_paint()
  {
    this.computer.paint();
    this.player.paint();
    //paint options
    if (this.player.isState(XactikaHand.WAITING_FOR_SHAPE))
    {
    	this.shapeManager.setImage(XactikaCard.BALLS,this.cardsPlayed[0].getBalls()); 
      this.shapeManager.setImage(XactikaCard.CUBES,this.cardsPlayed[0].getCubes()); 
      this.shapeManager.setImage(XactikaCard.CONES,this.cardsPlayed[0].getCones()); 
      this.shapeManager.setImage(XactikaCard.STARS,this.cardsPlayed[0].getStars()); 
    }
    else
    {
      this.shapeManager.setImage(XactikaCard.BALLS,0); 
      this.shapeManager.setImage(XactikaCard.CUBES,0); 
      this.shapeManager.setImage(XactikaCard.CONES,0); 
      this.shapeManager.setImage(XactikaCard.STARS,0); 
      if(this.shapePlayed  != null)
      {
        this.shapeManager.setImage(this.shapePlayed.shape,this.shapePlayed.number); 
      }
    }
  }
  //***********************************************************************/
  function XactikaBoard_cardClicked(place)
  {
    //alert(this.player.getCard(place).toMyString());
    var card = null;
    card = this.player.leadCard(place);
    if (card != null)
    {
    	this.shapePlayed = null;
      this.cardsPlayed[0] = card; 
    }
    this.paint();
  }
  //***********************************************************************/
  function XactikaBoard_playShape(shape)
  {
    var combo = null;
    combo = this.player.playShape(shape);
    if (combo != null)
    {
      this.shapePlayed = combo;
      this.cardsPlayed[1] = this.computer.playCard(this.cardsPlayed[0],combo);
      this.paint();
      this.nextRound();
    }
  }
  //***********************************************************************/
  function XactikaBoard_nextRound()
  {
    // Check If Lost	
    if ((this.cardsPlayed[0].getPoints() <= this.cardsPlayed[1].getPoints()) && 
        this.shapePlayed.isCardValid(this.cardsPlayed[1]))
    {
      this.lose(this.cardsPlayed[0].getPoints() == this.cardsPlayed[1].getPoints());
    }
		else if (!this.shapePlayed.isCardValid(this.cardsPlayed[1]))
		{
			this.setCommentary("The computer sloughed.\nYou win the trick!\n\nPick another card.");
		}
		else 
    {
    	this.setCommentary("You win with the highest card.\n\nPick another card.");
    }
    this.player.nextRound();
    this.computer.nextRound();
    if(this.player.isState(XactikaHand.GAME_OVER))
    {
      this.win();
    }
  }

  //***********************************************************************/
  function XactikaBoard_clearCards()
  {
    if (this.player.isState(XactikaHand.WAITING_FOR_CARD))
    {
      this.shapePlayed = null;
      this.paint();
    }
  }  

  //***********************************************************************/
  function XactikaBoard_undo()
  {
  	this.player.undo();
    this.computer.undo(this.player.getRound());
    this.paint();
  }  

  //***********************************************************************/  
  function XactikaBoard_win()
  {
    this.setCommentary("YOU WIN!");
		this.winner();
  }
  //***********************************************************************/  
  function XactikaBoard_lose(equal)
  {
  	this.player.lose();
    this.computer.win();
		if (equal)
		{
  		this.setCommentary("YOU LOST!\nRemember, ties go to the LAST card played\nPress Restart to try again...");
		}
		else
		{
			this.setCommentary("YOU LOST!\nPress Restart to try again...");
		}
  }
  //***********************************************************************/  
  function XactikaBoard_setCommentary(commentary)
  {
  	document.input.commentary.value = commentary;
  }

  
  //***********************************************************************/
  //***********************************************************************/
  /************************************************************************/
  /************************************************************************/
  XactikaShapes.prototype.getURL   = XactikaShapes_getURL;
  XactikaShapes.prototype.getImage = XactikaShapes_getImage;
  XactikaShapes.prototype.setImage = XactikaShapes_setImage;
  /**
    * Small class for the shape and Number Combination
    *
    **/
  function XactikaShapes(zeroPoint,emptyCard)
  {
    this.zeroPoint = zeroPoint;
    this.emptyCard = emptyCard;
  }
  /************************************************************************/
  function XactikaShapes_getURL(shape, number)
  {
    var text = null;
    if(number == 0)
    {
    	text = this.emptyCard;
    }
    else
    {
    	text = "../images/xactika_cards/medium/shapes/" + 
    	       XactikaCard.getShapeText(shape).toLowerCase() + 
    	       "_" + number + ".jpg";

    }
    return text;
  }
  /************************************************************************/
  function XactikaShapes_getImage(shape)
  {
    
    var number = 0;
    switch (shape)
    {
      case XactikaCard.BALLS : break;
      case XactikaCard.CUBES : number += 1;break;
      case XactikaCard.CONES : number += 2;break;
      case XactikaCard.STARS : number += 3;break;

    }
    return document.images[this.zeroPoint + number];
  }
  /************************************************************************/
  function XactikaShapes_setImage(shape, number)
  {
    this.getImage(shape).src = this.getURL(shape,number);	
  }
  
  /************************************************************************/
  /************************************************************************/  
