// 57.38.1-66.05.7, JS/GP/CWHead.js for cross-sentence puzzle at mvvm.net/
//-------------------------------------------------------------------------->

finishT=false; showInfoT=false; showCluesT=false; showTrayT=false;
alphabetS="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
wrong_wrdN=0; right_wrdN=0;   // global variables for score
cellDimensioN=48;  //  global variable used for cell width and height
                   //  48px largest for table with 11x11 or 12x12 cells
leftN=0;  // horizontal distance of PuzzleBox from left edge window/frame

AnsA = new Array();  // array of (last) answers given
 for (y=0; y<NumA.length; y++)
 { AnsA[y] =new Array('','','','','','','','','',''); }

document.write('<script src="',PathS,'JS/GP/JM',1000-CSP_N,'.js" type="text/javascript"></script>');

//====================

function quickSniffO()
{
 var agtS = navigator.userAgent.toLowerCase();
 var ieT = (agtS.indexOf("msie")!=-1);
// FirefoxT = (((agtS.indexOf("mozilla")!=-1) && (majorI>4))
//           && (agtS.indexOf("firefox")!=-1));
// FirefoxT special case of Nav4UpT

 if (ieT) { IE4UpT=true; Nav4UpT=false }
  else { IE4UpT=false; Nav4UpT=true }

 Nav5UpT=Nav4UpT;  // temporarily
 if (Nav4UpT)
   { topS = ".top="; lftS = ".left="; docS = "document."; stlS = "";
     if (Nav5UpT)  // for Netscape 6 and 7
      { docS = "document.getElementById('"; stlS = "').style" }
      else { docS = "document."; stlS = "" }  // for Netscape 4
   }
  else
   { topS=".pixelTop="; lftS=".pixelLeft="; docS="document.all.";
     stlS=".style";
   }
}

//====================

function number_of_chrO(T,N1,N2)
{
 var acrosT=T; var x=N1; var y=N2; 
 var leN=1;

 if (acrosT)
   { for (leN=0; leN<NumA[0].length-y; leN++)
     {if (NumA[x][y+leN]==99) break }
   }
  else
   { for (leN=0; leN<NumA.length-x; leN++)
     { if (NumA[x+leN][y]==99) break }
   }
 return leN
}

function getCluesO()
{
 var x=0; var y=0;
 var cellCodeN=1; var leN=1;
 var tempS='<table cellpadding="0" cellspacing="0">';

 Get_GP_CS();
 CluesA_TableS=tempS; CluesD_TableS=tempS;

 for (x=0; x<NumA.length; x++)
 {
  for (y=0; y<NumA[0].length; y++)
  {
   if ((NumA[x][y]!='00') && (NumA[x][y]!='99'))
   {
    if (NumA[x][y].charAt(0)=="0") NumA[x][y]=NumA[x][y].substring(1);
    cellCodeN=NumA[x][y];
    if (AClA[cellCodeN-1]>'')
    {
     tempS ='<tr><td align="left" valign="top"><small><b>'+cellCodeN+
      '</b></small>&nbsp;</td><td align="left">';
     tempS+=AClA[cellCodeN-1]+'&nbsp;<small>(';
     tempS+=number_of_chrO(true,x,y)+')</small></td></tr>'

     CluesA_TableS+=tempS
    }     
    if (DClA[cellCodeN-1]>'')
    {
     tempS ='<tr><td align="left" valign="top"><small><b>'+cellCodeN+
      '</b></small>&nbsp;</td><td align="left">';
     tempS+=DClA[cellCodeN-1]+'&nbsp;<small>(';
     tempS+=number_of_chrO(false,x,y)+')</small></td></tr>'
     CluesD_TableS+=tempS
    }    
   }
  }
 }

 CluesA_TableS+='</table>'; CluesD_TableS+='</table>'
}

//=====================================================

function Adjust_Cell(C,AnswS,inOutS)
{
 var i=inOutS.indexOf(C);
 var beginS=""; var endS="";

 while (i>-1)
 {
  beginS=inOutS.substring(0, i);
  endS  =inOutS.substring(i+1,inOutS.length);
  inOutS=beginS+AnswS+endS;
  i=inOutS.indexOf(C);
 }

 return inOutS;
}

