// start variables

bg1 = '#ffd000'; // top n bottom of puzzle, headers in help contents
bg2 = '#ff0000'; // puzzle margin
bg3 = '#ffffff'; // puzzle area and help contents

goodColor = '#0000ff';
badColor  = '#ff0000';

// end variables

ua   = navigator.userAgent.toLowerCase();
ie6  = (ua.indexOf("msie") && document.all && ua.indexOf("netscape") == -1);
nnf  = (ua.indexOf("netscape")  != -1 && ua.indexOf("gecko") != -1);
nni  = (ua.indexOf("netscape")  != -1 && ua.indexOf("msie") != -1);
nn6  = (ua.indexOf("netscape")  != -1);
gek  = (ua.indexOf("gecko")     != -1);
ff1  = (ua.indexOf("firefox")   != -1);
opr  = (ua.indexOf("opera")     != -1);
mac  = (ua.indexOf("mac")       != -1);
web  = (ua.indexOf("webtv")     != -1);
saf  = (ua.indexOf("safari")    != -1);
kon  = (ua.indexOf("konqueror") != -1);
nn4  = (document.layers);

hDivs = new Array(82);
for(i = 0; i < 82; i++){
	hDivs[i] = new Array(10);
	
	for(j = 0; j < 10; j++){
		hDivs[i][j] = 0;
	}
}

