tPopWait=1;
tPopShow=60000;
showPopStep=20;
popOpacity=50;
sPop=null;
curShow=null;
MouseX=0;
MouseY=0;
popLeftAdjust=0;
popTopAdjust=0;
searchkeyword = "http://search.msn.co.uk/results.aspx";

//Create CrossNo Board Infomation
function CreateCrossBoardInfo()
{
    var templateinfo = document.getElementById('templateinfo');
    var innerHtml = '<span id="lblInfo" class="msntip"></span><select id="crossNo"></select><span id="finish" class="finished"></span>';
    templateinfo.innerHTML = innerHtml;
    document.getElementById('crossNo').attachEvent("onchange", showCrossNo);    
    for(i = crossList.length - 1; i >= 0; i--)
    {
        var oOption = document.createElement("OPTION");
        oOption.innerText = "Crossword No." + crossList[i];
        oOption.value = crossList[i];
        document.getElementById('crossNo').appendChild(oOption);
    }
    showCrossNo();
}

function showCrossNo()
{
    var obj = document.getElementById('crossNo');
    document.getElementById('lblInfo').innerHTML = obj.options[obj.selectedIndex].innerText;
    var str = (document.getElementById("crossNo")).value;
    if(parseInt(str, 10) == crossNum) return;
    crossNum = parseInt(str, 10);
    initgame();

    var obj1 = document.getElementById("backDiv");
    obj1.innerHTML = buildBack2();

    var obj2 = document.getElementById("frontDiv");
    obj2.innerHTML = buildFront2();

    var obj3 = document.getElementById("acrossdiv");
    obj3.innerHTML = buildAcross2();

    var obj4 = document.getElementById("downdiv");
    obj4.innerHTML = buildDown2();

    clearInterval(timer);
    reset();
}

//Create CrossGame
function CreateCrossGame()
{
    var crossgame = document.getElementById('crossgame');
    var backdiv = document.createElement("div"); 
    var frontdiv = document.createElement("div");
    backdiv.id = "backDiv";
    frontdiv.id = "frontDiv";
            
    crossgame.appendChild(backdiv);
    crossgame.appendChild(frontdiv);
          
    var backTable = document.createElement("table"); 
    backTable.cellSpacing=1;
    backTable.cellPadding=2;
    backTable.id= "backTable";
    backTable.className="backtable";
    backdiv.appendChild(backTable);
    
    var frontTable = document.createElement("table");
    frontTable.cellSpacing=1;
    frontTable.cellPadding=2;
    frontTable.id="frontTable";    
    frontTable.setAttribute("onkeydown", "javascript:doInput(event); return false;");
    frontdiv.appendChild(frontTable);
    
    var gidback, num,backTR,backTD,backDIV;
    for(i = 0; i < rows; i++)
    {
        backTR = document.createElement("tr");
        backTable.appendChild(backTR);
        for(j = 0; j < cols; j++)
        {
            gidback = grid[i][j].substr(4);
            num = grid[i][j].substr(0, 4);
            backTD = document.createElement("td");
            backTR.appendChild(backTD);

            if(gidback != "0000")
            {
                backTD.id=grid[i][j];
                backTD.className="occupy";              
                backDIV = document.createElement("div");
                backTD.appendChild(backDIV);
                backDIV.className='small';
                if(smallNum[num] != null)
                {
                    backDIV.innerHTML = smallNum[num];
                }                
            }                
        }            
    }
    
    var gidfront;
    for(k = 0; k < rows; k++)
    {
        var frontTR = document.createElement("tr");
        frontTable.appendChild(frontTR);
        for(g = 0; g < cols; g++)
        {
         gidfront= grid[k][g].substr(4);
         var frontTD = document.createElement("td");
         frontTR.appendChild(frontTD);
         if(gidfront != "0000")
         {
           if (window.navigator.userAgent.indexOf("Firefox")>=1)
           { 
               //Firefox
               var frontINPUT = document.createElement("input"); 
               frontINPUT.className = "cellinput";
               frontINPUT.id = "f" + grid[k][g];
               frontINPUT.maxlength = "1";
               frontINPUT.setAttribute("onclick", "javascript:doclick('" + grid[k][g] + "');");
               frontTD.appendChild(frontINPUT);
           }
           else
           {
              //IE, Opera
              var frontDIV = document.createElement("div"); 
              frontDIV.className = "cell";
              var divid = "f" + grid[k][g];
              frontDIV.id = divid
              frontDIV.setAttribute("onclick", "javascript:doclick('" + grid[k][g] + "');");
              frontTD.appendChild(frontDIV);
           }
         }
        }
      }
      
   crossgame.innerHTML = crossgame.innerHTML;
}

