//------------------------------------------------------------------------------
//                              -= OSI2I =-
//------------------------------------------------------------------------------
// Description:
//  Objet XmlHTTPRequest
//
// Version:
//   14/06/2007 - RLA - V1.00
//    - Creation du fichier
//   25/06/2007 - RLA - V1.01
//    - Ajout de la prise en charge du format XML RESPONCE + ModalBox
//   26/08/2007 - RLA - V1.02
//    - Ajout du support de reconnection (modal)
//------------------------------------------------------------------------------

//Propriete du buffer interne
var OSXHR_STAT_UNINITIALIZED  = 0;
var OSXHR_STAT_LOADING        = 1;
var OSXHR_STAT_LOADED         = 2;
var OSXHR_STAT_INTERACTIVE    = 3;
var OSXHR_STAT_COMPLETE       = 4;

//Trame de retour XML RESPONCE
var OSXHR_CODE_ERR_NOTFOUND       = '_ERR_NOTFOUND';
var OSXHR_CODE_ERR_RIGHTS         = '_ERR_RIGHTS';
var OSXHR_CODE_ERR_SESSION        = '_ERR_SESSION';
var OSXHR_CODE_MSG_OK             = '_MSG_OK';
var OSXHR_CODE_MSG_ERR            = '_MSG_ERR';
var OSXHR_CODE_MSG_WARN           = '_MSG_WARN';

var log = 1;
//Class osXhr
osXhr = function( modal ){
  this.url          = '';
  this.params       = '';
  this.method       = '';
  this.onLoad       = null;
  this.onError      = this.onDefaultError;
  this.req          = null;
  this.modal        = modal; //(modal) ? modal : modalObj;

  //Creation de l'objet XMLHttpRequest
  if(window.XMLHttpRequest){
    this.req = new XMLHttpRequest();
  }else if(window.ActiveXObject){
    this.req = new ActiveXObject("Microsoft.XMLHTTP");
  }

  //Controle de la creation
  if ( !this.req ) {
     alert('Impossible de creer une instance de XMLHTTP');
  }
  
  //Affectation des callback
  this.setCallback = function(onLoad, onError){
    this.onLoad       = onLoad;
    this.onError      = (onError) ? onError : this.onDefaultError;
  }

  //Chargement du fichier
  this.load = function(url, method, params, onLoad ){
    if ( !url )  return;
    this.url      = encodeURI(url);
    this.params   = params;
    this.method   = (method) ? method : 'GET';
    if ( onLoad ) this.onLoad = onLoad;
    this.req.open(this.method, this.url, true);
    var obj = this;
    this.req.onreadystatechange = function(){
      obj.onStatChange.call( obj );
    }
    if ( this.method == 'POST' )
      this.req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=UTF-8"'); //charset=iso-8859-1
    this.req.send(params);
  }
  
  this.onAuthenticate = function( username, password){
	var account = '&login=1&username=' + username + '&password=' + password;
	if ( this.method == 'GET' )
		this.url = this.url + account;
	else
		this.params = this.params + account;
	//alert('OnAuthenticate - url: ' + this.url + ' - Params: ' + this.params);	
	this.load( this.url, this.method, this.params, this.onLoad );
  }
  
  //Handle onreadystatechange
  this.onStatChange = function( ){
    if ( !this.req ) return;
    if (this.req.readyState == OSXHR_STAT_COMPLETE ){
        if ( this.req.status == '200' ) {
          //Verification de la requete
          var code = this.getCode();
        //  alert( 'code: ' + code );
	
          if ( code != '' ){
              if (( code == OSXHR_CODE_ERR_SESSION )  && ( log == 1 )){
			    log = 0;
                this.modal.login( this ); 
              }else if (( code == OSXHR_CODE_ERR_RIGHTS ) && ( log == 1 )){
				 log = 0;
                this.modal.box( this.getText(), OSMODAL_ERROR );
              }else if (( code == OSXHR_CODE_MSG_OK ) && ( log == 1 )){
			     log = 0;
                this.modal.msgbox( this.getText(), OSMODAL_INFO );
                if ( this.onLoad ) this.onLoad.call( this, code );
              }else if (( code == OSXHR_CODE_MSG_ERR ) && ( log == 1 )){
			     log = 0;
                this.modal.box( this.getText(), OSMODAL_ERROR );
              }else if (( code == OSXHR_CODE_MSG_WARN ) && ( log == 1 )){
			     log = 0;
                this.modal.box( this.getText(),OSMODAL_WARN );
                if ( this.onLoad ) this.onLoad.call( this, code );
              }else if (log == 1){
                if ( this.onLoad ) this.onLoad.call( this, code );
              }
			 
          }else{
            if ( this.onLoad )
              this.onLoad.call( this, '' );
          }
        }else{
          if ( this.onError )
            this.onError.call( this );
        }
    }
  }
  

  
  //Handle onreadystatechange
  this.onDefaultError = function(){
    alert( "Erreur Composant XHR:\n" +
           "Etat: " + this.req.readyState + "\n" +
           "Erreur: " + this.req.status + "\n" +
           "Entête: " + this.req.getAllResponseHeaders() + "\n" );
  }
  
  //Recupere la reponse au format texte brute (avec entete)
  this.getRowText = function(){
    return this.req.responseText;
  }
  
  //Recupere la reponse au format XML-DOM (avec entete)
  this.getRowXml = function(){
    return this.req.responseXML;
  }
  
    //Recupere la reponse au format texte
  this.getCode = function(){
    var txt = this.req.responseText;
    var len = txt.length;
    var startid = txt.indexOf('<responce>', 0);
    if ( startid<0 )  return '';
    startid = txt.indexOf('<code>', startid );
    if ( startid<0 )  return '';
    var endid = txt.indexOf('</code>', startid );
    if ( endid<0 )  return '';
    var res = txt.substr(startid+6, endid-startid-6);

    return res;
  }
  
  //Recupere la reponse au format texte
  this.getText = function(){
    var txt = this.req.responseText;
    var len = txt.length;
    var startid = txt.indexOf('<responce>', 0);
    if ( startid<0 )  return txt;
    startid = txt.indexOf('<content>', startid );
    if ( startid<0 )  return txt;
    var endid = txt.indexOf('</content>', startid );
    if ( endid<0 )  return txt;
    var res = txt.substr(startid+9, endid-startid-9);

	

	return res;
  }

  //Recupere la reponse au format XML-DOM
  this.getXml = function(){
  
  
    var xml = this.req.responseXML;
    var responce = getDomChildNodeByTag(xml, 'responce' );
    return getDomNodeByTag( responce, 'content' );

  }
  

  
  

}



