bhajs.AJAX = new function() {
    
    this.request = function(ajaxComponentId, requestData) {
        var component = bhajs.getObject("BHA.Web.UI.Ajax.AjaxComponent", ajaxComponentId);
        component.request(requestData);
    };
    
    this.asyncRequest = function(ajaxComponentId, requestData) {
        var component = bhajs.getObject("BHA.Web.UI.Ajax.AjaxComponent", ajaxComponentId);
        component.asyncRequest(requestData);
    };
    
    this.onResponse = function(responseData, ajaxComponentId) {
        var component = bhajs.getObject("BHA.Web.UI.Ajax.AjaxComponent", ajaxComponentId);
        component.onResponse(responseData);
    };
    
    this.onError = function(errorData, ajaxComponentId) {
        var component = bhajs.getObject("BHA.Web.UI.Ajax.AjaxComponent", ajaxComponentId);
        component.onError(errorData);
    };
    
};


bhajs.defineClass("BHA.Web.UI.Ajax", "AjaxComponent", 
    function (id, requestScript, asyncRequestScript, onResponseScript, onErrorScript) {
        this.$id = id;
        this.$classInfo = bhajs.getClassInfo("BHA.Web.UI.Ajax.AjaxComponent");
        this.$classInfo.registerObject(this.$id, this);
        
        this.$doRequest = new Function("eventArgument", "eventCallback", "context", "errorCallback", requestScript);
        this.$doRequestAsync = new Function("eventArgument", "eventCallback", "context", "errorCallback", asyncRequestScript);
        
        
        this.request = function(requestData, async) {
            this.$doRequest(requestData, bhajs.AJAX.onResponse, this.$id, bhajs.AJAX.onError);
        };
        
        this.asyncRequest = function(requestData) {
            this.$doRequestAsync(requestData, bhajs.AJAX.onResponse, this.$id, bhajs.AJAX.onError);
        };
        
        if (onResponseScript) {
            this.onResponse = new Function("responseData", onResponseScript);
        }else{
            this.onResponse = new Function("responseData", "");
        }
        
        if (onErrorScript) {
            this.onError = new Function("errorData", onErrorScript);
        }else{
            this.onError = new Function("errorData", "");
        }
        
    }
    
);

