/* ----------------------- Standings Table Management routines --------------------------- */

// Constants defining rowColors

GRAY_COLOR = "#CCCCCC";

d = document;

/**
* Addes the table and table title for standings
*
* stageCount - count of stages in the game
* stages = Array of { name : String, date : String };
**/
function AddStandingTitle( stageCount, stages ) {
	startTable();
	
	for( i = 0; i < stageCount; i++ ) {
		writeStageCaption( stages[i] );	
	}
	
	finishStangingsTitle();
}

function CloseStandingsTable() {
	d.writeln( "</tbody>" );
	d.writeln( "</table>" );
}

function startTable() {
	d.writeln( "<TABLE cellSpacing=0 cols=10 cellPadding=0 width=594 align=center border=1 valign='TOP'>" );
	d.writeln( "<TBODY>" );
	d.writeln( "<TR align=middle>" );
	d.writeln( "<TD width=20><SPAN>№</SPAN></TD>" );
	d.writeln( "<TD width='220'><SPAN>Назва команди,<BR>капітан</SPAN></TD>" );
}

function writeStageCaption( stage ) {
	name = stage.name;
	date = stage.date;
	
	d.write( "<TD width=52><SPAN><B>"+name+"</B>" );
	
	if( date != '' ) {
		d.write("<BR><FONT size=-2>"+date+"</FONT>" );
	}
	
	d.writeln( "</SPAN></TD>" );
}

function finishStangingsTitle() {
	d.writeln( "<TD width=45><SPAN><B>Сума балів</B></SPAN></TD></TR>" );
}

/**
* Addes the new row to standings table
*
* stageCount - count of stages in the game
* rowColor - color of row ('' if default)
* place - pleca of team
* teamName - team title
* teamLink - link on a personal site of team if specified
* teamResult - Array of TourResult={ points : Number; rating : Number; gamePoints : Number; absent : Boolean };
**/
function AddResultRow( stageCount, rowColor, place, teamName, teamLink, teamResult ) {
	startRow( rowColor );
	writePlace( place );
	addTeamCaption( teamName, teamLink );
	
	sumGamePoints = 0;
	absentCount = 0;   
	for( i = 0; i < stageCount; i++ ) {
		writeTourResult( i+2, teamResult[i] );
		
		if( !teamResult[i].absent ) {
			sumGamePoints += teamResult[i].gamePoints;
		} else {
			absentCount++;
		}
	}                                   
	
	disqualificated = (absentCount >= 2);
	writeRowResult( sumGamePoints, disqualificated );
}

function startRow( rowColor ) {	
	d.write( "<tr align=left" );
	
	if( rowColor != "" ) {
		d.write( " bgColor="+rowColor );
	}
	
	d.writeln( ">" );
}

function writePlace( place ) {
	d.writeln( "<TD align=middle><SPAN>"+place+".</SPAN></TD>");
}

function addTeamCaption( name, link ) {
	d.write( "<td><span>" );
	
	if( link != '' ) {
		d.write( "<a href='"+link+"'>" );
	}
	
	d.write( name );
	
	if( link != '' ) {
		d.write( "</a>" );
	}
	
	d.writeln( "</span></td>" );
}

function writeTourResult( colIndex, tourResult ) {
 	absent = tourResult.absent;
  	
	if( !absent ) {
		points		= tourResult.points;
		rating		= tourResult.rating;
		gamePoints	= tourResult.gamePoints;
	} else {
		points		= "-*";
		rating		= "-*";
		gamePoints	= "-*";
	}
	
	d.write( "<td><span><font color=#CC0000>О.</font>: "+points+"</span><br/>" );
	d.write( "<span><font color=#006600>Р.</font>: "+rating+"</span><br/>" );
	d.writeln( "<span><font color=#3333ff>Б.</font>: "+gamePoints+"</span><br/></td>" );
}

function writeRowResult( points, disqualificated ) {
	if( !disqualificated ) {
		d.writeln( "<TD align=middle><SPAN><B>"+points+"</B></SPAN></TD></TR>" );
	} else {
		d.writeln( "<TD align=middle><SPAN><I><FONT size=1>Команда знята ("+points+")</FONT></I></SPAN></TD></TR>");
	}
}