// www.TANTRIX.hu - Tantrix tournament - Group phase
// Copyright (c) 2002, Uniqsoft Ltd., info@uniqsoft.hu
// Author - Stefan Krausz, krausz.istvan@uniqsoft.hu
// 2002 - for 1st Hungarian Championchip
// 2003.04.26 - Time penalty error fixed
// 2004.04.19 - More rounds in group - GroupSum(); Hide group name in GroupGame()
// 2003.03.18 - EU 2005 - Hide 'white' results - English text items

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 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;
            aGames[i*10+j][7] = aGames[i*10+j][7] < 0 ? 0 : aGames[i*10+j][7];
            aGames[i*10+j][8] = aGames[i*10+j][8] > 200 ? 200 : aGames[i*10+j][8];
          }
          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;
            aGames[i*10+j][7] = aGames[i*10+j][7] > 200 ? 200 : aGames[i*10+j][7];
            aGames[i*10+j][8] = aGames[i*10+j][8] < 0 ? 0 : aGames[i*10+j][8];
          }
          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="0" border="1">' );
  document.write( '<tr align="center" bgcolor="'+sGroupColor+'">' );
  sFont = '<th><font size="-1" color="' + sFontColor + '">';
  document.write( sFont+'Player 1</th>'+sFont+'Time</th>'+sFont+'TP</th><th colspan="3"><font size="-1"color="'+sFontColor+'">Tiles</th>'+sFont+'TP</th>'+sFont+'Time</th>'+sFont+'Player 2</th>'+sFont+'Game ID</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 {
        if( aGames[i*10+j][2] != 'white' ){ // Hide white (not used) result
          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="0" border="1">' );
  document.write( '<tr align="center">' );
  document.write( '<td nowrap bgcolor="'+sGroupColor+'" colspan="9"><b><font color="' + sFontColor + '">Group&nbsp;'+ sGroupName + '</font></b></td>' );
  document.write( '</tr>' );
  document.write( '<tr align="center" bgcolor="'+sGroupColor+'">' );
  sFont = '<font size="-1" color="' + sFontColor + '">';
  document.write( '<th>'+sFont+'Nick</th><th>'+sFont+'&nbsp;</th><th>'+sFont+'Name / 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;TP&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];
    if( aSortPlayers[i][3] == 'black' ){ // defaulted
      sAttrB = '<s>';
      sAttrE = '</s>';
    }
    else {
      sAttrB = '';
      sAttrE = '';
    }
    document.write( '<tr align="center">' );
    document.write( '<td><font size="-1"><b>' + sAttrB + aSortPlayers[i][0] + sAttrE + '</b></font></td>');
    sTmpNation = aSortPlayers[i][10];
    document.write( '<td><img src="../../flags/'+sTmpNation.toLowerCase()+'2.gif" height="16" width="21" border="0" title="'+sTmpNation+'"></td>');
    document.write( '<td nowrap><font size="-1"><a href="mailto:' + aSortPlayers[i][2] + '">' + sAttrB + aSortPlayers[i][1] + sAttrE + '</a></font></td>');
    document.write( '<td><font size="-1">' + sAttrB + String( nGame ) + sAttrE + '</font></td>');
    document.write( '<td><font size="-1">' + sAttrB + aSortPlayers[i][5] + sAttrE + '</font></td>');
    document.write( '<td><font size="-1">' + sAttrB + aSortPlayers[i][6] + sAttrE + '</font></td>');
    document.write( '<td><font size="-1">' + sAttrB + aSortPlayers[i][7] + sAttrE + '</font></td>');
    document.write( '<td><font size="-1">' + sAttrB + aSortPlayers[i][8]/10 + sAttrE + '</font></td>');
    document.write( '<td><font size="-1"><b>&nbsp;' + sAttrB + aSortPlayers[i][9]/10 + '%' + sAttrE + '&nbsp;</b></font></td>');
    document.write( '</tr>' );
    if( i == 1 &&  nGame != 0 ){
      document.write( '<tr><td colspan="9" 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="0" 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] < 0 )
        document.write( '---' );
      else {
        if( aPoints[i][j]/10 > 30 )
          document.write( '<font color="#CC0000">' );
        document.write( aPoints[i][j]/10 );
        if( aPoints[i][j]/10 > 30 )
          document.write( '</font>' );
        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 GroupSum( sGroupName, nRound ){
  aPlayers = eval( 'aPlayer' + 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( j=1; j <= nRound; j++ ){
    aTmpPlayers = eval( 'aPlayer' + sGroupName + j );
    aTmpPoints = eval( 'aPoint' + sGroupName + j );
    for( i=0; i < aPlayers.length; i++ ){
      aPlayers[i][5] += aTmpPlayers[i][5];
      aPlayers[i][6] += aTmpPlayers[i][6];
      aPlayers[i][7] += aTmpPlayers[i][7];
      aPlayers[i][8] += aTmpPlayers[i][8];
      for( k=0; k < aPlayers.length; k++ ){
        aPoints[i][k] += aTmpPoints[i][k];
      }
    }
  }
}

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

function GroupStat( sGroupName ){
  aGames = eval( 'aGame' + sGroupName );
  nSum = 0;
  nPlayed = 0;
  for( i=0; i<aGames.length; i++ ){
    if( typeof( aGames[i] ) != 'undefined' ){
      nSum++;
      if( aGames[i][0] > 0 ){
        nPlayed++;
      }
    }
  }
  document.write( 'Játékok: ' + nPlayed + '/' + nSum + ' <b>' + Math.round( nPlayed/nSum*100 ) + '%</b>' );
}

function Info(){
        document.write( '<a href="http://tournaments.tantrix.co.uk/sendres.shtml" target="_blank">Sending results form</a><br><br><a href="http://tournaments.tantrix.co.uk/2005euro.shtml" target="_blank">Euro home page</a><br><a href="http://tournaments.tantrix.co.uk/results/05arrgam.htm" target="_blank">Help with arranging games</a><br><a href="http://tournaments.tantrix.co.uk/results/timezonw.htm" target="_blank">Time zone convertor</a><br><a href="http://tournaments.tantrix.co.uk/phpBB2/viewforum.php?f=39" target="_blank">Previews &amp; reports</a><br><a href="http://tournaments.tantrix.co.uk/rules0.shtml" target="_blank">Rules</a><br><br><a href="http://www.tantrix.hu/jatek/tour/inter/euro2005/euro2005.html">Return</a>' );
}

function GroupSelect(){
  document.write( '<form name="form1" method="post" action="">                                                                                                                              ' );
  document.write( '  <select name="GROUP" id="GROUP" onChange="if( this.options[this.selectedIndex].value != \'00\' ) self.location=\'g\'+this.options[this.selectedIndex].value+\'.html\'">' );
  document.write( '    <option value="00" selected>Select a group</option>                                                                                                                  ' );
  document.write( '    <option value="01">Group 01</option>                                                                                                                                 ' );
  document.write( '    <option value="02">Group 02</option>                                                                                                                                 ' );
  document.write( '    <option value="03">Group 03</option>                                                                                                                                 ' );
  document.write( '    <option value="04">Group 04</option>                                                                                                                                 ' );
  document.write( '    <option value="05">Group 05</option>                                                                                                                                 ' );
  document.write( '    <option value="06">Group 06</option>                                                                                                                                 ' );
  document.write( '    <option value="07">Group 07</option>                                                                                                                                 ' );
  document.write( '    <option value="08">Group 08</option>                                                                                                                                 ' );
  document.write( '    <option value="09">Group 09</option>                                                                                                                                 ' );
  document.write( '    <option value="10">Group 10</option>                                                                                                                                 ' );
  document.write( '    <option value="11">Group 11</option>                                                                                                                                 ' );
  document.write( '    <option value="12">Group 12</option>                                                                                                                                 ' );
  document.write( '    <option value="13">Group 13</option>                                                                                                                                 ' );
  document.write( '    <option value="14">Group 14</option>                                                                                                                                 ' );
  document.write( '    <option value="15">Group 15</option>                                                                                                                                 ' );
  document.write( '    <option value="16">Group 16</option>                                                                                                                                 ' );
  document.write( '    <option value="17">Group 17</option>                                                                                                                                 ' );
  document.write( '    <option value="18">Group 18</option>                                                                                                                                 ' );
  document.write( '    <option value="19">Group 19</option>                                                                                                                                 ' );
  document.write( '    <option value="20">Group 20</option>                                                                                                                                 ' );
  document.write( '  </select>                                                                                                                                                              ' );
  document.write( '</form>                                                                                                                                                                  ' );
}
