// Script to generate Summer Challenge Table 
var races=26	//Number of races
var toCount=10 //Number of scores to count so far
var x=0;	//universal counter;
var sorted=new Array();	//Empty array for sorted results
var totalScore=new Array();

var marks= new Array();
var n=0;
marks[n++]=["Gary & Janet Crane",3,3,6,6,5,3,4,1,,,,,"A","A",6,6,,,1,2,5,5,,,,,0];
marks[n++]=["Barbara & Brenda",2,4,1,2,4,5,3,3,2,1,3,3,"A","A",3,3,2,3,3,4,,,,,4,1,0];
marks[n++]=["Clive & Rachael",1,2,3,1,3,4,1,4,,5,,,"A","A",1,1,3,2,2,3,3,3,4,,3,3,0];
marks[n++]=["Roy Child",4,1,2,3,,,2,2,1,4,1,1,"A","A",5,4,1,1,"4*",1,1,1,1,1,1,4,0];
marks[n++]=["Priscilla & Helen",,,5,4,,,,,,,,,"A","A",,,,,,,,,2,2,,,0];
marks[n++]=["Alison",,,4,5,,,,,,,,,"A","A",,,,,,,,,,,,,0];
marks[n++]=["Roger",,,7,7,6,6,,,,,4,4,"A","A",4,5,,,,,,,,,6,6,0];
marks[n++]=["Chris Weston",,,,,2,2,,,,,,,"A","A",,,,,,,,,,,,,0];
marks[n++]=["Chris & Helen",,,,,1,1,,,,,2,2,"A","A",2,2,,,,,,,3,3,2,2,0];
marks[n++]=["Chris Lode & Cheryl",,,,,,,5,,4,3,,,"A","A",,,,,,,,,,,,,0];
marks[n++]=["Brenda",,,,,,,,,3,2,,,"A","A",,,4,4,,,4,2,,,,,0];
marks[n++]=["Emma & Dave",,,,,,,,,,,,,,,,,,,,,2,4,,,5,5,0];
//marks[n++]=["xxx",,,,,,,,,,,,,,,,,,,,,,,,,,,0];
//Make an array for scores to count
var scores=new Array();
function resetScores(myarray)	{
	for (var score=0; score<toCount; score++)	{
		scores[score]=(myarray.length)*100 + races;	//initialize it to all discard scores, ie number of competitors
	}
}

function numberOrder(a,b){	//used in conjunction with Array.sort() to specify sort order.
	return a-b;
}

function maketable()	{
	var mark;
	var raceNumber;
	var datatag;
	var points;
	var place;
	for (var entry=0; entry<marks.length; entry++)	{
		resetScores(marks);
		points=0;
		for (x=0; x<marks[entry].length-1; x++)	{
			checkDiscard(marks[entry][x],x-1,marks);
		}
		//Calculate total score
		for (score=0; score<toCount; score++)	{
			raceNumber=scores[score]%100;
			points=points+(scores[score]-raceNumber)/100;
		}
		//add total score as last array element
		marks[entry][x]=points;
		//store the score in totalScore array
		totalScore[entry]=points;
	}
	totalScore.sort(numberOrder);	//Sort scores array into numerical order

//sort marks array into points order here!!!!!!!!!!!!
	var sailors=marks.length;
	for (entry=0; entry<sailors; entry++)	{
		x=0;
		var found=false;
		do	{
			if (marks[x][marks[x].length-1]==totalScore[entry])	{
				found=true
				sorted[entry]=marks[x];	//copy entry to sorted array
				marks.splice(x,1);	//delete the entry
			}
			x++;
		}
		while (!found);
	}
				
//Write table rows
	for (entry=0; entry<sorted.length; entry++)	{
		document.write("<TR>");
		resetScores(sorted);
		for (x=0; x<sorted[entry].length-1; x++)	{
			checkDiscard(sorted[entry][x],x-1,sorted);
		}
		for (x=0; x<sorted[entry].length; x++)	{
			mark=sorted[entry][x];
			if(mark==undefined)	{
				mark="&nbsp;";
			}
			datatag="<TD>";
			// check if it's a counting score and set inverse tag
			for (score=0; score<toCount; score++)	{
				raceNumber=scores[score]%100;
				if (raceNumber==x-1)	{
					datatag="<TD class=inverse>";
				}
			}
			document.write(datatag+mark+"</TD>");
		}
		// check for tied place
		if (entry==0)	{
			place=1;
		}
		else	{
			if (totalScore[entry-1]==totalScore[entry])	{
				place=entry +"=";
			}
			else	{
				place=entry+1;
			}
		}
		document.write("<TD>"+place+"</TD>");
		document.write("</TR>");
		
	}
}

function checkDiscard(place, race, myarray)	{		//myarray is a copy of sorted reults array
	if (place==undefined)	{	//Blank score
		place=myarray.length;
	}
	else	{	//takes account of a 8(DNF) type mark, scores an 8, prints 8(DNF)
		place=parseInt(place);
	}
	if (isNaN(parseInt(place)))	{ //non-numeric score, eg abandoned
		return;
	}
	var merit=place*100 + race
	// Check if merit score is less than greatest score to count so far and replace if so.
	if (scores[toCount-1] > merit)	{
		scores[toCount-1] = merit;
	}
	scores.sort(numberOrder);	//re-sort ready for next time
	//alert(place+" * "+race + " ** " + scores[toCount-1]);
}


