function revsort(a,b)
{
	// Team sorting rules, higher numbers take precedence.
	// This function is used to sort the idx array after all
	// the statistics are calculated. Prioritize as you wish.
	if (prct[a]>prct[b])
		return -1
	if (prct[a]<prct[b])
		return 1
	if (mpts[a]>mpts[b])
		return -1
	if (mpts[a]<mpts[b])
		return 1
	if (tbrk[a]>tbrk[b])
		return -1
	if (tbrk[a]<tbrk[b])
		return 1
	if (usat[a]>usat[b])
		return -1
	if (usat[a]<usat[b])
		return 1
	if (gpts[a]>gpts[b])
		return -1
	if (gpts[a]<gpts[b])
		return 1
	if (loss[b]>loss[a])
		return -1
	if (loss[b]<loss[a])
		return 1
	if (keys[teams[a]]>keys[teams[b]])
		return 1
	if (keys[teams[a]]<keys[teams[b]])
		return -1
	return 0
} // end function

function calcStats() 
{
var a	// scratch variable
var b	// scratch variable
var x	// scratch variable
var y	// looping index

// calculate team statistics
for (y in sched) {
	// first pass through all match results
	if ((sched[y][1] == 0) && (sched[y][3] == 0)) {
		// this match not yet played
		continue
	} else {
		// add match score to game points
		gpts[keys[sched[y][0]]]+=sched[y][1]
		gpts[keys[sched[y][2]]]+=sched[y][3]
	}
	if (sched[y][1] > sched[y][3]) {
		// visiting team wins match
		wins[keys[sched[y][0]]]+=1
		mpts[keys[sched[y][0]]]+=1
		loss[keys[sched[y][2]]]+=1
		continue
	}
	if (sched[y][1] < sched[y][3]) {
		// home team wins match
		wins[keys[sched[y][2]]]+=1
		mpts[keys[sched[y][2]]]+=1
		loss[keys[sched[y][0]]]+=1
		continue
	}
	if (sched[y][1] == sched[y][3]) {
		// match was drawn
		draw[keys[sched[y][0]]]+=1
		mpts[keys[sched[y][0]]]+=0.5
		draw[keys[sched[y][2]]]+=1
		mpts[keys[sched[y][2]]]+=0.5
		continue
	}
    }
for (y in sched) {
	// second pass through all match results.
	// calculate usat totals and tie-breaks.
	// only need to process completed matches.
	if ((sched[y][1] != 0) || (sched[y][3] != 0)) {
		// increment team usat totals
		usat[keys[sched[y][0]]]+=sched[y][1]*mpts[keys[sched[y][2]]]
		usat[keys[sched[y][2]]]+=sched[y][3]*mpts[keys[sched[y][0]]]

		// determine if h2h tie-breaks are applicable
		if (mpts[keys[sched[y][0]]]!=mpts[keys[sched[y][2]]]) {
			// team matchpoints are not identical, no h2h tie-break
			continue;
		}
		a=wins[keys[sched[y][0]]]+loss[keys[sched[y][0]]]+draw[keys[sched[y][0]]]
		b=wins[keys[sched[y][2]]]+loss[keys[sched[y][2]]]+draw[keys[sched[y][2]]]
		if (a!=b) {
			// total played matches are not identical, no h2h tie-break
			continue;
		}
		// calculate team tie-break totals (also works for multi-way ties)
		if (sched[y][1] > sched[y][3]) {
			// visiting team earns a tie-break point
			tbrk[keys[sched[y][0]]]+=1
			continue
		}
		if (sched[y][1] < sched[y][3]) {
			// home team earns a tie-break point
			tbrk[keys[sched[y][2]]]+=1
			continue
		}
		if (sched[y][1] == sched[y][3]) {
			// draw, teams split the tie-break point
			tbrk[keys[sched[y][0]]]+=0.5
			tbrk[keys[sched[y][2]]]+=0.5
			continue
		}
	}
    }
for (y in prct) {
	// determine winning percentage for each team
	x=wins[y]+loss[y]+draw[y]
	if (x == 0) {
		// no matches played
		prct[y]=0
	} else {
		prct[y]=mpts[y]/x
	}
    }
} // end function

