// requires select.js::AlphaOnly()
function cSelectionClues(chk, sel)
{
	this.IdChk = chk;
	this.IdSel = sel;
	this.count = function()
	{
		var tbl = document.getElementById(this.IdSel);
		var aSel = tbl.getElementsByTagName('select');
		var correct = 0
		var c
		for (c = 0; c < aSel.length; c++)
		{
			var sel1 = aSel[c];
			if (AlphaOnly(sel1.id.toLowerCase()) == AlphaOnly(sel1[sel1.selectedIndex].value.toLowerCase()))
			{
				correct++
			}
		}
		return correct;
	}
	this.update = function()
	{
		if (this.count() == 25)
		{
			// All correct
			alert("You solved it!")
		}
	}
	this.check = function()
	{
		var ok = this.count();
		alert(ok + " selections are correct!")
	}
	this.reset = function()
	{
		var tbl = document.getElementById(this.IdSel);
		var aSel = tbl.getElementsByTagName('select');
		var c
		for (c = 0; c < aSel.length; c++)
		{
			var sel = aSel[c];
			sel.selectedIndex = 0;
		}
		var tbl1 = document.getElementById(this.IdChk);
		var aChk = tbl1.getElementsByTagName('input');
		for (c = 0; c < aChk.length; c++)
		{
			var chk = aChk[c];
			chk.checked = 0;
		}
	}
}

function cTextDigSum(expr, sel, sum, dig)
{
	this.SumText = expr;				// Text expression of sum
	this.IdSel = sel;						// id of owner of <select> objects
	this.IdSum = sum;						// id of object showing sum expression
	this.IdDig = dig;						// id of object showing remaining digits

	this.OutSum = function(txt)
	{
		var rv = "&nbsp;" + txt.substring(0, 4) + "<br>" + txt.substring(4, 9) + "<br>" + txt.substring(9, 14) + "<br>" + txt.substring(14);
		return rv;
	}
	this.update = function()
	{
		var c
		var i
		var SendText = this.SumText;
		var SendLetters = "0 1 2 3 4 5 6 7 8 9";
		var tbl = document.getElementById(this.IdSel);
		var aSel = tbl.getElementsByTagName('select');
		for (c = 0; c < SendText.length; c++)
		{
			for (i = 0; i < aSel.length; i++)
			{
				var sel = aSel[i];
				if (SendText.charAt(c) == sel.name.charAt(0))
				{
					var j = sel.selectedIndex;
					if (j > 0)
					{
						SendText=SendText.substring(0, c) + String.fromCharCode(48+j-1) + SendText.substring(c+1)
						SendLetters=SendLetters.substring(0, 2*(j-1)) + String.fromCharCode(32) + SendLetters.substring((2*(j-1))+1)
					}
				}
			}
		}
		var OutStr = this.OutSum(SendText);
		document.getElementById(this.IdSum).innerHTML = OutStr;
		OutStr = "Remaining digits:<br>" + SendLetters;
		document.getElementById(this.IdDig).innerHTML = OutStr;
	}
	this.reset = function()
	{
		document.getElementById(this.IdSum).innerHTML = this.OutSum(this.SumText);
		document.getElementById(this.IdDig).innerHTML = "Remaining digits:<br>0 1 2 3 4 5 6 7 8 9";
		var tbl = document.getElementById(this.IdSel);
		var aSel = tbl.getElementsByTagName('select');
		var i;
		for (i = 0; i < aSel.length; i++)
		{
			aSel[i].selectedIndex = 0;
		}
	}
}

