/**
 * @fileoverview 
 * Ajax<br/>
 * @author Fernando Henrique da S N de Souza - C001222
 * @version 23/06/2008
 */

/**
 * @class Classe de controle de informações com o Servidor
 * @return {Object} 
 */
function Ajax(){
	Ajax.CTRLINSTAJAX.push(this);
	/**
	 * Identificação da Lista de instâncias do CTRLINSTAJAX  
	 * @type {Number}  
	 */
	this.xmlHttpId =  Ajax.CTRLINSTAJAX.length - 1;
	/**
	 * Objeto XmlHttp  
	 * @type {Object}  
	 */
    this.xmlHttp = null;
	/**
	 * Retorno do documento XML  
	 * @type {XMLDocument}  
	 */
    this.responseXML  = null;
	/**
	 * Código do status do HTTP  
	 * @type {Number}  
	 */
    this.status       = null;
	/**
	 * Descrição do Status HTTP 
	 * @type {String}  
	 */
    this.statusText   = null;
	/**
	 * 	Código de status do xmlHttp  
	 * @type {void}  
	 */
    this.readyState   = null;
	/**
	 * Texto do Retorno do XmlHttp  
	 * @type {String}  
	 */
    this.responseText = null;
	/**
	 * Abilita ou desabilita o cache do XmlHttp
	 * @type{Boolean}
	 */
	this.cache = true;
/**
 * Atualização do valores das propriedades da classe
 * @return {void}
 * @method
 */
    this.atualizarDados = function(){
      try{
        this.responseXML  = this.xmlHttp.responseXML ? this.xmlHttp.responseXML : null;
        this.status       = this.xmlHttp.status ? this.xmlHttp.status: null;
        this.statusText   = this.xmlHttp.statusText ? this.xmlHttp.statusText: null;
        this.readyState   = this.xmlHttp.readyState ? this.xmlHttp.readyState: null;
        this.responseText = this.xmlHttp.responseText ? this.xmlHttp.responseText: null;
      }catch(e){
      
      }  
    }
/**
 * Configurando o readystatechange no xmlHttp
 * @return {void}
 * @method
 */	 
    this.setExecuteThread = function(){
      var comando = 
      "function readystatechange(){                                                                                     \n"+ 
      "  switch(Ajax.CTRLINSTAJAX["+this.xmlHttpId+"].xmlHttp.readyState){                                                   \n"+
      "    case Ajax.RS_CARREGANDO:                                                                                     \n"+
      "                                                                                                                 \n"+
      "    break;                                                                                                       \n"+
      "    case Ajax.RS_CARREGADO:                                                                                      \n"+
      "                                                                                                                 \n"+                  
      "    break;                                                                                                       \n"+
      "    case Ajax.RS_INTERATIVO:                                                                                     \n"+
      "                                                                                                                 \n"+                 
      "    break;                                                                                                       \n"+
      "    case Ajax.RS_COMPLETO:                                                                                       \n"+
      "    Ajax.CTRLINSTAJAX["+this.xmlHttpId+"].atualizarDados();                                                           \n"+
      "      if(Ajax.CTRLINSTAJAX["+this.xmlHttpId+"].xmlHttp.status > 299){                                                 \n"+
      "        if(Ajax.CTRLINSTAJAX["+this.xmlHttpId+"].onException != null){                                                \n"+
      "          Ajax.CTRLINSTAJAX["+this.xmlHttpId+"].onException(Ajax.CTRLINSTAJAX["+this.xmlHttpId+"].xmlHttp.status, Ajax.CTRLINSTAJAX["+this.xmlHttpId+"].xmlHttp.statusText); \n"+
      "        }                                                                                                        \n"+
      "      }else{                                                                                                     \n"+
      "        if(Ajax.CTRLINSTAJAX["+this.xmlHttpId+"].onExecute != null){                                                  \n"+
      "          Ajax.CTRLINSTAJAX["+this.xmlHttpId+"].onExecute();                                                          \n"+      
      "        }                                                                                                        \n"+                           
      "      }                                                                                                          \n"+
      "    break;                                                                                                       \n"+
      "  }                                                                                                              \n"+     
      "}                                                                                                                \n";
      eval(comando);
      this.xmlHttp.onreadystatechange = readystatechange;
    }          
/**
 * Instancia o Objeto Ajax.xmlHttp
 * @return {void}
 * @method
 */	                                                                                                              
    this.createxmlHttp = function(){    
  	  this.xmlHttp = null;                                                                                    
      try{                                                                                                                   
 			this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //funciona para IE...
		}catch(ee){                                                                                       
		  try{
		    this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //Utilizamos este no início para IE
			}catch(e){
			  try{
				  this.xmlHttp = new XMLHttpRequest(); //Funciona para Firefox...
				}catch(E){
				  this.xmlHttp = false;
				}
			}
		}
      if(this.xmlHttp && this.nomeInstancia != '')
        this.setExecuteThread();
    }
/**
 * Abrindo a conexão com o xmlHttp 
 * @method open
 * @param {String} metodo String 
 * @param {String} url String
 * @param {Boolean} assicrone Boolean
 */	
    this.open = function(metodo, url, assicrone){
      this.createxmlHttp();
      this.xmlHttp.open(metodo, url, assicrone);
	  if(metodo.toLowerCase() == "post"){
	  	this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	  }
	  if(this.cache){
	  	this.xmlHttp.setRequestHeader("Cache-Control","no-store, no-cache, must-revalidate");
	  }
    }
/**
 * Enviando os dados e executando o xmlHttp
 * @param parametros String
 * @method send
 */	
    this.send = function(parametros){
      this.xmlHttp.send(parametros);
      this.atualizarDados();
    }
/**
 * Configurando o Exception do Ajax caso tem erro no retorno
 * 
 * @param {Number} Status
 * @param {String} Mensagem
 * @method onException
 */
    this.onException = function(Status, Mensagem){  // Evendo para tratamento de error
    /**
     * Mensagem que será mostrado para usuário
     */
      var msg = "Ocorreu o seguinte erro ao solicitar os dados:\n\""+Mensagem+"\" \n Codigo: "+Status;
      alert(msg);
      if(this.onExecute != null)
        this.onExecute(); 
  }
/**
 * Configurando o execussão da função apos o retorno do xmlHttp
 * @method
 */  
    this.onExecute = null; // Evento de execução ao final da Thread xmlHttpRequest
    
}

/*
 * ---------------------
 *  Constantes do ajax
 * ---------------------
 */
  /** Status onde esta esperando o retorno 
   * @public 
   * @constant
   * @static
   */
  Ajax.RS_CARREGANDO = 1;
  /** Status onde já tem o retorno 
   * @public 
   * @constant
   * @static
   */
  Ajax.RS_CARREGADO  = 2;
  /** Status de inativação do objeto xmlHttp 
   * @public 
   * @constant
   * @static
   */
  Ajax.RS_INTERATIVO = 3;
  /** Status de transação Concluida
   * @public 
   * @constant
   * @static
   */
  Ajax.RS_COMPLETO   = 4;
  
/** listas de Objetos instanciados do Ajax
 * @protected
 * @static
 * @return {Array}  listas de Objetos xmlHttp
 */
  Ajax.CTRLINSTAJAX  = Array();

