//Create Namespace
var $UTIL = TGN.namespace("TGN.Util");

if ( 'undefined' == typeof _YDom ) _YDom = YAHOO.util.Dom;

//create a common Request Object
$UTIL.AjaxRequest = function(HTTPmethod, path, params, callerCallback, postParams){
    this.HTTPmethod = HTTPmethod;
    this.path = path;
    this.params = params;
    this.callerCallback = callerCallback;
    this.postParams = postParams;
    this.connectionObject;
        
    this.mycallback = 
    {
        success: this.handleSuccess,
        failure: this.handleFailure,
        scope: this
    };
    
    //check to see if timeout has been defined if not default to 5 sec
    if(typeof callerCallback.timeout == "undefined")
        this.mycallback.timeout = 5000;
    else
        this.mycallback.timeout = callerCallback.timeout;
};

$UTIL.AjaxRequest.prototype = {
    invoke: function() {
        //add request Type param on params
        if(null == this.params || this.params.length < 1)
            this.params = "requestType=async";
        else
            this.params += "&requestType=async";
        
      
        //look for requestId on the client to send back to the server for exception logging purposes
        if(typeof requestId != "undefined" && requestId.length > 0)
            this.params += "&pageRequestId=" + requestId;
         
        this.path = this.path + "?" + this.params;
        
        //if Dbugger exsist then update icon to waiting
        if(typeof $DD != "undefined")
            $DD.updateIconStatus("wait");
        
        this.connectionObject = YAHOO.util.Connect.asyncRequest(this.HTTPmethod, this.path, this.mycallback, this.postParams);
    },
    
    handleSuccess : function(o){
        //if the dbugger exists, then tell it to refresh
        if(typeof $DD != "undefined")
            $DD.refresh();
               
        //call caller's callback
        this.callerCallback.success(o, this.callerCallback.scope);
    },
    
    handleFailure : function(o){
        //if the dbugger exists, then tell it to refresh
        if(typeof $DD != "undefined")
            $DD.refresh();
        
        //call caller's callback    
        this.callerCallback.failure(o, this.callerCallback.scope);
    },
    
    abort : function(isTimeout){
        //if the dbugger exists, then tell it to refresh
        if(typeof $DD != "undefined")
            $DD.refresh();
            
        return YAHOO.util.Connect.abort(this.connectionObject, this.myCallBack, isTimeout);    
    }
};