function makePuzzlO()
{
 var tableWidth=NumA[0].length*cellDimensioN;
 var gridS=
  '<table width="'+tableWidth+'" cellpadding="0" cellspacing="0" bgcolor="#00ffff" '+
  'align="center" border="1">';
 var cell0BgrS="#202000"; var cell1BgrS="bgcolor='#a0a080'";
// var cell0BgrS="#000020";
// var cell1BgrS="background='../../Graf/Bkgr/HalfTrsp.gif'";
   // cell of type 0: blank; 1: letter/space only; 2: (first) letter/space and number of word
 var chrColS="#000000"; var chrBkgS='style="background:'+cell1BgrS+'"';
 var cell0S ='<td bgcolor="'+cell0BgrS+'" width="'+cellDimensioN+'" '+
  'height="'+cellDimensioN+'" align="center" valign="middle">'+
  '<font color="'+cell0BgrS+'">&#149;</font></td>';
    // with symbol for printing
// var cell1S ='<td bgcolor="'+cell1BgrS+'" width="'+cellDimensioN+'" '+
 var cell1S ='<td '+cell1BgrS+' width="'+cellDimensioN+'" '+
  'height="'+cellDimensioN+'" align="left" valign="top" id="CellXY">&nbsp;</td>';
// var cell2S ='<td bgcolor="'+cell1BgrS+'" width="'+cellDimensioN+'" '+
 var cell2S ='<td '+cell1BgrS+' width="'+cellDimensioN+'" '+
  'height="'+cellDimensioN+'" align="left" valign="top" id="CellXY">'+
  '<font face="Courier"><small><a '+
  'href="javascript:Enter_Words(!)">!</a></small>';
    // symbol '!' indicates place of number
 var buttonS=
  '<table height="'+cellDimensioN+'" align="center" id="InfoNScore"><tr>'+
   '<td><table bgcolor="#808080" border="1"><tr><td><font size="-1" face="Courier">&nbsp;'+
    '<a href="JavaScript:showInfO()" class="Button">&nbsp;INFO&nbsp;</a>&nbsp;</font>'+
    '</td></tr></table></td>'+
   '<td width="16"> </td>'+
   '<td><table bgcolor="#808080" border="1"><tr><td align="center"><font '+
    'size="-1" face="Courier">&nbsp;'+
    '<a href="JavaScript:showCluesO()" class="Button">&nbsp;CLUES&nbsp;</a>&nbsp;</font>'+
    '</td></tr></table></td>'+
   '<td width="16"> </td>'+
   '<td><table bgcolor="#808080" border="1"><tr><td align="center"><font '+
    'size="-1" face="Courier">&nbsp;'+
    '<a href="JavaScript:switchTrayO()" class="Button">&nbsp;SHOW/HIDE TRAY&nbsp;</a>&nbsp;</font>'+
    '</td></tr></table></td>'+
   '<td width="16"> </td>'+
   '<td><table bgcolor="#808080" border="1"><tr><td><font size="-1" face="Courier">&nbsp;'+
    '<a href="JavaScript:showScorO()" class="Button">&nbsp;SCORE&nbsp;</a>&nbsp;</font>'+
    '</td></tr></table></td>'+
  '</tr></table>';
 var tempS='';

 for (var y=0; y<NumA.length; y++)
 {
  gridS+="<tr>";
  for (var x=0; x<NumA[y].length; x++)
  {
   if (NumA[y][x]==99) gridS+=cell0S;
    else
    {
     if (NumA[y][x]>0)
       { var numberS=NumA[y][x];
         if (numberS.charAt(0)=="0") numberS=numberS.substring(1);
         tempS=Adjust_Cell('!',numberS,cell2S) }
      else tempS=cell1S;
     tempS=Adjust_Cell('X',x,tempS);
     tempS=Adjust_Cell('Y',y,tempS);
     if ((AnsA[y][x]=="") || (AnsA[y][x]=="_")) AnsA[y][x]="&nbsp;";
     gridS+=tempS
    } //else
  } //for
  gridS+="</tr>";
 } //for
 gridS+="</table>";
 puzzlS=gridS+buttonS+'</div>'
}

