// Script to generate Summer Challenge Table 
var races=22	//Number of races
var toCount=9 //Number of scores to count
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++]=["Chris D","A","A",1,1,,,,,4,,,,,,1,6,2,1,5,5,,,,,"A","A",0];
marks[n++]=["Rachael","A","A",2,3,1,2,3,,,,2,1,3,4,9,3,6,4,4,4,,,,,"A","A",0];
marks[n++]=["Emma D","A","A",3,5,,,,,,,,,,,,,,,,,,,,,"A","A",0];
marks[n++]=["Wilf","A","A",4,4,,,,,,5,,,,,,,,,,,,,,,"A","A",0];
marks[n++]=["Barbara","A","A",5,6,3,3,6,,5,4,,,2,3,6,5,1,2,1,3,3,3,1,2,"A","A",0];
marks[n++]=["Mick B","A","A",6,2,2,1,1,2,3,2,,,5,5,4,4,4,3,3,6,2,5,5,3,"A","A",0];
marks[n++]=["Roy","A","A",,,,,2,1,1,1,1,2,1,1,9,1,3,6,,,,,,,"A","A",0];
marks[n++]=["Brenda","A","A",,,,,4,3,7,6,3,3,,,2,9,,,,,6,4,,,"A","A",0];
marks[n++]=["Gabriel","A","A",,,,,,,2,,,,,,,,,,,,,,,,"A","A",0];
marks[n++]=["Clive","A","A",,,,,,,6,3,,,,,,,,,,,,,,,"A","A",0];
marks[n++]=["Brian","A","A",,,,,,,,,,,6,6,,,,,,,,,,,"A","A",0];
marks[n++]=["Carol","A","A",,,,,,,,,,,4,2,,,,,,2,,,,,"A","A",0];
marks[n++]=["Rod","A","A",,,,,,,,,,,,,3,2,,,,,,,2,1,"A","A",0];
marks[n++]=["Cheryl","A","A",,,,,,,,,,,,,5,9,,,,,,,,,"A","A",0];
marks[n++]=["Helen","A","A",,,,,,,,,,,,,,,,,2,1,,,,,"A","A",0];
marks[n++]=["Emma","A","A",,,,,,,,,,,,,,,,,,,1,2,,,"A","A",0];
marks[n++]=["Eddie","A","A",,,,,,,,,,,,,,,,,,,,1,,,"A","A",0];
marks[n++]=["Hilary","A","A",,,,,,,,,,,,,,,,,,,6,,,,"A","A",0];
marks[n++]=["Mike","A","A",,,,,,,,,,,,,,,,,,,,,5,4,"A","A",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=2; 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=2; 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 class=smchall>";
			// check if it's a counting score and set bold 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 class=smchall>"+place+"</TD>");
		document.write("</TR>");
		
	}
}

function checkDiscard(place, race, myarray)	{
	if (place==undefined)	{	//Blank score
		place=myarray.length;
	}
	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]);
}



