/* 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 = {};
	this.paramCount = -1;
	
	if (qs == null){
		if(location.search){
			qs = location.search.substring(1, location.search.length);
		}
		else{
			return;
		}
	}
	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, ' ');
	this.args = qs.split('&'); // parse out name/value pairs separated via &
	this.paramCount = this.args.length;
	
// split out each name=value pair
	for (var i = 0; i < this.args.length; i++) {
		var pair = this.args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		var value = (pair.length==2) ? decodeURIComponent(pair[1]) : name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}
Querystring.prototype.getAt = function(index, default_) {
	if(!this.args){
		return null;
	}
	var pair = this.args[index].split('=');
	var name = decodeURIComponent(pair[0]);
	var value = (pair.length==2) ? decodeURIComponent(pair[1]) : name;
	
	return {key:name, value:(value != null) ? value : default_};
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}

Querystring.prototype.copyTo = function(obj, prefixFlter) {
	var rt;
	for(var i = 0; i < this.paramCount; i++){
		rt = this.getAt(i);
		if(rt){
			if(prefixFlter && rt.key.indexOf(prefixFlter)==0){
				obj[rt.key] = rt.value;
			}
			else if(!prefixFlter){
				obj[rt.key] = rt.value;
			}
		}
	}
	return obj;
}

