<!--


function layoutNewPuzzle(cwPuzzle)
{
	var pzl = cwPuzzle.length;
	
	var ctrWid = Math.ceil( 37.2 * cwPuzzle.length) - widthCorrection;
	containerWidth = ctrWid + "px";
	var containerDiv = document.getElementById("crossword_container");
	containerDiv.style.width=containerWidth;
	for (var row=0; row<cwPuzzle.length; row++)
	{
		var rowDiv = document.createElement("div");
		rowDiv.style.height=defaultSqSide;
		rowDiv.style.width=containerWidth;
		containerDiv.appendChild(rowDiv);
	
		var cwRow = cwPuzzle[row].cwrow;
		for (var col=0; col<cwRow.length; col++)
		{
			var cwSq = cwRow[col];
			var sqDiv = document.createElement("div");
			var sqId = "cws_"+row+"_"+col;
			sqDiv.id = sqId;
			
			// type = 0;    // default 0 = clear: 1 = black; 2 =  white with data
			sqDiv.style.border="1px solid black";
			
			var sqType = cwSq.type;
			if (sqType == 2){
				numLetterSquares++;
				sqDiv.className="cws_white";
				var cwSqNum = cwSq.num;				
				sqDiv.onclick = highlightSquare;
				cwSq.userEntry="";
					
				var sqText="";
				if (cwSqNum>0){
					sqText = "<span><font size=2>" + cwSqNum + "</font>";
				} else {
					sqText = "<span><font size=3><sup><sup>" + "&nbsp;" + "</sup></sup></font>";
				}	
				sqDiv.innerHTML=sqText;
										
			} else if (sqType == 1) {
				sqDiv.className="cws_black";
			} else { // sqType == 0
				sqDiv.className="cws_clear";
			}			
			rowDiv.appendChild(sqDiv);	
		}		
	}
}

function showClues()
{
	var acrossText = "   <b>Across</b><br>";
	for (var i=0; i<acrossClues.length; i++)
	{
		var clue=acrossClues[i];
		var clueNum = clue.squareNum;
		var clueText = clue.clue;
		acrossText += clueNum + ". " + clueText + "<br>";
	}
	var right_cluesDiv = document.getElementById("right_clues");
	right_cluesDiv.style.height=containerWidth;
	right_cluesDiv.style.overflow="scroll";
	right_cluesDiv.innerHTML = acrossText;
	
	var mid = Math.floor(downClues.length/2);
	var downTextLeft = "   <b>Down</b><br>";
	for (var j=0; j<mid; j++){
		var clue=downClues[j];
		var clueNum = clue.squareNum;
		var clueText = clue.clue;
		downTextLeft += clueNum + ". " + clueText + "<br>";
	}

	var bottom_clues_leftDiv = document.getElementById("bottom_clues_left");
	bottom_clues_leftDiv.innerHTML = downTextLeft;		
	var downTextRight = "";
	for (var j=mid; j<downClues.length; j++){
		var clue=downClues[j];
		var clueNum = clue.squareNum;
		var clueText = clue.clue;
		downTextRight += clueNum + ". " + clueText + "<br>";
	}
	var bottom_clues_rightDiv = document.getElementById("bottom_clues_right");
	bottom_clues_rightDiv.innerHTML = downTextRight;
}


function highlightSquare(e,inputObj)
{
     // game is already done; do nothing & return:
  if (gameFinished){
    alert("Please press Start to begin.");
    return;
  }
  if (!inputObj){
    inputObj = this;
  }
  switchHighlight(inputObj);
}

function switchHighlight(inputObj)
{
    // was there a previous highlighted cell?
  if (highlightedCell && highlightedCell!=inputObj){
    highlightedCell.style.backgroundColor='white';
  }
    // if we are here, the clicked square is ok for focus;
  highlightedCell = inputObj;
  highlightedCell.style.backgroundColor='yellow';

  if(document.all)inputObj.focus();
}