function showPuzzlO()
{  // only used one time, when starting
 var x_inner=0;
 var letterI=0; var I=0; var C=' '; var HC=' ';
 var leftI=0; var l_QuoteS='"0px"'; var topI=0; var t_QuoteS='"0px"';
 var chrColS="#000000";
 var trayS=
   '<table width="100%" background="../../Graf/Bkgr/HalfTrsp.gif" '+
   'border="2"><tr height="390"><td valign="bottom" align="right">'+

   '<table>'+
   '<tr><td colspan="3" align="center"><font size="+4" color="#ff0000"><b>'+
   '&#27721;&#23383;&#30424;&#23376;</b></font></td></tr>'+
   '<tr height="16"><td></td></tr>'+
   '<tr><td  valign="top" align="center">'+
   '<font color="#000000"><b>HANZI CHARACTER TRAY &nbsp;<br />'+
   'with tiles (still) available</b> &nbsp;</font></td>'+
   '<td width="16"></td>'+
   '<td valign="top" align="right"><font size="-1" color="#000000"><b>Interactive '+
   'Putonghua Cross-Sentence Puzzle&nbsp;<br />'+
   'on MVVM-site (mvvm.net)&nbsp;<br />'+
   '&copy; M. Vincent van Mechelen, 62-64 aSWW &nbsp;</b></font>'+
   '</td></tr></table>'+

   '</td></tr>'+
   '</table>';

// if (Nav4UpT) x_inner=eval("window.innerWidth") -16;
//  else x_inner = eval("window.document.body.clientWidth");
// leftN=Math.round((x_inner-580)/2)  // 580 fixed width of PuzzleBox 
// leftN=20;  // (620-580)/2
 if (PrePageT) leftN=Math.round((displayWidN+2-580)/2);
   // (700+2-580)/2=121/2
// if (BodyObjsT) leftN=Math.round((displayWidN+2-580)/2);  // At TRINPsite

 makePuzzlO();
 window.document.write('<div id="PuzzleBox" style="left:'+leftN+'px">');
 window.document.write(puzzlS+'</div>');

 topI=223+((NumA[0].length+1)*(cellDimensioN+2));
 window.document.write('<div id="LetterTray" style="top:'+topI+'px">');
 window.document.write(trayS+'</div>');

 ChA = new Array();  // 26x10 array with string values for character tiles
 PyA = new Array();  // 26x10 array with corresponding Pinyin strings
   // in addition to a syllable's own first letter the next may be used,
   // except for homonyms, which must be put together in the first-letter row!
   // tone numbers 6-0 are for homonymic syllables and stand for 1-5
 ChA[0] =new Array("&#29233;","&#26114;","","","","","","","",""); // A
 PyA[0] =new Array("ai4","ang2","","","","","","","","");
   // ai4 not in puzzle #1
 ChA[1] =new Array("&#30334;","&#26495;","&#25253;","&#19981;","","","","","",""); // B
 PyA[1] =new Array("bai3","ban3","bao4","bu2","","","","","","");
   // bai3 and ban3 not in puzzle #1
 ChA[2] =new Array("&#38169;","","","","","","","","",""); // C
 PyA[2] =new Array("cuo4","","","","","","","","","");
 ChA[3] =new Array("&#22823;","&#24471;","&#25958;","&#36530;","&#30340;","","","","",""); // D
 PyA[3] =new Array("da4","de0","dun1","duo3","de0","","","","","","");
 ChA[4] =new Array("&#20799;","","","","","","","","",""); // E
 PyA[4] =new Array("er2","","","","","","","","","");
   // er2 not in puzzle #1
 ChA[5] =new Array("&#27861;","&#20961;","&#33539;","","","","","","",""); // F
 PyA[5] =new Array("fa3","fan2","fan4","","","","","","","");
   // fa3 not in puzzle #1
 ChA[6] =new Array("&#35813;","&#39640;","&#21578;","&#36319;","&#20851;","&#22269;","&#36807;","","",""); // G
 PyA[6] =new Array("gai1","gao1","gao4","gen1","guan1","guo2","guo5","","","");
   // gai1 and gao4 not in puzzle #1
 ChA[7] =new Array("&#22909;","&#36203;","&#24456;","&#24680;","&#30011;","&#21270;","","","",""); // H
 PyA[7] =new Array("hao3","he4","hen3","hen4","hua9","hua9","","","","");
   // hen4 and one hua4=hua9 not in puzzle #1
 ChA[8] =new Array("&#24212;","","","","","","","","",""); // I for YI
 PyA[8] =new Array("ying1","","","","","","","","","");
   // jing1 not in puzzle #1
 ChA[9] =new Array("&#25945;","&#25937;","&#23478;","","","","","","",""); // J 
 PyA[9] =new Array("jiao4","jiu4","jia1","","","","","","","");
   // jiao4 not in puzzle #1
 ChA[10]=new Array("&#30475;","","","","","","","","",""); // K
 PyA[10]=new Array("kan4","","","","","","","","","");
 ChA[11]=new Array("&#32769;","&#26519;","&#20262;","","","","","","",""); // L
 PyA[11]=new Array("lao3","lin2","lun2","","","","","","","");
   // lao3 not in puzzle #1
 ChA[12]=new Array("&#27809;","&#26757;","&#38376;","&#20204;","","","","","","",""); // M
 PyA[12]=new Array("mei7","mei7","men2","men5","","","","","","","");
   // men5 not in puzzle #1
 ChA[13]=new Array("&#37027;","&#33021;","&#20320;","&#24180;","","","","","",""); // N
 PyA[13]=new Array("na4","neng2","ni3","nian2","","","","","","","");
 ChA[14]=new Array("&#27431;","","","","","","","","",""); // O
 PyA[14]=new Array("ou1","","","","","","","","","");
   // ou1 not in puzzle #1
 ChA[15]=new Array("&#25340;","","","","","","","","",""); // P
 PyA[15]=new Array("pin1","","","","","","","","","");
 ChA[16]=new Array("&#21435;","","","","","","","","",""); // Q
 PyA[16]=new Array("qu4","","","","","","","","","");
 ChA[17]=new Array("&#28982;","&#35748;","&#20154;","","","","","","","",""); // R
 PyA[17]=new Array("ran2","ren4","ren2","","","","","","","","");
 ChA[18]=new Array("&#26862;","","","","","","","","",""); // S, except SH
 PyA[18]=new Array("sen1","","","","","","","","","");
 ChA[19]=new Array("&#20182;","&#22825;","&#21516;","&#29305;","","","","","",""); // T
 PyA[19]=new Array("ta1","tian1","tong2","te4","","","","","","");
   // ta1 not in puzzle #1
 ChA[20]=new Array("&#21320;","","","","","","","","",""); // U for WU
 PyA[20]=new Array("wu3","","","","","","","","","");
   // wu3 not in puzzle #1
 ChA[21]=new Array("&#35841;","&#35799;","&#39318;","&#26159;","","","","","",""); // V for SH
 PyA[21]=new Array("shei4","shi1","shou3","shi4","","","","","","");
 ChA[22]=new Array("&#25105;","&#25991;","","","","","","","",""); // W, except WU
 PyA[22]=new Array("wo3","wen2","","","","","","","","");
 ChA[23]=new Array("&#19979;","&#24819;","&#20123;","&#26032;","&#24515;","&#22995;","&#24615;","&#23398;","",""); // X
 PyA[23]=new Array("xia4","xiang3","xie1","xin6","xin6","xing9","xing9","xue2","","");
   // xiang3 and 2 x xin1=xin6 and one xing4=xing9 not in puzzle #1
 ChA[24]=new Array("&#22812;","&#26377;","&#20110;","&#35201;","","","","","",""); // Y, except YI
 PyA[24]=new Array("ye4","you3","yu2","yao4","","","","","","");
 ChA[25]=new Array("&#21746;","&#32773;","&#20013;","&#31181;","&#33258;","&#23447;","","","",""); // Z
 PyA[25]=new Array("zhe2","zhe3","zhong1","zhong3","zi4","zong1","","","","");

 UseA = new Array();  // 26x10 array with truth values for character tiles
     // 'true' now means 'no tile in store'; later it also means 'in use'
 UseA[0] =new Array(false,false,true,true,true,true,true,true,true,true); // A
 UseA[1] =new Array(false,false,false,false,true,true,true,true,true,true); // B
 UseA[2] =new Array(false,true,true,true,true,true,true,true,true,true); // C
 UseA[3] =new Array(false,false,false,false,false,false,false,false,false,true); // D
 UseA[4] =new Array(false,true,true,true,true,true,true,true,true,true); // E
 UseA[5] =new Array(false,false,false,true,true,true,true,true,true,true); // F
 UseA[6] =new Array(false,false,false,false,false,false,false,true,true,true); // G
 UseA[7] =new Array(false,false,false,false,false,false,true,true,true,true); // H
 UseA[8] =new Array(false,true,true,true,true,true,true,true,true,true); // I
 UseA[9] =new Array(false,false,false,false,true,true,true,true,true,true); // J 
 UseA[10]=new Array(false,true,true,true,true,true,true,true,true,true); // K
 UseA[11]=new Array(false,false,false,true,true,true,true,true,true,true); // L
 UseA[12]=new Array(false,false,false,false,true,true,true,true,true,true); // M
 UseA[13]=new Array(false,false,false,false,true,true,true,true,true,true); // N
 UseA[14]=new Array(false,true,true,true,true,true,true,true,true,true); // O
 UseA[15]=new Array(false,true,true,true,true,true,true,true,true,true); // P
 UseA[16]=new Array(false,true,true,true,true,true,true,true,true,true); // Q
 UseA[17]=new Array(false,false,false,false,true,true,true,true,true,true); // R
 UseA[18]=new Array(false,false,true,true,true,true,true,true,true,true); // S
 UseA[19]=new Array(false,false,false,false,false,true,true,true,true,true); // T
 UseA[20]=new Array(false,true,true,true,true,true,true,true,true,true); // U
 UseA[21]=new Array(false,false,false,false,false,true,true,true,true,true); // V
 UseA[22]=new Array(false,false,false,true,true,true,true,true,true,true); // W
 UseA[23]=new Array(false,false,false,false,false,false,false,false,true,true); // X
 UseA[24]=new Array(false,false,false,false,true,true,true,true,true,true); // Y
 UseA[25]=new Array(false,false,false,false,false,false,true,true,true,true); // Z

   for (letterI=0; letterI<=25; letterI++)
   {
    C=alphabetS.charAt(letterI);
    for (I=1; I<=UseA[0].length; I++)
    {
     if (UseA[letterI][I-1]==false)
      {
       cellS='<div id="PuzzleBox"><div class="GivenLetter" id="'+C+I+'">'+
       '<table width="32" height="32" bgcolor="#ffff00" border="0" class="Tile"><tr>'+
       '<td align="center" valign="middle">';
       if (Nav4UpT) cellS+='<small>';  // because of resizing effects
//       cellS+='<b><font face="Courier" color="'+chrColS+'">'+C+'</font></b>';

       if (ChA[letterI][I]=="")
        { ChA[letterI][I]=ChA[letterI][I-1];
          PyA[letterI][I]=PyA[letterI][I-1] }
            // 26 tiles for characters (the last ones) used more than once
       HC=ChA[letterI][I-1];
       cellS+='<b><font size="+1" face="Courier" color="'+chrColS+'">'+
        HC+'</font></b>';
       if (Nav4UpT) cellS+='</small>';
       cellS+='</td></tr></table></div></div>';
  // absolute position in PuzzleBox layer, because relative position on
  // LetterTray does not work in IE (even tho working in Firefox)
       window.document.write(cellS);
       leftI=10+(letterI*38); 
       topI=((NumA[0].length+1)*cellDimensioN)+I*38
       moveTileO(C+I,leftI,topI,false)
      } // if
    } // for
   } // for
}

