//  Javascript file
var xmlHttp;
var someVariable = "post";

function showTheName(str, str2) {
  /*  str = the source and str2 = a value chosen, if there is one */
  //alert(str+" "+str2);
  if (str.length == 0) { 
    document.getElementById("ajaxinfo").innerHTML="Make selection again . . .";
    return;
  }
  xmlHttp=GetXmlHttpObject()
  if (xmlHttp == null) {
    alert ("Browser does not support HTTP Request");
    return;
  }
  
  if (someVariable == "get") {
  /* Using GET - if retrieving (getting) data. */
    var url = str;
    url = url + "?q=" + str2;
    url =url + "&sid=" + Math.random();
    xmlHttp.open("get", url, true);
    //alert("after call to xmlHttp.open: url = "+url);
    xmlHttp.onreadystatechange = stateChanged;
    //alert("after call to stateChanged: str = "+str);
    xmlHttp.send(null);
    //alert("after call to xmlHttp.send: ajaxthename = "+document.getElementById("ajaxthename").innerHTML);
    //alert("xmlHttp.responseText = "+xmlHttp.responseText);
  }
  else {
  /* Using POST - if sending (posting) data*/
    var url = str;
    q = "q="+str2;
    q = q + "&sid=" + Math.random();
    //alert("before call to xmlHttp.open: url = "+url);
    //alert("before call to xmlHttp.open: q = "+q);    
    xmlHttp.open("post", url, true);
    //alert("after call to xmlHttp.open: url = "+url);
    // Send the proper header information along with the request.
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", q.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange = stateChanged;
    //alert("after call to stateChanged: str = "+str);
    xmlHttp.send(q);
    //alert("after call to xmlHttp.send: ajaxthename = "+document.getElementById("ajaxthename").innerHTML);
    //alert("xmlHttp.responseText = "+xmlHttp.responseText);
  }
}
/***
The stateChanged() function executes every time the state of the XMLHTTP object changes.
When the state changes to 4 ("which means 'complete'),
the content of the 'ajaxthename' placeholder is filled with the response text.  
***/
function stateChanged() {

  debugVar = "Probably not connected to a database.";
  switch(xmlHttp.readyState)
  {
    
    case 0:
      document.getElementById("ajaxinfo").innerHTML = "Uninitialized . . .";
      break;
    /***** NOop case to prevent message from displaying after each 'call'. ****/
    case 1:
      document.getElementById("ajaxinfo").innerHTML =
        "<img src='images/busyrotatearrow.gif' height='40' width='40' alt=''>" +
        "  Loading . . .";
      break;
    case 2:
      document.getElementById("ajaxinfo").innerHTML =
        "<img src='images/busyrotatearrow.gif' height='40' width='40' alt=''>" +
        "  Almost got it . . .";
      break;
    case 3:
      document.getElementById("ajaxinfo").innerHTML = "Got it . . .";
      break;
    case 4:
      // readyState = complete, so now check status too.
      if (xmlHttp.status == 200) {
        document.getElementById("ajaxinfo").innerHTML = xmlHttp.responseText;
      }
      else {
        document.getElementById("ajaxinfo").innerHTML=debugVar;
      }
      break;
    /***** NOop case to prevent message from displaying after each 'call'. ****/
    default:
      document.getElementById("ajaxinfo").innerHTML = "No Connection ? . . .";
  }
} 
/***
The purpose of the function is to solve the problem of creating different
XMLHTTP objects for different browsers.
***/
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;
}

