    var AcrossClues;
    var DownClues;
    var mAcross;
    var mDown;
    var cwMoves;
    var cwCluePos;
    var iCurrX;
    var iCurrY;
    var iCurrAxis;
    var sHLBarCol;
    var sHLSqCol;
    var sNCol;
    var AcrossClueHL;
    var DownClueHL;
    var ClueHLCol;
    var sWrongMoveCol;
    var curOpac;
    var IE = document.all;
    var oPrizeInverval;
    var sCongratsMsg;
    var TotalSize;
    var cwGrid;
    var cwID;

    sHLBarCol='#d9ff93';
    sHLSqCol='#93cc85';
    sNCol='white';
    ClueHLCol='#d9ff93';
    sWrongMoveCol='#ff7f7f';
    
    iCurrAxis=1;

  
 function XBrowserAddHandler(target,eventName,handlerName) { 
  if ( target.addEventListener ) { 
    target.addEventListener(eventName, function(e){target[handlerName](e);}, false);
  } else if ( target.attachEvent ) { 
    target.attachEvent("on" + eventName, function(e){target[handlerName](e);});
  } else { 
    var originalHandler = target["on" + eventName]; 
    if ( originalHandler ) { 
      target["on" + eventName] = function(e){originalHandler(e);target[handlerName](e);}; 
    } else { 
      target["on" + eventName] = target[handlerName]; 
    } 
  } 
}

function MouseMoveXY(e)
{
iCurrX =parseInt(this.id.split('x')[0].substring(1));
iCurrY = parseInt(this.id.split('x')[1]);
iCurrAxis=iCurrAxis*-1;
highlightBar(true);
this.style.backgroundColor=sHLSqCol;
}

function KeyCodeGet(e)
{
var keycode;
if (e) keycode=e.which;
     else keycode=window.event.keyCode;
   return keycode;
}

function IsKeyOK(sChar)
{
var sOKChars='ABCDEFGHIJKLMNOPQRSTUVWXYZ ';
if (sOKChars.indexOf(sChar)==-1)
	return false;
	else return true;
}

function KeyboardPress(e)
{
var iNewNum;
var sChar;
var keycode;

keycode=KeyCodeGet(e);
if (keycode==8 || (keycode>36 && keycode<41))
    { ArrowKeyPressed(keycode); return false; }
    
sChar=String.fromCharCode(keycode)
sChar=sChar.toUpperCase();

if (IsKeyOK(sChar)==false)
	return false;

if (sChar==' ')
    sChar='&nbsp;';
    
if (iCurrX==undefined) return true;

PlaceChar(sChar);
}

function PlaceChar(sChar)
{
document.getElementById('M' + iCurrX + 'x' + iCurrY).innerHTML=sChar;
document.getElementById('T' + iCurrX + 'x' + iCurrY).style.backgroundColor=sHLBarCol;

if (iCurrAxis==1)
    {
    if (iCurrX<mAcross)
        {
        if (cwGrid[iCurrY][iCurrX+1][1]!='#') iCurrX++;
        }
    } else
    {
    if (iCurrY<mDown)
        {
        if (cwGrid[iCurrY+1][iCurrX][1]!='#') iCurrY++;
        }
    }
document.getElementById('T' + iCurrX + 'x' + iCurrY).style.backgroundColor=sHLSqCol;

if (sChar!='')
    CheckSolution();
}