function putTileOnTrayO(S)
{
 var letterI=S.charCodeAt(0)-65; var I=parseInt(S.substring(1));
 var leftI=0; var topI=0;

 leftI=10+(letterI*38); 
 topI=((NumA[0].length+1)*cellDimensioN)+I*38;
 moveTileO(S,leftI,topI,showTrayT);
 UseA[letterI][I-1]=false;
}

function moveTileO(S,I1,I2,T)
{
 var objS=S; var leftI=I1; var topI=I2; var visibleT=T;

 if (Nav5UpT)
   { l_QuoteS='"'+parseInt(leftI)+'px"';  // value with "px" 
     t_QuoteS='"'+parseInt(topI)+'px"';   // between quotes
     eval(docS + objS + stlS + lftS + l_QuoteS);
     eval(docS + objS + stlS + topS + t_QuoteS) }
  else 
   { eval(docS + objS + stlS + lftS + leftI);
     eval(docS + objS + stlS + topS + topI) }
 if (visibleT==true)
   eval(docS + objS + stlS + '.visibility="visible"');
  else
   eval(docS + objS + stlS + '.visibility="hidden"');
}

function locateInputO(N1,N2,T,S)
{
 var x_wordN=N1; var y_wordN=N2; var acrosT=T; var wordS=S;
 var letterI=26; var placeI=-1; var objS='';
 var tray_linI=1;
 var objTopS=".pixelTop";  if (Nav4UpT) objTopS=".top";
 var C_N_S="?0";
 var I=0; var J=1;
 var margiN=Math.round((580-NumA[0].length*cellDimensioN)/2);
   // 580 fixed width of PuzzleBox (usually wider than puzzle)
 var leftI=0;
 var l_QuoteS='"0px"'; var topI=0; var t_QuoteS='"0px"'; 
 var cellHeightN=parseInt(cellDimensioN)+2;
  if (Nav4UpT) cellHeightN=cellHeightN-2;
 var alertS="Alas, there are not enough letters!";
 
 margiN=margiN+parseInt(cellDimensioN)-37;
   // to locate tile (32 wide) to the (bottom) right of cell

 for (I=0; I<wordS.length; I=I+2)
 { leftI=0;
   letterI=wordS.charCodeAt(I)-65;
   objS=wordS.charAt(I);
   tray_linI=wordS.charCodeAt(I+1)-47;

   if (acrosT)
     { if (AnsA[y_wordN][x_wordN+I/2].charAt(0)==objS) leftI=-1 }
    else
     { if (AnsA[y_wordN+I/2][x_wordN].charAt(0)==objS) leftI=-1 }

   if (leftI>-1)
    {
      if ((tray_linI<ChA[0].length) &&
          (ChA[letterI][tray_linI]==ChA[letterI][tray_linI-1]))
        {
         J=UseA[0].length-1;
         while ((J<UseA[0].length) && (J>tray_linI-2))  // J 0-9; tray_linI 1-10 
         {
          if (UseA[letterI][J]==false)
           { UseA[letterI][J]=true; placeI=J+1; J=-1 }
          J-- // note J=-1 becomes J=-2
         } // end while
         if (J>-2) J=-1;
        }
       else
        {
         if (UseA[letterI][tray_linI-1]==false)
          { UseA[letterI][tray_linI-1]=true; placeI=tray_linI; J=-2 }
         else J=-1;
        }

     if (J==-1)
       {
        alertS='Alas, the character for '+PyA[letterI][tray_linI-1]+
               ' is not on the tray anymore! '+
               'If you need it, put it back first (by clicking the '+
               'sentence number and pressing OK without entering anything). '+
               'Then you can use it again.';
        alert(alertS); I=wordS.length }
      else
       {
        objS=wordS.charAt(I)+placeI;
        if (acrosT) C_N_S=AnsA[y_wordN][x_wordN+I/2];
               else C_N_S=AnsA[y_wordN+I/2][x_wordN]; 
        letterI=C_N_S.charCodeAt(0);
        if ((letterI>64) && (letterI<91))
         {
          letterI=letterI-65;
          J=parseInt(C_N_S.substring(1))-1;  // number may be 2 digits!
          UseA[letterI][J]=false;

          if (J<10)  // (J+1)<11
            {
             leftI=10+(letterI*38); 
             topI=((NumA[0].length+1)*cellDimensioN)+J*38+38
            }
           else
            {
             leftI=48+(letterI*38);
             topI=((NumA[0].length+1)*cellDimensioN)+760-(J*38)
            } 
          moveTileO(C_N_S,leftI,topI,showTrayT)
         }  
        if (acrosT)
          {
           AnsA[y_wordN][x_wordN+I/2]=objS;
           leftI=(x_wordN+I/2)*cellDimensioN;

           topI=10+y_wordN*cellHeightN;
          }
         else
          { 
           AnsA[y_wordN+I/2][x_wordN]=objS;
           leftI=x_wordN*cellDimensioN;
           topI=10+(y_wordN+I/2)*cellHeightN;

          }
        moveTileO(objS,leftN+margiN+leftI,topI,true);
       }  // end else
    }  // end if 
    wrong_wrdN=-1;  // temporarily, to make sure that a word has been entered
  } // end for
}

