chess


Web Chess Pro – Cannot run scripts

// <![CDATA[
var
blackSqrColor = '#111111',
whiteSqrColor = '#FDFDFD',
preSelColor = 'blue',
fromSelColor = '#FF3300',
toSelColor = '#00AA00',
exSelColor = 'yellow',
gameVer = '1.0',
fileVer = '1.0',

sqr_i = new Array(64),
sqr_j = new Array(64),
sqrLbls = new Array(64),
sqrCols = new Array(64),

pcNames = new Array(64),
pcCols = new Array(64),
dlgX = null,
dlgY = null,
timerId = null,

selNum = -1,
legEpNum = -1,
moveNum = 0,
turn = true,
playing = false,
doingPromotion = false,
players = new Array('Player 1','Player 2'),
castState = new Array(3,3),
moveInfo = new Array(-1,-1,-1,-1),
playersTime = new Array(0,0),
curFile = '',
lastTime = 60,

htmlNewGame = '

Players

Player 1:
Player 2:

Options

Player 1 is white
Player 2 is white
Choose colors by chance


Timed Game
Game Time:  min.

  

‘,
htmlLoadGame = ‘

 Load:

  

‘,
htmlSaveGame = ‘

 Save:

  

‘,
htmlAbout = ‘

Web Chess Pro

Version: 1.0.0
Copyrigth © 2007 Soroush Javadi
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.\nThis program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\nYou should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.URL to download Web Chess Pro 1.0:
 [ Copy ]

‘,
strSecWrn1 = ‘Your browser may be preventing Web Chess Pro from running an ActiveX control.\nIn order to ‘,
strSecWrn2 = ‘ the game, you must allow this interaction. Please check your Internet security settings.\n\nWeb Chess Pro will use this ActiveX control ONLY to ‘;

