Emv.Ajax = function (width, height) {

    // Default type of the request ('POST' or 'GET')
    this.requestType = 'POST';

    // Default type of the response (json, xml, script, html, jsonp or text)
    this.responseType = 'json';

    // Dialog popup appears on failure?
    this.dialogOnFailure = true;

    // Default dialog name
    this.dialogName = 'jq-dialog';

    // Default dialog position
    // May be string value in case of viewport
    // or array in case of x/y values (e.g. [350,100], ['right','top'])
    this.dialogPosition = 'center';

    // Default dialog width
    this.dialogWidth = 'auto';
    // Modify default dialog width
    if (undefined != width) {

        this.dialogWidth = width;
    }

    // Default dialog height
    this.dialogHeight = 'auto';
    // Modify dialog height
    if (undefined != height) {

        this.dialogHeight = height;
    }

    // Callback function on success
    this.callback;

    // Callback function on success
    this.callbackParam = '';

    // Forward uri on success
    this.forwardUri =  '';

    // Submit form on success
    this.submitForm;

    this.setRequestType = function(type) {

        this.requestType = type;
    };

    this.setResponseType = function(type) {

        this.responseType = type;
    };

    this.setDialogName = function(dialogName) {

        this.dialogName = dialogName;
    };

    this.setDialogPosition = function(dialogPosition) {

        this.dialogPosition = dialogPosition;
    };

    this.setDialogWidth = function(dialogWidth) {

        this.dialogWidth = dialogWidth;
    };

    this.setDialogHeight = function(dialogHeight) {

        this.dialogHeight = dialogHeight;
    };

    this.setDialogOnFailure = function(switcher, dialogName) {

        this.dialogOnFailure = switcher;

        if ('undefined' !== typeof(dialogName) && '' !== dialogName) {

            this.setDialogName(dialogName);
        }
    };

    this.setCallback = function(callback) {

        this.callback = callback;
    };

    this.setCallbackParam = function(callbackParam) {

        this.callbackParam = callbackParam;
    };

    this.setForwardUri = function(forwardUri) {

        this.forwardUri = forwardUri;
    };

    this.setSubmitForm = function(submitForm) {

        this.submitForm = submitForm;
    };

    /**
     * Perform an ajax request.
     * If dialogOnFailure is set to true, error messages will be shown in a popup.
     *
     * @param string url The ajax request will be called against this url.
     * @param string requestData The request data.
     * @return boolean
     */
    this.request = function(url, requestData) {

        if('' === url) {

            return false;
        }

        var dialogOnFailure = this.dialogOnFailure;
        var dialogName      = this.dialogName;
        var dialogPosition  = this.dialogPosition;
        var showErrorDialog = function (html) {

            $('#' + dialogName).dialog(
                    {title: 'Es ist ein Fehler aufgetreten',
                     modal: true,
                     position: dialogPosition}
            );
            $('#' + dialogName).html(html);
            $('#' + dialogName).dialog('open');
        };

        $('#' + dialogName).dialog(
                {width: this.dialogWidth,height: this.dialogHeight}
        );

        var callback      = this.callback;
        var callbackParam = this.callbackParam;
        var submitForm    = this.submitForm;
        var forwardUri    = this.forwardUri;

        if ('object' == typeof(requestData.options)) {

            requestData.options = JSON.stringify(requestData.options);
        }

        $.ajax({

            type: this.requestType,
            dataType: this.responseType,
            url: url,
            data: requestData,

            success: function(response) {

                if ('undefined' == typeof(response) || null === response) {

                    return false;
                }

                if ('function' == typeof(callback)) {

                	return callback(response, submitForm, callbackParam);
                }

                if ('undefined' == typeof(response.status)) {

                    if (dialogOnFailure) {

                        // Invalid status return!
                        showErrorDialog('<p>Status-Fehler beim Senden der Anfrage</p>');
                    }

                    return false;
                }

                if ('success' != response.status) {

                    // User/form error!
                    if ('undefined' !== typeof(response.errorMessage)) {

                        showErrorDialog(response.errorMessage);
                    } else {

                        showErrorDialog('<p>Undefinierter Fehler beim Senden der Anfrage</p>');
                    }

                    return false;
                } else {

                    // Everything is fine

                    // Do we have a form submit action?
                    if ('undefined' !== typeof(submitForm) && '' !== submitForm) {

                        submitForm.submit();
                        return true;
                    }

                    // Do we have a new redirect uri in response?
                    if ('undefined' !== typeof(response.fwdUri) && '' !== response.fwdUri) {

                        forwardUri = response.fwdUri;
                    }

                    if( 'undefined' !== typeof(forwardUri) && '' !== forwardUri ) {

                        // Forward to given page
                        window.location.href = forwardUri;
                    }

                    return true;
                }
            },

            error: function(response) {

                // asyncRequest failure
                if (dialogOnFailure) {

                    // Getting error status (404, ..)
                    if ('undefined' !== typeof(response.status)  && '' !== response.status && '200' != response.status) {

                        showErrorDialog('Fehler beim Senden der Anfrage: ' + response.status);
                    } else if ('undefined' !== typeof(response.status) && '200' == response.status) {

                        showErrorDialog('Server antwortete mit unerwartetem Ergebnis!');
                    }
                    return false;
                }
            }
        });

        return false;
    };
};

Emv.Ajax.SimpleReplace = function (url, id, idScrollTo, showDeepAjaxOverlay) {

    var target = ($.browser.opera) ? 'html' : 'html,body';

    if ('undefined' !== typeof(showDeepAjaxOverlay) && true === showDeepAjaxOverlay) {

        $('#'+id).find('.ajax-loading-overlay').show();
    } else {

        $('#'+id+ ' > div.ajax-loading-overlay').show();
    }

    if ('undefined' !== typeof(idScrollTo) && '' !== idScrollTo) {

        $(target).animate({scrollTop:$('#' + idScrollTo).position().top}, 'fast');
    }

    $('#'+id).load(url, function() {

        $('#'+id).find('.ajax-loading-overlay').hide();
    });
};
