carry=0;
this_gap=-1;
return_obj=-1;
helpflag=0;
checkflag=0;
all_done=0;

app="I";if (navigator.appName=="Netscape") app="N";
mac=1;if(navigator.appVersion.indexOf("Macintosh")==-1) mac=0;

app="I";
if(navigator.appName=="Netscape") app="N";
if(app=="N" && parseInt(navigator.appVersion)>4) app="N6";

if (app=="N")  {
doc_ref='document.layers["';
style_ref='"]';
layer_ref='document.instruction.document.';
}
else {
if (app=="N6") {
doc_ref='document.getElementById("';
style_ref='").style';
layer_ref='document.';
}
else {
doc_ref='document.all["';
style_ref='"].style';
layer_ref='document.';
}
}

function hideLayer(layerName){eval(doc_ref+layerName+style_ref+'.visibility="hidden"');}
function showLayer(layerName){eval(doc_ref+layerName+style_ref+'.visibility="visible"');}

function moveLayer(layerName,left,top){
eval(doc_ref+layerName+style_ref+'.top=top');
eval(doc_ref+layerName+style_ref+'.left=left');
return false;
}



// *****************

if (app=="I") {
window.document.onmousemove=mouseMoveIE;
}
else {

if (app=="N") {
document.captureEvents(Event.MOUSEMOVE);
document.onmousemove=mouseMoveNS;
}
else window.onmousemove=mouseMoveNS;
}


function mouseMoveIE() {
lx=window.event.x;ly=window.event.y+14;
//if(mac==1) {lx+=document.body.scrollLeft;ly+=document.body.scrollTop;}
ly+=document.body.scrollTop;
if(carry>0) moveLayer("carryword",lx,ly);
}

function mouseMoveNS(e) {
lx=e.pageX;ly=e.pageY+14;
if(carry>0) moveLayer("carryword",lx,ly);
}

// *** DATA ***


order=new Array(total_words);

moved_flag=new Array(total_words);

placed_word=new Array(total_words);

placed_flag=new Array(total_words);

empty="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"

text_width=470;

buttonsx=666;

v=0;for(i=0;i<total_words;i++) {placed_flag[i]=-1;moved_flag[i]=0;placed_word[i]=empty;v+=block[i].length;v+=words[i].length;}
v+=block[i].length;
text_length=v*7.14;
tlines=text_length/text_width+br;
theight=tlines*18+40;
bheight=total_words*18+48;
if(theight>bheight) bheight=theight;
else if(bheight>theight) theight=bheight;


// *** LAYERS ***

makelayer('dragon','visible',-145,10,145,101,1100);
w('<img src="../images/dragrt.gif" width="145" height="101">');
endbit();

makelayer('ball','visible',-100,80,18,18,1000);
w('<img src="../images/ball.gif" width="18" height="18">');
endbit();

makelayer('congrats','hidden',320,theight-40,134,29,1000);
w('<p align="center"><img src="../images/congrats.gif" width="14" height="29">');
endbit();

makelayer('para','visible',160,20,text_width,200,500);
endbit();

makelayer('wordlist','visible',20,20,150,200,500);
endbit();

makelayer('carryword','hidden',20,100,100,20,700);
endbit();

makelayer('textback','visible',150,10,text_width+20,theight,400);
w('<table border="0" cellpadding="0" cellspacing="0" width="'+(text_width+20)+'" height="'+theight+'" bgcolor="#FFFFEC"><tr><td>&nbsp; </td></tr></table>');
endbit();

makelayer('listback','visible',10,10,120,bheight,400);
w('<table border="0" cellpadding="0" cellspacing="0" width="120" height="'+bheight+'" bgcolor="#FFFFEC"><tr><td>&nbsp; </td></tr></table>');
endbit();

makelayer('buttons','visible',buttonsx,20,54,100,600);
w('<table border="0" cellpadding="0" cellspacing="0"><tr><td>');
w('<a href="javascript:check(1)" onmouseover="checkbut.src=\'../images/check2.gif\'" onmouseout="checkbut.src=\'../images/check1.gif\'"><img name="checkbut" src="../images/check1.gif" width="54" height="39" border="0"></a>');
w('<br>&nbsp; </td></tr><tr><td align="center">');
w('<a href="javascript:discard()" onmouseover="wdbin.src=\'../images/bin2.gif\'" onmouseout="wdbin.src=\'../images/bin1.gif\'"><img name="wdbin" src="../images/bin1.gif" width="37" height="63" border="0"></a>');
w('</td></tr><tr><td>&nbsp; <br>');
w('<a href="javascript:help()" onmouseover="helpbut.src=\'../images/help2.gif\'" onmouseout="helpbut.src=\'../images/help1.gif\'"><img name="helpbut" src="../images/help1.gif" width="54" height="28" border="0"></a>');
w('</td></tr><tr><td>&nbsp; <br>');
w('<a href="javascript:quit()" onmouseover="quitbut.src=\'../images/quit2.gif\'" onmouseout="quitbut.src=\'../images/quit1.gif\'"><img name="quitbut" src="../images/quit1.gif" width="54" height="28" border="0"></a>');
w('</td></tr></table>');
endbit();

