var xmlHttp;

function SayRespondOnEnter(event,str)
{
   var keynum;

   if (window && window.event) // IE
   {
      keynum = event.keyCode;
   }
   else if (event && event.which) // Netscape/Firefox/Opera
   {
      keynum = event.which;
   }

   
   if (keynum == 13)
   {
      // SayRespond(str);
      // To avoid premature submissions when Enter is pressed
      // on the drop-down, we'll wait a bit and check again.
      setTimeout("CheckResponse('" + escape(str) + "')", 50)
      return false;
   }

   return true;
}

/// Check for the special case of Enter on the drop-down. 
function CheckResponse(str)
{
   newStr = document.getElementById("youSay").value;
   
   // Is the current string the same as the original?
   if (newStr == unescape(str))
   {
      // Probably a vanilla Enter, not on the drop-down. Submit.
      SayRespond(newStr);
   }
}

/// Submit the input to the responder.
function SayRespond(str)
{
   // Don't take one-letter input.  Too silly.
   if (str.length <= 1)
   {
      return;
   }

   xmlHttp = GetXmlHttpObject();
   if (xmlHttp == null)
   {
      alert("Browser does not support HTTP Request");
      return;
   }
   var url = "respond.php";
   url = url + "?youSay=" + str;
   url = url + "&dummy=" + Math.random();
   xmlHttp.onreadystatechange = stateChanged;
   xmlHttp.open("GET", url, true);
   xmlHttp.send(null);

   // Show the wait image
   document.getElementById("theWaitImage").style.visibility = "visible";
   
   document.getElementById("TheResponse").innerHTML += "<font color=red>" + str + "</font><br />";
   scrollChatPanel();
}

/// The response state has changed
function stateChanged()
{
   if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
   {
      document.getElementById("TheResponse").innerHTML += "<font color=blue>" + xmlHttp.responseText + "</font><br />";
      scrollChatPanel();
      
      // Hide the wait image
      document.getElementById("theWaitImage").style.visibility = "hidden";
   }
}

/// Get the response object.
function GetXmlHttpObject()
{
   var xmlHttp = null;

   try
   {
      // Firefox, Opera 8.0+, Safari
      xmlHttp = new XMLHttpRequest();
   }
   catch (e)
   {
      // Internet Explorer
      try
      {
         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
   }
   return xmlHttp;
}

