var AjaxRequest = Class.create();
var AjaxRequestId = 0;
AjaxRequest.prototype = {
   url_         : undefined,
   handler_     : undefined,
   raw_         : false,
   id           : undefined,

   initialize : function(url,successHandler,failureHandler,raw) {
      this.id = AjaxRequestId++;
      this.url_ = url;
      if(raw != undefined) this.raw_ = raw;
      this.handler_ = {
         onSuccess : successHandler,
         onFailure : failureHandler
      };
   },
   setHandler : function(type,func) {
      this.handler_[type] = func;
   },
   send : function(param,method) {
      if(!method) method = 'post'
      var options = {
         method      :method,
         parameters  :$H(param||{}).toQueryString(),
         onSuccess   :this.onSuccess.bind(this),
         onFailure   :this.onFailure.bind(this),
         onException :this.onException.bind(this)
      };
      new Ajax.Request(this.url_,options);
   },
   onSuccess : function(res) {
      if(this.raw_) {
         this.handler_.onSuccess(res.responseText);
      } else {
         var obj = res.responseText.evalJSON();
         this.handler_.onSuccess 
            ? this.handler_.onSuccess(obj)
            : alert('success:\n' + $H(obj).inspect());
      }
   },
   onFailure : function(res,o) {
      
      this.handler_.onFailure
         ? this.handler_.onFailure(res,o)
         : alert('failure:\n' + $H(obj).inspect());
   },
   onException : function(res,e) {
      alert('exception:\n'+e.message);
   }
};
