/**********************************************************************************
 * 
 * Nome Da Classe: AjaxTO
 * Módulo: Framework
 * Data: 27/10/2007 - 03:40:00
 * 
 * Autor: Nickael Almeida - nickael@tribodejuda.com
 * CopyRight: Tribo de Judá Informática LTDA. © 2007
 * 
 * Acesso: Privado
 * Alteração: Somente com Autorização
 * 
 **********************************************************************************/

function AjaxTO(){

  this.sClassName;
  this.sFunctionName;
  this.oParameter;
  this.bAssincrono;
}



/**********************************************************************************
 * 
 * Nome Da Classe: AjaxErro
 * Módulo: Framework
 * Data: 11/03/2008 - 13:45:00
 * 
 * Autor: Nickael Almeida - nickael@tribodejuda.com
 * CopyRight: Tribo de Judá Informática LTDA. © 2007
 * 
 * Acesso: Privado
 * Alteração: Somente com Autorização
 * 
 **********************************************************************************/

function AjaxErro(){
	this.bStatus;
  this.sMSG;
}



/**********************************************************************************
 * 
 * Nome Da Classe: Ajax
 * Módulo: Framework
 * Data: 27/10/2007 - 03:40:00
 * 
 * Autor: Nickael Almeida - nickael@tribodejuda.com
 * CopyRight: Tribo de Judá Informática LTDA. © 2007
 * 
 * Acesso: Privado
 * Alteração: Somente com Autorização
 * 
 **********************************************************************************/


/**
 * Ajax::Ajax()
 * Recebe os objeto necessarios e executa a função de conexão via Ajax
 * 
 * @param object AjaxTO - Objeto com os dados de conexão com o PHP
 * @param object oFuncRet - Função de retorno
 * @return void
 **/
function Ajax(AjaxTO, oFuncRet, bConexao){
  
  this.sURL       = 'Include/LIB/Json/AjaxProxy.php';
  this.oParameter = AjaxTO;
  this.oFuncRet   = oFuncRet;

  if (bConexao !== false) {
    this.Conexao();
  }
}

Ajax.prototype = {
  
  
  /**
   * Ajax::Conexao()
   * Seleciona a conexão de acordo com o navegador e à executa
   * 
   * @return void
   **/
  Conexao: function(){
    
    this.httprequest = null;
    
    if(window.XMLHttpRequest){
      this.httprequest = new XMLHttpRequest();
    } else if(window.ActiveXObject){
      try {
        this.httprequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e){
        try {
          this.httprequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) { }
      }
    }

    if(this.httprequest != null && this.httprequest != undefined){
    
      var oObj       = this;
      var oJSON      = new JSON();
      var sParameter = oJSON.toJSONString(this.oParameter);
      
      this.httprequest.onreadystatechange = function(){oObj.Retorno.call(oObj);}
      this.httprequest.open('post', this.sURL, this.oParameter.bAssincrono);
      this.httprequest.setRequestHeader("Content-length", sParameter.length);
      this.httprequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      this.httprequest.send("AjaxParameter=" + escape(sParameter));
    }
  },



  /**
   * Ajax::Retorno()
   * Executa a função passada para ser usada como retorno
   * 
   * @return void
   **/
  Retorno: function(){
    if(this.httprequest.readyState == 4){
      if(this.httprequest.status == 200){
        if(this.oFuncRet != null){
          oRes = this.httprequest.responseText.parseJSON();
          this.oFuncRet(oRes.oResponse);
        }
      } else {
        this.Erro();
      }
    }
  },



  /**
   * Ajax::Erro()
   * Função para tratamento no caso de erro
   * 
   * @return void
   **/
  Erro: function(){
    
    var oAjaxErro     = new AjaxErro();
    oAjaxErro.bStatus = false;
    oAjaxErro.sMSG    = 'Erro inesperado de Conexão. Tente novamente!';
    
    this.oFuncRet(oAjaxErro);
  }
}