// Config - A class to handle getting and setting command line arguments
// 
// History:
// Feb 3, 2006, Orginal version, Noel Gorelick, (gorelick@asu.edu)
//

var Config = Class.create();

//
// params is the .search part of the URL
// <default> is an object containing named default arguments.
//
Config.prototype.initialize = function (params, defaults) {
    this.defaults = {};
    this.current = {};
    for (var name in defaults) {
        this.current[name] = this.defaults[name] = defaults[name];
    }
    if (params && params.length != 0 ) {
        var pairs = params.split("&");
        for (var i=0; i < pairs.length; i++) {
            var apair = unescape(pairs[i]).split("=");
            if (apair[0]) { this.current[apair[0]] = apair[1]; }
        }
    }
}

Config.prototype.getDefaults = function() { return(this.defaults); }
Config.prototype.getCurrent = function() { return(this.current); }
Config.prototype.getValue = function(name) { return(this.current[name]); }
Config.prototype.setValue = function(name,value) { this.current[name] = value; }
Config.prototype.toStr = function() {
    var str = "";
    for (key in this.current) {
        str += key + "=" + this.current[key] + "\n";
    }
    return(str);
}

// Set many values at once
Config.prototype.setValues = function(values) { 
    for (var name in values) {
        this.setValue(name, values[name]);
    }
}

// return things that have changed as an array of "name=value" pairs
// I like using an array because you can just .join it to make a URL
Config.prototype.getChanged = function()  {
    var changed = Array();
    for (var name in this.current) {
        if (this.current[name] != this.defaults[name]) {
            changed.push(name + "=" + this.current[name]);
        }
    }
    return(changed);
}
