var aWindows = new Array()

function makeWindow(cName, objWin, bIndependent) {
	this.name = cName
	this.win = objWin
	this.independent = bIndependent
}

function OpenWindow(cUrl, cName, lWidth, lHeight, bIndependent, bNotResizable){

	
	document.body.onunload = closeAllWindows


	// centra la posizione
	lLeftPosition = (screen.width) ? (screen.width-lWidth)/2 : 0;
	lTopPosition = (screen.height) ? (screen.height-lHeight)/2 : 0;
	
	if (!bNotResizable)
		cSettings = 'height='+lHeight+',width='+lWidth+',top='+lTopPosition+',left='+lLeftPosition+',resizable=yes,directories=no,status=no,hotkeys=no,menubar=no, scrollbars=yes'; // dependent=yes,
	else
		cSettings = 'height='+lHeight+',width='+lWidth+',top='+lTopPosition+',left='+lLeftPosition+',resizable=no,directories=no,status=no,hotkeys=no,menubar=no';
	var objCurrentWin = null;
	var lIndex = 0
	lIndex = aWindows.length
	
	// finestre caricate
	for (var i = 0; i < aWindows.length; i++) 
		if (aWindows[i].name == cName) {
			objCurrentWin = aWindows[i].win
			lIndex = i
			break
		};
	
	var bOpen = true
	if (objCurrentWin) 
		bOpen = objCurrentWin.closed;
	
	if (bOpen) {
		objCurrentWin = window.open(cUrl, cName, cSettings)
		aWindows[lIndex] = new makeWindow(cName, objCurrentWin, bIndependent)
	}
	
	objCurrentWin.focus()
}

// apertura finestra
function openWindow(cUrl, cName, lWidth, lHeight, bIndependent, bNotResizable) {
	OpenWindow(cUrl, cName, lWidth, lHeight, bIndependent, bNotResizable)
}


// chiusura di tutte le finestre dipendenti
function closeAllWindows() {

	for (var i = 0; i < aWindows.length; i++) {
		if ((!aWindows[i].independent) && (!aWindows[i].win.closed)) 
			aWindows[i].win.close();
	}
}	