function cMatrixSum(sel)
{
	this.IdSel = sel;				// ID of owner table
	this.easy = 0;					// Calculate easier matrix if bit0 set
	// Get int value of rows[r].cells[c]
	this.IntGet = function(r, c)
	{
		var tbl = document.getElementById(this.IdSel);
		var txt = tbl.rows[r].cells[c].getElementsByTagName('div')[0].innerHTML;
		var rv = 0;
		if (txt.indexOf("$") >= 0)
		{
			txt = txt.substring(txt.indexOf("$")+1);
			rv = Math.round(100*parseFloat(txt));
		}
		else
		{
			rv = parseInt(txt);
		}
		return rv;
	}
	// Set text of rows[r].cells[c] from int/float v
	this.IntSet = function(r, c, v, d)
	{
		var txt;
		if (typeof(d) != "undefined")
		{
			// v is value, d is leading currency sign
			txt = d;
			v = v/100.0;
			txt += v.toFixed(2);
		}
		else
		{
			// v is integer value
			txt = v.toString();
		}
		var tbl = document.getElementById(this.IdSel);
		tbl.rows[r].cells[c].getElementsByTagName('div')[0].innerHTML = txt;
	}
	// Update checkmarks and remaining coins from select values
	this.update = function()
	{
		var tbl = document.getElementById(this.IdSel);
		var aColSum = new Array(0, 0, 0, 0, 0);
		var aCoinSum = new Array(0, 0, 0, 0, 0, 0);
		var r;
		var c;
		for (r = 0; r < 5; r++)
		{
			var RowSum = 0;
			for (c = 0; c < 5; c++)
			{
				var n = tbl.rows[r].cells[c];
				var aSel = n.getElementsByTagName('select');
				var s = aSel[0];
				if (s.selectedIndex)
				{
					var val = parseInt(s[s.selectedIndex].value);
					RowSum += val;
					aColSum[c] += val;
					aCoinSum[s.selectedIndex]++;
				}
				if (r == 4)
				{
					// Last row, aColSum complete
					tbl.rows[6].cells[c].getElementsByTagName('input')[0].checked = (aColSum[c] == this.IntGet(5, c)) ? 1 : 0;
				}
			}
			tbl.rows[r].cells[6].getElementsByTagName('input')[0].checked = (RowSum == this.IntGet(r, 5)) ? 1 : 0;
		}
		for (r = 1; r < 6; r++)
		{
			this.IntSet(r, 9, 5-aCoinSum[r]);
		}
	}
	// Reset all selects, checks and remaining coin counts
	this.reset = function()
	{
		var tbl = document.getElementById(this.IdSel);
		var aSel = tbl.getElementsByTagName('select');
		var c;
		for (c = 0; c < aSel.length; c++)
		{
			aSel[c].selectedIndex = 0;
		}
		this.update();
	}
	// Display solution (index stored in name field of select)
	this.solve = function()
	{
		var tbl = document.getElementById(this.IdSel);
		var aSel = tbl.getElementsByTagName('select');
		var c;
		for (c = 0; c < aSel.length; c++)
		{
			var sel = aSel[c];
			sel.selectedIndex = parseInt(sel.name);
		}
		this.update();
	}
	// Calculate and display row and column sums
	this.calculate = function()
	{
		var tbl = document.getElementById(this.IdSel);
		var aColSum = new Array(0, 0, 0, 0, 0);
		var r;
		var c;
		for (r = 0; r < 5; r++)
		{
			var RowSum = 0;
			for (c = 0; c < 5; c++)
			{
				var s = tbl.rows[r].cells[c].getElementsByTagName('select')[0];
				var val = parseInt(s[parseInt(s.name)].value);
				RowSum += val;
				aColSum[c] += val;
				if (r == 4)
				{
					// Last row, aColSum complete
					this.IntSet(5, c, aColSum[c], "$");
				}
			}
			this.IntSet(r, 5, RowSum, "$");
		}
		this.update();
	}
	this.RandomNameSet = function(value)
	{
		var tbl = document.getElementById(this.IdSel);
		var aSel = tbl.getElementsByTagName('select');

		var c = Math.round(Math.random() * 24);

		while (aSel[c].name != "0")
		{
			// Find next unassigned element
			c = (c+1) % 25
		}
		var txt = value.toString();
		aSel[c].name = txt;
	}

	this.NewGame = function()
	{
		// Randomize to create new game
		var tbl = document.getElementById(this.IdSel);
		var aSel = tbl.getElementsByTagName('select');

		var i			// Coin counter
		var j			// Iteration counter
		var b			// Beginning index for random loop
		var e			// End index for random loop
		var r			// Random row
		var c			// Random column

		this.reset();
		for (i = 0; i < 25; i++)
		{
			// Clear all
			aSel[i].name = "0";
		}
		// Assume totally random
		b = 0;
		e = 4;
		if (this.easy & 1)
		{
			// Easy
			r = Math.round(Math.random() * 4);		// 1st row for pennies
			c = Math.round(Math.random() * 4);		// 1st column for pennies
			b = 5 * r;														// Starting index
			e = Math.round(Math.random() * 2);		// Open column in 2nd row of pennies
			for (j = 0; j < 3; j++)
			{
				aSel[b+c].name = "1";
				if (j != e)
				{
					// 2 rows difference between rows of pennies
					aSel[(b+c+10) % 25].name = "1";
				}
				c = (c + 2) % 5;
			}
			e = Math.round(Math.random() * 4);	// Open column in row of half-dollars
			b = (5 * (r + 4)) % 25;							// Starting index for half-dollars
			for (j = 0; j < 5; j++)
			{
				if (j != e)
				{
					aSel[b].name = "5";
				}
				b = (b+1) % 25;
			}
			this.RandomNameSet(5);							// Final half-dollar placed randomly
			b = 1;															// Pennies done
			e = 3;															// Half-dollars done
		}
		// Randomly place remaining coins
		for (i = b; i <= e; i++)
		{
			for (j = 0; j < 5; j++)
			{
				this.RandomNameSet(i+1);
			}
		}
		this.easy++;													// Toggle easy/hard
		this.calculate();
	}
}

