﻿//funzione per risolvere i problemi del target blank.
function OpenNewWindow(a, w, h){
	if (w==0 || h==0){
		a.target = '_blank';
	}
	else{
		try{
			if(event.shiftKey || event.shiftLeft){
				a.target = '_blank';
			}
			else{
				a.target = 'prova';
				return OpenSizedWindow(a.href, 'prova', w, h);
			}
		}
		catch(except){
			a.target = '_blank';
		}
	}
}

function OpenWindow(url, width, height){
	OpenSizedWindow(url, '_blank', width, height)
}

function OpenSizedWindow(url, target, width, height){
	var properties
	if (width=='')
		properties = ''
	else
		properties = 'width=' + width + ', height=' + height + ', resizable=yes, scrollbars=yes, status=yes, menubar=no, toolbars=no'
	var w = window.open(url, target, properties);
	if (!w.opener)
		w.opener = this;
	return void(0);
}

function OpenAutoPositionedSizedWindow(url, target, width, height){
	var properties
	var x, y;
	var top, left;
	try{
		//calcola coordinata Y
		y = (event.screenY - event.offsetY) + 20;
		if ((y + height)>(screen.height-100))
			y = (screen.height - height - 100);
	
		//calcola coordinata X
		x = (event.screenX - (width/2));
		if (x < 20)
			x = 20;
		else if ((x + width)>(screen.width-20))
			x = (screen.width - width - 20);
		
		top = 'top=' + y + ', ';
		left = 'left=' + x + ', ';
	}
	catch(except){
		top='';
		left='';
	}
	
	if (width=='')
		properties = ''
	else
		properties = 'width=' + width + ', height=' + height + ', '
	properties += top + left + 'resizable=yes, scrollbars=yes, status=yes, menubar=no, toolbars=no'
	
	var w = window.open(url, target, properties);
	if (!w.opener)
		w.opener = this;
	return void(0);
}

function OpenPositionedScrollWindow(url, target, left, top, width, height, scrollbars){
	var properties
	if ((height + top)>(screen.height-60)){
		height = screen.height - top - 60;
		width += 20;
	}
	
	if (width=='')
		properties = '';
	else
		properties = 'left=' + left + ', top=' + top + ', width=' + width + ', height=' + height + 
					 ', resizable=yes, status=yes, menubar=no, toolbars=no';
	if (scrollbars)
		properties += ", scrollbars=yes";
	else
		properties += ", scrollbars=no";
		
	var pW = window.open(url, target, properties);
	return pW;
}

function OpenAutoPositionedScrollWindow(url, target, width, height, scrollbars){
	var top, left, except;
	try {
		//calcola coordinata Y
		top = (event.screenY - event.offsetY) + 20;
		if ((top + height)>(screen.height-100))
			top = (screen.height - height - 100);
	
		//calcola coordinata X
		left = (event.screenX - (width/2));
		if (left <20)
			left = 20;
		else if ((left + width)>(screen.width-20))
			left = (screen.width - width - 20);
	}
	catch(except){
		top = screenX + (outerWidth - innerWidth);
		left = screenY + (outerHeight - innerHeight);
	}
	
	var pW = OpenPositionedScrollWindow(url, target, left, top, width, height, scrollbars)
	return pW;
}


function opensmartimage(image){
	OpenSmartImage(image);
}
var ImageWindowCount = 0;
var ImageWindow;

function OpenSmartImage(image){
	if (ImageWindowCount){
		ImageWindow.close();
		ImageWindow = null;
		ImageWindowCount = 0;
	}
	ImageWindow = window.open('', 'ImageWindow', 'width=300,height=250,resizable=yes,scrollbars=no');
	ImageWindowCount++;
	ImageWindow.document.open();
	ImageWindow.document.write('<html>\n');
	ImageWindow.document.write('\t<head>\n');
	ImageWindow.document.write('\t<meta http-equiv=Content-Type content="text/html; charset=utf-8">\n');
	ImageWindow.document.write('\t\t<title></title>\n');
	ImageWindow.document.write('\t</head>\n');
	ImageWindow.document.write('\t<body bottommargin="0" leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">\n');
	ImageWindow.document.write('\t\t<div style="width:100%; height:100%; text-align:center;">\n');
	if (image.indexOf("http") == -1)
		image = imageURL + image
	ImageWindow.document.write('\t\t\t<img align="center" src="'+ image +'" onLoad="opener.OpenSmartImageResizer(this.width, this.height);" ondblclick="window.close();" alt="'+ imageAlt +'" title="'+ imageAlt +'">\n');
	ImageWindow.document.write('\t\t</div>\n');
	ImageWindow.document.write('\t</body>\n');
	ImageWindow.document.write('</html>\n');
	ImageWindow.document.close();

	if (!ImageWindow.opener)
		ImageWindow.opener = this;
	return void(0);
}

function OpenSmartImageResizer(w, h){
	ImageWindow.resizeTo(w + 12, h + 61);
}

//Funzione per il debug: elenca tutte le proprieta' dell'oggetto passato come parametro
function displayproperties(obj){
	var w = window.open("", "properties","left=350, top=300, width=400,height=350, scrollbars=yes, menubar=no, toolsbar=no, status=no, resizable")
	var prop;
	var i=0;
	var str="<table border=\"1\" style=\"font:11px Courier New;\">";
	
	for (prop in obj){
		i +=1;
		str += "<tr>\n<td>\n\t" + i + "\n</td>\n<td>\n\t" + prop + "\n</td>\n<td><code>" + eval("obj." + prop) + "</code></td>\n</tr>\n";
	}
	str += "</table>"
	w.document.open();
	w.document.write(str);
	w.document.close();
}

var initWidth, initHeight;
// evento chiamato sull'onload dalla funzione ResizeWindow
function Fit() {
	window.resizeBy(MaxLayerWidth() - initWidth, MaxLayerHeight() - initHeight);
}
// ridimensiona la finestra partendo dalle dimensioni iniziali in input
function ResizeWindow(w, h) {
	initWidth = w, initHeight = h;
	window.moveTo(0, 0)
	window.onload = Fit
}

// restituisce il numero della versione dell'explorer
function getMSIEBrowserVersion() {
	try {
		var browser = navigator.appVersion.split(';')[1];
		if (browser.indexOf('MSIE') == -1)
			return -1;
		return parseInt(browser.substr(browser.lastIndexOf(' ')))
	} catch (e) {
		return -1;
	}
}
var MSIEVersione = getMSIEBrowserVersion();




