﻿var CommonHelper = {
    // Tests if an object is an Array. Not tested 
    // in all browsers so may need some modification.
    isArray: function(obj) {
        if (obj == null) return false;
        return typeof (obj) == "object" && obj.length && typeof (obj.length) == "number";
        // Alternatives.
        //return obj instanceof Array.
    },
    // Converts the date text returned from the
    // server to a JavaScript Date object.
    toJavaScriptDate: function(obj) {
        if (CommonHelper.isArray(obj)) {
            var arr = new Array();
            for (var i = 0; i < obj.length; i++)
                arr.push(CommonHelper.toJavaScriptDate(obj[i]));
            return arr;
        }
        if (obj == null) return null;
        obj = obj.replace('/Date(', '');
        obj = obj.replace(')/', '');
        var index = obj.indexOf("+");
        if (index == -1)
            index = obj.indexOf("-");
        obj = obj.substring(0, index);
        var date = new Date();
        date.setTime(obj);
        return date;
    },
    // Converts a JavaScript Date object to a
    // string that can be parsed by the server.
    fromJavaScriptDate: function(obj) {
        if (CommonHelper.isArray(obj)) {
            var arr = new Array();
            for (var i = 0; i < obj.length; i++)
                arr.push(CommonHelper.fromJavaScriptDate(obj[i]));
            return arr;
        }

        if (obj == null) return null;
        return "/Date(" + obj.getTime() + "+0)/";
    },

    getUrlVars: function () {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }
}

