var pwindows = {};
var pwindowcount = 0;
function popup(page, width, height, options) {
	if(!page) {
		return;
	}

	var winWidth;
	var winHeight;

	if(typeof(navigator.innerWidth) != 'undefined') {
		winWidth	= navigator.innerWidth;
		winHeight	= navigator.innerHeight;
	}
	else if(document.documentElement && typeof(document.documentElement.clientWidth) != 'undefined') {
		winWidth	= document.documentElement.clientWidth;
		winHeight	= document.documentElement.clientHeight;
	}
	else if(document.body && typeof(document.body.clientWidth) != 'undefined') {
		winWidth	= document.body.clientWidth;
		winHeight	= document.body.clientHeight;
	}

	if(!width) {
		width = parseInt(parseInt(winWidth) * 0.70);
		if(!width || width < 500) {
			width = 500;
		}
	}
	if(!height) {
		height = parseInt(parseInt(winHeight) * 0.70);
		if(!height || height < 400) {
			height = 400;
		}
	}
	
	if(typeof(options) == 'undefined' || options == null) {
		options = {};
	}
	else if(typeof(options) == 'string') {
		var sets = options.split(',');
		var tmp = {};
		for(var i = 0; i < sets.length; i++) {
			var t = sets[i].split('=');
			tmp[t[0]] = t[1];
		}
		options = tmp;
	}
	var atts = ['scrollbars', 'resizable', 'toolbar', 'location', 'status','directories','menubar'];
	for(var i = 0; i < atts.length; i++) {
		if(!options[atts[i]]) {
			options[atts[i]] = 'yes';
		}
	}
	
	if(!options['left']) {
		options['left'] = 10;
	}
	if(!options['top']) {
		options['top'] = 10;
	}

	var features = "width="+width+",height="+height;
	for(var opt in options) {
		features += ',' + opt + '=' + options[opt];
	}
	var popname = window.location.pathname;
	popname = popname.replace(/^\W/,'');
	popname = popname.replace(/\//g,'_');
	popname = popname.replace(/\./g,'_');
	popname += '' + pwindowcount;
	if(!pwindowcount || !pwindows[page] || pwindows[page].closed) {
		pwindows[page] = window.open(page, popname, features);
		pwindowcount++;
	}
	//if(pwindows[page] != null && typeof(pwindows[page].focus) != 'undefined') {
		//pwindows[page].focus();
	//}
	return false;
}

