// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var check = function() {
	var rows = new Array;
	var cols = new Array;
	var groups = new Array;
	var row, col, group;
	
	for(var i=0; i < 9; i++) {
		rows[i] = new Array;
		cols[i] = new Array;
		groups[i] = new Array;
	}
	var row_errors = new Array;
	var col_errors = new Array;
	var group_errors = new Array;
	var done = true;
	for(var i = 0; i < 81; i++) {
		row = Math.floor(i/9);
		col = i%9;
		group = Math.floor(row / 3) * 3 + Math.floor(col / 3);
		var value = parseInt($('c'+col+'r'+row).value);
		if(value) {					                            
			rows[row].push(value);
			cols[col].push(value);
            groups[group].push(value);
		} else {
			done = false;
		}
	}
	for(var i=0; i<9; i++) {
		if(find_collisions(rows[i])) row_errors.push(i);
		if(find_collisions(cols[i])) col_errors.push(i);
		if(find_collisions(groups[i])) group_errors.push(i);
	}
	if(done==true && row_errors.length==0 && col_errors.length==0 && group_errors.length==0) {
		return true;
	}
	if(row_errors.length > 0 || col_errors.length > 0 || group_errors.length > 0) {
		var errors;
		(81).times( function(i) {
			row = Math.floor(i/9);
			col = i%9;
			group = Math.floor(row / 3) * 3 + Math.floor(col / 3);
			errors = 0;
			if(row_errors.indexOf(row) != -1) errors++;
			if(col_errors.indexOf(col) != -1) errors++;
			if(group_errors.indexOf(group) != -1) errors++;
			if(errors > 0) {
				Element.addClassName('c'+col+'r'+row, 'er'+errors);
			}
		});
		Element.update('user_info','You made some mistakes in the puzzle.');
		setTimeout('clear_errors()',5000);
		return false;
	}
	if(done==false) {
		(81).times( function(i) {
			row = Math.floor(i/9);
			col = i%9;
			if($('c'+col+'r'+row).value == "") Element.addClassName($('c'+col+'r'+row), 'emp');
		});
		Element.update('user_info','There are still some empty fields left.');
		setTimeout('clear_emp()',5000);
	}
	return false;
}

var find_collisions = function(array) {
	while(array.length > 0) {
		value = array.pop(0);
		for(var i=0; i<array.length; i++) {
			if(value == array[i]) {
				return true;
			}
		}
	}
	return false;
}

var refresh = function() {
	window.location.href = unescape(window.location.pathname);
}

function autocomplete_off() {
	(9).times(function(row) {
		(9).times(function(col) {
			$('c'+col+'r'+row).setAttribute('autocomplete', 'off');
		})
	})
}

function field_filled(e) {
	number = $R(1,9,false);

  	if(!number.include(parseInt(Event.element(e).value))) {
		Event.element(e).value = "";
	}

	var col = parseInt(Event.element(e).id.charAt(1));
	var row = parseInt(Event.element(e).id.charAt(3));

	switch(e.keyCode) {
		case 37:
		col = (col-1) < 0 ? 8 : (col-1);
		break;
		case 38:
		row = (row-1) < 0 ? 8 : (row-1);
		break;
		case 39:
		col = (col+1)%9;
		break;
		case 40:
		row = (row+1)%9;
		break;
	}
	$('c'+col+'r'+row).focus();
}

function clear_errors() {
	(81).times( function(i) {
		row = Math.floor(i/9);
		col = i%9;
		(3).times( function(er) {
			Element.removeClassName('c'+col+'r'+row, 'er'+(er+1));			
		});

	});
	Element.update('user_info', 'When you solve this Sudoku you can challenge a friend to beat your time.');
}

function clear_emp() {
	(81).times( function(i) {
		row = Math.floor(i/9);
		col = i%9;
		Element.removeClassName('c'+col+'r'+row, 'emp');			
	});
	Element.update('user_info', 'When you solve this Sudoku you can challenge a friend to beat your time.');
}

var update_clock = function() {
	if(this.clock == undefined) {
		this.clock = 0;
	}
	this.clock++;
	var seconds = this.clock%60
	var minutes = Math.floor(this.clock/60)
	seconds = seconds > 9 ? ""+seconds : "0"+seconds;
	minutes = minutes > 9 ? ""+minutes : "0"+minutes;
	Element.update('clock', ""+minutes+":"+seconds);
}
