// www.TANTRIX.hu - Tantrix tournament client side functions
// Copyright (c) 2002, Uniqsoft Ltd., info@uniqsoft.hu
// Author - Stefan Krausz, krausz.istvan@uniqsoft.hu
// Version 1.0 - I. Magyar Bajnoksag
// Version 1.1 - I. Magyar Mesterek Tornaja (csak 1 csoport)

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];

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++ ){
      if( aGames[h*10+i][0] != 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][6]++;
          aPlayers[h*10+1][6]++;
        }
        else {
          if( nResult > 0 ){ // 1 nyert
            aGames[h*10+i][7] = nTP;
            aGames[h*10+i][8] = 200-nTP;
            aPlayers[h*10][5]++;
            aPlayers[h*10+1][7]++;
          }
          else{ // 1 vesztett
            aGames[h*10+i][7] = 200-nTP;
            aGames[h*10+i][8] = nTP;
            aPlayers[h*10][7]++;
            aPlayers[h*10+1][5]++;
          }
        }
        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][8] += aGames[h*10+i][7];
        aPlayers[h*10+1][8] += 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][8] > aPlayers[nPos*10+1][8];
  lPlayer2Won = aPlayers[nPos*10+1][8] > aPlayers[nPos*10][8];

  document.write( '<table cellpadding="1" cellspacing="1" border="1">' );
  document.write( '<tr>' );
  sPlayer = aPlayers[nPos*10][0];
  sPlayer = lPlayer1Won ? '<b>'+sPlayer+'</b>' : sPlayer;
  document.write( '<td align="center" width="50"><font size="-1">' + sPlayer + 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][8]/10;
  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>' );
  sPlayer = aPlayers[nPos*10+1][0];
  sPlayer = lPlayer2Won ? '<b>'+sPlayer+'</b>' : sPlayer;
  document.write( '<td align="center" width="50"><font size="-1">' + sPlayer + 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][8]/10;
  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 ){
  aPlayers = eval( 'aPlayer' + nRound );
  aGames = eval( 'aGame' + nRound );
  nSumTime1 = 0;
  nSumTime2 = 0;
  nSumTP1 = 0;
  nSumTP2 = 0;
  nSumPoint1 = 0;
  nSumPoint2 = 0;

  sTD = '<td align="center"><font size="-1">';
  sDT = '</font></td>';

  document.write( '<table cellpadding="1" cellspacing="1" border="1">' );
  document.write( '<tr align="center" bgcolor="#006699">' );
  sFont = '<th nowrap><font size="-1" color="white">';
  document.write( sFont+'Játékos 1</th>'+sFont+'Idő</th>'+sFont+'TP</th><th colspan="3"><font size="-1"color="white">Eredmény</th>'+sFont+'TP</th>'+sFont+'Idő</th>'+sFont+'Játékos 2</th>'+sFont+'Játék azonosító</th>' );
  document.write( '</tr>' );
  for( i=0; i<nGame; i++ ){
    document.write( '<tr>' );
    document.write( sTD + '<b>' + aPlayers[nPos*10][0] + '</b>' + sDT ); // jatekos1
    if( aGames[nPos*10+i][4] > 14 )  // ido1
      document.write(  sTD + '<font color="red"><b>' + aGames[nPos*10+i][4] + '</b></font>' + sDT );
    else
      document.write(  sTD + aGames[nPos*10+i][4] + sDT );
    document.write( sTD + aGames[nPos*10+i][7]/10 + 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];
    document.write( '<td align="center" bgcolor="'+aGames[nPos*10+i][2]+'"><font size="-1" color="'+sFontColor+'">'+sPoint+sDT );
    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];
    document.write( '<td align="center" bgcolor="'+aGames[nPos*10+i][3]+'"><font size="-1" color="'+sFontColor+'">'+sPoint+sDT );
    document.write( sTD + aGames[nPos*10+i][8]/10 + sDT ); // TP2
    if( aGames[nPos*10+i][5] > 14 )  // ido2
      document.write(  sTD + '<font color="red"><b>' + aGames[nPos*10+i][5] + '</b></font>' + sDT );
    else
      document.write(  sTD + aGames[nPos*10+i][5] + sDT );
    document.write( sTD + '<b>' + aPlayers[nPos*10+1][0] + '</b>' + sDT ); // jatekos2
    document.write( sTD + aGames[nPos*10+i][6] + sDT ); // gameNo
    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];
  }
  document.write( '<tr align="center">' );
  sTD = '</td><td><font size="-1">';
  document.write( '<td><font size="-1">&nbsp;'+sTD+nSumTime1+sTD+'<b>'+nSumTP1/10+'</b>'+sTD+nSumPoint1+sTD+'-'+sTD+nSumPoint2+sTD+'<b>'+nSumTP2/10+'</b>'+sTD+nSumTime2+'</td>' );
  document.write( '</tr>' );
  document.write( '</table>' );
}

