/*+++++++++++++++++++++ 
+ 
+   AJAX FUNCTIONS
+ 
++++++++++++++++++++++*/

/*
 * Returns a new XMLHttpRequest object, or false if this browser
 * doesn't support it
 */
function newXMLHttpRequest() {

  var xmlreq = false;

  if (window.XMLHttpRequest) {

    // Create XMLHttpRequest object in non-Microsoft browsers
    xmlreq = new XMLHttpRequest();

  } else if (window.ActiveXObject) {

    // Create XMLHttpRequest via MS ActiveX
    try {

      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer
      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e1) {

      // Failed to create required ActiveXObject

      try {
        // Try version supported by older versions
        // of Internet Explorer
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (e2) {

        // Unable to create an XMLHttpRequest with ActiveX
      }
    }
  }

  return xmlreq;
}

/*
 * Checks to update the status of new users that are online
 */

// Obtain an XMLHttpRequest instance
var req = newXMLHttpRequest();

function tracker(url) {

  req = newXMLHttpRequest();
  // Set the handler function to receive callback notifications
  // from the request object
  req.onreadystatechange = sendInfo;
  
  // Open an HTTP POST connection to the ajax php script.
  // Third parameter specifies request is asynchronous.
  req.open("GET", url, true);

  // Specify that the body of the request contains form data
  //  req.setRequestHeader("Content-Type", 
  //                     "text/html");

  // Send form encoded data stating that I want to add the 
  // specified item to the cart.
  req.send("");
}

/*
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its XML response to the given handler function.
 * req - The XMLHttpRequest whose state is changing
 * responseXmlHandler - Function to pass the XML response to
 */
function sendInfo()
  {
  if (req.readyState == 1) 
    {
    // document.getElementById("curONLINE").innerHTML="loading...";
	  }

  // If the request's status is "complete"
  if (req.readyState == 4) 
    {
    // Check that a successful server response was received
    if (req.status == 200) 
    {
      // Pass the Answer of the response to the 
      // handler function
	  // document.getElementById('curONLINE').innerHTML = answer;
      
	  } 
   else 
	    {
      // An HTTP problem has occurred
      alert("HTTP error: "+req.status);
      }
    }	
  }

// SEND AHAH REQUEST
function ahah(url,target) {
   // native XMLHttpRequest object
   document.getElementById(target).innerHTML = 'sending...';
   if (window.XMLHttpRequest) {
       req = new XMLHttpRequest();
       req.onreadystatechange = function() {ahahDone(target);};
       req.open("GET", url, true);
       req.send(null);
   // IE/Windows ActiveX version
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
       if (req) {
           req.onreadystatechange = function() {ahahDone(target);};
           req.open("GET", url, true);
           req.send();
       }
   }
}

// RECEIVE AHAH REQUEST
function ahahDone(target) {
   // only if req is "loaded"
   if (req.readyState == 4) {
       // only if "OK"
       if (req.status == 200) {
           document.getElementById(target).innerHTML = req.responseText;
       } else {
           document.getElementById(target).innerHTML="ahah error:\n" +
               req.statusText;
       }
   }
}

// SEARCH USING AHAH
function ahahSearch(type)
   {
   if (type == "labels")
	   {
		 val = document.getElementById('search').value;
		 setTimeout("ahah('scripts/ahah/yp.php?search='+val,'results')",50);
		 }
   }

function ahahTagEdit(id,tagid)
   {
	 
	 val = 'edit'+tagid;
	 val = document.getElementById(val).value;
	 ahah('../ahah/tags.php?edit='+tagid+'&to='+val,id);
	 }