function drawBoard(){
	cFocus = 0;
	dFocus = 0;
	
	// styles
	mBox  = '<style>';
	mBox += 'table, div {font-family: Arial; font-size: 13px; cursor: default; color: #000000;}';
	mBox += '.cDiv { padding: 0px;border: white solid 2px;margin: 0px;}';
	mBox += '.hDiv {width: 40px;height: 12px;display: block;font-size: 8px;padding: 0px;border: #ffffff solid 1px;margin: 0px;}';
	mBox += '.hDig {display: inline;font-size: 8px;padding: 0px;border: 0px;margin: 0px;}';
	mBox += '.pDiv {width: 40px;height: 28px;display: block;text-align:center;vertical-align:middle;font-size: 16px;font-weight: bold;padding :0px;border: #ffffff solid 1px;margin: 0px;}';
	mBox += '.clock {width: 62px;display: inline;font-weight: bold;color: #ffffff;font-size: 13px;padding: 3px;background: #444444;border: #ffffff inset 2px;}';
	mBox += '.counterBox {border: black solid 1px; display : inline;width: 18px;height: 18px;text-align:center;font-weight: bold;}';
	mBox += '</style>';
	
	// controls
	mBox += '<form name="_controls" style="display: inline;">';
	mBox += '<table align="center" bgcolor="' + bg2 + '" cellpadding="0" cellspacing="0" width="460" style="border: #000000 solid 1px;">';
	mBox += '<tr>';
	mBox += '<td>';
	
	mBox += '<table bgcolor="' + bg1 + '" align="center" width="460" style="border-bottom: #000000 solid 1px;" onclick="boxFocus(0);">';
	mBox += '<tr>';
	mBox += '<td><select name="gLevel">';
	mBox += '<option value="1"> Easy';
	mBox += '<option value="2"> Medium';
	mBox += '<option value="3"> Hard';
	mBox += '</select></td>';
	mBox += '<td>Hints <input type="Checkbox" name="hints" checked></td>';
	mBox += '<td><input onclick="createGame();" type="Button" value="New Game" style="width: 90px;"></td>';
	mBox += '<td><input onclick="showHelp();" type="Button" value="Help" id="hButton"></td>';
	mBox += '<td><input onclick="pauseCounter();" type="Button" value="Pause" style="width: 90px;" id="pButton"></td>';
	mBox += '<td align="right"><div id="clock" class="clock">00:00</div></td>';
	mBox += '</tr>';
	mBox += '</table>';
	
	mBox += '</form><br>';

	mBox += '<table align="center" bgcolor="' + bg3 + '" border="0" cellpadding="0" cellspacing="0" style="border: #888888 solid 3px;" id="pTable">';
	
	cID = 1;
	for(a = 1; a < 10; a++){
		mBox += '<tr>';
		
		for(b = 1; b < 10; b++){
			
			cStyle = '';
			
			if(a < 9){
				cStyle += 'border-bottom: #888888 solid 1px;';
			}
			if(a == 3 || a == 6){
				cStyle += 'border-bottom: #888888 solid 3px;';
			}
			if(b < 9){
				cStyle += 'border-right: #888888 solid 1px;';
			}
			if(b == 3 || b == 6){
				cStyle += 'border-right: #888888 solid 3px;';
			}
			
			mBox += '<td id="cell' + cID + '" style="' + cStyle + '" onclick="boxFocus(' + cID + ')">';
			mBox += '<div id="cDiv' + cID + '" class="cDiv">';
			mBox += '<div id="hDiv' + cID + '" class="hDiv" onclick="dFocus = 1"></div>';
			mBox += '<div id="pDiv' + cID + '" class="pDiv" onclick="dFocus = 2"></div>';
			mBox += '</div>';
			mBox += '</td>';
			
			cID++;
		}
		
		mBox += '</tr>';
	}
	
	mBox += '</table>';
	
	mBox += '<table align="center" width="460" id="pauseMsg" style="display: none;">';
	mBox += '<tr>';
	mBox += '<td align="center">Game paused... click Restart to re-commence same Game</td>';
	mBox += '</tr>';
	mBox += '</table>';
	mBox += '<br>';
	
	mBox += '<table bgcolor="' + bg1 + '" align="center" width="460" style="border-top: #000000 outset 1px;" onclick="boxFocus(0);">';
	mBox += '<tr>';
	mBox += '<td height="26">';
	
	mBox += '<table align="center" style="font-weight: bold;">';
	mBox += '<tr>';
	mBox += '<td>';
	mBox += '<div class="counterBox">&nbsp;1&nbsp;</div> ';
	mBox += '<div id="cb1" class="counterBox">&nbsp;&nbsp;&nbsp;&nbsp;</div> &nbsp; ';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">&nbsp;2&nbsp;</div> ';
	mBox += '<div id="cb2" class="counterBox">&nbsp;&nbsp;&nbsp;&nbsp;</div> &nbsp; ';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">&nbsp;3&nbsp;</div> ';
	mBox += '<div id="cb3" class="counterBox">&nbsp;&nbsp;&nbsp;&nbsp;</div> &nbsp; ';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">&nbsp;4&nbsp;</div> ';
	mBox += '<div id="cb4" class="counterBox">&nbsp;&nbsp;&nbsp;&nbsp;</div> &nbsp; ';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">&nbsp;5&nbsp;</div> ';
	mBox += '<div id="cb5" class="counterBox">&nbsp;&nbsp;&nbsp;&nbsp;</div> &nbsp; ';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">&nbsp;6&nbsp;</div> ';
	mBox += '<div id="cb6" class="counterBox">&nbsp;&nbsp;&nbsp;&nbsp;</div> &nbsp; ';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">&nbsp;7&nbsp;</div> ';
	mBox += '<div id="cb7" class="counterBox">&nbsp;&nbsp;&nbsp;&nbsp;</div> &nbsp; ';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">&nbsp;8&nbsp;</div> ';
	mBox += '<div id="cb8" class="counterBox">&nbsp;&nbsp;&nbsp;&nbsp;</div> &nbsp; ';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">&nbsp;9&nbsp;</div> ';
	mBox += '<div id="cb9" class="counterBox">&nbsp;&nbsp;&nbsp;&nbsp;</div>';
	mBox += '</td>';
	mBox += '</tr>';
	mBox += '</table>';
	
	mBox += '</td>';
	mBox += '</tr>';
	mBox += '</table>';
	
	mBox += '</td>';
	mBox += '</tr>';
	mBox += '</table>';
	
	mBox += '<table align="center" width="460">';
	mBox += '<tr>';
	mBox += '<td align="right">Thank you for playing! <a href="http://#">Incognito Sudoku</a></td>';
	mBox += '</tr>';
	mBox += '</table>';
	
	mBox += '<table align="center" bgcolor="' + bg3 + '" border="0" cellpadding="3" cellspacing="0" width="460" id="helpMsg" style="display: none; border: #000000 solid 1px;">';
	mBox += '<tr>';
	mBox += '<td bgcolor="' + bg1 + '" style="border-bottom: #000000 solid 1px;">';
	mBox += '<b>Instructions</b>';
	mBox += '</td>';
	mBox += '</tr>';
	mBox += '';
	mBox += '<tr>';
	mBox += '<td>';
	mBox += '<br>';
	mBox += '<form style="display: inline;">';
	mBox += '<ol>';
	mBox += '<li>Select game <i>Level</i>, <select name="gLevel"><option value="1"> Easy <option value="2"> Medium <option value="3"> Hard </select>&nbsp;Easy will remove 3 random numbers from each row, Medium will remove 4 random numbers from each row and Hard will remove 5 random numbers from each row.';
	mBox += '<br>';
	mBox += '<br>';
	mBox += '';
	mBox += '<li>If <i>Hints</i> <input type="Checkbox" checked> is checked, any incorrect numbers entered into the main cage will be in <font color="' + badColor + '">Red</font> print, correct numbers will be in <font color="' + goodColor + '">Blue</font> print. If <i>Hints</i> is not checked, all entries will be in <font color="' + goodColor + '">Blue</font> print.';
	mBox += '<br>';
	mBox += '<br>';
	mBox += '';
	mBox += '<li>Click <input type="Button" value="New Game" style="width: 90px;"> to start a new game with a fresh set of numbers in the puzzle.';
	mBox += '<br>';
	mBox += '<br>';
	mBox += '';
	mBox += '<li>During the Game, click <input type="Button" value="Pause" style="width: 90px;"> to take a break. When paused, the clock will stop and the current Game will be hidden from view. Click <i>Restart</i> to recommence the same unfinished Game and restart the clock.';
	mBox += '<br>';
	mBox += '<br>';
	mBox += '';
	mBox += '<li>To enter numbers into a <div style="border: #000000 solid 1px; width: 40px; height: 40px; display: inline; vertical-align: middle; padding: 10 3 10 3px;">Cage</div>&nbsp;left click on the lower section of each cage and press any of the keys numbered 1 - 9. To remove a number from a cage, click on the cage and press the <i>Esc</i> key. You may also click on the upper section of each cage to enter possible answers. Press the possible numbers again to remove the possible answers.';
	mBox += '<br>';
	mBox += '<br>';
	mBox += '';
	mBox += '<li>The Game ends with success once all cages have been filled with their correct numbers. ';
	mBox += '<br>';
	mBox += '<br>';
	mBox += '';
	mBox += '<li>Number pairs beneath the Puzzle <div class="counterBox" style="background:' + bg1 + '">&nbsp;1&nbsp;</div> <div class="counterBox" style="background:' + badColor + '">&nbsp;3&nbsp;</div>&nbsp;let you know how many times each number from 1-9 has been entered. Once a number has been entered 9 times, the right side of the number pair will be shown in <font color="' + goodColor + '">Blue</font>. <font color="' + goodColor + '">Blue</font> does not necessarily mean they are all correct, just that 9 instances of the left side of the number pair are visible in the puzzle.';
	mBox += '';
	mBox += '</ol>';
	mBox += '</td>';
	mBox += '</tr>';
	mBox += '';
	mBox += '<tr>';
	mBox += '<td bgcolor="' + bg1 + '" style="border-top: #000000 solid 1px;border-bottom: #000000 solid 1px;">';
	mBox += '<b>Rules</b>';
	mBox += '</td>';
	mBox += '</tr>';
	mBox += '';
	mBox += '<tr>';
	mBox += '<td>';
	mBox += '<br>';
	mBox += '<ol>';
	mBox += '<li>The objective of the Game is to enter the numbers 1-9, without repeating a number in each row, column and region of 3 X 3 cages. This is the only rule.';
	mBox += '<br>';
	mBox += '<br>';
	mBox += '';
	mBox += '<table border="0" cellpadding="0" cellspacing="0">';
	mBox += '<tr>';
	mBox += '<td>Row</td>';
	mBox += '<td>&nbsp;</td>';
	mBox += '<td>Column</td>';
	mBox += '<td>&nbsp;</td>';
	mBox += '<td align="center">Region</td>';
	mBox += '<td>&nbsp;</td>';
	mBox += '<td>Cage</td>';
	mBox += '</tr>';
	mBox += '';
	mBox += '<tr>';
	mBox += '<td valign="top">';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div>';
	mBox += '</td>';
	mBox += '';
	mBox += '<td>&nbsp;</td>';
	mBox += '';
	mBox += '<td align="center" valign="top">';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><br>';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><br>';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><br>';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><br>';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><br>';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><br>';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><br>';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><br>';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div>';
	mBox += '</td>';
	mBox += '';
	mBox += '<td>&nbsp;</td>';
	mBox += '';
	mBox += '<td valign="top">';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><br>';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><br>';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div>';
	mBox += '</td>';
	mBox += '';
	mBox += '<td>&nbsp;</td>';
	mBox += '';
	mBox += '<td align="center" valign="top">';
	mBox += '<div style="border: #000000 solid 1px; width: 20px; height: 20px; display: inline;">&nbsp;&nbsp;&nbsp;&nbsp;</div>';
	mBox += '</td>';
	mBox += '</tr>';
	mBox += '</table>';
	mBox += '';
	mBox += '</ol>';
	mBox += '</form>';
	mBox += '';
	mBox += '</td>';
	mBox += '</tr>';
	mBox += '</table>';

	document.write(mBox);
	f = document._controls;
}

