/*
*
* ajax-reader.js (c) 2006 Michael Thompson lMaker@michaelthompson.org
* for the latest info visit http://michaelthompson.org/doc/
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
*/
  var theDiv;
  var theBook;
  var theChapter;
  var thePage;

  var bIE = false;
  var theBuff;
  var theURL;

  var http_request = false;
  function fromServer()
  {
    if( http_request.readyState==1 ) 
    { 
      //alert(1);
      return;
    }
    if( http_request.readyState==2 ) 
    { 
      //alert(2);
      return;
    }

    if( http_request.readyState==3 ) 
    {
      //alert(3);
      return;
    }

    if( http_request.readyState==4 )
    {
      if( http_request.status==200 )
      {
// results from call to the server appear here in http_request.responseText;
// but to Netscape this only returns 4096 bytes at a time
        rsp = http_request.responseText;
        theBuff = theBuff + rsp;
        if( !bIE && rsp.length>0 ) 
        { makeRequest(theURL);
          return;
        }
        rsp = theBuff;

        if( rsp=="__NEXT__" ) 
        {
fillPage('./pt.php',theBook,1 + parseInt(theChapter),0,'chaps');
        }
        else
        {
          var e;
          if( theDiv=='chaps' )
          {
            ar = rsp.split('|');
            e = document.getElementById('title');
            e.innerHTML = ar[0];
            e = document.getElementById('author');
            e.innerHTML = ar[1];
            e = document.getElementById('chaps');
            e.innerHTML = ar[2];
fillPage('./pt.php',theBook,theChapter,thePage,'navbar');
            return;
          }

          e = document.getElementById(theDiv);
          e.innerHTML = rsp;
          if( theDiv=='page' )
          {
document.getElementById('status').innerHTML='';
          }
          if( theDiv=='navbar' ) 
          {
fillPage('./pt.php',theBook,theChapter,thePage,'page');
          }
        }
      }
      else
      {
// uncomment next line if you want to show any error messages
//alert('error: ' + http_request.responseText);
      }
    }
  }

  function makeRequest(url) 
  {
    http_request = false;
    if( window.XMLHttpRequest ) 
    { 
      // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) 
      { http_request.overrideMimeType('text/html');
      }
    } 
    else if (window.ActiveXObject) 
    { // IE
      try 
      { http_request = new ActiveXObject("Msxml2.XMLHTTP");
        bIE = true;
      } 
      catch (e) 
      { try
        { http_request = new ActiveXObject("Microsoft.XMLHTTP");
          bIE = true;
        } catch (e) {}
      }
    }
    if( !http_request ) 
    {
// uncomment next line if you want to show any error messages
//alert('Cannot create XMLHTTP instance');
      return false;
    }
    http_request.onreadystatechange = fromServer;
    http_request.open('GET', url + '&skip=' + theBuff.length, true);
    http_request.send(null);
  }

  function fillPage(server,book,chapter,page,div)
  {
    theDiv = div;
    msg = '<span style="background-color: #ff6666; color: #ffffcc">';
    msg = msg + '&nbsp;&nbsp;processing...&nbsp;&nbsp;';
    msg = msg + '</span>';
    document.getElementById('status').innerHTML=msg;

    theBook = book;
    theChapter = chapter;
    thePage = page;

    url = server + '?book=' + book;
    url = url + '&chapter=' + chapter;
    url = url + '&page=' + page;
    if( theDiv=="chaps" ) url = url + '&x';
    else if( theDiv=="navbar" ) url = url + '&y';

    document.getElementById('the_url').innerHTML='<!--' + url + '-->';
    theBuff = '';
    theURL = url;
    makeRequest(url);

    return false;
  }


