
function PopWin( URL, width, height ){
  // poiché dopo c'è l'attributo scrollbars impostato a yes, 
	// devo calcolare la larghezza della barra verticale
	// su Explorer
	/*if ( navigator.appName.indexOf('Microsoft') != -1 ) {
    width += 16;
  }*/
	
	var screenW = screen.width;
	var screenH = screen.height;

  // chiudi pop-up se aperto precedentemente
	if ( (myWin) && (!myWin.closed) ) {
	  if (debug) debugMsg('Chiudo il popup...');  // Info x debug
	  CloseWin();
	}
	
	var xPos = parseInt( (screenW-width)/2 );
	var yPos = parseInt( (screenH-height)/2 );
	
	// x e y passate opzionalmente come 4° e 5° parametro
	if (PopWin.arguments.length >= 5) {
	  xPos = PopWin.arguments[3];
		yPos = PopWin.arguments[4];
		// Se viene passato un valore tra 0 e 1 allora consideralo come percentuale
		// es.  se xPos == .75 allora apri il pop-up a 3/4 della larghezza
		if (xPos > 0 && xPos < 1) xPos *= screenW;
		if (yPos > 0 && yPos < 1) yPos *= screenH;
	}

  // attributi per pop-up
  var attr = "top=" + yPos + ",left=" + xPos;
	attr += ",width=" + width + ",height=" + height + ",scrollbars=no,resizable=no";
	
	if ( URL.substring( 0, 1 ) == "#" ) {
	  URL = URL.substr(1);
		myWin = window.open('', 'thewin', attr);
		myWin.document.write(URL);
	}
	else {
    if (debug) debugMsg('Riapro il popup');  // Info x debug
	  myWin = window.open(URL, "thewin", attr);
	}
	// alla chiusura della finestra aggiorna il riferimento
	window.onunload = CloseWin;
	// porta in primo piano
	myWin.focus();
	if (debug) debugMsg('');  // Info x debug
}

function debugMsg( msg ) {
  if ( msg == "" ) {
    debugWin.document.writeln( "myWin: *" + myWin + "*<br>" );
	}
	else {
	  debugWin.document.writeln( msg + '<br>' );
	}	
}

function CloseWin() {
  if (debug) debugMsg('Metodo CloseWin');  // Info x debug
  if ( (myWin) && (!myWin.closed) ) {
	  myWin.close();
		myWin = null;
	}
	if (debug) debugMsg('');		 // Info x debug
}

{ // Creo riferimento alla finestra pop-up
  var myWin = null;
	var debug = false;

  // Info x debug
	if (debug) var debugWin = window.open('', '', 'width=200,height=100,scrollbars=yes,resizable=yes');
  if (debug) debugWin.document.write('Inizio debug<br>----------<br>');
  if (debug) debugMsg('');
}

function popflashURL(newURL) {

  if (window.opener && !window.opener.closed) {
    window.opener.location.href = newURL;
    window.opener.focus();
  }
  window.close();

}