function cPlayTNT()
{
	this.ChalScoreHi = 0			// Challenger's score (hi digit)
	this.ChalScoreLo = 0			// Challenger's score (lo digit)
	this.ProfScoreHi = 0			// Professor's score (hi digit)
	this.ProfScoreLo = 0			// Professor's score (lo digit)
	this.CountLo = 0					// Count lo digit
	this.CountHi = 0					// Count hi digit
	this.GifIndex = 4					// GIF animation index for fists.src
	this.LastAdd = 1					// Challenger's last request
	this.vTimeOut = 0					// Running, not timeout
	this.timerID=null					// Timer ID
	this.fingers=new Array(4)								// Index as (2*Challenger)-1 + (Prof-1)
	this.ScoreGif=new Array(10)					// Score digit images

	for (var i = 0; i < 10; i++)
	{
		this.ScoreGif[i] = new Image()
	}
	// Preload images
	this.ScoreGif[0].src = "Score0.gif"
	this.ScoreGif[1].src = "Score1.gif"
	this.ScoreGif[2].src = "Score2.gif"
	this.ScoreGif[3].src = "Score3.gif"
	this.ScoreGif[4].src = "Score4.gif"
	this.ScoreGif[5].src = "Score5.gif"
	this.ScoreGif[6].src = "Score6.gif"
	this.ScoreGif[7].src = "Score7.gif"
	this.ScoreGif[8].src = "Score8.gif"
	this.ScoreGif[9].src = "Score9.gif"

	for (var i = 0; i < 6; i++)
	{
		this.fingers[i] = new Image()
	}
	// Preload images
	this.fingers[0].src = "Both1.gif"				// Both 1
	this.fingers[1].src = "BothLeft2.gif"		// Prof 2, Challenger 1
	this.fingers[2].src = "BothRight2.gif"	// Prof 1, Challenger 2
	this.fingers[3].src = "Both2.gif"				// Both 2
	this.fingers[4].src = "tntplay2.gif"		// Animation
	this.fingers[5].src = "tntplay.gif"			// Copy of animation
 
	this.display = function()
	{
		document.ProfHi.src = this.ScoreGif[this.ProfScoreHi].src
		document.ProfLo.src = this.ScoreGif[this.ProfScoreLo].src
		document.ChalHi.src = this.ScoreGif[this.ChalScoreHi].src
		document.ChalLo.src = this.ScoreGif[this.ChalScoreLo].src
		document.TurnHi.src = this.ScoreGif[this.CountHi].src
		document.TurnLo.src = this.ScoreGif[this.CountLo].src
	}

	this.NewGame = function()
	{
		if (this.timerID != null) clearTimeout(this.timerID)
		this.timerID = null
		this.LastAdd = 1				// Challengers last request, Professor's next request
		this.ChalScoreHi = 0		// Challenger's score (hi digit)
		this.ChalScoreLo = 0		// Challenger's score (lo digit)
		this.ProfScoreHi = 0		// Professor's score (hi digit)
		this.ProfScoreLo = 0		// Professor's score (lo digit)
		this.CountLo = 0				// Count lo digit
		this.CountHi = 0				// Count hi digit
		this.vTimeOut = 0			// Running
		document.getElementsByName("TO")[0].value = "Time Out"
		document.fists.src = this.fingers[this.GifIndex].src
		this.GifIndex ^= 1;
		this.display()
	}

	this.NextRound = function()
	{
		if (this.timerID != null) clearTimeout(this.timerID);
		this.timerID = null;
		document.fists.src = this.fingers[this.GifIndex].src;
		this.GifIndex ^= 1;
	}

	this.StopTimer = function()
	{
		if (this.timerID != null) clearTimeout(this.timerID)
		this.timerID = null
	}

	this.TimeOut = function()
	{
		this.vTimeOut ^= 1
		if (this.vTimeOut)
		{
			// Timeout
			if (this.timerID != null) clearTimeout(this.timerID)
			this.timerID = null
			document.getElementsByName("TO")[0].value = " Resume "
			document.fists.src = "FistsHi.gif"
		}
		else
		{
			// Resuming after timeout
			document.getElementsByName("TO")[0].value = "Time Out"
			document.fists.src = this.fingers[this.GifIndex].src;
			this.GifIndex ^= 1;
		}
	}

	this.BtnPress = function(add)
	{
		var ps = 0
		var cs = 0

		if (!this.vTimeOut)
		{
			document.fists.src = this.fingers[2*(add-1) + this.LastAdd-1].src
	
			if (add == 1)
			{
				// Challenger asked for 1
				if (this.LastAdd == 1)
				{
					// Professor asks for 1
					cs = 1
					ps = 1
				}
				else ps = 2			// Professor asks for 2
			}
			else
			{
				// Challenger asked for 2
				if (this.LastAdd == 1) cs = 2		// Professor asks for 1
			}
	
			this.LastAdd = add
	
			this.ChalScoreLo += cs
			if (this.ChalScoreLo > 9)
			{
				this.ChalScoreHi++
				this.ChalScoreLo -= 10
			}	
	
			this.ProfScoreLo += ps
			if (this.ProfScoreLo > 9)
			{
				this.ProfScoreHi++
				this.ProfScoreLo -= 10
			}	
	
			this.CountLo++

			if (this.CountLo > 9)
			{
				this.CountHi++
				this.CountLo -= 10
			}
	
			if ((10*this.CountHi + this.CountLo) == 99) this.NewGame()
			else
			{
				if (((10*this.ProfScoreHi + this.ProfScoreLo) >= 98) || ((10*this.ChalScoreHi + this.ChalScoreLo) >= 98)) this.NewGame()
				else
				{
					this.display()
					this.timerID=setTimeout("NextRound()", 1500)
				}
			}
		}
	}
}

