﻿/* Client-side access to querystring name=value pairs
Version 1.3
28 May 2008
	
License (Simplified BSD):
http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&'); // parse out name/value pairs separated via &

    // split out each name=value pair
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
			? decodeURIComponent(unescape(pair[1]))
			: name;

        if (this.params[name]) {
            if (this.params[name] instanceof Array) {
                this.params[name].push(value);
            } else {
                var v2 = [];
                v2.push(this.params[name]);
                v2.push(value);

                this.params[name] = v2;
            }
        } else {
            this.params[name] = value;
        }
    }
}

Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.set = function(key, value) {
    this.params[key] = value;
}

Querystring.prototype.toString = function() {
    var _values = "";
    for (var i in this.params) {
        var values = this.params[i];

        if (values instanceof Array) {
            for (var z in values) {
                if (_values.length > 0)
                    _values = _values + "&";
                _values = _values + i + "=" + escape(values[z]);
            }
        } else {
            if (_values.length > 0)
                _values = _values + "&";
            _values = _values + i + "=" + escape(values);
        }
    }
    return _values;
}

Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
}

