/*Copyright © 2006 Cliff Leitch*/
var lett,numm,OKselect,showsol
BrowserBrand=navigator.appName
BrowserBrand=BrowserBrand.toLowerCase()
BrowserVersion=navigator.appVersion
ind=0
j=-1
while(j<BrowserVersion.length&&ind==0){j+=1
ch=BrowserVersion.charAt(j)
if((ch<"0"||ch>"9")&&ch!="."){ind=j}}
verNum=BrowserVersion.substring(0,j)
OKselect=1
if(BrowserBrand.indexOf("netscape")>=0&&verNum<5.0){OKselect=0}
if(BrowserBrand.indexOf("internet explorer")>=0&&verNum<4.0){OKselect=0}
if(BrowserBrand.indexOf("webtv")>=0){OKselect=0}
if(BrowserBrand.indexOf("msntv")>=0){OKselect=0}
var blulet=new Array(26)
var redlet=new Array(26)
lett=new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
for(j=0;j<26;j++){blulet[j]=new Image
blulet[j].src="cb"+lett[j]+".gif"
redlet[j]=new Image
redlet[j].src="cr"+lett[j]+".gif"}
whtsq=new Image
whtsq.src="c__.gif"
numm=new Array("0","1","2","3","4","5","6","7","8","9")
var digit1=new Array(10)
var digit2=new Array(10)
for(j=0;j<10;j++){digit1[j]=new Image
digit1[j].src="ci"+numm[j]+".gif"
digit2[j]=new Image
digit2[j].src="cj"+numm[j]+".gif"}
digit2wht=new Image
digit2wht.src="cj_.gif"
selwht=new Image
selwht.src="cx_.gif"
selacross=new Image
selacross.src="cxa.gif"
seldown=new Image
seldown.src="cxd.gif"
solution=new Image
solution.src="cwsolution.gif"
retpuzz=new Image
retpuzz.src="cwreturn.gif"
var puzscreen=new Array(ncols*nrows)
var selcell
var seldir
selcell=0
seldir=0
for(j=0;j<ncols*nrows;j++){ch=puz.charAt(j)
if(ch=="-"){puzscreen[j]="-"}
else{puzscreen[j]="_"}}
function select(jcell){if(puzscreen[jcell]!="-"&&jcell>=0&&jcell<ncols*nrows&&!showsol){if(seldir&&jcell==selcell){new_seldir=-seldir}else{if(!seldir){new_seldir=1}
new_selcell=jcell}
if(seldir&&jcell!=selcell){imname="ic"+selcell
document[imname].src=selwht.src}
if(new_seldir!=0){imname="ic"+jcell
numcell=0
if(puz.charCodeAt(jcell)>=65&&puz.charCodeAt(jcell)<=90){numcell=1
for(kcell=jcell-1;kcell>=0;kcell--){if(puz.charCodeAt(kcell)>=65&&puz.charCodeAt(kcell)<=90){numcell+=1}}}
if(new_seldir==1){document[imname].src=selacross.src
if(numcell>0&&cla[numcell]){CleanClue(cla[numcell])
document.headers.clue.value=numcell+"A: "+cltmp}}
else{document[imname].src=seldown.src
if(numcell>0&&cld[numcell]){CleanClue(cld[numcell])
document.headers.clue.value=numcell+"D: "+cltmp}}}}
else{if(OKselect==1){document.headers.clue.value=""}
if(seldir){imname="ic"+selcell
document[imname].src=selwht.src}
new_seldir=0
new_selcell=0}
seldir=new_seldir
selcell=new_selcell}
function CleanClue(cxin){cltmp=""
i=-1
lcxin=cxin.length
while(i<lcxin){i++
ch=cxin.substring(i,i+1)
if(ch=="|"){cltmp+=" "}
else{if(ch=="&"){if(cxin.substring(i,i+6)=="&quot;"){cltmp+="\""
i+=5}
if(cxin.substring(i,i+5)=="&amp;"){cltmp+="&"
i+=4}
if(cxin.substring(i,i+4)=="&lt;"){cltmp+="<"
i+=3}
if(cxin.substring(i,i+4)=="&gt;"){cltmp+=">"
i+=3}}
else{cltmp+=ch}}}}
function KeyCapture(e){if(e.keyCode){keycode=e.keyCode}
else{keycode=e.which}
if(seldir!=0||keycode==8){if(keycode==27){select(-1)}
else{if(keycode==38){select(selcell-ncols)}
else{if(keycode==40){select(selcell+ncols)}
else{if(keycode==37){select(selcell-1)}
else{if(keycode==39){select(selcell+1)}
else{if(keycode==8){if(seldir!=0){if(seldir==-1){select(selcell-ncols)}
else{select(selcell-1)}}
if(seldir!=0){imname="id"+selcell
document[imname].src=whtsq.src
puzscreen[selcell]="_"}}
else{if(keycode==32||keycode==46){imname="id"+selcell
document[imname].src=whtsq.src
puzscreen[selcell]="_"
if(keycode==32){if(seldir==-1){select(selcell+ncols)}
else{select(selcell+1)}}
else{if(seldir==-1){select(selcell-ncols)}
else{select(selcell-1)}}}
else{if(keycode>=97&&keycode<=122){keycode-=32}
if(keycode>=65&&keycode<=90){imname="id"+selcell
document[imname].src=blulet[keycode-65].src
puzscreen[selcell]=String.fromCharCode(keycode)
if(seldir==-1){select(selcell+ncols)}
else{if(Math.round((selcell+1)%ncols)==Math.round(selcell%ncols+1)){select(selcell+1)}
else{select(-1)}}}}}}}}}}
e.returnValue=0}}
function clues(code){select(-1)
cldir=code.charAt(code.length-1)
clnum=code.substring(0,code.length-1)
ncells=ncols*nrows
j=-1
inds=0
while(inds<clnum&&j<ncells){j+=1
if(puz.charCodeAt(j)>=65&&puz.charCodeAt(j)<=90){inds+=1}}
j_start=j
letts=""
clThisRow=Math.floor(j/ncols)
clLastRow=clThisRow
while(puzscreen[j]!="-"&&j<ncells&&clThisRow==clLastRow){letts+=puzscreen[j]
if(cldir=="d"){j+=ncols}
else{j+=1
clLastRow=clThisRow
clThisRow=Math.floor(j/ncols)}}
numchars=letts.length
letprompt=""
for(j=0;j<numchars;j++){letprompt+=" "
letprompt+=letts.charAt(j)}
if(cldir=="a"){CleanClue(cla[clnum])
cltext=clnum+" across: "+cltmp+"\n"+numchars+" letters: "+letprompt}
else{CleanClue(cld[clnum])
cltext=clnum+" down: "+cltmp+"\n"+numchars+" letters: "+letprompt}
clword=window.prompt(cltext,"")
if(clword){j=j_start
for(k=0;k<Math.min(numchars,clword.length);k++){ch=clword.charAt(k)
ch=ch.toUpperCase()
puzscreen[j]=ch
imname="id"+j
if(ch.charCodeAt(0)<65||ch.charCodeAt(0)>90){document[imname].src=whtsq.src
puzscreen[j]="_"}
else{document[imname].src=blulet[ch.charCodeAt(0)-65].src}
if(cldir=="a"){j+=1}
else{j+=ncols}}}}
function solut(){if(showsol==1){showsol=0
for(j=0;j<ncols*nrows;j++){ch=puzscreen[j]
if(ch!="-"){imname="id"+j
if(ch=="_"){document[imname].src=whtsq.src}
else{document[imname].src=blulet[ch.charCodeAt(0)-65].src}}}
document["solbutton"].src=solution.src}
else{select(-1)
showsol=1
for(j=0;j<ncols*nrows;j++){ch=puz.charAt(j)
if(ch!="-"){ch=ch.toUpperCase()
if(puzscreen[j]!=ch){imname="id"+j
document[imname].src=redlet[ch.charCodeAt(0)-65].src}}}
document["solbutton"].src=retpuzz.src}}
function TableWrite(LinesToWrite){if(!twStep){twStep=1
jtw=-1
twind=-1}
lncount=0
while(twStep<=nClueLines+5){lncount+=1
if(twStep==1){document.write("<FONT class=\"dirheads\">ACROSS</FONT><BR>\n")}
if(twStep==2){document.write("<FONT class=\"dirheads\"> <BR></FONT>\n")}
if(twStep>=3&&twStep<=nCluea+2){if(twind>-1){ndxa=twind+1
twind=cla[jtw].indexOf("|",ndxa)
leader="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"}
else{jtw+=1
while(!cla[jtw]&&jtw<claMax){jtw+=1}
twind=cla[jtw].indexOf("|")
ndxa=0
if(jtw<10){leader="&nbsp;&nbsp;"+jtw+" "}
else{leader=jtw+" "}}
if(twind>=0){ndxb=twind
twStep-=1}
else{ndxb=cla[jtw].length}
if(ndxa==0||lncount==1){document.write("<A HREF=\"javascript:clues('" + jtw + "a')\">")}
document.write(leader+cla[jtw].substring(ndxa,ndxb)+"<BR>")
if(twind<0||lncount==LinesToWrite){document.write("</A>\n")}}
if(twStep==nCluea+3){document.write("<FONT class=\"dirheads\"> <BR></FONT>\n")}
if(twStep==nCluea+4){document.write("<FONT class=\"dirheads\">DOWN</FONT><BR>\n")
jtw=0}
if(twStep==nCluea+5){document.write("<FONT class=\"dirheads\"> <BR></FONT>\n")}
if(twStep>=nCluea+6){if(twind>-1){ndxa=twind+1
twind=cld[jtw].indexOf("|",ndxa)
leader="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"}
else{jtw+=1
if(jtw>cldMax){break}
while(!cld[jtw]&&jtw<cldMax){jtw+=1}
twind=cld[jtw].indexOf("|")
ndxa=0
if(jtw<10){leader="&nbsp;&nbsp;"+jtw+" "}
else{leader=jtw+" "}}
if(twind>=0){ndxb=twind
twStep-=1}
else{ndxb=cld[jtw].length}
if(ndxa==0||lncount==1){document.write("<A HREF=\"javascript:clues('" + jtw + "d')\">")}
document.write(leader+cld[jtw].substring(ndxa,ndxb)+"<BR>")
if(twind<0||lncount==LinesToWrite){document.write("</A>\n")}}
twStep+=1
if(lncount==LinesToWrite){break}}}
function Loading(){if(OKselect){document.write("<BODY onkeydown=\"KeyCapture(event);\" BACKGROUND=\"../imagefiles/bkgrnd1.jpg\" BGCOLOR=\"#F5E3CF\">\n")
document.write("<CENTER><H2>"+ptitle+"</H2>\n")
document.write("<P><I>Three ways to solve: 1) Click on a square to type across, or click twice to type down; 2) Click on a clue then type; 3) Print this page and solve on paper.</CENTER></I>\n")}
else{document.write("<BODY BACKGROUND=\"imagefiles/bkgrnd1.jpg\" BGCOLOR=\"#F5E3CF\">\n")
document.write("<CENTER><H2>"+ptitle+"</H2>\n")
document.write("<BR><I>Two ways to solve: 1) Click on a clue then type; 2) Print this page and solve on paper.</CENTER></I>\n")}}
function showhelp(fname){Hgt=160
Wth=320
if(screen){Hgt=screen.height/4
Wth=Math.max(screen.width/2,400)
LeftPos=(screen.width-Wth)/2}
HelpWindow=window.open(fname,'popwin2','resizable=yes,scrollbars=yes,width='+Wth+',height='+Hgt+',left='+LeftPos+',top=0')
HelpWindow.focus()}
function drawpuzz(){twStep=0
if(OKselect){helpfile="crosswordhelp3.htm"}
else{helpfile="crosswordhelp2.htm"}
document.write("<FORM NAME=\"headers\">\n")
document.write("<BR><CENTER><A HREF=\"javascript:showhelp('" + helpfile + "')\" ><IMG SRC=\"cwhelp.gif\" WIDTH=\"56\" HEIGHT=\"32\" ALIGN=\"top\" BORDER=\"0\" TITLE=\"Technical help for crossword puzzles\" ALT=\"Technical help for crossword puzzles\" NAME=\"cwhelpl\"></A>\n")
document.write(" <A HREF=\"javascript:solut()\" ><IMG SRC=\"cwsolution.gif\" WIDTH=\"66\" HEIGHT=\"32\" ALIGN=\"top\" BORDER=\"0\" TITLE=\"Switch between puzzle and solution\" ALT=\"Switch between puzzle and solution\" NAME=\"solbutton\"></A>\n")
if(OKselect){document.write(" <TEXTAREA ROWS=\"1\" COLS=\"50\" NAME=\"clue\"></TEXTAREA>\n")}
document.write("</FORM>\n")
num=0
ncells=ncols*nrows
numword=0
twidth=ncols*28+1
theight=nrows*28+1
nClueLines=5
nCluea=0
for(j=1;j<=claMax;j++){if(cla[j]){nClueLines+=1
nCluea+=1
loc=0
while(loc>=0){loc=cla[j].indexOf("|",loc+1)
if(loc>=0){nClueLines+=1}}}}
nClued=0
for(j=1;j<=cldMax;j++){if(cld[j]){nClueLines+=1
nClued+=1
loc=0
while(loc>=0){loc=cld[j].indexOf("|",loc+1)
if(loc>=0){nClueLines+=1}}}}
PixLine=20
nCluePix=nClueLines*PixLine
colspan=Math.ceil(twidth/150)
cluecols=colspan
if(screen){ScrWth=screen.width
if(ScrWth>=800){cluecols=4}
if(ScrWth>=1024){cluecols=5}}
for(cluecols=cluecols;cluecols>=colspan;cluecols--){ColH2=Math.floor((nCluePix-(theight+PixLine)*(cluecols-colspan))/cluecols)
if(ColH2>96){break}}
if(cluecols==colspan){colspan=3
cluecols=3}
ClLinesShort=Math.round(ColH2/PixLine)
if(cluecols>colspan){ClLinesLong=Math.ceil((nClueLines-colspan*ClLinesShort)/(cluecols-colspan))}
document.write("<TABLE BORDER=\"0\" CELLPADDING=\"6\" CELLSPACING=\"0\">\n")
document.write("<TR>\n")
if(cluecols>colspan){for(j=0;j<(cluecols-colspan);j++){document.write("<TD WIDTH=\"138\" VALIGN=\"TOP\" ROWSPAN=\"2\">\n")
TableWrite(ClLinesLong)
document.write("</TD>\n")}}
xheight=theight+19
document.write("<TD HEIGHT=\""+xheight+"\" VALIGN=\"TOP\" COLSPAN=\""+colspan+"\">\n")
document.write("<CENTER>")
document.write("<TABLE BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" WIDTH=\""+twidth+"\" HEIGHT=\""+theight+"\">\n")
for(row=0;row<nrows;row++){document.write("<TR>\n")
for(col=0;col<ncols;col++){jcell=row*ncols+col
if(puz.charCodeAt(jcell)>=65&&puz.charCodeAt(jcell)<=90){numword+=1
ch=numword.toString()
image1="ci"+ch.charAt(0)+".gif"
image2="cj_.gif"
image3="cx_.gif"
wrtlink=1
if(ch.charAt(1)){image2="cj"+ch.charAt(1)+".gif"}}
else{if(puz.charAt(jcell)=="-"){wrtlink=0
if(bbgnd){image1="ci-.gif"
image2="ci-.gif"
image3="cx-.gif"}
else{kcell=(row-1)*ncols+col
lcell=row*ncols+(col-1)
if(row>0&&puz.charAt(kcell)!="-"){image2="cic.gif"
image3="cxc.gif"
if(col>0&&puz.charAt(lcell)!="-"){image1="cid.gif"}
else{image1="cic.gif"}}
else{image2="cia.gif"
image3="cxt.gif"
if(col>0&&puz.charAt(lcell)!="-"){image1="cib.gif"}
else{image1="cia.gif"}}}}
else{image1="ci0.gif"
image2="cj_.gif"
image3="cx_.gif"
wrtlink=1}}
document.write("<TD WIDTH=\"28\" HEIGHT=\"10\">")
if(wrtlink&&OKselect){document.write("<A HREF=\"javascript:select("+jcell+")\"><IMG SRC=\""+image1+"\" WIDTH=\"9\" HEIGHT=\"10\" ALIGN=\"BOTTOM\" BORDER=\"0\" NAME=\"ia"+jcell+"\"></A>")
document.write("<A HREF=\"javascript:select("+jcell+")\"><IMG SRC=\""+image2+"\" WIDTH=\"9\" HEIGHT=\"10\" ALIGN=\"BOTTOM\" BORDER=\"0\" NAME=\"ib"+jcell+"\"></A>")
document.write("<A HREF=\"javascript:select("+jcell+")\"><IMG SRC=\""+image3+"\" WIDTH=\"10\" HEIGHT=\"10\" ALIGN=\"BOTTOM\" BORDER=\"0\" NAME=\"ic"+jcell+"\"></A>")}
else{document.write("<IMG SRC=\""+image1+"\" WIDTH=\"9\" HEIGHT=\"10\" ALIGN=\"BOTTOM\" BORDER=\"0\">")
document.write("<IMG SRC=\""+image2+"\" WIDTH=\"9\" HEIGHT=\"10\" ALIGN=\"BOTTOM\" BORDER=\"0\">")
document.write("<IMG SRC=\""+image3+"\" WIDTH=\"10\" HEIGHT=\"10\" ALIGN=\"BOTTOM\" BORDER=\"0\">")}
document.write("</TD>\n")}
document.write("<TD WIDTH=\"1\" HEIGHT=\"10\">")
lcell=row*ncols+(col-1)
if(bbgnd||(lcell>=0&&puz.charAt(lcell)!="-")){image1="cx10.gif"}
else{image1="ct10.gif"}
document.write("<IMG SRC=\""+image1+"\" WIDTH=\"1\" HEIGHT=\"10\" ALIGN=\"BOTTOM\" BORDER=\"0\">")
document.write("</TD>\n")
document.write("</TR>\n")
document.write("<TR>\n")
for(col=0;col<nrows;col++){jcell=row*ncols+col
if(puz.charAt(jcell)=="-"){wrtlink=0
if(bbgnd){image1="c--.gif"}
else{lcell=row*ncols+(col-1)
if(col>0&&puz.charAt(lcell)!="-"){image1="c-b.gif"}
else{image1="c-a.gif"}}}
else{image1="c__.gif"
wrtlink=1}
document.write("<TD WIDTH=\"28\" HEIGHT=\"18\">")
if(wrtlink&&OKselect){document.write("<A HREF=\"javascript:select("+jcell+")\"><IMG SRC=\""+image1+"\" WIDTH=\"28\" HEIGHT=\"18\" ALIGN=\"BOTTOM\" BORDER=\"0\" NAME=\"id"+jcell+"\"></A>")}
else{document.write("<IMG SRC=\""+image1+"\" WIDTH=\"28\" HEIGHT=\"18\" ALIGN=\"BOTTOM\" BORDER=\"0\" NAME=\"id"+jcell+"\">")}
document.write("</TD>\n")}
document.write("<TD WIDTH=\"1\" HEIGHT=\"18\">")
lcell=row*ncols+(col-1)
if(bbgnd||(lcell>=0&&puz.charAt(lcell)!="-")){image1="cx18.gif"}
else{image1="ct18.gif"}
document.write("<IMG SRC=\""+image1+"\" WIDTH=\"1\" HEIGHT=\"18\" ALIGN=\"BOTTOM\" BORDER=\"0\">")
document.write("</TD>\n")
document.write("</TR>\n")}
document.write("<TR>\n")
for(col=0;col<nrows;col++){document.write("<TD WIDTH=\"28\" HEIGHT=\"1\">")
kcell=(row-1)*ncols+col
if(bbgnd||(kcell>=0&&puz.charAt(kcell)!="-")){image1="cx28.gif"}
else{image1="ct28.gif"}
document.write("<IMG SRC=\""+image1+"\" WIDTH=\"28\" HEIGHT=\"1\" ALIGN=\"BOTTOM\" BORDER=\"0\">")
document.write("</TD>\n")}
document.write("<TD WIDTH=\"1\" HEIGHT=\"1\">")
if(bbgnd){image1="cx1.gif"}
else{image1="ct1.gif"}
document.write("<IMG SRC=\""+image1+"\" WIDTH=\"1\" HEIGHT=\"1\" ALIGN=\"BOTTOM\" BORDER=\"0\">")
document.write("</TD>\n")
document.write("</TR>\n")
document.write("</TABLE></CENTER>\n")
xwth=138
if(cluecols==colspan){xwth=Math.round(100/cluecols)+"%"}
document.write("</TD>\n<TR>\n")
for(j=0;j<colspan;j++){document.write("<TD WIDTH=\""+xwth+"\" VALIGN=\"TOP\">\n")
if(j==2){TableWrite(-1)}
else{TableWrite(ClLinesShort)}
document.write("</TD>\n")}
document.write("</TR>\n")
document.write("</TABLE>\n")}
