﻿
String.prototype.format = function() {
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture) { return args[capture.match(/\d+/)]; });
}

function FailedCallback(err, fnc) {
    alert('Error ' + '{1} <br/>[CB:{0}]'.format(fnc, err));
    //refresh the page
    location.reload(); 

}

function ServiceCall(method, parameters, onSucess, onFailure) {
    var parms = "{" + (($.isArray(parameters)) ? parameters.join(',') : parameters) + "}";
    $.ajax({
        type: "POST",
        url: "services/OutageData.asmx/" + method,
        data: parms,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (typeof onSucess == 'function' || typeof onSucess == 'object')
                onSucess(msg.d);
        },
        error: function(msg, err) {
            if (typeof onFailure == 'function' || typeof onFailure == 'object')
                var errObj = null; var errMsg = ''; var errTrace = '';
            try {
                errObj = eval('(' + msg.responseText + ')');
                errMsg = errObj.Message;
                errTrace = errObj.StackTrace;
            }
            catch (e2) {
                errMsg = 'unknown error';
                errTrace = 'not available';
            }
            onFailure(errMsg, errTrace);
        }
    });
}      