// start variables

bg1 = '#ffff00'; // top n bottom of puzzle, headers in help contents
bg2 = '#008f00'; // 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 {font-family: Arial; font-size: 13px; cursor: default; color: #000000;}';
	mBox += '.cDiv { padding: 0px;border: white solid 2px;margin: 0px;}';
	mBox += '.hDiv {width: 32px;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: 32px;height: 20px;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; text-align:center;font-weight: bold; display: none; height: 0px}';
	mBox += '.hidden {display: none;}';
	mBox += '</style>';


	// controls
	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="360" id="pauseMsg" style="display: none;">';
	mBox += '<tr>';
	mBox += '<td align="center">Game paused... click Continue to resume your game!</td>';
	mBox += '</tr>';
	mBox += '</table>';
	mBox += '<br>';

	mBox += '<table align="center" width="360" onclick="boxFocus(0);">';
	mBox += '<tr>';
	mBox += '<td>';

	mBox += '<table align="center">';
	mBox += '<tr>';
	mBox += '<td>';
	mBox += '<div class="counterBox">1</div>';
	// mBox += '<div id="cb1" class="counterBox">&nbsp;&nbsp;&nbsp;&nbsp;</div> &nbsp; ';
	mBox += '<div id="cb1" class="counterBox"></div>';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">2</div>';
	mBox += '<div id="cb2" class="counterBox"></div>';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">3</div>';
	mBox += '<div id="cb3" class="counterBox"></div>';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">4</div>';
	mBox += '<div id="cb4" class="counterBox"></div>';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">5</div>';
	mBox += '<div id="cb5" class="counterBox"></div>';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">6</div>';
	mBox += '<div id="cb6" class="counterBox"></div>';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">7</div>';
	mBox += '<div id="cb7" class="counterBox"></div>';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">8</div>';
	mBox += '<div id="cb8" class="counterBox"></div>';
	mBox += '</td>';
	mBox += '<td>';
	mBox += '<div class="counterBox">9</div>';
	mBox += '<div id="cb9" class="counterBox"></div>';
	mBox += '</td>';
	mBox += '</tr>';
	mBox += '</table>';

	mBox += '</td>';
	mBox += '</tr>';
	mBox += '</table>';

/*
	mBox += '';
	mBox += '</ol>';
	mBox += '</form>';
	mBox += '';
	mBox += '</td>';
	mBox += '</tr>';
	mBox += '</table>';
*/

	mBox += '<form name="_controls" style="display: inline;">';
	mBox += '<table align="center" width="360" onclick="boxFocus(0);">';
	mBox += '<tr>';
	mBox += '<td><input onclick="createGame();" type="Button" value="New Game" style="width: 90px;"></td>';
	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><div class="hidden">Hints <input type="Checkbox" name="hints"></div></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>';

	document.write(mBox);
	f = document._controls;
}

function createGame(){
	games = new Array (
			"362498175891756342475312869916523784247861593583947621658234917734189256129675438",
			"894215736531876429267394158756942381943581672128637945485163297679428513312759864",
			"825673941634198275719542386386759412491826537257431698142385769563917824978264153",
			"142793658978516432653248971397482516481365297526971843765124389839657124214839765",
			"598126734642753981731984562265417893314869275879235416986342157123578649457691328",
			"526497183314628795798153264853276941142389576679514328935842617467931852281765439",
			/*
			//"534678912672195348198342567859761423426853791713924856961537284287419635345286179",
			//"358196274492567138613978425175842693826453719249731856987324561734615982561289347",
			*/
			"",
			"",
			""
			);

	gameLevel = f.gLevel[f.gLevel.selectedIndex].value;

	gameAnswers = new Array();
	gameAnswers[0] = 0;

	gameDefaults = new Array();
	gameDefaults[0] = 0;

	// which game - bad code, needs to find length of games array.
	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 || ASCIICode == 49){
		theNum = String(1);
	} else if(ASCIICode == 98 || ASCIICode == 50){
		theNum = String(2);
	} else if(ASCIICode == 99 || ASCIICode == 51){
		theNum = String(3);
	} else if(ASCIICode == 100 || ASCIICode == 52){
		theNum = String(4);
	} else if(ASCIICode == 101 || ASCIICode == 53){
		theNum = String(5);
	} else if(ASCIICode == 102 || ASCIICode == 54){
		theNum = String(6);
	} else if(ASCIICode == 103 || ASCIICode == 55){
		theNum = String(7);
	} else if(ASCIICode == 104 || ASCIICode == 56){
		theNum = String(8);
	} else if(ASCIICode == 105 || ASCIICode == 57){
		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>';
			document.getElementById(theDiv).innerText = theNum; //'<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 (a == 1 && b == 1)
						alert(document.getElementById(theDiv).innerText);
					if(document.getElementById(theDiv).innerHTML == (gameAnswers[strPos] + '')){
					*/
					if(document.getElementById(theDiv).innerText == (gameAnswers[strPos] + '')){
						correct++;
					}
					strPos++;
				}
				}

				if(correct == 81){
					document.submit_board.submit();
				} else {
					alert("Not quite right yet...");
				}
			}
		}

	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 = "Continue";
		}
	}

	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;