function ArrowKeyPressed(keycode)
{
var i;
switch (keycode)
    {
    case 8:
        document.getElementById('T' + iCurrX + 'x' + iCurrY).style.backgroundColor=sHLBarCol;
        document.getElementById('M' + iCurrX + 'x' + iCurrY).innerHTML='&nbsp;';
        if (iCurrAxis==1)
            {
            if (iCurrX>1)
                {
                if (cwGrid[iCurrY][iCurrX-1][1]!='#') iCurrX--;
                }
            } else
            {
            if (iCurrY>1)
                {
                if (cwGrid[iCurrY-1][iCurrX][1]!='#') iCurrY--;
                }
            }
        break;
    case 37:
        i=1;
        while (iCurrX-i>0)
            {
            if (cwGrid[iCurrY][iCurrX-i][1]!='#')
                break;
            i++;
            }
        if (iCurrX-i>0)
            iCurrX=iCurrX-i;
        iCurrAxis=1;
        highlightBar(true);
        break;
    case 38:
        i=1;
        while (iCurrY-i>0)
            {
            if (cwGrid[iCurrY-i][iCurrX][1]!='#')
                break;
            i++;
            }
        if (iCurrY-i>0)
            iCurrY=iCurrY-i;
        iCurrAxis=-1;
        highlightBar(true);
        break;
    case 39:
        i=1;
        while (iCurrX+i<mAcross+1)
            {
            if (cwGrid[iCurrY][iCurrX+i][1]!='#')
                break;
            i++;
            }
        if (iCurrX+i<mAcross+1)
            iCurrX=iCurrX+i;
        iCurrAxis=1;
        highlightBar(true);
        break;
    case 40:
        i=1;
        while (iCurrY+i<mDown+1)
            {
            if (cwGrid[iCurrY+i][iCurrX][1]!='#')
                break;
            i++;
            }
        if (iCurrY+i<mDown+1)
            iCurrY=iCurrY+i;
        iCurrAxis=-1;
        highlightBar(true);
        break;
    }
document.getElementById('T' + iCurrX + 'x' + iCurrY).style.backgroundColor=sHLSqCol;

}

function highlightClear()
{
var x;
var y;

	for (y=1; y <=mDown; y++)
		{ 
		for (x=1; x<=mAcross;x++)
		    { 
		    if (cwGrid[y][x][1]!='#')
		            document.getElementById('T' + x + 'x' + y).style.backgroundColor=sNCol;
            }
        }
}

function highlightBar(AllowChange)
{
var AxisOk;
var i;
var oSquare;
var sID;

AxisOk = false;

highlightClear();

if (iCurrAxis==1)
    {
    if (iCurrX>1)
        { if (cwGrid[iCurrY][iCurrX-1][1]!='#') AxisOk=true;}
    if (iCurrX<mAcross)
        { if (cwGrid[iCurrY][iCurrX+1][1]!='#') AxisOk=true;}
    } else
    {
    if (iCurrY>1)
        { if (cwGrid[iCurrY-1][iCurrX][1]!='#') AxisOk=true;}
    if (iCurrY<mDown)
        { if (cwGrid[iCurrY+1][iCurrX][1]!='#') AxisOk=true;}
    }
    
if (AxisOk == false)
    {
    if (AllowChange==true)
        iCurrAxis=iCurrAxis*-1;
    else
        return false;
    }
    
i=0;
if (iCurrAxis==1)
    {
    while (cwGrid[iCurrY][iCurrX-i][1]!='#')
        {
        sID ='T' + (iCurrX-i) + 'x' + iCurrY;
        document.getElementById(sID).style.backgroundColor=sHLBarCol;
        i++;
        if (iCurrX-i<=0) break;
        }
    ScrollClues('frAcross',cwGrid[iCurrY][iCurrX-i+1][2]);
    
    i=0;
    while (cwGrid[iCurrY][iCurrX+i][1]!='#')
        {
        sID ='T' + (iCurrX+i) + 'x' + iCurrY;
        document.getElementById(sID).style.backgroundColor=sHLBarCol;
        i++;
        if (iCurrX+i>=mDown+1) break;
        }
    } else
    {
    while (cwGrid[iCurrY-i][iCurrX][1]!='#')
        {
        sID ='T' + iCurrX + 'x' + (iCurrY-i);
        document.getElementById(sID).style.backgroundColor=sHLBarCol;
        i++;
        if (iCurrY-i<=0) break;
        }
    ScrollClues('frDown',cwGrid[iCurrY-i+1][iCurrX][2]);
    
    i=0;
    while (cwGrid[iCurrY+i][iCurrX][1]!='#')
        {
        sID ='T' + iCurrX + 'x' + (iCurrY+i);
        document.getElementById(sID).style.backgroundColor=sHLBarCol;
        i++;
        if (iCurrY+i>=mDown+1) break;
        }
    }
return true;
}

