/*
*   ajax.js
*   Basic methods for asynchronous http communications
*/
var xmlhttp=null; // global XmlHttpRequest object

/* Instantiate the XmlHttpRequest object.
*/
function getXmlParser() {
    var obj = null;
    if (!window.XMLHttpRequest) {
        /*@cc_on @*/
        /*@if (@_jscript_version >= 5)
        try {
            obj = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch(e1) {
            try {
                obj = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch(e2) {
                obj = null
            }
        }
        @end @*/
    } else {
        if(typeof XMLHttpRequest != "undefined") {
            try {
                obj = new XMLHttpRequest();
            }
            catch(e) {
                obj = null;
            }
        }
        else if(window.createRequest) {
            try {
                obj = window.createRequest();
            }
            catch(e) {
                obj = null
            }
        }
    }
    return obj;
}

/* Attach a new handler to the given element and event.
    pElement: which DOM element to attach the event handler to
    pEvent: which event to handle
    pFunc: pointer to the event handler function
*/
function AddEventHandler(pElement,pEvent,pFunc)
{ 
  if (pElement.addEventListener)
  { 
    pElement.addEventListener(pEvent, function(e) {pElement[pFunc](e);}, false);
  }
  else if (pElement.attachEvent)
  {
    pElement.attachEvent("on" + pEvent, function(e) {pElement[pFunc](e);});
  } 
  else
  { 
    var oldHandler = target["on" + pEvent]; 
    if (oldHandler)
    { 
      pElement["on" + pEvent] = function(e){oldHandler(e);pElement[pFunc](e);}; 
    } 
    else
    { 
      pElement["on" + pEvent] = pElement[pFunc]; 
    } 
  } 
}

/* Initiate a simple GET request to the specified url.
    pUrl: absolute url (must be in the same domain as the parent)
    pHandler: pointer to function that will handle the onreadystatechange event
*/
function fetch(pUrl,pHandler)
{
    var rc = false
    if(typeof xmlhttp != "undefined" && xmlhttp != null) {
        try {
            xmlhttp.onreadystatechange = pHandler;
            xmlhttp.open("GET", pUrl, true);
            xmlhttp.send(null);
            rc = true;
        }
        catch(e) {
            // no-op
        }
    }
    return rc;
}