function createGame(){
	games = new Array (
		"362498175891756342475312869916523784247861593583947621658234917734189256129675438",
		"894215736531876429267394158756942381943581672128637945485163297679428513312759864",
		"825673941634198275719542386386759412491826537257431698142385769563917824978264153",
		"142793658978516432653248971397482516481365297526971843765124389839657124214839765",
		"598126734642753981731984562265417893314869275879235416986342157123578649457691328",
		"526497183314628795798153264853276941142389576679514328935842617467931852281765439",
		"",
		"",
		"",
		"",
		""
	);
	
	gameLevel = f.gLevel[f.gLevel.selectedIndex].value;
	
	gameAnswers = new Array();
	gameAnswers[0] = 0;
	
	gameDefaults = new Array();
	gameDefaults[0] = 0;
	
	// which game
	gameNum = Math.floor(Math.random() * 10);
	while(games[gameNum] == ''){
		gameNum = Math.floor(Math.random() * 10);
	}
	
	dObj = new Date();
	mSec = dObj.getMilliseconds();
	mSec = mSec.toString();
	mSec = mSec.charAt(0);
	
	if(mSec % 2 != 0){
		// reverse the array to double number of games
		revStr = '';
		for(a = 81; a >= 0; a--){
			revStr += games[gameNum].charAt(a);
		}
		games[gameNum] = revStr;
	}
	
	strPos = 1;
	for(a = 1; a < 10; a ++){
		
		getSpaces();
		
		for(b = 1; b < 10; b ++){
			theDiv = 'pDiv' + strPos;
			
			if(b == spaces[1] || b == spaces[2] || b == spaces[3] || b == spaces[4] || b == spaces[5]){
				document.getElementById(theDiv).innerHTML = "";
				gameDefaults[strPos] = 0;
			} else {
				document.getElementById(theDiv).innerHTML = games[gameNum].charAt(strPos - 1);
				gameDefaults[strPos] = 1;
			}
			
			gameAnswers[strPos] = games[gameNum].charAt(strPos - 1);
			strPos++;
		}
	}
	
	if(cFocus){
		theDiv = 'cDiv' + cFocus;
		document.getElementById(theDiv).style.borderColor = "#ffffff";
		document.getElementById(theDiv).style.border = "#ffffff solid 2px";
		
		theDiv = 'hDiv' + cFocus;
		document.getElementById(theDiv).style.background = "#ffffff";
		document.getElementById(theDiv).style.borderBottom = "";
		
		theDiv = 'pDiv' + cFocus;
		document.getElementById(theDiv).style.background = "#ffffff";
		document.getElementById(theDiv).style.borderTop    = "";
		
		cFocus = 0;
	}
	
	stopCounter();
	numSecs = -1;
	counter();
	
	counters = new Array();
	counters[0] = 0;
	counters[1] = 0;
	counters[2] = 0;
	counters[3] = 0;
	counters[4] = 0;
	counters[5] = 0;
	counters[6] = 0;
	counters[7] = 0;
	counters[8] = 0;
	counters[9] = 0;
	
	// count number sets
	strPos  = 1;
	for(a = 1; a < 10; a ++){
		for(b = 1; b < 10; b ++){
			theDiv = 'pDiv' + strPos;
			
			if(ie6){
				numEntered = document.getElementById(theDiv).innerText;
			} else {
				numEntered = document.getElementById(theDiv).innerHTML;
				numEntered = numEntered.replace(/.*?>/, "");
				numEntered = numEntered.replace(/<.*/, "");
			}
			
			if(numEntered){
				counters[numEntered]++;
			}
			// reset all helpDigits divs
			document.getElementById('hDiv' + strPos).innerHTML = '';
			strPos++;
		}
	}
	
	for(a = 1; a < counters.length; a ++){
		if(counters[a] == 9){
			cbClr = '#00cc00';
		} else {
			cbClr = badColor;
		}
		document.getElementById('cb' + a).innerHTML = '&nbsp;' + counters[a] + '&nbsp;';
		document.getElementById('cb' + a).style.background = cbClr;
	}
	
	gameInProgress = 1;
}