function getAnchorPosition (frameName, anchorName) {
  if (document.layers) {
    var anchor = document.frames[frameName].document.anchors[anchorName];
    return { x: anchor.x, y: anchor.y };
  }
  else  { 
	if (document.all) 	var anchor = document.frames[frameName].document.getElementById(anchorName);
		else 	var anchor = document.getElementById(frameName).contentWindow.document.anchors[anchorName];


    var coords = {x: 0, y: 0 };
    while (anchor) {
      coords.x += anchor.offsetLeft;
      coords.y += anchor.offsetTop;
      anchor = anchor.offsetParent;
    }
    return coords;
  }
}
function ScrollClues (frName,ClueNumber) {
    var coords = getAnchorPosition(frName,'Clue' + ClueNumber);
    document.getElementById(frName).contentWindow.scrollTo(coords.x , coords.y);
    
    if (AcrossClueHL)
        AcrossClueHL.style.backgroundColor = '';
    if (DownClueHL)
        DownClueHL.style.backgroundColor = '';
        
    if (frName=='frAcross')
        {
        if (document.all) AcrossClueHL = document.frames[frName].document.getElementById('divClue' + ClueNumber);
            else AcrossClueHL =document.getElementById(frName).contentWindow.document.getElementById('divClue' + ClueNumber);
        AcrossClueHL.style.backgroundColor = ClueHLCol;
        } else
        {
        if (document.all) DownClueHL = document.frames[frName].document.getElementById('divClue' + ClueNumber);
            else DownClueHL =document.getElementById(frName).contentWindow.document.getElementById('divClue' + ClueNumber);
        DownClueHL.style.backgroundColor = ClueHLCol;
        }

    return false;
}

function RevealLetter()
{

if (!iCurrX)
    return true;
    
PlaceChar(cwGrid[iCurrY][iCurrX][1]);

}

function CheckSolution()
{
var x;
var y;
var bOK;

    bOK = true;
    
	for (y=1; y <=mDown; y++)
		{ 
		for (x=1; x<=mAcross;x++)
		    { 
		    if (cwGrid[y][x][1]!='#')
		        {
		        if (document.getElementById('M' + x + 'x' + y).innerHTML!= cwGrid[y][x][1])
		            { bOK=false; break; }
		        }
            }
 		    if (bOK==false)
		        break;
       }
    if (bOK==true)
        {
        ShowPrize();
        }
}

function RemoveLetters()
{
var x;
var y;

	for (y=1; y <=mDown; y++)
		{ 
		for (x=1; x<=mAcross;x++)
		    { 
		    if (cwGrid[y][x][1]!='#')
		            document.getElementById('T' + x + 'x' + y).innerHTML="<div style='font-size:10px;width:10px;margin:0;padding:0;position:relative;top:-1px'>&nbsp;</div><div style='font-size:20px;text-align:center;margin:0;padding:0;position:relative;top:-5px;line-height:18px'>&nbsp;</div>";
            }
        }
}

function CheckMoves()
{
var x;
var y;
var allok;

	allok=true;
	for (y=1; y <=mDown; y++)
		{ 
		for (x=1; x<=mAcross;x++)
		    { 
		    if (cwGrid[y][x][1]!='#')
		        {
		        if (document.getElementById('M' + x + 'x' + y).innerHTML!= cwGrid[y][x][1] && document.getElementById('M' + x + 'x' + y).innerHTML!='&nbsp;')
		            { document.getElementById('T' + x + 'x' + y).style.backgroundColor=sWrongMoveCol;
			    allok=false; }	
		        }
            }
        }
	if (allok==true)
		alert('All moves so far are okay.');

}