function prtStandings()
{
var base
var fr1
var fr2
var fr3
// print table headings for the standings
document.write("<P><TABLE BORDER=3 CELLSPACING=0 CELLPADDING=1>")
document.write("<CAPTION><B><FONT SIZE=+2>STANDINGS</FONT></B></CAPTION>")
document.write("<TR bgcolor=#cccccc><TD ALIGN=left WIDTH=60><B>Team</TD>")
document.write("<TD ALIGN=center WIDTH=40><B>W</TD>")
document.write("<TD ALIGN=center WIDTH=40><B>L</TD>")
document.write("<TD ALIGN=center WIDTH=40><B>D</TD>")
document.write("<TD ALIGN=center WIDTH=50><B>GPTS</TD>")
document.write("<TD ALIGN=center WIDTH=50><B>MPTS</TD>")
document.write("<TD ALIGN=center WIDTH=60><B>PCT</TD>")
document.write("<TD ALIGN=center WIDTH=60><B>USAT</TD>")
//remove PR column 20111111 jjb document.write("<TD ALIGN=center WIDTH=60><B>PR</TD>")
document.write("<TD ALIGN=center WIDTH=232><B>Comments</TD></TR>")
// generate the division standings based on the sorted idx array
for (x in idx) {
	// print team name
	document.write("<TR><TD bgcolor=#66ff66>"+teams[idx[x]]+"</TD>")
	// print team record
	document.write("<TD bgcolor=#ffffff ALIGN=center>"+wins[idx[x]]+"</TD>")
	document.write("<TD bgcolor=#ffffff ALIGN=center>"+loss[idx[x]]+"</TD>")
	document.write("<TD bgcolor=#ffffff ALIGN=center>"+draw[idx[x]]+"</TD>")
	// print game points, convert floating number to decimals
	base=Math.floor(gpts[idx[x]])
	fr1=Math.round((gpts[idx[x]]*10)%10)
	document.write("<TD bgcolor=#ddaaff ALIGN=center>"+base+"."+fr1+"</TD>")
	// print match points, convert floating number to decimals
	base=Math.floor(mpts[idx[x]])
	fr1=Math.round((mpts[idx[x]]*10)%10)
	document.write("<TD bgcolor=#ddaaff ALIGN=center>"+base+"."+fr1+"</TD>")
	// print winning percentage, convert to three decimal places
	base=Math.floor(prct[idx[x]])
	fr1=Math.round(Math.floor(prct[idx[x]]*10)%10)
	fr2=Math.round(Math.floor(prct[idx[x]]*100)%10)
	fr3=Math.round(Math.floor(prct[idx[x]]*1000)%10)
	document.write("<TD bgcolor=#ffff99 ALIGN=center>"+base+"."+fr1+fr2+fr3+"</TD>")
	// print team usat total, convert to two decimal places
	base=Math.floor(usat[idx[x]])
	fr1=Math.round(Math.floor(usat[idx[x]]*10)%10)
	fr2=Math.round(Math.floor(usat[idx[x]]*100)%10)
	document.write("<TD bgcolor=#ffff99 ALIGN=center>"+base+"."+fr1+fr2+"</TD>")
	// print performance rating (not calculated, hard-coded at initialization)
//remove PR column 20111111 jjb 	document.write("<TD bgcolor=#ff9999 ALIGN=center>"+perf[idx[x]]+"</TD>")
	// print any optional comments pertaining to the team
	document.write("<TD bgcolor=#ffffff ALIGN=left>"+notes[idx[x]]+"</TD>")
	document.write("</TR>")
    }
document.write("</TABLE>")
// Add MVP selection at end of season, otherwise comment out this line
// document.write("<FONT color=#ff0000><B>MVP: Paul Freidel 7-1-1 (Baker)</B></font>")
} // end function

