/* Common JavaScript Functions */

/* New PopUp function */
/* Last Modified: August 5, 2008 */
/**
 *	Example Usage:
 *	PopUp({
 *		url: 'http://www.example.com', 
 *		name: '_new',
 *		width: 500,
 *		height: 300,
 *		top: 0,
 *		left: 0,
 *		focus: true,
 *		menubar: true
 *	}); 
 */
function PopUp(options) {
	var options = options || {};
	
	// all of our defaults
	var defaults = {
		  url: 'about:blank'
		, name: '_new'
		, focus: true
		, width: 640
		, height: 480
		, top: 0
		, left: 0
	};
	
	// properties array, to be joined later
	var winPropsArray = [];
	
	// window element
	var newWin;
	
	// check defaults
	for (var d in defaults) {
		if (options[d] === undefined) { options[d] = defaults[d]; }
	}
	
	// run through options
	for (var k in options) {
		switch (k) {
		
			// skip these
			case 'url':
			case 'name':
			case 'focus':
				break;
				
			// numerical properties
			case 'width':
			case 'height':
			case 'top':
			case 'left':
				if (isNaN(options[k])) { throw new Error('Property "'+k+'" must be a number'); }
				winPropsArray.push(k+'='+options[k]);
				break;
				
			// all others are assumed boolean properties
			default:
				if (options[k] !== false) { winPropsArray.push(k+'=yes'); }
				break;
		}
	}
		
	// open new window
	newWin = window.open(options.url, options.name, winPropsArray.join());
	
	// focus, if applicable
	if (options.focus === true) { newWin.window.focus(); }
	
	// return the new window
	return newWin;
}

/* Parse Query String */
function ParseQueryString(str) {
	var ret = {};
	var str = str || window.location.search.substring(1);
	
	// split query string and loop through each portion
	var query = str.split('&');
	
	// reverse while loop, fastest
	var i = query.length; while (i--) {(function(){
	
		// split into key and value pairs
		var pairs = query[i].split('=');
		var key = decodeURIComponent(pairs[0]);
		var value = pairs[1] ? decodeURIComponent(pairs[1]) : '';
		
		// check that the key isn't empty
		if (key !== '') {
			// if there's already this parameter specified . . .
			if (ret[key]) {
				// if it's an array, push on a the new value . . .
				if (typeof ret[key] === 'object' && ret[key].constructor === Array) {
					ret[key].push(value);
				}
				// . . . otherwise, turn it into a two-item array
				else {
					ret[key] = [ret[key], value];
				}
			}
			// . . . otherwise, just set the property
			else {
				ret[key] = value;
			}
		}
	})();}
	return ret;
}