function canMove(from, to, updateMoveInfo, captureOnly) {

var i, j, t1, t2, ret;

if (!captureOnly && ((pcCols[from] == pcCols[to]) || (pcNames[from] == ”) || (pcNames[to] == ‘K’))) {
return(false);
}

if (pcNames[from] == ‘P’) {

if (pcCols[from]) {
t1 = from; t2 = to; i = 6;
}
else {
t1 = to; t2 = from; i = 1;
}

if ((sqr_j[from] == sqr_j[to]) && (pcNames[to] == ”) && !captureOnly) {
if (sqr_i[t1] – sqr_i[t2] == 1) {
return(true);
}
else if ((sqr_i[from] == i) && (sqr_i[t1] – sqr_i[t2] == 2) && (pcNames[getSqrNum(sqr_i[t1] – 1, sqr_j[from])] == ”)) {
return(true);
}
}
else if ((sqr_i[t1] – sqr_i[t2] == 1) && (Math.abs(sqr_j[from] – sqr_j[to]) == 1) && (pcNames[to] != ”)) {
return(true);
}

if (pcCols[from]) {
i = 3; j = 2;
}
else {
i = 4; j = 5;
}

if ((sqr_i[from] == i) && (sqr_i[to] == j) && (Math.abs(sqr_j[from] – sqr_j[to]) == 1) && (pcNames[to] == ”)) {

t1 = getSqrNum(sqr_i[from], sqr_j[to]);

if ((pcNames[t1] == ‘P’) && (pcCols[t1] != pcCols[from]) && (t1 == legEpNum)) {
if (updateMoveInfo) {
moveInfo[3] = t1;
}
return(true);
}
}

return(false);
}

if (pcNames[from] == ‘N’) {

t1 = new Array(new Array(2,2,-2,-2),new Array(1,-1,1,-1));

for (i = 0; i < 2; i++) {
for (j = 0; j < 4; j++) {
if ((sqr_i[to] == sqr_i[from] + t1[0][j]) && (sqr_j[to] == sqr_j[from] + t1[1][j])) {
return(true);
}
}

t1.reverse();
}

return(false);
}

if ((pcNames[from] == 'R') || (pcNames[from] == 'Q')) {

ret = false;

if (sqr_i[from] == sqr_i[to]) {
ret = true;
if (sqr_j[from] < sqr_j[to]) {
for (j = sqr_j[from] + 1; j sqr_j[to]; j–) {
if (pcNames[getSqrNum(sqr_i[from], j)] != ”) {
return(false);
}
}
}
}
else if (sqr_j[from] == sqr_j[to]) {
ret = true;
if (sqr_i[from] < sqr_i[to]) {
for (i = sqr_i[from] + 1; i sqr_i[to]; i–) {
if (pcNames[getSqrNum(i, sqr_j[from])] != ”) {
return(false);
}
}
}
}

if (ret || (pcNames[from] == ‘R’)) {
return(ret);
}
}

if ((pcNames[from] == ‘B’) || (pcNames[from] == ‘Q’)) {

ret = false;

if (sqr_i[from] – sqr_i[to] == sqr_j[from] – sqr_j[to]) {
ret = true;
if (sqr_i[from] – sqr_i[to] < 0) {
for (i = sqr_i[from] + 1, j = sqr_j[from] + 1; i sqr_i[to]; i–, j–) {
if (pcNames[getSqrNum(i, j)] != ”) {
return(false);
}
}
}
}
else if (sqr_i[from] – sqr_i[to] == sqr_j[to] – sqr_j[from]) {
ret = true;
if (sqr_i[from] – sqr_i[to] < 0) {
for (i = sqr_i[from] + 1, j = sqr_j[from] – 1; i sqr_i[to]; i–, j++) {
if (pcNames[getSqrNum(i, j)] != ”) {
return(false);
}
}
}
}

return(ret);
}

if (pcNames[from] == ‘K’) {

i = getKingLocation(!pcCols[from]);

if ((((Math.abs(sqr_i[from] – sqr_i[to]) == 1) && (sqr_j[from] == sqr_j[to])) || ((Math.abs(sqr_j[from] – sqr_j[to]) == 1) && (sqr_i[from] == sqr_i[to])) || ((Math.abs(sqr_i[from] – sqr_i[to]) == 1) && (Math.abs(sqr_j[from] – sqr_j[to]) == 1))) &&
(((Math.abs(sqr_i[to] – sqr_i[i]) != 1) || (sqr_j[to] != sqr_j[i])) && ((Math.abs(sqr_j[to] – sqr_j[i]) != 1) || (sqr_i[to] != sqr_i[i])) && ((Math.abs(sqr_i[to] – sqr_i[i]) != 1) || (Math.abs(sqr_j[to] – sqr_j[i]) != 1)))) {

return(true);
}

if (captureOnly) {
return(false);
}

i = (pcCols[from] ? 0 : 1);

if (castState[i] != 0) {
if ((sqr_j[to] – sqr_j[from] == 2) && (sqr_i[from] == sqr_i[to]) && ((castState[i] == 1) || (castState[i] == 3)) && (pcNames[to – 1] == ”) &&
isNotUnderAttack(from, pcCols[from]) && isNotUnderAttack(to, pcCols[from]) && isNotUnderAttack(to – 1, pcCols[from])) {

if (updateMoveInfo) {
moveInfo[0] = to + 1;
moveInfo[1] = to – 1;
moveInfo[2] = true;
}
return(true);
}
else if ((sqr_j[to] – sqr_j[from] == -2) && (sqr_i[from] == sqr_i[to]) && ((castState[i] == 2) || (castState[i] == 3)) && (pcNames[to – 1] == ”) && (pcNames[to + 1] == ”) &&
isNotUnderAttack(from, pcCols[from]) && isNotUnderAttack(to, pcCols[from]) && isNotUnderAttack(to + 1, pcCols[from])) {

if (updateMoveInfo) {
moveInfo[0] = to – 2;
moveInfo[1] = to + 1;
moveInfo[2] = false;
}
return(true);
}
}

return(false);
}
}

function isInCheck(color) {

var i, k;

k = getKingLocation(color);

for (i = 0; i < 64; i++) {
if ((pcNames[i] != '') && (pcCols[i] != color) && (pcNames[i] != 'K') && canMove(i, k, false, true)) {
return(true);
}
}

return(false);
}

function willBeInCheck(color, from, to) {

var sv = new Array(), ep = -1, ret;

if ((pcNames[from] == 'P') &&
(sqr_i[from] == (pcCols[from] ? 3 : 4)) && (sqr_i[to] == (pcCols[from] ? 2 : 5)) && (Math.abs(sqr_j[from] – sqr_j[to]) == 1) && (pcNames[to] == '')) {

ep = getSqrNum(sqr_i[from], sqr_j[to]);

if ((pcNames[ep] != 'P') || (pcCols[ep] == pcCols[from]) || (ep != legEpNum)) {
ep = -1;
}
}

sv[0] = pcNames[from];
sv[1] = pcCols[from];
sv[2] = pcNames[to];
sv[3] = pcCols[to];
if (ep != -1) {
sv[4] = pcNames[ep];
sv[5] = pcCols[ep];
}

pcNames[to] = pcNames[from];
pcCols[to] = pcCols[from];
pcNames[from] = '';
pcCols[from] = null;
if (ep != -1) {
pcNames[ep] = '';
pcCols[ep] = null;
}

ret = isInCheck(color);

pcNames[from] = sv[0];
pcCols[from] = sv[1];
pcNames[to] = sv[2];
pcCols[to] = sv[3];
if (ep != -1) {
pcNames[ep] = sv[4];
pcCols[ep] = sv[5];
}

return(ret);
}