//Create Across
function CreateAcross()
{
   var oAcross = document.getElementById('acrossdiv');
   var acrossTable = document.createElement("table");
   acrossTable.id="acrossTable";
   acrossTable.cellspacing="0";
   oAcross.appendChild(acrossTable);
   var acrossTR = document.createElement("tr");
   var acrossTD = document.createElement("td");
   for(h = 0; h < xGame.length; h++)
 {
  acrossTable.appendChild(acrossTR);
  acrossTR.appendChild(acrossTD);
  var tbl = document.createElement("table");
  tbl.id="x" + xGame[h].id;
  tbl.className="nolight";
  tbl.setAttribute("onclick", "javascript:hintClick('" + "x" + xGame[h].id + "');");
  if (window.navigator.userAgent.indexOf("Firefox")>=1)
  {
    tbl.setAttribute("onmouseover", "javascript:showPopupText1(event);");
  }
  else
  {
    tbl.setAttribute("onmouseover", "javascript:showPopupText('" + "tdx" + xGame[h].id + "');");
  }
  tbl.setAttribute("onmouseout", "javascript:hidePopupText(event);"); 

  acrossTD.appendChild(tbl);
  var tr = document.createElement("tr");
  var td1 = document.createElement("td");
  var td2 = document.createElement("td");
  td2.id = "tdx" + xGame[h].id;  
  tbl.appendChild(tr);
  td1.className = "cindex";
  td1.innerText = parseInt(xGame[h].id, 10);
  td2.innerText = xGame[h].hint;
  tr.appendChild(td1);
  tr.appendChild(td2);
 }
 
   oAcross.innerHTML = oAcross.innerHTML;
}


//Create Down
function CreateDown()
{
   var oDown = document.getElementById('downdiv');
   var downTable = document.createElement("table");
   downTable.id="downTable";
   downTable.cellspacing="0";
   oDown.appendChild(downTable);
   var downTR = document.createElement("tr");
   var downTD = document.createElement("td");
   for(w = 0; w < yGame.length; w++)
   {
  downTable.appendChild(downTR);
  downTR.appendChild(downTD);
 var tbl = document.createElement("table");
 tbl.id="y" + yGame[w].id;
 tbl.className = "nolight";
 tbl.setAttribute("onclick", "javascript:hintClick('" + "y" + yGame[w].id + "');");
  if (window.navigator.userAgent.indexOf("Firefox")>=1)
  {
     tbl.setAttribute("onmouseover", "javascript:showPopupText1(event);");
  }
  else
  {
     tbl.setAttribute("onmouseover", "javascript:showPopupText('" + "tdy" + yGame[w].id + "');");
  }
  tbl.setAttribute("onmouseout", "javascript:hidePopupText(event);");  
     
 downTD.appendChild(tbl);
 var tr = document.createElement("tr");
 var td1 = document.createElement("td");
 var td2 = document.createElement("td");
 td2.id = "tdy" + yGame[w].id; 
 tbl.appendChild(tr);
 td1.className = "cindex";
 td1.innerText = parseInt(yGame[w].id, 10);
 td2.innerText = yGame[w].hint;
 tr.appendChild(td1);
 tr.appendChild(td2);
 }
   oDown.innerHTML = oDown.innerHTML;
}


function doclick(id)
{
var obj;
var hash;
var hid;

// clear last word color
if(clickId != id)
{
 if(clickId != 0)
 {
  changeColor(clickId, "url(http://estc.msn.com/br/intl/spec/en-gb/crossword/css/1/decoration/gray_lite.gif)");
 }
}
else
{
 changeColor(clickId, "url(http://estc.msn.com/br/intl/spec/en-gb/crossword/css/1/decoration/gray_lite.gif)");
 bVertical = !bVertical;
}

if(bVertical) hash = id.substr(6, 2);
else hash = id.substr(4, 2);
if(hash == "00") bVertical = !bVertical;
var scroll;
if(bVertical)
{
 scroll = document.getElementById("downdiv");
 hid = "y" + id.substr(6, 2);
}
else
{
 scroll = document.getElementById("acrossdiv");
 hid = "x" + id.substr(4, 2);
}

changeHintColor(hid);

changeColor(id, "url(http://estc.msn.com/br/intl/spec/en-gb/crossword/css/1/decoration/green_lite.gif)");

obj = document.getElementById(id);
obj.style.backgroundImage = "url(http://estc.msn.com/br/intl/spec/en-gb/crossword/css/1/decoration/blue_lite.gif)";

clickId = id;

obj = document.getElementById(hid);

var parent = obj;
var top = parent.offsetTop;
var height = obj.offsetHeight;

while(parent.offsetParent)
{
 parent = parent.offsetParent;
 if(parent.tagName == "TABLE") break;
 top += parent.offsetTop;
}

//while(scroll.scrollTop > top)
//{
//    scroll.doScroll("scrollbarUp");
//}

//while(scroll.scrollTop + 100 < top + height)
//{
// scroll.doScroll("scrollbarDown");
//}
}

function changeColor(cid, color)
{
var hash;
var x, y;

if(bVertical)
{
 hash = cid.substr(6, 2);
 x = parseInt(yHash[hash].x, 10) - 1;
 y = parseInt(yHash[hash].y, 10) - 1;
 for(i = 0; i < yHash[hash].len; i++)
 {
  obj = document.getElementById(grid[x + i][y]);
  obj.style.backgroundImage = color;
 }
}
else
{
 hash = cid.substr(4, 2);
 x = parseInt(xHash[hash].x, 10) - 1;
 y = parseInt(xHash[hash].y, 10) - 1;
 for(i = 0; i < xHash[hash].len; i++)
 {
  obj = document.getElementById(grid[x][y + i]);
  obj.style.backgroundImage = color;
 }
}
}