makelayer('helpmess','hidden',165,140,400,100,900);
w('<table border="1" cellpadding="6" cellspacing="0" bgcolor="#0000FF" width="400"><tr><td><p id="helpstyle"><a Class="white">');
w('Click on a word in the word list to pick it up.<br><br>Click on a space to drop the word into the text.<br><br>If you want to remove a word from the text, click on it and then click on the Word Bin.<br><br>Press the <strong>check</strong> button to see how well you are doing.</a>');
w('<form><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td align="center"><input type="button" value="ok" name="ok2" onclick="help()"></td></tr></table></form>');
w('</td></tr></table>');
endbit();

nobit="&nbsp;no&nbsp;";
if(mac==1) nobit="no";

makelayer('binmessage','visible',-500,20,140,100,1000);
w('<table ID="this_table" border="1" cellpadding="6" cellspacing="0" bgcolor="#F2F2FF"><tr><td align="center">');
w('<p id="helpstyle">If you want to remove a word from a space, you can drop it in the Word Bin.');
w('<form><input type="button" value="ok" name="ok" onclick="nobin()"></form>');
w('</td></tr></table>');
endbit();

makelayer('checkmessage','visible',-500,20,140,100,1000);
w('<table ID="this_table" border="1" cellpadding="6" cellspacing="0" bgcolor="#F2F2FF"><tr><td align="center">');
w('<p id="helpstyle">You have not put any words in the spaces yet.');
w('<form><input type="button" value="ok" name="ok" onclick="nocheck()"></form>');
w('</td></tr></table>');
endbit();

makelayer('quitmessage','visible',-500,20,140,100,1000);
w('<table ID="this_table" border="1" cellpadding="6" cellspacing="0" bgcolor="#F2F2FF"><tr><td align="center">');
w('<p id="helpstyle">Are you sure you want to quit?');
w('<form><input type="button" value="'+nobit+'" name="n" onclick="noquit()">&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="yes" name="y" onclick="yes()"></form>');
w('</td></tr></table>');
endbit();

window.focus();

//**** DRAGON ****

function dragon() {
hideLayer("para");
;moveLayer("para",-700,0)
all_done=1;
q=-7;
setTimeout('move_dragon()',40);
}

function move_dragon() {
moveLayer("dragon",q*32,20);
q++;
if(q==4) {b=0;move_ball();}
if(q<22) setTimeout('move_dragon()',40);
else moveLayer("dragon",-145,20);
}

ballx=new Array(90,206,261,298,323,339,355,371,377);

bally1=100;bally2=theight-40;bystep=(bally2-bally1)/8;

function move_ball() {
moveLayer("ball",ballx[b],bally1+(bystep*b));
b++;
if(b<9) setTimeout('move_ball()',60);
else  setTimeout('hide_ball()',120);
}

function hide_ball(){
moveLayer("ball",-145,20);
showLayer("congrats");
cw=4;
grow();
}

function grow() {
cw+=10;
str='<p align="center"><img src="../images/congrats.gif" width="'+cw+'" height="29">';
write_to_layer("congrats");
if(cw<134) setTimeout('grow()',40);
else {showLayer("para");moveLayer("para",160,20);write_text(1);}
}

function chckcon(){
if(all_done==1) hideLayer("congrats");
}

//**** HELP ****

function help() {
chckcon();
if(helpflag==0) {showLayer("helpmess");moveLayer("helpmess",165,140);helpflag=1;}
else {hideLayer("helpmess");moveLayer("helpmess",-200,-200);helpflag=0;}
}


//**** QUIT ****

function quit() {
moveLayer("quitmessage",buttonsx-160,140);
}

function noquit(){
moveLayer("quitmessage",-500,-500);
}

function nobin(){
moveLayer("binmessage",-500,-500);
}

function nocheck(){
moveLayer("checkmessage",-500,-500);
}

function yes(){
moveLayer("quitmessage",-500,-500);
history.back();
}


// *** CHECK ***