function cShapeShuffle(tbl, empt)
{
	this.IdTbl = tbl;
	this.IdEmpty = empt;
	this.NoTile = null;
	this.move = function(cell)
	{
		var tbl = document.getElementById(this.IdTbl);
		var empty = document.getElementById(this.IdEmpty);
		if (this.NoTile == null)
		{
			this.NoTile = empty.getElementsByTagName('img')[0].src;
		}
		var r = Math.round(cell / 10);			// source row
		var c = cell % 10;			// source cell
		var snode = null;				// source image
		if (r == 5)
		{
			snode = empty.getElementsByTagName('img')[0];
		}
		else
		{
			snode = tbl.rows[r].cells[c].getElementsByTagName('img')[0];
		}
		// Find empty cell
		var i = 0;
		var j = 0;
		var dnode = null;
		for (i = 0; i < 5; i++)
		{
			for (j = 0; j < 5; j++)
			{
				var tile = tbl.rows[i].cells[j].getElementsByTagName('img')[0].src;
				if (tile == this.NoTile) break;
			}
			if (j < 5) break;
		}
		if (i == 5)
		{
			// No empty cells, move to empty node
			dnode = empty.getElementsByTagName('img')[0];
		}
		else
		{
			if ((r == 5) || ((i == r) && (Math.abs(j-c) == 1)) || ((j == c) && (Math.abs(i-r) == 1)))
			{
				dnode = tbl.rows[i].cells[j].getElementsByTagName('img')[0];
			}
		}
		if (dnode != null)
		{
			dnode.src = snode.src;
			snode.src = this.NoTile;
		}
	}
	this.shuffle = function()
	{
		var tbl = document.getElementById(this.IdTbl);
		var empty = document.getElementById(this.IdEmpty);
		if (this.NoTile == null)
		{
			this.NoTile = empty.getElementsByTagName('img')[0].src;
		}
		var aTiles = tbl.getElementsByTagName('img');
		var aTileNames = new Array();
		aTileNames.push(empty.getElementsByTagName('img')[0].src);
		empty.getElementsByTagName('img')[0].src = this.NoTile;
		for (var i = 0; i < aTiles.length; i++)
		{
			aTileNames.push(aTiles[i].src);
			aTiles[i].src = this.NoTile;
		} 
		for (var i = 0; i < aTileNames.length; i++)
		{
			var txt = aTileNames[i];
			if (txt != this.NoTile)
			{
				var j = Math.round(Math.random() * (aTiles.length-1));
				while (aTiles[j].src != this.NoTile)
				{
					j = (j + 1) % aTiles.length;
				}
				aTiles[j].src = txt;
			}
		} 
	}
}

