﻿Type.registerNamespace("BancWorks");

BancWorks.AjaxHandler = function(options) {
    this._virtualPath = options["VirtualAppPath"];
}

BancWorks.AjaxHandler.prototype = {

    //Post to a page using ajax with json paramters.
    ajax: function(module, func, params, callback) {
        params.CompleteCall = module + "." + func;
        $.ajax({
            dataType: "json",
            type: "POST",
            url: "Scripts/Ajax.aspx",
            cache: false,
            success: this.successHandler,
            data: params,
            error: this.errorHandler,
            callback: callback
        });
    },
    
    //Handler for when an error occurs.
    errorHandler: function(data, text) {
        alert("There was an error processing your request.");
    },

    //Handler for a successful response.
    successHandler: function(data, text) {

        try {
            if (data == "BANCWORKS_EXCEPTION") {
                //An exception occurred in the request processing.
                window.location = "errors/errorreport.aspx";
            }
            else if (data == "BANCWORKS_ERROR") {
                //Need to redirect to the error page.
                window.location = "error.aspx";
            }
            else {
                //Run the success callback.
                this.callback(data);
            }
        }
        catch (e) {
            alert(e);
        }
    },

    //Get the Path of the application.
    get_VirtualPath: function() {
        if (this._virtualPath == null || this._virtualPath == "")
            throw "Virtual Application Path not set.";
        else
            return this._virtualPath;
    }
}

BancWorks.AjaxHandler.registerClass('BancWorks.AjaxHandler');
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