function ShowPrize()
{
	alert("Congratulations for solving the crossword!");
}

function ShowPrizeGrad()
{
document.getElementById('PrizePic').style.display = '';

curOpac++;

if (curOpac>100 && curOpac<500)
	{
	curOpac=1000;
	document.getElementById('hideDiv').style.MozOpacity = '1.0';
	document.getElementById('tblCrossword').style.MozOpacity = '0.0';
	RemoveLetters();
	alert("Congratulations for solving the crossword!");
	return true;
	}
	
IE?document.getElementById("hideDiv").style.filter="alpha(opacity="+curOpac+")":document.getElementById("hideDiv").style.MozOpacity=curOpac/90.1;

IE?document.getElementById("tblCrossword").style.filter="alpha(opacity="+(90-curOpac)+")":document.getElementById("tblCrossword").style.MozOpacity=(90-curOpac)/90.1;

}

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name,DefaultVal) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break; 
}
return DefaultVal;
}  

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}


function LoadGame()
{
var sMoves;
var x;
var y;
var iPos;

sMoves= GetCookie("Moves_" + cwID,"");
if (sMoves=="")
	{
	alert("There is no saved game for this crossword puzzle (clearing cookies will erase saved games).");
	return true;
	}


iPos=0;
	for (y=1; y <=mDown; y++)
		{ 
		for (x=1; x<=mAcross;x++)
		    { 
		    if (cwGrid[y][x][1]!='#')
		        {
		        document.getElementById('M' + x + 'x' + y).innerHTML=sMoves.split('|')[iPos];
			iPos++;
		        }
		    }	
            	}

}


function SaveGame()
{
var x;
var y;
var sMoves;
var sMovesSaved;

var expdate = new Date ();
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 31));

sMoves='';

	for (y=1; y <=mDown; y++)
		{ 
		for (x=1; x<=mAcross;x++)
		    { 
		    if (cwGrid[y][x][1]!='#')
		        {
		        sMoves = sMoves + document.getElementById('M' + x + 'x' + y).innerHTML + '|';
		        }
		    }
            	}

SetCookie("Moves_" + cwID,sMoves,expdate);
sMovesSaved= GetCookie("Moves_" + cwID,"");

if (sMovesSaved==sMoves)
	alert("Crossword was successfully saved.");
else
	alert("Unable to save the game. Possible causes and fixes are:\n\n 1. Cookies are blocked. You can allow cookies for this site through your browser.\n\n2. Your space for cookies in your browser is full. To fix, delete old cookies to recover some space.");
}



function CWInit()
{
document.onkeydown = KeyboardPress;
if (document.layers)
document.captureEvents(Event.KEYDOWN); 

  document.getElementById("frAcross").contentWindow.document.open();
  document.getElementById("frAcross").contentWindow.document.write(AcrossClues);
  document.getElementById("frAcross").contentWindow.document.close();
  document.getElementById("frDown").contentWindow.document.open();
  document.getElementById("frDown").contentWindow.document.write(DownClues);
  document.getElementById("frDown").contentWindow.document.close();

var x;
var y;
var i;

i=0;

	cwGrid = new Array(mDown);
	for (y=1; y <=mDown; y++)
		{ 
		cwGrid[y]=new Array(mAcross);
		for (x=1; x<=mAcross;x++)
		    { 
		    cwGrid[y][x]=new Array(2)
		    cwGrid[y][x][1] = cwMoves.substring(i,i+1);
		    cwGrid[y][x][2] = cwCluePos.split(',')[i];
		    i++;
            }
        }		

	for (x=1; x <=mAcross; x++)
	    {
		for (y=1; y<=mDown;y++)
		    {
		    if (cwGrid[y][x][1]!='#')
		        {
 	            oControl=document.getElementById('T' + x + 'x' + y);
             	oControl.ClickHandler = MouseMoveXY; 
    	        XBrowserAddHandler(oControl,"click","ClickHandler");
	            }
	        }
	    }

}