function changeHintColor(hid)
{
var obj;

if(lastHintId == hid)
 return;

if(lastHintId != "")
{
 obj = document.getElementById(lastHintId);
 obj.className = "nolight";
}
obj = document.getElementById(hid);
obj.className = "highlight";
lastHintId = hid;
}


function hintClick(cid)
{
 if(lastHintId == cid)
  return;
  
 var obj;

 changeHintColor(cid);
 
 var dir = cid.substr(0, 1);
 var id = cid.substr(1, 2);
 var x, y;
 
 if(dir == "x")
 {
  x = parseInt(xHash[id].x, 10) - 1;
  y = parseInt(xHash[id].y, 10) - 1;
 }
 else
 {
  x = parseInt(yHash[id].x, 10) - 1;
  y = parseInt(yHash[id].y, 10) - 1;
 }
 changeColor(clickId, "url(http://estc.msn.com/br/intl/spec/en-gb/crossword/css/1/decoration/gray_lite.gif)");
 
 if(dir == "x")
  bVertical = false;
 else
  bVertical = true;

changeColor(grid[x][y], "url(http://estc.msn.com/br/intl/spec/en-gb/crossword/css/1/decoration/green_lite.gif)");
 
obj = document.getElementById(grid[x][y]);
obj.style.backgroundImage = "url(http://estc.msn.com/br/intl/spec/en-gb/crossword/css/1/decoration/blue_lite.gif)";
 
// if (window.navigator.userAgent.indexOf("Firefox")>=1)
// {
//     alert(obj.id);
//     obj.onFocus = "javascript:doInput(event); return false;"; //Firefox 
// }
// else 
     document.getElementById("frontTable").focus();   //Can't get the focus in Firefox.
   
 clickId = grid[x][y];
}


function doInput(evt)
{  
 var key;
 var x, y;
 var obj, newid, newobj;
 if(clickId == 0) return;
 evt = (evt) ? evt : ((window.event) ? event : null);
 if(evt.keyCode == 8) key = "";
 else key = String.fromCharCode(evt.keyCode);
 
 if (window.navigator.userAgent.indexOf("Firefox")>=1)
    {
     if(key >= 'a' && key <= 'z') key = key.toUpperCase();
     else
     { 
         if((key >= 'A' && key <= 'Z') || (key == ""))
         {
          x = parseInt(clickId.substr(0, 2), 10) - 1;
          y = parseInt(clickId.substr(2, 2), 10) - 1;
          
          obj = document.getElementById("f" + clickId); //obj is an Input Element in Firefox
          
          if(key != "") 
          { 
            obj.value = key;
          }
          
          if(bVertical)
          {
           if(key == "")
           {
            if (obj.value == "") x--;
            if(x < 0) x = 0;
           }
           else
           {
            x++;
            if(x > rows-1) x = rows-1;
           }
          }
          else
          {
           if(key == "")
           {
            if (obj.value == "") y--;
            if(y < 0) y = 0;
           }
           else
           {
            y++;
            if(y > cols-1) y = cols-1;
           }
          }
          newid = grid[x][y];
          if(key == "")
          {
           obj = document.getElementById("f" + newid);
           if(obj != null) obj.value = "";
          }
          
          if(clickId != newid && (newobj = document.getElementById(newid)) != null)
          {
           obj = document.getElementById(clickId);
           obj.style.backgroundImage = "url(http://estc.msn.com/br/intl/spec/en-gb/crossword/css/1/decoration/green_lite.gif)";
           newobj.style.backgroundImage = "url(http://estc.msn.com/br/intl/spec/en-gb/crossword/css/1/decoration/blue_lite.gif)";
           clickId = newid;
          }
          checkAnswer(clickId);
         }
     }        
    }
    else  //IE
    {
     if(key >= 'a' && key <= 'z') key = key.toUpperCase();
     else
     { 
         if((key >= 'A' && key <= 'Z') || (key == ""))
         {
          x = parseInt(clickId.substr(0, 2), 10) - 1;
          y = parseInt(clickId.substr(2, 2), 10) - 1;
      
          obj = document.getElementById("f" + clickId); //obj is an div element in IE
          if(key != "") obj.innerText = key;
          if(bVertical)
          {
           if(key == "")
           {
         if (obj.innerText == "") x--;
            if(x < 0) x = 0;
           }
           else
           {
            x++;
            if(x > rows-1) x = rows-1;
           }
          }
          else
          {
           if(key == "")
           {
            if (obj.innerText == "") y--;
            if(y < 0) y = 0;
           }
           else
           {
            y++;
            if(y > cols-1) y = cols-1;
           }
          }
          newid = grid[x][y];
          if(key == "")
          {
           obj = document.getElementById("f" + newid);
           if(obj != null) obj.innerText = "";
          }
      
          if(clickId != newid && (newobj = document.getElementById(newid)) != null)
          {
           obj = document.getElementById(clickId);
           obj.style.backgroundImage = "url(http://estc.msn.com/br/intl/spec/en-gb/crossword/css/1/decoration/green_lite.gif)";
           newobj.style.backgroundImage = "url(http://estc.msn.com/br/intl/spec/en-gb/crossword/css/1/decoration/blue_lite.gif)";
           clickId = newid;
          }
      
          checkAnswer(clickId);
     }
 }

   }
}

