var origXhr = dojo.xhr;
dojo.xhr = function(method, args, hasBody) {
    //Make sure to not mess with original args in case they are reused
    var oldError = args.error;
    var msg = new Efm.Msg();
    args = dojo.delegate(args);
    args.error = function(err, ioArgs) {
        //Call the old error function.
        if(oldError){
            //oldError.apply(args, arguments);
            //alert(err+' -> ' +ioArgs+ ' arg:'+args+' arggum'+arguments);
            oldError.apply(args, arguments);
            if(ioArgs.xhr.status != 200) {
                        var errStr = '';
                        switch(ioArgs.xhr.status) {
                            case 403:
                                errStr = 'You don\'t have permission to access this resource.';
                                break;
                            case 404:
                                errStr = 'The requested page was not found.';
                                break;
                            case 405:
                                errStr = 'This method is not allowed here.';
                                break;
                            case 500:
                                errStr = 'Internal server error. Please try again in few minutes.';
                                break;
                            case 501:
                                errStr = 'Not implemented.';
                                break;
                            case 503:
                                errStr = 'Service Unavaliable. Please try again in a few minutes.';
                                break;
                            case 504:
                                errStr = 'Gateway timeout.';
                                break;
                            case 0:
                                errStr = 'Can\'t connect to server or the server did not responded!\n<br/>Please try in a few minutes.';
                                break;
                            default:
                                errStr = 'HTTP error.';
                                break;
                        }
                        msg.addError(ioArgs.xhr.status+ ' ' + errStr);
            } else {
                        msg.addError('An error occured: '+err);
            }
        }
        //Do general work here.
        if(request.xhr.status == 403){
            msg.showError('error403');
        }
    }
    var call = true;
    if (msg.hasErrors()) {
        msg.showMessages();
        msg.clearMessages();
        call = false;
    }
    if (call) {
        return origXhr.call(dojo, method, args, hasBody);
    }
    return false;
}