// www.TANTRIX.hu - Tantrix tournament client side functions
// Copyright (c) 2002, Uniqsoft Ltd., info@uniqsoft.hu
// v1.0 - 1st Hungarian Champ
// v1.1 - 5th WTC Plate - country flag, group functions removed
// v1.2 - 6th WTC Plate - style modification, fix player1 withdraw

aTourPoint = [100,133,139,143,147,150,153,155,158,160,162,164,166,168,170,172,173,175,177,178,180,181,183,184,185,187,188,189,191,192,193,194,195,197,198,199,200];
aTourStat = new Array();
aTourStat[1] = [0,0];
aTourStat[2] = [0,0];
aTourStat[3] = [0,0];
aTourStat[4] = [0,0];
aTourStat[5] = [0,0];
aTourStat[6] = [0,0];
aTourStat[7] = [0,0];

defPlayerNick = 0;
defPlayerName = 1;
defPlayerMail = 2;
defPlayerColor1 = 3;
defPlayerColor2 = 4;
defPlayerWon = 5;
defPlayerEqual = 6;
defPlayerLost = 7;
defPlayerTP = 8;
defPlayerNat = 9;

function RoundInit( nRound, nPos, nGame ){
  aPlayers = eval( 'aPlayer' + nRound );
  aGames = eval( 'aGame' + nRound );

  for( h=0; h<nPos; h++ ){
    for( i=0; i<nGame; i++ ){
      aTourStat[nRound][0]++;
      if( aGames[h*10+i][2] == '' )
        aGames[h*10+i][2] = 'white';
      else
        aTourStat[nRound][1]++;
      if( aGames[h*10+i][3] == '' )
        aGames[h*10+i][3] = 'white';
      if( aGames[h*10+i][0] != 0 || aGames[h*10+i][1] != 0 ){
        nResult = aGames[h*10+i][0]-aGames[h*10+i][1];
        nAbsResult = Math.abs( nResult ) > 36 ? 36 : Math.abs( nResult );
        nTP = aTourPoint[nAbsResult];
        if( nResult == 0 && aGames[h*10+i][0] > 0 ){ // dontetlen
          aGames[h*10+i][7] = nTP;
          aGames[h*10+i][8] = 200-nTP;
          aPlayers[h*10][defPlayerEqual]++;
          aPlayers[h*10+1][defPlayerEqual]++;
        }
        else {
          if( nResult > 0 ){ // 1 nyert
            aGames[h*10+i][7] = nTP;
            aGames[h*10+i][8] = 200-nTP;
            aPlayers[h*10][defPlayerWon]++;
            aPlayers[h*10+1][defPlayerLost]++;
          }
          else{ // 1 vesztett
            aGames[h*10+i][7] = 200-nTP;
            aGames[h*10+i][8] = nTP;
            aPlayers[h*10][defPlayerLost]++;
            aPlayers[h*10+1][defPlayerWon]++;
          }
        }
        nPenalty = aGames[h*10+i][4]-14;
        if( nPenalty > 0 ){ // 1 ido bunti
          aGames[h*10+i][7] -= nPenalty*10;
          aGames[h*10+i][8] += nPenalty*10;
        }
        nPenalty = aGames[h*10+i][5]-14;
        if( nPenalty > 0 ){ // 2 ido bunti
          aGames[h*10+i][7] += nPenalty*10;
          aGames[h*10+i][8] -= nPenalty*10;
        }
        aPlayers[h*10][defPlayerTP] += aGames[h*10+i][7];
        aPlayers[h*10+1][defPlayerTP] += aGames[h*10+i][8];
      }
    }
  }
}