function checkAnswer(checkId)
{
 var hash;
 var x, y;
 var ii;
   
 hash = checkId.substr(6, 2);
 if(hash != "00")
 {
  x = parseInt(yHash[hash].x, 10) - 1;
  y = parseInt(yHash[hash].y, 10) - 1;
  
  if (window.navigator.userAgent.indexOf("Firefox")>=1)
  {
      for(ii = 0; ii < yHash[hash].len; ii++)
      {
       obj = document.getElementById("f" + grid[x + ii][y]); // obj is an Input Element in Firefox
       if(obj.value != yHash[hash].solution.charAt(ii).toUpperCase())
        break;
      }
      if(ii == yHash[hash].len)
      {
       if(!yCorrect[hash])
       {
        yCorrect[hash] = true;
        nCorrect++;
        obj = document.getElementById("y" + hash);
        obj.style.textDecoration = "line-through";
       }
      }
      else if(yCorrect[hash])
      {
       yCorrect[hash] = false;
       nCorrect--;
       obj = document.getElementById("y" + hash);
       obj.style.textDecoration = "none";
      }
  }
  else
  {
      for(ii = 0; ii < yHash[hash].len; ii++)
      {
       obj = document.getElementById("f" + grid[x + ii][y]); // obj is an Div Element in IE
       if(obj.innerText != yHash[hash].solution.charAt(ii).toUpperCase())
        break;
      }
      if(ii == yHash[hash].len)
      {
       if(!yCorrect[hash])
       {
        yCorrect[hash] = true;
        nCorrect++;
        obj = document.getElementById("y" + hash);
        obj.style.textDecoration = "line-through";
       }
      }
      else if(yCorrect[hash])
      {
       yCorrect[hash] = false;
       nCorrect--;
       obj = document.getElementById("y" + hash);
       obj.style.textDecoration = "none";
      }   
  }
 }

 hash = checkId.substr(4, 2);
 if(hash != "00")
 {
  x = parseInt(xHash[hash].x, 10) - 1;
  y = parseInt(xHash[hash].y, 10) - 1;
  
  if (window.navigator.userAgent.indexOf("Firefox")>=1)
  {
      for(ii = 0; ii < xHash[hash].len; ii++)
      {
       obj = document.getElementById("f" + grid[x][y + ii]);
                if(obj.value != xHash[hash].solution.charAt(ii).toUpperCase()) 
       break;
      }
      if(ii == xHash[hash].len)
      {
       if(!xCorrect[hash])
       {
        xCorrect[hash] = true;
        nCorrect++;
        obj = document.getElementById("x" + hash);
        obj.style.textDecoration = "line-through";
       }
      }
      else if(xCorrect[hash])
      {
       xCorrect[hash] = false;
       nCorrect--;
       obj = document.getElementById("x" + hash);
       obj.style.textDecoration = "none";
      }
  }
  else
  {
      for(ii = 0; ii < xHash[hash].len; ii++)
      {
       obj = document.getElementById("f" + grid[x][y + ii]);
                if(obj.innerText != xHash[hash].solution.charAt(ii).toUpperCase()) 
       break;
      }
      if(ii == xHash[hash].len)
      {
       if(!xCorrect[hash])
       {
        xCorrect[hash] = true;
        nCorrect++;
        obj = document.getElementById("x" + hash);
        obj.style.textDecoration = "line-through";
       }
      }
      else if(xCorrect[hash])
      {
       xCorrect[hash] = false;
       nCorrect--;
       obj = document.getElementById("x" + hash);
       obj.style.textDecoration = "none";
      }
  }
 }
 
 obj = document.getElementById("finish");
 if(nCorrect == xGame.length + yGame.length)
 {
  obj.innerText = "Completed!";
  var timeobj = document.getElementById("time");
  alert("Crossword Complete!\nTotal time used: " + timeobj.innerText);
 }
 else
  obj.innerText = "";
}