function check(chck) {
chckcon();
j=0;for(i=0;i<total_words;i++) {j+=placed_flag[i];}
if((j+total_words)==0) {
moveLayer("checkmessage",buttonsx-160,20);
};
correct=0;
for(i=0;i<total_words;i++) {
if(placed_word[i]==words[i]) correct++;
}
if(chck==1) {
if(checkflag==1) write_text(0);
else write_text(1);
}
}


// *** OVER GAP ***

function over_gap(gn) {
this_gap=gn;
if(carry==1) {
placeword();
carry=0;
}
else if(carry==0){
if(placed_flag[this_gap]>-1) {
return_obj=this_gap;
getword(placed_flag[this_gap],2);
}
}
else if(carry==2) do_swap();
}


// *** PLACE WORD ***

function placeword() {
if(placed_flag[this_gap]>-1) moved_flag[placed_flag[this_gap]]=0;
hideLayer("carryword");
moveLayer("carryword",-160,-100);
placed_word[this_gap]=words[order[word]];
placed_flag[this_gap]=word;
moved_flag[word]=1;
make_list();
check(0);
if(correct==total_words) dragon();
else setTimeout('write_text(0)',40);
}



// *** SWAP WORDS ***

function do_swap() {
r=return_obj;t=this_gap;
temp=placed_flag[t];placed_flag[t]=placed_flag[r];placed_flag[r]=temp;
temp=placed_word[t];placed_word[t]=placed_word[r];placed_word[r]=temp;
setTimeout('do_swap2()',40);
}

function do_swap2() {
write_text(0);
carry=0;
hideLayer("carryword");
check(0);
if(correct==total_words) dragon();
}


// *** GET WORD FROM LIST ***

function getword(wd,cr) {
carry=cr;
word=wd;
str='<p id="textstyle"><a Class="blue">'+words[order[word]]+'</a></p>';
write_to_layer("carryword");
moveLayer("carryword",lx,ly);
showLayer("carryword");
}


// *** CANCEL WORD ***

function discard() {
chckcon();
if(carry==0) {
moveLayer("binmessage",buttonsx-160,60);
}
hideLayer("carryword");
moveLayer("carryword",-200,-200);
moved_flag[word]=0;
make_list();
placed_word[return_obj]=empty;
placed_flag[return_obj]=-1;
write_text(0);
carry=0;
}

// *** UPDATE LIST ***

function make_list() {
str='<p id="textstyle">';
for(i=0;i<total_words;i++) {
bit='<a href="javascript:getword('+i+',1)">'+words[order[i]]+'</a><br>';
if(moved_flag[i]==1) bit='<font color="#D9D9FF">'+words[order[i]]+'<br>';
str+=bit;
}
str+='</p>';
write_to_layer("wordlist");
}

// *** UPDATE TEXT ***

function write_text(checking) {
checkflag=checking;
str='<p id="textstyle" align="center"><strong>'+title+'</strong></p><p id="textstyle">';
for(i=0;i<total_words;i++) {
if(checking==0) tcolor="blue";
else {tcolor="red";if(placed_word[i]==words[i]) tcolor="#35CA44";}
str+=(block[i]+'<a href="javascript:over_gap('+i+')"><font color="'+tcolor+'">'+placed_word[i]+'</font></a>');
}
str+=block[i];
write_to_layer("para");
}

function wbin(bn) {
eval('wdbin.src="../images/bin'+bn+'.gif"');
}


function init() {
randomise();
write_text(0);
make_list();
}

// *** RANDOM ORDER ***

function randomise() {
order[0]=get_random();
for(i=1;i<(total_words); i++) {
ok=false;
while(ok==false) {
order[i]=get_random();
ok2=true;
for(j=0;j<i; j++) {
if(order[i]==order[j]) ok2=false;
}
if(ok2==true) ok=true;
}
}
}

function get_random() {return parseInt(Math.random()*1000)%total_words;}


// *****************


function write_to_layer(layer) {
if (app=="N") {with (document.layers[layer].document) {open();write(str);close();}}
else
if (app=="N6") {
eval(doc_ref+layer+'").innerHTML=str');
}
else document.all[layer].innerHTML=str;
}

function makelayer(name,vis,left,top,width,height,z) {
if(app=="N") w('<layer id="'+name+'" position=absolute visibility='+vis+' left='+left+' top='+top+' width='+width+' height='+height+' z-index='+z+';>');
else w('<div id="'+name+'" STYLE="position:absolute;visibility:'+vis+';left:'+left+';top:'+top+';width:'+width+';height:'+height+';z-index:'+z+';">');
}

function endbit() {
if (app=="N") w('</layer>');
else w('</div>');
}

function w(cont) {document.write(cont);}
