function slide(id) {
      if (document.getElementById(id).style.display == "none")
         {document.getElementById(id).style.display = "block"}
      else
         {document.getElementById(id).style.display = "none"}
      }
	  
var xmlHttp = createXmlHttpRequestObject(); 

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
 
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function process()
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // initiate reading a file from the server
      xmlHttp.open("GET", "/xml/list.php?id=" + dataId, 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() 
{
  // 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
        handleServerResponse2();
      }
      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 handleServerResponse2()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;  
  // obtain arrays with book titles and ISBNs 
  author = xmlRoot.getElementsByTagName("author");
  date = xmlRoot.getElementsByTagName("date");
  comment = xmlRoot.getElementsByTagName("comment");
  // generate HTML output
  var html = "";  
  // iterate through the arrays and create an HTML structure
  if(author.length > 0) html = '';
  for (var i=0; i<author.length; i++) {
  var style = "";
  var style2 = "";
  var s = i%2;
    if(s == 0) { style = ''; style2 = ' style="color: #0c72a2;"';}
	html += '<div class="commentblock" style="'+ style +'">' + '<span class="author" '+ style2 +' >' + author.item(i).firstChild.data + '</span> в ' + '<span class="date">' + date.item(i).firstChild.data + '</span>' + '<div class="comment">' + comment.item(i).firstChild.data + '</div>' + '</div>';
    
			 }
  // obtain a reference to the <div> element on the page
  myDiv = document.getElementById("commentsAjax");
  // display the HTML output
  myDiv.innerHTML = html;
  
}
function addcomment() {

	comment = encodeURIComponent(document.getElementById("comment-form").value);
    author = encodeURIComponent(document.getElementById("author-form").value);
	
	
	
	code = encodeURIComponent(document.getElementById("security_code").value);
	
	
	document.getElementById("butLoad").innerHTML = 'Подождите...';
    

	
    // execute the quickstart.php page from the server
    
      var url = "/xml/comment.php";
var params = "id=" + dataId + "&comment=" + comment + "&author=" + author  + "&code=" + code;
xmlHttp.open("POST", url, true);

//Send the proper header information along with the request
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");


    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponseAddComment;
    // make the server request
    xmlHttp.send(params);
}
function handleServerResponseAddComment() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp.responseXML;
      // obtain the document element (the root element) of the XML structure
      xmlDocumentElement = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      // the the document element
      statuss = xmlDocumentElement.firstChild.data;
      var button = '<span class="button3" onclick="addcomment();">Добавить комментарий</span>';
      document.getElementById("commentForm").style.opacity = "1";	
 if(statuss == 0) { 
	 process();
	 document.getElementById("comment-form").value = '';
	 document.getElementById("security_code").value = '';
	 document.getElementById("butLoad").innerHTML = '';
	 
	 document.getElementById("error").innerHTML = '<div class="success">Ваш комментарий добавлен</span>';
	  
	 }
	 else {
	   document.getElementById("butLoad").innerHTML = '';
      document.getElementById("error").innerHTML = '<ul>' + statuss + '</ul>';
	  
	 }
	 
	  
      
      // restart sequence
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}
function ctrlEnter(event)
    {
    if((event.ctrlKey) && ((event.keyCode == 0xA)||(event.keyCode == 0xD)))
        {
        addcomment();
        }
    }
