/*-------------------------------------------*/
/*														   */
/*  Iron Sudoku JavaScript Framework v1.1.1  */
/*  Epigroove <http://www.epigroove.com>     */
/*														   */
/*-------------------------------------------*/

var tile_type = "";
var difficulty = "";
var solution = "";
var saved_state = "";
var puzzle_id = "";
var puzzle_date = "";
var puzzle_closed = 0;
var unsaved_work = false;
var player_id = "";
var account_type = "";
var completed = "";

// set up puzzle object
// grid[row][col] = the number in that square
// grid[row][col]['mini'] = array of "maybe's" and "is not's". -1 = is not, 0 = nothing, 1 = maybe
var master_grid = new Array(); // contans the initial puzzle. fetched during Initialize()
var grid = new Array(); // contans the current state of the puzzle if the user has saved their progress. fetched during Initialize()
master_grid[1] = new Array(10); master_grid[2] = new Array(10); master_grid[3] = new Array(10); master_grid[4] = new Array(10); master_grid[5] = new Array(10); master_grid[6] = new Array(10); master_grid[7] = new Array(10); master_grid[8] = new Array(10); master_grid[9] = new Array(10);
grid[1] = new Array(10); grid[2] = new Array(10); grid[3] = new Array(10); grid[4] = new Array(10); grid[5] = new Array(10); grid[6] = new Array(10); grid[7] = new Array(10); grid[8] = new Array(10); grid[9] = new Array(10);
for (i=1; i<=9; i++) {
	for (j=1; j<=9; j++) {
		master_grid[i][j] = new Object();
		grid[i][j] = new Object();
		master_grid[i][j]['mini'] = Array(10);
		grid[i][j]['mini'] = Array(10);
		for (k=1; k<=9; k++) {
			master_grid[i][j]['mini'][k] = 0;
			grid[i][j]['mini'][k] = 0;
		}
	}
}

function InitializeSudoku () {
	var myAjax = new Ajax.Request ('/action/initialize_sudoku', { method: 'get', onComplete: InitializeSudokuPostOp } );
}

function InitializeSudokuPostOp (result) {
	//$('testing').innerHTML += result.responseText + '<br>';
	var response = result.responseText;
	
	if (response == "invalid_id") { // player selected an invalid puzzle
		window.location = "http://" + window.location.host + "/error";
		return;
	}
	
	try {
		var data = eval("(" + result.responseText + ")"); // convert results into a javascript object
	}
	catch (e) {
		if (player_id == 1) $('testing').innerHTML += "error during initialize sudoku post-op<br>";
		return;
	}

	var grid_array = new Array();
	var subscripts_array = new Array();
	var key = 0;
	var sub_key = 0;
	var val = "";
	
	puzzle_id = data.puzzle_id;
	puzzle_closed = data.closed;
	puzzle_date = data.puzzle_date;
	master_string = data.grid;
	saved_state = data.saved_state;
	difficulty = data.difficulty;
	solution = data.solution;
	tile_type = data.tile_type;
	player_id = data.player_id;
	account_type = data.account_type;
	
	$('difficulty').innerHTML = "Difficulty: " + difficulty;
	$('puzzle_date').innerHTML = puzzle_date;
	
	if (saved_state == solution) {
		completed = 1;
	}
	else if (account_type != 1 && puzzle_closed == 1) {
		ShowBillboard('pro_only');
	}
	
	// now that we know the tile_type - preload the mini tile images
	PreloadImages();
		
	// convert the master grid string into the temp grid array, and then transfer to the master grid array
	grid_array = master_string.split(',');
	
	for (i=1; i<=9; i++) {
		for (j=1; j<=9; j++) {
			key = (i-1)*9+(j-1);
			master_grid[i][j]['value'] = grid_array[key];
		}
	}
	
	// convert the saved state string into the temp grid array, and then transfer to the grid array
	if (saved_state) {
		StateToGrid();
	}
	else {
		for (i=1; i<=9; i++) {
			for (j=1; j<=9; j++) {
				grid[i][j]['value'] = master_grid[i][j]['value'];
			}
		}
	}
	
	PopulatePuzzle(grid);
	ClockSniffer();
}

