
  //////////////
 // CWndPopups
//
function CWndPopups()
{
	this.active = null;
	this.opened = null;

	this.over 	= function (popup) { this.active = popup; };
	this.out 	= function ( )     { this.active = null; };
	this.open   = function (popup) { if (this.opened != null && this.opened != popup) this.opened.close(); this.opened = popup; };
	this.close 	= function ( )     { if (this.active == null && this.opened != null) this.opened.close(); };
}
WndPopup = new CWndPopups();


  ///////////////
 // CWndDialogs
//
function CWndDialogs()
{
	this.opened = null;

	this.focus = function()
	{
		try
		{
			if (this.opened != null && this.opened.hWnd != null) 
			{
				// Chiamata ricorsiva
				if ( this.opened.hWnd.WndDialog.focus() )		
					return true;
				// o focus sul dialogo corrente
				this.opened.hWnd.focus(); 
				return true; 
			} 
			return false; 
		}
		catch(e)
		{}
		return false;
	}
	
	this.open = function (dlg) { if (this.opened != null && this.opened != dlg) this.opened.close(); this.opened = dlg; };
	this.close = function () { this.opened = null; };
}
WndDialog = new CWndDialogs();




/********************************* EVENTS ************************************/

function BODY_onmousedown(e)
{
	if ( IE )
	{
		e = window.event;
		if ( WndDialog.focus() )
		{
			e.returnValue = false;
			return;
		}		
		if ( e.altKey || e.ctrlKey || e.shiftKey )
			return; 
		if ( e.button == 1 )
			WndPopup.close();
/*			
		if ( !IsEmpty(e.srcElement.tagName) )
			if ( e.srcElement.tagName.search(/INPUT/i) == 0 || e.srcElement.tagName.search(/TEXTAREA/i) == 0 )
				return;

		e.returnValue = false;
*/		
	}
	if ( NE )
	{
		if ( WndDialog.focus() )
		{
			e.preventDefault(); 
			return;
		}		
		if (e.altKey || e.ctrlKey || e.shiftKey)
			return;
		if ( e.which == 1 ) 
			WndPopup.close();
/*
		if ( !IsEmpty(e.target.tagName) )
			if ( e.target.tagName.search(/INPUT/i) == 0 || e.target.tagName.search(/TEXTAREA/i) == 0 )
				return;
		
		e.preventDefault();
*/		
	}
}


function BODY_onmousemove(e)
{
	if ( IE )
	{
		e = window.event;
		if ( WndDialog.focus() )
		{
			e.returnValue = false;
			return;
		}		
		if ( e.altKey || e.ctrlKey || e.shiftKey )
			return; 
		if ( e.button == 1 )
			WndPopup.close();
/*
		if ( !IsEmpty(e.srcElement.tagName) )
			if ( e.srcElement.tagName.search(/INPUT/i) == 0 || e.srcElement.tagName.search(/TEXTAREA/i) == 0 )
				return;

		e.returnValue = false;
*/		
	}
	else
	{
		if ( WndDialog.focus() )
		{
			e.preventDefault(); 
			return;
		}		
		if (e.altKey || e.ctrlKey || e.shiftKey)
			return;
		if ( e.which == 1 ) 
			WndPopup.close();
/*		
		if ( !IsEmpty(e.target.tagName) )
			if ( e.target.tagName.search(/INPUT/i) == 0 || e.target.tagName.search(/TEXTAREA/i) == 0 )
				return;
		
		e.preventDefault(); 
*/		
	}
}


function BODY_onmouseup(e)
{
	if ( IE )
	{
		e = window.event;
		if ( WndDialog.focus() )
		{
			e.returnValue = false;
			return;
		}		
		if ( e.altKey || e.ctrlKey || e.shiftKey )
			return; 
		if ( e.button == 1 )
			WndPopup.close();
/*
		if ( !IsEmpty(e.srcElement.tagName) )
			if ( e.srcElement.tagName.search(/INPUT/i) == 0 || e.srcElement.tagName.search(/TEXTAREA/i) == 0 )
				return;
*/
		e.returnValue = false;
	}
	else
	{
		if ( WndDialog.focus() )
		{
			e.preventDefault(); 
			return;
		}		
		if (e.altKey || e.ctrlKey || e.shiftKey)
			return;
		if ( e.which == 1 ) 
			WndPopup.close();
/*
		if ( !IsEmpty(e.target.tagName) )
			if ( e.target.tagName.search(/INPUT/i) == 0 || e.target.tagName.search(/TEXTAREA/i) == 0 )
				return;
		
		e.preventDefault(); 
*/		
	}
}