function isNotUnderAttack(num, color) {

var i;

for (i = 0; i < 64; i++) {
if ((pcNames[i] != '') && (pcCols[i] != color) && canMove(i, num, false, true)) {
return(false);
}
}

return(true);
}

function hasLegalMove(color) {

var i, j;

for (i = 0; i < 64; i++) {
if (pcCols[i] == color) {
for (j = 0; j < 64; j++) {
if (canMove(i, j, false, false) && !willBeInCheck(color, i, j)) {
return(true);
}
}
}
}

return(false);
}

function canCheckmate(color) {

var i, pc, b = 0, n = 0;

for (i = 0; i < 64; i++) {

pc = pcNames[i];

if ((pc == '') || (pcCols[i] != color) || (pc == 'K')) {
continue;
}
if ((pc == 'P') || (pc == 'R') || (pc == 'Q')) {
return(true);
}

if (pc == 'B') {
b++;
}
else {
n++;
}
}

return((b == 2) || ((b == 1) && (n == 1)));
}

function isDeadPos() {

var i, pc, b = new Array(), n = 0;

for (i = 0; i < 64; i++) {

pc = pcNames[i];

if ((pc == '') || (pc == 'K')) {
continue;
}
if ((pc == 'P') || (pc == 'R') || (pc == 'Q')) {
return(false);
}

if (pc == 'B') {
b[b.length] = i;
}
else {
n++;
}
}

return(
((b.length == 0) && (n == 0)) ||
((b.length == 1) && (n == 0)) ||
((b.length == 0) && (n == 1)) ||
((b.length == 2) && (pcCols[b[0]] != pcCols[b[1]]) && (sqrCols[b[0]] == sqrCols[b[1]])));
}

function createChessboard() {

var i, j, n = 0, col = whiteSqrColor, tbl, ltrs;

tbl = '

‘;
ltrs = ‘

‘;

for (i = 0; i < 8; i++) {

tbl += '

‘ + (8 – i) + ‘

‘;
ltrs += ‘

‘ + String.fromCharCode(97 + i) + ‘

‘;

for (j = 0; j < 8; j++, n++) {

if (j != 0) {
col = (col == whiteSqrColor ? blackSqrColor : whiteSqrColor);
}

tbl += '

‘;

sqr_i[n] = i;
sqr_j[n] = j;
sqrLbls[n] = String.fromCharCode(97 + j) + (8 – i);
sqrCols[n] = (col == whiteSqrColor);

}

tbl += ‘

‘ + (8 – i) + ‘

‘;
if (i != 7) {
tbl += ‘ ‘;
}

}

tbl += ‘

‘;
ltrs += ‘

‘;

tbl = ‘

‘ + ltrs + tbl + ltrs + ‘

‘;

divChessboard.innerHTML = tbl;
}

function activateChessboard() {

var i, sqr;

for (i = 0; i < 64; i++) {
sqr = document.getElementById('sqr' + i);
sqr.onmouseover = new Function('sqrMouseOver(' + i + ');');
sqr.onmouseout = new Function('sqrMouseOut(' + i + ');');
sqr.onclick = new Function('sqrClick(' + i + ');');
}

btnSave.disabled = btnSaveAs.disabled = false;
spnSave.style.cursor = spnSaveAs.style.cursor = 'hand';
}

function inactivateChessboard() {

var i, sqr;

for (i = 0; i < 64; i++) {
sqr = document.getElementById('sqr' + i);
sqr.onmouseover = sqr.onmouseout = sqr.onclick = function(){return;};
}
}

