function bind(context, name){ 
  return function(){ 
    return context[name].apply(context, arguments); 
  }; 
} 

AsyncRequest = function()
{	
	var AR = {}; // public container
	var config = {};
	
	config.URI = BASEPATH + "shape.php";
	config.method = "POST";
	config.data = {};
	config.returnType = "json";
	config.handler = null;
	config.errorHandler = null;	
	
	function checkData(data) {
		if ((data != null) && ((config.returnType == "json" && (data.error == 0 || data.error == null)) || (config.returnType == "html"))) {
			if (config.handler)
				config.handler(data);
		} else {
			errorHandler(data);
		}
	}
	
	function errorHandler(data) {
		if (config.errorHandler)
			if (config.errorHandler(data))
				return;
	}

	// public
	AR.URI = function(URI) {
		config.URI = URI;
		return this;	
	}
	
	AR.method = function(method) {
		if (method == "POST" || method == "GET")
			config.method = method;
		return this;	
	}
	
	AR.data = function(data) {
		config.data = data;
		return this;
	}
	
	AR.returnType = function(type) {
		if (type == "json" || type == "html")
			config.returnType = type;
		return this;
	}
	
	AR.handler = function(f) {
		config.handler = f;
		return this;	
	}
	
	AR.errorHandler = function(f) {
		config.errorHandler = f;
		return this;	
	}
	
	AR.send = function() {
		$.ajax(
		{
			url: config.URI,
			type: config.method,
			dataType: config.returnType,
			data: config.data,
			cache: true, // only for GET
			success: checkData,
			error: errorHandler
		});
	}
	
	return AR;
}
