// $Id: commands.js,v 1.1 2009/02/18 19:27:41 kgarcia Exp $

// these functions interact with the server
  function disableButtons()
  {
    getObject("btnUndo").disabled = true;
    getObject("btnDraw").disabled = true;
    getObject("btnResign").disabled = true;
  }

  function undo()
  {
	/** kj changes -  **/
        if (confirm("Are you sure you want to undo? \n\nOK=Yes - Cancel=No")){
    disableButtons();
    document.gamedata.requestUndo.value = "yes";
    if (DEBUG)
      alert("gamedata.requestUndo = " + document.gamedata.requestUndo.value);

    document.gamedata.submit();
	}else{
		//do nothing
	}
  }

  function draw()
  {
	/** kj changes -  **/
        if (confirm("Are you sure you want to undo? \n\nOK=Yes - Cancel=No")){
    disableButtons();
    document.gamedata.requestDraw.value = "yes";
    if (DEBUG)
      alert("gamedata.requestDraw = " + document.gamedata.requestDraw.value);

    document.gamedata.submit();
	}else{
		//do nothing
	}
  }

  function resigngame()
  {
	/** kj changes -  **/
        if (confirm("Are you sure you want to resign? \n\nOK=Yes - Cancel=No")){
    disableButtons();
    document.gamedata.resign.value = "yes";
    if (DEBUG)
      alert("gamedata.resign = " + document.gamedata.resign.value);

    document.gamedata.submit();
	}else{
		//do nothing
	}
  }

  function displayMainmenu()
  {
    this.disabled = true;
    disableButtons();
    window.open('?q=chess', '_self');
  }

  function reloadPage(btnReload)
  {
    btnReload.disabled = true;
    disableButtons();
    window.open('?q=chess/game', '_self');  
  }

  function downloadPGN()
  {
    window.open('?q=chess/openpgn', '_self')
  }
  
  function logout()
  {
    document.gamemenu.action = "mainmenu.php";
    document.gamemenu.submit();
  }

  function promotepawn()
  {
    var blackPawnFound = false;
    var whitePawnFound = false;
    var i = -1;
    while (!blackPawnFound && !whitePawnFound && i < 8)
    {
      i++;
      
      /* check for black pawn being promoted */
      if (board[0][i] == (BLACK | PAWN))
        blackPawnFound = true;
      
      /* check for white pawn being promoted */
      if (board[7][i] == (WHITE | PAWN))
        whitePawnFound = true;
    }

    /* to which piece is the pawn being promoted to? */
    var promotedTo = 0;
    for (var j = 0; j <= 3; j++)
    {
      if (document.gamedata.promotion[j].checked)
        promotedTo = parseInt(document.gamedata.promotion[j].value);
    }

    /* change pawn to promoted piece */
    var ennemyColor = "black";
    if (blackPawnFound)
    {
      ennemyColor = "white";
      board[0][i] = (BLACK | promotedTo);
      
      if (DEBUG)
        alert("Promoting to: (black) " + board[0][i]);

    }
    else if (whitePawnFound)
    {
      board[7][i] = (WHITE | promotedTo);
      
      if (DEBUG)
        alert("Promoting to: (white) " + board[7][i]);
    }
    else
      alert("WARNING!: cannot find pawn being promoted!");
      
    /* verify check and checkmate status */
    if (isInCheck(ennemyColor))
    {
      if (DEBUG)
        alert("Promotion results in check!");

      document.gamedata.isInCheck.value = "true";
      document.gamedata.isCheckMate.value = isCheckMate(ennemyColor);
    }
    else
      document.gamedata.isInCheck.value = "false";
    document.gamedata.fromRow.value = chessHistory[numMoves][FROMROW];
    document.gamedata.fromCol.value = chessHistory[numMoves][FROMCOL];
    document.gamedata.toRow.value = chessHistory[numMoves][TOROW];
    document.gamedata.toCol.value = chessHistory[numMoves][TOCOL];

    /* update board and database */
    document.gamedata.submit();
  }