function newGame(bShowDlg) {

var i, pcs, p1, p2, p1w, tm;

if (bShowDlg) {
if (!showDlg('NEW GAME', htmlNewGame, 'txtPlayer1', 'newGame(false);')) {return;}
frmDlg.txtPlayer1.value = players[0];
frmDlg.txtPlayer2.value = players[1];
frmDlg.chkTimedGame.checked = (lastTime != -1);
frmDlg.txtTime.value = (lastTime == -1 ? '' : lastTime);
chkTimedGameClick();
return;
}

p1 = trim(frmDlg.txtPlayer1.value);
p2 = trim(frmDlg.txtPlayer2.value);

if (p1 == '') {alert('Please enter a name as Player 1.'); frmDlg.txtPlayer1.focus(); return;}
if (p2 == '') {alert('Please enter a name as Player 2.'); frmDlg.txtPlayer2.focus(); return;}
if (p1.toLowerCase() == p2.toLowerCase()) {alert('Please enter two different names as player names.'); frmDlg.txtPlayer1.focus(); return;}

if (frmDlg.chkTimedGame.checked) {
tm = parseInt(frmDlg.txtTime.value, 10);
if (isNaN(tm) || (tm < 1) || (frmDlg.txtTime.value != tm.toString())) {alert('Please enter a positive integer as Game Time.'); frmDlg.txtTime.focus(); return;}
playersTime[0] = playersTime[1] = tm * 60;
}
else {
playersTime[0] = playersTime[1] = tm = -1;
}

for (i = 0; i < 3; i++) {
if (frmDlg['radGameOpt' + i].checked) {
break;
}
}

switch (i) {
case 0: p1w = true; break;
case 1: p1w = false; break;
case 2: p1w = (Math.random() <= 0.5); break;
}

players[p1w ? 0 : 1] = p1;
players[p1w ? 1 : 0] = p2;

pcs = new Array('R','N','B','Q','K','B','N','R');

for (i = 0; i < 8; i++) {setPiece(i, 'B' + pcs[i]);}
for (i = 8; i < 16; i++) {setPiece(i, 'BP');}
for (i = 16; i < 48; i++) {setPiece(i, '');}
for (i = 48; i < 56; i++) {setPiece(i, 'WP');}
for (i = 56; i < 64; i++) {setPiece(i, 'W' + pcs[i – 56]);}

spnMoves.innerText = '';

selNum = -1;
legEpNum = -1;
moveNum = 0;
turn = false;
playing = true;
doingPromotion = false;
castState[0] = castState[1] = 3;
moveInfo[0] = moveInfo[1] = moveInfo[2] = moveInfo[3] = -1;
curFile = '';
lastTime = tm;

activateChessboard();
document.title = 'Web Chess Pro [' + players[0] + ' vs ' + players[1] + ']';
hideDlg();
changeTurn();

if (tm == -1) {
stopClock(true);
}
else {
startClock();
}

showStatus(false);
}

