
// Extension to Ajax allowing for classes of requests of which only one (the latest) is ever active at a time
// - stops queues of now-redundant requests building up / allows you to supercede one request with another easily.

// just pass in onlyLatestOfClass: 'classname' in the options of the request

Ajax.currentRequests = {};

Ajax.Responders.register({
  onCreate: function(request) {
    if (request.options.onlyLatestOfClass && Ajax.currentRequests[request.options.onlyLatestOfClass]) {
            // if a request of this class is already in progress, attempt to abort it before launching this new request
           try { Ajax.currentRequests[request.options.onlyLatestOfClass].transport.abort(); } catch(e) {}
        }
        // keep note of this request object so we can cancel it if superceded
        Ajax.currentRequests[request.options.onlyLatestOfClass] = request;
  },
    onComplete: function(request) {
    if (request.options.onlyLatestOfClass) {
            // remove the request from our cache once completed so it can be garbage collected
             Ajax.currentRequests[request.options.onlyLatestOfClass] = null;
        }
    }
});

/*
// timeout management

function callInProgress (xmlhttp) {
	switch (xmlhttp.readyState) {
	case 1: case 2: case 3:
		return true;
		break;
	
	// Case 4 and 0
	
	default:
		return false;
		break;
	}
}

function showFailureMessage() {
	alert('Request timed out. Please try again or contact ReNet Support.');
}

// Register global responders that will occur on all AJAX requests
Ajax.Responders.register({
	onCreate: function(request) {
	request['timeoutId'] = window.setTimeout(
	function() {
		// If we have hit the timeout and the AJAX request is active, abort it and let the user know
		if (callInProgress(request.transport)) {
			request.transport.abort();
			showFailureMessage();
			
			// Run the onFailure method if we set one up when creating the AJAX object

			if (request.options['onFailure']) {
				request.options['onFailure'](request.transport, request.json);
			}
		}
	},
	
	10000 // 10 seconds
	);
},
		onComplete: function(request) {
			// Clear the timeout, the request completed ok
			window.clearTimeout(request['timeoutId']);
		}
	}
);
*/
