﻿// JavaScript File

//----------------------------------------------------------------------
// Puzzle stuff
//

	var imagesNormal = new Array();
	
	imagesNormal['0'] = "images/blank.png";
	imagesNormal['1'] = "images/1.png";
	imagesNormal['2'] = "images/2.png";
	imagesNormal['3'] = "images/3.png";
	imagesNormal['4'] = "images/4.png";
	imagesNormal['5'] = "images/5.png";
	imagesNormal['6'] = "images/6.png";
	imagesNormal['7'] = "images/7.png";
	imagesNormal['8'] = "images/8.png";
	imagesNormal['9'] = "images/9.png";
	imagesNormal['a'] = "images/a.png";
	imagesNormal['b'] = "images/b.png";
	imagesNormal['c'] = "images/c.png";
	imagesNormal['d'] = "images/d.png";
	imagesNormal['e'] = "images/e.png";
	imagesNormal['f'] = "images/f.png";
	imagesNormal['g'] = "images/g.png";
	imagesNormal['h'] = "images/h.png";
	imagesNormal['i'] = "images/i.png";

	function initCells()
	{	
		for (var i=0; i<81; i++)
		{
			var img;
			var target = "imgCell" + i;

			img = document.getElementById(target);
			img.src = imagesNormal[_puzzle[i]];
					
			var td = document.getElementById("td" + i);
			if (_puzzle[i] <= '9')
			{
				td.style.cursor = "pointer";
			}
			else
			{
				td.style.cursor = "default";
			}
		}
	}

	var yourImages = 
		new Array(	"images/0.png",
					"images/1.png",
					"images/2.png",
					"images/3.png",
					"images/4.png",
					"images/5.png",
					"images/6.png",
					"images/7.png",
					"images/8.png",
					"images/9.png",
					"images/a.png",
					"images/b.png",
					"images/c.png",
					"images/d.png",
					"images/e.png",
					"images/f.png",
					"images/g.png",
					"images/h.png",
					"images/i.png",
					"images/blank.png");

	if (document.images) 
	{
		var preImages = new Array();
		var currCount = 0;
		var loaded = new Array();
		var timerID;
	}

	function loadImages() 
	{ 
		for (i = 0; i < yourImages.length; i++) 
		{ 
			preImages[i] = new Image();
			preImages[i].src = yourImages[i];
		}
		
		for (i = 0; i < preImages.length; i++) 
		{ 
			loaded[i] = false;
		}
		
		checkLoad();
	}

	function checkLoad() 
	{
		if (currCount == preImages.length) 
		{ 
			initCells();
			return;
		}
		for (i = 0; i <= preImages.length; i++) 
		{
			if (loaded[i] == false && preImages[i].complete) 
			{
				loaded[i] = true;
				currCount++;
			}
		}

		timerID = setTimeout("checkLoad()", 50);
	}

	var currentCell = -1;

	function getEleX(node)
	{
		var px;
		
		px = node.offsetLeft;
		
		if (node.offsetParent)
			px += getEleX(node.offsetParent);
			
		return px;
	}
	
	function getEleY(node)
	{
		var py;
		
		py = node.offsetTop;
		
		if (node.offsetParent)
			py += getEleY(node.offsetParent);
			
		return py;
	}
	
	function onCellClick(evt)
	{
		var x; 
		var y;
		var td;
		var target = "td" + currentCell;
		var val;
		
	
		evt = (evt) ? evt : ((window.event) ? event : null);
		if (evt)
		{
			try
			{
				evt.cancelBubble = true;

				if (currentCell < 0)
				{
					return;
				}

				td = document.getElementById(target);
				if (evt.layerX)
				{
					x = (evt.layerX - getEleX(td)) - 2;
					y = (evt.layerY - getEleY(td)) - 2;
				}
				else
				{
					x = evt.offsetX;
					y = evt.offsetY;
				}
			}
			catch (ex)
			{
				alert(ex.message);
			}
			
			if ((x >= 0) && (x <= 10) && (y >= 0) && (y <= 10))
			{
				val = '1';
			}
			else if ((x >= 11) && (x <= 21) && (y >= 0) && (y <= 10))
			{
				val = '2';
			}
			else if ((x >= 22) && (x <= 32) && (y >= 0) && (y <= 10))
			{
				val = '3';
			}
			else if ((x >= 0) && (x <= 10) && (y >= 11) && (y <= 21))
			{
				val = '4';
			}
			else if ((x >= 11) && (x <= 21) && (y >= 11) && (y <= 21))
			{
				val = '5';
			}
			else if ((x >= 22) && (x <= 32) && (y >= 11) && (y <= 21))
			{
				val = '6';
			}
			else if ((x >= 0) && (x <= 10) && (y >= 22) && (y <= 32))
			{
				val = '7';
			}
			else if ((x >= 11) && (x <= 21) && (y >= 22) && (y <= 32))
			{
				val = '8';
			}
			else if ((x >= 22) && (x <= 32) && (y >= 22) && (y <= 32))
			{
				val = '9';
			}
			
			_puzzle[currentCell] = val;
			updateCellValue(currentCell, val)			

			var img = document.getElementById("imgCell" + currentCell);
			if (img)
			{
				img.src = imagesNormal[val];
			}
		}
	}
	
	function onCellOver(num)
	{
		if (_puzzle[num] <= '9')
		{
			var img;
			var target = "imgCell" + num;

			img = document.getElementById(target);
			img.src = "images/0.png";
			
			currentCell = num;
		}
		
		return true;
	}
	
	function onCellOut(num)
	{
		if (_puzzle[num] <= '9')
		{
			var img;
			var target = "imgCell" + num;

			img = document.getElementById(target);
			img.src = imagesNormal[_puzzle[num]];
			
			currentCell = -1;
		}

		return true;
	}
	
	if (document.addEventListener) 
	{
		document.addEventListener("DOMContentLoaded", doInitCells, null);
	}
	else
	{
		document.onreadystatechange=doInitCells;
	}
		
	function doInitCells()
	{
		setTimeout("loadImages()", 10);
	}


	var allRules = new Array(27);
	
	// Regions
	allRules[0] = new Array(0, 1, 2, 9,10,11,18,19,20);
	allRules[1] = new Array(3, 4, 5,12,13,14,21,22,23);
	allRules[2] = new Array(6, 7, 8,15,16,17,24,25,26);
	
	allRules[3] = new Array(27,28,29,36,37,38,45,46,47);
	allRules[4] = new Array(30,31,32,39,40,41,48,49,50);
	allRules[5] = new Array(33,34,35,42,43,44,51,52,53);
	
	allRules[6] = new Array(54,55,56,63,64,65,72,73,74);
	allRules[7] = new Array(57,58,59,66,67,68,75,76,77);
	allRules[8] = new Array(60,61,62,69,70,71,78,79,80);

	// Rows	
	allRules[9] = new Array(0,1,2,3,4,5,6,7,8);
	allRules[10] = new Array( 9,10,11,12,13,14,15,16,17);
	allRules[11] = new Array(18,19,20,21,22,23,24,25,26);
	allRules[12] = new Array(27,28,29,30,31,32,33,34,35);
	allRules[13] = new Array(36,37,38,39,40,41,42,43,44);
	allRules[14] = new Array(45,46,47,48,49,50,51,52,53);
	allRules[15] = new Array(54,55,56,57,58,59,60,61,62);
	allRules[16] = new Array(63,64,65,66,67,68,69,70,71);
	allRules[17] = new Array(72,73,74,75,76,77,78,79,80);

	// Columns
	allRules[18] = new Array(0, 9,18,27,36,45,54,63,72);
	allRules[19] = new Array(1,10,19,28,37,46,55,64,73);
	allRules[20] = new Array(2,11,20,29,38,47,56,65,74);
	allRules[21] = new Array(3,12,21,30,39,48,57,66,75);
	allRules[22] = new Array(4,13,22,31,40,49,58,67,76);
	allRules[23] = new Array(5,14,23,32,41,50,59,68,77);
	allRules[24] = new Array(6,15,24,33,42,51,60,69,78);
	allRules[25] = new Array(7,16,25,34,43,52,61,70,79);
	allRules[26] = new Array(8,17,26,35,44,53,62,71,80);



	//--------------------------------------------------
	// Test for a completed puzzle.
	function checkComplete()
	{
		for (var i=0; i<81; i++)
		{
			if (_puzzle[i] == '0')
			{
				return false;
			}
		}
	
		for (var a=0; a<27; a++)
		{
			if (!testRule(allRules[a]))
			{
				return false;
			}
		}
				
		return true;
	}
	
	//-------------------------------------------------------
	// Test a rule.
	function testRule(indexes)
	{
		var val;

		for (var a=0; a<9; a++)
		{
			val = getPuzzleValue([indexes[a]]);
			
			for (var b=(a+1); b<9; b++)
			{
				if (val == getPuzzleValue([indexes[b]]))
				{
					return false;
				}
			}
		}
		
		return true;
	}

	function getPuzzleValue(index)
	{
		var val = 0;

		if (_puzzle[index] >= 'a')
		{
			switch (_puzzle[index])
			{
				case 'a':
					val = 1;
					break;
				case 'b':
					val = 2;
					break;
				case 'c':
					val = 3;
					break;
				case 'd':
					val = 4;
					break;
				case 'e':
					val = 5;
					break;
				case 'f':
					val = 6;
					break;
				case 'g':
					val = 7;
					break;
				case 'h':
					val = 8;
					break;
				case 'i':
					val = 9;
					break;
			}
		}
		else
		{
			val = Number(_puzzle[index]);
		}
		
		return val;
	}
	
	function getPuzzleString()
	{
		var tmp = "";
		
		for (var i=0; i<81; i++)
			tmp = tmp.concat(_puzzle[i]);
			
		return tmp;
	}


	//cellReadonly
	function resetCells()
	{
		var tbl;
		var node;
		
		tbl = document.getElementById("tblGrid");
		
		for (var i=0; i<tbl.rows.length; i++)
		{
			for (var c=0; c< tbl.rows[i].cells.length; c++)
			{
				node = tbl.rows[i].cells[c];
				if ((node.tagName == 'TD') && (node.id.substr(0, 4) == 'cell'))
				{
					node.className = "cellComplete";
				}
			}
		}
		
		for (var a=0; a<dd.elements.length; a++)
		{
			dd.elements[a].setDraggable(false);
		}
	}

	function updateCellValue(index, val)
	{
		var elem = document.getElementById("hdnPuzzle");
		_puzzle[index] = val.toString();

		elem.value = getPuzzleString();

		if (checkComplete())
		{
			//resetCells();
			alert("Puzzle Complete!");
		}
	}
	

	function displayPrintable()
	{
		var url = "printsudoku.aspx?sudoku=" + getPuzzleString();
		window.open(url, null, "resizable=yes,status=yes,toolbar=no,menubar=yes,location=no");
	}


	var httpSolution;
		
	function getSodukoSolution()
	{
		var url = "solution.aspx?sudoku=" + getPuzzleString();
	}
	
	function displaySolution()
	{
		var n;
	}
	

function loadXMLDoc(url)
{
	if (window.XMLHttpRequest)
	{
		// code for Mozilla, etc.
		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=xmlhttpChange;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else if (window.ActiveXObject)
	{
		// code for IE
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlhttp)
		{
			xmlhttp.onreadystatechange=xmlhttpChange;
			xmlhttp.open("GET",url,true);
			xmlhttp.send();
		}
	}
}

function xmlhttpChange()
{
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4)
	{
		// if "OK"
		if (xmlhttp.status==200)
		{
			// ...some code here...
			var i = 0;
		}
		else
		{
			alert("Problem retrieving XML data")
		}
	}
}
	
