//
// Display a popup window
//

function NewWindow(mypagenm,myname,w,h,scroll){
  var win = null;
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable=yes';
  re = /(http:\/\/[^\/]+\/)([^?]+)(.*)/;
// split
  if (re.exec(mypagenm) === null) {
          parts = re.exec(window.location);
          if (parts !== null) {
                mypagenm = parts[1] + mypagenm;
                }
          }
  mypagenm = mypagenm.replace(/\/\//g, "/");            // remove double backslashes
  mypagenm = mypagenm.replace(/^http:\//, "http://");   // except the one near http...
  wn = window.open(mypagenm,myname,settings);
  if (wn.window.focus){wn.window.focus();}
  }





// Window sizing to content:

function getAbsolutePos(el) {
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
};


// Rescale to content!

function RescaleToContent(bottom) {
	var body = document.body;
	var body_height = 0;
	if (typeof bottom == "undefined") {
		var div = document.createElement("div");
		body.appendChild(div);
		var pos = getAbsolutePos(div);
		body_height = pos.y;
	} else {
		var pos = getAbsolutePos(bottom);
		body_height = pos.y + bottom.offsetHeight;
	}
	if (body_height > 700) body_height = 700;
	if (!document.all) {
		window.sizeToContent();
		window.sizeToContent();	// for reasons beyond understanding,
								// only if we call it twice we get the
								// correct size.
		// center on parent
		var x = opener.screenX + (opener.outerWidth - window.outerWidth) / 2;
		var y = opener.screenY + (opener.outerHeight - window.outerHeight) / 2;
		window.moveTo(x, y);
		var body_width;
		body_width = body.offsetWidth + 5;
		if (body_width > 900) body_width = 900;
		window.innerWidth = body_width;
		window.innerHeight = body_height + 50;
	} else {
		var height,width;
		if (document.body.scrollHeight > document.body.offsetHeight) {
			width = document.body.scrollWidth;
			height = document.body.scrollHeight;
			} else {
			width = document.body.offsetWidth;
			height = document.body.offsetHeight;
			}
		window.resizeTo(width, height+55);		
		var W = body.offsetWidth;
		var H = 2 * body_height - height;
		var x = (screen.availWidth - W) / 2;
		var y = (screen.availHeight - H) / 2;
		window.moveTo(x, y);
	}    
}


//
// Add a hidden input to a form
//
function AddHiddenInput(form_id, name, value) {
  var frm, inp;
  frm = document.getElementById(form_id);
  if (frm === null) return false;
  inp = document.createElement("input");
  inp.setAttribute("type", "hidden");
  inp.setAttribute("name", name);
  inp.setAttribute("value", value);
  frm.appendChild(inp);  
  return true
  } 