function PreloadImages() {
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/1_mini.gif" height="40" width="40" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/2_mini.gif" height="40" width="40" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/3_mini.gif" height="40" width="40" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/4_mini.gif" height="40" width="40" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/5_mini.gif" height="40" width="40" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/6_mini.gif" height="40" width="40" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/7_mini.gif" height="40" width="40" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/8_mini.gif" height="40" width="40" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/9_mini.gif" height="40" width="40" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/1_micro_red.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/2_micro_red.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/3_micro_red.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/4_micro_red.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/5_micro_red.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/6_micro_red.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/7_micro_red.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/8_micro_red.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/9_micro_red.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/1_micro_green.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/2_micro_green.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/3_micro_green.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/4_micro_green.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/5_micro_green.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/6_micro_green.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/7_micro_green.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/8_micro_green.gif" height="13" width="13" />';
	$('tile_preloader').innerHTML += '<img src="/tiles/' + tile_type + '/9_micro_green.gif" height="13" width="13" />';
}

function ShowSolution () {
	var solution_grid = solution.split(',');
	
	for (i=1; i<=9; i++) {
		for (j=1; j<=9; j++) {
			divid = 'r' + parseInt(i) + 'c' + parseInt(j);
			key = (i-1)*9+(j-1);
			val = solution_grid[key].toString();
			$(divid).innerHTML = '<img src="/tiles/' + tile_type + '/' + val + '.gif" height="40" width="40" />';
			$(divid).style.backgroundColor = "#e3e3e3";
		}
	}
}

function PopulatePuzzle (grid) {
	var index = "";
	var divid = "";
	var square_html = "";
	var func = '';
	var mod_key = '';
	
	// fill in numbers
	for (i=1; i<=9; i++) {
		for (j=1; j<=9; j++) {
			divid = 'r' + i + 'c' + j;
			if (master_grid[i][j].value) {
				$(divid).innerHTML = '<img src="/tiles/' + tile_type + '/' + grid[i][j].value + '.gif" height="40" width="40" />';
				$(divid).style.backgroundColor = "#e3e3e3";
			}
			else if (completed) {
				$(divid).innerHTML = '<img src="/tiles/' + tile_type + '/' + grid[i][j].value + '.gif" height="40" width="40" />';
			}
			else {
				if (!player_id) func = 'PopUpSignUp(' + i + ',' + j + ')';
				else if (account_type != 1 && puzzle_closed) func = 'PopUpUpgrade(' + i + ',' + j + ')';
				else func = 'PopUpSquare(' + i + ',' + j + ')';
				if (grid[i][j].value) {
					$(divid).innerHTML = '<img src="/tiles/' + tile_type + '/' + grid[i][j].value + '.gif" height="40" width="40" onClick="' + func + '" style="cursor:pointer" />';
				}
				else {
					$(divid).innerHTML = '<img src="/tiles/blank.gif" height="40" width="40" onClick="' + func + '" style="cursor:pointer" />';
					for (k=1; k<=9; k++) {
						if (grid[i][j]['mini'][k] != 0) {
							mod_key = (grid[i][j]['mini'][k] == -1) ? 'alt' : 'shift';
							CreateTinySquare(i,j,k,mod_key);
						}
					}
				}
			}
		}
	}
}