function RoundResult( nRound, nPos, nGame ){
  aPlayers = eval( 'aPlayer' + nRound );
  aGames = eval( 'aGame' + nRound );
  sDT = '</font></td>';
  lPlayer1Won = aPlayers[nPos*10][defPlayerTP] > aPlayers[nPos*10+1][defPlayerTP];
  lPlayer2Won = aPlayers[nPos*10+1][defPlayerTP] > aPlayers[nPos*10][defPlayerTP];

  document.write( '<table cellpadding="1" cellspacing="1" border="1"' );
  document.write( '<tr>' );
  sTmpNation = aPlayers[nPos*10][defPlayerNat];
//  document.write( '<td><a href="#' + nRound+nPos+nGame + '"><img src="../../flags/'+sTmpNation.toLowerCase()+'2.gif" height="16" border="0" alt="'+sTmpNation+'"></a></td>' ); // 1 flag
  document.write( '<td><a href="javascript:RoundGameDetail('+nRound+','+nPos+','+nGame+')"><img src="../../flags/'+sTmpNation.toLowerCase()+'2.gif" height="16" border="0" title="'+sTmpNation+'"></a></td>' ); // 1 flag
  sPlayerNick = lPlayer1Won ? '<b>'+aPlayers[nPos*10][0]+'</b>' : aPlayers[nPos*10][0];
  document.write( '<td align="center" width="70"><font size="-1">' + sPlayerNick + sDT ); // 1 nick
  for( i=0; i<nGame; i++ ){ // 1 point
    sFontColor = aGames[nPos*10+i][2] == 'yellow' ? 'black' : 'white';
    sPoint = aGames[nPos*10+i][0] > aGames[nPos*10+i][1] ? '<b>'+aGames[nPos*10+i][0]+'</b>' : aGames[nPos*10+i][0];
    document.write( '<td align="center" bgcolor="'+aGames[nPos*10+i][2]+'"><font size="-1" color="'+sFontColor+'">'+sPoint+sDT );
  }
  sTP = aPlayers[nPos*10][defPlayerTP]/10;
  sTP = sTP > nGame*10 ? '<font color="red">'+sTP+'</font>' : sTP;
  sTP = lPlayer1Won ? '<b>'+sTP+'</b>' : sTP;
  document.write( '<td align="center" width="25"><font size="-1">' + sTP + sDT ); // 1 TP
  document.write( '</tr>' );
  document.write( '<tr>' );
  sTmpNation = aPlayers[nPos*10+1][defPlayerNat];
//  document.write( '<td><a href="#' + nRound+nPos+nGame + '"><img src="../../flags/'+sTmpNation.toLowerCase()+'2.gif" height="16" border="0" alt="'+sTmpNation+'"></a></td>' ); // 2 flag
  document.write( '<td><a href="javascript:RoundGameDetail('+nRound+','+nPos+','+nGame+')"><img src="../../flags/'+sTmpNation.toLowerCase()+'2.gif" height="16" border="0" title="'+sTmpNation+'"></a></td>' ); // 2 flag
  sPlayerNick = lPlayer2Won ? '<b>'+aPlayers[nPos*10+1][0]+'</b>' : aPlayers[nPos*10+1][0];
  document.write( '<td align="center" width="70"><font size="-1">' + sPlayerNick + sDT ); // 2 nick
  for( i=0; i<nGame; i++ ){ // 2 point
    sFontColor = aGames[nPos*10+i][3] == 'yellow' ? 'black' : 'white';
    sPoint = aGames[nPos*10+i][1] > aGames[nPos*10+i][0] ? '<b>'+aGames[nPos*10+i][1]+'</b>' : aGames[nPos*10+i][1];
    document.write( '<td align="center" bgcolor="'+aGames[nPos*10+i][3]+'"><font size="-1" color="'+sFontColor+'">'+sPoint+sDT );
  }
  sTP = aPlayers[nPos*10+1][defPlayerTP]/10;
  sTP = sTP > nGame*10 ? '<font color="red">'+sTP+'</font>' : sTP;
  sTP = lPlayer2Won ? '<b>'+sTP+'</b>' : sTP;
  document.write( '<td align="center" width="25"><font size="-1">' + sTP + sDT ); // 2 TP
  document.write( '</tr>' );
  document.write( '</table>' );
}