// ----------------------------------------------------------------------------

function GroupInit( sGroupName ){
  aTmp = new Array();
  aPlayers = eval( 'aPlayer' + sGroupName );
  aGames = eval( 'aGame' + sGroupName );
  aPoints = eval( 'aPoint' + sGroupName );

  for( i=0; i<aPlayers.length; i++ ){
    aPoints[i] = new Array( aPlayers.length );
    for( j=0; j<aPlayers.length; j++ ){
      aPoints[i][j] = 0;
    }
  }

  for( i=0; i<aPlayers.length; i++ ){
    for( j=0; j<aPlayers.length; j++ ){
      if( i==j ){
        aPoints[i][j] = -1;
      }
      else {
        if( aGames[i*10+j][0] != 0 ){
          nResult = aGames[i*10+j][0]-aGames[i*10+j][1];
          nAbsResult = Math.abs( nResult ) > 36 ? 36 : Math.abs( nResult );
          nTP = aTourPoint[nAbsResult];
          if( nResult == 0 && aGames[i*10+j][0] > 0 ){ // dontetlen
            aGames[i*10+j][7] = nTP;
            aGames[i*10+j][8] = 200-nTP;
            aPlayers[i][6]++;
            aPlayers[j][6]++;
          }
          else {
            if( nResult > 0 ){ // 1 nyert
              aGames[i*10+j][7] = nTP;
              aGames[i*10+j][8] = 200-nTP;
              aPlayers[i][5]++;
              aPlayers[j][7]++;
            }
            else{ // 1 vesztett
              aGames[i*10+j][7] = 200-nTP;
              aGames[i*10+j][8] = nTP;
              aPlayers[i][7]++;
              aPlayers[j][5]++;
            }
          }
          nPenalty = aGames[i*10+j][4]-14;
          if( nPenalty > 0 ){ // 1 ido bunti
            aGames[i*10+j][7] -= nPenalty*10;
            aGames[i*10+j][8] += nPenalty*10;
          }
          nPenalty = aGames[i*10+j][5]-14;
          if( nPenalty > 0 ){ // 2 ido bunti
            aGames[i*10+j][7] += nPenalty*10;
            aGames[i*10+j][8] -= nPenalty*10;
          }
          aPoints[i][j] += aGames[i*10+j][7];
          aPoints[j][i] += aGames[i*10+j][8];
          aPlayers[i][8] += aGames[i*10+j][7];
          aPlayers[j][8] += aGames[i*10+j][8];
        }
      }
    }
  }
}

