/*****************************************************
	Objet HTTPCLIENT
*****************************************************/

function HTTPClient() {};

/*****************************************************
	On ajoute toutes les methodes et les propriétés
*****************************************************/
HTTPClient.prototype.url = null

 // Instance of XMLHttpRequest
HTTPClient.prototype.xmlhttp = null

HTTPClient.prototype.requete_en_cours = false

// The user defined handler - see MyHandler below
HTTPClient.prototype.userhandler = null

HTTPClient.prototype.init = function(url) 
{
  this.url = url;
  
  var xmlhttp = false; 
	/*@cc_on @*/ 
	/*@if (@_jscript_version >= 5) 
	// JScript gives us Conditional compilation, we can cope with old IE versions. 
	// and security blocked creation of the objects. 
	  try { 
	  xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.3.0"); 
	  } catch (e) { 
	   try { 
	    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	   } catch (E) { 
	    xmlhttp = false; 
	   } 
	  } 
	@end @*/ 
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
	{ 
  	xmlhttp = new XMLHttpRequest(); 
	}
	this.xmlhttp = xmlhttp
	
	if(!this.xmlhttp)
	{
		alert('Votre browser internet (' +  navigator.appName + navigator.appVersion + ') ne supporte pas ce script!')
	}
}

// Le 'handler' est une methode du client pour gerer la reponse asynchronisée avec le deroulement du script. cf fin de page
HTTPClient.prototype.asyncGET = function (handler) 
{
  // Empeche l'execution de deux requetes a la fois
  if (this.requete_en_cours) 
  {
    throw "Requette HTTP en cours! Veuillez attendre.";
  }
	
	this.requete_en_cours = true;
	
  this.userhandler = handler;

  // Open an async request - third argument makes it async
  this.xmlhttp.open('GET',this.url,true);

  // Assign a closure to the onreadystatechange callback
  this.xmlhttp.onreadystatechange = function() 
  {
  	this.stateChangeCallback();
  }

  // Send the request
  this.xmlhttp.send(null);
}

HTTPClient.prototype.stateChangeCallback = function() 
{
  switch (this.xmlhttp.readyState) 
  {
    // Requete non commencée
    case 1:
      try 
      {
      	this.userhandler.onInit();
      } 
      catch (e) 
      { 
      	/* Handler method not defined */ 
      }
    	break;

    // Contact etabli avec le serveur mais téléchargement non commencé
    case 2:
      try 
      {
        // Check for HTTP status 200: OK ?
        if ( this.xmlhttp.status != 200 ) 
        {
            this.userhandler.onError(
                this.xmlhttp.status,
                this.xmlhttp.statusText
                );

            // Abort the request
            this.xmlhttp.abort();

            // Call no longer in progress
            this.requete_en_cours = false;
        }
        else 
        {
        	this.userhandler.onWaiting();
        }                  	
      } 
      catch (e) 
      {
          /* Handler method not defined */
      }
    	break;

    // Appelé plusieurs fois au cours du téléchargement
    case 3:
      // Notify user handler of download progress
      try 
      {
      	// Get the total content length
      	// -useful to work out how much has been downloaded
      	try 
      	{
      	  var contentLength = this.xmlhttp.getResponseHeader("Content-Length");
      	} 
      	catch (e) 
      	{
      	  var contentLength = NaN;
      	} 
      	
      	// Call the progress handler with what we've got
      	this.userhandler.onProgress(this.xmlhttp.responseText,contentLength)

      } 
      catch (e) 
      { 
      	/* Handler method not defined */ 
      }
    	break;

    // Download completé
    case 4:
      try 
      {
        this.userhandler.onLoaded(this.xmlhttp.responseText);
      } 
      catch (e) 
      {
        /* Handler method not defined */
      } 
      finally 
      {
        // Call no longer in progress
        this.requete_en_cours = false;
      }
    	break;
  }
}
  
HTTPClient.prototype.syncGET = function (handler) 
{
	// Empeche l'execution de deux requetes a la fois
	if (this.requete_en_cours) 
	{
	    throw "Requette HTTP en cours! Veuillez attendre.";
	}
	
	//alert('deb GET')
	
	this.requete_en_cours = true;
	
	this.userhandler = handler;
	
	this.userhandler.onInit();
	// Open a sync request - third argument makes it sync
	this.xmlhttp.open('GET',this.url,false);    	
	this.userhandler.onWaiting();    	
	
	//alert('sending')
	
	// Send the request
	this.xmlhttp.send(null);
	
	//alert('sent')
	
	if(this.xmlhttp.status != 200)
	{
		this.userhandler.onError();
	}
	else
	{
		this.userhandler.onLoaded(this.xmlhttp.responseText);
	}
	//alert('reponse:\n'+this.xmlhttp.responseText)    	
}



/*******************************************************
	Classe Handler 
	
		-méthodes à redéfinir au cas par cas
*******************************************************/

function Handler() {}
Handler.prototype.onInit = function() {}
Handler.prototype.onWaiting = function() {}
Handler.prototype.onError = function(status,statusText) {}
Handler.prototype.onProgress = function(responseText,length) {}
Handler.prototype.onLoaded = function(result) {}


//var Handler = {
//    onInit: function() {},
//    onWaiting: function() {},
//    onError: function(status,statusText) {},
//    onProgress: function(responseText,length) {},
//    onLoaded: function(result) {}
//}