function NoSelection_onmouseup(e)
{
	if ( IE )
	{
		e = window.event;
		if ( WndDialog.focus() )
		{
			e.returnValue = false;
			return;
		}		
		if ( e.altKey || e.ctrlKey || e.shiftKey )
			return; 
		if ( e.button == 1 )
			WndPopup.close();

		if ( !IsEmpty(e.srcElement.tagName) )
			if ( e.srcElement.tagName.search(/INPUT/i) == 0 || e.srcElement.tagName.search(/TEXTAREA/i) == 0 )
				return;

		document.selection.empty();
		e.returnValue = false;
	}
	else
	{
		if ( WndDialog.focus() )
		{
			e.preventDefault(); 
			return;
		}		
		if (e.altKey || e.ctrlKey || e.shiftKey)
			return;
		if ( e.which == 1 ) 
			WndPopup.close();
			
		if ( !IsEmpty(e.target.tagName) )
			if ( e.target.tagName.search(/INPUT/i) == 0 || e.target.tagName.search(/TEXTAREA/i) == 0 )
				return;
		
		e.preventDefault(); 
		
	}
}

/*
function EVENTS_onselectstart(e)
{
	if ( IE )
	{
		var e = window.event;
		if ( e.altKey || e.ctrlKey || e.shiftKey )
			return true; 
		if ( !IsEmpty(e.srcElement.tagName) )
			if ( e.srcElement.tagName.search(/INPUT/i) == 0 || e.srcElement.tagName.search(/TEXTAREA/i) == 0 )
				return true;
		document.selection.empty();
		return false;
	}
}

function BODY_onselectstart(e)
{
	if ( IE )
	{
		e = window.event;
		
		if ( WndDialog.focus() )
		{
			e.returnValue = false;
			return;
		}		
		if ( e.altKey || e.ctrlKey || e.shiftKey )
			return; 
		if ( !IsEmpty(e.srcElement.tagName) )
			if ( e.srcElement.tagName.search(/INPUT/i) == 0 || e.srcElement.tagName.search(/TEXTAREA/i) == 0 )
				return;

		e.returnValue = false;
	}
}


function EVENT_onselectstart(e)
{
	var e = window.event;
	if ( e.altKey || e.ctrlKey || e.shiftKey )
		return true; 
	if ( !IsEmpty(e.srcElement.tagName) )
		if ( e.srcElement.tagName.search(/INPUT/i) == 0 || e.srcElement.tagName.search(/TEXTAREA/i) == 0 )
			return true;
	document.selection.empty();
	return false;
}

if ( NE )
{
//	document.body.onmousemove = function (e) { e.preventDefault() };
//	document.body.onclick = function (e) { };
	document.body.onmouseup = function (e) { if (!e.altKey && !e.ctrlKey && !e.shiftKey) e.preventDefault() };
}

if ( IE )
{
//	document.body.onmousemove = function () { if ( window.event.altKey || window.event.ctrlKey || window.event.shiftKey ); else window.event.returnValue = false; };
	document.body.oncontextmenu = function () { if (!window.event.altKey && !window.event.ctrlKey && !window.event.shiftKey) window.event.returnValue = false; };
		
//	document.body.onmouseup = EVENT_onselectstart;
//	document.body.onselectstart = EVENT_onselectstart; //per consentire il search evidenziato: onselectstart="return false;" sul div che contiene gli oggetti
//	document.body.onselect = BODY_onselectstart;
//	document.body.ondragstart = BODY_onselectstart;
}

*/


function EVENTS_Capture()
{
/*	
	document.body.onmousedown = BODY_onmousedown;
	document.body.onmousemove = BODY_onmousemove;
	document.body.onmouseup = BODY_onmouseup;
*/	
	document.onmousedown = BODY_onmousedown;
	document.onmousemove = BODY_onmousemove;
	document.onmouseup = BODY_onmouseup;
}
EVENTS_Capture();

/******************************** FUNCTIONS **********************************/

function CatchAlert(sender) { window.alert(sender + " - Exception!"); }

function CenterWindow(width, height)
{
	// Resizing solo con dimensioni specificate
	if ( arguments.length < 2 )
	{
		height = window.height;
		width = window.width;
	}
	else
		window.resizeTo(width, height);

	var x = (window.screen.width - width) / 2;
	var y = (window.screen.height - height) / 2;
	window.moveTo(x, y);
}

function WindowPos(item)
{
	var pos = [0,0];
	do
	{
		pos[0] += item.offsetLeft;
		pos[1] += item.offsetTop;
		item = item.offsetParent;
	} 
	while (item != null);
	return pos;
}

