//-- Constructor
//-- build the XMLHttpRequest object used later on
function LoadVars()
   {
   if( window.XMLHttpRequest )
      {
      this.m_xmlhttp = new XMLHttpRequest();
      }
   else if( window.ActiveXObject )
      {
      this.m_xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      if( this.m_xmlhttp == undefined )
         this.m_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
   else
      { return null; }
   if( this.m_xmlhttp == undefined ) return null;
   this.loaded = null;
   this.cansend = true;
   this.latence = 250;
   }
   
//-- Add request headers
LoadVars.prototype.addRequestHeader = function( headerName, headerValue )
   {
   this.m_xmlhttp.setRequestHeader( headerName, headerValue );
   }
   
//--
LoadVars.prototype.attachHandler = function()
   {
   thisObj = this;
   this.m_xmlhttp.onreadystatechange = function() { thisObj.onReadyStateChange(); };
   }

//-- Event hadler of the XMLHttpRequest
LoadVars.prototype.onReadyStateChange = function()
   {
   if(!( this instanceof LoadVars )) { alert( "not the good class" ); return; }
   if( this == undefined ) {alert( 'onReadyStateChange::no object' );return;}
   if( this.m_xmlhttp == undefined ) {alert( 'onReadyStateChange::no XML object' );return;}
   if( this.m_xmlhttp.readyState == undefined ) {alert( 'onReadyStateChange::no state' );return;}
   switch( this.m_xmlhttp.readyState )
      {
      case 0:
         // not initialized
         this.cansend = false;
         if( this.onNotInitialized) this.onNotInitialized( this.m_xmlhttp.responseText );
         break;
      case 1:
         // open successful
         this.cansend = false;
         if( this.onOpen) this.onOpen( this.m_xmlhttp.responseText );
         break;
      case 2:
         // send successful
         this.cansend = false;
         if( this.onSent) this.onSent( this.m_xmlhttp.responseText );
         break;
      case 3:
         // transfer in progress
         this.cansend = false;
         if( this.onTransfer) this.onTransfer( this.m_xmlhttp.responseText );
         break;
      case 4:
         // transfer ended
         this.cansend = true;
         if( 200 != this.m_xmlhttp.status )
            this.onData( null );
         else
            {
            this.loaded = true;
            this.onData( this.m_xmlhttp.responseText );
            }
         break;
      }
   }
   
//-- Decode variables retrieved
LoadVars.prototype.decode = function( variables )
   {
   if( null == variables || undefined == variables || variables.length == 0 )
      {
      this.m_variables = null;
      }
   else if( variables.indexOf( '<br />', 0 ) == 0 )
      {
      if( ( variables.indexOf( '<b>Notice</b>', 0 ) > -1 ) || ( variables.indexOf( '<b>Error</b>', 0 ) > -1 ) )
         {
         alert( variables );
         this.m_variables = null;
         variables = '';
         }
      }
   else
      {
      variables = variables.replace( '&amp;', '&' );
   
      var _variables = variables.split( '&' );
      for( idx = 0; idx < _variables.length; idx++ )
         {
         args = _variables[ idx ].split( '=' );
         varname = unescape( args[ 0 ] );
         varvalue = args[ 1 ];
         if( varname != undefined )
            eval( "this." + varname + "='" + varvalue + "';" );
         }
      this.m_variables = null;
      }
   }
   
//-- Event handler
LoadVars.prototype.onData = function( src )
   {
   this.decode( src );
   this.onLoad( undefined != src );
   }
   
//-- Event handler to be
LoadVars.prototype.onLoad = function( success ) {}
/*
LoadVars.prototype.onNotInitialized = function( text ) {}
LoadVars.prototype.onOpen = function( text ) {}
LoadVars.prototype.onSent = function( text ) {}
LoadVars.prototype.onTransfer = function( text ) {}
*/
LoadVars.prototype.onNotInitialized = null;
LoadVars.prototype.onOpen = null;
LoadVars.prototype.onSent = null;
LoadVars.prototype.onTransfer = null;

//-- Load data from a specific URL
LoadVars.prototype.load = function( url )
   {
   url = baseurl + url;
   this.m_xmlhttp.open( "get", url, true );
   if( !this.cansend )
      {
      thisObj = this;
      setTimeout( "thisObj.load('"+url+"');", this.latence );
      return;
      }
   this.loaded = false;
   this.m_variables = null;
   this.attachHandler();
   this.m_xmlhttp.send( null );
   }

//-- function used to send datas
LoadVars.prototype._send = function( url, method, receive )
   {
   url = baseurl + url;
   //alert( '_send( "'+url+'", "'+method+'", "'+receive+'" )' );
   if( !this.cansend )
      {
      thisObj = this;
      setTimeout( "thisObj._send('"+url+"', '"+method+"', "+receive+");", this.latence );
      return;
      }
   if( method.length == 0 )
      method = "post";
   method = method.toUpperCase();
   this.loaded = false;
   this.m_variables = "";
   var prefix = '';
   for( vari in this )
      {
      if( vari in LoadVars.prototype || vari == 'm_xmlhttp' || vari == 'loaded' || vari == 'm_variables' || vari == 'cansend' || vari == 'latence' ) continue;
      this.m_variables += prefix + escape( vari ) + '=' + escape( eval('this.' + vari) );
      prefix = '&';
      }
   if( "GET" == method )
      {
      ret = this.m_xmlhttp.open( method, url + '?' + this.m_variables, true );
      if( receive == true )
         this.attachHandler();
      else
         this.m_xmlhttp.onreadystatechange = null;
      this.m_xmlhttp.send( null );
      }
   else
      {
      this.m_xmlhttp.open( method, url, true );
      if( receive == true )
         this.attachHandler();
      else
         this.m_xmlhttp.onreadystatechange = null;
      this.m_xmlhttp.send( this.m_variables );
      }
   this.m_variables = null;
   }

//-- send data to the server, no retrieval
LoadVars.prototype.send = function( url, method )
   {
   this._send( url, method, false );
   }

//-- send data to the server and retrieve answer parsing server variables and populating the object
LoadVars.prototype.sendAndLoad = function( url, method )
   {
   //alert( 'sendAndLoad( "'+url+'", "'+method+'" )' );
   this._send( url, method, true );
   }

