var xmlHttp;

function getReactions( url )
{
  /* create a XmlHttp object */  
  xmlHttp = GetXmlHttpObject()
  if (xmlHttp == null)
  {
    alert ("Foutmelding: uw browser biedt geen ondersteuning voor HTTP Request!");
    return;
  }
    
  /* send the request to the server */
  xmlHttp.onreadystatechange = showReactions;
  xmlHttp.open("GET",url,true);
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
  xmlHttp.send( null ); 
}

function showReactions()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
    /* load the menu items container */
    var panel = MM_findObj("reaction_panel");
    panel.innerHTML = xmlHttp.responseText;  
  }  
}

function postReaction( url )
{
  /* create a XmlHttp object */  
  xmlHttp = GetXmlHttpObject()
  if (xmlHttp == null)
  {
    alert ("Foutmelding: uw browser biedt geen ondersteuning voor HTTP Request!");
    return;
  }
  
  /* load the reaction of the visitor */
  var reac_text = MM_findObj( "reaction_textarea" );  
  var reaction = escape(reac_text.value);
  
  /* send the request to the server */
  xmlHttp.onreadystatechange = updateReactionsPanel;
  xmlHttp.open("POST",url,true);
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
  xmlHttp.send( "bericht=" + reaction );   
}

function updateReactionsPanel()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
    /* load the response code */
    var response_msg = xmlHttp.responseText;
    var response_code = response_msg.substr(0,2);
    var reponse_text = response_msg.substr(3);
    
    if ( response_code == "OK" )
    {
      /* load the menu items container */
      var panel = MM_findObj("reaction_panel");
      panel.innerHTML = reponse_text;  
      
      /* clear the textarea */
      var reac_text = MM_findObj( "reaction_textarea" ); 
      reac_text.value = "";      
    }
    else if ( response_code == "ER" )
    {
      /* show an error */
      alert( "Foutmelding: " + reponse_text );
      var reac_text = MM_findObj( "reaction_textarea" ); 
      reac_text.focus();
    }
  }  
}

function removeReactionById( id )
{
  /* create a XmlHttp object */  
  xmlHttp = GetXmlHttpObject()
  if (xmlHttp == null)
  {
    alert ("Foutmelding: uw browser biedt geen ondersteuning voor HTTP Request!");
    return;
  }
   
  /* send the request to the server */
  xmlHttp.onreadystatechange = showRemoveReactionByIdResponse;
  xmlHttp.open("GET","http://www.uwhondenkat.nl/ajax/reacties.php?action=remove&id=" + id,true);
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
  xmlHttp.send( null );
}

function showRemoveReactionByIdResponse()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
    /* load the response code */
    var response_msg = xmlHttp.responseText;
    var response_code = response_msg.substr(0,2);
    var reponse_text = response_msg.substr(3); 
        
    /* show the result */
    if ( response_code == "OK" ) 
    {
      /* show confirmation */
      alert("De reactie is verwijderd!");
      
      /* redirect */
      window.location.reload( true );
    }  
    else
    {
      alert("Foutmelding: " +  reponse_text );
    } 
  }
}