function GroupGames( sGroupName, sGroupColor ){
  aPlayers = eval( 'aPlayer' + sGroupName );
  aGames = eval( 'aGame' + sGroupName );
  sFontColor = sGroupColor == 'yellow' ? 'black' : 'white';
  sTD = '<td align="center"><font size="-1">';
  sDT = '</font></td>';
  document.write( '<table cellpadding="1" cellspacing="1" border="1">' );
  document.write( '<tr align="center">' );
  document.write( '<td nowrap bgcolor="' + sGroupColor + '" colspan="10"><b><font color="'+sFontColor+'">&nbsp;'+ sGroupName + '&nbsp;csoport</font></b></td>' );
  document.write( '</tr>' );
  document.write( '<tr align="center" bgcolor="'+sGroupColor+'">' );
  sFont = '<th><font size="-1" color="' + sFontColor + '">';
  document.write( sFont+'Játékos 1</th>'+sFont+'Idő 1</th>'+sFont+'TP 1</th><th colspan="3"><font size="-1"color="'+sFontColor+'">Eredmény</th>'+sFont+'TP 2</th>'+sFont+'Idő 2</th>'+sFont+'Játékos 2</th>'+sFont+'Játék azonosító</th>' );
  document.write( '</tr>' );
  for( i=0; i<aPlayers.length; i++ ){
    for( j=0; j<aPlayers.length; j++ ){
      if( i!=0 && j==0 )
        document.write( '<tr><td></td></tr>' );
      if( i==j ){
        ;
      }
      else {
        document.write( '<tr>' );
        document.write( sTD + '<b>' + aPlayers[i][0] + '</b>' + sDT ); // jatekos1
        if( aGames[i*10+j][4] > 14 )  // ido1
          document.write(  sTD + '<font color="red"><b>' + aGames[i*10+j][4] + '</b></font>' + sDT );
        else
          document.write(  sTD + aGames[i*10+j][4] + sDT );
        document.write( sTD + aGames[i*10+j][7]/10 + sDT ); // TP1
        sFontColor = aGames[i*10+j][2] == 'yellow' ? 'black' : 'white';
        sPoint = aGames[i*10+j][0] > aGames[i*10+j][1] ? '<b>'+aGames[i*10+j][0]+'</b>' : aGames[i*10+j][0];
        document.write( '<td align="center" bgcolor="'+aGames[i*10+j][2]+'"><font size="-1" color="'+sFontColor+'">'+sPoint+sDT );
        document.write( sTD + '&nbsp;-&nbsp;' + sDT );
        sFontColor = aGames[i*10+j][3] == 'yellow' ? 'black' : 'white';
        sPoint = aGames[i*10+j][1] > aGames[i*10+j][0] ? '<b>'+aGames[i*10+j][1]+'</b>' : aGames[i*10+j][1];
        document.write( '<td align="center" bgcolor="'+aGames[i*10+j][3]+'"><font size="-1" color="'+sFontColor+'">'+sPoint+sDT );
        document.write( sTD + aGames[i*10+j][8]/10 + sDT ); // TP2
        if( aGames[i*10+j][5] > 14 )  // ido2
          document.write(  sTD + '<font color="red"><b>' + aGames[i*10+j][5] + '</b></font>' + sDT );
        else
          document.write(  sTD + aGames[i*10+j][5] + sDT );
        document.write( sTD + '<b>' + aPlayers[j][0] + '</b>' + sDT ); // jatekos2
        document.write( sTD + aGames[i*10+j][6] + sDT ); // gameNo
        document.write( '</tr>' );
      }
    }
  }
  document.write( '</table>' );
}