function RoundGames( nRound, nPos, nGame, oWindow ){
  if( typeof oWindow != 'object' )
    oWindow = this.window;
  aPlayers = eval( 'aPlayer' + nRound );
  aGames = eval( 'aGame' + nRound );
  nSumTime1 = 0;
  nSumTime2 = 0;
  nSumTP1 = 0;
  nSumTP2 = 0;
  nSumPoint1 = 0;
  nSumPoint2 = 0;

  sTD = '<td align="center">';
  sDT = '</td>';

  oWindow.document.write( '<table cellpadding="1" cellspacing="1" border="1">' );
  oWindow.document.write( '<tr align="center" bgcolor="#006699">' );
  sFont = '<th nowrap><font color="white">';
  oWindow.document.write( '<th colspan="3" nowrap><font color="white">Player 1</th>'+sFont+'Time</th>'+sFont+'TP</th><th colspan="3"><font color="white">Result</th>'+sFont+'TP</th>'+sFont+'Time</th><th colspan="3" nowrap><font color="white">Player 2</th>'+sFont+'Game ID</th>' );
  oWindow.document.write( '</tr><a name="' + nRound+nPos+nGame + '"></a>' ); // games anchor
  for( i=0; i<nGame; i++ ){
    oWindow.document.write( '<tr>' );
    oWindow.document.write( '<td><font size="-1">'+aPlayers[nPos*10][defPlayerNat]+'</font></td>' ); // 1 nation
    oWindow.document.write( '<td><img src="../../flags/'+aPlayers[nPos*10][defPlayerNat].toLowerCase()+'2.gif"></td>' ); // 1 flag
    oWindow.document.write( sTD + '<b>' + aPlayers[nPos*10][0] + '</b>' + sDT ); // jatekos1
    if( aGames[nPos*10+i][4] > 14 )  // ido1
      oWindow.document.write(  sTD + '<font size="-1" color="red"><b>' + aGames[nPos*10+i][4] + '</b></font>' + sDT );
    else
      oWindow.document.write(  sTD + '<font size="-1">' + aGames[nPos*10+i][4] + '</font>' + sDT );
    oWindow.document.write( sTD + '<b>' + aGames[nPos*10+i][7]/10 + '</b>' + sDT ); // TP1
    sFontColor = aGames[nPos*10+i][2] == 'yellow' ? 'black' : 'white';
    sPoint = aGames[nPos*10+i][0] > aGames[nPos*10+i][1] ? '<b>'+aGames[nPos*10+i][0]+'</b>' : aGames[nPos*10+i][0];
    oWindow.document.write( '<td align="center" bgcolor="'+aGames[nPos*10+i][2]+'"><font color="'+sFontColor+'">'+sPoint+sDT );
    oWindow.document.write( sTD + '&nbsp;-&nbsp;' + sDT );
    sFontColor = aGames[nPos*10+i][3] == 'yellow' ? 'black' : 'white';
    sPoint = aGames[nPos*10+i][1] > aGames[nPos*10+i][0] ? '<b>'+aGames[nPos*10+i][1]+'</b>' : aGames[nPos*10+i][1];
    oWindow.document.write( '<td align="center" bgcolor="'+aGames[nPos*10+i][3]+'"><font color="'+sFontColor+'">'+sPoint+sDT );
    oWindow.document.write( sTD + '<b>' + aGames[nPos*10+i][8]/10 + '</b>' + sDT ); // TP2
    if( aGames[nPos*10+i][5] > 14 )  // ido2
      oWindow.document.write(  sTD + '<font size="-1" color="red"><b>' + aGames[nPos*10+i][5] + '</b></font>' + sDT );
    else
      oWindow.document.write(  sTD + '<font size="-1">' + aGames[nPos*10+i][5] + '</font>' + sDT );
    oWindow.document.write( sTD + '<b>' + aPlayers[nPos*10+1][0] + '</b>' + sDT ); // jatekos2
    oWindow.document.write( '<td><img src="../../flags/'+aPlayers[nPos*10+1][defPlayerNat].toLowerCase()+'2.gif"></td>' ); // 2 flag
    oWindow.document.write( '<td><font size="-1">'+aPlayers[nPos*10+1][defPlayerNat]+'</font></td>' ); // 2 nation
    oWindow.document.write( sTD + aGames[nPos*10+i][6] + sDT ); // gameNo
    oWindow.document.write( '</tr>' );
    nSumPoint1 += aGames[nPos*10+i][0];
    nSumPoint2 += aGames[nPos*10+i][1];
    nSumTP1 += aGames[nPos*10+i][7];
    nSumTP2 += aGames[nPos*10+i][8];
    nSumTime1 += aGames[nPos*10+i][4];
    nSumTime2 += aGames[nPos*10+i][5];
  }
  oWindow.document.write( '<tr align="center">' );
  sTD = '</td><td>';
  oWindow.document.write( '<td colspan="3">'+aPlayers[nPos*10][defPlayerName]+sTD+'<font size="-1">'+nSumTime1+'</font>'+sTD+'<b>'+nSumTP1/10+'</b>'+sTD+nSumPoint1+sTD+'-'+sTD+nSumPoint2+sTD+'<b>'+nSumTP2/10+'</b>'+sTD+'<font size="-1">'+nSumTime2+'</font></td><td colspan="3">'+aPlayers[nPos*10+1][defPlayerName]+sTD+'&nbsp;</td>' );
  oWindow.document.write( '</tr>' );
  oWindow.document.write( '</table>' );
}

function RoundGameDetail( nRound, nPos, nGame ){
  wTmp = window.open("","tempWindow","width=800,height=300,resizable=yes,scrollbars=yes,menubar=no");
  wTmp.document.open();
  wTmp.document.write('<link rel="stylesheet" href="/tantrix.css" type="text/css">');
  wTmp.document.write('<b>Round ' + nRound + '</b>' );
  RoundGames( nRound, nPos, nGame, wTmp );
  wTmp.document.close();
  wTmp.focus();
}

function RoundStatBar( nRound ){
  nPercent = Math.round( aTourStat[nRound][1]/aTourStat[nRound][0]*100 );
  document.writeln('<table border="0" cellpadding="0" cellspacing="0">');
  document.writeln('<tr>');
  document.writeln('<td><font size="-1">');
  document.write( 'Games:&nbsp;' + aTourStat[nRound][1] + "/" + aTourStat[nRound][0] + '&nbsp;' );
  document.writeln('</font></td>');
  document.writeln('<td>');
  PercentBar( nPercent );
  document.writeln('</td>');
  document.writeln('</tr>');
  document.writeln('</table>');
}

function PercentBar( nPercent, nHeight ){
  nHeight = nHeight ? nHeight : 1;
  document.writeln('<table border="0" cellpadding="0" cellspacing="0">');
  document.writeln('<tr>');
  document.writeln('<td width="'+nPercent+'" height="3" bgcolor="#CC0000"></td>');
  document.writeln('<td width="'+(100-nPercent)+'" height="3" bgcolor="#006699"></td>');
  document.writeln('</tr>');
  document.writeln('</table>');
}
