<!-- begin script

function getCookieVal (offset) {
 var endstr = document.cookie.indexOf (";", offset);
 if (endstr == -1)
  endstr = document.cookie.length;
 return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
 var arg = name + "=";
 var alen = arg.length;
 var clen = document.cookie.length;
 var i = 0;
 while (i < clen) {
  var j = i + alen;
  if (document.cookie.substring(i, j) == arg)
   return getCookieVal (j);
  i = document.cookie.indexOf(" ", i) + 1;
  if (i == 0) break; 
 }
 return null;
}

function SetCookie (name,value,expires,path,domain,secure) {
 document.cookie = name + "=" + escape (value) +
  ((expires) ? "; expires=" + expires.toGMTString() : "") +
  ((path) ? "; path=" + path : "") +
  ((domain) ? "; domain=" + domain : "") +
  ((secure) ? "; secure" : "");
}

function DeleteCookie (name,path,domain) {
 if (GetCookie(name)) {
  document.cookie = name + "=" +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   "; expires=Thu, 01-Jan-05 00:00:01 GMT";
 }
}

// End of Bills Code - back to Cookie-Tree

//Uncomment this to reset Games!!
//DeleteCookie("OTHELLOGAMES");
var expdate = new Date();
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000));

gamestring = GetCookie("OTHELLOGAMES");
if (gamestring == null) {
  gamestring = "0:0:";
  SetCookie("OTHELLOGAMES",gamestring,expdate);
}
mywins = parseInt(gamestring.substring(0,gamestring.indexOf(":")));
yourwins = parseInt(gamestring.substring(gamestring.indexOf(":")+1,gamestring.indexOf(":")+2));

function delay (time) {
  startdate = new Date();
  start = startdate.getTime();
  nowdate = new Date();
  while ((nowdate.getTime() - start) / 10 < time) {
     nowdate = new Date();
  }
}

hexn = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function tocode(numb) {
  return hexn.substring(numb,numb + 1);
}

function tonum(code) {
  return hexn.indexOf(code);
}

function makearray (dim) {
  this.length = dim;
  for (var i=1; i <= dim; i++) {
      this[i] = '';
  }
  return this
}

scoresuffix = "scorenr";
yourtiles = 2;
mytiles = 2;
gridsize=8
inomove = "N";
younomove = "N";

navvendor = navigator.appName.substring(0,8).toUpperCase();
navversion = parseInt(navigator.appVersion.substring(0,1));
runsok = "N";

if ((navvendor == "NETSCAPE" && navversion >= 3) || (navvendor == "MICROSOF" && navversion >=4)) {

runsok = "Y";

grid = new makearray(gridsize + 1);
for (var i=1; i < grid.length; i++) {
    grid[i] = new makearray(gridsize + 1);
}

valu = new makearray(gridsize + 1);
for (var i=1; i < valu.length; i++) {
    valu[i] = new makearray(gridsize + 1);
}
for (i=1;i <= gridsize / 2;i = i + 2) {
  valu[1] [i] = 65 - i;
  valu[2] [i] = i;
  valu[1] [i+1] = i;
  valu[2] [i+1] = i;
  valu[i] [1] = 65 - i;
  valu[i+1] [1] = i;
  valu[i] [2] = i;
  valu[i+1] [2] = i;
  valu[i] [i] = 65 - i;
  valu[i+1] [i] = i;
  valu[i] [i+1] = i;
  valu[i+1] [i+1] = i;

  valu[gridsize+1-(1)] [i] = 65 - i;
  valu[gridsize+1-(2)] [i] = i;
  valu[gridsize+1-(1)] [i+1] = i;
  valu[gridsize+1-(2)] [i+1] = i;
  valu[gridsize+1-(i)] [1] = 65 - i;
  valu[gridsize+1-(i+1)] [1] = i;
  valu[gridsize+1-(i)] [2] = i;
  valu[gridsize+1-(i+1)] [2] = i;
  valu[gridsize+1-(i)] [i] = 65 - i;
  valu[gridsize+1-(i+1)] [i] = i;
  valu[gridsize+1-(i)] [i+1] = i;
  valu[gridsize+1-(i+1)] [i+1] = i;

  valu[1] [gridsize+1-(i)] = 65 - i;
  valu[2] [gridsize+1-(i)] = i;
  valu[1] [gridsize+1-(i+1)] = i;
  valu[2] [gridsize+1-(i+1)] = i;
  valu[i] [gridsize+1-(1)] = 65 - i;
  valu[i+1] [gridsize+1-(1)] = i;
  valu[i] [gridsize+1-(2)] = i;
  valu[i+1] [gridsize+1-(2)] = i;
  valu[i] [gridsize+1-(i)] = 65 - i;
  valu[i+1] [gridsize+1-(i)] = i;
  valu[i] [gridsize+1-(i+1)] = i;
  valu[i+1] [gridsize+1-(i+1)] = i;

  valu[gridsize+1-(1)] [gridsize+1-(i)] = 65 - i;
  valu[gridsize+1-(2)] [gridsize+1-(i)] = i;
  valu[gridsize+1-(1)] [gridsize+1-(i+1)] = i;
  valu[gridsize+1-(2)] [gridsize+1-(i+1)] = i;
  valu[gridsize+1-(i)] [gridsize+1-(1)] = 65 - i;
  valu[gridsize+1-(i+1)] [gridsize+1-(1)] = i;
  valu[gridsize+1-(i)] [gridsize+1-(2)] = i;
  valu[gridsize+1-(i+1)] [gridsize+1-(2)] = i;
  valu[gridsize+1-(i)] [gridsize+1-(i)] = 65 - i;
  valu[gridsize+1-(i+1)] [gridsize+1-(i)] = i;
  valu[gridsize+1-(i)] [gridsize+1-(i+1)] = i;
  valu[gridsize+1-(i+1)] [gridsize+1-(i+1)] = i;

}

scoredigit = new makearray(9);
for (var i=0;i< 10;i++) {
  scoredigit[i] = new Image(30,30);
  scoredigit[i].src = "score/" + i + scoresuffix + ".gif";
}

black = new Image(30,30);
black.src = "black.gif";
white = new Image(30,30);
white.src = "white2.gif";
plain = new Image(30,30);
plain.src = "plain.gif";
ghostb = new Image(30,30);
ghostb.src = "ghostb.gif";
ghostw = new Image(30,30);
ghostw.src = "ghostw.gif";

youare = black.src;
iam = white.src;

endreached = "N";

}

