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


// 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)){
				// Hack courtesy http://dev.rubyonrails.org/ticket/9490
				
				//call onAbort listener, if existent
				(request.options.onAbort || Prototype.emptyFunction)(request.transport, request.json);
				//trash existing handlers
				request.options={};
				//now abort the request
				request.transport.abort();
		
				handle_error(request);
				// 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);
				}
			}
		}, 20000)  // 20 seconds on all AJAX requests
	},

	onComplete: function(request) {
		// Clear the timeout, the request completed ok
		window.clearTimeout(request['timeoutId']);
	}
});



function handle_error(request, redirect) {

	var status = Try.these(
		function() { return request.status },
		function() { return 'timed out'}
	);
	
	if(status) {
		alert ('Sorry, but there was a problem accessing the requested page (' + status + '). Please try again later.');
	}
	else{
		alert ('Sorry, but there was a problem accessing the requested page. Please try again later.');
	}
	this.location.href=redirect;
	return false;
}