function insertLetter(e)
{
//alert("insertLetter: highlightedCell="+highlightedCell);
  if (document.all){ e = event; }
  e.preventDefault();
  e.stopPropagation();
  if (!highlightedCell){ return; }
  if (gameFinished){ return; }
  if (e.keyCode){ code = e.keyCode; } else if (e.which) { code = e.which; }
  
  var numbers = highlightedCell.id.split('_');
  var row = numbers[1]/1;
  var col = numbers[2]/1;
  var ssqrow= cwPuzzle[row].cwrow;
  var puzSq=ssqrow[col];
  var cw_num = puzSq.num;

  var nextObject = false;
  if (e.metaKey || e.ctrlKey || e.altKey){
        return true;
  } else
  if(code==39 || code==9) // Right arrow or tab
  {
    nextObject = document.getElementById('cws_' + row + '_' + ((col/1)+1));
    while(row<cwPuzzle.length)
    {
	    if (nextObject==null){
	        row=row+1;
	        col=0;
	    	nextObject = document.getElementById('cws_' + row + '_' + 0);
	    }
	    while((nextObject != null) && (nextObject.className != "cws_white"))
	    {
	        col = col+1;
	        nextObject = document.getElementById('cws_' +  row + '_' + col);
	    }
	    if (nextObject!=null){
	    	nextObject.focus();
	    	break; }
	}
  }
  if(code==37){ // Left arrow
    nextObject = document.getElementById('cws_' + row + '_' + (col/1-1));
      while((nextObject != null) && (nextObject.className != "cws_white"))
      {
        col = col-1;
        nextObject = document.getElementById('cws_' + row + '_' + col);
        nextObject.focus();
      }
  }
  if(code==38){ // Up arrow
    nextObject = document.getElementById('cws_' + (row-1) + '_' + col);
      while((nextObject != null) && (nextObject.className != "cws_white"))
      {
        row = row-1;
        nextObject = document.getElementById('cws_' + row + '_' + col);
        nextObject.focus();
      }
  }
    if(code==40){ // Down arrow
    nextObject = document.getElementById('cws_' + (row+1) + '_' + col);
      while((nextObject != null) && (nextObject.className != "cws_white"))
      {
        row = row+1;
        nextObject = document.getElementById('cws_' + row + '_' + col);
        nextObject.focus();
      }
  }
  if(nextObject != null && nextObject != false) {
     switchHighlight(nextObject);
     nextObject.focus();
  }
 
    // Delete
  if(code==46 || code==8)
  { 
    if (cw_num == null || cw_num == '') {
      cw_content_span.innerHTML = '';
    } else {
      cw_content_span.innerHTML =
          '<font size=4><sup><sup>' + cw_num + '</sup></sup></font><font size='+textFontSize+'>&nbsp;</font>';
    }
     puzSq.userEntry = '';
  }
  if (97 <= code && code <= 122){ code -= 32; }
  if (65 <= code && code <= 90)
  {
    var theChar = String.fromCharCode(code);
    var resultInnerHTML;
    if (cw_num == null || cw_num == '') {
      resultInnerHTML = '<font size='+textFontSize+'>' + theChar + '</font>';
    } else {
      resultInnerHTML =
          '<font size=4><sup><sup>' + cw_num + '</sup></sup></font><font size='+textFontSize+'>' + theChar + '</font>';
    }
	highlightedCell.innerHTML = resultInnerHTML;
	puzSq.userEntry = theChar;
  }
 
       // sets gameFinished
  curscore = checkGameFinished();
  if (gameFinished)
  {
    stop_play();
  }
  numcorrectdiv.innerHTML = curscore;

    // suppress attempting to enter the backspace in the square:
  if (code==8 || code==9){ return false; }
    // suppress attempting to enter up arrow or down arrow:
  if (code==38 || code==40){ return false; }
  if (code==39 || code==37){ return false; }
}

function checkGameFinished()
{
  testGameFinished = true;
  curscore = 0;
  
  for (var r=0; r<cwPuzzle.length; r++)
  {
  	var puzRow = cwPuzzle[r].cwrow;
		for (var c=0; c<puzRow.length; c++)
		{
			var puzSq = puzRow[c];
			if (puzSq.type == 2)
			{
				var uEnt = puzSq.userEntry;	
				if (uEnt !=""){
					var entry = puzSq.solutionEntry;
					if (uEnt == entry){
						curscore++;
					} else {
						testGameFinished = false;
					}
				} else {
					testGameFinished = false;
				}
			}
  		}
  }
  gameFinished = testGameFinished;
  return curscore;
}

function showFinalStatus()
{
	for (var r=0; r<cwPuzzle.length; r++)
	{
		var puzRow = cwPuzzle[r].cwrow;
		for (var c=0; c<puzRow.length; c++)
		{
			var puzSq = puzRow[c];
			if (puzSq.type == 2)
			{
				var id="cws_"+r+"_"+c;
				var tgtDiv = document.getElementById(id);
				var cwSqNum = puzSq.num;
									
				var uEnt = puzSq.userEntry;	
				if (uEnt == "" || uEnt == null || uEnt == '' || uEnt == '&nbsp;')
      			{
      				tgtDiv.style.background="LightBlue";
				} else {
					if (uEnt == puzSq.solutionEntry){
						tgtDiv.style.background="LightGreen";
					} else {
						tgtDiv.style.background="Red";
					}
				}
														
				var sqText="";
				if (cwSqNum>0){
					if (uEnt == ""){
						sqText = "<span><font size=2>" + cwSqNum + "</font>";
					} else {
						sqText = "<span><font size=3><sup><sup>" + cwSqNum + "</sup></sup></font>";
					}
				} else {
					sqText = "<span><font size=3><sup><sup>" + "&nbsp;" + "</sup></sup></font>";
				}	
				var entry = uEnt;
				if (entry == 'I'){
					sqText += "&nbsp;";
					if (cwSqNum == 0){
						sqText += "&nbsp;";
					}
				}
				sqText +=
				  '<span style="vertical-align:baseline; text-align:center;"><font size=5>' + entry + '</font></span>';
				tgtDiv.innerHTML=sqText;
			}
		}
	} 
}

function showSolution()
{
	for (var r=0; r<cwPuzzle.length; r++)
	{
		var puzRow = cwPuzzle[r].cwrow;
		for (var c=0; c<puzRow.length; c++)
		{
			var puzSq = puzRow[c];
			if (puzSq.type == 2)
			{
				var id="cws_"+r+"_"+c;
				var tgtDiv = document.getElementById(id);
				tgtDiv.style.background="white";
				var cwSqNum = puzSq.num;
								
				var sqText="";
				if (cwSqNum>0){
					sqText = "<span><font size=3><sup><sup>" + cwSqNum + "</sup></sup></font>";
				} else {
					sqText = "<span><font size=3><sup><sup>" + "&nbsp;" + "</sup></sup></font>";
				}	
				var entry = puzSq.solutionEntry;
				if (entry == 'I'){
					sqText += "&nbsp;";
					if (cwSqNum == 0){
						sqText += "&nbsp;";
					}
				}
				sqText +=
				  '<span style="vertical-align:baseline; text-align:center;"><font size=5>' + entry + '</font></span>';
				tgtDiv.innerHTML=sqText;
			}
		}
	}
}

function showInstructions()
{
    var ipath= "crossword-instructions";
    var entry_win=window.open(
                     ipath,
                     'Crossword_Instructions',
                     'height=380,width=650');
    return false;
}




// -->