// global xmlHttp object
var xmlHttp = false;

// getXMLRequester
// Instantiates a new xmlHttp object
// Returns xmlHttp request object or false
function getXMLRequester()
{
  var xmlHttp = false;

  // try to create a new instance of the xmlhttprequest object
  try
  {
    // Internet Explorer
    if (window.ActiveXObject)
    {
      // Try to use the latest msxml dll
      for (var i = 5; i; i--)
      {
        if (i == 2)
        {
          // loading of a newer version of msxml dll (msxml3 - msxml5) failed
          // use fallback solution
          // (old style msxml version independent, deprecated)
          xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
          break;
        }

        try
        {
          xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
          break;
        }
        catch (excNotLoadable)
        {
          xmlHttp = false;
        }
      }
    }
    // Mozilla, Opera und Safari
    else if (window.XMLHttpRequest)
    {
      xmlHttp = new XMLHttpRequest();
    }
  }
  // Loading of xmlhttp object failed
  catch (excNotLoadable)
  {
    xmlHttp = false;
  }
  return xmlHttp;
}


// sendRequest
// Send the request to the server
// Parameters:
// strSource = URL to load
// strID = ID of the object to replace
function sendRequest (strSource, strID)
{
  this.strID = strID;
  this.strSource = strSource;

  // Clean an already existing xmlHttp object
  if (xmlHttp)
  {
    xmlHttp.abort();
    xmlHttp = false;
  }

  // Create a new instance of xmlHttp object
  xmlHttp = getXMLRequester();
  if (!xmlHttp)
  {
    alert ("ERROR: Could not create AJAX object");
    return (false);
  }

  // Open the connection
  xmlHttp.open ("GET", strSource, true);

  // Set OnLoad event-handler
  xmlHttp.onreadystatechange = function () {processResponse();};

  // send request to server
  xmlHttp.send (null);

  return (true);
}


// processResponse
// Processes the response of the request by sendRequest
// Parameters:
// strID = ID of the object to replace
function processResponse (strID)
{
  // status 0 UNINITIALIZED open() has not been called yet.
  // status 1 LOADING send() has not been called yet.
  // status 2 LOADED send() has been called, headers and status are available.
  // status 3 INTERACTIVE Downloading, responseText holds the partial data.
  // status 4 COMPLETED Finished with all operations.
  switch (xmlHttp.readyState)
  {
  case 0: // UNINITIALIZED open() has not been called yet.
  case 1: // LOADING send() has not been called yet.
  case 2: // LOADED send() has been called, headers and status are available.
  case 3: // INTERACTIVE Downloading, responseText holds the partial data.
    break;
  case 4: // COMPLETED Finished with all operations.
    /*
    if (xmlHttp == false)
    {
      break;
    }
    else if (xmlHttp.status != 200)
    */
    if (xmlHttp.status != 200)
    {
      // Loading not successfull, e.g. page not available
      if (window.handleAJAXError)
      {
        handleAJAXError (xmlHttp, strID);
      }
      else
      {
        alert ("ERROR with "+this.strSource);
        alert ("HTTP Status="+xmlHttp.status);
        /*
        var alertMsg = "ERROR:\nHTTP Status=" + xmlHttp.status;
        alertMsg = alertMsg + "\nStatus Text:\n" + xmlHttp.statusText;
        alert (alertMsg);
        */
      }
    }
    else
    {
      document.getElementById (this.strID).innerHTML=xmlHttp.responseText;
      evalScript (xmlHttp.responseText);
      locateXY (this.strID);
    }
  }
}


function evalScript(scripts)
{
  try
  {
    if (scripts != '')
    {
      var script = "";
      scripts = scripts.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi,
                   function()
                   {
                     if (scripts !== null) script += arguments[1] + '\n';
                     return '';
                   });
      if (script) (window.execScript) ? window.execScript(script)
                                      : window.setTimeout(script, 0);
    }
    return false;
  }
  catch(e)
  {
    alert(e)
  }
}


// locateElement
// Scroll to object strID
function locateXY (strID)
{
  var obj = document.getElementById (strID);

  var x = 0;
  var y = 0;

  while (obj)
  {
    x += obj.offsetLeft;
    y += obj.offsetTop;
    obj = obj.offsetParent;
  }

  window.scrollTo(x, y);
}

