var Cheated = false;  // did player use one of the solve buttons?
var MouseDown = false; // used to determine whether to activate a square if the mouse is over it
var ColorToChange = 0;

// funtions to prevent text selection
document.onselectstart=new Function('return false');
//function ds(e){return false;}
function ra(){return true;}
//document.onmousedown=ds;
document.onclick=ra;

// Puts an end to sliding no matter where on the webpage the mouse button is released
function endSliding()
{
   MouseDown=false;
   ColorToChange = 0;
}

document.onmouseup=endSliding;

function writeReleaseDate()
{
	document.write(releaseDate);
}

function writeSolveTimesDate()
{
	document.write(solveTimesDate);
}

function CheckTable(){
  var i;
  var j;
  for (i=0; i<(mHeight*mWidth); i++)
  {
    if (cellData[i] === 1)
	{
       if (cellImageData[i] !== 1)
	   {
          alert("Some squares are incorrectly marked. Keep trying!");
          return;
       }
    }
    else // cellData is 0
	{
       if (cellImageData[i] === 1)
	   {
            alert("Some squares are incorrectly marked. Keep trying!");
            return;
       }               
    }
  }
  alert("All squares have been marked correctly!  Now, can you solve the riddle?");
}

function RevealTable()
{
  var i;
  var j;
  Cheated = true;
  for (j = 0; j<mHeight; j++){
    for (i=0; i<mWidth; i++){
      cellI = j*mWidth+i;
      cellN = "sos" + cellI;
      if (cellData[cellI] !== 1)  mSetColor(cellN, blocOColor);
      else mSetColor(cellN, blocXColor);
    }
  }
}
function TrBlocStart() { document.write ('<tr valign="center" align="center">'); }

function TrEnd() { document.write ("</tr>"); }

function SilverBloc() { document.write ('<td bgcolor="'+numFildColor+'" width='+cellSize+' height='+cellSize+'></td>'); }

function BlocNum(j)
{
     document.write ('<td bgcolor="'+numFildColor+'" width='+cellSize+' height='+cellSize+' style="font-size: '+fontSize+'px; color: '+fontColor+'; font-family: Verdana, Arial, Helvetica, sans-serif" onmousedown="cellClick(this)"><b>'+j+'</b></td>');
}

function ImageWhiteBloc(mID,countID)
{
   document.write ('<td id="'+mID+'" onmousedown="cellClick(this)" onmouseover="cellOver(this)" onmouseup="MouseDown=false" bgcolor="'+baseFildColor+'" width='+cellSize+' height='+cellSize+' align="left" valign="top"><font face="Verdana, Arial, Helvetica, sans-serif" color="black" size="2">'+cellLetters.charAt(countID)+'</font></td>'); 
}

function incrementChar(charToIncr)
{
	var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var indexOfChar = alphabet.search(charToIncr);

    if (indexOfChar + 1 < alphabet.length) 
       return(alphabet.charAt(indexOfChar + 1));
	else
	   return(alphabet.charAt(indexOfChar - 25));	
}

function decrementChar(charToDec)
{
	var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var indexOfChar = alphabet.search(charToDec);
    if (indexOfChar - 1 >= 0) 
       return(alphabet.charAt(indexOfChar - 1));
	else
       return(alphabet.charAt(indexOfChar + 25));
}

function decodeAnswer()
{
	var plntxt = "";
	var x;
    var y = sltWrd.length;

	for (x = 0; x < y; x++)	
	{
	    if (x % 2 !== 0)
			plntxt=plntxt + incrementChar(sltWrd.charAt(x));
		else
		    plntxt=plntxt + decrementChar(sltWrd.charAt(x));
	}

	return plntxt;
}

//Changes a cell's color if the user is dragging across multiple cells
function cellOver(gid)
{
	var mID = gid.id;

	if (MouseDown)
	{
		mID = mID.replace("sos","");
		mID = parseInt(mID);

		// don't change black squares yellow
		if((ColorToChange===1) && (cellImageData[mID]===1))
		{
			return;
		}

		// if the origination cell turned yellow, change white cells to yellow when dragging
		if((ColorToChange === 1) && (cellImageData[mID]=== 0))
		{
			cellImageData[mID] = 1; //set the cell data to black so when the next if is proccessed, it changes to yellow
		}
	
		// 
		if (cellImageData[mID] === ColorToChange)
		{
			cellClick(gid); // teat it as if the mouse was clicked
		}
	}
}