//Add Functions Below On 30-Mar
function CreateTopTimeControls()
{
    var otoptime = document.getElementById('toptime');
    var innerHtml = '<div id="time">00:00:00</div>';  
    innerHtml += '<div id="divpause">';
    innerHtml += '<input id="pause" class="msnbtn" style="width: 77px" onclick="pauseTime(this)" type="button"';
    innerHtml += ' value="Pause" name="pause" /></div>';
    innerHtml += '<div id="divclear">';
    innerHtml += '<input id="clear" class="msnbtn" style="width: 150px" onclick="clearCross()" type="button"';
    innerHtml += ' value="Clear Crossword" name="clear" /></div>';
    innerHtml += '<div id="divemail">';
    innerHtml += '<a href="javascript:mailAns()">Email me the answers</a></div>';
    innerHtml += '<div id="divsave">';
    innerHtml += '<input id="save" class="msnbtn" style="width: 150px" onclick="saveCross()" type="button"';
    innerHtml += ' value="Save my answers" name="save" /></div>';
    innerHtml += '<div style="display: none">';
    innerHtml += '<form id="mailform" action="mailto:" method="GET">';
    innerHtml += '<input name="subject" id="subject" type="hidden" value="">';
    innerHtml += '<textarea name="body" id="body" cols="30" rows="20"></textarea>';
    innerHtml += '</form></div>';
      
    otoptime.innerHTML = innerHtml;
}


function showTime()
{
 if(!bTimer)
  return;
  
 second++;
 
 if(second == 60)
 {
  second = 0;
  minute++;
 }
 var secStr = "" + (second < 10 ? "0" + second : second);
 
 if(minute == 60)
 {
  minute = 0;
  hour++;
 }
 
 if(hour == 24)
  hour = 0;
  
 var minStr = "" + (minute < 10 ? "0" + minute : minute);
 var hStr = "" + (hour < 10 ? "0" + hour : hour);
 
 var obj = document.getElementById("time");
 obj.innerText = hStr + ":" + minStr + ":" + secStr;
}

function pauseTime(btn)
{
 bTimer = !bTimer;
 if(bTimer)
  btn.value = "Pause";
 else
  btn.value = "Resume";
}

function clearCross()
{
 var obj;
 
 if(!window.confirm("Are you sure?")) return;
    
 if (window.navigator.userAgent.indexOf("Firefox")>=1)
 {
     for(i = 0; i < rows; i++)
     {
      for(j = 0; j < cols; j++)
      {
       obj = document.getElementById("f" + grid[i][j]);
       if(obj != null) obj.value = "";
      }
     }
 }
 else
 {
     for(i = 0; i < rows; i++)
     {
      for(j = 0; j < cols; j++)
      {
       obj = document.getElementById("f" + grid[i][j]);
       if(obj != null) obj.innerText = "";
      }
     } 
 }
 
 minute = 0;
 second = 0;
 hour = 0;
 
 nCorrect = 0;
 for(i = 0; i < xGame.length; i++)
 {
  xCorrect[xGame[i].id] = false;
  obj = document.getElementById("x" + xGame[i].id);
  obj.style.textDecoration = "none";
 }
 for(i = 0; i < yGame.length; i++)
 {
  yCorrect[yGame[i].id] = false;
  obj = document.getElementById("y" + yGame[i].id);
  obj.style.textDecoration = "none";
 }
 obj = document.getElementById("finish");
 obj.innerText = "";
}

function saveCross()
{
 var str = "";
 var obj;
 var today = new Date();
 var nextyear = new Date(today.getFullYear() + 1, 0 , 1);
    
    if (window.navigator.userAgent.indexOf("Firefox")>=1)
    {
     for(i = 0; i < rows; i++)
     {
      for(j = 0; j < cols; j++)
      {
       obj = document.getElementById("f" + grid[i][j]);
       if(obj != null)
       {
        if(obj.value != "")
         str += obj.value;
        else
         str += "1";
       }
       else
        str += "0";
      }
     }
 }
 else
 {
     for(i = 0; i < rows; i++)
     {
      for(j = 0; j < cols; j++)
      {
       obj = document.getElementById("f" + grid[i][j]);
       if(obj != null)
       {
        if(obj.innerText != "")
         str += obj.innerText;
        else
         str += "1";
       }
       else
        str += "0";
      }
     }     
 }
 
 objtime = document.getElementById("time"); 
 document.cookie = "answer=" + crossNum + objtime.innerText + str + ";expires=" + nextyear.toUTCString();
 alert("Your current crossword solutions have been saved\n and will appear next time you visit this page");
}

function mailAns()
{
 var form = document.getElementById("mailform");
 var sub = document.getElementById("subject");
 var mb = document.getElementById("body");
 sub.value = "MSN_Crossword_Solutions" + crossNum;
 
 var sol = "Across:\n";
 
 for(i = 0; i < xGame.length; i++)
 {
  sol += xGame[i].id + ".\t" + xGame[i].solution + "\n";
 }
 
 sol += "\nDown:\n";
 for(i = 0; i < yGame.length; i++)
 {
  sol += yGame[i].id + ".\t" + yGame[i].solution + "\n";
 }
 mb.value = "\n\n\n\n\n\n\n\n" + sol;
 form.submit();
}

function reset()
{
 var back = document.getElementById("backTable");
 var x = back.offsetLeft;
 var y = back.offsetTop;
 while(back.offsetParent)
 {
  back = back.offsetParent;
  x += back.offsetLeft;
  y += back.offsetTop;
 }
 var front = document.getElementById("frontDiv");
 front.style.left = x;
 front.style.top = y + 2;
 document.getElementById('pause').value = "Pause";
 timer = setInterval("showTime();", 1000);
 loadCross();
 var a = parseInt(xGame[0].x, 10) - 1;
 var b = parseInt(xGame[0].y, 10) - 1;
 doclick(grid[a][b]);
 var obj = document.getElementById("frontTable");
 obj.focus();
}

