
////////////////////////////////////////////////////////////////

function CallAjax( url, query, id, onFeedback )
{
 var request = new AjaxRequest( url, query, id );
 if ( request != null )
 {
  request.onFeedback = onFeedback; 
  request.Perform();
 }
}

////////////////////////////////////////////////////////////////

function CreateRequestAjax()
{
 var httpRequest = null;

 if ( window.XMLHttpRequest )   // Mozilla, Safari, ...
 {
  httpRequest = new XMLHttpRequest();
 } 
 else if (window.ActiveXObject) // IE
 { 
  try 
  {
   httpRequest = new ActiveXObject("Msxml2.XMLHTTP.6.0");
  } 
  catch (e) 
  {
   try 
   {
    httpRequest = new ActiveXObject("MSXML2.XMLHTTP.3.0");
   } 
   catch ( e ) 
   {
    try 
    {
     httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch ( e ) 
    {
     try 
     {
      httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
     } 
     catch ( e ) 
     {
      httpRequest = null;
     }
    }
   }
  }
 }

 return httpRequest;
}

function onLoadingHelperAjax( self )
{
 if ( self.id != null && self.onFeedback != null )
 {
  if ( self.onFeedback.onLoading != null )
  {
   self.onFeedback.onLoading( self.id );
  }
 }
}

function onLoadedHelperAjax( self )
{
 if ( self.id != null && self.onFeedback != null )
 {
  if ( self.onFeedback.onLoaded != null )
  {
   self.onFeedback.onLoaded( self.id );
  }
 }
}

function onInteractiveHelperAjax( self )
{
 if ( self.id != null && self.onFeedback != null )
 {
  if ( self.onFeedback.onInteractive != null )
  {
   self.onFeedback.onInteractive( self.id );
  }
 }
}

function onCompletedHelperAjax( self )
{
 if ( self.id != null )
 {
  if ( self.onCompleted != null )
  {
   self.onCompleted( self.id, self.request );
  }
 }
}

function onErrorHelperAjax( self )
{
 if ( self.id != null )
 {
  if ( self.onError != null )
  {
   self.onError( self.id, self.request.status, self.request.statusText );
  }
 }
}

//////////////////////////////////////////////

function onCompletedAjax( id, request )
{
 SetElementTextById( id, request.responseText );
}

function onErrorAjax( id, status, statusText )
{
 SetElementTextById( id, status + " " + statusText );
}

//////////////////////////////////////////////

function SetMethodAjax( method )
{
 this.method = method;
}

function SetMimeTypeAjax( mimeType )
{
 this.mimeType = mimeType;
}

function StateChange( self )
{
 if ( self.request != null )
 {
  if ( self.request.readyState == 1 )
  {
   self.onLoadingHelper( self );
  }
  else if ( self.request.readyState == 2 )
  {
   self.onLoadedHelper( self );
  }
  else if ( self.request.readyState == 3 )
  {
   self.onInteractiveHelper( self );
  }
  else if ( self.request.readyState == 4 )
  { 
   if ( self.request.status == 200 )
   {
    self.onCompletedHelper( self );
   }
   else
   {
    self.onErrorHelper( self );
   }

   self.request = null;
  }
 }
}

function PerformAjax()
{
 this.request = this.CreateRequest();
 if ( this.request != null )
 {
  var self = this;
  
  if ( this.mimeType != null )
  {
   if ( this.request.overrideMimeType )
   {
    this.request.overrideMimeType( this.mimeType );
   }
  }
  
  var url = this.url;
  if ( this.query != null )
  {
   url += "?" + this.query;
  }

  this.request.open( this.method, url, true ); 
  
  this.request.onreadystatechange = function()
  {
   self.StateChange( self );
  }

  this.request.send( null ); 
 }
}

////////////////////////////////////////////////////////////////

// CLASS AjaxRequest

function AjaxRequest( url, query, id )
{
 // member variables
 this.url      = url;
 this.query    = query;
 this.id       = id;
 this.request  = null;
 this.method   = "GET";
 this.mimeType = null;

 // member methods for handling request
 this.CreateRequest = CreateRequestAjax;
 this.Perform       = PerformAjax;
 this.StateChange   = StateChange;
 this.SetMethod     = SetMethodAjax;
 this.SetMimeType   = SetMimeTypeAjax;

 // helper member methods for handling feedback
 this.onLoadingHelper     = onLoadingHelperAjax;
 this.onLoadedHelper      = onLoadedHelperAjax;
 this.onInteractiveHelper = onInteractiveHelperAjax;
 this.onCompletedHelper   = onCompletedHelperAjax;
 this.onErrorHelper       = onErrorHelperAjax;

 this.onCompleted = onCompletedAjax;
 this.onError     = onErrorAjax;

 // member variable to class for handling feedback
 this.onFeedback    = null;
}

////////////////////////////////////////////////////////////////