function cellClick(gid)
{
  var mID = gid.id;
  mID = mID.replace("sos","");
  mID = parseInt(mID);

  if(MouseDown === false)
  {
	MouseDown = true;
	ColorToChange = cellImageData[mID];  // ColorToChange is acutally one step behind new color
  }

  if (cellImageData[mID] === 0) {cellImageData[mID] = 1; mSetColor(gid.id, blocXColor); return;} // turn a white square black
  if (cellImageData[mID] === 1) {cellImageData[mID] = 2; mSetColor(gid.id, blocOColor); return;} // turn a black sqaure yellow
  if (cellImageData[mID] === 2) {cellImageData[mID] = 0; mSetColor(gid.id, baseFildColor); return;} // turn a yellow square white
}

function startclock()
{
	if (Cheated === false)
	{
		lnclock = new Date();
	}
}
function ClearTable()
{
  var i;
  var j;
  for (j = 0; j<mHeight; j++){
    for (i=0; i<mWidth; i++){
      cellI = j*mWidth+i;
      cellN = "sos" + cellI;
      mSetColor(cellN, baseFildColor);
    }
  }
  for (i=0; i<cellImageData.length; i++)
  {
	cellImageData[i] = 0;
  }
  startclock();
}
function showclock()
{
	lnnow = new Date();	
	lnseconds = Math.round((lnnow.getTime() - lnclock.getTime()) / 1000);

	lnmins = Math.floor(lnseconds / 60);
	lnsecs = lnseconds - (60 * lnmins);
	if (lnsecs < 10)
	{
		lnsecs = "0" + lnsecs;
	}

	return lnmins + ":" + lnsecs;
}

function checkAnswer(form){
	var userAnswer = form.userInput.value.toUpperCase();
	var realAnswer = decodeAnswer();
	if (userAnswer === realAnswer)
	{
	    if (Cheated === false)
	    {
			alert(userAnswer + " is correct!  Your solve time is " + showclock() + ".  Visit the forums to compare against others!");
		}
		else  // Cheated === true
		{
			alert(userAnswer + " is correct!");
		}
	}
	else if (userAnswer==="")
	{
	    alert("Mind's a lot like the answer you gave, eh?");
	}
    else if (userAnswer===sltWrd)
    {
		alert("Please have your cat take a typing class!");
	}
	else
	{
		alert("I'm afraid "+userAnswer+" is incorrect.  Try again!");
	}
	return false;
}

function revealRiddleAnswer()
{
	var realAnswer = decodeAnswer();

	Cheated = true;

	alert("The solution to the riddle is " + realAnswer + ".");	
}

function mSetColor(mF,mS){
  var curElement = document.getElementById(mF);
  curElement.style.backgroundColor = mS;
}
function WriteTable()
{
  var i;
  var j;
  for (i = 0; i<(mHeight*mWidth); i++){cellImageData[i]= 0;}
  document.write ('<TABLE BGCOLOR="'+lineColor+'" BORDER="0" CELLSPACING="1" width='+((mWidth*cellSize+mWidth+1)+(mYw*cellSize+mYw+1))+'height='+((mHeight*cellSize+mHeight+1)+(mXh*cellSize+mXh+1)) +'>'); 
  document.write ('<TR VALIGN=center ALIGN=center>');
  if (mImageHtml !== ""){
    document.write ('<TD COLSPAN="'+mYw+'" ROWSPAN="'+mXh+'" BGCOLOR="'+logoFildColor+'" VALIGN=CENTER>'+ mImageHtml +'</TD>');
  }
  else{
    document.write ('<TD COLSPAN="'+mYw+'" ROWSPAN="'+mXh+'" BGCOLOR="'+logoFildColor+'" VALIGN=CENTER><b>HINT</b><br/>'+ pzHint + '<br/>(' + pzLength + ')</TD>');
  }
  for (i = 0; i<mXw; i++){
    if (cellXNumber[i] === 0) SilverBloc();
    else if (cellXNumber[i] === -1) BlocNum("?");
    else BlocNum(cellXNumber[i]);
  }
  for (j=1; j<mXh; j++){
    TrBlocStart();
    for (i = 0; i<mXw; i++){
      if (cellXNumber[j*mXw+i] === 0) SilverBloc();
      else if(cellXNumber[j*mXw+i] === -1) BlocNum("?");
      else BlocNum(cellXNumber[j*mXw+i]);
    }
    TrEnd();
  }
  for (j = 0; j<mYh; j++){
    TrBlocStart();
    for (i = 0; i<mYw; i++){
      if (cellYNumber[j*mYw+i] === 0) SilverBloc();
      else if (cellYNumber[j*mYw+i] === -1) BlocNum("?");
      else BlocNum(cellYNumber[j*mYw+i]);
    }
    for (i = 0; i<mWidth; i++){
      cellI = j*mWidth+i;
      cellN = "sos" + cellI;
      ImageWhiteBloc(cellN,cellI);
    } 
    TrEnd();
  }
  document.write ('</TABLE>');
  startclock();
}
