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 = handleRequestStateChangeC;
      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 handleRequestStateChangeC() 
{
  // 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
        handleServerResponseCommentsC();
      }
      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 handleServerResponseCommentsC()
{
  // 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");
  text = xmlRoot.getElementsByTagName("comment");
  comment_id = xmlRoot.getElementsByTagName("comment_id");
   plus = xmlRoot.getElementsByTagName("plus");
  minus = xmlRoot.getElementsByTagName("minus");
  // generate HTML output
  var html = "";  
  // iterate through the arrays and create an HTML structure
  if(author.length > 0) html = '<div class="comments">Комментарии</div>' + '<a onclick="process();" style="cursor: pointer; float: right; color: green">обновить</a><br />';
  for (var i=0; i<author.length; i++) {
  var style = "";
  var style2 = "";
  var s = i%2;
    if(s == 0) { style = 'background: #f7fbf1; border-top: 1px dotted #dadada;border-bottom: 1px dotted #dadada'; style2 = ' style="color: #0c72a2;"';}
	html += '<div class="commentblock" style="'+ style +'">' + '<span class="author"' + style2 + '>' + author.item(i).firstChild.data + '</span> пишет: <br />' + '<span class="date">' + date.item(i).firstChild.data + '</span>' + '<div class="comment">' + text.item(i).firstChild.data + '</div>' + '<div class="likes"><span id="plus' + comment_id.item(i).firstChild.data + '" class="plus">' + plus.item(i).firstChild.data + '</span> <a onclick="like(' + comment_id.item(i).firstChild.data + ', ' + "'" + 'plus' + "'" + ');" style="text-decoration: none"><img src="/style/img/plus.gif" align="top" width="16" /></a> <span class="minus" id="minus' + comment_id.item(i).firstChild.data + '">' + minus.item(i).firstChild.data + '</span>  <a onclick="like(' + comment_id.item(i).firstChild.data + ', ' + "'" + 'minus' + "'" + ');" style="text-decoration: none"><img src="/style/img/minus.gif" align="top" width="16" /></a> <span id="score' + comment_id.item(i).firstChild.data + '"></span></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() {

	comment2 = encodeURIComponent(document.getElementById("comment-form").value);
    author = encodeURIComponent(document.getElementById("author-form").value);
	
	email = encodeURIComponent(document.getElementById("email-form").value);
	
	code = encodeURIComponent(document.getElementById("security_code").value);
	
	
	document.getElementById("butLoad").innerHTML = '<img src="/style/img/loader.gif" />';
    
	 
	
    // execute the quickstart.php page from the server
    
      var url = "/xml/comment.php";
var params = "id=" + dataId + "&comment=" + comment2 + "&author=" + author + "&email=" + email + "&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) 
    {
    
	   var xmlResponse = xmlHttp.responseXML;
 
  xmlRoot = xmlResponse.documentElement;  
   
 
  st = xmlRoot.getElementsByTagName("st");
  smth = xmlRoot.getElementsByTagName("smth");
 
	  
      var button = '<span class="button3" onclick="addcomment();">Добавить комментарий</span>';
 
 if(st.item(0).firstChild.data != 0) { 
  document.getElementById("butLoad").innerHTML = '';
      document.getElementById("error").innerHTML = '<ul>' + st.item(0).firstChild.data + '</ul>';
	  
	  
	 }
	 else {
   document.getElementById("butLoad").innerHTML = '';
	 document.getElementById("comment-form").value = '';
	 document.getElementById("security_code").value = '';
	 document.getElementById("commentsAjax").innerHTML += smth.item(0).firstChild.data;
	 
	 document.getElementById("error").innerHTML = '<div class="success">Ваш комментарий добавлен</div>';
	  
	 }
	 
	  
      
      // 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();
        }
    }