function plainTextO(S)
{  // deletes hypertext codes between square brackets
   // char codes between '&' and ';' will be replaced by one character
 var inS=S; var outS="";
 var brackCodeT=false; var ampCodeI=-1;
 var I=0; var C="?";

 for (I=0; I<inS.length; I++)
 {
  C=inS.charAt(I);
  if (C=="<") brackCodeT=true;
  if (C=="&") ampCodeI=I;
  if ((!brackCodeT) && (ampCodeI==-1)) outS=outS+C;
  if (C==">") brackCodeT=false;
  if ((C==";") && (ampCodeI>-1))
   { C=inS.charAt(ampCodeI+1);
     if (C=="n") C=" ";  // for '&nbsp;'
     outS=outS+C;
     ampCodeI=-1 }
 } // for

 return outS
}

function Enter_Words(N)
{
 var wordN=N;
 var acrosC=''; var acrosT=true;
 var x=0; var y=0; var x2=0; var y2=0; var z=0;
 var x_wordN=-1; var y_wordN=0;
 var msgS="!!"; var inS="??"; var formerS="";
 var alertS="[message]";
 var startS=""; var syllS=""; var sentenS=""; var tempS="";
 var colC="a"; colN=0;

if (finishT)
{
 alertS='After having seen the results you cannot continue! '+
   'To start again you will first have to reload this page.' 
 alert(alertS)
}
else
{

 while (x_wordN<0)
  {
   for (x=0; x<NumA[0].length; x++)  // no 'while' within 'while'!
   { 
    if (NumA[y][x]==wordN) { x_wordN=x; y_wordN=y }
   }
   y++
  }
 if ((AClA[wordN-1]>'') && (DClA[wordN-1]>''))
   {
    msgS="Sentence "+wordN
    if (AnsA[y_wordN][x_wordN+1]!='&nbsp;') 
      { msgS+=" down?"; acrosT=false }
     else msgS+=" across?";
    if (IE4UpT)  // use VBScript
      {
returnI = window.makeMsgBox("Across or down?",msgS,32,4,0,0);
       if (returnI==7) acrosT=!acrosT
      }
     else  // use JavaScript
      {
       if (!window.confirm(msgS)) acrosT=!acrosT
      }
   }
  else
   { if (AClA[wordN-1]>'') acrosT=true; else acrosT=false }

 msgS='ENTER SENTENCE '+wordN;
 if (acrosT)
   msgS+=' across ('+number_of_chrO(true,y_wordN,x_wordN)+
         ' Pinyin syllables with numbers)\n'+
    'CLUE: '+plainTextO(AClA[wordN-1])+'\n';
  else
   msgS+=' down ('+number_of_chrO(false,y_wordN,x_wordN)+
         ' Pinyin syllables with numbers)\n'+
    'CLUE: '+plainTextO(DClA[wordN-1])+'\n';

 while (inS=="??")
 { 
   inS=prompt(msgS,formerS); formerS=inS;

   if (inS!=null)
    {

if (inS=="")
{
 if (acrosT)
   { for (z=x_wordN; z<x_wordN+number_of_chrO(true,y_wordN,x_wordN); z++)
     { syllS=AnsA[y_wordN][z];
       if ((syllS.charAt(0)>="A") && (syllS.charAt(0)<="Z"))
        { putTileOnTrayO(syllS); AnsA[y_wordN][z]="" }
     }
   }
  else
   { for (z=y_wordN; z<y_wordN+number_of_chrO(false,y_wordN,x_wordN); z++)
     { syllS=AnsA[z][x_wordN];
       if ((syllS.charAt(0)>="A") && (syllS.charAt(0)<="Z"))
        { putTileOnTrayO(syllS); AnsA[z][x_wordN]="" }
     }
   }
 inS="??";
}
else
{

     inS=inS.toLowerCase();
     startS=""; syllS=""; sentenS="";
     n_of_syllN=0;

     for (z=0; z<inS.length; z++)
      { y=0;
        if (z<inS.length+1)
          { y=inS.charCodeAt(z);
            if ((y==32) || ((y>43) && (y<47)) || (y==63))
            inS=startS+inS.substring(z+1)
          }
         else z=2*inS.length

        if ((y>48) && (y<54))
         {

    syllS=inS.substring(startS.length,z+1);
    tempS=syllS.substring(0,2);
    if ((tempS!="yi") && (tempS!="sh") && (tempS!="wu"))
      { colC=syllS.charAt(0); colN=syllS.charCodeAt(0)-97 }
     else
      { if (tempS=="yi") { colC="i"; colN=8 }  // 'yi' in 'i'-row
         else 
          { if (tempS=="wu") { colC="u"; colN=20 }  // 'wu' in 'u'-row
             else
              { colC="v"; colN=21;  // 'sh' in 'v'-row
                if (syllS=="shui4") syllS="shei4" }
          }
      }

    for (y=0; y<9; y++)
    { tempS=PyA[colN][y];
      if (tempS==syllS) { sentenS+=colC+y; y=11 }
       else
        { if ((colN<25) && (PyA[colN+1][y]==syllS))
            { sentenS+=alphabetS.charAt(colN+1)+y; y=11 }
           else
            { x=tempS.charCodeAt(tempS.length-1);
              if (x>53) // fake tone numbers 6-9 will be real 1-4
               { x=x-53; tempS=tempS.substring(0,tempS.length-1)+x }
              if (x==48) tempS=tempS.substring(0,tempS.length-1)+"5";
              if (tempS==syllS)
               {
                x=colN+1;
                tempS='There are several characters for '+syllS+' !\n'+
                      'Do you mean the character in column ';
                tempS+=x+' and row ';
                x=y+1;
                tempS+=x+' on the tray?';
                if (window.confirm(tempS))
                 {
                  if (acrosT) { x2=x_wordN+n_of_syllN; y2=y_wordN }
                   else { x2=x_wordN; y2=y_wordN+n_of_syllN }
                  if ((UseA[colN][y]==false) ||           // still available
                     (colC.toUpperCase()+x==AnsA[y2][x2]))  // already there
                      // the tiles in AnsA are called A1-A10 to Z1-Z10
                    { sentenS+=colC+y; y=11 }
                   else alert('This particular character is not on the '+
                              'tray anymore! If you need it, put it back '+
                              'first (by clicking the sentence number and '+
                              'pressing OK only).')
                 } // if 
               } // if
            } // else
        } // else
    }  // for (11 will be 12, 10 will be 11)
   if (y<11) alert('The entry '+syllS+' is not accepted. Please try again.');

else startS=startS+syllS;

          if (y<12) { inS="??"; break }
           else n_of_syllN++
         }
      } // for

     if (inS!="??")
     {
      if (n_of_syllN>number_of_chrO(acrosT,y_wordN,x_wordN))
        { alert('Your entry is too long!');
          inS="??" }
       else
        {
         for (x=0; x<inS.length; x++)
          { y=inS.charCodeAt(x);
            if (!((y==32) || ((y>43) && (y<47)) ||
                  ((y>48) && (y<54)) || ((y>96) && (y<123))))
             { alertS="Please, use only Pinyin with the numbers 1-5 "+
                      "(no tone marks or apostrophes)!";
               alert(alertS); x=inS.length; inS="??" }
          } // for
        } // else
      } // if

} // else

    } // if
 } // while

 sentenS=sentenS.toUpperCase();
 if (inS!=null) locateInputO(x_wordN,y_wordN,acrosT,sentenS);

}  // else (finishT=false)
}