function PopUpSquare (r,c,x,y) {
	var square_html = "";
	var offsetLeft = $('puzzle').offsetLeft + (c*41);
	var offsetTop = $('puzzle').offsetTop + (r*41) - 80;
	
	// if the browser window is wide enough, and the user clicked in the first column, hang the big_square off the left edge.
	offsetLeft = (offsetLeft < 70) ? 0 : offsetLeft - 70;
	
	$('big_square').style.left = offsetLeft + 'px';
	$('big_square').style.top = offsetTop + 'px';

	// create the beef of the newly added node
	for (i=1; i<=9; i++) {
		square_html += '<div class="mini_square sq'+i+'" id="mini'+i+'" onClick="SetNumber('+r+','+c+','+i+',ModKey(event));" style="cursor:pointer"><img src="/tiles/' + tile_type + '/'+i+'_mini.gif" height="30" width="30" /></div>';
	}
	square_html += '<div id="clear" onClick="ClearSquare('+r+','+c+');" style="cursor:pointer">CLEAR</div>';
	square_html += '<div id="cancel" onClick="CloseBigSquare();" style="cursor:pointer">CANCEL</div>';
	
	// add the beef and flip it on
	$('big_square').innerHTML = square_html;
	$('big_square').style.display = 'block';

	// highlight already selected numbers
	for (i=1; i<=9; i++) {
		if (grid[r][c]['value'] == i) $('mini'+i).style.backgroundColor = "#FFFF00";
		else if (grid[r][c]['mini'][i] == 1) $('mini'+i).style.backgroundColor = "#CCFF00";
		else if (grid[r][c]['mini'][i] == -1) $('mini'+i).style.backgroundColor = "#FFCCCC";
	}
}

function PopUpUpgrade (r,c) {
	var square_html = "";
	var offsetLeft = $('puzzle').offsetLeft + (c*41) - 7;
	var offsetTop = $('puzzle').offsetTop + (r*41) - 87;
	
	// if the browser window is wide enough, and the user clicked in the first column, hang the big_square off the left edge.
	offsetLeft = (offsetLeft < 70) ? 0 : offsetLeft - 70;
	
	$('big_square').style.left = offsetLeft + 'px';
	$('big_square').style.top = offsetTop + 'px';

	// create the beef
	square_html = '<div style="padding: 15px 10px; font-size: 13px; line-height: 120%; text-align: center;" onClick="$(\'big_square\').style.display=\'none\';"><p><a href="/upgrade" onClick="$(\'big_square\').style.display=\'none\';">Upgrade to Pro</a></p><p>Only $14.95</p><span style="font-size: 11px">[ <a href="javascript:void(0)" onClick="$(\'big_square\').style.display=\'none\';">close</a> ]</span></div>';
	
	// add the beef and flip it on
	$('big_square').innerHTML = square_html;
	$('big_square').style.display = 'block';
}

function PopUpSignUp (r,c) {
	var square_html = "";
	var offsetLeft = $('puzzle').offsetLeft + (c*41) - 7;
	var offsetTop = $('puzzle').offsetTop + (r*41) - 87;
	
	// if the browser window is wide enough, and the user clicked in the first column, hang the big_square off the left edge.
	offsetLeft = (offsetLeft < 70) ? 0 : offsetLeft - 70;
	
	$('big_square').style.left = offsetLeft + 'px';
	$('big_square').style.top = offsetTop + 'px';

	// create the beef
	square_html = '<div style="padding: 15px 10px; font-size: 13px; line-height: 120%; text-align: center;" onClick="$(\'big_square\').style.display=\'none\';"><p><a href="/sign_up" onClick="$(\'big_square\').style.display=\'none\';">Sign up for Iron Sudoku</a></p><p>It\'s free!</p><span style="font-size: 11px">[ <a href="javascript:void(0)" onClick="$(\'big_square\').style.display=\'none\';">close</a> ]</span></div>';
	
	// add the beef and flip it on
	$('big_square').innerHTML = square_html;
	$('big_square').style.display = 'block';
}