function prtSchedule()
{
  var cnt=0 // number of processed matches so far
  var x=0   // high-light matches while x<10
  var y     // used as a loop index
  var z     // current round number
  var num   // number of matches per round
  var base  // scratch variable
  var fr1   // scratch variable

  // generate division schedule and match results
  document.write("<BR><TABLE BORDER=3 CELLSPACING=0 CELLPADDING=1>")
  document.write("<CAPTION><B><FONT SIZE=+2>SCHEDULE </FONT></B>")
  document.write("<BR>\(Match detail provided by online entry tool.\)")
  document.write("</CAPTION>")
  // print table headings for the schedule
  document.write("<TR bgcolor=#cccccc><TD ALIGN=left><B>"+labels[0]+"</TD>")
  document.write("<TD ALIGN=center><B>"+labels[1]+"</TD>")
  document.write("<TD ALIGN=center WIDTH=120><B>"+labels[2]+"</TD>")
  for (y=3; y<labels.length; y++) {
    document.write("<TD ALIGN=center WIDTH=100><B>"+labels[y]+"</TD>")
  }
  num=labels.length-3
  for (y in sched) {
	// process each match
	if (cnt % num == 0) {
		// start a new row for the next round
		document.write("</TR><TR VALIGN=middle>")
		if ((cnt > 0) && (x > 1)) { 
			// discontinue match high-lights for this row.
			// always high-light at least 2 future matches.
			// make sure first row is always high-lighted.
			x=10;
		}
		// set and print the current round number
		z=Math.floor(cnt/num)+1
		document.write("<TD bgcolor=#ff9999 HEIGHT=40 ALIGN=center>"+z+"</TD>")
		// print the completion date for this round
		document.write("<TD bgcolor=#ddaaff ALIGN=center>"+playby[z-1]+"</TD>")
		if (logo2[z-1] != "") {
		  // print team logo if available
		  document.write("<TD bgcolor=#66ff66>")
		  if (byes[z-1] == "Case") { 
		    document.write("<img src="+logo2[z-1]+" align=absmiddle hspace=7 height=40 width=54>")
		  } else if (byes[z-1] == "Pawns") {
		    document.write("<img src="+logo2[z-1]+" align=absmiddle hspace=3 height=40 width=62>")
		  } else if (byes[z-1] == "Wmbat") {
		    document.write("<img src="+logo2[z-1]+" align=absmiddle hspace=3 height=40 width=62>")
		  } else {
		    document.write("<img src="+logo2[z-1]+" align=absmiddle hspace=14 height=40 width=40>")
		  }
		} else {
		  // no logo available for this team
		  document.write("<TD bgcolor=#66ff66 ALIGN=center>")		  
		}
		// print team name with roster link
		document.write("<a href="+links[z-1]+">"+byes[z-1]+"</a></TD>")
	}
	if ((sched[y][1] == 0) && (sched[y][3] == 0)) {
		// this is an unplayed match, no score available.
		// match high-lights are used for upcoming matches.
		if (x < 10) {
			if (sched[y][0] == "Blank") {
				document.write("<TD bgcolor=#eeeee align=center>")
			}
			else {
				// use high-lights for this match (current/next round)
				document.write("<TD bgcolor=#ffff99 align=center>")
				document.write("<a href=http://www.chicagochessleague.org/cicl/match/results/"+sched[y][0]+sched[y][2]+"0"+z+".htm>")
				x++	// keep running tally
			}
		} 
		else {
			// use white color for this match (future rounds)
			document.write("<TD bgcolor=#ffffff align=center>")
			document.write("<a href=http://www.chicagochessleague.org/cicl/match/results/"+sched[y][0]+sched[y][2]+"0"+z+".htm>")
		}
		if (sched[y][0] == "Blank") {
			// no match scheduled
			document.write("BYE</TD>")
		}
		else {
			document.write("<a href=http://www.chicagochessleague.org/cicl/match/results/"+sched[y][0]+sched[y][2]+"0"+z+".htm>")
			document.write(sched[y][0]+" at<BR>"+sched[y][2]+" "+sched[y][4]+"</TD>")
		}
	} else {
		// use gray color for completed matches
		document.write("<TD bgcolor=#eeeeee align=center>")
		// provide link to match summary file, example format is 3a.txt
		// jjb document.write("<a href="+y+".txt>")
		document.write("<a href=http://www.chicagochessleague.org/cicl/match/results/"+sched[y][0]+sched[y][2]+"0"+z+".htm>")
		// print visiting team name
		document.write(sched[y][0]+" ")
		if (sched[y][1] < 0) {
			// handle negative game points
			document.write(sched[y][1])
		}
		else {
			// get result for visiting team
			base=Math.floor(sched[y][1])
			fr1=Math.round((sched[y][1]*10)%10)
			document.write(base+"."+fr1)
		}
		// print home team name
		document.write("<BR>"+sched[y][2]+" ")
		if (sched[y][3] < 0) {
			// handle negative game points
			document.write(sched[y][3])
		}
		else {
			// get result for home team
			base=Math.floor(sched[y][3])
			fr1=Math.round((sched[y][3]*10)%10)
			document.write(base+"."+fr1)
		}
		// finish the match entry
		document.write("</a></TD>")
	}
	cnt++	// increment number of processed matches
  }
  document.write("</TR></TABLE>")
  // document.write("Upper Board Forfeits: ")
  // document.write("<BR>")
} // end function

