/********
 ****** (C) Wipplinger EDV Dienstleistungen 1999 - 2006
 **** All Rights Reserved
 ** Confidential
 *
 *
 * PROJECT:      CaveSeekers
 * AUTHOR:       Jürgen L. Wipplinger
 * LANGUAGE:     Java Script
 * DATE:         01.10.2006
 * STATE:        STILL CRITICAL
 *
 ******************************************************************************/

 /**
  * Common Ajax Wrapper used here and there on www.CaveSeekers.com.
  * 
  * Typical usage:
  * 
  *    var myAjax = new Ajax();
  *    myAjax.url="php/classes/Geheimdienst.php";
  *    myAjax.params="onlineUsers=1";
  *    myAjax.bypassBrowserCache=true;
  *    myAjax.onSuccess=successUpdateOnlineUsers;
  *    myAjax.asynchronous=true; (default)
  *    myAjax.doRequest();
  * 
  */
  
function Ajax() 
{
  this.url="";
  this.params="";
  this.bypassBrowserCache=false;
  this.method="GET";
  this.onSuccess=null;
  this.asynchronous=true;
  
  // Default Error Function -> Alert
  this.onError=function (msg) 
  {
    // alert(msg)
  }
}


/**
function Func1()
{
alert("Delayed 3 seconds");
} 
function Func1Delay()
{
setTimeout("Func1()", 3000);
}

**/
Ajax.prototype.doRequest=function() 
{

  // Step 001: Checking Parameters

  if (!this.url) 
  {
    this.onError("INTERNAL ERROR: No Target URL specified. Asyncronous request cancelled.");
    return false;
  }

  if (!this.method) 
  {
    this.method="GET";
  } 
  else 
  {
    this.method=this.method.toUpperCase();
  }

  // Step 002: Providing access to this class for our readyStateHandler

  var _this = this;

  // Step 003: Creating XMLHttpRequest Object  

  var xmlHttpRequest=getXMLHttpRequest();
  
  if (!xmlHttpRequest) 
  {
    this.onError("AJAX PROBLEM: Unable to create XMLHttpRequest-Objekt.");
    return false;
  }

  // Step 004: BrowserCache-Problem.
  //           If bypassBrowserCache is set to true, a dummy-variable is appended which
  //           carries the current time in ms as value. This prevents browsers from caching
  //           our requests and not to forward them to the server.

  if (this.bypassBrowserCache == true)
  {
  	if (this.params.length > 0)
  	{
  		this.params += "&dummy="+new Date().getTime();
  	}
  	else
  	{
  		this.params += "dummy="+new Date().getTime();
  	}
  }

  // Step 005: Get or Post

  switch (this.method) 
  {
    case "GET": xmlHttpRequest.open(this.method, this.url+"?"+this.params, this.asynchronous);
                xmlHttpRequest.onreadystatechange = readyStateHandler;
                xmlHttpRequest.send(null);
  				break;
    case "POST": xmlHttpRequest.open(this.method, this.url, this.asynchronous);
                 xmlHttpRequest.onreadystatechange = readyStateHandler;
                 xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                 xmlHttpRequest.send(this.params);
                 break;
  }  
  
  if (this.asynchronous == false)
  {
    readyStateHandler();
  }


  // Step 006: local readyStateHandler

  function readyStateHandler() 
  {
    if (xmlHttpRequest.readyState < 4) 
    {
      return false;
    }
    if (xmlHttpRequest.status == 200 || xmlHttpRequest.status==304) 
    {
      if (_this.onSuccess) 
      {
        _this.onSuccess(xmlHttpRequest.responseText, xmlHttpRequest.responseXML);
      }
    } 
    else 
    {
      if (_this.onError) 
      {
        _this.onError("["+xmlHttpRequest.status+" "+xmlHttpRequest.statusText+"] AJAX-PROBLEM while transferring data.");
      }
    }
  }
}

//Returns a XMLHttpRequest Object - regardless of which browser is in use.
function getXMLHttpRequest() 
{
  if (window.XMLHttpRequest) 
  {
    //XMLHttpRequest fÃ¼r Firefox, Opera, Safari, ...
    return new XMLHttpRequest();
  } 
  else 
  if (window.ActiveXObject) 
  {
    try 
    {   
      //XMLHTTP (neu) fÃ¼r Internet Explorer 
      return new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch(e) 
    {
      try 
      {        
        //XMLHTTP (alt) fÃ¼r Internet Explorer
        return new ActiveXObject("Microsoft.XMLHTTP");  
      } 
      catch (e) 
      {
        return null;
      }
    }
  }
  return false;
}
