﻿Tools = {

    Log: function (str, config) {
        if (config.Debug) {
            if (window.console) console.log(str);
        }
    },

    Format: function (template, model) {
        var result = template;
        for (param in model) {
            var re = new RegExp("(%7B|\{)" + param + "(%7D|\})", "g");
            result = result.replace(re, model[param].toString());
        }
        return result;
    },

    FindFirst: function (str, re, options) {
        try {
            var m = re.exec(str);
            if (m == null) {
                return null;
            } else {
                return m[1];
            }
        }
        catch (err) {
            if (options.OnError) { options.OnError(str); }
        }
        return null;
    },

    Ajax: function (options) {
        jQuery.ajax({
            url: options.url,
            type: "POST",
            data: jQuery.toJSON(options.data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                if (data && data.d) data = data.d;
                options.success(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                if (options.OnError) {
                    options.OnError(XMLHttpRequest.responseText);
                } else {
                    alert(XMLHttpRequest.responseText);
                }
            }
        });

    },

    Trim: function (str) {
        var str = str.replace(/^\s\s*/, ''),
    		ws = /\s/,
    		i = str.length;
        while (ws.test(str.charAt(--i)));
        return str.slice(0, i + 1);
    }

}