function loadCross()
{
 var index;
 var s;
 var obj;

 if(document.cookie.length < rows * cols + 7 || (index = document.cookie.indexOf("answer=")) == -1)
 {
  return;
 }
 
 var str = document.cookie.substr(index + 7, 4);
 if(parseInt(str, 10) != crossNum)
  return;
  
 str = document.cookie.substr(index + 11, 8);
 hour = parseInt(str.substr(0, 2), 10);
 minute = parseInt(str.substr(3, 2), 10);
 second = parseInt(str.substr(6, 2), 10);
 objtime = document.getElementById("time");
 objtime.innerText = str;
  
 str = document.cookie.substr(index + 19, rows * cols);
 
 if (window.navigator.userAgent.indexOf("Firefox")>=1)
 {
     for(i = 0; i < rows; i++)
     {
      for(j = 0; j < cols; j ++)
      {
       s = str.substr(i * rows + j, 1);
       if(s != "1" && s != "0")
       {
        obj = document.getElementById("f" + grid[i][j]);
        obj.value = s;
        checkAnswer(grid[i][j]);
       }
      }
     }
 }
 else
 {
     for(i = 0; i < rows; i++)
     {
      for(j = 0; j < cols; j ++)
      {
       s = str.substr(i * rows + j, 1);
       if(s != "1" && s != "0")
       {
        obj = document.getElementById("f" + grid[i][j]);
        obj.innerText = s;
        checkAnswer(grid[i][j]);
       }
      }
     } 
 }
 
 document.cookie = "answer= ;expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function CreateMSNSearch()
{
    var omsnsearch = document.getElementById("msnsearch");
    var innerHtml = '<input id="keyword" type="text" name="keyword" size="18"'; 
    innerHtml += 'onkeyup="searchReturn()">';
    innerHtml += '<span class="tbbdr">';
    innerHtml += '<input id="search" type="button" class="msnbtn" width="40" value="Search" name="Search" onclick="doSearch(\'keyword\')"></span>';
    innerHtml += '<span class="tbbdr"><input id="gobutton" type="button" class="gobtn" onclick="showList(\'gobutton\')" name="gobutton"></span>';
    innerHtml += '<div id="hideOpt" class="hideopt" style="display: none; z-index: 10">';
    innerHtml += '<div class="hideitem" onclick="searchOpt(\'0\')" onmouseover="itementer(this)" onmouseout="itemleave(this)">Web</div>';
    innerHtml += '<div class="hideitem" onclick="searchOpt(\'1\')" onmouseover="itementer(this)" onmouseout="itemleave(this)">News</div>';
    innerHtml += '<div class="hideitem" onclick="searchOpt(\'2\')" onmouseover="itementer(this)" onmouseout="itemleave(this)">Shopping</div>';
    innerHtml += '<div class="hideitem" onclick="searchOpt(\'3\')" onmouseover="itementer(this)" onmouseout="itemleave(this)">Encarta</div>';
    innerHtml += '<div class="hideitem" onclick="searchOpt(\'4\')" onmouseover="itementer(this)" onmouseout="itemleave(this)">Groups</div>'; 
    
    omsnsearch.innerHTML = innerHtml;
}

function doSearch(id)
{
 var obj = document.getElementById(id);
 var str;
 
 if(window.encodeURIComponent)
  str = encodeURIComponent(obj.value);
 else
  str = obj.value;
 
 str = "http://" + al[1] + str; 
 
 var sWnd = window.open(str, "_blank");
 sWnd.focus();
}

function searchOpt(opt)
{
 var obj = document.getElementById(schkey);
 i = parseInt(opt, 10) * 2;
 var str;
 
 if(window.encodeURIComponent)
  str = encodeURIComponent(obj.value);
 else
  str = obj.value;

if (i == 8)
{
  str = "http://" + al[9] + "q=" + str + "+site%3Agroups.msn.com";
}
else
{
   str = "http://" + al[i+1] + str;
} 
 var sWnd = window.open(str, "_blank");
 sWnd.focus();
}


function itementer(item)
{
 item.style.backgroundColor = "#999";
}

function itemleave(item)
{
 item.style.backgroundColor = "white";
}

function showList(src)
{
 if(src == "gobutton")
  schkey = "keyword";
 else
  schkey = "keyword2";
  
 var obj = document.getElementById("hideOpt");
 if(obj.style.display == "")
  obj.style.display = "none";
 else
 {
  obj.style.display = "";

  var btn = document.getElementById(src);
  var x = btn.offsetLeft;
  var y = btn.offsetTop;
  while(btn.offsetParent)
  {
   btn = btn.offsetParent;
   x += btn.offsetLeft;
   y += btn.offsetTop;
  }
  obj.style.left = x;
  if(src == "gobutton")
   obj.style.top = y + 20;
  else
   obj.style.top = y - obj.offsetHeight;
 }
}

function searchReturn(evt)
{
 evt = (evt) ? evt : ((window.event) ? event : null);
 
 if(evt.keyCode == 10 || evt.keyCode == 13)
 {
   return doSearch(evt.srcElement.id);
 }
}

function CreateSubfoot()
{
    var subfoot = document.getElementById('subfoot');
    subfoot.innerHTML = "";
    var innerHtml = '<div class="parent chrome6 triple1 cf">';
    innerHtml += '<div id="subfoot1" class="child c1 first">';
    innerHtml += '<span class="imglink3 cf">';
    innerHtml += '<img src="http://stb.msn.com/i/5D/73163521B6D5D5C73C288328177A.jpg" width="16" height="11" alt="Send to a friend" />';
    innerHtml += '<a href="javascript:mailFriend()">Send to a friend</a></span></div>';
    innerHtml += '<div id="subfoot2" class="child c2">';
    innerHtml += '<span class="imglink3 cf">';
    innerHtml += '<img src="http://stb.msn.com/i/16/5CB0060BAF24DC932FEBB29671A78.jpg" width="16" height="16" alt="Print friendly version" />';
    innerHtml += '<a href="http://specials.uk.msn.com/crossword/crosswordprint.aspx" target="_blank">Print friendly version</a></span></div>';
    innerHtml += '<div id="subfoot3" class="child c3 last">';
    innerHtml += '<span class="imglink3 cf">';
    innerHtml += '<img src="http://stb.msn.com/i/45/2FBD55E88B33178EBC0ED9562C82B.jpg" width="15" height="16" alt="Email me the answers" />';
    innerHtml += '<a href="javascript:mailAns()">Email me the answers</a></span></div></div>';
    
    subfoot.innerHTML = innerHtml;
}

function mailFriend()
{
    var sSubject = "Play our Crossword game";
    var sBody = "Please go to http://specials.uk.msn.com/crossword/default.aspx";
    var et='mailto:?subject=' + sSubject + '&body=' + sBody;
    window.open(et);
}

function createMSNSearchTips()
{
    var o = document.getElementById('area2');
    var innerHtml = '<div id="dypopLayer" style="position:absolute;z-index:1000;height:29px;width:120px;cursor:hand;display:none;"';
    innerHtml += 'onmouseover="javascript:displayPopLayer()" onmouseout="javascript:hidePopLayer()">';
    innerHtml += '<a id="dypoplink" href="javascript:opensearch(searchkeyword)">';
    innerHtml += '<img src=\"http://estc.msn.com/br/intl/spec/en-gb/crossword/css/1/decoration/tip.png\" border=\"0\" /></a></div>';
    o.innerHTML += innerHtml;
}

function displayPopLayer()
{
    document.getElementById('dypopLayer').style.display = "";
}

function hidePopLayer()
{
    document.getElementById('dypopLayer').style.display = "none";
}

function buildBack2()
{
 var gid, num;
 var result = "";
 
 result = "<table id='backTable' cellSpacing='1' cellPadding='2' class='backtable'>";
 for(i = 0; i < rows; i++)
 {
  result += "<tr>";
  for(j = 0; j < cols; j++)
  {
   gid = grid[i][j].substr(4);
   num = grid[i][j].substr(0, 4);
   
   if(gid == "0000")
    result += "<td></td>";
   else
   {
    result += "<td id='" + grid[i][j] +"' class='occupy'>";
    if(smallNum[num] != null)
     result += "<div class='small'>" + smallNum[num] + "</div>";
    else
     result += "<div class='small'></div>";
    result += "</td>";
   }
  }
  result += "</tr>";
 }
 result += "</table>";
 
 return result;
}

function buildFront2()
{
 var gid;
 var result = "";
 result = "<table id='frontTable' onkeydown='doInput(event); return false;' cellSpacing='1' cellPadding='2'>";

 if (window.navigator.userAgent.indexOf("Firefox")>=1)
 {
     for(g = 0; g < rows; g++)
     {
      result += "<tr>"; 
      for(h = 0; h < cols; h++)
      {
       gid = grid[g][h].substr(4);
       
       if(gid == "0000")
        result += "<td></td>";
       else
       {
        result += "<td><input id=\"f" + grid[g][h] + "\" class=\"cellinput\" onclick=\"doclick('" + grid[g][h] + "')\" maxlength=\"1\"></input></td>";
       }
      }
      result += "</tr>";
     }  
 }
 else
 {
     for(i = 0; i < rows; i++)
     {
      result += "<tr>"; 
      for(j = 0; j < cols; j++)
      {
       gid = grid[i][j].substr(4);
       
       if(gid == "0000")
        result += "<td></td>";
       else
       {
        result += "<td><div id=\"f" + grid[i][j] + "\" class=\"cell\" onclick=\"doclick('" + grid[i][j] + "')\"></div></td>";
       }
      }
      result += "</tr>";
     }
 }
 
 result += "</table>";
 return result;
}

function buildAcross2()
{
 var result = "";
 result = "<table id='acrossTable' cellSpacing='0'>";
 if (window.navigator.userAgent.indexOf("Firefox")>=1)
 {
     for(j = 0; j < xGame.length; j++)
     {
      result += "<tr><td>";
      result += "<table class='nolight' id='x" + xGame[j].id + "' onclick=\"hintClick('" + "x" + xGame[j].id + "')\" onmouseover=\"showPopupText1(event)\" onmouseout=\"hidePopupText(event)\"><tr>";
      result += "<td class='cindex'>" + parseInt(xGame[j].id, 10) + "</td><td id='tdx" + xGame[j].id + "'>" + xGame[j].hint + "</td>";
      result += "</tr></table>";
      result += "</td></tr>";
     }     
 }
 else
 {
     for(i = 0; i < xGame.length; i++)
     {
      result += "<tr><td>";
      result += "<table class='nolight' id='x" + xGame[i].id + "' onclick=\"hintClick('" + "x" + xGame[i].id + "')\" onmouseover=\"showPopupText('" + "tdx" + xGame[i].id + "')\" onmouseout=\"hidePopupText(event)\"><tr>";
      result += "<td class='cindex'>" + parseInt(xGame[i].id, 10) + "</td><td id='tdx" + xGame[i].id + "'>" + xGame[i].hint + "</td>";
      result += "</tr></table>";
      result += "</td></tr>";
     }
 }
 result += "</table>";
 return result;
}

function buildDown2()
{
 var result = "";
 result = "<table id='downTable' cellSpacing='0'>";
 if (window.navigator.userAgent.indexOf("Firefox")>=1)
 {
     for(j = 0; j < yGame.length; j++)
     {
      result += "<tr><td>";
      result += "<table class='nolight' id='y" + yGame[j].id + 
       "' onclick=\"hintClick('" + "y" + yGame[j].id + "')\" onmouseover=\"showPopupText1(event)\" onmouseout=\"hidePopupText(event)\"><tr>";
      result += "<td class='cindex'>" + parseInt(yGame[j].id, 10) + 
      "</td><td id='tdy" + yGame[j].id + "'>" + yGame[j].hint + "</td>";
      result += "</tr></table>";
      result += "</td></tr>";
     } 
 }
 else
 {
     for(i = 0; i < yGame.length; i++)
     {
      result += "<tr><td>";
      result += "<table class='nolight' id='y" + yGame[i].id + 
       "' onclick=\"hintClick('" + "y" + yGame[i].id + "')\" onmouseover=\"showPopupText('" + 
       "tdy" + yGame[i].id + "')\" onmouseout=\"hidePopupText(event)\"><tr>";
      result += "<td class='cindex'>" + parseInt(yGame[i].id, 10) + 
      "</td><td id='tdy" + yGame[i].id + "'>" + yGame[i].hint + "</td>";
      result += "</tr></table>";
      result += "</td></tr>";
     }
 }
 result += "</table>";
 return result;
}


function showPopupText1(e)
{
    document.getElementById('dypopLayer').setAttribute("onmouseover", "javascript:displayPopLayer();");
    document.getElementById('dypopLayer').setAttribute("onmouseout", "javascript:hidePopLayer();");
    var o = e.target;
    MouseX=e.clientX ;
    MouseY=e.clientY;    
    curShow=setTimeout("showIt('" + o.id + "')",tPopWait);    
}

function showPopupText(cid)
{
    e = window.event;
    var o = e.srcElement;
    MouseX=e.clientX ;
    MouseY=e.clientY;    
    curShow=setTimeout("showIt('" + cid + "')",tPopWait);
}

function showIt(cid)
{
    var odyp = document.getElementById('dypopLayer');   
    odyp.style.display='';
    MouseY -= 15;
    if (window.navigator.userAgent.indexOf("Firefox")>=1)
    {
       var popWidth = odyp.offsetWidth;
       var popHeight = odyp.offsetHeight;
       if ( MouseX+12+popWidth > document.body.clientWidth ) 
       {
          popLeftAdjust = -popWidth-24;
       }
       else 
       {
         popLeftAdjust=0;
       }
        
       if ( MouseY+12+popHeight > document.body.clientHeight )
       {
         popTopAdjust=-popHeight-24;
       }
       else
       {
         popTopAdjust=0;
       }
       var FFMouseX = MouseX+12+document.body.scrollLeft+popLeftAdjust;
       var FFMouseY = MouseY+12+document.body.scrollTop+popTopAdjust;
       odyp.style.left = FFMouseX + "px";
       odyp.style.top = FFMouseY + "px"; 
    }
    else
    {
      odyp.style.left = MouseX + "px";
      odyp.style.top = MouseY + "px";
    }    
    var searchkey = document.getElementById(cid).innerText;
    searchkeyword = "http://search.msn.co.uk/results.aspx?q=" + searchkey + "&form=QBRE";
}

function hidePopupText(e)
{
 document.getElementById('dypopLayer').style.display = "none";
}

function opensearch(searchkeyword)
{
    window.open(searchkeyword, "_blank");
}