function settile(link,setto) {
if (runsok == "Y") {
  document.images[link].src = setto;
}
}

function makegrid () {
  for (x=1;x <= gridsize;x++) {
    for (y=1;y <= gridsize;y++) {
      grid[x] [y] = plain.src;
      settile("C" + tocode(x) + tocode(y),plain.src);
    }
  }
  grid [gridsize / 2] [gridsize / 2] = black.src;
  grid [gridsize / 2] [gridsize / 2 + 1] = white.src;
  grid [gridsize / 2 + 1] [gridsize / 2 + 1] = black.src;
  grid [gridsize / 2 + 1] [gridsize / 2] = white.src;
  settile("C" + tocode(gridsize / 2) + tocode(gridsize / 2),black.src);
  settile("C" + tocode(gridsize / 2) + tocode(gridsize / 2 + 1),white.src);
  settile("C" + tocode(gridsize / 2 + 1) + tocode(gridsize / 2),white.src);
  settile("C" + tocode(gridsize / 2 + 1) + tocode(gridsize / 2 + 1),black.src);
}

function restart() {
  endreached = "N";
  yourtiles = 2;
  mytiles = 2;
  makegrid();
  setscores();
}


function setdigits (value,value_len,digitpref) {
  scorelen = 0;
  for (var i = 1;i <= value_len;i++) {
    if (value_len + 1 - i <= value.length) {
       document.images[digitpref + i].src = scoredigit[value.substring(scorelen,scorelen + 1)].src;
       scorelen++;
    } else {
       document.images[digitpref + i].src = scoredigit[0].src;
    }
  }
}

function setscores () {
  setdigits(mytiles.toString(),3,"MS");
  setdigits(yourtiles.toString(),3,"YS");
  setdigits(mywins.toString(),3,"MW");
  setdigits(yourwins.toString(),3,"YW");
}

function move(movetype,link,whoare) {
  ok = 0;
  score = 0;
  x = tonum(link.substring(1,2));
  y = tonum(link.substring(2,3));
  for (offx = -1;offx <= 1;offx++) {
    for (offy = -1;offy <= 1;offy++) {
      if (x + offx >= 1 && x + offx <= gridsize && y + offy >= 1 && y + offy <= gridsize && (offx != 0 || offy != 0)) {
        if (grid[x + offx] [y + offy] != plain.src && grid[x + offx] [y + offy] != whoare) {
          n = 1
          while (x + offx * n >= 1 && x + offx * n <= gridsize && y + offy * n >= 1 && y + offy * n <= gridsize) {
            if (movetype == "C") {
              if (grid[x + offx * n] [y + offy * n] == plain.src) {
                 break
              } else if (grid[x + offx * n] [y + offy * n] == whoare) {
                if (n - 1> ok) {
                   ok = n - 1;
                   break;
                }
              }

            } else if (movetype =="M") {
              if (grid[x + offx * n] [y + offy * n] == plain.src) {
                 break
              } else if (grid[x + offx * n] [y + offy * n] == whoare && movetype == "M") {
                for (n2 = 1;n2 < n;n2++) {
                  settile("C" + tocode(x + offx * n2) + tocode(y + offy * n2),whoare);
                  grid[x + offx * n2] [y + offy * n2] = whoare;
                  score++;
                }
                break;
              }
            }
            n++;
          }
        }
      }
    }
  }
  if (movetype == "C") {
     return ok;
  } else if (movetype == "M") {
     settile(link,whoare);
     grid[x] [y] = whoare;
     score++;
     return score;
  }
}