//========================

function onClickO(S)
{
 var NewFileS=S; var newFileS=NewFileS.toLowerCase();
 var pathS=PathS.toLowerCase();
 var windoS="width=622,height=330,resizable=yes,scrollbars=yes,toolbar=yes";
 var alertS="This link does not work in the present internal frame or "+
  "pop-up window. Load the puzzle into the (original) parent window, if you "+
  "would like to make use of this link.\n\n"+
  "Deze snelverbinding werkt niet in het huidige interne deelraam of "+
  "bijraam. Laad de puzzel in het (oorspronkelijke) oudervenster, indien "+
  "je van deze verbinding gebruik wilt maken.";

 if (iFrameT)
  {
   if (pathS.indexOf('///')>-1)
    pathS='file://c:'+pathS.substring(10);  // off-line '///C|'='//C:'
   if (newFileS.indexOf('file:///c')>-1)
    newFileS='file://c:'+newFileS.substring(10);  // off-line '///C|'='//C:'
   if ((newFileS.indexOf(pathS)>-1) || (newFileS.indexOf('../')>-1)
       || (newFileS.indexOf('file://')>-1))
     { window.MVVM_IRight1.location.href=NewFileS
       eval(docS+'CluesBox'+stlS+'.visibility="hidden"');
       eval(docS+'InfoBox'+stlS+'.visibility="hidden"');
       showCluesT=false; showInfoT=false } 
    else
     { NewFileS+='#iFrame';
       if (iFramePopUpT && iFramePopUp.closed) iFramePopUpT=false;
       if (iFramePopUpT)
         { iFramePopUp.location.replace(NewFileS); iFramePopUp.focus() }
        else 
         { createNewVu("iFramePopUp",NewFileS,"TITLE",windoS);
           iFramePopUpT=true }
     }
  }
  else alert(alertS)
}