function loadGame(path) {

var i, txt, fso, file, e, ary, tpc, tl;

if (typeof(path) == 'undefined') {
showDlg('LOAD GAME', htmlLoadGame, 'inpFileLoad', 'loadSubmit();');
return;
}

hideDlg();

try {
fso = new ActiveXObject('Scripting.FileSystemObject');
}
catch(e) {
if (e.number == -2146827859) {
alert(strSecWrn1 + 'load' + strSecWrn2 + 'read the "' + path + '" file.');
}
else {
alert('An error was encountered trying to create an ActiveX object.\nFailed to load the game.');
}
return;
}

try {
file = fso.OpenTextFile(path, 1, false, 0);
txt = file.ReadAll();
file.Close();
}
catch(e) {
alert('Cannot open the "' + path + '" file: ' + e.description + '.\nFailed to load the game.');
return;
}

try {
ary = txt.split('\t');

if (ary.length != 13) {throw 0;}
if (ary[0] != fileVer) {
alert('"' + path + '" is not a valid file for this version of Web Chess Pro.');
return;
}

selNum = parseInt(ary[1], 10); if (isNaN(selNum) || (selNum 63)) {throw 1;}
legEpNum = parseInt(ary[2], 10); if (isNaN(legEpNum) || (legEpNum 63)) {throw 2;}
moveNum = parseFloat(ary[3]); if (isNaN(moveNum) || (moveNum < 0)) {throw 3;}
castState[0] = parseInt(ary[4], 10); if (isNaN(castState[0]) || (castState[0] 3)) {throw 4;}
castState[1] = parseInt(ary[5], 10); if (isNaN(castState[1]) || (castState[1] 3)) {throw 5;}
ary[6] = parseInt(ary[6], 10); if (isNaN(ary[6]) || ((ary[6] != 0) && (ary[6] != 1))) {throw 6;}
turn = (ary[6] == 1);
playersTime[0] = parseInt(ary[7], 10); if (isNaN(playersTime[0]) || (playersTime[0] < -1)) {throw 7;}
playersTime[1] = parseInt(ary[8], 10); if (isNaN(playersTime[1]) || (playersTime[1] 20) {
players[0] = players[0].substr(0, 20);
}
if (players[1].length > 20) {
players[1] = players[1].substr(0, 20);
}

spnMoves.innerText = ary[12];

txt = ary[11];
ary = txt.split(‘;’);

if (ary.length != 64) {throw 10;}

for (i = 0; i < 64; i++) {

tpc = ary[i].toUpperCase();
tl = tpc.length;

if ((tl != 0) && (tl != 2)) {throw 11;}

if (tl == 2) {

tl = tpc.charAt(0);
if ((tl != 'W') && (tl != 'B')) {throw 12;}

tl = tpc.charAt(1);
if ((tl != 'P') && (tl != 'R') && (tl != 'N') && (tl != 'B') && (tl != 'Q') && (tl != 'K')) {throw 13;}
}

setPiece(i, tpc);
}

playing = true;
doingPromotion = false;
moveInfo[0] = moveInfo[1] = moveInfo[2] = moveInfo[3] = -1;
curFile = path;

if (selNum != -1) {
showBorder(selNum, fromSelColor);
}

document.title = 'Web Chess Pro [' + players[0] + ' vs ' + players[1] + ']';
spnMovesEnd.scrollIntoView();

if (playersTime[0] == -1) {
stopClock(true);
}
else {
tblClock.rows(0).cells(1).innerText = formatSecs(playersTime[0]);
tblClock.rows(1).cells(1).innerText = formatSecs(playersTime[1]);

i = -1;
if (playersTime[0] == 0) {i = 0;}
if (playersTime[1] == 0) {i = 1;}
if (i != -1) {
timeUp(i == 0, false);
return;
}
}

activateChessboard();
changeTurn();
showStatus(false);

if (playing && (playersTime[0] != -1)) {
startClock();
}
}
catch(e) {
clearGame();
alert('"' + path + '" is either not a valid file or damaged.');
}
}

function saveGame(path, promptOverwrite) {

var i, txt, fso, file, e, pcs = '';

i = (typeof(path) == 'undefined');

if ((i && (curFile == '')) || (path == '\n')) {
if (!showDlg('SAVE GAME', htmlSaveGame, 'inpFileSave', 'saveSubmit();')) {return;}
frmDlg.inpFileSave.value = players[0] + ' vs ' + players[1] + '.dat';
return;
}

if (i) {
saveGame(curFile, false);
return;
}

hideDlg();

try {
fso = new ActiveXObject('Scripting.FileSystemObject');
}
catch(e) {
if (e.number == -2146827859) {
alert(strSecWrn1 + 'save' + strSecWrn2 + 'create the "' + path + '" file.');
}
else {
alert('An error was encountered trying to create an ActiveX object.\nFailed to save the game.');
}
return;
}

if (promptOverwrite && fso.FileExists(path) && !confirm('"' + path + '" already exists.\nDo you want to replace it?')) {
return;
}

for (i = 0; i < 64; i++) {
pcs += (pcNames[i] == '' ? ';' : (pcCols[i] ? 'W' : 'B') + pcNames[i] + ';');
}
pcs = pcs.slice(0, -1);

txt =
fileVer + '\t' +
selNum + '\t' +
legEpNum + '\t' +
moveNum + '\t' +
castState[0] + '\t' +
castState[1] + '\t' +
(turn ? '0' : '1') + '\t' +
playersTime[0] + '\t' +
playersTime[1] + '\t' +
players[0] + '\t' +
players[1] + '\t' +
pcs + '\t' +
spnMoves.innerText;

try {
file = fso.CreateTextFile(path, true, false);
file.Write(txt);
file.Close();
}
catch(e) {
alert('Cannot create the "' + path + '" file: ' + e.description + '.\nFailed to save the game..');
return;
}

curFile = path;
}

function clearGame() {

var i;

hideDlg();

for (i = 0; i < 64; i++) {
setPiece(i, '');
}

if (selNum != -1) {
hideBorder(selNum);
}

spnMoves.innerText = '';
stopClock(true);
inactivateChessboard();
btnSave.disabled = btnSaveAs.disabled = true;
spnSave.style.cursor = spnSaveAs.style.cursor = 'default';

selNum = -1;
legEpNum = -1;
moveNum = 0;
turn = true;
playing = false;
doingPromotion = false;
castState[0] = castState[1] = 3;
moveInfo[0] = moveInfo[1] = moveInfo[2] = moveInfo[3] = -1;
curFile = '';

document.title = 'Web Chess Pro';
spnStatus.innerText = 'Ready for new game.';
}

function movePiece(from, to) {

var pcFrom, pcTo, i, s, l;

pcFrom = document.getElementById('pc' + from);
pcTo = document.getElementById('pc' + to);
pcTo.src = pcFrom.src;
pcTo.style.visibility = 'inherit';
pcFrom.style.visibility = 'hidden';

if (pcCols[from]) {
i = 0; s = 63; l = 56;
}
else {
i = 1; s = 7; l = 0;
}

if (castState[i] != 0) {
if (pcNames[from] == 'K') {
castState[i] = 0;
}
else if (pcNames[from] == 'R') {
if ((from == s) && (castState[i] != 2)) {
castState[i]–;
}
else if ((from == l) && (castState[i] != 1)) {
castState[i] -= 2;
}
}
}

i = legEpNum = -1;
if (pcNames[from] == 'P') {

if (sqr_i[to] == (pcCols[from] ? 0 : 7)) {
i = to;
}

if (pcCols[from]) {
s = 6; l = 4;
}
else {
s = 1; l = 3;
}

if ((sqr_i[from] == s) && (sqr_i[to] == l)) {
legEpNum = to;
}
}

pcNames[to] = pcNames[from];
pcCols[to] = pcCols[from];
pcNames[from] = '';
pcCols[from] = null;

if (i != -1) {
doPromotion(i);
}
}

function setPiece(num, pc) {

if (pc == '') {
document.getElementById('pc' + num).style.visibility = 'hidden';
pcNames[num] = '';
pcCols[num] = null;
}
else {
with (document.getElementById('pc' + num)) {
src = 'files/' + pc + '.gif';
style.visibility = 'inherit';
}

pc = pc.toUpperCase();
pcNames[num] = pc.charAt(1);
pcCols[num] = (pc.charAt(0) == 'W');
}
}

function doPromotion(num, pc) {

var i, c, pcn, htmlProm;

if (typeof(pc) == 'undefined') {

doingPromotion = true;
pcn = new Array('Q','R','B','N','Queen','Rook','Bishop','Knight');
c = (pcCols[num] ? 'W' : 'B');

htmlProm = '

' + (pcCols[num] ? 'White' : 'Black') + ' pawn on square ' + sqrLbls[num] + ' must be exchanged for a new queen, rook, bishop or knight.
Please select the new piece:

‘;
for (i = 0; i < 4; i++) {
htmlProm += '

‘;
}
htmlProm += ‘


‘ + pcn[i + 4] + ‘

‘;

showDlg(‘PROMOTION’, htmlProm, ”, ”);
return;
}

hideDlg();
setPiece(num, pc);
addToLastMove(‘=’ + pc.charAt(1));
doingPromotion = false;
changeTurn();
showStatus(true);
}

function sqrMouseOver(num) {

if (selNum == -1) {
if (pcCols[num] == turn) {
setCursor(num, ‘hand’);
showBorder(num, preSelColor);
}
}
else if (selNum == num) {
setCursor(num, ‘hand’);
}
else if (canMove(selNum, num, true, false) && !willBeInCheck(pcCols[selNum], selNum, num)) {

setCursor(num, ‘hand’);
showBorder(num, toSelColor);

if (moveInfo[0] != -1) {
showBorder(moveInfo[0], exSelColor);
showBorder(moveInfo[1], exSelColor);
}
else if (moveInfo[3] != -1) {
showBorder(moveInfo[3], exSelColor);
}
}
}

function sqrMouseOut(num) {

setCursor(num, ‘default’);

if (num != selNum) {
hideBorder(num);
}

if (moveInfo[0] != -1) {
hideBorder(moveInfo[0]);
hideBorder(moveInfo[1]);
moveInfo[0] = moveInfo[1] = -1;
}
else if (moveInfo[3] != -1) {
hideBorder(moveInfo[3]);
moveInfo[3] = -1;
}
}

function sqrClick(num) {

var mvstr;

if (selNum == -1) {
if (pcCols[num] == turn) {
showBorder(num, fromSelColor);
selNum = num;
sqrMouseOut(num);
sqrMouseOver(num);
}
}
else if (selNum == num) {
hideBorder(num);
selNum = -1;
sqrMouseOut(num);
sqrMouseOver(num);
}
else if (canMove(selNum, num, true, false) && !willBeInCheck(pcCols[selNum], selNum, num)) {

if (moveInfo[0] != -1) {
mvstr = (moveInfo[2] ? ‘0-0’ : ‘0-0-0’);
}
else if (moveInfo[3] != -1) {
mvstr = pcNames[selNum] + sqrLbls[selNum] + ‘ – ‘ + sqrLbls[num] + ‘ x P’ + sqrLbls[moveInfo[3]] + ‘ e.p.’;
}
else {
mvstr = pcNames[selNum] + sqrLbls[selNum] + (pcNames[num] == ” ? ‘ – ‘ : ‘ x ‘) + pcNames[num] + sqrLbls[num];
}

hideBorder(selNum);
movePiece(selNum, num);

if (moveInfo[0] != -1) {
movePiece(moveInfo[0], moveInfo[1]);
}
else if (moveInfo[3] != -1) {
setPiece(moveInfo[3], ”);
}

selNum = -1;
addMove(turn, mvstr);

if (!doingPromotion) {
changeTurn();
}

showStatus(true);
sqrMouseOut(num);
sqrMouseOver(num);
}
}

function showStatus(add) {

var i, j, ch, mv, p = ‘‘ + players[turn ? 0 : 1] + ‘‘;

if (doingPromotion) {
spnStatus.innerHTML = ‘Waiting for ‘ + p + ‘ to select a piece…’;
return;
}

if (isDeadPos()) {
spnStatus.innerText = ‘Game ended in a dead position.’;
if (add) {
addToLastMove(‘ [½-½]’);
}
gameOver();
return;
}

ch = isInCheck(turn);
mv = hasLegalMove(turn);

switch (true) {
case (!ch && mv):
spnStatus.innerHTML = ‘Waiting for ‘ + p + ‘ to move…’;
break;

case (ch && mv):
spnStatus.innerHTML = (turn ? ‘White’ : ‘Black’) + ‘ king is in check. Waiting for ‘ + p + ‘ to move…’;
if (add) {
addToLastMove(‘ +’);
}
break;

case (ch && !mv):
spnStatus.innerHTML = p + ‘ is checkmated!’;
if (add) {
addToLastMove(‘ # [‘ + (turn ? ‘0-1’ : ‘1-0’) + ‘]’);
}
gameOver();
break;

case (!ch && !mv):
spnStatus.innerText = ‘Game ended in stalemate.’;
if (add) {
addToLastMove(‘ [½-½]’);
}
gameOver();
break;
}
}

function changeTurn() {

turn = !turn;

if (playersTime[0] != -1) {
with(tblClock.rows(turn ? 0 : 1).cells(2)) {
style.color = ‘green’;
innerHTML = ‘&#9658’;
}
with(tblClock.rows(turn ? 1 : 0).cells(2)) {
style.color = ‘red’;
innerHTML = ‘ll’;
}
}
}

function getKingLocation(color) {

var i;

for (i = 0; i < 64; i++) {
if ((pcNames[i] == 'K') && (pcCols[i] == color)) {
return(i);
}
}
}

function getSqrNum(i, j) {
return(i * 8 + j);
}

function formatSecs(secs) {

var sc = secs % 60;
if (sc < 10) {sc = '0' + sc;}
return(Math.floor(secs / 60) + ':' + sc);
}

function trim(str) {
return(str.replace(/^[\s]+|[\s]+$/g, ''));
}

function stopClock(reset) {

clearInterval(timerId);

with(tblClock.rows(0).cells(2)) {
style.color = 'red';
innerHTML = 'll';
}
with(tblClock.rows(1).cells(2)) {
style.color = 'red';
innerHTML = 'll';
}

if (reset) {
tblClock.rows(0).cells(1).innerText = tblClock.rows(1).cells(1).innerText = '00:00';
}
}

function startClock() {

clearInterval(timerId);

tblClock.rows(0).cells(1).innerText = formatSecs(playersTime[0]);
tblClock.rows(1).cells(1).innerText = formatSecs(playersTime[1]);

timerId = setInterval('timer();', 1000);
}

function addMove(color, str) {
moveNum += 0.5;
spnMoves.innerText += (moveNum == 0.5 ? '' : '\n') + Math.ceil(moveNum) + '.' + (color ? 'W' : 'B') + '. ' + str;
spnMovesEnd.scrollIntoView();
}

function addToLastMove(str) {
spnMoves.innerText += str;
}

function copyText(text) {
if (clipboardData) {
clipboardData.setData('text', text);
}
else {
alert('This feature is not supported by your browser.');
}
}

function setCursor(num, cursor) {
document.getElementById('sqr' + num).style.cursor = document.getElementById('pc' + num).style.cursor = cursor;
}

function showBorder(num, color) {
document.getElementById('sqr' + num).style.border = '2pt solid ' + color;
}

function hideBorder(num) {
document.getElementById('sqr' + num).style.border = 'none';
}

function promptEndGame() {
return(!playing || confirm('Are you sure you want to end the current game?'));
}

function exitGame() {
if (promptEndGame()) {close();}
}

function disableEdit() {
document.onselectstart = document.oncontextmenu = function(){return(false);};
}

function enableEdit() {
document.onselectstart = document.oncontextmenu = function(){return(true);};
}

function initPage() {

disableEdit();
window.onbeforeunload = function(){if (playing) {event.returnValue = document.title;}};

createChessboard();
clearGame();

spnGameTitle.innerText += ' ' + gameVer;
aMail.href += ' ' + gameVer;
divAll.style.visibility = 'visible';

checkNavigator();
}

function checkNavigator() {

var i, av, e;

av = navigator.appVersion;
i = av.indexOf('MSIE');

try {
if (i == -1) {
throw true;
}

if (parseFloat(av.substr(i + 5, 3)) < 5.5) {
throw false;
}
}
catch(e) {
alert('Web Chess Pro ' + gameVer + ' is compatible with Internet Explorer 5.5 or later, but you are using ' +
(e ? navigator.appName : 'an older version of Internet Explorer') +
'.\nIt might cause you some problems.');
}
}

function showDlg(title, body, focus, submit) {

if (frmDlg.style.visibility != 'hidden') {return(false);}

pDlgTitle.innerText = title;
frmDlg.innerHTML = pDlgTitle.outerHTML + body;
frmDlg.style.posLeft = (document.body.clientWidth – frmDlg.offsetWidth) / 2 + document.body.scrollLeft;
frmDlg.style.posTop = (document.body.clientHeight – frmDlg.offsetHeight) / 2 + document.body.scrollTop;
frmDlg.onsubmit = new Function('event.returnValue=false;' + submit);
divOpaqueScreen.style.visibility = 'visible';
frmDlg.style.visibility = 'visible';
setTimeout('frmDlg.' + (focus == '' ? 'focus();' : focus + '.focus();'), 0);

return(true);
}

function hideDlg() {

if (frmDlg.style.visibility == 'hidden') {return;}

frmDlg.style.visibility = 'hidden';
divOpaqueScreen.style.visibility = 'hidden';
frmDlg.style.posLeft = frmDlg.style.posTop = 0;
pDlgTitle.innerHTML = '';
frmDlg.innerHTML = pDlgTitle.outerHTML;
frmDlg.onsubmit = function(){return(false);};
disableEdit();
}

function timer() {

var i = (turn ? 0 : 1);

tblClock.rows(i).cells(1).innerText = formatSecs(–playersTime[i]);

if (playersTime[i] == 0) {
timeUp(turn, true);
}
}

function timeUp(color, add) {

gameOver();
hideDlg();

if (canCheckmate(!color)) {
spnStatus.innerHTML = '‘ + players[color ? 1 : 0] + ‘ wins the game!’;
if (add) {
spnMoves.innerText += (moveNum == 0 ? ” : ‘\n’) + (color ? ‘[0-1]’ : ‘[1-0]’);
}
}
else {
spnStatus.innerHTML = ‘Game is drawn.’;
if (add) {
spnMoves.innerText += (moveNum == 0 ? ” : ‘\n’) + ‘[½-½]’;
}
}

if (add) {
spnMovesEnd.scrollIntoView();
}
}

function gameOver() {
stopClock(false);
inactivateChessboard();
playing = false;
}

function spnMovesPropChange() {
if (event.propertyName == ‘innerText’) {
if (spnMoves.innerText == ”) {
btnCopy.disabled = true;
btnCopy.style.cursor = spnCopy.style.cursor = ‘default’;
}
else {
if (btnCopy.disabled) {
btnCopy.disabled = false;
btnCopy.style.cursor = spnCopy.style.cursor = ‘hand’;
}
}
}
}

function chkTimedGameClick() {
frmDlg.txtTime.disabled = lblTime.disabled = lblMin.disabled = !frmDlg.chkTimedGame.checked;
}

function saveSubmit() {
if (trim(frmDlg.inpFileSave.value) == ”) {
frmDlg.inpFileSave.focus();
}
else {
saveGame(frmDlg.inpFileSave.value, true);
}
}

function loadSubmit() {
if (trim(frmDlg.inpFileLoad.value) == ”) {
frmDlg.inpFileLoad.focus();
}
else {
loadGame(frmDlg.inpFileLoad.value);
}
}

function dlgMouseDown() {
dlgX = event.clientX – frmDlg.style.posLeft;
dlgY = event.clientY – frmDlg.style.posTop;
}

function bodyMouseMove() {

var x, y;

if ((dlgX != null) && ((event.button == 1) || (event.button == 3) || (event.button == 5) || (event.button == 7))) {

x = event.clientX – dlgX;
y = event.clientY – dlgY;

if (x document.body.clientWidth + document.body.scrollLeft) {
frmDlg.style.posLeft = document.body.clientWidth – frmDlg.offsetWidth + document.body.scrollLeft;
}
else {
frmDlg.style.posLeft = x;
}

if (y document.body.clientHeight + document.body.scrollTop) {
frmDlg.style.posTop = document.body.clientHeight – frmDlg.offsetHeight + document.body.scrollTop;
}
else {
frmDlg.style.posTop = y;
}
}
else {
dlgX = dlgY = null;
}
}
// ]]>

&lt;table style=”width:100%;height:50%;text-align:center”&gt; &lt;tr&gt; &lt;td&gt; &lt;span style=”font:14pt tahoma;color:#4465BF”&gt;Your browser does not support scripting or has restricted this page from running scripts.&lt;/p&gt; &lt;p&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style=”font:10pt tahoma;color:#707070″&gt;Web Chess Pro requires a JavaScript capable browser.&lt;br /&gt;If your browser supports scripting, please check your Internet security settings.&lt;/span&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s