function getSpaces(){
	RANDS = new Array(1,2,3,4,5,6,7,8,9,1);
	
	spaces = new Array();
	spaces[0] = 0;
	spaces[1] = 0;
	spaces[2] = 0;
	spaces[3] = 0;
	spaces[4] = 0;
	spaces[5] = 0;
	
	if(gameLevel == 1){
		spaces[1] = RANDS[Math.floor(Math.random() * 10)];
		while(spaces[2] == 0 || spaces[2] == spaces[1]){
			spaces[2] = RANDS[Math.floor(Math.random() * 10)];
		}
		while(spaces[3] == 0 || spaces[3] == spaces[1] || spaces[3] == spaces[2]){
			spaces[3] = RANDS[Math.floor(Math.random() * 10)];
		}
	} else if (gameLevel == 2){
		spaces[1] = RANDS[Math.floor(Math.random() * 10)];
		while(spaces[2] == 0 || spaces[2] == spaces[1]){
			spaces[2] = RANDS[Math.floor(Math.random() * 10)];
		}
		while(spaces[3] == 0 || spaces[3] == spaces[1] || spaces[3] == spaces[2]){
			spaces[3] = RANDS[Math.floor(Math.random() * 10)];
		}
		while(spaces[4] == 0 || spaces[4] == spaces[1] || spaces[4] == spaces[2] || spaces[4] == spaces[3]){
			spaces[4] = RANDS[Math.floor(Math.random() * 10)];
		}
	} else if (gameLevel == 3){
		spaces[1] = RANDS[Math.floor(Math.random() * 10)];
		while(spaces[2] == 0 || spaces[2] == spaces[1]){
			spaces[2] = RANDS[Math.floor(Math.random() * 10)];
		}
		while(spaces[3] == 0 || spaces[3] == spaces[1] || spaces[3] == spaces[2]){
			spaces[3] = RANDS[Math.floor(Math.random() * 10)];
		}
		while(spaces[4] == 0 || spaces[4] == spaces[1] || spaces[4] == spaces[2] || spaces[4] == spaces[3]){
			spaces[4] = RANDS[Math.floor(Math.random() * 10)];
		}
		while(spaces[5] == 0 || spaces[5] == spaces[1] || spaces[5] == spaces[2] || spaces[5] == spaces[3] || spaces[5] == spaces[4]){
			spaces[5] = RANDS[Math.floor(Math.random() * 10)];
		}
	}
}