function redefinedLinksO(S)  // pages linked to will be shown in iframe
{                   // or pop-up window instead of parent/opener window
 var boxS=S;
 var I=boxS.indexOf(' href="') // only lower case href="..."
 var J=0;
 var fileS="name.html";

 while (I>-1)
 {
  J=boxS.substring(I+7).indexOf('"');
  fileS="'"+boxS.substring(I+7,I+J+7)+"'";
  boxS=boxS.substring(0,I+1)+
   'HREF="JavaScript:onClickO('+fileS+')"'+
   boxS.substring(I+J+8);
  I=boxS.indexOf(' href="')  // now upper case HREF="..." discarded
 }

 return boxS
}

function makeInfoBoxO()
{
 var infoS=
  '<table height="100%" cellpadding="0" cellspacing="0">'+
   '<tr><td valign="top" align="left">'+
    '<b class="Theme">'+themeS+'</b>'+
    '<hr /><br />'+
    '<span class="Info">'+
    '<b>G E N E R A L &nbsp; I N F O R M A T I O N<br />for all '+
    '<a href="../Pzzl.htm">MVVM word and sentence puzzles</a></b>'+
    '<br/><br />'+
    'You can enter any number of words/sentences in any order. '+
    '<nobr>To&nbsp;(re)enter</nobr> (the beginning of) a particular '+
    'word/sentence click its number.<br />'+
    'The letters/characters chosen must still be available from the '+
    'letter/character tray, which may or may not be made visible at the '+
    'bottom of this page. '+
    '(Three of the left buttons are ON/OFF buttons for visible/invisible.) '+
    'There may be more (different) letters/characters on the tray than '+
    'needed for the puzzle.<br />'+
    'You can see your final score and find out which '+
    '<nobr>word(s)/</nobr>character(s) are correct at any moment. '+
    'Cells with no or wrong letters/characters are colored red, '+
    'the other ones green.<br />'+
    '<br />'+
    '<b>S P E C I A L &nbsp; I N F O R M A T I O N<br />for '+
    'sentence puzzles in Zhezhong Yuyan</b>'+
    '<br/><br />'+
    'You must enter the answers as Pinyin with tone numbers: '+
    'e.g., Wo3men5 hui4 xie3 Zhong1wen2.<br />'+
    'You may omit punctuation and spaces, and use small letters only.<br />'+
    'By entering nothing (that is, by pressing OK or RETURN only) the '+
    'previous entry for the sentence concerned, if any, will be removed. '+
    'The tiles used for that sentence will then be put back on the tray, '+
    'so that they become available again.<br />'+
    'Clues may simply require a literal translation or may be cryptic. '+
    'If a clue is (too) cryptic and you are on the main puzzle page '+
    'itself or in this internal frame (not in a prompt), you may put your '+
    'cursor on it, after which a sentence will be shown which only needs '+
    'a literal translation.'+
    '</span></td></tr>'+
    '<tr><td valign="bottom" align="right" class="Info"><br />'+
    '&copy; M. Vincent van Mechelen, 60-62 aSWW&nbsp;'+
   '</td></tr>'+
  '</table>';

 document.write('<div id="InfoBox">');
 document.write(redefinedLinksO(infoS));
 document.write('</div>');
}

