// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject(); 

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;
} 

// make asynchronous HTTP request using the XMLHttpRequest object 
function add_new_comment(add_path,item_id,name,message,code)
{

document.getElementById("comment-input-name").disabled = true;
document.getElementById("comment-input-text").disabled = true;
document.getElementById("button").disabled = true;
document.getElementById("textfield").disabled = true;

  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // execute the quickstart.php page from the server
    var now = new Date();
    params = "item_id=" + item_id+"&name="+name+"&message="+message+"&code="+code;
    xmlHttp.open("POST", add_path+"add_comment.php?&time="+now, true );  
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    // define the method to handle server responses
    xmlHttp.onreadystatechange = display_responce;
    // make the server request
    xmlHttp.send(params);

  }
  else
  {
     setTimeout("add_new_comment('"+add_path+"','"+item_id+"','"+name+"','"+message+"','"+code+"');",1000);
     // if the connection is busy, try again after one second  
     //alert('The server is busy. Try again');
  }

}

function display_responce() 
{
  // 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.responseText;

er = document.getElementById("comments-error");
er.style.visibility = "visible";
er.style.display = "block";
er.innerHTML=xmlResponse;

document.getElementById("comment-input-name").disabled = false;
document.getElementById("comment-input-text").disabled = false;
document.getElementById("button").disabled = false;
document.getElementById("textfield").disabled = false;

var error_text = er.innerHTML.substring(2,3);
if(error_text=="r")
{
}
else
{
document.getElementById('comment-input-name').value='';
document.getElementById('comment-input-text').value='';
document.getElementById('textfield').value='';
}

var now = new Date();
kcaptcha= document.getElementById("kcaptcha");
kcaptcha.src = "../kcaptcha/index.php?time="+now;

    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
} 

// make asynchronous HTTP request using the XMLHttpRequest object 
function refresh_comments(add_path,item_id)
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // execute the quickstart.php page from the server
    var now = new Date();
    xmlHttp.open("GET", add_path+"refresh_comment.php?item_id="+item_id+"&time="+now, true );  
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    // define the method to handle server responses
    xmlHttp.onreadystatechange = display_comments;
    // make the server request
    xmlHttp.send(null);
  }
  else
  {
     // if the connection is busy, try again after one second  
setTimeout("refresh_comments('"+add_path+"','"+item_id+"');",1000);
  }

}

function display_comments() 
{
  // 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.responseText;
document.getElementById("comments-list").innerHTML=xmlResponse ;


er = document.getElementById("comments-error");
var error_text = er.innerHTML.substring(2,3);

if(error_text=="r")
{
}
else
{
setTimeout("clear_error();", 5000);
}
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
} 

function test()
{
 document.getElementById("comment-input-name").disabled = true;
document.getElementById("comment-input-text").disabled = true;
document.getElementById("button").disabled = true;
}

function clear_error()
{
er.style.visibility = "hidden";
er.style.display = "none";
er.innerHTML="";
}