function SetNumber(r,c,num,mod_key) {
	unsaved_work = true;

	if ($('r'+r+'c'+c+'n'+num)) $('puzzle').removeChild($('r'+r+'c'+c+'n'+num));
	
	if ((mod_key == 'shift' && grid[r][c]['mini'][num] == 1) || (mod_key == 'alt' && grid[r][c]['mini'][num] == -1)) { // toggle tiny number off
		grid[r][c]['mini'][num] = 0;
		CloseBigSquare();
	}
	else if (mod_key == 'shift' || mod_key == 'alt') { // toggle tiny number on
		if (mod_key == 'shift') {
			grid[r][c]['mini'][num] = 1;
		}
		else if (mod_key == 'alt') {
			grid[r][c]['mini'][num] = -1;
		}
		grid[r][c]['value'] = 0;
		$('r'+r+'c'+c).innerHTML = '<img src="/tiles/blank.gif" height="40" width="40" onClick="PopUpSquare('+r+','+c+')" style="cursor:pointer" />';
		CreateTinySquare(r,c,num,mod_key);
		CloseBigSquare();
	}
	else {
		if (grid[r][c]['value'] == num) { // toggle number off
			grid[r][c]['value'] = 0;
			$('r'+r+'c'+c).innerHTML = '<img src="/tiles/blank.gif" height="40" width="40" onClick="PopUpSquare('+r+','+c+')" style="cursor:pointer" />';
			CloseBigSquare();
		}
		else { // toggle number on
			grid[r][c]['value'] = num;
			grid[r][c]['mini'][num] = 0;
			$('r'+r+'c'+c).innerHTML = '<img src="/tiles/' + tile_type + '/' + num + '.gif" height="40" width="40" onClick="PopUpSquare('+r+','+c+')" style="cursor:pointer" />';
			CloseBigSquare();
			ClearTinySquares(r,c);
			CheckMove(r,c);
			CheckPuzzle();
		}
		Blink('r'+r+'c'+c,4);
	}
}

function CreateTinySquare(r,c,num,mod_key) {
	var divid = "r"+r+"c"+c+"n"+num;
	var parent_div = $('puzzle');
	var color = (mod_key == 'alt') ? 'red' : 'green';
	var class_value = "tiny_square row"+r+" col"+c+" num"+num;
	
	// create "tiny_square" <div> if it doesn't already exist
	if ($(divid)) {
		var tiny_square = $(divid);
	}
	else {
		var tiny_square = document.createElement("div");
		
		tiny_square.setAttribute("id",divid);

		tiny_square.setAttribute("className", class_value);
		tiny_square.setAttribute("class", class_value);
	
		// append the new "tiny_square" node
		parent_div.appendChild(tiny_square);
	}
	
	// add the beef
	tiny_square.innerHTML = '<span onClick="PopUpSquare('+r+','+c+')" style="cursor:pointer"><img src="/tiles/' + tile_type + '/' + num + '_micro_' + color + '.gif" height="13" width="13" /></span>';
}

function ClearSquare(r,c) {
	grid[r][c]['value'] = '';
	for (i=1; i<=9; i++) {
		grid[r][c]['mini'][i] = 0; // clear all the mini numbers
	}
	ClearTinySquares(r,c);
	$('r'+r+'c'+c).innerHTML = '<img src="/tiles/blank.gif" height="40" width="40" onClick="PopUpSquare('+r+','+c+')" style="cursor:pointer" />';
	CloseBigSquare();
}

function CloseBigSquare() {
	$('big_square').style.display = 'none';
}

function ToggleModLink(name) {
	if ($(name).className == "") {
		$(name).className = "on";
		
		if (name == "maybe") { // if the user clicked "maybe", clear other two buttons
			$('is_not').className = "";
			$('is').className = "";
			maybe = 1;
			is_not = 0;
			iz = 0;
		}
		else if (name == "is_not") { // if the user clicked "is not", clear other two button BG
			$('maybe').className = "";
			$('is').className = "";
			maybe = 0;
			is_not = 1;
			iz = 0;
		}
		else if (name == "is") { // if the user clicked "is", clear other two button BG
			$('is_not').className = "";
			$('maybe').className = "";
			maybe = 0;
			is_not = 0;
			iz = 1;
		}
	}
	else {
		$(name).className = "";
		maybe = 0;
		is_not = 0;
		iz = 0;
	}
}

function ClearTinySquares(r,c) {
	for (x=1; x<=9; x++) {
		grid[r][c]['mini'][x] = 0;
		if ($('r'+r+'c'+c+'n'+x)) $('puzzle').removeChild($('r'+r+'c'+c+'n'+x));
	}

}

function ModKey(e) {
	if (!e) var e = window.event;
	
	if (e.shiftKey)
		return 'shift';
	else if (e.altKey)
		return 'alt';
	else
		return false;
}