function boxFocus(cID){
	if(!gameInProgress) return;
	
	if(cFocus){
		theDiv = 'cDiv' + cFocus;
		document.getElementById(theDiv).style.border = "#ffffff solid 2px";
		
		theDiv = 'hDiv' + cFocus;
		document.getElementById(theDiv).style.background   = "#ffffff";
		document.getElementById(theDiv).style.borderBottom = "#ffffff solid 1px";
		
		theDiv = 'pDiv' + cFocus;
		document.getElementById(theDiv).style.background   = "#ffffff";
		document.getElementById(theDiv).style.borderTop    = "#ffffff solid 1px";
	}
	
	if(gameDefaults[cID] || cID == 0){
		cFocus = 0;
		return;
	}
	
	cFocus = cID;
	
	theDiv = 'cDiv' + cID;
	document.getElementById(theDiv).style.border = "#000000 ridge 2px";
	
	if(dFocus == 1){
		theDiv = 'hDiv' + cID;
		document.getElementById(theDiv).style.background   = bg1;
		document.getElementById(theDiv).style.borderBottom = "#000000 solid 1px";
		
		theDiv = 'pDiv' + cID;
		document.getElementById(theDiv).style.background   = "#ffffff";
		document.getElementById(theDiv).style.borderTop    = "#ffffff solid 1px";
	}
	if(dFocus == 2){
		theDiv = 'hDiv' + cID;
		document.getElementById(theDiv).style.background   = "#ffffff";
		document.getElementById(theDiv).style.borderBottom = "#000000 solid 1px";
		
		theDiv = 'pDiv' + cID;
		document.getElementById(theDiv).style.background   = bg1;
		document.getElementById(theDiv).style.borderTop    = bg1 + " solid 1px";
	}
}

