
function addcomment() {

	comment = encodeURIComponent(document.getElementById("comment-form").value);
    author = encodeURIComponent(document.getElementById("author-form").value);
	
	email = encodeURIComponent(document.getElementById("email-form").value);
	
	code = encodeURIComponent(document.getElementById("codefromimg").value);
	type = encodeURIComponent(document.getElementById("type-form").value);
	
	document.getElementById("butLoad").innerHTML = '<i>Подождите...</i>';
    
	document.getElementById("add-comment").style.opacity = "0.7";
	document.getElementById("add-comment").style.filter = "alpha(opacity=70)";
	
    // execute the quickstart.php page from the server
    
      var url = "/xml/comment.php";
var params = "id=" + dataId + "&comment=" + comment + "&author=" + author + "&email=" + email + "&code=" + code + "&type=" + type;
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;
      
      document.getElementById("add-comment").style.opacity = "1";	
 if(statuss == 0) { 
	 process();
	 document.getElementById("comment-form").value = '';
	
	 
	 
	 document.getElementById("butLoad").innerHTML = '<div class="success">Ваш комментарий добавлен</span>';
	  
	 }
	 else {
	
      document.getElementById("butLoad").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 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()
{
  
  var xmlResponse = xmlHttp.responseXML;

  xmlRoot = xmlResponse.documentElement;  
   
  commentid = xmlRoot.getElementsByTagName("commentid");
  author = xmlRoot.getElementsByTagName("author");
  date = xmlRoot.getElementsByTagName("date");
  comment = xmlRoot.getElementsByTagName("comment");

  reg = xmlRoot.getElementsByTagName("reg");
  ava = xmlRoot.getElementsByTagName("ava");
  var html = "";  
 
  for (var i=0; i<author.length; i++) {
  var style = "";
  var style2 = "";
  var s = i%2;
  var id = '';
  var username = '';
    var avatar = '';  
    if(reg.item(i).firstChild.data != '0') {
        id = reg.item(i).firstChild.data;
        username = '<a href="/user/' + id + '/" class="author">'  + author.item(i).firstChild.data + '</a>';
	} else  username = '<span class="author"' + style2 + '>' + author.item(i).firstChild.data + '</span>';
 
 if(ava.item(i).firstChild.data != '0') 
   avatar = '<td width="60" valign="top" style="text-align: center"><a href="/user/' + id + '/"><img src="/i/user/small/' + ava.item(i).firstChild.data + '"  /></a></td>';
  
   html += '<div id="' + commentid.item(i).firstChild.data + '" class="commentary"><table width="100%"><tr>' + avatar + '<td  valign="top"> '+ username  + ' <span class="date">' 
   + date.item(i).firstChild.data + '</span>' + '<div class="comment">' 
   + comment.item(i).firstChild.data + '</div>' + 
   '<div id="white2">&nbsp;</div></td></tr></div>';
    
			 }
  // obtain a reference to the <div> element on the page
  myDiv = document.getElementById("comments");
  // display the HTML output
  myDiv.innerHTML =  myDiv.innerHTML + html;
  
}
