var a,b,e,i;
var f=document.myform;
var mess="Roll the dice !";
var mess2="Penalty Score = ";
var mess3="Well done !";
var total;
var numstr;
var whatsleft;
var oneless,onemore;
var nums=new Array(1,2,3,4,5,6,7,8,9);
var gameover=false;
var cango=false;
var first,second;
var rolls;
var temp;


function init(){
cango=false;
f=document.myform;
f.dice[0].value="";
f.dice[1].value="";
rolls=0;
gameover=false;
f=document.myform;
for(i=0;i<9;i++){
a=i+1;
nums[i]=a;
onemore=a.toString();
f.shut[i].value=onemore;
}
thelement=document.getElementById("sumtotal");
thelement.innerHTML="<b style='font-size:12;font-weight:bold; color:red;'>"+mess+"</b>";
}

function rolldice(){
if(gameover==false){
if(rolls!=12){
first=Math.ceil(Math.random()*6);
second=Math.ceil(Math.random()*6);
f.dice[0].value=first;
f.dice[1].value=second;
rolls++;
total=first+second;
thelement=document.getElementById("sumtotal");
thelement.innerHTML="<b style='font-size:12;font-weight:bold; color:red;'>"+total+"</b>";
window.setTimeout("rolldice()",75);
}
else
checktotal();
  }
}

function cell(e){
oneless=e-1;
if(cango==true){
if(gameover==false && nums[oneless]==total){
nums[oneless]=0;
f.shut[oneless].value="";
cango=false;
rolls=0;
thelement=document.getElementById("sumtotal");
thelement.innerHTML="<b style='font-size:12;font-weight:bold; color:red;'>"+mess+"</b>";
checkwin();
    }
  }
if(cango==true && gameover==false){
temp=total-nums[oneless];
for(i=0;i<9;i++){
if(nums[i]==temp && nums[i]!=nums[oneless]){
nums[oneless]=0;
f.shut[oneless].value="";
total=temp;
thelement=document.getElementById("sumtotal");
thelement.innerHTML="<b style='font-size:12;font-weight:bold; color:red;'>"+temp+"</b>";
break;
             }
        }
     }

if(cango==true && gameover==false){
temp=total-nums[oneless];
for(i=0;i<9;i++){
a=i+1;
while(a<8){
if(nums[i]!=nums[oneless] && nums[a]!=nums[oneless]){
if(nums[i]+nums[a]==temp){
nums[oneless]=0;
f.shut[oneless].value="";
total=temp;
thelement=document.getElementById("sumtotal");
thelement.innerHTML="<b style='font-size:12;font-weight:bold; color:red;'>"+temp+"</b>";
break;
            }
        }
     a++;
    }
   if(nums[oneless]==0){
    break;
        }
     }
  }
if(nums[oneless]>0){
document.all.beep.src="http://www.johnlayton.co.uk/sounds/beep.wav";

}
}

function checktotal(){
if(total<=9 && nums[total-1]>0){
cango=true;
   }
if(cango==false){
for(i=0;i<8;i++){
a=i+1;
while(a<9){
if((nums[i]+nums[a])==total){
cango=true;
break;
              }
a++;
        }
if(cango==true){
break;
      }
   }
 }
if(cango==false){
for(i=0;i<8;i++){
a=i+1;
b=i+2;
while(b<9){
if((nums[i]+nums[a]+nums[b])==total){
cango=true;
break;
              }
b++;
        }
if(cango==true){
break;
      }
   }
 }
if(cango==false){
if((nums[0]+nums[1]+nums[2]+nums[3])==total){
cango=true;
         }
else
if((nums[0]+nums[1]+nums[2]+nums[4])==total){
cango=true;
         }
else
if((nums[0]+nums[1]+nums[2]+nums[5])==total){
cango=true;
         }
   }
if(cango==false){
whatsleft=0;
for(i=0;i<9;i++){
whatsleft=whatsleft+nums[i];
}
thelement=document.getElementById("sumtotal");
thelement.innerHTML="<b style='font-size:12;font-weight:bold; color:red;'>"+mess2+whatsleft+"</b>";
document.all.beep.src="http://www.johnlayton.co.uk/sounds/beep.wav";
gameover=true;
   }
}

function checkwin(){
temp=0;
for(i=0;i<9;i++){
temp+=nums[i];
   }
if(temp==0){
gameover=true;
thelement.innerHTML="<b style='font-size:12;font-weight:bold; color:red;'>"+mess3+"</b>";
document.all.beep.src="http://www.johnlayton.co.uk/sounds/beep.wav";
    }
}

function help(){
alert("Begin by clicking the roll dice button\nand then close boxes to the value of your\ntotal score by clicking on them. The object\nof the game is to close all the boxes. If\nyou can't go your penalty is the total of\nthe numbers left on the board.");  
}
