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

lc="abcdefghijklmnopqrstuvwxyz0123456789()+&@:,~$.=#-%!£?;'*_/[]";
uc="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

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+20;
if(carry>0) moveLayer("carryword",lx-8,ly);
}


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

// *** DATA ***

moved_flag=new Array(total_punc_marks);

placed_word=new Array(total_punc_marks);

placed_flag=new Array(total_punc_marks);

pmarks=new Array("dq1","dq2","sq1","sq2","comma","dot","pling","query","semi","colon");
punc=new Array(10);

empty="empty";

text_width=510;

v=0;for(i=0;i<total_punc_marks;i++) {placed_flag[i]=-1;moved_flag[i]=0;placed_word[i]=empty;v+=block[i].length;v+=punc_marks[i].length;}
v+=block[i].length;
text_length=v*7.14;
tlines=text_length/text_width+br;
theight=tlines*18+40;

k=0;
for(i=0;i<10; i++) {
ok=false;
for(j=0;j<total_punc_marks; j++) {
if((ok==false)&&(pmarks[i]==punc_marks[j])) {ok=true;punc[k]=pmarks[i];k++;}
}
}
used=k;


// *** LAYERS ***

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

makelayer('star','hidden',510,theight-140,83,61,1100);
w('<img src="../images/star.gif" width="83" height="61">');
endbit();

makelayer('para','visible',130,20,text_width,200,600);
endbit();

makelayer('punclist','visible',666,20,44,50,500);
odd=used%2;
if(used==1) odd=2;
w('<table border="1" cellpadding="0" cellspacing="0" bgcolor="#EDF5F5">');
j=0;
for(i=0;i<used/2;i++) {
w('<tr>');
w('<td><a href="javascript:getpunc('+j+',1)"><img src="../puncs/l'+punc[j]+'.gif" width="22" height="34" border="0"></a></td>');
j++;
if((odd==1)&&(j==used)) w('<td>&nbsp; </td>');
else if(used>1) w('<td><a href="javascript:getpunc('+j+',1)"><img src="../puncs/l'+punc[j]+'.gif" width="22" height="34" border="0"></a></td>');
j++;
w('</tr>');
}
w('</table>');
endbit();

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

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

makelayer('buttons','visible',10,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="91" height="48" 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="35" height="65" 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="91" height="48" 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="91" height="48" border="0"></a>');
w('</td></tr></table>');
endbit();

makelayer('helpmess','hidden',165,100,300,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 punctuation mark to pick it up.<br><br>To drop the punctuation mark into the text, click on one of these:  <img src="../puncs/empty.gif" width="11" height="17" border="0" align="baseline"><br><br>If you want to remove a punctuation mark from the text, click on it and then click on the 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();


makelayer('message','visible',-500,20,140,100,1000);
endbit();


//**** DRAGON ****

function dragon() {
write_text(1);
all_done=1;
q=0;
str='<img src="../images/dragon.gif" width="145" height="101">';
write_to_layer("dragon");
setTimeout('move_dragon()',40);
}

function move_dragon() {
moveLayer("dragon",770-(q*20),theight-100);
q++;
if(q<11) setTimeout('move_dragon()',40);
else  setTimeout('showLayer("star")',120);
}

function chckcon(){
if(all_done==1) {moveLayer("dragon",-200,0);hideLayer("star");}
}



//**** MESSAGE ****

function message(mess,my) {
str=messagebit1+mess+okbit;
write_to_layer("message");
moveLayer("message",100,my);
}

//**** HELP ****

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


//**** QUIT ****

function quit() {
moveLayer("quitmessage",100,190);
}

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_punc_marks;i++) {j+=placed_flag[i];}
if((j+total_punc_marks)==0) {
moveLayer("checkmessage",100,10);
}
correct=0;
for(i=0;i<total_punc_marks;i++) {
if(placed_word[i]==punc_marks[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) {
placepunc();
carry=0;
}
else if(carry==0){
if(placed_flag[this_gap]>-1) {
return_obj=this_gap;
getpunc(placed_flag[this_gap],2);
}
}
else if(carry==2) do_swap();
}


// *** PLACE PUNC ***

function placepunc() {
if(placed_flag[this_gap]>-1) moved_flag[placed_flag[this_gap]]=0;
hideLayer("carryword");
moveLayer("carryword",-200,-200);
placed_word[this_gap]=punc[this_punc];
placed_flag[this_gap]=this_punc;
moved_flag[this_punc]=1;
check(0);
if(correct==total_punc_marks) 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");
moveLayer("carryword",-200,-200);
check(0);
if(correct==total_punc_marks) dragon();
}


// *** GET PUNC FROM LIST ***

function getpunc(symb,cr) {
carry=cr;
this_punc=symb;

str='<img src="../puncs/l'+punc[symb]+'.gif" width="22" height="34">';

write_to_layer("carryword");
moveLayer("carryword",lx,ly);
showLayer("carryword");
}


// *** CANCEL WORD ***

function discard() {
chckcon();
if(carry==0) {
moveLayer("binmessage",100,40);
return;
}
hideLayer("carryword");
moveLayer("carryword",-200,-200);
moved_flag[this_punc]=0;
placed_word[return_obj]=empty;
placed_flag[return_obj]=-1;
write_text(0);
carry=0;
}


// *** UPDATE TEXT ***

function write_text(checking) {
capmem=0;
checkflag=checking;
str='<p id="textstyle" align="center"><strong>'+title+'</strong></p><p id="textstyle">'+block[0];
for(i=0;i<total_punc_marks;i++) {
if(checking==0) tcolor="";
else {tcolor="r";if(placed_word[i]==punc_marks[i]) tcolor="g";if(placed_word[i]=="empty") tcolor="";}
str+='<a href="javascript:over_gap('+i+')"><img src="../puncs/'+tcolor+placed_word[i]+'.gif" width="11" height="17" border="0" align="absbottom"></a>';
thisbit=block[i+1];

if(capmem==1) {capitalise();capmem=0;}
if((cap==1)&&(placed_word[i]=="dot")) capitalise();

str+=thisbit;
}
write_to_layer("para");
}


// *** CAPITALISE ***

function capitalise() {
capflag=0;
ln=thisbit.length;
if(ln==0) capmem=1;
for(j=0;j<ln;j++) {
letter=thisbit.substring(j,j+1);
if((letter==" ")&&(ln==1)) capmem=1;
if(letter=="<") {
j+=4;
if(j==ln) capmem=1;
else letter=thisbit.substring(j,j+1);
}
if(capflag==0) {
upos=uc.indexOf(letter);
pos=lc.indexOf(letter)+1;
if(upos!=-1) capflag=1;
else if((pos>0)&&(pos<27)) {
capflag=1;
thisbit=thisbit.substring(0,j)+uc.substring(pos-1,pos)+thisbit.substring(j+1,ln);
}
else if(pos>26) capflag=1;
}
}
}


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


function init() {
write_text(0);
}


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


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);}