function checkEntry(event){
	if(!gameInProgress) return;
	
	hColor = goodColor;
	
	if(ie6){
		ASCIICode = window.event.keyCode;
	} else {
		ASCIICode = event.keyCode;
	}
	
	//alert(ASCIICode)
	
	// context key
	if(ASCIICode == 93 || ASCIICode == 96){
		return false;
	}
	
	// escape key
	if(ASCIICode == 27 && cFocus){
		theDiv = 'pDiv' + cFocus;
		document.getElementById(theDiv).innerHTML = '';
		return;
	}
	
	// keypads
	if(ASCIICode == 97){
		theNum = String(1);
	} else if(ASCIICode == 98){
		theNum = String(2);
	} else if(ASCIICode == 99){
		theNum = String(3);
	} else if(ASCIICode == 100){
		theNum = String(4);
	} else if(ASCIICode == 101){
		theNum = String(5);
	} else if(ASCIICode == 102){
		theNum = String(6);
	} else if(ASCIICode == 103){
		theNum = String(7);
	} else if(ASCIICode == 104){
		theNum = String(8);
	} else if(ASCIICode == 105){
		theNum = String(9);
	} else {
		theNum = String.fromCharCode(ASCIICode);
	}
	
	if(theNum.search(/\D+/) >= 0 || theNum.search(/0/) >= 0){
		return;
	}
	
	if(cFocus){
		if(dFocus == 1){
			theDiv = 'hDiv' + cFocus;
			
			if(hDivs[cFocus][theNum]){
				// remove it
				hDivs[cFocus][theNum] = 0;
			} else {
				// add it
				hDivs[cFocus][theNum] = theNum;
			}
			
			hStr = '';
			for(a = 0; a < hDivs[cFocus].length; a++){
				if(hDivs[cFocus][a] > 0){
					hStr += hDivs[cFocus][a];
				}
			}
			
			document.getElementById(theDiv).innerHTML = hStr;
		}
		
		if(dFocus == 2){
			if(f.hints.checked){
				if(gameAnswers[cFocus] != theNum){
					hColor = badColor;
				}
			}
			
			theDiv = 'pDiv' + cFocus;
			document.getElementById(theDiv).innerHTML = '<font color="' + hColor + '">' + theNum + '</font>';
		}
	}
	
	// completed ?
	counters = new Array();
	counters[0] = 0;
	counters[1] = 0;
	counters[2] = 0;
	counters[3] = 0;
	counters[4] = 0;
	counters[5] = 0;
	counters[6] = 0;
	counters[7] = 0;
	counters[8] = 0;
	counters[9] = 0;
	
	strPos  = 1;
	filled  = 0;
	for(a = 1; a < 10; a ++){
		for(b = 1; b < 10; b ++){
			theDiv = 'pDiv' + strPos;
			
			if(ie6){
				numEntered = document.getElementById(theDiv).innerText;
			} else {
				numEntered = document.getElementById(theDiv).innerHTML;
				numEntered = numEntered.replace(/.*?>/, "");
				numEntered = numEntered.replace(/<.*/, "");
			}
			
			if(numEntered){
				filled++;
				counters[numEntered]++;
			}
			strPos++;
		}
	}
	
	for(a = 1; a < counters.length; a ++){
		if(counters[a] == 9){
			cbClr = '#00cc00';
		} else {
			cbClr = badColor;
		}
		document.getElementById('cb' + a).innerHTML = '&nbsp;' + counters[a] + '&nbsp;';
		document.getElementById('cb' + a).style.background = cbClr;
	}
	
	strPos  = 1;
	correct = 0
	if(filled == 81){
		stopCounter();
		
		for(a = 1; a < 10; a ++){
			for(b = 1; b < 10; b ++){
				theDiv = 'pDiv' + strPos;
				if(document.getElementById(theDiv).innerText == gameAnswers[strPos]){
					correct++;
				}
				strPos++;
			}
		}
		
		if(correct == 81){
			alert("Well done!");
		} else {
			alert("Bad Luck!");
		}
	}
}