function makeWarningBoxO()
{
 var warningS=
  '<span class="Info">'+
  '<b>WARNING</b>:<br />'+
  'by following a link on the left of this frame or in one of '+
  'the two top floating menus you will lose the puzzle and everything you '+
  'may already have filled in.<br />'+
  'However, apart from the links in the top right (TRINPsite) menu, you '+
  'can safely click on any link in or over this internal frame without '+
  'abandoning the puzzle.<br />'+
  '(This message will be deleted after clicking INFO or CLUES.)'+
  "</span>";

 document.write('<div align="left" id="WarningBox">');
 document.write(warningS);
 document.write('</div>');
}

function makeCluesBoxO()
{
 var boxS=
  '<b class="Theme">'+themeS+'</b>'+
  '<hr />'+
  '<span class="Info">'+
  '<b>A C R O S S</b><br />'+
  CluesA_TableS+'<br />'+
  '<b>D O W N</b><br />'+
  CluesD_TableS+
  '</span>';

 document.write('<div align="left" id="CluesBox">');
 document.write(redefinedLinksO(boxS));
 document.write('</div>');
}

function showIFramesColBoxesO()
{
 if (iFrameT && PostPageT)
  { makeWarningBoxO(); moveToIFramesColLeftO("WarningBox");
    makeCluesBoxO(); moveToIFramesColLeftO("CluesBox");
    makeInfoBoxO(); moveToIFramesColLeftO("InfoBox"); }
}

//===========================================================

function showInfO()
{
 if (showInfoT)
   { eval(docS+'InfoBox'+stlS+'.visibility="hidden"');
     showInfoT=false }
  else
   { eval(docS+'InfoBox'+stlS+'.visibility="visible"');
     if (iFrameT && PostPageT)
      eval(docS+'WarningBox'+stlS+'.visibility="hidden"');
     showInfoT=true }
}

function showCluesO()
{
 if ((showCluesT) && (!showInfoT))  // clues not hidden under info
   { eval(docS+'CluesBox'+stlS+'.visibility="hidden"');
     showCluesT=false }
  else
   { eval(docS+'CluesBox'+stlS+'.visibility="visible"');
     if (iFrameT && PostPageT)
      eval(docS+'WarningBox'+stlS+'.visibility="hidden"');
     eval(docS+'InfoBox'+stlS+'.visibility="hidden"');
     showCluesT=true; showInfoT=false }
}

function switchTrayO()
{
 var tableHeight=(NumA[0].length+1)*cellDimensioN;  // includes buttons

 showTrayT=!showTrayT
 if (showTrayT)
   eval(docS + "LetterTray" + stlS + '.visibility="visible"');
  else
   {
    eval(docS + "LetterTray" + stlS + '.visibility="hidden"');
    if (Nav4UpT)
     eval(docS+"PuzzleBox"+stlS+'.height="'+tableHeight+'px"');
       // PuzzleBox reduced because otherwise links not accessible
   }

 for (var letterI=0; letterI<=25; letterI++)
 {
  C=alphabetS.charAt(letterI);
  for (var I=1; I<=UseA[0].length; I++)
  {
   if (UseA[letterI][I-1]==false)
    {
     objS=C+I;
     if (showTrayT)
       eval(docS + objS + stlS + '.visibility="visible"');
      else
       eval(docS + objS + stlS + '.visibility="hidden"');
    } // if
  } // for
 } // for
}

function showScorO()
{
 var x=0; var x2=0; var y=0; var y2=0;
 var syllAnS="";
 var wrongChrT=true;
 var scoreColS="#e02020";
 var wordS="character of";

 if (finishT) wrong_wrdN=-1;  // to see score again

 if (wrong_wrdN==-1)
  {
   wrong_wrdN=0;  // initial value given back
   finishT=true;

   for (y=0; y<NumA.length; y++)
   {
    for (x=0; x<NumA[y].length; x++)
    {
     syllAnS=AnsA[y][x];
     wrongChrT=true;
     scoreColS="#e02020"

     if (syllAnS!="")
     {
      if (syllAnS!="&nbsp;") 
       {
        y2=syllAnS.charCodeAt(0)-65;
        x2=parseInt(syllAnS.substring(1))-1;
        if (KeyA[y][x]==PyA[y2][x2]) wrongChrT=false;
         else
          { if ((KeyA[y][x].charAt(0)=="&") &&  // charCode instead of Pinyin
                (KeyA[y][x].substring(0,8)==ChA[y2][x2])) wrongChrT=false
          } 
       }
      if (wrongChrT) wrong_wrdN++;
       else { right_wrdN++; scoreColS="#c0e0c0" } 
      eval(docS + 'Cell'+x+y + stlS + '.background="'+scoreColS+'"')
     }
    } //for
   } //for

   x=right_wrdN+wrong_wrdN;  // total no. of characters=syllables
   y=Math.round((100*right_wrdN)/x)
    // % of words right
   if (right_wrdN>1) wordS="characters out of";
   alert("Score: "+right_wrdN+" "+wordS+" "+x+"  ("+y+"% correct)")
   wrong_wrdN=0; right_wrdN=0;  // in case score is revisited
  }
  else alert('You have not entered any word yet.')
}

// (C) 57-66 ASWW, M. Vincent van Mechelen
