
var _popup_children = new Array();

function popup_close_children() {
	if (_popup_children) {
		for (var i = 0; i < _popup_children.length; i++) {
			if (!_popup_children[i].closed && _popup_children[i].close) {
				_popup_children[i].close();
			}
		}
	}
}

function popup_modal_children() {
	if (_popup_children.length > 0) {
		for (var i = 0; i < _popup_children.length; i++) {
			if (!_popup_children[i].closed && _popup_children[i].mix_modal && _popup_children[i].focus) {
				_popup_children[i].focus();
			}
		}
	}
}

function popup_child_close() {
	if (window.opener && !window.opener.closed) {
		// Remove this window from the popup list
		window.opener.popup_remove(window);
	}
}

function popup_add(popup) {
	var found = false;

	for (var i = 0; i < _popup_children.length; i++) {
		if (_popup_children[i] == popup) {
			found = true;
		}
	}

	if (!found) {
		_popup_children.push(popup);
	}
}

function popup_remove(popup) {
	var found = 0;
	if (_popup_children) {
		for (var i = 0; i < _popup_children.length; i++) {
			if (_popup_children[i] == popup) {
				found = i;
			}
		}
	}

	if (found > 0) {
		_popup_children.splice(i, 1);
	}
}

function popup_window(page, name, width, height, scroll, unload, modal) {
	if (typeof unload != 'boolean') {
		unload = true;
	}
	if (typeof modal != 'boolean') {
		modal = false;
	}

  var winl = (screen.width-width)/2;
  var wint = (screen.height-height)/2;
  var settings  ='height='+height+',';
      settings +='width='+width+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
      settings +='resizable=yes';

  child = window.open(page, name, settings);

  if (!child.opener) {
  	child.opener = window;
  }

 	child.mix_modal = modal;

	if (unload) {
		add_onunload(child, popup_child_close);
  	_popup_children.push(child);
  }

  if (child.focus) {
  	child.focus();
  }
}

add_onunload(window, popup_close_children);
add_onfocus(window, popup_modal_children);

