var starter = 0;
var turn = 1;

function check (square) {
	return document.getElementById(square).style.backgroundImage;
}

function clicky(square) {
	if (check(square) == '') {
		document.getElementById(square).style.backgroundImage = 'url(tictactoe/x.gif)';
		checkwinner('x',square);
		if (square != 5) { document.getElementById('5').style.backgroundImage = 'url(tictactoe/o.gif)'; }
		
	} else {
		alert ('Invalid move.');
	}
}

function checkwinner(player,location) {
	switch (location) {
		case '1':
			if (check(1) == 'url(tictactoe/'+player+'.gif)' && check(2) == 'url(tictactoe/'+player+'.gif)' && check(3) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(1) == 'url(tictactoe/'+player+'.gif)' && check(4) == 'url(tictactoe/'+player+'.gif)' && check(7) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(1) == 'url(tictactoe/'+player+'.gif)' && check(5) == 'url(tictactoe/'+player+'.gif)' && check(9) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			break;
		case '2':
			if (check(1) == 'url(tictactoe/'+player+'.gif)' && check(2) == 'url(tictactoe/'+player+'.gif)' && check(3) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(2) == 'url(tictactoe/'+player+'.gif)' && check(5) == 'url(tictactoe/'+player+'.gif)' && check(8) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			break;
		case '3':
			if (check(1) == 'url(tictactoe/'+player+'.gif)' && check(2) == 'url(tictactoe/'+player+'.gif)' && check(3) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(3) == 'url(tictactoe/'+player+'.gif)' && check(6) == 'url(tictactoe/'+player+'.gif)' && check(9) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(3) == 'url(tictactoe/'+player+'.gif)' && check(5) == 'url(tictactoe/'+player+'.gif)' && check(7) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			break;
		case '4':
			if (check(4) == 'url(tictactoe/'+player+'.gif)' && check(5) == 'url(tictactoe/'+player+'.gif)' && check(6) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(1) == 'url(tictactoe/'+player+'.gif)' && check(4) == 'url(tictactoe/'+player+'.gif)' && check(7) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			break;
		case '5':
			if (check(1) == 'url(tictactoe/'+player+'.gif)' && check(5) == 'url(tictactoe/'+player+'.gif)' && check(9) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(3) == 'url(tictactoe/'+player+'.gif)' && check(5) == 'url(tictactoe/'+player+'.gif)' && check(7) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(2) == 'url(tictactoe/'+player+'.gif)' && check(5) == 'url(tictactoe/'+player+'.gif)' && check(8) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(4) == 'url(tictactoe/'+player+'.gif)' && check(5) == 'url(tictactoe/'+player+'.gif)' && check(6) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			break;
		case '6':
			if (check(3) == 'url(tictactoe/'+player+'.gif)' && check(6) == 'url(tictactoe/'+player+'.gif)' && check(9) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(4) == 'url(tictactoe/'+player+'.gif)' && check(5) == 'url(tictactoe/'+player+'.gif)' && check(6) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			break;
		case '7':
			if (check(7) == 'url(tictactoe/'+player+'.gif)' && check(8) == 'url(tictactoe/'+player+'.gif)' && check(9) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(1) == 'url(tictactoe/'+player+'.gif)' && check(4) == 'url(tictactoe/'+player+'.gif)' && check(7) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(3) == 'url(tictactoe/'+player+'.gif)' && check(5) == 'url(tictactoe/'+player+'.gif)' && check(7) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			break;
		case '8':
			if (check(7) == 'url(tictactoe/'+player+'.gif)' && check(8) == 'url(tictactoe/'+player+'.gif)' && check(9) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(2) == 'url(tictactoe/'+player+'.gif)' && check(5) == 'url(tictactoe/'+player+'.gif)' && check(8) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			break;
		case '9':
			if (check(7) == 'url(tictactoe/'+player+'.gif)' && check(8) == 'url(tictactoe/'+player+'.gif)' && check(9) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(3) == 'url(tictactoe/'+player+'.gif)' && check(6) == 'url(tictactoe/'+player+'.gif)' && check(9) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			if (check(1) == 'url(tictactoe/'+player+'.gif)' && check(5) == 'url(tictactoe/'+player+'.gif)' && check(9) == 'url(tictactoe/'+player+'.gif)') { alert (player+' wins!'); }
			break;
	}
}
