//Simple static object to keep data returned from ajax requests
//The idea is to minimize ajax requests to server(and also make page faster)
var Cache = {
    CACHE_MAX_SIZE: 50, //Shield against memory leaks
    
    contents: {},
    set: function(key, value, overwrite) {
        overwrite = overwrite || false;
        if(!overwrite && Cache.contains(key)) {
            throw "Cannot overwrite value for " + key;
        }
        if(Cache.contents.length==Cache.CACHE_MAX_SIZE) {
            throw "Cache overflow";
        }
        this.contents[key] = value;
    },
    contains: function(key) {
        return typeof Cache.contents[key] != "undefined";
    },
    get: function(key) {
        if(!Cache.contains(key)) {
            throw "Unknow Cache key " + key;
        }
        return Cache.contents[key];
    },
    update: function(key, value) {
        if(!Cache.contains(key)) {
            throw "Cannot update " + key + ": key doesnt exists.";
        }
        Cache.set(key, value, 1);
    }
}