/*
SuDoku Solver by Logic v1.0 - Utility Routines
Copyright (C) Peter Wake 2005

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

The GNU General Public License can be seen at
  http://www.gnu.org/licenses/gpl.html

Or contact the author via the website link at
  http://www.sudokusolver.co.uk
*/
Date.prototype.addDays=function(d)
{
  with(this) setTime(getTime()+d*86400000);
}
String.prototype.trim=function()
{
  return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.anyMatch=function(compareString)
{
  var i,c;

  for(i=0;i<compareString.length;i++)
  {
    c=compareString.charAt(i);
    if(this.indexOf(c)>=0) return true;
  }
  return false;
}
String.prototype.firstMatch=function(compareString)
{
  var i,c;

  for(i=0;i<compareString.length;i++)
  {
    c=compareString.charAt(i);
    if(this.indexOf(c)>=0) return c;
  }
  return false;
}
Object.prototype.complexCopyOf=function(copyTo)
{
  var el;
  for(el in this)
  {
      copyTo[el]=this[el].copyOf();
  }
  return copyTo;
}
Object.prototype.copyOf=function()
{
  if(this.constructor==Function) return this.constructor;
  if(this.constructor==Boolean || this.constructor==Number || this.constructor==String) return this.valueOf();
  if(this.constructor==Array) return this.complexCopyOf(new Array());
  if(this.constructor==Date) return new Date(this.getTime());
  return this.complexCopyOf(new this.constructor);
}
Array.prototype.showHTML=function()
{
  var i,a0;

  a0="";
  for(i=0;i<this.length;i++)
  {
    a0+=this[i].showHTML();
  }
  return a0;
}
function setCookie(name,value,daysToExpiry)
{
  var expDate;
  
  expDate=new Date();
  expDate.addDays(daysToExpiry);
  document.cookie  = name+"="+escape(value)+"; expires=" + expDate.toGMTString() + ";"
}
function getCookie(cookieName)
{
  var dc,valuePairStringArray,i,valuePairArray;
  
  if(document.cookie)
  {
    dc=document.cookie;
    valuePairStringArray=dc.split(";");
    for(i=0;i<valuePairStringArray.length;i++)
    {
      valuePairArray=valuePairStringArray[i].split("=");
      if(valuePairArray[0].trim()==cookieName) return valuePairArray[1].trim();
    }
  }
  return null;
}
function getSudokuString(delimeter)
{
  var a0,x,y,formEl,chr;

  a0="";
  for(y=0;y<9;y++)
  {
    for(x=0;x<9;x++)
    {
      formEl=document.getElementById("sgd_"+x+"_"+y);
      chr=formEl.value+" ";
      chr=chr.charAt(0);
      if(chr==" ") chr="_"
      a0+=chr.charAt(0);
    }
    if(y!=8) a0+=delimeter;
  }
  return a0;
}
function isValidSudokuString(a0,delimeter)
{
  var a1,sArr,x,y,chr;

  //first check the string is valid
  a1="123456789_.";
  sArr=a0.split(delimeter);
  if(sArr.length!=9) return false;
  for(y=0;y<9;y++)
  {
    if(sArr[y].length!=9) return false;
    for(x=0;x<9;x++)
    {
      chr=sArr[y].charAt(x);
      if(a1.indexOf(chr)==-1) return false;
    }
  }
  return true;
}

function setSudokuString(a0,delimeter)
{
  var sArr,x,y,formEl,chr;

  //first check the string is valid
  if(!isValidSudokuString(a0,delimeter)) return false;

  sArr=a0.split(delimeter);

  //now fill the grid
  for(y=0;y<9;y++)
  {
    for(x=0;x<9;x++)
    {
      chr=sArr[y].charAt(x);
      if(chr=="_") chr="";
      if(chr==".") chr="";
      formEl=document.getElementById("sgd_"+x+"_"+y);
      formEl.value=chr;
    }
  }
  return true;
}
function saveSudokuCookie()
{
  var a0;
  a0=getSudokuString("+");
  setCookie("sudoku",a0,365);
  if(a0!=getCookie("sudoku")) alert(mlText.cookie_not_set);
}
function loadSudokuCookie()
{
  var a0;
  a0=getCookie("sudoku");
  if(a0==null) return alert(mlText.cookie_unavailable);
  var set = setSudokuString(a0,"+");
  if(set==false) alert(mlText.cookie_invalid);
}
function saveCurrentGrid()
{
  var a0;
  a0=getSudokuString("+");
  setCookie("currentgrid",a0,0.1); //just keep for a couple of hours
}
function currentGridWatch()
{
  //uncomment these to help with debugging
  //alert("Not watching!");
  //return;
  saveCurrentGrid();
  setTimeout("currentGridWatch()", 2000)
}
function loadCurrentGrid()
{
  var a0;
  a0=getCookie("currentgrid");
  if(a0==null) return false;
  setSudokuString(a0,"+");
  return true;
}
function enterSudoku()
{
  var a0,done,promptStr;
  done=false;
  a0=getSudokuString("+");
  promptStr=mlText.string_format_message;
  while(done==false)
  {
    a0=prompt(promptStr, a0);
    if (a0==null) return;
    done=setSudokuString(a0,"+");
    if(done==false)
    {
      done=setSudokuString(a0,"x");
      if(done==false) promptStr=mlText.string_error_in_format;
    }
  }
}
function createSudokuString()
{
  var a0;
  a0=prompt(mlText.copy_string_to_clipboard, getSudokuString("+"));
}
function emailer()
{
  //trying to prevent spam
  var a0="mailto:comments";
  a0+="@";
  a0+="sudokusolver.co.uk?body=%0a%0aStart Sudoku:%0a%0a"+getSudokuString("%0a")+"%0a%0aCookie String:%0a"+getSudokuString("+")+"%0a";
  window.location=a0;
}

var mostPopularGrid; //global variable
var sudokuLoaded=false;
var mostPopularGridLoaded=false;

function setTodaysMostPopularGrid()
{
  var popFrameEl=document.getElementById("mostPopularGridFrame");
  var oDoc = (popFrameEl.contentWindow || popFrameEl.contentDocument);
  if (oDoc.document) oDoc = oDoc.document;
  mostPopularGrid=oDoc.body.innerHTML;
  if(sudokuLoaded==true) setSudokuString(mostPopularGrid,"x");
  if(isValidSudokuString(mostPopularGrid,"x")) mostPopularGridLoaded=true;
}
function submitGridString(a0,d,g,s)
{
  strPage = "submitGrid2.php?grid="+a0+"&difficulty="+d+"&guesses="+g+"&solutions="+s;
  var submitFrameEl=document.getElementById("submitGridFrame");
  if(submitFrameEl) submitFrameEl.src=strPage;
}
function submitGrid(d,g,s)
{
  var a0=getSudokuString("x");
  submitGridString(a0,d,g,s);
}
function goToPage(pageURL)
{
  document.location=mlText.language_precursor+pageURL;
}
