/* getBoard.js -- functions to retrieve Sudoku puzzles,   */
/* solutions and hints from the Daily Sudoku using JSON.  */
/* Copyright (c) Daily Sudoku 2006.  All rights reserved. */
/* http://www.dailysudoku.com/                            */
/* Author: Sam Griffiths-Jones                            */
/* Email: sudoku@dailysudoku.co.uk                        */

var httpRequest;

// work in both IE and firebox 
// stolen from 
// http://developer.apple.com/internet/webcontent/xmlhttpreq.html
function getHTTPObject() {
  var req = false;
  // branch for native XMLHttpRequest object
  if(window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch(e) {
      req = false;
    }
  // branch for IE/Windows ActiveX version
  } else if(window.ActiveXObject) {
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        req = false;
      }
    }
  }
  return req;
}


function getJsonPuzzleByDate( y,m,d,type ) {
   httpRequest = getHTTPObject();
   var url = "/cgi-bin/sudoku/get_board.pl"; // This URL should give us JSON data. 
   window.d = d;
   if( !window.d ) {
     var mydate = new Date();
     y = mydate.getYear();
     if( y<1900 ) {
        y = y + 1900;
     }
     m = mydate.getMonth();
     m = m + 1;
     d = mydate.getDate();
   }

  if( m.length < 2 ) {
     m = "0"+m;
  }

  if( !type ) {
     type = '';
  }

  // Download the JSON data from the server.
  httpRequest.onreadystatechange = parseJsonPuzzle;
  httpRequest.open("GET", url+"?year="+y+"&month="+m+"&day="+d+"&type="+type, true);
  httpRequest.send(null);
}

function getJsonSolution( nums, maskName, show ) {
  httpRequest = getHTTPObject();
  var url = "/cgi-bin/sudoku/get_solution_dummy.pl"; // This URL should give us JSON data. 
  window.nums = nums;
  if( window.nums ) {
    // Download the JSON data from the server.
    httpRequest.onreadystatechange = parseJsonPuzzle;
    httpRequest.open("GET", url+"?numbers="+nums+"&maskname="+maskName+"&show="+show, true);
    httpRequest.send(null);
  }
}

function getJsonHint( nums, maskName ) {
  httpRequest = getHTTPObject();
  var url = "/cgi-bin/sudoku/get_hint_dummy.pl"; // This URL should give us JSON data. 
  window.nums = nums;
  if( window.nums ) {
    // Download the JSON data from the server.
    httpRequest.onreadystatechange = parseJsonHint;
    httpRequest.open("GET", url+"?numbers="+nums+"&maskname="+maskName, true);
    httpRequest.send(null);
  }
}

function parseJsonPuzzle() {
  var boardData = new Object;
  if (httpRequest.readyState == 4) {
    if (httpRequest.status == 200) {
      var jsonData = httpRequest.responseText; 
      eval("boardData = ("+jsonData+")");
      parseBoardData( boardData );
    } else {
      alert('Error: failed to retrieve puzzle.  This could be a temporary network error - please try again later.  If the problem persists then please email sudoku@dailysudoku.co.uk with a description of the error.');
    }
    httpRequest = null;
  }
}

function parseJsonHint() {
  var hintData = new Object;
  if (httpRequest.readyState == 4) {
    if (httpRequest.status == 200) {
      var jsonData = httpRequest.responseText; 
      eval("hintData = ("+jsonData+")");
      parseHintData( hintData );
    } else {
      alert('Error: failed to get hint.  This could be a temporary network error - please try again later.  If the problem persists then please email sudoku@dailysudoku.co.uk with a description of the error.');
    }
    httpRequest = null;
  }
}

