var w3c=(document.getElementById)?true:false;
var ns4=(document.layers)?true:false;
var ie4=(document.all && !w3c)?true:false;
var ie5=(document.all && w3c)?true:false;
var ns6=(w3c && !document.all)?true:false;

var done,iswon,grid,scores;
var player2wins=0, player1wins=0, draws=0, games=-1;
var pl1turn=true;
var oktoplay=false;

function cacheImages(){
var imgs=new Array();
for(i=0;i<arguments.length;i++){
imgs[i]=new Array();
imgs[i].src=arguments[i];
}}
cacheImages("pl1a.gif","pl1b.gif","pl2a.gif","pl2b.gif");

function showStatus(num){
oktoplay=false;
var cntnt='. Press "Ok" to start a new game.';
if(num==1)alert('This game is a draw'+cntnt);
else if(num==2)alert('Black won this round'+cntnt);
else if(num==3)alert('Red won this round'+cntnt);
init();
}

function showScores(){
alert('Games Played ........ '+games+'\nBlack Score ........... '+player1wins+'\nRed Score ............ '+player2wins+'\nTie Games ............ '+draws+'\n\nClick "Ok" to continue.');
}

function testgrid(tmp){
done=true;
for(c=1;c<=7;c++){
if(grid[c][1]==0)done=false;
}
if(done){
draws++;
showStatus(1);
}else{
var tmp=(pl1turn)?1:2;
var rx,cx;
// TEST |
for(c=1;c<=7;c++){
for(r=1;r<=6;r++){
if((grid[c][r]==tmp)&&(grid[c][r+1]==tmp)&&(grid[c][r+2]==tmp)&&(grid[c][r+3]==tmp))iswon=true;
}}
// TEST -
if(!iswon){
for(r=1;r<=6;r++){
for(c=1;c<=4;c++){
if((grid[c][r]==tmp)&&(grid[c+1][r]==tmp)&&(grid[c+2][r]==tmp)&&(grid[c+3][r]==tmp))iswon=true;
}}}
// TEST \
if(!iswon){
for(r=1;r<=3;r++){
for(c=1;c<=4;c++){
if((grid[c][r]==tmp)&&(grid[c+1][r+1]==tmp)&&(grid[c+2][r+2]==tmp)&&(grid[c+3][r+3]==tmp))iswon=true;
}}}
// TEST /
if(!iswon){
for(r=4;r<=6;r++){
for(c=1;c<=4;c++){
if((grid[c][r]==tmp)&&(grid[c+1][r-1]==tmp)&&(grid[c+2][r-2]==tmp)&&(grid[c+3][r-3]==tmp))iswon=true;
}}}
if(iswon){
eval('player'+tmp+'wins++');
showStatus(tmp+1);
}else{
pl1turn=!pl1turn;
oktoplay=true;
}}}

function showhover(isON,column){
window.status='';
var plx=(pl1turn)?1:2;
document.images['col'+column].src=(isON)? "pl"+plx+"b.gif" : "nothing.gif";
}

function dropchip(column){
if(oktoplay){
oktoplay=false;
var moved=false;
var stop=false;
for(r=6;r>=1;r--){
if(!stop){
if(grid[column][r]==0){
grid[column][r]=(pl1turn)? 1 : 2 ;
document.images[('rc'+r+column)].src=(pl1turn)? "pl1a.gif" : "pl2a.gif";
moved=true;
stop=true;
}}}
document.images[('col'+column)].src=(pl1turn)? "pl2b.gif" : "pl1b.gif";
if(ie4||ie5)document.all["c4sound"].src="click.wav";
testgrid();
}}

function startC4(){
cacheC4Sounds();
alert('This is a 2 player game, black against red. Take turns dropping\nyour chips to get 4 chips in a row, column, or diagonally to win.\n\nClick "Ok" to continue...');
init();
}

function init(){
games++;
iswon=false;
grid=new Array();
for(c=1;c<=7;c++){
showhover(false,c);
grid[c]=new Array();
for(r=1;r<=6;r++){
grid[c][r]=0;
document.images[('rc'+r+c)].src="start.gif";
}}
oktoplay=true;
}

//JAVASCRIPT/\SOUND SECTION FOR IE ONLY
function cacheC4Sounds(){
if(ie4||ie5){
var c4SoundArray=["click.wav"];
var str='';
for (i=0;i<c4SoundArray.length;i++)str+='<embed src="'+c4SoundArray[i]+'" autostart="false" hidden="true">';
document.all["c4embed"].innerHTML=str;
}}

function writeHTML(){
var txt='<table cellpadding="0" cellspacing="0" border="0">';
txt+='<tr><td>&nbsp;</td>';
for(c=1;c<=7;c++)txt+='<td><a href="javascript:dropchip('+c+')" onmouseover="showhover(true,'+c+'); return true" onmouseout="showhover(false,'+c+')"><img src="nothing.gif" name="col'+c+'" border="0"></a></td>';
txt+='<td>&nbsp;</td></tr>';
for(r=1;r<=6;r++){
txt+='<tr><td><img src="side.gif"></td>';
for(c=1;c<=7;c++)txt+='<td width="40" height="40"><img src="start.gif" name="rc'+r+c+'"></td>';
txt+='<td><img src="side.gif"></td></tr>';
}
txt+='<tr><td><img src="side.gif"></td>';
for(c=1;c<=7;c++)txt+='<td valign="top"><img src="bot.gif"></td>';
txt+='<td><img src="side.gif"></td></tr>';
txt+='<tr align="center"><td colspan="9"><form><input type="button" class="button" value="Show Score" onclick="showScores()"></form></td></tr>';
txt+='</table>';
if(ie4||ie5)txt+='<div id="c4embed" style="position:absolute;"></div>';
document.write(txt);
}
writeHTML();

function refillGrid(){
for(r=1;r<=6;r++)
for(c=1;c<=7;c++)
if(grid[c][r]!=0)document.images[('rc'+r+c)].src=(grid[c][r]==1)? "pl1a.gif" : "pl2a.gif";
}


window.onload=function(){
startC4();
}

window.onresize=function(){
if(ns4)setTimeout('refillGrid()',400);
}