function checkmove () {
  for (x = 1;x <= gridsize;x++) {
    for (y = 1;y <= gridsize;y++) {
      if (grid[x] [y] == plain.src) {
         if (grid[x] [y] == plain.src && move("C","C" + tocode(x) + tocode(y),youare) > 0) {
            return "N";
         }
      }
    }
  }
  return "Y";
}

function compmove() {
  maxx = 0
  numb = 0;
  rndn = Math.random() * 8;
  bestx = 0
  besty = 0
  inomove = "N";
  for (x = 1;x <= gridsize;x++) {
    for (y = 1;y <= gridsize;y++) {
      if (grid[x] [y] == plain.src && move("C","C" + tocode(x) + tocode(y),iam) > 0) {
//Uncomment these to 'watch' the computer thinking!!
//        if (iam == white.src) {
//           settile("C" + tocode(x) + tocode(y),white.src);
//        } else {
//           settile("C" + tocode(x) + tocode(y),black.src);
//        }
//        delay(10);
//        settile("C" + tocode(x) + tocode(y),plain.src);
//       delay(10);
        if (valu[x] [y] > maxx || valu[x] [y] == maxx && numb <= rndn) {
         if (valu[x] [y] > maxx) {
            numb = 0;
         }
         bestx = x; besty=y; maxx=valu[x] [y]; numb++;
        } 
      } 
    }
  }
  if (bestx == 0) {
     inomove = "Y";
     alert("I have no move - you move again");
  } else {
    for (i=1;i < 5;i++) {
      if (iam == white.src) {
         settile("C" + tocode(bestx) + tocode(besty),ghostw.src);
      } else {
         settile("C" + tocode(bestx) + tocode(besty),ghostb.src);
      }
      delay(10);
      settile("C" + tocode(bestx) + tocode(besty),plain.src);
      delay(10);
    }
    score = move("M","C" + tocode(bestx) + tocode(besty),iam);
    mytiles = mytiles + score;
    yourtiles = yourtiles - (score - 1);
  }
}

function procclick (link) {
if (runsok == "Y" && endreached == "N") {
  if (grid[tonum(link.substring(1,2))] [tonum(link.substring(2,3))] == plain.src && move("C",link,youare) > 0) {
     score = move("M",link,youare);
     yourtiles = yourtiles + score;
     mytiles = mytiles - (score - 1);
     if (mytiles + yourtiles < 64) {
        compmove();
        if (mytiles + yourtiles < 64) {
          younomove = checkmove();
        }
     }
     if (mytiles + yourtiles == 64) {
        endreached = "Y";
     }
     while(younomove == "Y" && endreached == "N") {
       if (inomove == "Y") {
           endreached = "Y";
       } else {
         alert("You have no possible move - I will move again!");
         compmove();
       }
     }
     setscores();
     if (endreached == "Y") {
       if (yourtiles > mytiles) {
          confst = "YOU WON! - Another Game?";
          yourwins++;
       } else if (yourtiles == mytiles) {
          confst = "A Draw!! - Another Game?";
          mywins++;
       } else {
          confst = "I WON! - Another Game?";
          mywins++;
       }
       setscores;
       SetCookie("OTHELLOGAMES",mywins + ":" + yourwins,expdate);
       if (confirm(confst)) {
          restart();
       }
     }
  }
}

}

function procover (link) {
if (runsok == "Y" && endreached == "N") {
  if (grid[tonum(link.substring(1,2))] [tonum(link.substring(2,3))] == plain.src) {
     if (move("C",link,youare) > 0) {
        if (youare == black.src) {
           settile(link,ghostb.src);
        } else {
           settile(link,ghostw.src);
        }
     }
  }
}

}

function procout (link) {
if (runsok == "Y" && endreached == "N") {
  if (grid[tonum(link.substring(1,2))] [tonum(link.substring(2,3))] == plain.src) {
     settile(link,grid[tonum(link.substring(1,2))] [tonum(link.substring(2,3))]);
  }
}

}

if (runsok == "Y") {
 document.writeln("<table border=0 cellpadding=0 cellspacing=0>");
  for (var x = 1;x <= gridsize;x++) {
    document.writeln("<TR>");
    for (var y = 1;y <= gridsize;y++) {
      document.write("<TD HEIGHT=30 WIDTH=30>");
      document.write('<A HREF="javascript://" onMouseOver="procover(\'C' + tocode(x) + tocode(y) + '\')" onMouseout="procout(\'C' + tocode(x) + tocode(y) + '\')" onClick="procclick(\'C' + tocode(x) + tocode(y) + '\')" >');
      document.write('<IMG SRC="plain.gif" WIDTH=30 HEIGHT=30 BORDER=0 NAME="C' + tocode(x) + tocode(y) + '"></A>');
      document.writeln("</TD>");
    }
    document.writeln("</TR>");
  }
  document.writeln("</TABLE>");
  makegrid();
  setscores();
} else {
  document.writeln("<CENTER><P><H2>Sorry - This game only works with<BR>Netscape Navigator 3 or 4 or MS Internet Explorer 4</H2></CENTER>");
}

// --><!-- end hiding contents from old browsers -->
