﻿function Master() {

    var currentObj = this;
    var func = {
        proccessRequest: function (response, reqcallback, reqparams) {
            if (reqcallback) {
                reqcallback(response, reqparams);
            }
        }

    };
    this.Request = {};
    this.resetRequestParams = function () {
        this.Request.type = "POST";
        this.Request.contentType = "application/json";
        this.Request.dataType = "json";
        this.Request.processData = true;
        this.Request.baseUrl = window.location.protocol + "//" + window.location.host + "/bindaasbargain/";
    };
    this.Events = {
        OnError: function (ex) {
            alert("error:" + ex.status + "\nmessage:" + ex.statusText);
        }
    };

    $.ajaxSetup({
        "error": currentObj.OnError
    });
    this.sendRequest = function (requrl, reqcallback, reqparams) {
        $.getJSON(requrl, function (response) {
            func.proccessRequest(response, reqcallback, reqparams);
        }, "json");

    };
    this.postWcfRequest = function (requrl, reqdata, reqasync, reqcallback, reqparams) {
        var strData = JSON.stringify(reqdata);
        $.ajax({
            url: requrl,
            data: strData,
            type: currentObj.Request.type,
            processData: currentObj.Request.processData,
            contentType: currentObj.Request.contentType,
            dataType: currentObj.Request.dataType,
            async: true,
            success: function (response) {
                func.proccessRequest(response, reqcallback, reqparams);
            },
            error: currentObj.Events.OnError
        });
        currentObj.resetRequestParams();
    };
    this.postRequest = function (requrl, reqdata, reqasync, reqcallback, reqparams) {
        $.ajax({
            type:currentObj.Request.type,
            url: requrl,
            data: reqdata,
            dataType: currentObj.Request.dataType,
            async: true,
            success: function (response) {
                func.proccessRequest(response, reqcallback, reqparams);
            },
            error: currentObj.Events.OnError
        });
        currentObj.resetRequestParams();
    };     
    this.resetRequestParams();
    return this;
}