function counter(){
	numSecs++;
	
	secs = numSecs % 60;
	mins = Math.floor(numSecs / 60);
	ours = Math.floor(mins / 60);
	
	if(ours > 0){ mins = mins - ours * 60; }
	
	if(secs < 10) secs = "0" + secs;
	if(mins < 10) mins = "0" + mins;  
	if(ours < 10) ours = "0" + ours;  
	
	if(ours > 0){
		clockStr = ours + ':' + mins + ':' + secs;
	} else {
		clockStr = mins + ':' + secs;
	}
	
	document.getElementById('clock').innerHTML = clockStr;
	timer = window.setTimeout("counter(" + numSecs + ")", 1000);
}

function stopCounter(){
	if(timer) clearTimeout(timer);
}

function pauseCounter(){
	if(!gameInProgress) return ;
	if(timerPaused){
		counter(numSecs);
		timerPaused = 0;
		document.getElementById('pTable').style.display = "";
		document.getElementById('pauseMsg').style.display = "none";
		document.getElementById('pButton').value = "Pause";
	} else {
		if(timer){ clearTimeout(timer); }
		timerPaused = 1;
		document.getElementById('pTable').style.display = "none";
		document.getElementById('pauseMsg').style.display = "";
		document.getElementById('pButton').value = "Restart";
	}
}

function showHelp(){
	if(helpVisible){
		helpVisible = 0;
		document.getElementById('helpMsg').style.display = "none";
	} else {
		helpVisible = 1;
		document.getElementById('helpMsg').style.display = "";
	}
}

function noContext(){return false;}

f                      = "";
timer                  = "";
numSecs                = -1;
gameInProgress         = 0;
timerPaused            = 0;
helpVisible            = 0;
document.onkeyup       = checkEntry;
document.oncontextmenu = noContext;
