/* 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]);

		if (name=="") {
			continue;
		}
		
		//parse the name for matching arrays
		var arrMatches = name.match(/\[([^\]]*)\]/g);
		
		if (arrMatches!==null && arrMatches.length>0) {
			//use the start of the name as the actually name
			var shortName = name.substring(0, name.indexOf("["));
			
			//do this for multiple levels and add the given value to any currently constructed 
			if (!this.params.hasOwnProperty(shortName)) {
				this.params[shortName] = {};
			}
			
			var prevObj = this.params;
			var prevIndex = shortName;
			var activeObj = this.params[shortName];
		
			for (var j=0; j<arrMatches.length; j++) {
				var arrName = arrMatches[j];
				arrName = arrName.substring(1, arrName.length-1); //remove the bracket from the match
				if (arrName=="" || parseInt(arrName)==arrName) {
					//treat the parameters as an array
					if (activeObj.constructor !== [].constructor) {
						prevObj[prevIndex] = [];
						activeObj = prevObj[prevIndex];
					}
					
					if (arrName=="") {
						activeObj.push({});
						var insertIndex = activeObj.length-1;
					} else {
						var insertIndex = parseInt(arrName);
						//alert(insertIndex);
						//ensure the array is at least as large as the index
						if (activeObj.length<=insertIndex) {
							//extend the active object by creating a new one of the length and copying the values over
							var newElems = [activeObj.length, 0];
							
							for (var k=0; k<(insertIndex-activeObj.length)+1; k++) {
								newElems.push({});
							}
							
							Array.prototype.splice.apply(activeObj, newElems);
						}
					}
					
					//update references for next round
					prevIndex = insertIndex;
					prevObj = activeObj;
					activeObj = activeObj[insertIndex];
				} else {
					//treat the parameter as an object and add it in
					if (!activeObj.hasOwnProperty(arrName)) {
						activeObj[arrName] = {};
					}
					
					prevIndex = arrName;
					prevObj = activeObj;
					activeObj = activeObj[arrName];
				}
			}
		} else {
			var prevObj = this.params;
			var prevIndex = name;
		}
		
		prevObj[prevIndex] = ((pair.length==2) ? decodeURIComponent(pair[1]) : name);
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}
