// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();


// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
                                    'MSXML2.XMLHTTP.5.0',
                                    'MSXML2.XMLHTTP.4.0',
                                    'MSXML2.XMLHTTP.3.0',
                                    'MSXML2.XMLHTTP',
                                    'Microsoft.XMLHTTP');
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// read a file from the server
function process(paramm)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // initiate reading a file from the server
	  if(paramm=='next'){
		  		  xmlHttp.open("GET", "books1.php?id=" + document.f.www.value +"", true);
      xmlHttp.onreadystatechange = handleRequestStateChange1;
      xmlHttp.send(null);
	  }else{
      xmlHttp.open("GET", "books.php?id=" + paramm +"", true);
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(null);
	  }
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChange() 
{
	 if (document.f.g1.value=='false') 
  {  myDiv1 = document.getElementById("myDivElement");
  myDiv1.innerHTML = "<center><br><p><p><img src=/iko/loading1.gif align=center><br><br></center>";
  }
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponse();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
    }
  }
}

function handleRequestStateChange1() 
{
	 if (document.f.g1.value=='true') 
  {  myDiv2 = document.getElementById("anekdot");
  myDiv2.innerHTML = "<center><br><img src=/iko/loading27.gif align=center><br><br></center>";
  }
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponse1();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
    }
  }
}
 
// handles the response received from the server
function handleServerResponse()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  if(!xmlResponse || !xmlResponse.documentElement)
  throw("Bad xml stryktyra XML:\n" + xmlresponse.Text);
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if(rootNodeName == "parsererror")
   throw("Bad xml stryktyra XML:\n" + xmlresponse.Text);
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;  
  // obtain arrays with book titles and ISBNs 
  titleArray = xmlRoot.getElementsByTagName("title");
  linkArray = xmlRoot.getElementsByTagName("link");
   descriptionArray = xmlRoot.getElementsByTagName("description");
   imgArray = xmlRoot.getElementsByTagName("img");
  // generate HTML output
  var html = "";  
  // iterate through the arrays and create an HTML structure
  for (var i=0; i<titleArray.length; i++)
     html += "<table width=100%  cellpadding=5><tr><td><A href=\"go.php?go=" + linkArray.item(i).firstChild.data + "\" target=_blank ><h3>" + titleArray.item(i).firstChild.data + "</h3></a><font class=title2><span class=\"imm\"><img src=" + imgArray.item(i).firstChild.data + " align=left>" + descriptionArray.item(i).firstChild.data + "</span></font></td></tr></table>";
   
   // html += titleArray.item(i).firstChild.data + 
     //       ", " + isbnArray.item(i).firstChild.data + "<br/>";
  // obtain a reference to the <div> element on the page
  myDiv = document.getElementById("myDivElement");
  // display the HTML output
  myDiv.innerHTML = html;
}
function handleServerResponse1()
{
   response = xmlHttp.responseText;
 
  var html = "";  
  
     html += response;
	

  myDiv = document.getElementById("anekdot");

  myDiv.innerHTML = html;
  document.f.g1.value='false';
}