function CheckPuzzle() {
	var grid_string = '';
	
	for (i=1; i<=9; i++) {
		for (j=1; j<=9; j++) {
			grid_string += grid[i][j]['value'];
			if (i != 9 || j != 9) {
				grid_string += ',';
			}
		}
	}
	
	if (grid_string == solution) { // puzzle solved!
		completed = 1;
		$('sub_puzzle').innerHTML = "<div style=\"margin-bottom: 4px\"><a href=\"/archives\"><img src=\"/images/play_more_puzzles.gif\" alt=\"Play More Puzzles\" width=\"124\" height=\"18\" /></a></div>\n<span id=\"checkpoint\">You have completed this puzzle</span>";
		SaveAsCheckpoint();
		ShowBillboard('congrats');
		PopulatePuzzle(grid);
		AwardPoints();
	}
}

function AwardPoints() {
	var myAjax = new Ajax.Request ('/action/award_points', { method: 'post', parameters: 'puzzleid='+puzzle_id } );
}

function GetScore(difficulty) {
	if (difficulty == "Easy") return 1;
	else if (difficulty == "Medium") return 2;
	else if (difficulty == "Hard") return 3;
	else if (difficulty == "Expert") return 5;
}

function CheckMove(r,c) {
	var num = grid[r][c]['value'];
	var min_row = max_row = min_col = max_col = 0; // used to check the region
	var error_showing = false;
	
	// check row
	for (i=1; i<=9; i++) {
		if (i != c && grid[r][i]['value'] == num) {
			DisplayRowError(r);
			error_showing = true;
		}
	}

	// check col
	for (i=1; i<=9; i++) {
		if (i != r && grid[i][c]['value'] == num) {
			DisplayColumnError(c);
			error_showing = true;
		}
	}
	
	// check region
	if (r <= 3) { min_row = 1; max_row = 3; }
	else if (r >= 4 && r <= 6) { min_row = 4; max_row = 6; }
	else if (r >= 7) { min_row = 7; max_row = 9; }
	if (c <= 3) { min_col = 1; max_col = 3; }
	else if (c >= 4 && c <= 6) { min_col = 4; max_col = 6; }
	else if (c >= 7) { min_col = 7; max_col = 9; }

	for (i=min_row; i<=max_row; i++) {
		for (j=min_col; j<=max_col; j++) {
			if ((i != r || j != c) && grid[i][j]['value'] == num) {
				DisplayRegionError(min_row,max_row,min_col,max_col);
				error_showing = true;
			}
		}
	}

	if (error_showing) {
		setTimeout('ClearPuzzleErrors()',3000);
	}
}

function DisplayRowError (row) {
	for (i=1; i<=9; i++) {
		$('r'+row+'c'+i).style.background = '#fcc';
	}
}

function DisplayColumnError (col) {
	for (i=1; i<=9; i++) {
		$('r'+i+'c'+col).style.background = '#fcc';
	}
}

function DisplayRegionError (min_row,max_row,min_col,max_col) {
	for (i=min_row; i<=max_row; i++) {
		for (j=min_col; j<=max_col; j++) {
			$('r'+i+'c'+j).style.background = '#fcc';
		}
	}
}

function ClearPuzzleErrors () {
	for (i=1; i<=9; i++) {
		for (j=1; j<=9; j++) {
			if (master_grid[i][j]['value']) {
				$('r'+i+'c'+j).style.background = '#e3e3e3';
			}
			else {
				$('r'+i+'c'+j).style.background = '#fff';
			}
		}
	}
}

function PrintGrid (grid) {
	for (i=1; i<=9; i++) {
		for (j=1; j<=9; j++) {
			$('testing').innerHTML += grid[i][j]['value'];
			if (j==9) $('testing').innerHTML += '<br>';
		}
	}
}

function ClockSniffer (puzzleid) {
	var myAjax = new Ajax.Request ('/action/get_time_left', { method: 'get', parameters: 'puzzleid='+ puzzle_id, onComplete: ClockSnifferPostOp } );
}