function GroupResult( sGroupName, sGroupColor ){
  aPlayers = eval( 'aPlayer' + sGroupName );
  aSortPlayers = eval( 'aSortPlayer' + sGroupName );;
  for( i=0; i<aPlayers.length; i++ ){
    aSortPlayers[i] = aPlayers[i];
    nGame = aSortPlayers[i][5]+aSortPlayers[i][6]+aSortPlayers[i][7];
    aSortPlayers[i][9] = nGame ? Math.round(aSortPlayers[i][8]/(nGame*2)*10) : 0;
  }
  aSortPlayers.sort( byPercentDesc );
  sFontColor = sGroupColor == 'yellow' ? 'black' : 'white';
  document.write( '<table cellpadding="1" cellspacing="1" border="1">' );
  document.write( '<tr align="center">' );
  document.write( '<td nowrap bgcolor="'+sGroupColor+'" colspan="10"><b><font color="'+sFontColor+'">&nbsp;'+ sGroupName + '&nbsp;csoport</font></b></td>' );
  document.write( '</tr>' );
  document.write( '<tr align="center" bgcolor="'+sGroupColor+'">' );
  sFont = '<font size="-1" color="' + sFontColor + '">';
  document.write( '<th>'+sFont+'Név</th><th>'+sFont+'1</th><th>'+sFont+'2</th><th>'+sFont+'Név/email</th><th>'+sFont+'<font face="Symbol">&nbsp;S&nbsp;</th><th>'+sFont+'&nbsp;+&nbsp;</th><th>'+sFont+'&nbsp;x&nbsp;</th><th>'+sFont+'&nbsp;-&nbsp;</th><th>'+sFont+'&nbsp;Pont&nbsp;</th><th>'+sFont+'&nbsp;%&nbsp;</th>' );
  document.write( '</tr>' );
  for( i=0; i<aSortPlayers.length; i++ ){
    nGame = aSortPlayers[i][5]+aSortPlayers[i][6]+aSortPlayers[i][7];
    document.write( '<tr align="center">' );
    document.write( '<td><font size="-1">' + '<b>' + aSortPlayers[i][0] + '</b>' + '</font></td>');
    document.write( '<td  bgcolor="' + aSortPlayers[i][3] + '"><font size="-1">&nbsp;</font></td>');
    document.write( '<td  bgcolor="' + aSortPlayers[i][4] + '"><font size="-1">&nbsp;</font></td>');
    document.write( '<td nowrap><font size="-1"><a href="mailto:' + aSortPlayers[i][2] + '">' + aSortPlayers[i][1] + '</a></font></td>');
    document.write( '<td><font size="-1">' + String( nGame ) + '</font></td>');
    document.write( '<td><font size="-1">' + aSortPlayers[i][5] + '</font></td>');
    document.write( '<td><font size="-1">' + aSortPlayers[i][6] + '</font></td>');
    document.write( '<td><font size="-1">' + aSortPlayers[i][7] + '</font></td>');
    document.write( '<td><font size="-1">' + aSortPlayers[i][8]/10 + '</font></td>');
    document.write( '<td><font size="-1"><b>&nbsp;' + aSortPlayers[i][9]/10 + '%&nbsp;</b></font></td>');
    document.write( '</tr>' );
    if( i == 2 &&  nGame != 0 ){
      document.write( '<tr><td colspan="10" bgcolor="'+sGroupColor+'"><img src="../../../../1pixel.gif" width="1" height="1"></td></tr>' );
    }
  }
  document.write( '</table>' );

}

function GroupCross( sGroupName, sGroupColor ){
  aPlayers = eval( 'aPlayer' + sGroupName );
  aPoints = eval( 'aPoint' + sGroupName );
  sFontColor = sGroupColor == 'yellow' ? 'black' : 'white';
  document.write( '<table cellpadding="1" cellspacing="1" border="1">' );
// First line, table header
  document.write( '<tr align="center">' );
  document.write( '<td nowrap bgcolor="' + sGroupColor + '"><b><font color="'+sFontColor+'">&nbsp;'+ sGroupName + '&nbsp;</font></b></td>' );
  for( i=0; i<aPlayers.length; i++ ){
    document.write( '<td nowrap><font size="-1">' + aPlayers[i][0] + '</font></td>');
  }
  document.write( '<th nowrap><font size="-1">Total</font></th>');
  document.write( '</tr>' );
// Player matrix  
  for( i=0; i<aPlayers.length; i++ ){
    document.write( '<tr align="center">' );
    document.write( '<td nowrap><font size="-1">' + '<b>' + aPlayers[i][0] + '</b>' + '</font></td>');
    nSum = 0;
    for( j=0; j<aPoints.length; j++ ){
      document.write( '<td nowrap><font size="-1">' );
      if( aPoints[i][j] == -1 )
        document.write( '-' );
      else {
        document.write( aPoints[i][j]/10 );
        nSum += aPoints[i][j];
      }
      document.write( '</font></td>');
    }
    document.write( '<th><font size="-1">' + nSum/10 + '</font></th>' );
    document.write( '</tr>' );
  }
  document.write( '</table>' );
}

function WriteCurrentPlayer( sGroupName, nNo ){
  aSortPlayers = eval( 'aSortPlayer' + sGroupName );
  if( aSortPlayers[nNo-1][8] > 0 )
    document.write( aSortPlayers[nNo-1][0] );
}

function byPercentDesc( a, b ){
   return b[9] - a[9];
}