function ClockSnifferPostOp (result) {
	var time_left = result.responseText; /* response = time left in minutes */
	
	$('time_left').style.display = "block";
	
	if (Math.floor(time_left) == 0) $('time_left_text').innerHTML = 'Tomorrow\'s puzzle will be available in 1 minute';
	else if (Math.floor(time_left/60) == 0) $('time_left_text').innerHTML = 'Tomorrow\'s puzzle will be available in ' + (Math.floor(time_left) + 1) + ' minutes';
	else if (Math.floor(time_left/60) == 1) $('time_left_text').innerHTML = 'Tomorrow\'s puzzle will be available in 1+ hour';
	else $('time_left_text').innerHTML = 'Tomorrow\'s puzzle will be available in ' + Math.floor(time_left/60) + '+ hours';

	setTimeout('ClockSniffer('+puzzle_id+')',15000);
}

function StartOver () {
	var myAjax = new Ajax.Request ('/action/start_over', { method: 'post', parameters: 'puzzleid='+ puzzle_id, onComplete: StartOverPostOp } );
}

function StartOverPostOp (result) {
	saved_state = '';
	ClearAllSubscripts();
	PopulatePuzzle(master_grid);
	$('checkpoint').innerHTML = "No checkpoint saved";
	unsaved_work = false;
}

function SaveAsCheckpoint() {
	unsaved_work = false;

	saved_state = '';
	
	for (i=1; i<=9; i++) {
		for (j=1; j<=9; j++) {
			if (grid[i][j]['value'] != 0) {
				saved_state += grid[i][j]['value'];
			}
			else {
				saved_state += "{";
				
				for (k=1; k<=9; k++) {
					if (grid[i][j]['mini'][k] != 0) {
						saved_state += grid[i][j]['mini'][k];
					}
					if (k != 9) {
						saved_state += '|';
					}
				}
				
				saved_state += "}";
			}

			if (i != 9 || j != 9) {
				saved_state += ',';
			}
		}
	}
	
	var myAjax = new Ajax.Updater('checkpoint','/action/save_state', { method: 'post', parameters: 'puzzleid='+puzzle_id+'&grid='+saved_state } );
}

function ReturnToCheckpoint () {
	if (!saved_state) {
		alert("You don't have a saved checkpoint to return to.");
	}
	else {
		if (confirm("Are you sure you want to go back to your last checkpoint?\n\nIf you click \"OK\" to continue, you will lose any unsaved progress.")) {
			ClearAllSubscripts();
			StateToGrid();
			PopulatePuzzle(grid);
			unsaved_work = false;
		}
	}
}

function DisplaySaveWarning () {
	if (unsaved_work) {
		return confirm("WARNING: You have unsaved work.\n\nIf you click \"OK\" to continue, you will lose any unsaved progress.");
	}	
}

function StateToGrid () {
	var grid_array = saved_state.split(',');
	
	for (i=1; i<=9; i++) {
		for (j=1; j<=9; j++) {
			key = (i-1)*9+(j-1);
			val = grid_array[key].toString();
			if (val.substr(0,1) == "{") { // there are subscripts in this square
				val = val.substr(1,val.length-2);
				subscripts_array = val.split('|');
				for (k=1; k<=9; k++) {
					grid[i][j]['mini'][k] = subscripts_array[k-1];
				}
				grid[i][j]['value'] = '';
			}
			else {
				grid[i][j]['value'] = grid_array[key];
			}
		}
	}
}

function ClearAllSubscripts () {
	for (i=1; i<=9; i++) {
		for (j=1; j<=9; j++) {
			grid[i][j]['value'] = master_grid[i][j]['value'];
			ClearTinySquares(i,j);	
		}
	}
}

function ShowBillboard (command) {
	var points = GetScore(difficulty);
	var myAjax = new Ajax.Updater('billboard','/action/show_billboard', { method: 'get', parameters: 'command='+command+'&points='+points } );
	$('puzzle').style.display = 'none';
	$('billboard').style.display = 'block';
}

function HideBillboard () {
	$('puzzle').style.display = 'block';
	$('billboard').style.display = 'none';
}
